Commit f5c65f90 authored by DevPilot's avatar DevPilot

fix(procurement,hr): fix crashing reports — wrong PO-item columns, missing view, wrong join

PO balance and purchase-volume reports referenced poi.quantity /
poi.qty_received, which don't exist — the real columns are
quantity_ordered / quantity_received. Price-deviation report pointed
at a view that was never created. HR insurance form2 joined
hr_salary_adjustments through employees.id, but that table links
straight to hr_employee_profiles via employee_profile_id (confirmed
against the model and every other query on that table) — the extra
employees join used a column, employee_id, that was never there.
Found live via BOOT FAILURE pages while capturing screenshots for the
accountant's tutorial PDF.
parent ea19e57b
......@@ -114,8 +114,7 @@ class InsuranceController extends Controller
CONCAT(hp.first_name_ar, ' ', hp.last_name_ar) as full_name,
hp.insurance_number, hp.national_id
FROM hr_salary_adjustments sa
JOIN employees e ON e.id = sa.employee_id
JOIN hr_employee_profiles hp ON hp.employee_id = e.id
JOIN hr_employee_profiles hp ON hp.id = sa.employee_profile_id
WHERE YEAR(sa.effective_date) = ? AND MONTH(sa.effective_date) = ?
AND sa.status IN ('approved', 'applied')
AND hp.insurance_number IS NOT NULL
......
......@@ -63,11 +63,11 @@ class DeliveryTrackingController extends Controller
$orders = $db->select(
"SELECT po.id, po.po_number, po.created_at as order_date, po.total_amount, po.status,
s.name_ar as supplier_name,
SUM(poi.quantity) as total_qty_ordered,
SUM(poi.qty_received) as total_qty_received,
SUM(poi.quantity - poi.qty_received) as total_qty_remaining,
CASE WHEN SUM(poi.quantity) > 0
THEN ROUND((SUM(poi.qty_received) / SUM(poi.quantity)) * 100, 1)
SUM(poi.quantity_ordered) as total_qty_ordered,
SUM(poi.quantity_received) as total_qty_received,
SUM(poi.quantity_ordered - poi.quantity_received) as total_qty_remaining,
CASE WHEN SUM(poi.quantity_ordered) > 0
THEN ROUND((SUM(poi.quantity_received) / SUM(poi.quantity_ordered)) * 100, 1)
ELSE 0 END as delivery_percentage
FROM purchase_orders po
JOIN suppliers s ON s.id = po.supplier_id
......
......@@ -46,7 +46,7 @@ class ProcurementReportController extends Controller
// Top items by quantity
$topItems = $db->select(
"SELECT i.`name_ar`, i.`sku`,
COALESCE(SUM(poi.`quantity`), 0) as total_qty,
COALESCE(SUM(poi.`quantity_ordered`), 0) as total_qty,
COALESCE(SUM(poi.`line_total`), 0) as total_value
FROM `purchase_order_items` poi
JOIN `inventory_items` i ON i.`id` = poi.`item_id`
......
<?php $__template->layout('Layout.main'); ?>
<?php $__template->section('title'); ?>تقرير انحراف الأسعار<?php $__template->endSection(); ?>
<?php $__template->section('page_actions'); ?>
<a href="/procurement" class="btn btn-outline"><i data-lucide="arrow-right" style="width:15px;height:15px;vertical-align:middle;margin-left:4px;"></i> العودة</a>
<?php $__template->endSection(); ?>
<?php $__template->section('content'); ?>
<!-- Filters -->
<div class="card" style="margin-bottom:20px;padding:15px;">
<form method="GET" action="/procurement/reports/price-deviation" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
<div style="min-width:150px;">
<label class="form-label" style="font-size:12px;">نسبة الانحراف الحد الأدنى (%)</label>
<input type="number" name="threshold" class="form-input" value="<?= e((string) $threshold) ?>" step="1" min="0">
</div>
<button type="submit" class="btn btn-outline"><i data-lucide="search" style="width:16px;height:16px;vertical-align:middle;"></i> بحث</button>
</form>
</div>
<!-- Price Deviation Table -->
<div class="card">
<div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;align-items:center;gap:8px;">
<i data-lucide="trending-up" style="width:18px;height:18px;color:#D97706;"></i>
<h3 style="margin:0;color:#D97706;font-size:15px;">أصناف تجاوزت نسبة الانحراف السعري</h3>
<?php if (!empty($deviations)): ?>
<span style="background:#FFF7ED;color:#D97706;font-size:12px;padding:2px 8px;border-radius:10px;font-weight:600;margin-right:8px;"><?= count($deviations) ?> عنصر</span>
<?php endif; ?>
</div>
<?php if (!empty($deviations)): ?>
<div class="table-responsive">
<table class="data-table">
<thead>
<tr>
<th>الصنف</th>
<th>المورد</th>
<th>تاريخ الشراء</th>
<th>السعر الفعلي</th>
<th>متوسط السعر التاريخي</th>
<th>نسبة الانحراف</th>
</tr>
</thead>
<tbody>
<?php foreach ($deviations as $d): ?>
<?php $pct = (float) ($d['deviation_pct'] ?? 0); ?>
<tr>
<td style="font-weight:600;"><?= e($d['item_name'] ?? '—') ?> <span style="color:#6B7280;font-size:12px;">(<?= e($d['sku'] ?? '') ?>)</span></td>
<td><?= e($d['supplier_name'] ?? '—') ?></td>
<td><?= e($d['effective_date'] ?? '—') ?></td>
<td style="direction:ltr;text-align:left;"><?= money($d['actual_price'] ?? 0) ?></td>
<td style="direction:ltr;text-align:left;color:#6B7280;"><?= money($d['avg_price'] ?? 0) ?></td>
<td style="font-weight:700;color:<?= $pct > 0 ? '#DC2626' : '#059669' ?>;"><?= $pct > 0 ? '+' : '' ?><?= number_format($pct, 1) ?>%</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div style="padding:40px 20px;text-align:center;color:#6B7280;">
<p style="margin:0;">لا توجد أصناف تجاوزت نسبة الانحراف المحددة (<?= e((string) $threshold) ?>%)</p>
</div>
<?php endif; ?>
</div>
<?php $__template->endSection(); ?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment