import { LoginUser } from '@tomei/sso'; import { OrderItemRepository } from './order-item.repository'; import { ApplicationConfig } from '@tomei/config'; import cuid from '../../helpers/cuid'; import { IOrderItemAttr, IUpdateOrderItemTaxDetails, } from '../../interfaces/index'; import { ObjectBase } from '@tomei/general'; import { ActionEnum, Activity } from '@tomei/activity-history'; export abstract class OrderItemBase extends ObjectBase { ObjectId: string; ObjectName: string; ObjectType = 'OrderItem'; TableName: string; OrderNo: string; ItemType: string; Currency: string; SalePrice: number; RetailPrice: number; TaxAmount: number; TaxCode: string; TaxRate: number; SerialNo: string; BatchNo: string; Description: string; Name: string; Quantity: number; get ItemId(): string { return this.ObjectId; } set ItemId(value: string) { this.ObjectId = value; } protected static _OrderItemRepo = new OrderItemRepository(); protected constructor(orderItemAttr?: IOrderItemAttr) { super(); if (orderItemAttr) { this.ItemId = orderItemAttr.ItemId; this.OrderNo = orderItemAttr.OrderNo; this.ItemType = orderItemAttr.ItemType; this.Currency = orderItemAttr.Currency; this.SalePrice = orderItemAttr.SalePrice; this.RetailPrice = orderItemAttr.RetailPrice; this.TaxAmount = orderItemAttr.TaxAmount; this.TaxCode = orderItemAttr.TaxCode; this.TaxRate = orderItemAttr.TaxRate; this.SerialNo = orderItemAttr.SerialNo; this.BatchNo = orderItemAttr.BatchNo; this.Description = orderItemAttr.Description; this.Name = orderItemAttr.Name; this.Quantity = orderItemAttr.Quantity; } } protected async create( loginUser: LoginUser, dbTransaction: any, ): Promise { // This method will create a new order item data. try { // Part 1: Check Privileges const systemCode = ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = await loginUser.checkPrivileges( systemCode, 'Order - Create', ); if (!isPrivileged) { throw new Error('You do not have permission to create OrderItem.'); } //Part 2: Create Order Item //Set ItemId value this.ItemId = cuid(); //Call the repository create method to create a new order item. await OrderItemBase._OrderItemRepo.create( { ItemId: this.ItemId, OrderNo: this.OrderNo, ItemType: this.ItemType, Currency: this.Currency, SalePrice: this.SalePrice, RetailPrice: this.RetailPrice, TaxAmount: this.TaxAmount, TaxCode: this.TaxCode, TaxRate: this.TaxRate, SerialNo: this.SerialNo, BatchNo: this.BatchNo, Description: this.Description, Name: this.Name, Quantity: this.Quantity, }, { transaction: dbTransaction, }, ); } catch (error) { throw error; } } public async updateTaxDetails( orderItemTaxDetails: IUpdateOrderItemTaxDetails, dbTransaction: any, ) { try { //Part 1: Update Order.DeliveryMethod with repo await OrderItemBase._OrderItemRepo.update( { TaxAmount: orderItemTaxDetails.TaxAmount, TaxCode: orderItemTaxDetails.TaxCode, TaxRate: orderItemTaxDetails.TaxRate, }, { where: { ItemId: this.ItemId, }, transaction: dbTransaction, }, ); } catch (error) { throw error; } } public async update(loginUser: LoginUser, dbTransaction?: any) { try { const oldData = await OrderItemBase._OrderItemRepo.findByPk(this.ItemId, { transaction: dbTransaction, }); const entityValueBefore: IOrderItemAttr = oldData.get({ plain: true }); const payload = { OrderNo: this.OrderNo, ItemType: this.ItemType, Currency: this.Currency, SalePrice: this.SalePrice, RetailPrice: this.RetailPrice, TaxAmount: this.TaxAmount, TaxCode: this.TaxCode, TaxRate: this.TaxRate, SerialNo: this.SerialNo, BatchNo: this.BatchNo, Description: this.Description, Name: this.Name, Quantity: this.Quantity, }; await OrderItemBase._OrderItemRepo.update(payload, { where: { ItemId: this.ItemId, }, transaction: dbTransaction, }); const entityValueAfter: IOrderItemAttr = { ItemId: this.ObjectId, ...payload, }; const activity = new Activity(); activity.ActivityId = activity.createId(); activity.Action = ActionEnum.UPDATE; activity.Description = `Update Order Item ${this.ItemId}`; activity.EntityId = this.ItemId; activity.EntityType = 'OrderItem'; activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(entityValueAfter); await activity.create(loginUser.ObjectId, dbTransaction); } catch (error) { throw error; } } }