fcsc_ipi_backend/app/controllers/calculation.controller.js
2025-12-19 17:37:52 +05:30

83 lines
3.9 KiB
JavaScript

const db = require("../models");
const Submission = db.Submission;
const SubmissionProduct = db.SubmissionProduct;
const SubmissionHistory = db.SubmissionHistory;
const Establishment = db.Establishment;
const EstablishmentUser = db.EstablishmentUser;
const UnitMaster = db.UnitMaster;
const VariationReasonMaster = db.VariationReasonMaster;
const ZeroTargetReasonMaster = db.ZeroTargetReasonMaster;
const Product = db.Product;
const CityTown = db.CityTown;
const Emirate = db.Emirate;
const QuarterlyWindowsConfiguration = db.QuarterlyWindowsConfiguration;
const User = db.user;
const ExcelJS = require("exceljs");
const { Op } = require("sequelize");
const { Sequelize } = require("sequelize");
const { sendEmail } = require("../services/email.service");
const { sendEmailService } = require("../services/email.service");
const { getQuarterPeriods } = require("../services/quarterService");
const { sanitizeForLog } = require("../utils/sanitize");
const logger = require("../services/logger");
// SELECT
// p.id AS product_id,
// p.hs_code AS product_hs_code,
// s.year,
// u.uom_short_name AS unit,
// -- Q1
// SUM(CASE WHEN s.quarter = 'Q1' THEN sp.current_quantity_period_one END) AS Jan,
// SUM(CASE WHEN s.quarter = 'Q1' THEN sp.current_quantity_period_two END) AS Feb,
// SUM(CASE WHEN s.quarter = 'Q1' THEN sp.current_quantity_period_three END)AS Mar,
// -- Q2
// SUM(CASE WHEN s.quarter = 'Q2' THEN sp.current_quantity_period_one END) AS Apr,
// SUM(CASE WHEN s.quarter = 'Q2' THEN sp.current_quantity_period_two END) AS May,
// SUM(CASE WHEN s.quarter = 'Q2' THEN sp.current_quantity_period_three END)AS Jun,
// -- Q3
// SUM(CASE WHEN s.quarter = 'Q3' THEN sp.current_quantity_period_one END) AS Jul,
// SUM(CASE WHEN s.quarter = 'Q3' THEN sp.current_quantity_period_two END) AS Aug,
// SUM(CASE WHEN s.quarter = 'Q3' THEN sp.current_quantity_period_three END)AS Sep,
// -- Q4
// SUM(CASE WHEN s.quarter = 'Q4' THEN sp.current_quantity_period_one END) AS Oct,
// SUM(CASE WHEN s.quarter = 'Q4' THEN sp.current_quantity_period_two END) AS Nov,
// SUM(CASE WHEN s.quarter = 'Q4' THEN sp.current_quantity_period_three END)AS `Dec`,
// -- Average (A.M.) Base Year Production
// ROUND(
// (
// COALESCE(SUM(CASE WHEN s.quarter='Q1' THEN sp.current_quantity_period_one END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q1' THEN sp.current_quantity_period_two END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q1' THEN sp.current_quantity_period_three END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q2' THEN sp.current_quantity_period_one END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q2' THEN sp.current_quantity_period_two END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q2' THEN sp.current_quantity_period_three END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q3' THEN sp.current_quantity_period_one END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q3' THEN sp.current_quantity_period_two END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q3' THEN sp.current_quantity_period_three END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q4' THEN sp.current_quantity_period_one END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q4' THEN sp.current_quantity_period_two END),0) +
// COALESCE(SUM(CASE WHEN s.quarter='Q4' THEN sp.current_quantity_period_three END),0)
// ) / 12,
// 2) AS avg_by_production
// FROM submission_products sp
// JOIN submission s ON s.id = sp.submission_id
// JOIN products p ON p.id = sp.product_id
// LEFT JOIN unit_master u ON u.id = p.unit_id -- if you have units table
// WHERE
// s.year = 2022 -- base year
// AND sp.is_active = 1
// AND s.status = 'Approved' -- optional, recommended
// GROUP BY
// p.id;