WIP: Allocate landed cost.

This commit is contained in:
a.bouhuolia
2021-07-24 03:10:32 +02:00
parent 70aea9bf2d
commit cf2ebe9597
30 changed files with 602 additions and 218 deletions

View File

@@ -1,4 +1,5 @@
import { Model } from 'objection';
import { lowerCase } from 'lodash';
import TenantModel from 'models/TenantModel';
export default class BillLandedCost extends TenantModel {
@@ -16,6 +17,25 @@ export default class BillLandedCost extends TenantModel {
return ['createdAt', 'updatedAt'];
}
/**
* Virtual attributes.
*/
static get virtualAttributes() {
return ['allocationMethodFormatted'];
}
/**
* Allocation method formatted.
*/
get allocationMethodFormatted() {
const allocationMethod = lowerCase(this.allocationMethod);
const keyLabelsPairs = {
value: 'Value',
quantity: 'Quantity',
};
return keyLabelsPairs[allocationMethod] || '';
}
/**
* Relationship mapping.
*/

View File

@@ -1,3 +1,4 @@
import { Model } from 'objection';
import TenantModel from 'models/TenantModel';
export default class BillLandedCostEntry extends TenantModel {
@@ -7,4 +8,25 @@ export default class BillLandedCostEntry extends TenantModel {
static get tableName() {
return 'bill_located_cost_entries';
}
/**
* Relationship mapping.
*/
static get relationMappings() {
const ItemEntry = require('models/ItemEntry');
return {
itemEntry: {
relation: Model.BelongsToOneRelation,
modelClass: ItemEntry.default,
join: {
from: 'bill_located_cost_entries.entryId',
to: 'items_entries.referenceId',
},
filter(builder) {
builder.where('reference_type', 'Bill');
},
},
};
}
}

View File

@@ -39,14 +39,18 @@ export default class Expense extends TenantModel {
}
static get virtualAttributes() {
return ['isPublished', 'unallocatedLandedCost'];
return ['isPublished', 'unallocatedCostAmount'];
}
isPublished() {
return Boolean(this.publishedAt);
}
unallocatedLandedCost() {
/**
* Retrieve the unallocated cost amount.
* @return {number}
*/
get unallocatedCostAmount() {
return Math.max(this.amount - this.allocatedCostAmount, 0);
}

View File

@@ -13,14 +13,14 @@ export default class ExpenseCategory extends TenantModel {
* Virtual attributes.
*/
static get virtualAttributes() {
return ['unallocatedLandedCost'];
return ['unallocatedCostAmount'];
}
/**
* Remain unallocated landed cost.
* @return {number}
*/
get unallocatedLandedCost() {
get unallocatedCostAmount() {
return Math.max(this.amount - this.allocatedCostAmount, 0);
}

View File

@@ -21,8 +21,8 @@ export default class ItemEntry extends TenantModel {
return ['amount'];
}
static amount() {
return this.calcAmount(this);
get amount() {
return ItemEntry.calcAmount(this);
}
static calcAmount(itemEntry) {
@@ -34,6 +34,7 @@ export default class ItemEntry extends TenantModel {
static get relationMappings() {
const Item = require('models/Item');
const BillLandedCostEntry = require('models/BillLandedCostEntry');
return {
item: {
@@ -44,6 +45,14 @@ export default class ItemEntry extends TenantModel {
to: 'items.id',
},
},
allocatedCostEntries: {
relation: Model.HasManyRelation,
modelClass: BillLandedCostEntry.default,
join: {
from: 'items_entries.referenceId',
to: 'bill_located_cost_entries.entryId',
},
},
};
}
}