fix: make unique number of sales and purchases transactions is optional.

This commit is contained in:
Ahmed Bouhuolia
2020-10-26 14:32:35 +02:00
parent 3446dba0c5
commit 4ed96e5143
6 changed files with 42 additions and 20 deletions

View File

@@ -124,7 +124,7 @@ export default class BillPaymentsService {
* @param {string} paymentMadeNumber -
* @return {Promise<IBillPayment>}
*/
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,

View File

@@ -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);