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']; ?>
To order
Waiting to go on a purchase order
Awaiting approval
On order
still coming
Received
All in this month

Purchase Orders

0, 'qty' => 0, 'received' => 0, 'clients' => '', 'stock_lines' => 0]; $clients = array_filter(explode('|', (string) $line['clients'])); $for = array_merge($clients, intval($line['stock_lines']) ? ['Our stock'] : []); $arriving = in_array($row['po_status'], ['ordered', 'partial'], true); ?>
For
line
2 ? ' +' . (count($for) - 2) : '')) : 'Nothing yet' ?> 0) { ?>
of in
Made
Not yet' ?>
'purchase orders', 'icon' => 'fa-file-signature', 'filtered' => $q !== '' || $vendor_filter, 'views' => ['show'], 'view' => emptyStatePill($views, $view, 'open'), 'text' => 'What you buy from your vendors - made from To Order, approved when it needs to be, emailed to the vendor and received as it arrives.', 'actions' => $can_edit ? [['label' => 'New Purchase Order', 'modal' => 'modals/purchasing/po_new.php']] : [], ]; require_once "../includes/filter_footer.php"; ?>