All files / src/domains/finance/services tax-engine.service.ts

0% Statements 0/32
0% Branches 0/18
0% Functions 0/5
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                                                                                                                                           
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { FinanceTaxRegistration, TaxTransaction } from '../schemas/taxation.schema';
 
@Injectable()
export class TaxEngineService {
  constructor(
    @InjectModel(FinanceTaxRegistration.name)
    private readonly registrationModel: Model<FinanceTaxRegistration>,
    @InjectModel(TaxTransaction.name)
    private readonly transactionModel: Model<TaxTransaction>,
  ) {}
 
  async createRegistration(tenantId: string, data: any): Promise<FinanceTaxRegistration> {
    const doc = new this.registrationModel({
      ...data,
      tenantId: new Types.ObjectId(tenantId),
      legalEntityId: new Types.ObjectId(data.legalEntityId)
    });
    return doc.save();
  }
 
  async calculateTax(tenantId: string, params: {
    legalEntityId: string;
    taxableAmountMinor: number;
    taxCode: string; // e.g. "GST-18", "GST-5"
    isInterState: boolean;
  }): Promise<{ taxAmountMinor: number; rateDecimal: number; CGST: number; SGST: number; IGST: number }> {
    // Determine rate from tax code
    let rateDecimal = 0.18;
    if (params.taxCode.endsWith('-5')) {
      rateDecimal = 0.05;
    } else if (params.taxCode.endsWith('-12')) {
      rateDecimal = 0.12;
    } else if (params.taxCode.endsWith('-28')) {
      rateDecimal = 0.28;
    } else if (params.taxCode.endsWith('-0')) {
      rateDecimal = 0.0;
    }
 
    const taxAmountMinor = Math.round(params.taxableAmountMinor * rateDecimal);
 
    let CGST = 0;
    let SGST = 0;
    let IGST = 0;
 
    if (params.isInterState) {
      IGST = taxAmountMinor;
    } else {
      CGST = Math.round(taxAmountMinor / 2);
      SGST = taxAmountMinor - CGST;
    }
 
    return {
      taxAmountMinor,
      rateDecimal,
      CGST,
      SGST,
      IGST
    };
  }
 
  async recordTaxTransaction(tenantId: string, data: any): Promise<TaxTransaction> {
    const doc = new this.transactionModel({
      ...data,
      tenantId: new Types.ObjectId(tenantId),
      legalEntityId: new Types.ObjectId(data.legalEntityId),
      taxRegistrationId: new Types.ObjectId(data.taxRegistrationId)
    });
    return doc.save();
  }
 
  async listTaxTransactions(tenantId: string): Promise<TaxTransaction[]> {
    return this.transactionModel.find({ tenantId }).exec();
  }
}