"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInvoiceDifference = exports.getInvoiceDifferenceFull = void 0;
const excel_helper_1 = require("../../helpers/excel.helper");
const collaterals_db_1 = require("../../db/collaterals.db");
const collaterals_enum_1 = require("../../enums/collaterals.enum");
const BBCDate_model_1 = require("../../models/BBCDate.model");
const receivables_db_1 = require("../../db/receivables.db");
const getInvoiceDifferenceFull = async (params) => {
    const { addedInvoices, removedInvoices } = await (0, exports.getInvoiceDifference)(params);
    const header = getInvoiceDifferenceHeader();
    const addedInvoicesFull = (0, excel_helper_1.addHeaderToData)(header, addedInvoices);
    const removedInvoicesFull = (0, excel_helper_1.addHeaderToData)(header, removedInvoices);
    return { 'added': addedInvoicesFull, 'removed': removedInvoicesFull };
};
exports.getInvoiceDifferenceFull = getInvoiceDifferenceFull;
const getInvoiceDifference = async (params) => {
    const { bbcDateId } = params;
    const currentBbcDate = await BBCDate_model_1.BBCDateModel.findById(bbcDateId);
    if (currentBbcDate) {
        const previousDocuments = await (0, collaterals_db_1.getPreviousCollateralDocument)(currentBbcDate.borrowerId.toString(), currentBbcDate.bbcDate, collaterals_enum_1.ECollaterals.RECEIVABLE, 1);
        const items1 = await (0, collaterals_db_1.getCollateralListByBBCAndType)(currentBbcDate._id.toString(), collaterals_enum_1.ECollaterals.RECEIVABLE);
        const items2 = await (0, collaterals_db_1.getCollateralListByBBCAndType)(previousDocuments[0]._id.toString(), collaterals_enum_1.ECollaterals.RECEIVABLE);
        return (0, receivables_db_1.compareInvoices)(items1, items2.length ? items2 : []);
    }
    return { addedInvoices: [], removedInvoices: [] };
};
exports.getInvoiceDifference = getInvoiceDifference;
const getInvoiceDifferenceHeader = () => {
    return {
        bbcDate: 'BBC date',
        customerTitle: 'Customer',
        dueDate: 'Due date',
        invoiceAmount: 'Invoice amount',
        originalAmount: 'Original amount',
        invoiceDate: 'Invoice date',
        invoiceNumber: 'Invoice number',
        invoiceDetail1: 'Invoice detail 1',
        invoiceDetail2: 'Invoice detail 2',
        invoiceDetail3: 'Invoice detail 3',
    };
};
