fix: issue in sales invoices and bills.

This commit is contained in:
Ahmed Bouhuolia
2020-10-25 19:58:32 +02:00
parent 5e615e57c3
commit ca6acf6517
8 changed files with 64 additions and 34 deletions

View File

@@ -146,17 +146,14 @@ export default class BillPaymentsService {
* @param {Response} res
* @param {NextFunction} next
*/
private async validateBillsExistance(tenantId: number, billPaymentEntries: IBillPaymentEntry[], notVendorId?: number) {
private async validateBillsExistance(tenantId: number, billPaymentEntries: IBillPaymentEntry[], vendorId: number) {
const { Bill } = this.tenancy.models(tenantId);
const entriesBillsIds = billPaymentEntries.map((e: any) => e.billId);
const storedBills = await Bill.query().onBuild((builder) => {
builder.whereIn('id', entriesBillsIds);
const storedBills = await Bill.query()
.whereIn('id', entriesBillsIds)
.where('vendor_id', vendorId);
if (notVendorId) {
builder.where('vendor_id', notVendorId);
}
});
const storedBillsIds = storedBills.map((t: IBill) => t.id);
const notFoundBillsIds = difference(entriesBillsIds, storedBillsIds);
@@ -252,14 +249,24 @@ export default class BillPaymentsService {
amount: sumBy(billPaymentDTO.entries, 'paymentAmount'),
...formatDateFields(billPaymentDTO, ['paymentDate']),
};
// Validate vendor existance on the storage.
await this.getVendorOrThrowError(tenantId, billPaymentObj.vendorId);
// Validate the payment account existance and type.
await this.getPaymentAccountOrThrowError(tenantId, billPaymentObj.paymentAccountId);
// Validate the payment number uniquiness.
await this.validatePaymentNumber(tenantId, billPaymentObj.paymentNumber);
await this.validateBillsExistance(tenantId, billPaymentObj.entries);
// Validates the bills existance and associated to the given vendor.
await this.validateBillsExistance(tenantId, billPaymentObj.entries, billPaymentDTO.vendorId);
// Validates the bills due payment amount.
await this.validateBillsDueAmount(tenantId, billPaymentObj.entries);
const billPayment = await BillPayment.query()
.insertGraph({
.insertGraphAndFetch({
...omit(billPaymentObj, ['entries']),
entries: billPaymentDTO.entries,
});
@@ -304,14 +311,23 @@ export default class BillPaymentsService {
...formatDateFields(billPaymentDTO, ['paymentDate']),
};
// Validate vendor existance on the storage.
await this.getVendorOrThrowError(tenantId, billPaymentObj.vendorId);
// Validate the payment account existance and type.
await this.getPaymentAccountOrThrowError(tenantId, billPaymentObj.paymentAccountId);
// Validate the items entries IDs existance on the storage.
await this.validateEntriesIdsExistance(tenantId, billPaymentId, billPaymentObj.entries);
await this.validateBillsExistance(tenantId, billPaymentObj.entries);
// Validate the bills existance and associated to the given vendor.
await this.validateBillsExistance(tenantId, billPaymentObj.entries, billPaymentDTO.vendorId);
// Validates the bills due payment amount.
await this.validateBillsDueAmount(tenantId, billPaymentObj.entries);
const billPayment = await BillPayment.query()
.upsertGraph({
.upsertGraphAndFetch({
id: billPaymentId,
...omit(billPaymentObj, ['entries']),
entries: billPaymentDTO.entries,

View File

@@ -135,13 +135,15 @@ export default class PaymentReceiveService {
* @param {number} tenantId -
* @param {} paymentReceiveEntries -
*/
async validateInvoicesIDsExistance(tenantId: number, paymentReceiveEntries: any): Promise<void> {
async validateInvoicesIDsExistance(tenantId: number, customerId: number, paymentReceiveEntries: IPaymentReceiveEntryDTO[]): Promise<void> {
const { SaleInvoice } = this.tenancy.models(tenantId);
const invoicesIds = paymentReceiveEntries.map((e) => e.invoiceId);
const storedInvoices = await SaleInvoice.query().whereIn('id', invoicesIds);
const storedInvoicesIds = storedInvoices.map((invoice) => invoice.id);
const invoicesIds = paymentReceiveEntries.map((e: IPaymentReceiveEntryDTO) => e.invoiceId);
const storedInvoices = await SaleInvoice.query()
.whereIn('id', invoicesIds)
.where('customer_id', customerId);
const storedInvoicesIds = storedInvoices.map((invoice) => invoice.id);
const notFoundInvoicesIDs = difference(invoicesIds, storedInvoicesIds);
if (notFoundInvoicesIDs.length > 0) {
@@ -200,7 +202,7 @@ export default class PaymentReceiveService {
await this.getDepositAccountOrThrowError(tenantId, paymentReceiveDTO.depositAccountId);
// Validate payment receive invoices IDs existance.
await this.validateInvoicesIDsExistance(tenantId, paymentReceiveDTO.entries);
await this.validateInvoicesIDsExistance(tenantId, paymentReceiveDTO.customerId, paymentReceiveDTO.entries);
// Validate invoice payment amount.
await this.validateInvoicesPaymentsAmount(tenantId, paymentReceiveDTO.entries);
@@ -265,8 +267,8 @@ export default class PaymentReceiveService {
await this.itemsEntries.validateEntriesIdsExistance(
tenantId, paymentReceiveId, 'PaymentReceive', paymentReceiveDTO.entries
);
// Validate payment receive invoices IDs existance.
await this.validateInvoicesIDsExistance(tenantId, paymentReceiveDTO.entries);
// Validate payment receive invoices IDs existance and associated to the given customer id.
await this.validateInvoicesIDsExistance(tenantId, paymentReceiveDTO.customerId, paymentReceiveDTO.entries);
// Validate invoice payment amount.
await this.validateInvoicesPaymentsAmount(tenantId, paymentReceiveDTO.entries);
@@ -300,16 +302,16 @@ export default class PaymentReceiveService {
* @param {Integer} paymentReceiveId - Payment receive id.
* @param {IPaymentReceive} paymentReceive - Payment receive object.
*/
async deletePaymentReceive(tenantId: number, paymentReceiveId: number, paymentReceive: any) {
async deletePaymentReceive(tenantId: number, paymentReceiveId: number) {
const { PaymentReceive, PaymentReceiveEntry } = this.tenancy.models(tenantId);
const oldPaymentReceive = this.getPaymentReceiveOrThrowError(tenantId, paymentReceiveId);
const oldPaymentReceive = await this.getPaymentReceiveOrThrowError(tenantId, paymentReceiveId);
// Deletes the payment receive associated entries.
await PaymentReceiveEntry.query().where('payment_receive_id', paymentReceiveId).delete();
// Deletes the payment receive transaction.
await PaymentReceive.query().where('id', paymentReceiveId).delete();
await PaymentReceive.query().findById(paymentReceiveId).delete();
await this.eventDispatcher.dispatch(events.paymentReceipts.onDeleted, {
tenantId, paymentReceiveId, oldPaymentReceive,

View File

@@ -97,7 +97,7 @@ export default class SalesReceiptService {
this.logger.info('[sale_receipt] trying to insert sale receipt graph.', { tenantId, saleReceiptDTO });
const saleReceipt = await SaleReceipt.query()
.insertGraph({
.insertGraphAndFetch({
...omit(saleReceiptObj, ['entries']),
entries: saleReceiptObj.entries.map((entry) => ({
@@ -133,7 +133,7 @@ export default class SalesReceiptService {
await this.itemsEntriesService.validateNonSellableEntriesItems(tenantId, saleReceiptDTO.entries);
const saleReceipt = await SaleReceipt.query()
.upsertGraph({
.upsertGraphAndFetch({
id: saleReceiptId,
...omit(saleReceiptObj, ['entries']),