« Back to History
new_pasaria_entry.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('new_pasaria_entry'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $user_id = (int)$ctx['user']['id']; require_once __DIR__ . '/helpers/beam_auto_finish_helper.php'; require_once __DIR__ . '/helpers/machine_beam_status_helper.php'; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ---------- MACHINES ---------- */ $st=$pdo->prepare(" SELECT id,code FROM machines WHERE company_id=? AND is_active=1 AND group_slug IN('loom','rapier') ORDER BY code "); $st->execute([$company_id]); $machines=$st->fetchAll(PDO::FETCH_ASSOC); /* ---------- BEAMS ---------- */ $st=$pdo->prepare(" SELECT bs.id,bs.beam_no,bs.beam_slot,bs.quality_id, q.name quality_name FROM company_beam_stock bs JOIN beam_qualities q ON q.id=bs.quality_id WHERE bs.company_id=? AND bs.load_type='stock' "); $st->execute([$company_id]); $beams=$st->fetchAll(PDO::FETCH_ASSOC); /* ---------- EMPLOYEES ---------- */ $st=$pdo->prepare(" SELECT id,name FROM company_employee_master WHERE company_id=? AND is_active=1 AND designation_name='Pasaria' ORDER BY name "); $st->execute([$company_id]); $employees=$st->fetchAll(PDO::FETCH_ASSOC); /* ---------- PASARIA TYPES ---------- */ $st=$pdo->prepare(" SELECT pasaria_type FROM pasaria_rates WHERE company_id=? ORDER BY pasaria_type "); $st->execute([$company_id]); $pasaria_types=$st->fetchAll(PDO::FETCH_ASSOC); $msg=$err=''; if($_SERVER['REQUEST_METHOD']==='POST'){ $machine_id =(int)($_POST['machine_id']??0); $beam_primary =(int)($_POST['beam_primary']??0); $beam_second =(int)($_POST['beam_secondary']??0); $employee_id =(int)($_POST['employee_id']??0); $pasaria_type =trim($_POST['pasaria_type']??''); $load_date =$_POST['load_date']??date('Y-m-d'); if(!$machine_id||!$beam_primary||!$beam_second||!$employee_id||$pasaria_type===''){ $err='All fields required'; }else{ try{ $pdo->beginTransaction(); $st=$pdo->prepare("SELECT code FROM machines WHERE id=? AND company_id=?"); $st->execute([$machine_id,$company_id]); $code=(string)$st->fetchColumn(); preg_match('/(\d+)$/',$code,$m); $machine_no=ltrim($m[1],'0'); if($machine_no==='')$machine_no='0'; $st=$pdo->prepare(" SELECT id,beam_no,beam_slot,quality_id FROM company_beam_stock WHERE company_id=? AND load_type='stock' AND id IN (?,?) "); $st->execute([$company_id,$beam_primary,$beam_second]); $rows=$st->fetchAll(PDO::FETCH_ASSOC); foreach($rows as $beam){ auto_finish_running_beam( $pdo, $company_id, $machine_no, $beam['beam_slot'], $load_date, $user_id ); $pdo->prepare(" INSERT INTO beam_load_data (company_id,beam_no,event_type,event_date, machine_no,machine_type,beam_slot, quality_id,load_type,employee_id, load_date,created_by) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) ")->execute([ $company_id, $beam['beam_no'], 'loaded', $load_date, $machine_no, 'loom', $beam['beam_slot'], $beam['quality_id'], 'pasaria', $employee_id, $load_date, $user_id ]); $pdo->prepare(" UPDATE company_beam_stock SET load_type='pasaria',beam_load_date=? WHERE id=? ")->execute([$load_date,$beam['id']]); /* -------- LIFECYCLE -------- */ $pdo->prepare(" INSERT INTO beam_life_cycle(company_id,beam_no,life_events) VALUES(?,?,JSON_ARRAY()) ON DUPLICATE KEY UPDATE beam_no=beam_no ")->execute([ $company_id, $beam['beam_no'] ]); $pdo->prepare(" UPDATE beam_life_cycle SET life_events=JSON_ARRAY_APPEND( IFNULL(life_events,JSON_ARRAY()), '$', JSON_OBJECT( 'machine_no',?, 'slot',?, 'load_date',?, 'action','running', 'machine_id',? ) ) WHERE company_id=? AND beam_no=? ")->execute([ $machine_no, $beam['beam_slot'], $load_date, $machine_id, $company_id, $beam['beam_no'] ]); machine_on_beam_load( $pdo, $company_id, $machine_no, 'loom', $beam['beam_slot'], (int)$beam['beam_no'] ); } $pdo->prepare(" INSERT INTO pasaria_entry_new (company_id,pasaria_date,employee_id,pasaria_type,machine_no,created_at) VALUES(?,?,?,?,?,NOW()) ")->execute([ $company_id, $load_date, $employee_id, $pasaria_type, $machine_no ]); $pdo->commit(); $msg="Pasaria entry saved successfully"; }catch(Exception $e){ $pdo->rollBack(); $err=$e->getMessage(); } } } require_once __DIR__.'/partials/header.php'; ?> <div class="container py-4"> <div class="card shadow-sm"> <div class="card-header bg-primary text-white"> <h5 class="mb-0">Pasaria Entry</h5> </div> <div class="card-body"> <?php if($msg): ?> <div class="alert alert-success"><?=h($msg)?></div> <?php endif; ?> <?php if($err): ?> <div class="alert alert-danger"><?=h($err)?></div> <?php endif; ?> <form method="post" class="row g-3"> <div class="col-md-4"> <label class="form-label">Machine</label> <select name="machine_id" class="form-select" required> <option value="">Select</option> <?php foreach($machines as $m): ?> <option value="<?=$m['id']?>"><?=h($m['code'])?></option> <?php endforeach; ?> </select> </div> <div class="col-md-4"> <label class="form-label">Primary Beam</label> <select name="beam_primary" class="form-select" required> <option value="">Select</option> <?php foreach($beams as $b): if($b['beam_slot']=='primary'): ?> <option value="<?=$b['id']?>"><?=h($b['beam_no'])?></option> <?php endif; endforeach; ?> </select> </div> <div class="col-md-4"> <label class="form-label">Secondary Beam</label> <select name="beam_secondary" class="form-select" required> <option value="">Select</option> <?php foreach($beams as $b): if($b['beam_slot']=='secondary'): ?> <option value="<?=$b['id']?>"><?=h($b['beam_no'])?></option> <?php endif; endforeach; ?> </select> </div> <div class="col-md-4"> <label class="form-label">Pasaria</label> <select name="employee_id" class="form-select" required> <option value="">Select</option> <?php foreach($employees as $e): ?> <option value="<?=$e['id']?>"><?=h($e['name'])?></option> <?php endforeach; ?> </select> </div> <div class="col-md-4"> <label class="form-label">Pasaria Type</label> <select name="pasaria_type" class="form-select" required> <option value="">Select</option> <?php foreach($pasaria_types as $p): ?> <option value="<?=h($p['pasaria_type'])?>"> <?=h($p['pasaria_type'])?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-4"> <label class="form-label">Date</label> <input type="date" name="load_date" value="<?=date('Y-m-d')?>" class="form-control"> </div> <div class="col-12 text-end"> <button class="btn btn-primary px-4"> Save Pasaria Entry </button> </div> </form> </div> </div> </div> <?php require_once __DIR__.'/partials/footer.php'; ?>