From 4ed96e5143b55581b8db65c85297158dc1498aec Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Mon, 26 Oct 2020 14:32:35 +0200 Subject: [PATCH] fix: make unique number of sales and purchases transactions is optional. --- server/src/services/Purchases/BillPayments.ts | 11 ++++++++--- server/src/services/Purchases/Bills.ts | 12 ++++++++++-- server/src/services/Sales/PaymentsReceives.ts | 10 ++++++---- server/src/services/Sales/SalesEstimate.ts | 10 ++++++---- server/src/services/Sales/SalesInvoices.ts | 10 ++++++---- server/src/services/Sales/SalesReceipts.ts | 9 ++++++--- 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/server/src/services/Purchases/BillPayments.ts b/server/src/services/Purchases/BillPayments.ts index f60c671af..edbba8562 100644 --- a/server/src/services/Purchases/BillPayments.ts +++ b/server/src/services/Purchases/BillPayments.ts @@ -124,7 +124,7 @@ export default class BillPaymentsService { * @param {string} paymentMadeNumber - * @return {Promise} */ - private async validatePaymentNumber(tenantId: number, paymentMadeNumber: string, notPaymentMadeId?: string) { + private async validatePaymentNumber(tenantId: number, paymentMadeNumber: string, notPaymentMadeId?: number) { const { BillPayment } = this.tenancy.models(tenantId); const foundBillPayment = await BillPayment.query() @@ -259,8 +259,9 @@ export default class BillPaymentsService { await this.getPaymentAccountOrThrowError(tenantId, billPaymentObj.paymentAccountId); // Validate the payment number uniquiness. - await this.validatePaymentNumber(tenantId, billPaymentObj.paymentNumber); - + if (billPaymentObj.paymentNumber) { + await this.validatePaymentNumber(tenantId, billPaymentObj.paymentNumber); + } // Validates the bills existance and associated to the given vendor. await this.validateBillsExistance(tenantId, billPaymentObj.entries, billPaymentDTO.vendorId); @@ -328,6 +329,10 @@ export default class BillPaymentsService { // Validates the bills due payment amount. await this.validateBillsDueAmount(tenantId, billPaymentObj.entries); + // Validate the payment number uniquiness. + if (billPaymentObj.paymentNumber) { + await this.validatePaymentNumber(tenantId, billPaymentObj.paymentNumber, billPaymentId); + } const billPayment = await BillPayment.query() .upsertGraphAndFetch({ id: billPaymentId, diff --git a/server/src/services/Purchases/Bills.ts b/server/src/services/Purchases/Bills.ts index 9adc2ff60..3c9060b2c 100644 --- a/server/src/services/Purchases/Bills.ts +++ b/server/src/services/Purchases/Bills.ts @@ -181,9 +181,13 @@ export default class BillsService extends SalesInvoicesCost { this.logger.info('[bill] trying to create a new bill', { tenantId, billDTO }); const billObj = await this.billDTOToModel(tenantId, billDTO); + // Retrieve vendor or throw not found service error. await this.getVendorOrThrowError(tenantId, billDTO.vendorId); - await this.validateBillNumberExists(tenantId, billDTO.billNumber); + // Validate the bill number uniqiness on the storage. + if (billDTO.billNumber) { + await this.validateBillNumberExists(tenantId, billDTO.billNumber); + } // Validate items IDs existance. await this.itemsEntriesService.validateItemsIdsExistance(tenantId, billDTO.entries); @@ -237,9 +241,13 @@ export default class BillsService extends SalesInvoicesCost { const oldBill = await this.getBillOrThrowError(tenantId, billId); const billObj = await this.billDTOToModel(tenantId, billDTO, oldBill); + // Retrieve vendor details or throw not found service error. await this.getVendorOrThrowError(tenantId, billDTO.vendorId); - await this.validateBillNumberExists(tenantId, billDTO.billNumber, billId); + // Validate bill number uniqiness on the storage. + if (billDTO.billNumber) { + await this.validateBillNumberExists(tenantId, billDTO.billNumber, billId); + } await this.itemsEntriesService.validateEntriesIdsExistance(tenantId, billId, 'Bill', billDTO.entries); await this.itemsEntriesService.validateItemsIdsExistance(tenantId, billDTO.entries); await this.itemsEntriesService.validateNonPurchasableEntriesItems(tenantId, billDTO.entries); diff --git a/server/src/services/Sales/PaymentsReceives.ts b/server/src/services/Sales/PaymentsReceives.ts index 21d5ce44f..a77bd4163 100644 --- a/server/src/services/Sales/PaymentsReceives.ts +++ b/server/src/services/Sales/PaymentsReceives.ts @@ -193,8 +193,9 @@ export default class PaymentReceiveService { const paymentAmount = sumBy(paymentReceiveDTO.entries, 'paymentAmount'); // Validate payment receive number uniquiness. - await this.validatePaymentReceiveNoExistance(tenantId, paymentReceiveDTO.paymentReceiveNo); - + if (paymentReceiveDTO.paymentReceiveNo) { + await this.validatePaymentReceiveNoExistance(tenantId, paymentReceiveDTO.paymentReceiveNo); + } // Validate customer existance. await this.customersService.getCustomerByIdOrThrowError(tenantId, paymentReceiveDTO.customerId); @@ -255,8 +256,9 @@ export default class PaymentReceiveService { const oldPaymentReceive = await this.getPaymentReceiveOrThrowError(tenantId, paymentReceiveId); // Validate payment receive number uniquiness. - await this.validatePaymentReceiveNoExistance(tenantId, paymentReceiveDTO.paymentReceiveNo, paymentReceiveId); - + if (paymentReceiveDTO.paymentReceiveNo) { + await this.validatePaymentReceiveNoExistance(tenantId, paymentReceiveDTO.paymentReceiveNo, paymentReceiveId); + } // Validate the deposit account existance and type. this.getDepositAccountOrThrowError(tenantId, paymentReceiveDTO.depositAccountId); diff --git a/server/src/services/Sales/SalesEstimate.ts b/server/src/services/Sales/SalesEstimate.ts index 8ade46b2d..e534c37d0 100644 --- a/server/src/services/Sales/SalesEstimate.ts +++ b/server/src/services/Sales/SalesEstimate.ts @@ -99,8 +99,9 @@ export default class SaleEstimateService { }; // Validate estimate number uniquiness on the storage. - await this.validateEstimateNumberExistance(tenantId, estimateDTO.estimateNumber); - + if (estimateDTO.estimateNumber) { + await this.validateEstimateNumberExistance(tenantId, estimateDTO.estimateNumber); + } // Retrieve the given customer or throw not found service error. await this.customersService.getCustomer(tenantId, estimateDTO.customerId); @@ -146,8 +147,9 @@ export default class SaleEstimateService { }; // Validate estimate number uniquiness on the storage. - await this.validateEstimateNumberExistance(tenantId, estimateDTO.estimateNumber, estimateId); - + if (estimateDTO.estimateNumber) { + await this.validateEstimateNumberExistance(tenantId, estimateDTO.estimateNumber, estimateId); + } // Retrieve the given customer or throw not found service error. await this.customersService.getCustomer(tenantId, estimateDTO.customerId); diff --git a/server/src/services/Sales/SalesInvoices.ts b/server/src/services/Sales/SalesInvoices.ts index 01c690881..d1bd86970 100644 --- a/server/src/services/Sales/SalesInvoices.ts +++ b/server/src/services/Sales/SalesInvoices.ts @@ -126,8 +126,9 @@ export default class SaleInvoicesService extends SalesInvoicesCost { await this.customersService.getCustomerByIdOrThrowError(tenantId, saleInvoiceDTO.customerId); // Validate sale invoice number uniquiness. - await this.validateInvoiceNumberUnique(tenantId, saleInvoiceDTO.invoiceNo); - + if (saleInvoiceDTO.invoiceNo) { + await this.validateInvoiceNumberUnique(tenantId, saleInvoiceDTO.invoiceNo); + } // Validate items ids existance. await this.itemsEntriesService.validateItemsIdsExistance(tenantId, saleInvoiceDTO.entries); await this.itemsEntriesService.validateNonSellableEntriesItems(tenantId, saleInvoiceDTO.entries); @@ -174,8 +175,9 @@ export default class SaleInvoicesService extends SalesInvoicesCost { await this.customersService.getCustomerByIdOrThrowError(tenantId, saleInvoiceDTO.customerId); // Validate sale invoice number uniquiness. - await this.validateInvoiceNumberUnique(tenantId, saleInvoiceDTO.invoiceNo, saleInvoiceId); - + if (saleInvoiceDTO.invoiceNo) { + await this.validateInvoiceNumberUnique(tenantId, saleInvoiceDTO.invoiceNo, saleInvoiceId); + } // Validate items ids existance. await this.itemsEntriesService.validateItemsIdsExistance(tenantId, saleInvoiceDTO.entries); diff --git a/server/src/services/Sales/SalesReceipts.ts b/server/src/services/Sales/SalesReceipts.ts index f90b0eae0..36f89f7f1 100644 --- a/server/src/services/Sales/SalesReceipts.ts +++ b/server/src/services/Sales/SalesReceipts.ts @@ -127,8 +127,9 @@ export default class SalesReceiptService { await this.itemsEntriesService.validateNonSellableEntriesItems(tenantId, saleReceiptDTO.entries); // Validate sale receipt number uniuqiness. - await this.validateReceiptNumberUnique(tenantId, saleReceiptDTO.receiptNumber); - + if (saleReceiptDTO.receiptNumber) { + await this.validateReceiptNumberUnique(tenantId, saleReceiptDTO.receiptNumber); + } this.logger.info('[sale_receipt] trying to insert sale receipt graph.', { tenantId, saleReceiptDTO }); const saleReceipt = await SaleReceipt.query() .insertGraphAndFetch({ @@ -173,7 +174,9 @@ export default class SalesReceiptService { await this.itemsEntriesService.validateNonSellableEntriesItems(tenantId, saleReceiptDTO.entries); // Validate sale receipt number uniuqiness. - await this.validateReceiptNumberUnique(tenantId, saleReceiptDTO.receiptNumber, saleReceiptId); + if (saleReceiptDTO.receiptNumber) { + await this.validateReceiptNumberUnique(tenantId, saleReceiptDTO.receiptNumber, saleReceiptId); + } const saleReceipt = await SaleReceipt.query() .upsertGraphAndFetch({