fix: database migrations FK relations.

fix: database columns indexing.
This commit is contained in:
Ahmed Bouhuolia
2020-10-03 12:08:11 +02:00
parent 1250eccc0d
commit 0114ed9f8b
86 changed files with 788 additions and 801 deletions

View File

@@ -36,17 +36,20 @@ export default class Bill extends TenantModel {
* Relationship mapping.
*/
static get relationMappings() {
const Vendor = require('models/Vendor');
const Contact = require('models/Contact');
const ItemEntry = require('models/ItemEntry');
return {
vendor: {
relation: Model.BelongsToOneRelation,
modelClass: Vendor.default,
modelClass: Contact.default,
join: {
from: 'bills.vendorId',
to: 'vendors.id',
to: 'contacts.id',
},
filter(query) {
query.where('contact_type', 'Vendor');
}
},
entries: {

View File

@@ -22,7 +22,7 @@ export default class BillPayment extends TenantModel {
static get relationMappings() {
const BillPaymentEntry = require('models/BillPaymentEntry');
const AccountTransaction = require('models/AccountTransaction');
const Vendor = require('models/Vendor');
const Contact = require('models/Contact');
const Account = require('models/Account');
return {
@@ -37,11 +37,14 @@ export default class BillPayment extends TenantModel {
vendor: {
relation: Model.BelongsToOneRelation,
modelClass: Vendor.default,
modelClass: Contact.default,
join: {
from: 'bills_payments.vendorId',
to: 'vendors.id',
to: 'contacts.id',
},
filter(query) {
query.where('contact_type', 'Vendor');
}
},
paymentAccount: {

View File

@@ -1,81 +0,0 @@
import { Model } from 'objection';
import TenantModel from 'models/TenantModel';
export default class Customer extends TenantModel {
/**
* Table name
*/
static get tableName() {
return 'customers';
}
/**
* Model timestamps.
*/
get timestamps() {
return ['createdAt', 'updatedAt'];
}
/**
* Model modifiers.
*/
static get modifiers() {
return {
filterCustomerIds(query, customerIds) {
query.whereIn('id', customerIds);
},
};
}
/**
* Change vendor balance.
* @param {Integer} customerId
* @param {Numeric} amount
*/
static async changeBalance(customerId, amount) {
const changeMethod = (amount > 0) ? 'increment' : 'decrement';
return this.query()
.where('id', customerId)
[changeMethod]('balance', Math.abs(amount));
}
/**
* Increment the given customer balance.
* @param {Integer} customerId
* @param {Integer} amount
*/
static async incrementBalance(customerId, amount) {
return this.query()
.where('id', customerId)
.increment('balance', amount);
}
/**
* Decrement the given customer balance.
* @param {integer} customerId -
* @param {integer} amount -
*/
static async decrementBalance(customerId, amount) {
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);
}
}

View File

@@ -22,17 +22,20 @@ export default class PaymentReceive extends TenantModel {
static get relationMappings() {
const PaymentReceiveEntry = require('models/PaymentReceiveEntry');
const AccountTransaction = require('models/AccountTransaction');
const Customer = require('models/Customer');
const Contact = require('models/Contact');
const Account = require('models/Account');
return {
customer: {
relation: Model.BelongsToOneRelation,
modelClass: Customer.default,
modelClass: Contact.default,
join: {
from: 'payment_receives.customerId',
to: 'customers.id',
to: 'contacts.id',
},
filter(query) {
query.where('contact_type', 'Customer');
}
},
depositAccount: {

View File

@@ -21,16 +21,19 @@ export default class SaleEstimate extends TenantModel {
*/
static get relationMappings() {
const ItemEntry = require('models/ItemEntry');
const Customer = require('models/Customer');
const Contact = require('models/Contact');
return {
customer: {
relation: Model.BelongsToOneRelation,
modelClass: Customer.default,
modelClass: Contact.default,
join: {
from: 'sales_estimates.customerId',
to: 'customers.id',
to: 'contacts.id',
},
filter(query) {
query.where('contact_type', 'Customer');
}
},
entries: {

View File

@@ -57,7 +57,7 @@ export default class SaleInvoice extends TenantModel {
static get relationMappings() {
const AccountTransaction = require('models/AccountTransaction');
const ItemEntry = require('models/ItemEntry');
const Customer = require('models/Customer');
const Contact = require('models/Contact');
const InventoryCostLotTracker = require('models/InventoryCostLotTracker');
return {
@@ -75,11 +75,14 @@ export default class SaleInvoice extends TenantModel {
customer: {
relation: Model.BelongsToOneRelation,
modelClass: Customer.default,
modelClass: Contact.default,
join: {
from: 'sales_invoices.customerId',
to: 'customers.id',
to: 'contacts.id',
},
filter(query) {
query.where('contact_type', 'Customer');
}
},
transactions: {

View File

@@ -20,7 +20,7 @@ export default class SaleReceipt extends TenantModel {
* Relationship mapping.
*/
static get relationMappings() {
const Customer = require('models/Customer');
const Contact = require('models/Contact');
const Account = require('models/Account');
const AccountTransaction = require('models/AccountTransaction');
const ItemEntry = require('models/ItemEntry');
@@ -28,11 +28,14 @@ export default class SaleReceipt extends TenantModel {
return {
customer: {
relation: Model.BelongsToOneRelation,
modelClass: Customer.default,
modelClass: Contact.default,
join: {
from: 'sales_receipts.customerId',
to: 'customers.id',
to: 'contacts.id',
},
filter(query) {
query.where('contact_type', 'Customer');
}
},
depositAccount: {

View File

@@ -1,61 +0,0 @@
import { Model } from 'objection';
import TenantModel from 'models/TenantModel';
export default class Vendor extends TenantModel {
/**
* Table name
*/
static get tableName() {
return 'vendors';
}
/**
* Model timestamps.
*/
get timestamps() {
return ['createdAt', 'updatedAt'];
}
/**
* Changes the vendor balance.
* @param {Integer} customerId
* @param {Number} amount
* @return {Promise}
*/
static async changeBalance(vendorId, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';
return this.query()
.where('id', vendorId)
[changeMethod]('balance', Math.abs(amount));
}
/**
*
* @param {number} vendorId - Specific vendor id.
* @param {number} oldVendorId - The given old vendor id.
* @param {number} amount - The new change amount.
* @param {number} oldAmount - The old stored amount.
*/
static changeDiffBalance(vendorId, oldVendorId, amount, oldAmount) {
const diffAmount = (amount - oldAmount);
const asyncOpers = [];
if (vendorId != oldVendorId) {
const oldVendorOper = Vendor.changeBalance(
oldVendorId,
(oldAmount * -1)
);
const vendorOper = Vendor.changeBalance(
vendorId,
amount,
);
asyncOpers.push(vendorOper);
asyncOpers.push(oldVendorOper);
} else {
const balanceChangeOper = Vendor.changeBalance(vendorId, diffAmount);
asyncOpers.push(balanceChangeOper);
}
return Promise.all(asyncOpers);
}
}

View File

@@ -1,5 +1,3 @@
import Customer from './Customer';
import Vendor from './Vendor';
import Option from './Option';
import SaleEstimate from './SaleEstimate';
import SaleEstimateEntry from './SaleEstimateEntry';
@@ -22,8 +20,6 @@ import AccountType from './AccountType';
import InventoryLotCostTracker from './InventoryCostLotTracker';
export {
Customer,
Vendor,
SaleEstimate,
SaleEstimateEntry,
SaleReceipt,