Location: PHPKode > projects > Grocery List > listshop.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/listshop.php $
# Last Updated: $Date: 2010-03-06 17:43:20 -0800 (Sat, 06 Mar 2010) $
# Author(s): Neil McNab
#
# Description:
#   Generate a shopping list based on database values.
#
########################################################################

$QTY_VIEW='1';
$TOTAL_VIEW='2';

include_once("include/site.php");

$db = dbconnect();

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

$title = "List - $listname";

include_once("include/header.php");

$id = $_REQUEST['id'];
$view = $_REQUEST['view'];

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

  if (empty($view)) {
    // set default view
    if (dbexist($db, "SELECT * FROM listitems WHERE NOT (units='' OR size='0') AND acctid='$GLIST_ACCT_ID' AND type='tobuy' AND listid=" . $db->quote($id))) {
        $view = $TOTAL_VIEW;
    } elseif (!dbexist($db, "SELECT * FROM listitems WHERE type='mininventory' AND acctid='$GLIST_ACCT_ID' AND listid=" . $db->quote($id))) {
        $view = $QTY_VIEW;
    } else {
        $view = $TOTAL_VIEW;
    }
  }

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

$results = array();

// key on producutid,units for total view, all three for units view

foreach (array('tobuy','fullinventory','mininventory','instock') as $key) {
    foreach(get_view_shop_by_listid($db, $id, $key)->fetchAll() as $res) {
        if (!array_key_exists($res['productname'], $results))
            $results[$res['productname']] = array();
        if (!array_key_exists($res['units'], $results[$res['productname']]))
            $results[$res['productname']][$res['units']] = array();
        if (!array_key_exists($res['size'], $results[$res['productname']][$res['units']]))
            $results[$res['productname']][$res['units']][$res['size']] = array();
        if (!array_key_exists($res['type'], $results[$res['productname']][$res['units']][$res['size']])) {
            $results[$res['productname']][$res['units']][$res['size']][$res['type']] = array('count'=>$res['count'],'total'=>$res['total'],'notes'=>$res['notes']);
        }
    }
}

if ($view == $QTY_VIEW) {

print "<p><a href='?id=$id&amp;view=$TOTAL_VIEW'>Switch to Quantity View</a></p>\n";

$keys = array_keys($results);
asort($keys);
foreach ($keys as $name) {
  $row1 = $results[$name];
  foreach ($row1 as $units => $row2) {
    foreach ($row2 as $size => $row3) {
        $invsize = 0;
        $total = 0;
        $buysize = 0;
      foreach ($row3 as $type => $res) {
          if ($type == 'tobuy') {
            $buysize = $res['count'];
          }
          if ($type == 'instock') {
            $invsize = $res['count'];
          }
      }

      $style = 'text-align: left;';
      if (floatval($invsize) <= 0) {
        $style .= 'color: red;';
      }

      $total = floatval($buysize);
      $notes = htmlspecialchars($res['notes']);

      if ($total > 0) {
          list($size, $units) =  measure_unit_convert($size, $units, FALSE);
          list($size2, $units2) =  measure_unit_convert($size, $units, TRUE);
          if ($units == 'ct') {
              print "<p style='$style'><input type='checkbox' /> $total " . htmlspecialchars($name) .  " ($size $units)</p>\n";
          } else {
              print "<p style='$style'><input type='checkbox' /> $total " . htmlspecialchars($name) . " - $notes ($size $units/$size2 $units2)</p>\n";
          }
      }
    }
  }
}

if (dbexist($db, "SELECT * FROM listitems WHERE acctid='$GLIST_ACCT_ID' AND type='mininventory' AND listid=" . $db->quote($id))) {
    print '<p>NOTE: This view does not include inventory and baseline calculations.</p>';
}

} else {

print "<p><a href='?id=$id&amp;view=$QTY_VIEW'>Switch to Item Count View</a></p>\n";

$keys = array_keys($results);
asort($keys);
foreach ($keys as $name) {
        $row1 = $results[$name];
        $minsize = 0;
        $fullsize = 0;
        $invsize = 0;
        $total = 0;
        $buysize = 0;
  foreach ($row1 as $units => $row2) {
    foreach ($row2 as $size => $row3) {
      foreach ($row3 as $type => $res) {
          if ($type == 'mininventory') {
            $minsize = $res['total'];
          }
          if ($type == 'fullinventory') {
            $fullsize = $res['total'];
          }
          if ($type == 'tobuy') {
            $buysize += floatval($res['total']);
          }
          if ($type == 'instock') {
            $invsize += floatval($res['total']);
          }
      }
    }

  }

      $style = 'text-align: left;';
      if (floatval($invsize) <= 0) {
        $style .= 'color: red';
      }
      $understock = floatval($minsize) - floatval($invsize);
      $addstock = 0;
      if ($understock > 0) {
        $addstock = floatval($fullsize) - floatval($invsize);
      }
      $total = floatval($buysize) + $addstock;

      if ($total > 0) {
          $notes = htmlspecialchars($res['notes']);
          list($total, $units) =  measure_unit_convert($total, $units, FALSE);
          list($total2, $units2) =  measure_unit_convert($total, $units, TRUE);
          if ($units == 'ct') {
              print "<p style='$style'><input type='checkbox' />$total $units - " . htmlspecialchars($name) . " - $notes</p>\n";
          } else {
              print "<p style='$style'><input type='checkbox' />$total $units/$total2 $units2 - " . htmlspecialchars($name) . " - $notes</p>\n";
          }
      }
}

$result = $db->query("SELECT productname FROM listitems LEFT JOIN products USING (productid) WHERE (units='' OR size='0') AND listitems.acctid='$GLIST_ACCT_ID' AND products.acctid='$GLIST_ACCT_ID' AND type='tobuy' AND listid=" . $db->quote($id) . " GROUP BY productid;");

if ($result->rowCount()) {
    print '<p>NOTE: This view does not include items on your list without Amount/Units values: ';
    // show missing items here
    $resultarray = array();
    foreach ($result as $row) {
        $resultarray[] = $row[0];
    }
    print implode(", ", $resultarray);
    print '</p>';
}

}

}

$db = NULL;

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

?>
Return current item: Grocery List