From a3bd40889a817b8814e661d8d1fae6fbce8f4274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Zieli=C5=84ski?= Date: Sat, 8 Nov 2025 12:54:24 +0100 Subject: [PATCH] sort sale products list by stock_address --- modules/EcmSales/ProductSummary.php | 234 +++++++++++--------- modules/EcmSales/javascript/ListNewSales.js | 4 +- 2 files changed, 134 insertions(+), 104 deletions(-) diff --git a/modules/EcmSales/ProductSummary.php b/modules/EcmSales/ProductSummary.php index 7ea864ed..9a037a1d 100644 --- a/modules/EcmSales/ProductSummary.php +++ b/modules/EcmSales/ProductSummary.php @@ -1,120 +1,150 @@ '; -echo ''; -echo ''; -echo ''; +echo ''; +echo ''; +echo ''; $json_config = new json_config (); - $focus = new EcmSale (); - $db = $GLOBALS ['db']; - $data = array(); - $categoryArray=array(); - $id_array= explode(",", $_GET['ids']) ; - - //var_dump($id_array); - $where="and doc.id IN ('".implode("','",$id_array)."')"; - if($_GET['product_group']=='1')$where.=" and pr.group_ks=1"; - if($_GET['product_group']=='2')$where.=" and pr.group_ks=2"; - if($_GET['product_group']=='3')$where.=" and pr.group_ks=3"; - - $query= "SELECT - pr.id, - item.code, - item.name, - ROUND(sum(item.quantity),0) AS quantity, - ROUND(ifnull(x.stock, 0),0) AS stock, - pr.group_ks, - pr.product_category_name, - pr.product_category_id - FROM - ecmsaleitems as item - INNER JOIN - ecmsales as doc ON doc.id=item.ecmsale_id - INNER JOIN - ecmproducts as pr ON pr.id=item.ecmproduct_id - LEFT JOIN - (SELECT sum(quantity) as stock ,product_id - FROM - ecmstockstates - GROUP BY product_id) x ON x.product_id=item.ecmproduct_id - WHERE doc.deleted='0' and doc.status!='s10' " . $where; - $query.=" AND NOT EXISTS - ( - SELECT NULL - FROM ecmstockdocouts t1 - WHERE t1.ecmsale_id = doc.id - ) GROUP by item.ecmproduct_id - ORDER BY pr.group_ks DESC"; - - $rows = $db->query ($query); +$focus = new EcmSale (); +$db = $GLOBALS['db']; - while($r = $db->fetchByAssoc ( $rows )) - { - $row = array(); - $row["id"] = $r["id"]; - $row["code"] = $r["code"]; - $row["name"] = $r["name"]; - $row["quantity"] = $r["quantity"]; - $row["stock"] = $r["stock"]; - $row["product_category_name"]=$r["product_category_name"]; - $row["group_ks"] = $r["group_ks"]; - if($r["group_ks"]=='1')$daj='TH'; - if($r["group_ks"]=='2')$daj='WG'; - if($r["group_ks"]=='3')$daj='S'; - $row["group_ks_name"] = $daj; +$positions = array(); +$total = 0; +$stock = 0; +$id_array = isset($_GET['ids']) ? explode(",", $_GET['ids']) : array(); +$where = "and doc.id IN ('" . implode("','", $id_array) . "')"; +if (isset($_GET['product_group']) && $_GET['product_group'] == '1') $where .= " and pr.group_ks=1"; +if (isset($_GET['product_group']) && $_GET['product_group'] == '2') $where .= " and pr.group_ks=2"; +if (isset($_GET['product_group']) && $_GET['product_group'] == '3') $where .= " and pr.group_ks=3"; - // get stock addresses - $result = $db->query("SELECT stock_address, is_not_full FROM ecmproducts_stock_addresses WHERE ecmproduct_id='".$r['id']."';"); - $stock_addresses = ""; - while($addr=$db->fetchByAssoc($result)) { - if ($addr['is_not_full'] == '1') { - $stock_addresses .= " ".$addr['stock_address'].""; - } else { - $stock_addresses .= " ".$addr['stock_address']; - } - } - $row['stock_addresses'] = $stock_addresses; - - array_push($row, $r); - $data [] = $row; - $total+=$r['quantity']; - $stock+=$r['stock']; - } - $data['quantity']=$total; - $data['stock']=$stock; +$query = "SELECT + pr.id, + item.code, + item.name, + ROUND(SUM(item.quantity),0) AS quantity, + ROUND(IFNULL(x.stock, 0),0) AS stock, + pr.group_ks, + pr.product_category_name, + pr.product_category_id + FROM ecmsaleitems AS item + INNER JOIN ecmsales AS doc ON doc.id=item.ecmsale_id + INNER JOIN ecmproducts AS pr ON pr.id=item.ecmproduct_id + LEFT JOIN ( + SELECT SUM(quantity) AS stock, product_id + FROM ecmstockstates + GROUP BY product_id + ) x ON x.product_id=item.ecmproduct_id + WHERE doc.deleted='0' AND doc.status!='s10' " . $where . " + AND NOT EXISTS ( + SELECT NULL + FROM ecmstockdocouts t1 + WHERE t1.ecmsale_id = doc.id + ) + GROUP BY item.ecmproduct_id + ORDER BY pr.group_ks DESC"; +$rows = $db->query($query); -$edit->ss = new Sugar_Smarty (); -$edit->module = 'EcmSales'; +while ($r = $db->fetchByAssoc($rows)) { + $row = array(); + $row['id'] = $r['id']; + $row['code'] = $r['code']; + $row['name'] = $r['name']; + $row['quantity'] = $r['quantity']; + $row['stock'] = $r['stock']; + $row['product_category_name'] = $r['product_category_name']; + $row['group_ks'] = $r['group_ks']; -$edit->ss->assign ( "CREATED_BY_NAME", $focus->created_by_name ); -$edit->ss->assign ( "MODIFIED_BY_NAME", $focus->modified_by_name ); -$edit->ss->assign ( 'FOCUS', $focus); -$edit->ss->assign ( 'POST', $_POST); -$edit->ss->assign ( 'LINK', $link); -$edit->ss->assign ( 'IDS', $_GET['ids']); -$edit->ss->assign('POSITION_LIST',$data); -$edit->ss->assign ( 'MOD', $mod_strings); -$edit->ss-> display('modules/EcmSales/tpls/ProductSummary.tpl'); //4 + $daj = ''; + if ($r['group_ks'] == '1') $daj = 'TH'; + if ($r['group_ks'] == '2') $daj = 'WG'; + if ($r['group_ks'] == '3') $daj = 'S'; + $row['group_ks_name'] = $daj; + $result = $db->query("SELECT stock_address, is_not_full FROM ecmproducts_stock_addresses WHERE ecmproduct_id='" . $r['id'] . "' ORDER BY stock_address"); + $addresses_raw = array(); + while ($addr = $db->fetchByAssoc($result)) { + $addresses_raw[] = array( + 'addr' => trim($addr['stock_address']), + 'not_full' => ($addr['is_not_full'] == '1') + ); + } -// loading view -//echo '
'; -?> \ No newline at end of file + if (!empty($addresses_raw)) { + usort($addresses_raw, function ($a, $b) { + return strcmp($a['addr'], $b['addr']); + }); + } + + $only_addrs = array(); + $has_not_full = 0; + foreach ($addresses_raw as $ar) { + $only_addrs[] = $ar['addr']; + if ($ar['not_full']) $has_not_full = 1; + } + $row['stock_addresses_sort_key'] = implode(' ', $only_addrs); + $row['has_not_full'] = $has_not_full; + + $stock_addresses_html = ''; + foreach ($addresses_raw as $ar) { + if ($ar['not_full']) { + $stock_addresses_html .= " " . $ar['addr'] . ""; + } else { + $stock_addresses_html .= " " . $ar['addr']; + } + } + $row['stock_addresses'] = $stock_addresses_html; + + $positions[] = $row; + + $total += (float)$r['quantity']; + $stock += (float)$r['stock']; +} + +usort($positions, function ($a, $b) { + $ka = isset($a['stock_addresses_sort_key']) ? $a['stock_addresses_sort_key'] : ''; + $kb = isset($b['stock_addresses_sort_key']) ? $b['stock_addresses_sort_key'] : ''; + return strcmp($ka, $kb); +}); + +$data = $positions; +$data['quantity'] = $total; +$data['stock'] = $stock; + +if (!isset($_REQUEST['to_pdf'])) { + $edit->ss = new Sugar_Smarty(); + $edit->module = 'EcmSales'; + + $edit->ss->assign("CREATED_BY_NAME", $focus->created_by_name); + $edit->ss->assign("MODIFIED_BY_NAME", $focus->modified_by_name); + $edit->ss->assign('FOCUS', $focus); + $edit->ss->assign('POST', $_POST); + $edit->ss->assign('LINK', isset($link) ? $link : ''); + $edit->ss->assign('IDS', isset($_GET['ids']) ? $_GET['ids'] : ''); + $edit->ss->assign('POSITION_LIST', $data); + $edit->ss->assign('MOD', $mod_strings); + $edit->ss->display('modules/EcmSales/tpls/ProductSummary.tpl'); +} else { + $t = ''; + + foreach ($data as $r) { + if (isset($r['code'])) { + $t .= ''; + } + } + $t .= '
KodNazwaIlośćAdres magazynowy
' . $r['code'] . '' . $r['name'] . '' . $r['quantity'] . '' . $r['stock_addresses'] . '
'; + + echo $t; +} \ No newline at end of file diff --git a/modules/EcmSales/javascript/ListNewSales.js b/modules/EcmSales/javascript/ListNewSales.js index 4864a56f..cf8d53fc 100644 --- a/modules/EcmSales/javascript/ListNewSales.js +++ b/modules/EcmSales/javascript/ListNewSales.js @@ -48,8 +48,8 @@ function getList() { } function getList2() { if($('#idss').val()!=""){ - window.open('index.php?module=EcmSales&action=productsSummaryList&to_pdf=1&ids='+$('#idss').val()); - } else { + window.open('index.php?module=EcmSales&action=ProductSummary&to_pdf=1&ids='+$('#idss').val()); + } else { alert("Proszę zaznaczyć chociaż jedną pozycje!"); }