« Back to History
loom_quality_yarn_manage.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('loom_quality_yarn_manage'); $company_id = (int)$ctx['company_id']; $pdo = $ctx['pdo']; if (!$pdo) require_once __DIR__ . '/core/db.php'; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* Load qualities */ $q = $pdo->prepare("SELECT id, name, short_name FROM loom_quality_data WHERE cid=? ORDER BY name"); $q->execute([$company_id]); $qualities = $q->fetchAll(PDO::FETCH_ASSOC); ?> <?php require_once __DIR__ . '/partials/header.php'; ?> <div class="wrap"> <div class="card"> <h3>Loom Quality – Yarn Mapping</h3> <div class="form-grid cols-4" style="margin-bottom:16px"> <div> <label>Quality</label> <select id="quality_id"> <option value="">-- Select Quality --</option> <?php foreach ($qualities as $q): ?> <option value="<?= $q['id'] ?>"><?= h($q['name']) ?> (<?= h($q['short_name']) ?>)</option> <?php endforeach; ?> </select> </div> <div> <label>New Quality</label> <input type="text" id="new_quality_name"> </div> <div> <label>Short Name</label> <input type="text" id="short_name"> </div> </div> <h4>Yarns</h4> <div class="tablewrap"> <table class="table" id="yarnTable"> <thead> <tr> <th>Stock Type</th> <th>Yarn Type</th> <th>Denier</th> <th>Color</th> <th>Weight (gram / meter)</th> <th>Action</th> </tr> </thead> <tbody></tbody> </table> </div> <div style="margin-top:12px;display:flex;gap:10px"> <button type="button" class="btn btn-green" id="addRow">+ Add Yarn</button> <button type="button" class="btn btn-primary" id="saveData">Save Quality</button> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?> <script> function addRow(){ const tb = document.querySelector('#yarnTable tbody'); const tr = document.createElement('tr'); tr.innerHTML = ` <td><select class="stock_type"></select></td> <td><select class="yarn_type"></select></td> <td><select class="denier"></select></td> <td><select class="color"></select></td> <td><input type="number" step="0.001" class="gram_pm"></td> <td><button type="button" class="btn btn-red del">Delete</button></td> `; tb.appendChild(tr); loadStock(tr); } document.getElementById('addRow').onclick = addRow; /* Delete row */ document.addEventListener('click', e=>{ if(e.target.classList.contains('del')){ e.target.closest('tr').remove(); } }); /* Load stock types */ function loadStock(tr){ fetch('/erp/ajax/ajax_yarn.php?act=stock') .then(r=>r.json()) .then(d=>{ const s = tr.querySelector('.stock_type'); s.innerHTML = '<option value="">-- Select --</option>'; d.forEach(v=>{ s.innerHTML += `<option value="${v}">${v}</option>`; }); }); } /* Dependent dropdowns */ document.addEventListener('change', e=>{ const tr = e.target.closest('tr'); if(!tr) return; if(e.target.classList.contains('stock_type')){ fetch('/erp/ajax/ajax_yarn.php?act=yarn&stock='+encodeURIComponent(e.target.value)) .then(r=>r.json()) .then(d=>{ const y = tr.querySelector('.yarn_type'); y.innerHTML='<option value="">-- Select --</option>'; d.forEach(v=>y.innerHTML+=`<option>${v}</option>`); }); } if(e.target.classList.contains('yarn_type')){ const stock = tr.querySelector('.stock_type').value; fetch('/erp/ajax/ajax_yarn.php?act=denier&stock='+encodeURIComponent(stock)+'&yarn='+encodeURIComponent(e.target.value)) .then(r=>r.json()) .then(d=>{ const dn = tr.querySelector('.denier'); dn.innerHTML='<option value="">-- Select --</option>'; d.forEach(v=>dn.innerHTML+=`<option>${v}</option>`); }); } if(e.target.classList.contains('denier')){ const stock = tr.querySelector('.stock_type').value; const yarn = tr.querySelector('.yarn_type').value; fetch('/erp/ajax/ajax_yarn.php?act=color&stock='+encodeURIComponent(stock)+'&yarn='+encodeURIComponent(yarn)+'&denier='+encodeURIComponent(e.target.value)) .then(r=>r.json()) .then(d=>{ const c = tr.querySelector('.color'); c.innerHTML='<option value="">-- Select --</option>'; d.forEach(v=>c.innerHTML+=`<option>${v}</option>`); }); } }); /* Save */ document.getElementById('saveData').onclick = ()=>{ const rows = []; document.querySelectorAll('#yarnTable tbody tr').forEach(tr=>{ rows.push({ stock: tr.querySelector('.stock_type').value, yarn: tr.querySelector('.yarn_type').value, denier: tr.querySelector('.denier').value, color: tr.querySelector('.color').value, gram: tr.querySelector('.gram_pm').value }); }); fetch('/erp/ajax/save_loom_quality_yarn.php',{ method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({ quality_id:document.getElementById('quality_id').value, new_name:document.getElementById('new_quality_name').value, short_name:document.getElementById('short_name').value, rows:rows }) }) .then(r=>r.json()) .then(d=>alert(d.msg)); }; </script>