Purchase Orders - every purchase order the agent can see (one * for a client they can't see is left out). Along the top, what is waiting to * be ordered, waiting for approval, on order and in this month. Tabs: Open * (the default - not received or cancelled), Drafts, Awaiting approval, * Approved, Ordered (partly received too), Received, Cancelled and All. The * work is in functions/purchase_orders.php; the handlers are * agent/post/purchase_order.php. */ // Default Column Sortby/Order Filter $sort = "po_number"; $order = "DESC"; require_once "includes/inc_all.php"; poRequireLevel(1); $can_edit = poLevel() >= 2; $views = [ 'open' => ['label' => 'Open', 'icon' => '', 'where' => "po_status IN ('draft', 'pending', 'approved', 'ordered', 'partial')"], 'draft' => ['label' => 'Drafts', 'icon' => 'fa-pencil-alt', 'where' => "po_status = 'draft'"], 'pending' => ['label' => 'Awaiting approval', 'icon' => 'fa-hourglass-half', 'where' => "po_status = 'pending'"], 'approved' => ['label' => 'Approved', 'icon' => 'fa-thumbs-up', 'where' => "po_status = 'approved'"], 'ordered' => ['label' => 'Ordered', 'icon' => 'fa-paper-plane', 'where' => "po_status IN ('ordered', 'partial')", 'title' => 'Ordered, and partly received'], 'received' => ['label' => 'Received', 'icon' => 'fa-check-double', 'where' => "po_status = 'received'"], 'cancelled' => ['label' => 'Cancelled', 'icon' => 'fa-ban', 'where' => "po_status = 'cancelled'"], 'all' => ['label' => 'All', 'icon' => '', 'where' => '1 = 1'], ]; $view = isset($views[$_GET['show'] ?? '']) ? $_GET['show'] : 'open'; $vendor_filter = intval($_GET['vendor'] ?? 0); $sortable_columns = [ 'po_number' => "po_number $order", 'vendor_name' => "vendor_name $order, po_number DESC", 'po_total' => "po_total $order", 'po_order_date' => "po_order_date IS NULL, po_order_date $order, po_number DESC", 'po_status' => "FIELD(po_status, 'pending', 'draft', 'approved', 'ordered', 'partial', 'received', 'cancelled') $order, po_number DESC", ]; if (!isset($sortable_columns[$sort])) { $sort = 'po_number'; } $scope = poScopeSql('po_id'); $search = $q !== '' ? " AND (CONCAT(po_prefix, po_number) LIKE '%$q%' OR vendor_name LIKE '%$q%' OR po_vendor_reference LIKE '%$q%' OR po_note LIKE '%$q%' OR po_id IN (SELECT po_item_po_id FROM po_items LEFT JOIN clients ON client_id = po_item_client_id WHERE po_item_name LIKE '%$q%' OR po_item_vendor_part LIKE '%$q%' OR client_name LIKE '%$q%'))" : ''; $filters = $scope . $search . ($vendor_filter ? " AND po_vendor_id = $vendor_filter" : ''); $joins = "LEFT JOIN vendors ON vendors.vendor_id = po_vendor_id LEFT JOIN users ON user_id = po_created_by"; $pos = []; $num_rows = [0]; $counts = array_fill_keys(array_keys($views), 0); $stats = ['to_order' => 0, 'pending' => 0, 'on_order' => 0, 'on_order_total' => 0, 'received_month' => 0]; $lines = []; if (poReady()) { $sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS purchase_orders.*, vendor_name, user_name AS created_by_name FROM purchase_orders $joins WHERE {$views[$view]['where']} $filters ORDER BY {$sortable_columns[$sort]}, po_id DESC LIMIT $record_from, $record_to"); $num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()")); while ($row = mysqli_fetch_assoc($sql)) { $pos[] = $row; } // Each one's lines - how many, who they are for, and how much has come in if ($pos) { $ids = implode(',', array_map('intval', array_column($pos, 'po_id'))); $sql = mysqli_query($mysqli, "SELECT po_item_po_id, COUNT(*) AS lines_count, SUM(po_item_quantity) AS qty, SUM(LEAST(po_item_received, po_item_quantity)) AS received, GROUP_CONCAT(DISTINCT IF(po_item_client_id > 0, client_name, NULL) ORDER BY client_name SEPARATOR '|') AS clients, SUM(po_item_client_id = 0) AS stock_lines FROM po_items LEFT JOIN clients ON client_id = po_item_client_id AND po_item_client_id > 0 WHERE po_item_po_id IN ($ids) GROUP BY po_item_po_id"); while ($row = mysqli_fetch_assoc($sql)) { $lines[intval($row['po_item_po_id'])] = $row; } } $row = mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', array_map(function ($v) { return "COALESCE(SUM({$v['where']}), 0)"; }, $views)) . " FROM purchase_orders $joins WHERE 1 $filters")); $counts = array_combine(array_keys($views), array_map('intval', $row)); // The figures along the top - everything the agent can see, whatever the search $stats = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT COALESCE(SUM(po_status = 'pending'), 0) AS pending, COALESCE(SUM(po_status IN ('ordered', 'partial')), 0) AS on_order, COALESCE(SUM(IF(po_status IN ('ordered', 'partial'), po_total, 0)), 0) AS on_order_total, COALESCE(SUM(po_status = 'received' AND po_received_at >= '" . date('Y-m-01') . "'), 0) AS received_month FROM purchase_orders WHERE 1 $scope")); $stats['to_order'] = poNavCounts()['to_order']; } $filter_url = function ($changes) { $params = array_merge($_GET, $changes); unset($params['page']); return '?' . http_build_query(array_filter($params, function ($v) { return $v !== '' && $v !== null; })); }; $sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) { return '' . $label . ($sort === $column ? " $order_icon" : '') . ''; }; $csrf = $_SESSION['csrf_token']; ?> = poTabsHtml('purchase_orders.php') ?>
| = $sort_link('po_number', 'Purchase order') ?> | = $sort_link('vendor_name', 'Vendor') ?> | For | = $sort_link('po_status', 'Status') ?> | = $sort_link('po_order_date', 'Ordered') ?> | = $sort_link('po_total', 'Total') ?> | |
|---|---|---|---|---|---|---|
|
= escapeHtml(poNumber($row)) ?>
= intval($line['lines_count']) ?> line= intval($line['lines_count']) === 1 ? '' : 's' ?>= $row['po_vendor_reference'] ? ' · their order ' . escapeHtml($row['po_vendor_reference']) : '' ?>
|
= escapeHtml((string) $row['vendor_name']) ?> | = $for ? escapeHtml(implode(', ', array_slice($for, 0, 2)) . (count($for) > 2 ? ' +' . (count($for) - 2) : '')) : 'Nothing yet' ?> |
= poStatusBadge($row['po_status']) ?>
0) { ?>
= escapeHtml(billingQuantity($line['received'])) ?> of = escapeHtml(billingQuantity($line['qty'])) ?> in
Made = escapeHtml(timeAgo($row['po_created_at'])) ?>= $row['created_by_name'] ? ' by ' . escapeHtml($row['created_by_name']) : '' ?>
|
= $row['po_order_date'] ? escapeHtml(date('M j, Y', strtotime($row['po_order_date']))) : 'Not yet' ?> | = escapeHtml(poMoney($row['po_total'], $row['po_currency_code'])) ?> |
|