Files
bigcapital/server/src/models/Bill.js
Ahmed Bouhuolia 0114ed9f8b fix: database migrations FK relations.
fix: database columns indexing.
2020-10-03 12:08:11 +02:00

101 lines
2.2 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 Contact = require('models/Contact');
const ItemEntry = require('models/ItemEntry');
return {
vendor: {
relation: Model.BelongsToOneRelation,
modelClass: Contact.default,
join: {
from: 'bills.vendorId',
to: 'contacts.id',
},
filter(query) {
query.where('contact_type', 'Vendor');
}
},
entries: {
relation: Model.HasManyRelation,
modelClass: 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));
}
}