mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
feat(nestjs): migrate to NestJS
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
import { Model } from 'objection';
|
||||
import { castArray } from 'lodash';
|
||||
import * as moment from 'moment';
|
||||
import { unitOfTime } from 'moment';
|
||||
import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice';
|
||||
import { SaleReceipt } from '@/modules/SaleReceipts/models/SaleReceipt';
|
||||
import { Item } from '@/modules/Items/models/Item';
|
||||
import { BaseModel } from '@/models/Model';
|
||||
|
||||
export class InventoryCostLotTracker extends BaseModel {
|
||||
date: Date;
|
||||
direction: string;
|
||||
itemId: number;
|
||||
quantity: number;
|
||||
rate: number;
|
||||
remaining: number;
|
||||
cost: number;
|
||||
transactionType: string;
|
||||
transactionId: number;
|
||||
costAccountId: number;
|
||||
entryId: number;
|
||||
createdAt: Date;
|
||||
|
||||
exchangeRate: number;
|
||||
currencyCode: string;
|
||||
|
||||
warehouseId: number;
|
||||
|
||||
item?: Item;
|
||||
invoice?: SaleInvoice;
|
||||
receipt?: SaleReceipt;
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'inventory_cost_lot_tracker';
|
||||
}
|
||||
|
||||
/**
|
||||
* Model timestamps.
|
||||
*/
|
||||
static get timestamps() {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
groupedEntriesCost(query) {
|
||||
query.select(['date', 'item_id', 'transaction_id', 'transaction_type']);
|
||||
query.sum('cost as cost');
|
||||
|
||||
query.groupBy('transaction_id');
|
||||
query.groupBy('transaction_type');
|
||||
query.groupBy('date');
|
||||
query.groupBy('item_id');
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters transactions by the given date range.
|
||||
*/
|
||||
filterDateRange(
|
||||
query,
|
||||
startDate,
|
||||
endDate,
|
||||
type: unitOfTime.StartOf = 'day',
|
||||
) {
|
||||
const dateFormat = 'YYYY-MM-DD';
|
||||
const fromDate = moment(startDate).startOf(type).format(dateFormat);
|
||||
const toDate = moment(endDate).endOf(type).format(dateFormat);
|
||||
|
||||
if (startDate) {
|
||||
query.where('date', '>=', fromDate);
|
||||
}
|
||||
if (endDate) {
|
||||
query.where('date', '<=', toDate);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters transactions by the given branches.
|
||||
*/
|
||||
filterByBranches(query, branchesIds: number | Array<number>) {
|
||||
const formattedBranchesIds = castArray(branchesIds);
|
||||
|
||||
query.whereIn('branchId', formattedBranchesIds);
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters transactions by the given warehosues.
|
||||
*/
|
||||
filterByWarehouses(query, branchesIds: number | Array<number>) {
|
||||
const formattedWarehousesIds = castArray(branchesIds);
|
||||
|
||||
query.whereIn('warehouseId', formattedWarehousesIds);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const { Item } = require('../../Items/models/Item');
|
||||
const { SaleInvoice } = require('../../SaleInvoices/models/SaleInvoice');
|
||||
const {
|
||||
ItemEntry,
|
||||
} = require('../../TransactionItemEntry/models/ItemEntry');
|
||||
const { SaleReceipt } = require('../../SaleReceipts/models/SaleReceipt');
|
||||
|
||||
return {
|
||||
item: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Item,
|
||||
join: {
|
||||
from: 'inventory_cost_lot_tracker.itemId',
|
||||
to: 'items.id',
|
||||
},
|
||||
},
|
||||
invoice: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: SaleInvoice,
|
||||
join: {
|
||||
from: 'inventory_cost_lot_tracker.transactionId',
|
||||
to: 'sales_invoices.id',
|
||||
},
|
||||
},
|
||||
itemEntry: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: ItemEntry,
|
||||
join: {
|
||||
from: 'inventory_cost_lot_tracker.entryId',
|
||||
to: 'items_entries.id',
|
||||
},
|
||||
},
|
||||
receipt: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: SaleReceipt,
|
||||
join: {
|
||||
from: 'inventory_cost_lot_tracker.transactionId',
|
||||
to: 'sales_receipts.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
import { Model, raw } from 'objection';
|
||||
import { castArray } from 'lodash';
|
||||
import * as moment from 'moment';
|
||||
import { getTransactionTypeLabel } from '@/modules/BankingTransactions/utils';
|
||||
import { TInventoryTransactionDirection } from '../types/InventoryCost.types';
|
||||
import { TenantBaseModel } from '@/modules/System/models/TenantBaseModel';
|
||||
import { InventoryTransactionMeta } from './InventoryTransactionMeta';
|
||||
|
||||
export class InventoryTransaction extends TenantBaseModel {
|
||||
date!: Date | string;
|
||||
direction!: TInventoryTransactionDirection;
|
||||
itemId!: number;
|
||||
quantity!: number | null;
|
||||
rate!: number;
|
||||
transactionType!: string;
|
||||
transactionId!: number;
|
||||
costAccountId?: number;
|
||||
entryId!: number;
|
||||
|
||||
createdAt?: Date;
|
||||
updatedAt?: Date;
|
||||
|
||||
warehouseId?: number;
|
||||
meta?: InventoryTransactionMeta;
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'inventory_transactions';
|
||||
}
|
||||
|
||||
/**
|
||||
* Model timestamps.
|
||||
*/
|
||||
static get timestamps() {
|
||||
return ['createdAt', 'updatedAt'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve formatted reference type.
|
||||
* @return {string}
|
||||
*/
|
||||
get transcationTypeFormatted(): string {
|
||||
return getTransactionTypeLabel(this.transactionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Model modifiers.
|
||||
*/
|
||||
static get modifiers() {
|
||||
return {
|
||||
filterDateRange(
|
||||
query,
|
||||
startDate,
|
||||
endDate,
|
||||
type: moment.unitOfTime.StartOf = 'day',
|
||||
) {
|
||||
const dateFormat = 'YYYY-MM-DD';
|
||||
const fromDate = moment(startDate).startOf(type).format(dateFormat);
|
||||
const toDate = moment(endDate).endOf(type).format(dateFormat);
|
||||
|
||||
if (startDate) {
|
||||
query.where('date', '>=', fromDate);
|
||||
}
|
||||
if (endDate) {
|
||||
query.where('date', '<=', toDate);
|
||||
}
|
||||
},
|
||||
|
||||
itemsTotals(builder) {
|
||||
builder.select('itemId');
|
||||
builder.sum('rate as rate');
|
||||
builder.sum('quantity as quantity');
|
||||
builder.select(raw('SUM(`QUANTITY` * `RATE`) as COST'));
|
||||
builder.groupBy('itemId');
|
||||
},
|
||||
|
||||
INDirection(builder) {
|
||||
builder.where('direction', 'IN');
|
||||
},
|
||||
|
||||
OUTDirection(builder) {
|
||||
builder.where('direction', 'OUT');
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters transactions by the given branches.
|
||||
*/
|
||||
filterByBranches(query, branchesIds) {
|
||||
const formattedBranchesIds = castArray(branchesIds);
|
||||
|
||||
query.whereIn('branch_id', formattedBranchesIds);
|
||||
},
|
||||
|
||||
/**
|
||||
* Filters transactions by the given warehosues.
|
||||
*/
|
||||
filterByWarehouses(query, warehousesIds) {
|
||||
const formattedWarehousesIds = castArray(warehousesIds);
|
||||
|
||||
query.whereIn('warehouse_id', formattedWarehousesIds);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const { Item } = require('../../Items/models/Item');
|
||||
const {
|
||||
ItemEntry,
|
||||
} = require('../../TransactionItemEntry/models/ItemEntry');
|
||||
const { InventoryTransactionMeta } = require('./InventoryTransactionMeta');
|
||||
const { InventoryCostLotTracker } = require('./InventoryCostLotTracker');
|
||||
|
||||
return {
|
||||
// Transaction meta.
|
||||
meta: {
|
||||
relation: Model.HasOneRelation,
|
||||
modelClass: InventoryTransactionMeta,
|
||||
join: {
|
||||
from: 'inventory_transactions.id',
|
||||
to: 'inventory_transaction_meta.inventoryTransactionId',
|
||||
},
|
||||
},
|
||||
// Item cost aggregated.
|
||||
itemCostAggregated: {
|
||||
relation: Model.HasOneRelation,
|
||||
modelClass: InventoryCostLotTracker,
|
||||
join: {
|
||||
from: 'inventory_transactions.itemId',
|
||||
to: 'inventory_cost_lot_tracker.itemId',
|
||||
},
|
||||
filter(query) {
|
||||
query.select('itemId');
|
||||
query.sum('cost as cost');
|
||||
query.sum('quantity as quantity');
|
||||
query.groupBy('itemId');
|
||||
},
|
||||
},
|
||||
costLotAggregated: {
|
||||
relation: Model.HasOneRelation,
|
||||
modelClass: InventoryCostLotTracker,
|
||||
join: {
|
||||
from: 'inventory_transactions.id',
|
||||
to: 'inventory_cost_lot_tracker.inventoryTransactionId',
|
||||
},
|
||||
filter(query) {
|
||||
query.sum('cost as cost');
|
||||
query.sum('quantity as quantity');
|
||||
query.groupBy('inventoryTransactionId');
|
||||
},
|
||||
},
|
||||
item: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: Item,
|
||||
join: {
|
||||
from: 'inventory_transactions.itemId',
|
||||
to: 'items.id',
|
||||
},
|
||||
},
|
||||
itemEntry: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: ItemEntry,
|
||||
join: {
|
||||
from: 'inventory_transactions.entryId',
|
||||
to: 'items_entries.id',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { BaseModel } from '@/models/Model';
|
||||
import { Model, raw } from 'objection';
|
||||
|
||||
export class InventoryTransactionMeta extends BaseModel {
|
||||
transactionNumber!: string;
|
||||
description!: string;
|
||||
inventoryTransactionId!: number;
|
||||
|
||||
/**
|
||||
* Table name
|
||||
*/
|
||||
static get tableName() {
|
||||
return 'inventory_transaction_meta';
|
||||
}
|
||||
|
||||
/**
|
||||
* Relationship mapping.
|
||||
*/
|
||||
static get relationMappings() {
|
||||
const { InventoryTransaction } = require('./InventoryTransaction');
|
||||
|
||||
return {
|
||||
inventoryTransaction: {
|
||||
relation: Model.BelongsToOneRelation,
|
||||
modelClass: InventoryTransaction,
|
||||
join: {
|
||||
from: 'inventory_transaction_meta.inventoryTransactionId',
|
||||
to: 'inventory_transactions.inventoryTransactionId'
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user