fix issues in sales and purchases API module.

This commit is contained in:
Ahmed Bouhuolia
2020-08-05 21:01:45 +02:00
parent 57ec5bdfbe
commit 8d4b3f1ab3
6 changed files with 52 additions and 14 deletions

View File

@@ -157,10 +157,18 @@ export default class BillsPayments extends BaseController {
* @param {Function} res * @param {Function} res
*/ */
static async validatePaymentNumber(req: Request, res: Response, next: any) { static async validatePaymentNumber(req: Request, res: Response, next: any) {
const { BillPayment } = req.models;
const billPayment = { ...req.body }; const billPayment = { ...req.body };
const { id: billPaymentId } = req.params;
const { BillPayment } = req.models;
const foundBillPayment = await BillPayment.query() 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(); .first();
if (foundBillPayment) { if (foundBillPayment) {
@@ -283,7 +291,20 @@ export default class BillsPayments extends BaseController {
*/ */
static async editBillPayment(req: Request, res: Response) { static async editBillPayment(req: Request, res: Response) {
const billPayment = { ...req.body }; 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 }); return res.status(200).send({ id: 1 });
} }

View File

@@ -31,18 +31,25 @@ export default class Vendor extends TenantModel {
[changeMethod]('balance', Math.abs(amount)); [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) { static changeDiffBalance(vendorId, oldVendorId, amount, oldAmount) {
const diffAmount = (amount - oldAmount) * -1; const diffAmount = (amount - oldAmount);
const asyncOpers = []; const asyncOpers = [];
if (vendorId != oldVendorId) { if (vendorId != oldVendorId) {
const oldVendorOper = Vendor.changeBalance( const oldVendorOper = Vendor.changeBalance(
oldVendorId, oldVendorId,
oldAmount (oldAmount * -1)
); );
const vendorOper = Vendor.changeBalance( const vendorOper = Vendor.changeBalance(
vendorId, vendorId,
(amount + diffAmount) * -1 amount,
); );
asyncOpers.push(vendorOper); asyncOpers.push(vendorOper);
asyncOpers.push(oldVendorOper); asyncOpers.push(oldVendorOper);

View File

@@ -145,9 +145,9 @@ export default class BillPaymentsService {
// Change the different vendor balance between the new and old one. // Change the different vendor balance between the new and old one.
const changeDiffBalance = Vendor.changeDiffBalance( const changeDiffBalance = Vendor.changeDiffBalance(
billPayment.vendor_id, billPayment.vendor_id,
oldBillPayment.vendor_id, oldBillPayment.vendorId,
billPayment.amount, billPayment.amount * -1,
oldBillPayment.amount oldBillPayment.amount * -1,
); );
await Promise.all([ await Promise.all([
...opers, ...opers,

View File

@@ -79,10 +79,11 @@ export default class BillsService {
* - Re-write the inventory transactions. * - Re-write the inventory transactions.
* - Re-write the bill journal transactions. * - Re-write the bill journal transactions.
* *
* @param {Integer} billId * @param {Integer} billId - The given bill id.
* @param {IBill} bill * @param {IBill} bill - The given new bill details.
*/ */
static async editBill(billId, bill) { static async editBill(billId, bill) {
const oldBill = await Bill.tenant().query().findById(billId);
const amount = sumBy(bill.entries, 'amount'); const amount = sumBy(bill.entries, 'amount');
// Update the bill transaction. // Update the bill transaction.
@@ -104,12 +105,20 @@ export default class BillsService {
const patchEntriesOper = HasItemsEntries.patchItemsEntries( const patchEntriesOper = HasItemsEntries.patchItemsEntries(
bill.entries, storedEntries, 'Bill', billId, 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. // Record bill journal transactions.
const recordTransactionsOper = this.recordJournalTransactions(bill, billId); const recordTransactionsOper = this.recordJournalTransactions(bill, billId);
await Promise.all([ await Promise.all([
patchEntriesOper, patchEntriesOper,
recordTransactionsOper, recordTransactionsOper,
changeVendorBalanceOper,
]); ]);
} }
@@ -249,7 +258,7 @@ export default class BillsService {
'Bill' 'Bill'
); );
// Revert vendor balance. // Revert vendor balance.
const revertVendorBalance = Vendor.changeBalance(billId, bill.amount * -1); const revertVendorBalance = Vendor.changeBalance(bill.vendorId, bill.amount * -1);
await Promise.all([ await Promise.all([
deleteBillOper, deleteBillOper,

View File

@@ -100,8 +100,8 @@ export default class PaymentReceiveService {
...omit(paymentReceive, ['entries']), ...omit(paymentReceive, ['entries']),
}); });
const opers = []; const opers = [];
const entriesIds = paymentReceive.entries.filter((i) => i.id); const entriesIds = paymentReceive.entries.filter((i: any) => i.id);
const entriesShouldInsert = paymentReceive.entries.filter((i) => !i.id); const entriesShouldInsert = paymentReceive.entries.filter((i: any) => !i.id);
// Detarmines which entries ids should be deleted. // Detarmines which entries ids should be deleted.
const entriesIdsShouldDelete = ServiceItemsEntries.entriesShouldDeleted( const entriesIdsShouldDelete = ServiceItemsEntries.entriesShouldDeleted(
@@ -142,7 +142,7 @@ export default class PaymentReceiveService {
paymentReceive.customer_id, paymentReceive.customer_id,
oldPaymentReceive.customerId, oldPaymentReceive.customerId,
paymentAmount * -1, paymentAmount * -1,
oldPaymentReceive.amount, oldPaymentReceive.amount * -1,
); );
// Change the difference between the old and new invoice payment amount. // Change the difference between the old and new invoice payment amount.
const diffInvoicePaymentAmount = this.saveChangeInvoicePaymentAmount( const diffInvoicePaymentAmount = this.saveChangeInvoicePaymentAmount(

View File

@@ -43,6 +43,7 @@ export default class SaleInvoicesService {
}); });
opers.push(oper); opers.push(oper);
}); });
// Increment the customer balance after deliver the sale invoice.
const incrementOper = Customer.incrementBalance( const incrementOper = Customer.incrementBalance(
saleInvoice.customer_id, saleInvoice.customer_id,
balance, balance,