Location: PHPKode > projects > Grocery List > listinventory.php
<?php
########################################################################
#
# Project: Grocery List
# URL: http://sourceforge.net/projects/grocery-list/
# E-mail: hide@address.com
#
# Copyright: (C) 2010, Neil McNab
# License: GNU General Public License Version 3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Filename: $URL: https://grocery-list.svn.sourceforge.net/svnroot/grocery-list/releases/1.0/listinventory.php $
# Last Updated: $Date: 2010-03-06 00:03:07 -0800 (Sat, 06 Mar 2010) $
# Author(s): Neil McNab
#
# Description:
#   Manage a list of inventory amounts for a particular list.
#
########################################################################

include_once("include/site.php");

$type='instock';
$typelist=array('instock');

$db = dbconnect();

$listname = get_listname_from_id($db, $_REQUEST['id']);

$title = "List - $listname";
$onload = "document.getElementsByName('upc')[0].focus();";

include_once("include/header.php");

$select = $_REQUEST["select"];
$id = intval($_REQUEST['id']);

if (!empty($id) AND $id >= 0) {

print "<p><a href='listshop.php?id=$id'>Go Shopping</a> | ";
print "<a href='listitems.php?id=$id'>Edit Shopping</a> | ";
print "Edit Inventory | ";
print "<a href='listbaselines.php?id=$id'>Edit Baseline</a></p>";

// process delete form

if ($_REQUEST['submit'] == "Delete") {
    $count = 0;
    foreach ($_REQUEST['cb'] as $value) {
        if (!delete_listitem_by_id($db, $value, $typelist)) {
            print "<p>ERROR: 130 $count</p>";
        } else {
            $count += 1;
        }
    }
    print "<p>Deleted $count items.</p>";
}

// process update form

if ($_REQUEST['submit'] == "Update") {
    $listitemsstore = array();
    for ($i = 0; $i < sizeof($_REQUEST['listitemid']); $i++) {
        $key = $_REQUEST['listitemid'][$i];
        $listitemsstore[$key] = array($_REQUEST['productname'][$i], $_REQUEST['size'][$i], measure_unit_normalize($_REQUEST['units'][$i]), $_REQUEST['notes'][$i], $_REQUEST['count'][$i]);
    }
    $prodcount = 0;
    $itemcount = 0;
    $updatedkeys = array();
    foreach ($_SESSION['listitemsstore'] as $key => $value) {
        if ($_SESSION['listitemsstore'][$key][4] != $listitemsstore[$key][4]) {
            $prev = intval($_SESSION['listitemsstore'][$key][4]);
            $new = intval($listitemsstore[$key][4]);
            $diff = $new - $prev;
            for ($i = 0; $i < $diff; $i++) {
                 //do inserts here
                 if (!copy_listitem_from_listitemid($db, $key)) {
                     print "<p>ERROR: 100 $i</p>";
                 } else {
                     $updatedkeys[$key] = '';
                 }
            }
            if ($diff < 0) {
                $diff = abs($diff);
                //do deletes here
                if (!delete_similar_listitems_from_listitemid($db, $key, $typelist, $diff)) {
                     print "<p>ERROR: 110</p>";
                } else {
                     $updatedkeys[$key] = '';
                }
            }
        }
        if ($_SESSION['listitemsstore'][$key][0] != $listitemsstore[$key][0]) {
            //print "<p>Updating productid $prodid.</p>";
            $prodcount += update_productname_by_listitemid($db, $listitemsstore[$key][0], $key);
        }
        if (sizeof(array_diff_assoc(array_slice($_SESSION['listitemsstore'][$key], 1, 3), array_slice($listitemsstore[$key], 1, 3))) > 0) {
            //print "<p>Updating listitem $key.</p>";
            if (!update_similar_listitems_from_listitemid($db, $key, $typelist, $listitemsstore[$key][1], $listitemsstore[$key][2], $listitemsstore[$key][3])) {
                print "<p>ERROR: 120</p>";
            } else {
                $updatedkeys[$key] = '';
            }
        }
    }
    print "<p>Updated $prodcount product names in database.</p>";
    print "<p>Updated " . sizeof($updatedkeys) . " items in list.</p>";
}
///////// process add form

$addupc = '';
$addsize = '';
$addunits = '';
$addprid = '';
$addprname = '';
$addnotes = '';
$addcount = 1;

if (isset($_REQUEST['add'])) {
    $addupc = $_REQUEST['upc'];
    $addsize = $_REQUEST['size'];
    $addunits = measure_unit_normalize($_REQUEST['units']);
    $addprid = $_REQUEST['productid'];
    $addprname = $_REQUEST['productname'];
    $addnotes = $_REQUEST['notes'];
    $errors = "";
    $result = array();

	if (empty($addprid) AND !empty($addprname)) {
                $addprid = add_product($db, $addprname);
		if ($addprid === NULL) {
			print "<p>INSERT ERROR 10</p>";
		}
	}

        if (!empty($_REQUEST['upc'])) {
            $result = get_upc($db, $_REQUEST['upc']);
            $addupc = $result['upc'];
            if (empty($addsize)) {
	        $addsize = $result['size'];
            }
            if (empty($addunits)) {
                $addunits = measure_unit_normalize($result['units']);
            }
            //lookup productid here
            if (empty($addprid)) {
	        $addprid = get_productid_by_upc($db, $addupc);
            }
        }

    if(empty($addprid)) {
        $errors .= "<p>ERROR: No Generic Product name.</p>";
    }

    if (strlen($errors) == 0) {
        if (!empty($_REQUEST['count'])) {
            $addcount = intval($_REQUEST['count']);
        }

        add_update_item_by_upc($db, $addupc, $addprid, $result['itemname'], $result['size'], $result['units']);

        for ($i = 0; $i < $addcount; $i++) {
            if (!add_listitem($db, $id, $addprid, $addsize, $addunits, $addnotes, $type)) {
		print "<p>INSERT ERROR 20 $i</p>";
            }
        }

        $addupc = '';
        $addsize = '';
        $addunits = '';
        $addprid = '';
        $addprname = '';
        $addnotes = '';
        print "<p>Added product.</p>";
    } else {
        print $errors;
    }
}

///////////  add form

print '<script type="text/javascript" src="include/cuecat.js"></script>';

print '<form method="post" action=""><table>';
print '<tr><th>Generic Product Name</th><th>Quantity</th><th>Amount</th><th>Units</th><th>Notes</th><th>Bar Code</th></tr>';

print "<tr><td><select name='productid'>\n";
print get_option("", "(new)");
foreach (get_products($db) as $row) {
    print get_option($row['productid'], $row['productname'], $addprid);
}
print "</select>";

print '<input name="productname" maxlength="255" size="12" value="' . htmlspecialchars($addprname) . '" /></td>';
print '<td><input name="count" maxlength="10" size="3" value="' . $addcount . '" />@</td>';
print '<td><input name="size" maxlength="10" size="3" value="' . $addsize . '" /></td>';
print '<td><input name="units" size="6" maxlength="255" value="' . $addunits . '" /></td>';

print '<td><input name="notes" maxlength="255" value="' . htmlspecialchars($addnotes) . '" /></td>';
print '<td><input name="upc" maxlength="255" size="15" value="' . $addupc . '" /></td></tr>';
print '</table>';
print "<p><input type='hidden' name='id' value='$id' />";
print '<input type="submit" name="add" value="Add to Inventory List" /></p>';
print '</form>';

//////////////// table list 

$result = get_listitems_by_id($db, $id, $typelist);

print '<form method="post" action=""><p>';
print "<input type='submit' name='submit' value='Update' />";
print "<input type='submit' name='submit' value='Delete' />";
print '</p><table>';
print '<tr><th>Quantity</th><th>Generic Product Name</th><th>Amount</th><th>Units</th><th>Notes</th><th><input name="select" type="submit" value="';
if ($select == "Select All") {
    print "Select None";
} else {
    print "Select All";
}
print '" /></th></tr>';

$tempstore = array();

foreach ($result as $row) {
    $tempstore[$row['listitemid']] = array($row['productname'], $row['size'], $row['units'], $row['notes'], $row['count']);

    print '<tr>';
    print '<td><input name="count[]" maxlength="10" size="3" value="' . $row['count'] . '" /></td>';
    print '<td><input name="productname[]" maxlength="255" size="20" value="' . htmlspecialchars($row['productname']) . '" /></td>';
    print '<td><input name="size[]" maxlength="10" size="3" value="' . $row['size'] . '" /></td>';
    print '<td><input name="units[]" maxlength="255" size="6" value="' . $row['units'] . '" /></td>';
    print '<td><input name="notes[]" maxlength="255" value="' . htmlspecialchars($row['notes']) . '" /></td>'; 
    print '<td><input name="cb[]" value="' . $row['listitemid'] . '" type="checkbox"';
    if ($select == "Select All") {
        print ' checked="checked"';
    }
    print ' />';
    print '<input name="listitemid[]" type="hidden" value="' . $row['listitemid'] . '" />';
    print '</td></tr>';
    print "\n";
}
print '</table>';
print "<p><input type='hidden' name='id' value='$id' />";
print "<input type='submit' name='submit' value='Update' />";
print "<input type='submit' name='submit' value='Delete' />";
print '</p></form>';

$_SESSION['listitemsstore'] = $tempstore;

$db = NULL;

}

include_once($WEBROOT . "/include/footer.php");

?>
Return current item: Grocery List