From 8d4b3f1ab3371a6ab41f36be9105ed9a5e4f22c8 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Wed, 5 Aug 2020 21:01:45 +0200 Subject: [PATCH] fix issues in sales and purchases API module. --- .../controllers/Purchases/BillsPayments.ts | 25 +++++++++++++++++-- server/src/models/Vendor.js | 13 +++++++--- server/src/services/Purchases/BillPayments.js | 6 ++--- server/src/services/Purchases/Bills.js | 15 ++++++++--- server/src/services/Sales/PaymentsReceives.ts | 6 ++--- server/src/services/Sales/SalesInvoices.ts | 1 + 6 files changed, 52 insertions(+), 14 deletions(-) diff --git a/server/src/http/controllers/Purchases/BillsPayments.ts b/server/src/http/controllers/Purchases/BillsPayments.ts index f08db0e45..521377e04 100644 --- a/server/src/http/controllers/Purchases/BillsPayments.ts +++ b/server/src/http/controllers/Purchases/BillsPayments.ts @@ -157,10 +157,18 @@ export default class BillsPayments extends BaseController { * @param {Function} res */ static async validatePaymentNumber(req: Request, res: Response, next: any) { - const { BillPayment } = req.models; const billPayment = { ...req.body }; + const { id: billPaymentId } = req.params; + const { BillPayment } = req.models; + const foundBillPayment = await BillPayment.query() - .where('payment_number', billPayment.payment_number) + .onBuild((builder: any) => { + builder.where('payment_number', billPayment.payment_number) + + if (billPaymentId) { + builder.whereNot('id', billPaymentId); + } + }) .first(); if (foundBillPayment) { @@ -283,7 +291,20 @@ export default class BillsPayments extends BaseController { */ static async editBillPayment(req: Request, res: Response) { const billPayment = { ...req.body }; + const { id: billPaymentId } = req.params; + const { BillPayment } = req.models; + + const oldBillPayment = await BillPayment.query() + .where('id', billPaymentId) + .withGraphFetched('entries') + .first(); + + await BillPaymentsService.editBillPayment( + billPaymentId, + billPayment, + oldBillPayment, + ); return res.status(200).send({ id: 1 }); } diff --git a/server/src/models/Vendor.js b/server/src/models/Vendor.js index 035a6fd67..2c162b522 100644 --- a/server/src/models/Vendor.js +++ b/server/src/models/Vendor.js @@ -31,18 +31,25 @@ export default class Vendor extends TenantModel { [changeMethod]('balance', Math.abs(amount)); } + /** + * + * @param {number} vendorId - Specific vendor id. + * @param {number} oldVendorId - The given old vendor id. + * @param {number} amount - The new change amount. + * @param {number} oldAmount - The old stored amount. + */ static changeDiffBalance(vendorId, oldVendorId, amount, oldAmount) { - const diffAmount = (amount - oldAmount) * -1; + const diffAmount = (amount - oldAmount); const asyncOpers = []; if (vendorId != oldVendorId) { const oldVendorOper = Vendor.changeBalance( oldVendorId, - oldAmount + (oldAmount * -1) ); const vendorOper = Vendor.changeBalance( vendorId, - (amount + diffAmount) * -1 + amount, ); asyncOpers.push(vendorOper); asyncOpers.push(oldVendorOper); diff --git a/server/src/services/Purchases/BillPayments.js b/server/src/services/Purchases/BillPayments.js index 49d7e8478..a72727f93 100644 --- a/server/src/services/Purchases/BillPayments.js +++ b/server/src/services/Purchases/BillPayments.js @@ -145,9 +145,9 @@ export default class BillPaymentsService { // Change the different vendor balance between the new and old one. const changeDiffBalance = Vendor.changeDiffBalance( billPayment.vendor_id, - oldBillPayment.vendor_id, - billPayment.amount, - oldBillPayment.amount + oldBillPayment.vendorId, + billPayment.amount * -1, + oldBillPayment.amount * -1, ); await Promise.all([ ...opers, diff --git a/server/src/services/Purchases/Bills.js b/server/src/services/Purchases/Bills.js index bf5ddd857..9c9019b9b 100644 --- a/server/src/services/Purchases/Bills.js +++ b/server/src/services/Purchases/Bills.js @@ -79,10 +79,11 @@ export default class BillsService { * - Re-write the inventory transactions. * - Re-write the bill journal transactions. * - * @param {Integer} billId - * @param {IBill} bill + * @param {Integer} billId - The given bill id. + * @param {IBill} bill - The given new bill details. */ static async editBill(billId, bill) { + const oldBill = await Bill.tenant().query().findById(billId); const amount = sumBy(bill.entries, 'amount'); // Update the bill transaction. @@ -104,12 +105,20 @@ export default class BillsService { const patchEntriesOper = HasItemsEntries.patchItemsEntries( bill.entries, storedEntries, 'Bill', billId, ); + // Changes the diff vendor balance between old and new amount. + const changeVendorBalanceOper = Vendor.changeDiffBalance( + bill.vendor_id, + oldBill.vendorId, + amount, + oldBill.amount, + ); // Record bill journal transactions. const recordTransactionsOper = this.recordJournalTransactions(bill, billId); await Promise.all([ patchEntriesOper, recordTransactionsOper, + changeVendorBalanceOper, ]); } @@ -249,7 +258,7 @@ export default class BillsService { 'Bill' ); // Revert vendor balance. - const revertVendorBalance = Vendor.changeBalance(billId, bill.amount * -1); + const revertVendorBalance = Vendor.changeBalance(bill.vendorId, bill.amount * -1); await Promise.all([ deleteBillOper, diff --git a/server/src/services/Sales/PaymentsReceives.ts b/server/src/services/Sales/PaymentsReceives.ts index 29e4c1cad..1cd6ed9e9 100644 --- a/server/src/services/Sales/PaymentsReceives.ts +++ b/server/src/services/Sales/PaymentsReceives.ts @@ -100,8 +100,8 @@ export default class PaymentReceiveService { ...omit(paymentReceive, ['entries']), }); const opers = []; - const entriesIds = paymentReceive.entries.filter((i) => i.id); - const entriesShouldInsert = paymentReceive.entries.filter((i) => !i.id); + const entriesIds = paymentReceive.entries.filter((i: any) => i.id); + const entriesShouldInsert = paymentReceive.entries.filter((i: any) => !i.id); // Detarmines which entries ids should be deleted. const entriesIdsShouldDelete = ServiceItemsEntries.entriesShouldDeleted( @@ -142,7 +142,7 @@ export default class PaymentReceiveService { paymentReceive.customer_id, oldPaymentReceive.customerId, paymentAmount * -1, - oldPaymentReceive.amount, + oldPaymentReceive.amount * -1, ); // Change the difference between the old and new invoice payment amount. const diffInvoicePaymentAmount = this.saveChangeInvoicePaymentAmount( diff --git a/server/src/services/Sales/SalesInvoices.ts b/server/src/services/Sales/SalesInvoices.ts index ab67dd7a8..b0d7abaa3 100644 --- a/server/src/services/Sales/SalesInvoices.ts +++ b/server/src/services/Sales/SalesInvoices.ts @@ -43,6 +43,7 @@ export default class SaleInvoicesService { }); opers.push(oper); }); + // Increment the customer balance after deliver the sale invoice. const incrementOper = Customer.incrementBalance( saleInvoice.customer_id, balance,