CORE-POS/IS4C

View on GitHub
fannie/classlib2.0/lib/PriceLib.php

Summary

Maintainability
D
2 days
Test Coverage
F
38%
<?php
/*******************************************************************************

    Copyright 2013 Whole Foods Co-op

    This file is part of CORE-POS.

    CORE-POS 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; either version 2 of the License, or
    (at your option) any later version.

    CORE-POS 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
    in the file license.txt along with IT CORE; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*********************************************************************************/

namespace COREPOS\Fannie\API\lib;

/**
  @class PriceLib
*/
class PriceLib 
{
    /**
     * Lookup the current sale status of item(s)
     * @param $dbc [SQLManager] database connection
     * @param $config [FannieConfig] configuration object
     * @param $upc [string|array] UPC(s)
     *      (may include batch-style LC### UPCs)
     * @return [array]
     *
     *  In single-store mode the array is simply keyed by UPC
     *  and the value is a record w/ pricemethod, salePrice, etc
     *
     *  In multi-store mode the array is keyed by UPC and the
     *  value is *another* array keyed by storeID. The value at
     *  $return[UPC][storeID] is a record w/ pricemethod, salePrice,
     *  etc. If the return value only has data from some storeIDs
     *  then that UPC is only on sale at some stores
     */
    public static function effectiveSalePrice($dbc, $config, $upc)
    {
        if (!is_array($upc)) {
            $upc = array($upc);
        }
        list($inStr, $args) = $dbc->safeInClause($upc);

        $query = "SELECT l.upc, 
                    l.batchID, 
                    l.pricemethod, 
                    l.salePrice, 
                    l.groupSalePrice,
                    l.quantity,
                    b.startDate, 
                    b.endDate, 
                    b.discounttype,
                    b.transLimit
                  FROM batches AS b
                    INNER JOIN batchList AS l ON b.batchID = l.batchID
                  WHERE b.discounttype > 0
                    AND l.upc IN ({$inStr})
                    AND ? BETWEEN b.startDate AND b.endDate
                  ORDER BY l.upc,
                    l.salePrice DESC";
        if ($config->get('STORE_MODE') === 'HQ') {
            $query = str_replace('WHERE', ' LEFT JOIN StoreBatchMap AS s ON b.batchID=s.batchID WHERE ', $query);
            $query = str_replace('SELECT', 'SELECT s.storeID,', $query);
        }
        $prep = $dbc->prepare($query);
        $args[] = date('Y-m-d 00:00:00');
        $res = $dbc->execute($prep, $args);
        $ret = array();
        while ($row = $dbc->fetchRow($res)) {
            if (isset($row['storeID'])) {
                if (!isset($ret[$row['upc']])) {
                    $ret[$row['upc']] = array();
                }
                $ret[$row['upc']][$row['storeID']] = $row;
            } else {
                $ret[$row['upc']] = $row;
            }
        }

        return $ret;
    }

    public static function pricePerUnit($price,$sizeStr)
    {
        $country = \FannieConfig::factory()->get('COUNTRY', 'US');

        $num = "";
        $unit = "";
        $mult = 1;
        $inNum = 1;
        for ($i=0; $i < strlen($sizeStr); $i++) {
            if ($inNum == 1) {
                if (is_numeric($sizeStr[$i]) or $sizeStr[$i] == ".") {
                    $num .= $sizeStr[$i];
                } else if ($sizeStr[$i] == "/" or $sizeStr[$i] == "-") {
                    $mult = $num;
                    $num = "";
                } else {
                    $inNum = 0;
                    $unit .= $sizeStr[$i];
                }
            } else {
                $unit .= $sizeStr[$i];
            }
        }

        $unit = ltrim($unit);
        $unit = strtoupper($unit);
        if (strpos($unit,"FL") !== False) {
            $unit = "FLOZ";
        }
        if ($num == "") {
            $num = 1;
        }
        $num = (float)$num;
        $num = $num*$mult;
        if ($num == 0) {
            return '';
        }

        switch($unit) {
            case '#':
            case 'LB':
            case 'LBS':    
                if ($country == "US") {
                    return round($price/($num*16),3)."/OZ";
                } else {
                    return round($price/($num*453.59),3)."/G";
                }
            case 'ML':
                if ($country == "US") {
                    return round($price/($num*0.034),3)."/OZ";
                } else {
                    return round($price/$num,3)."/ML";
                }
            case 'FLOZ':
                if ( $country == 'US' ) {
                    return round($price/$num,3)."/OZ";
                } else {
                    return round($price/($num*29.5735),3)."/ML"; 
                }
            case 'OZ':
            case 'Z':
                if ( $country == 'US' ) {
                    return round($price/$num,3)."/OZ";
                } else {
                    return round($price/($num*28.35),3)."/G"; 
                }
            case 'PINT':
            case 'PINTS':
                if ($country == "US") {
                    return round($price/($num*16),3)."/OZ";
                } else {
                    return round($price/($num*473.18),3)."/ML";
                }
            case 'GR':
            case 'GRAM':
            case 'GM':
            case 'GRM':
            case 'G':
                if ($country == "US"){
                    return round($price/($num*0.035),3)."/OZ";
                } else {
                    return round($price/$num,3)."/G";
                }
            case 'LTR':
            case 'L':
                if ($country == "US"){
                    return round($price/($num*33.814),3)."/OZ";
                } else {
                    return round($price/1000,3)."/ML";
                }
            case 'GAL':
                if ($country == "US") {
                    return round($price/($num*128),3)."/OZ";
                } else {
                    return round($price/($num*3785.41),3)."/ML";
                }
            default:
                return round($price/$num,3)."/".$unit;
        }

        return "";
    }
}