« Back to History
production_entry_save.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; require_once __DIR__ . '/../modules/activity/activity_logger.php'; $ctx = page_require_access('production_entry'); $u = $ctx['user']; $company_id = (int)$ctx['company_id']; $pdo = $ctx['pdo']; header('Content-Type: application/json'); try { if ($_SERVER['REQUEST_METHOD'] !== 'POST') { throw new Exception('POST only'); } $machine_id = (int)$_POST['machine_id']; $khata_id = (int)$_POST['khata_id']; $quality_id = (int)$_POST['quality_id']; $entry_date = $_POST['entry_date']; $lines = json_decode($_POST['lines_json'] ?? '[]', true); if(!$machine_id || !$khata_id || !$quality_id || !$entry_date){ throw new Exception('Missing fields'); } if(!is_array($lines) || !count($lines)){ throw new Exception('No production lines'); } $total_meter = 0; foreach($lines as $l){ if(($l['meter'] ?? 0) <= 0) throw new Exception('Invalid meter'); $total_meter += $l['meter']; } $pdo->beginTransaction(); // Resolve beams $q = $pdo->prepare(" SELECT primary_running_beam_no pbn, secondary_running_beam_no sbn FROM beam_mapping WHERE company_id=? AND machine_id=? FOR UPDATE "); $q->execute([$company_id,$machine_id]); $bm = $q->fetch(PDO::FETCH_ASSOC); if(!$bm) throw new Exception('Beam mapping not found'); // Lock beam_book $b = $pdo->prepare(" SELECT beam_no, meter_pending, meter_total, meter_used FROM beam_book WHERE company_id=? AND beam_no IN (?,?) FOR UPDATE "); $b->execute([$company_id,$bm['pbn'],$bm['sbn']]); $beams = $b->fetchAll(PDO::FETCH_ASSOC); if(count($beams)!=2) throw new Exception('Beam book missing'); foreach($beams as $r){ if($r['meter_pending'] < $total_meter){ throw new Exception('Insufficient pending meter on beam '.$r['beam_no']); } } // Next taka $t = $pdo->prepare(" SELECT COALESCE(MAX(taka_no),0)+1 FROM production_entry_new WHERE company_id=? AND machine_id=? AND entry_date=? FOR UPDATE "); $t->execute([$company_id,$machine_id,$entry_date]); $taka = (int)$t->fetchColumn(); // Insert production $i = $pdo->prepare(" INSERT INTO production_entry_new (company_id,entry_date,machine_id,khata_id,quality_id, taka_no,extra_mtr,lines_json,meter_total,created_by,created_at) VALUES (?,?,?,?,?,?,0,?,?,?,NOW()) "); $i->execute([ $company_id,$entry_date,$machine_id,$khata_id,$quality_id, $taka,json_encode($lines),$total_meter,(int)$u['id'] ]); $insert_id = (int)$pdo->lastInsertId(); // Update beams $uQ = $pdo->prepare(" UPDATE beam_book SET meter_used = meter_used + ?, meter_pending = meter_total - (meter_used + ?) WHERE company_id=? AND beam_no=? "); foreach([$bm['pbn'],$bm['sbn']] as $bn){ $uQ->execute([$total_meter,$total_meter,$company_id,$bn]); } $pdo->commit(); activity_log([ 'company_id' => $company_id ?? 0, 'user_id' => $u['id'] ?? ($_SESSION['user_id'] ?? 0), 'module' => 'production', 'action_name' => 'create', 'entity_type' => 'production_entry', 'entity_id' => $insert_id ?? 0, 'remarks' => 'Production entry created' ]); echo json_encode(['ok'=>true]); } catch(Throwable $e){ if($pdo->inTransaction()) $pdo->rollBack(); http_response_code(400); echo json_encode(['ok'=>false,'msg'=>$e->getMessage()]); }