« Back to History
loom_salary_total.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /** * Loom Salary – FINAL GROSS total ONLY (SAFE VERSION) * Source logic: Calculates strictly gross (Meter × Quality Rate) without any deductions/advances. */ /* ========================= SAFE detect_date_col ========================= */ if (!function_exists('detect_date_col')) { function detect_date_col(PDO $pdo, string $table): string { foreach (['entry_date','date','created_at','production_date'] as $c) { $st = $pdo->prepare(" SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ? "); $st->execute([$table, $c]); if ($st->fetchColumn()) return $c; } throw new RuntimeException("No date column found in $table"); } } /* ========================= MAIN FUNCTION (GROSS ONLY) ========================= */ if (!function_exists('get_loom_salary_total')) { function get_loom_salary_total( PDO $pdo, int $company_id, string $from, string $to, int $khata_id = 0, int $machine_id = 0 ): float { /* ---- detect date column ---- */ $date_col = detect_date_col($pdo, 'production_entry'); /* ---- quality rates ---- */ $QUAL_RATE = []; $st = $pdo->prepare(" SELECT quality_id, rate FROM quality_rates WHERE company_id = ? "); $st->execute([$company_id]); foreach ($st as $r) { $QUAL_RATE[(int)$r['quality_id']] = (float)$r['rate']; } /* ---- production ---- */ $MTR = []; $sql = " SELECT quality_id, lines_json FROM production_entry WHERE company_id = ? AND $date_col BETWEEN ? AND ? ".($khata_id ? "AND khata_id = ?" : "")." ".($machine_id ? "AND machine_id = ?" : "")." "; $params = [$company_id, $from.' 00:00:00', $to.' 23:59:59']; if ($khata_id) $params[] = $khata_id; if ($machine_id) $params[] = $machine_id; $st = $pdo->prepare($sql); $st->execute($params); foreach ($st as $row) { $qid = (int)$row['quality_id']; if ($qid <= 0) continue; $lines = json_decode($row['lines_json'] ?? '', true); if (!is_array($lines)) continue; foreach ($lines as $li) { $kid = (int)($li['karigar_id'] ?? 0); $mtr = (float)($li['meter'] ?? 0); if ($kid <= 0 || $mtr <= 0) continue; $MTR[$kid][$qid] = ($MTR[$kid][$qid] ?? 0) + $mtr; } } /* ---- FINAL GROSS CALCULATION ---- */ $grand_gross = 0.0; foreach ($MTR as $kid => $qs) { foreach ($qs as $qid => $mtr) { $grand_gross += $mtr * ($QUAL_RATE[$qid] ?? 0); } } return round($grand_gross, 2); } }