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

View File

@@ -19,40 +19,27 @@ export default class BillPayment extends mixin(TenantModel, [CachableModel]) {
return ['createdAt', 'updatedAt'];
}
/**
* Extend query builder model.
*/
static get QueryBuilder() {
return CachableQueryBuilder;
}
static changePaymentAmount(billId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';
return this.tenant()
.query()
.where('id', billId)
[changeMethod]('payment_amount', Math.abs(amount));
}
/**
* Relationship mapping.
*/
static get relationMappings() {
const BillPaymentEntry = require('@/models/BillPaymentEntry');
const Vendor = require('@/models/Vendor');
const Account = require('@/models/Account');
return {
/**
*
*/
entries: {
relation: Model.BelongsToOneRelation,
relation: Model.HasManyRelation,
modelClass: this.relationBindKnex(BillPaymentEntry.default),
join: {
from: 'bills_payments.id',
to: 'bills_payments_entries.billPaymentId',
},
},
/**
*
*/
@@ -63,7 +50,19 @@ export default class BillPayment extends mixin(TenantModel, [CachableModel]) {
from: 'bills_payments.vendorId',
to: 'vendors.id',
},
}
},
/**
*
*/
paymentAccount: {
relation: Model.BelongsToOneRelation,
modelClass: this.relationBindKnex(Account.default),
join: {
from: 'bills_payments.paymentAccountId',
to: 'accounts.id',
},
},
};
}
}

View File

@@ -13,6 +13,6 @@ export default class BillPaymentEntry extends TenantModel {
* Timestamps columns.
*/
get timestamps() {
return ['createdAt', 'updatedAt'];
return [];
}
}

View File

@@ -38,8 +38,12 @@ export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
*/
static get relationMappings() {
const ItemEntry = require('@/models/ItemEntry');
const Customer = require('@/models/Customer');
return {
/**
*
*/
entries: {
relation: Model.HasManyRelation,
modelClass: this.relationBindKnex(ItemEntry.default),
@@ -48,6 +52,18 @@ export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
to: 'items_entries.referenceId',
},
},
/**
*
*/
customer: {
relation: Model.BelongsToOneRelation,
modelClass: this.relationBindKnex(Customer.default),
join: {
from: 'sales_invoices.customerId',
to: 'customers.id',
},
},
};
}
@@ -64,4 +80,6 @@ export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
.where('id', invoiceId)
[changeMethod]('payment_amount', Math.abs(amount));
}
}