- feat: remove unnecessary migrations, controllers and models files.

- feat: metable store
- feat: metable store with settings store.
- feat: settings middleware to auto-save and load.
- feat: DI db manager to master container.
- feat: write some logs to sale invoices.
This commit is contained in:
Ahmed Bouhuolia
2020-09-03 16:51:48 +02:00
parent abefba22ee
commit 9ee7ed89ec
98 changed files with 1697 additions and 2052 deletions

View File

@@ -27,6 +27,9 @@ export default class PaymentReceiveService {
@Inject()
journalService: JournalPosterService;
@Inject('logger')
logger: any;
/**
* Creates a new payment receive and store it to the storage
* with associated invoices payment and journal transactions.
@@ -43,6 +46,8 @@ export default class PaymentReceiveService {
} = this.tenancy.models(tenantId);
const paymentAmount = sumBy(paymentReceive.entries, 'payment_amount');
this.logger.info('[payment_receive] inserting to the storage.');
const storedPaymentReceive = await PaymentReceive.query()
.insert({
amount: paymentAmount,
@@ -50,12 +55,15 @@ export default class PaymentReceiveService {
});
const storeOpers: Array<any> = [];
this.logger.info('[payment_receive] inserting associated entries to the storage.');
paymentReceive.entries.forEach((entry: any) => {
const oper = PaymentReceiveEntry.query()
.insert({
payment_receive_id: storedPaymentReceive.id,
...entry,
});
this.logger.info('[payment_receive] increment the sale invoice payment amount.');
// Increment the invoice payment amount.
const invoice = SaleInvoice.query()
.where('id', entry.invoice_id)
@@ -64,6 +72,8 @@ export default class PaymentReceiveService {
storeOpers.push(oper);
storeOpers.push(invoice);
});
this.logger.info('[payment_receive] decrementing customer balance.');
const customerIncrementOper = Customer.decrementBalance(
paymentReceive.customer_id,
paymentAmount,

View File

@@ -16,6 +16,9 @@ export default class SaleEstimateService {
@Inject()
itemsEntriesService: HasItemsEntries;
@Inject('logger')
logger: any;
/**
* Creates a new estimate with associated entries.
* @async
@@ -31,12 +34,15 @@ export default class SaleEstimateService {
amount,
...formatDateFields(estimateDTO, ['estimate_date', 'expiration_date']),
};
this.logger.info('[sale_estimate] inserting sale estimate to the storage.');
const storedEstimate = await SaleEstimate.query()
.insert({
...omit(estimate, ['entries']),
});
const storeEstimateEntriesOpers: any[] = [];
this.logger.info('[sale_estimate] inserting sale estimate entries to the storage.');
estimate.entries.forEach((entry: any) => {
const oper = ItemEntry.query()
.insert({
@@ -48,6 +54,8 @@ export default class SaleEstimateService {
});
await Promise.all([...storeEstimateEntriesOpers]);
this.logger.info('[sale_estimate] insert sale estimated success.');
return storedEstimate;
}
@@ -67,6 +75,7 @@ export default class SaleEstimateService {
amount,
...formatDateFields(estimateDTO, ['estimate_date', 'expiration_date']),
};
this.logger.info('[sale_estimate] editing sale estimate on the storage.');
const updatedEstimate = await SaleEstimate.query()
.update({
...omit(estimate, ['entries']),
@@ -96,14 +105,14 @@ export default class SaleEstimateService {
*/
async deleteEstimate(tenantId: number, estimateId: number) {
const { SaleEstimate, ItemEntry } = this.tenancy.models(tenantId);
this.logger.info('[sale_estimate] delete sale estimate and associated entries from the storage.');
await ItemEntry.query()
.where('reference_id', estimateId)
.where('reference_type', 'SaleEstimate')
.delete();
await SaleEstimate.query()
.where('id', estimateId)
.delete();
await SaleEstimate.query().where('id', estimateId).delete();
}
/**
@@ -113,10 +122,10 @@ export default class SaleEstimateService {
* @param {Numeric} estimateId
* @return {Boolean}
*/
async isEstimateExists(estimateId: number) {
async isEstimateExists(tenantId: number, estimateId: number) {
const { SaleEstimate } = this.tenancy.models(tenantId);
const foundEstimate = await SaleEstimate.query()
.where('id', estimateId);
const foundEstimate = await SaleEstimate.query().where('id', estimateId);
return foundEstimate.length !== 0;
}
@@ -192,7 +201,6 @@ export default class SaleEstimateService {
const foundEstimates = await SaleEstimate.query()
.onBuild((query: any) => {
query.where('estimate_number', estimateNumber);
if (excludeEstimateId) {
query.whereNot('id', excludeEstimateId);
}

View File

@@ -23,6 +23,9 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
@Inject()
itemsEntriesService: HasItemsEntries;
@Inject('logger')
logger: any;
/**
* Creates a new sale invoices and store it to the storage
* with associated to entries and journal transactions.
@@ -43,12 +46,15 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
paymentAmount: 0,
invLotNumber,
};
this.logger.info('[sale_invoice] inserting sale invoice to the storage.');
const storedInvoice = await SaleInvoice.query()
.insert({
...omit(saleInvoice, ['entries']),
});
const opers: Array<any> = [];
this.logger.info('[sale_invoice] inserting sale invoice entries to the storage.');
saleInvoice.entries.forEach((entry: any) => {
const oper = ItemEntry.query()
.insertAndFetch({
@@ -61,15 +67,16 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
opers.push(oper);
});
this.logger.info('[sale_invoice] trying to increment the customer balance.');
// Increment the customer balance after deliver the sale invoice.
const incrementOper = Customer.incrementBalance(
saleInvoice.customer_id,
balance,
);
// Await all async operations.
await Promise.all([
...opers, incrementOper,
]);
await Promise.all([ ...opers, incrementOper ]);
// Records the inventory transactions for inventory items.
await this.recordInventoryTranscactions(tenantId, saleInvoice, storedInvoice.id);
@@ -100,6 +107,8 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
balance,
invLotNumber: oldSaleInvoice.invLotNumber,
};
this.logger.info('[sale_invoice] trying to update sale invoice.');
const updatedSaleInvoices: ISaleInvoice = await SaleInvoice.query()
.where('id', saleInvoiceId)
.update({
@@ -114,6 +123,8 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
const patchItemsEntriesOper = this.itemsEntriesService.patchItemsEntries(
tenantId, saleInvoice.entries, storedEntries, 'SaleInvoice', saleInvoiceId,
);
this.logger.info('[sale_invoice] change customer different balance.');
// Changes the diff customer balance between old and new amount.
const changeCustomerBalanceOper = Customer.changeDiffBalance(
saleInvoice.customer_id,
@@ -155,12 +166,14 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
.findById(saleInvoiceId)
.withGraphFetched('entries');
this.logger.info('[sale_invoice] delete sale invoice with entries.');
await SaleInvoice.query().where('id', saleInvoiceId).delete();
await ItemEntry.query()
.where('reference_id', saleInvoiceId)
.where('reference_type', 'SaleInvoice')
.delete();
this.logger.info('[sale_invoice] revert the customer balance.');
const revertCustomerBalanceOper = Customer.changeBalance(
oldSaleInvoice.customerId,
oldSaleInvoice.balance * -1,
@@ -203,7 +216,13 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
* @param {number} saleInvoiceId -
* @param {boolean} override -
*/
recordInventoryTranscactions(tenantId: number, saleInvoice, saleInvoiceId: number, override?: boolean){
recordInventoryTranscactions(
tenantId: number,
saleInvoice,
saleInvoiceId: number,
override?: boolean
){
this.logger.info('[sale_invoice] saving inventory transactions');
const inventortyTransactions = saleInvoice.entries
.map((entry) => ({
...pick(entry, ['item_id', 'quantity', 'rate',]),
@@ -228,6 +247,8 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
async revertInventoryTransactions(tenantId: number, inventoryTransactions: array) {
const { InventoryTransaction } = this.tenancy.models(tenantId);
const opers: Promise<[]>[] = [];
this.logger.info('[sale_invoice] reverting inventory transactions');
inventoryTransactions.forEach((trans: any) => {
switch(trans.direction) {
@@ -359,7 +380,11 @@ export default class SaleInvoicesService extends SalesInvoicesCost {
* Writes the sale invoice journal entries.
* @param {SaleInvoice} saleInvoice -
*/
async writeNonInventoryInvoiceJournals(tenantId: number, saleInvoice: ISaleInvoice, override: boolean) {
async writeNonInventoryInvoiceJournals(
tenantId: number,
saleInvoice: ISaleInvoice,
override: boolean
) {
const { Account, AccountTransaction } = this.tenancy.models(tenantId);
const accountsDepGraph = await Account.depGraph().query();