feat: add sale receipt number.

This commit is contained in:
Ahmed Bouhuolia
2020-10-26 13:47:42 +02:00
parent 1c842662ab
commit d0785c65db
5 changed files with 51 additions and 4 deletions

View File

@@ -66,9 +66,6 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
/**
*
* Validate whether sale invoice number unqiue on the storage.
* @param {Request} req
* @param {Response} res
* @param {Function} next
*/
async validateInvoiceNumberUnique(tenantId: number, invoiceNumber: string, notInvoiceId?: number) {
const { SaleInvoice } = this.tenancy.models(tenantId);

View File

@@ -19,6 +19,7 @@ const ERRORS = {
SALE_RECEIPT_NOT_FOUND: 'SALE_RECEIPT_NOT_FOUND',
DEPOSIT_ACCOUNT_NOT_FOUND: 'DEPOSIT_ACCOUNT_NOT_FOUND',
DEPOSIT_ACCOUNT_NOT_CURRENT_ASSET: 'DEPOSIT_ACCOUNT_NOT_CURRENT_ASSET',
SALE_RECEIPT_NUMBER_NOT_UNIQUE: 'SALE_RECEIPT_NUMBER_NOT_UNIQUE'
};
@Service()
export default class SalesReceiptService {
@@ -77,6 +78,30 @@ export default class SalesReceiptService {
}
}
/**
* Validate sale receipt number uniquiness on the storage.
* @param {number} tenantId -
* @param {string} receiptNumber -
* @param {number} notReceiptId -
*/
async validateReceiptNumberUnique(tenantId: number, receiptNumber: string, notReceiptId?: number) {
const { SaleReceipt } = this.tenancy.models(tenantId);
this.logger.info('[sale_receipt] validate receipt number uniquiness.', { tenantId, receiptNumber });
const saleReceipt = await SaleReceipt.query()
.findOne('receipt_number', receiptNumber)
.onBuild((builder) => {
if (notReceiptId) {
builder.whereNot('id', notReceiptId);
}
});
if (saleReceipt) {
this.logger.info('[sale_receipt] sale receipt number not unique.', { tenantId });
throw new ServiceError(ERRORS.SALE_RECEIPT_NUMBER_NOT_UNIQUE);
}
}
/**
* Creates a new sale receipt with associated entries.
* @async
@@ -91,10 +116,19 @@ export default class SalesReceiptService {
amount,
...formatDateFields(saleReceiptDTO, ['receipt_date'])
};
// Validate receipt deposit account existance and type.
await this.validateReceiptDepositAccountExistance(tenantId, saleReceiptDTO.depositAccountId);
// Validate items IDs existance on the storage.
await this.itemsEntriesService.validateItemsIdsExistance(tenantId, saleReceiptDTO.entries);
// Validate the sellable items.
await this.itemsEntriesService.validateNonSellableEntriesItems(tenantId, saleReceiptDTO.entries);
// Validate sale receipt number uniuqiness.
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({
@@ -126,12 +160,21 @@ export default class SalesReceiptService {
amount,
...formatDateFields(saleReceiptDTO, ['receipt_date'])
};
// Retrieve sale receipt or throw not found service error.
const oldSaleReceipt = await this.getSaleReceiptOrThrowError(tenantId, saleReceiptId);
// Validate receipt deposit account existance and type.
await this.validateReceiptDepositAccountExistance(tenantId, saleReceiptDTO.depositAccountId);
// Validate items IDs existance on the storage.
await this.itemsEntriesService.validateItemsIdsExistance(tenantId, saleReceiptDTO.entries);
// Validate the sellable items.
await this.itemsEntriesService.validateNonSellableEntriesItems(tenantId, saleReceiptDTO.entries);
// Validate sale receipt number uniuqiness.
await this.validateReceiptNumberUnique(tenantId, saleReceiptDTO.receiptNumber, saleReceiptId);
const saleReceipt = await SaleReceipt.query()
.upsertGraphAndFetch({
id: saleReceiptId,