fix issues in sales and purchases module.

This commit is contained in:
Ahmed Bouhuolia
2020-08-05 16:25:53 +02:00
parent ad772cf247
commit 57ec5bdfbe
12 changed files with 236 additions and 62 deletions

View File

@@ -37,12 +37,55 @@ export default class Bill extends mixin(TenantModel, [CachableModel]) {
}
/**
* Retrieve the not found bills ids as array.
* Relationship mapping.
*/
static get relationMappings() {
const Vendor = require('@/models/Vendor');
const ItemEntry = require('@/models/ItemEntry');
return {
/**
*
*/
vendor: {
relation: Model.BelongsToOneRelation,
modelClass: this.relationBindKnex(Vendor.default),
join: {
from: 'bills.vendorId',
to: 'vendors.id',
},
},
/**
*
*/
entries: {
relation: Model.HasManyRelation,
modelClass: this.relationBindKnex(ItemEntry.default),
join: {
from: 'bills.id',
to: 'items_entries.referenceId',
},
},
};
}
/**
* Retrieve the not found bills ids as array that associated to the given vendor.
* @param {Array} billsIds
* @param {number} vendorId -
* @return {Array}
*/
static async getNotFoundBills(billsIds) {
const storedBills = await this.tenant().query().whereIn('id', billsIds);
static async getNotFoundBills(billsIds, vendorId) {
const storedBills = await this.tenant().query()
.onBuild((builder) => {
builder.whereIn('id', billsIds);
if (vendorId) {
builder.where('vendor_id', vendorId);
}
});
const storedBillsIds = storedBills.map((t) => t.id);
const notFoundBillsIds = difference(
@@ -51,4 +94,12 @@ export default class Bill extends mixin(TenantModel, [CachableModel]) {
);
return notFoundBillsIds;
}
static changePaymentAmount(billId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';
return this.tenant()
.query()
.where('id', billId)
[changeMethod]('payment_amount', Math.abs(amount));
}
}