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
*/
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 });
}

View File

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

View File

@@ -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,

View File

@@ -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,

View File

@@ -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(

View File

@@ -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,