mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
- feat: Optimize tenancy software architecture.
This commit is contained in:
@@ -74,7 +74,7 @@ export default class Bill extends mixin(TenantModel, [CachableModel]) {
|
||||
* @return {Array}
|
||||
*/
|
||||
static async getNotFoundBills(billsIds, vendorId) {
|
||||
const storedBills = await this.tenant().query()
|
||||
const storedBills = await this.query()
|
||||
.onBuild((builder) => {
|
||||
builder.whereIn('id', billsIds);
|
||||
|
||||
@@ -94,8 +94,7 @@ export default class Bill extends mixin(TenantModel, [CachableModel]) {
|
||||
|
||||
static changePaymentAmount(billId, amount) {
|
||||
const changeMethod = amount > 0 ? 'increment' : 'decrement';
|
||||
return this.tenant()
|
||||
.query()
|
||||
return this.query()
|
||||
.where('id', billId)
|
||||
[changeMethod]('payment_amount', Math.abs(amount));
|
||||
}
|
||||
|
||||
@@ -33,10 +33,9 @@ export default class Customer extends TenantModel {
|
||||
* @param {Numeric} amount
|
||||
*/
|
||||
static async changeBalance(customerId, amount) {
|
||||
const changeMethod = amount > 0 ? 'increment' : 'decrement';
|
||||
const changeMethod = (amount > 0) ? 'increment' : 'decrement';
|
||||
|
||||
await this.tenant()
|
||||
.query()
|
||||
return this.query()
|
||||
.where('id', customerId)
|
||||
[changeMethod]('balance', Math.abs(amount));
|
||||
}
|
||||
@@ -47,8 +46,7 @@ export default class Customer extends TenantModel {
|
||||
* @param {Integer} amount
|
||||
*/
|
||||
static async incrementBalance(customerId, amount) {
|
||||
await this.tenant()
|
||||
.query()
|
||||
return this.query()
|
||||
.where('id', customerId)
|
||||
.increment('balance', amount);
|
||||
}
|
||||
@@ -59,9 +57,25 @@ export default class Customer extends TenantModel {
|
||||
* @param {integer} amount -
|
||||
*/
|
||||
static async decrementBalance(customerId, amount) {
|
||||
await this.tenant()
|
||||
.query()
|
||||
await this.query()
|
||||
.where('id', customerId)
|
||||
.decrement('balance', amount);
|
||||
}
|
||||
|
||||
static changeDiffBalance(customerId, oldCustomerId, amount, oldAmount) {
|
||||
const diffAmount = amount - oldAmount;
|
||||
const asyncOpers = [];
|
||||
|
||||
if (customerId != oldCustomerId) {
|
||||
const oldCustomerOper = this.changeBalance(oldCustomerId, (oldAmount * -1));
|
||||
const customerOper = this.changeBalance(customerId, amount);
|
||||
|
||||
asyncOpers.push(customerOper);
|
||||
asyncOpers.push(oldCustomerOper);
|
||||
} else {
|
||||
const balanceChangeOper = this.changeBalance(customerId, diffAmount);
|
||||
asyncOpers.push(balanceChangeOper);
|
||||
}
|
||||
return Promise.all(asyncOpers);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,13 +17,12 @@ export default class ItemEntry extends TenantModel {
|
||||
return ['created_at', 'updated_at'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
return {
|
||||
static get virtualAttributes() {
|
||||
return ['amount'];
|
||||
}
|
||||
|
||||
};
|
||||
static amount() {
|
||||
return this.calcAmount(this);
|
||||
}
|
||||
|
||||
static calcAmount(itemEntry) {
|
||||
|
||||
@@ -119,8 +119,7 @@ export default class SaleInvoice extends mixin(TenantModel, [CachableModel]) {
|
||||
static async changePaymentAmount(invoiceId, amount) {
|
||||
const changeMethod = amount > 0 ? 'increment' : 'decrement';
|
||||
|
||||
await this.tenant()
|
||||
.query()
|
||||
await this.query()
|
||||
.where('id', invoiceId)
|
||||
[changeMethod]('payment_amount', Math.abs(amount));
|
||||
}
|
||||
|
||||
@@ -25,8 +25,7 @@ export default class Vendor extends TenantModel {
|
||||
static async changeBalance(vendorId, amount) {
|
||||
const changeMethod = amount > 0 ? 'increment' : 'decrement';
|
||||
|
||||
return this.tenant()
|
||||
.query()
|
||||
return this.query()
|
||||
.where('id', vendorId)
|
||||
[changeMethod]('balance', Math.abs(amount));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user