Skip to content

Commit

Permalink
Allow view/edit of subdepartment in Product List Tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance Edgar committed May 29, 2024
1 parent bd1edf0 commit 9e324fa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
26 changes: 24 additions & 2 deletions fannie/item/ProductListPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,24 @@ function javascript_content()
if ($this->excel) return '';

$dbc = FannieDB::get($FANNIE_OP_DB);

$depts = array();
$prep = $dbc->prepare('SELECT dept_no,dept_name FROM departments ORDER BY dept_no');
$result = $dbc->execute($prep);
while($row = $dbc->fetchRow($result))
$depts[$row[0]] = $row[1];

$subdeptMap = [];
$prep = $dbc->prepare('SELECT subdept_no, subdept_name, dept_ID FROM subdepts ORDER BY subdept_no');
$result = $dbc->execute($prep);
while($row = $dbc->fetchRow($result)) {
$deptno = $row[2];
if (!array_key_exists($deptno, $subdeptMap)) {
$subdeptMap[$deptno] = [];
}
$subdeptMap[$deptno][$row[0]] = $row[1];
}

$taxes = array('-'=>array(0,'NoTax'));
$prep = $dbc->prepare('SELECT id, description FROM taxrates ORDER BY id');
$result = $dbc->execute($prep);
Expand All @@ -91,26 +104,30 @@ function javascript_content()
else
$taxes[strtoupper(substr($row[1],0,1))] = array($row[0], $row[1]);
}

$local_opts = array('-'=>array(0,'No'));
$origins = new OriginsModel($dbc);
$local_origins = $origins->getLocalOrigins();
foreach ($local_origins as $originID => $shortName) {
$local_opts[substr($shortName,0,1)] = array($originID,$shortName);
}
if (count($local_opts) == 1) $local_opts['X'] = array(1,'Yes'); // generic local if no origins defined

$vendors = array('', 'DIRECT');
$vModel = new VendorsModel($dbc);
foreach ($vModel->find('vendorName') as $v) {
$vendors[] = $v->vendorName();
}

ob_start();
?>
var deptObj = <?php echo json_encode($depts); ?>;
var subdeptObj = [];
var subdeptMap = <?php echo json_encode($subdeptMap); ?>;
var taxObj = <?php echo json_encode($taxes); ?>;
var localObj = <?php echo json_encode($local_opts); ?>;
var vendorObj = <?php echo json_encode($vendors); ?>;
<?php

return ob_get_clean();
}

Expand Down Expand Up @@ -149,6 +166,7 @@ private function ajax_response()
'brand' => 'brand',
'desc' => 'description',
'dept' => 'department',
'subdept' => 'subdept',
'price' => 'normal_price',
'cost' => 'cost',
'tax' => 'tax',
Expand Down Expand Up @@ -351,6 +369,8 @@ private function list_content()
i.description,
i.brand,
d.dept_name as department,
u.subdept_no,
u.subdept_name,
i.normal_price,
(CASE WHEN i.tax = 1 THEN 'X' WHEN i.tax=0 THEN '-' ELSE LEFT(t.description,1) END) as Tax,
(CASE WHEN i.foodstamp = 1 THEN 'X' ELSE '-' END) as FS,
Expand All @@ -364,6 +384,7 @@ private function list_content()
l.description AS storeName
FROM products as i
LEFT JOIN departments as d ON i.department = d.dept_no
LEFT JOIN subdepts AS u ON u.subdept_no = i.subdept
LEFT JOIN taxrates AS t ON t.id = i.tax
LEFT JOIN vendors AS v ON i.default_vendor_id=v.vendorID
LEFT JOIN Stores AS l ON i.store_id=l.storeID
Expand Down Expand Up @@ -444,7 +465,7 @@ private function list_content()
$ret .= '<table class="table table-striped table-bordered tablesorter small">
<thead>
<tr>';
$ret .= "<th>UPC</th><th>Brand</th><th>Description</th><th>Dept</th><th>" . _('Vendor') . "</th><th>Cost</th><th>Price</th>";
$ret .= "<th>UPC</th><th>Brand</th><th>Description</th><th>Dept</th><th>Subdept</th><th>" . _('Vendor') . "</th><th>Cost</th><th>Price</th>";
$ret .= "<th>Tax</th><th>FS</th><th>Disc</th><th>Wg'd</th><th>Local</th>";
if (!$this->excel && $this->canEditItems !== false) {
$ret .= '<th>&nbsp;</th>';
Expand Down Expand Up @@ -492,6 +513,7 @@ private function rowToTable($row, $multi)
$ret .= "<td align=center class=\"td_brand clickable\">{$row['brand']}</td>";
$ret .= "<td align=center class=\"td_desc clickable\">{$row['description']}</td>";
$ret .= "<td align=center class=\"td_dept clickable\">{$row['department']}</td>";
$ret .= "<td align=center class=\"td_subdept clickable\">{$row['subdept_name']}</td>";
$ret .= "<td align=center class=\"td_supplier clickable\">{$row['distributor']}</td>";
$ret .= "<td align=center class=\"td_cost clickable\">".sprintf('%.3f',$row['cost'])."</td>";
$ret .= "<td align=center class=\"td_price clickable\">{$row['normal_price']}</td>";
Expand Down
28 changes: 25 additions & 3 deletions fannie/item/productList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var productList = (function($) {
elem.find('.' + cell + ':first').html(content);
};

var drawKeyValSelect = function(elem, cell, field, obj) {
var drawKeyValSelect = function(elem, cell, field, obj, onChange) {
var dept = elem.find('.' + cell + ':first').text();
var content = "<select class=\"" + field + " form-control input-sm\"><optgroup style=\"font-size: 90%;\">";
for (var i in obj) {
Expand All @@ -25,6 +25,20 @@ var productList = (function($) {
}
content += '</optgroup></select>';
elem.find('.' + cell + ':first').html(content);
if (onChange) {
elem.find('select')[0].addEventListener('change', onChange);
}
};

var departmentChanged = function(event) {
var deptSelect = event.target;
var dept = $(deptSelect).val();

// update available subdepts
var obj = subdeptMap[dept];
var td = $('.td_subdept');
td.find('select')[0].remove();
drawKeyValSelect(td.parents('tr:first'), 'td_subdept', 'in_subdept', obj);
};

var drawTupleSelect = function(elem, cell, field, obj) {
Expand All @@ -47,7 +61,10 @@ var productList = (function($) {
drawTextBox(elem, 'td_'+i.name, 'in_'+i.name, i.size);
});

drawKeyValSelect(elem, 'td_dept', 'in_dept', deptObj);
drawKeyValSelect(elem, 'td_dept', 'in_dept', deptObj, departmentChanged);
var dept = elem.find('.in_dept:first').val();
subdeptObj = subdeptMap[dept] || [];
drawKeyValSelect(elem, 'td_subdept', 'in_subdept', subdeptObj);
drawKeyValSelect(elem, 'td_supplier', 'in_supplier', vendorObj);

var checks = ['fs', 'disc', 'wgt'];
Expand Down Expand Up @@ -106,6 +123,10 @@ var productList = (function($) {
var dept = elem.find('.in_dept:first').val();
elem.find('.td_dept:first').html(deptObj[dept]);

var subdept = elem.find('.in_subdept:first').val();
var obj = subdeptMap[dept] || {};
elem.find('.td_subdept:first').html(obj[subdept] || '');

var tax = elem.find('.in_tax:first').val().split(':');
elem.find('.td_tax:first').html(tax[0]);

Expand All @@ -119,7 +140,8 @@ var productList = (function($) {
elem.find('.td_cmd:first .edit-link').show();
elem.find('.td_cmd:first .save-link').hide();

dstr += '&upc='+upc+'&dept='+dept+'&store_id='+store_id;
dstr += '&upc='+upc+'&dept='+dept+'&store_id='+store_id
+ '&subdept=' + (subdept || 0);
dstr += '&tax='+tax[1]+'&local='+local[1];
$.ajax({
url: 'ProductListPage.php',
Expand Down

0 comments on commit 9e324fa

Please sign in to comment.