mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
fix: sync inventory items quantity with services.
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
import { Container } from 'typedi';
|
||||
import { EventSubscriber, On } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import ItemsEntriesService from 'services/Items/ItemsEntriesService';
|
||||
|
||||
@EventSubscriber()
|
||||
export default class BillSubscriber {
|
||||
tenancy: TenancyService;
|
||||
logger: any;
|
||||
itemsEntriesService: ItemsEntriesService;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor() {
|
||||
this.tenancy = Container.get(TenancyService);
|
||||
this.logger = Container.get('logger');
|
||||
this.itemsEntriesService = Container.get(ItemsEntriesService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the sale invoice items once the invoice created.
|
||||
*/
|
||||
@On(events.bill.onCreated)
|
||||
public async handleDecrementSaleInvoiceItemsQuantity({ tenantId, bill }) {
|
||||
await this.itemsEntriesService.incrementItemsEntries(
|
||||
tenantId,
|
||||
bill.entries
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the sale invoice items once the invoice deleted.
|
||||
*/
|
||||
@On(events.bill.onDeleted)
|
||||
public async handleIncrementSaleInvoiceItemsQuantity({ tenantId, oldBill }) {
|
||||
await this.itemsEntriesService.decrementItemsQuantity(
|
||||
tenantId,
|
||||
oldBill.entries
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle increment/decrement the different items quantity once the sale invoice be edited.
|
||||
*/
|
||||
@On(events.bill.onEdited)
|
||||
public async handleChangeSaleInvoiceItemsQuantityOnEdit({
|
||||
tenantId,
|
||||
bill,
|
||||
oldBill,
|
||||
}) {
|
||||
await this.itemsEntriesService.changeItemsQuantity(
|
||||
tenantId,
|
||||
bill.entries,
|
||||
oldBill.entries
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,21 @@ import { EventSubscriber, On } from 'event-dispatch';
|
||||
import { map, head } from 'lodash';
|
||||
import events from 'subscribers/events';
|
||||
import SaleInvoicesCost from 'services/Sales/SalesInvoicesCost';
|
||||
import InventoryItemsQuantitySync from 'services/Inventory/InventoryItemsQuantitySync';
|
||||
import { InventoryTransaction } from 'models';
|
||||
|
||||
@EventSubscriber()
|
||||
export class InventorySubscriber {
|
||||
depends: number = 0;
|
||||
startingDate: Date;
|
||||
saleInvoicesCost: SaleInvoicesCost;
|
||||
|
||||
itemsQuantitySync: InventoryItemsQuantitySync;
|
||||
agenda: any;
|
||||
|
||||
constructor() {
|
||||
this.saleInvoicesCost = Container.get(SaleInvoicesCost);
|
||||
this.itemsQuantitySync = Container.get(InventoryItemsQuantitySync);
|
||||
this.agenda = Container.get('agenda');
|
||||
}
|
||||
|
||||
@@ -30,7 +35,7 @@ export class InventorySubscriber {
|
||||
if (dependsComputeJobs.length === 0) {
|
||||
this.startingDate = null;
|
||||
|
||||
await this.saleInvoicesCost.scheduleWriteJournalEntries(
|
||||
await this.saleInvoicesCost.scheduleWriteJournalEntries(
|
||||
tenantId,
|
||||
startingDate
|
||||
);
|
||||
@@ -38,12 +43,46 @@ export class InventorySubscriber {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Sync inventory items quantity once inventory transactions created.
|
||||
*/
|
||||
@On(events.inventory.onInventoryTransactionsCreated)
|
||||
async syncItemsQuantityOnceInventoryTransactionsCreated({
|
||||
tenantId,
|
||||
inventoryTransactions,
|
||||
}) {
|
||||
const itemsQuantityChanges = this.itemsQuantitySync.getItemsQuantityChanges(
|
||||
inventoryTransactions
|
||||
);
|
||||
await this.itemsQuantitySync.changeItemsQuantity(
|
||||
tenantId,
|
||||
itemsQuantityChanges
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync inventory items quantity once inventory transactions deleted.
|
||||
*/
|
||||
@On(events.inventory.onInventoryTransactionsDeleted)
|
||||
async syncItemsQuantityOnceInventoryTransactionsDeleted({
|
||||
tenantId,
|
||||
oldInventoryTransactions,
|
||||
}) {
|
||||
const itemsQuantityChanges = this.itemsQuantitySync.getReverseItemsQuantityChanges(
|
||||
oldInventoryTransactions
|
||||
);
|
||||
await this.itemsQuantitySync.changeItemsQuantity(
|
||||
tenantId,
|
||||
itemsQuantityChanges
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@On(events.inventory.onInventoryTransactionsCreated)
|
||||
async handleScheduleItemsCostOnInventoryTransactionsCreated({
|
||||
tenantId,
|
||||
inventoryTransactions
|
||||
inventoryTransactions,
|
||||
}) {
|
||||
const inventoryItemsIds = map(inventoryTransactions, 'itemId');
|
||||
|
||||
@@ -61,7 +100,7 @@ export class InventorySubscriber {
|
||||
tenantId,
|
||||
transactionType,
|
||||
transactionId,
|
||||
oldInventoryTransactions
|
||||
oldInventoryTransactions,
|
||||
}) {
|
||||
// Ignore compute item cost with theses transaction types.
|
||||
const ignoreWithTransactionTypes = ['OpeningItem'];
|
||||
|
||||
@@ -33,7 +33,7 @@ export default class InventoryAdjustmentsSubscriber {
|
||||
await this.inventoryAdjustment.writeInventoryTransactions(
|
||||
tenantId,
|
||||
inventoryAdjustment
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,10 +43,10 @@ export default class InventoryAdjustmentsSubscriber {
|
||||
async handleRevertInventoryTransactionsOnceDeleted({
|
||||
tenantId,
|
||||
inventoryAdjustmentId,
|
||||
oldInventoryTransaction,
|
||||
oldInventoryAdjustment,
|
||||
}) {
|
||||
// Can't continue if the inventory adjustment is not published.
|
||||
if (!oldInventoryTransaction.isPublished) { return; }
|
||||
if (!oldInventoryAdjustment.isPublished) { return; }
|
||||
|
||||
await this.inventoryAdjustment.revertInventoryTransactions(
|
||||
tenantId,
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import { Container } from 'typedi';
|
||||
import { On, EventSubscriber } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import ItemsEntriesService from 'services/Items/ItemsEntriesService';
|
||||
|
||||
@EventSubscriber()
|
||||
export default class SyncItemsQuantityWithInvoices {
|
||||
logger: any;
|
||||
tenancy: TenancyService;
|
||||
itemsEntriesService: ItemsEntriesService;
|
||||
|
||||
/**
|
||||
* Constructor method.
|
||||
*/
|
||||
constructor() {
|
||||
this.logger = Container.get('logger');
|
||||
this.tenancy = Container.get(TenancyService);
|
||||
this.itemsEntriesService = Container.get(ItemsEntriesService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the sale invoice items once the invoice created.
|
||||
*/
|
||||
@On(events.saleInvoice.onCreated)
|
||||
public async handleDecrementSaleInvoiceItemsQuantity({
|
||||
tenantId,
|
||||
saleInvoice,
|
||||
}) {
|
||||
await this.itemsEntriesService.decrementItemsQuantity(
|
||||
tenantId,
|
||||
saleInvoice.entries
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the sale invoice items once the invoice deleted.
|
||||
*/
|
||||
@On(events.saleInvoice.onDeleted)
|
||||
public async handleIncrementSaleInvoiceItemsQuantity({
|
||||
tenantId,
|
||||
oldSaleInvoice,
|
||||
}) {
|
||||
await this.itemsEntriesService.incrementItemsEntries(
|
||||
tenantId,
|
||||
oldSaleInvoice.entries
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle increment/decrement the different items quantity once the sale invoice be edited.
|
||||
*/
|
||||
@On(events.saleInvoice.onEdited)
|
||||
public async handleChangeSaleInvoiceItemsQuantityOnEdit({
|
||||
tenantId,
|
||||
saleInvoice,
|
||||
oldSaleInvoice,
|
||||
}) {
|
||||
await this.itemsEntriesService.changeItemsQuantity(
|
||||
tenantId,
|
||||
saleInvoice.entries.map((entry) => ({
|
||||
...entry,
|
||||
quantity: entry.quantity * -1,
|
||||
})),
|
||||
oldSaleInvoice.entries.map((entry) => ({
|
||||
...entry,
|
||||
quantity: entry.quantity * -1,
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
import { Container } from 'typedi';
|
||||
import { On, EventSubscriber } from 'event-dispatch';
|
||||
import events from 'subscribers/events';
|
||||
import TenancyService from 'services/Tenancy/TenancyService';
|
||||
import ItemsEntriesService from 'services/Items/ItemsEntriesService';
|
||||
import SalesInvoicesCost from 'services/Sales/SalesInvoicesCost';
|
||||
|
||||
@EventSubscriber()
|
||||
export default class SaleReceiptSubscriber {
|
||||
logger: any;
|
||||
tenancy: TenancyService;
|
||||
itemsEntriesService: ItemsEntriesService;
|
||||
|
||||
constructor() {
|
||||
this.logger = Container.get('logger');
|
||||
this.tenancy = Container.get(TenancyService);
|
||||
this.itemsEntriesService = Container.get(ItemsEntriesService);
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the sale receipt items once be created.
|
||||
*/
|
||||
@On(events.saleReceipt.onCreated)
|
||||
public async handleDecremenReceiptItemsQuantity({ tenantId, saleReceipt }) {
|
||||
await this.itemsEntriesService.decrementItemsQuantity(
|
||||
tenantId,
|
||||
saleReceipt.entries
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrements the sale receipt items once be deleted.
|
||||
*/
|
||||
@On(events.saleReceipt.onDeleted)
|
||||
public async handleIncrementReceiptItemsQuantity({
|
||||
tenantId,
|
||||
oldSaleReceipt,
|
||||
}) {
|
||||
await this.itemsEntriesService.incrementItemsEntries(
|
||||
tenantId,
|
||||
oldSaleReceipt.entries
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle increment/decrement the different items quantity once
|
||||
* the sale receipt be edited.
|
||||
*/
|
||||
@On(events.saleReceipt.onEdited)
|
||||
public async handleChangeSaleInvoiceItemsQuantityOnEdit({
|
||||
tenantId,
|
||||
saleReceipt,
|
||||
oldSaleReceipt,
|
||||
}) {
|
||||
await this.itemsEntriesService.changeItemsQuantity(
|
||||
tenantId,
|
||||
saleReceipt.entries.map((entry) => ({
|
||||
...entry,
|
||||
quantity: entry.quantity * -1,
|
||||
})),
|
||||
oldSaleReceipt.entries.map((entry) => ({
|
||||
...entry,
|
||||
quantity: entry.quantity * -1,
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user