fix bugs in sales and purchases API.

This commit is contained in:
Ahmed Bouhuolia
2020-08-04 23:00:15 +02:00
parent db28cd2aef
commit ad772cf247
31 changed files with 420 additions and 819 deletions

View File

@@ -1,5 +1,6 @@
import { Model, mixin } from 'objection';
import moment from 'moment';
import { difference } from 'lodash';
import TenantModel from '@/models/TenantModel';
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
import CachableModel from '@/lib/Cachable/CachableModel';
@@ -29,8 +30,25 @@ export default class Bill extends mixin(TenantModel, [CachableModel]) {
/**
* Due amount of the given.
* @return {number}
*/
get dueAmount() {
return Math.max(this.balance - this.paymentAmount, 0);
return this.amount - this.paymentAmount;
}
/**
* Retrieve the not found bills ids as array.
* @param {Array} billsIds
* @return {Array}
*/
static async getNotFoundBills(billsIds) {
const storedBills = await this.tenant().query().whereIn('id', billsIds);
const storedBillsIds = storedBills.map((t) => t.id);
const notFoundBillsIds = difference(
billsIds,
storedBillsIds,
);
return notFoundBillsIds;
}
}

View File

@@ -39,10 +39,11 @@ export default class BillPayment extends mixin(TenantModel, [CachableModel]) {
*/
static get relationMappings() {
const BillPaymentEntry = require('@/models/BillPaymentEntry');
const Vendor = require('@/models/Vendor');
return {
/**
* Account model may belongs to account type.
*
*/
entries: {
relation: Model.BelongsToOneRelation,
@@ -52,6 +53,17 @@ export default class BillPayment extends mixin(TenantModel, [CachableModel]) {
to: 'bills_payments_entries.billPaymentId',
},
},
/**
*
*/
vendor: {
relation: Model.BelongsToOneRelation,
modelClass: this.relationBindKnex(Vendor.default),
join: {
from: 'bills_payments.vendorId',
to: 'vendors.id',
},
}
};
}
}