feat: Concurrency control items cost compute.

This commit is contained in:
Ahmed Bouhuolia
2020-08-23 23:38:42 +02:00
parent 45088b2d3b
commit ab6bc0517f
28 changed files with 463 additions and 341 deletions

View File

@@ -3,6 +3,7 @@ import moment from 'moment';
import TenantModel from '@/models/TenantModel';
import CachableQueryBuilder from '@/lib/Cachable/CachableQueryBuilder';
import CachableModel from '@/lib/Cachable/CachableModel';
import InventoryCostLotTracker from './InventoryCostLotTracker';
export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
/**
@@ -26,6 +27,26 @@ export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
return ['created_at', 'updated_at'];
}
/**
* Model modifiers.
*/
static get modifiers() {
return {
filterDateRange(query, startDate, endDate, type = 'day') {
const dateFormat = 'YYYY-MM-DD HH:mm:ss';
const fromDate = moment(startDate).startOf(type).format(dateFormat);
const toDate = moment(endDate).endOf(type).format(dateFormat);
if (startDate) {
query.where('invoice_date', '>=', fromDate);
}
if (endDate) {
query.where('invoice_date', '<=', toDate);
}
},
};
}
/**
* Due amount of the given.
*/
@@ -40,6 +61,7 @@ export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
const AccountTransaction = require('@/models/AccountTransaction');
const ItemEntry = require('@/models/ItemEntry');
const Customer = require('@/models/Customer');
const InventoryCostLotTracker = require('@/models/InventoryCostLotTracker');
return {
entries: {
@@ -73,6 +95,18 @@ export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
filter(builder) {
builder.where('reference_type', 'SaleInvoice');
},
},
costTransactions: {
relation: Model.HasManyRelation,
modelClass: this.relationBindKnex(InventoryCostLotTracker.default),
join: {
from: 'sales_invoices.id',
to: 'inventory_cost_lot_tracker.transactionId'
},
filter(builder) {
builder.where('transaction_type', 'SaleInvoice');
},
}
};
}