Files
bigcapital/server/src/models/Bill.js
Ahmed Bouhuolia ad00f140d1 feat: licenses administration basic authentication.
feat: accounts slug.
feat: duplicate accounts_balance table and merge balance with accounts table.
feat: refactoring customers and vendors.
feat: system user soft deleting.
feat: preventing build tenant database without any subscription.
feat: remove 'password' property from 'req.user' object.
feat: refactoring JournalPoster class.
feat: delete duplicated directories and files.
2020-09-09 21:30:19 +02:00

98 lines
2.1 KiB
JavaScript

import { Model } from 'objection';
import { difference } from 'lodash';
import TenantModel from '@/models/TenantModel';
export default class Bill extends TenantModel {
/**
* Virtual attributes.
*/
static get virtualAttributes() {
return ['dueAmount'];
}
/**
* Table name
*/
static get tableName() {
return 'bills';
}
/**
* Timestamps columns.
*/
get timestamps() {
return ['createdAt', 'updatedAt'];
}
/**
* Due amount of the given.
* @return {number}
*/
get dueAmount() {
return this.amount - this.paymentAmount;
}
/**
* 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',
},
filter(builder) {
builder.where('reference_type', 'SaleReceipt');
},
},
};
}
/**
* 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, vendorId) {
const storedBills = await this.query()
.onBuild((builder) => {
builder.whereIn('id', billsIds);
if (vendorId) {
builder.where('vendor_id', vendorId);
}
});
const storedBillsIds = storedBills.map((t) => t.id);
const notFoundBillsIds = difference(
billsIds,
storedBillsIds,
);
return notFoundBillsIds;
}
static changePaymentAmount(billId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';
return this.query()
.where('id', billId)
[changeMethod]('payment_amount', Math.abs(amount));
}
}