Example #1
0
/**
 * Calculates the data needed for the offer boxes
 *
 * @param array $request the request from the POST or the SESSIOn
 * @param array $locations list with locations with the units being delivered
 * @return array values to be used later on
 */
function html_offer_box($request, $locations)
{
    // Calculating prices
    $headers = product_feature_all(true);
    $rows = array();
    $product_prices = array();
    $variants = isset($request['VARIANT']) ? array_unique($request['VARIANT']) : array();
    $copies = array();
    $estimated_delivery = array();
    $estimated_production = array();
    $runs = array();
    // Getting the prices for each variant
    foreach ($variants as $index => $_product) {
        $product_prices[$_product] = product_get_all_prices($_product, $_SESSION['user']['PARTY_ID'], true);
        $copies[$_product] = product_assoc_get($_product, PRODUCT_ASSOC_PAIR, true);
    }
    // Getting the price of the pair products in this case the
    // copies it will be most probably be always the same case
    // where the copies are the procuts
    foreach ($copies as $_main_product => &$_pair_product) {
        $_pair_product[$_pair_product[0]['PRODUCT_ID']] = product_get_all_prices($_pair_product[0]['PRODUCT_ID'], $_SESSION['user']['PARTY_ID'], true);
    }
    $delivery = 0.0;
    $units = 0;
    if (!empty($request['MOTIVES'])) {
        foreach ($request['MOTIVES'] as $index => $_motive) {
            if ($_motive) {
                $new_row = array();
                foreach ($headers as $_header) {
                    $new_row[$_header] = $_motive * product_price_by_header($request['VARIANT'][$index], $product_prices[$request['VARIANT'][$index]]['FEATURES'], $_header);
                    $copy_price = product_price_by_header($copies[$request['VARIANT'][$index]][0]['PRODUCT_ID'], $copies[$request['VARIANT'][$index]][$copies[$request['VARIANT'][$index]][0]['PRODUCT_ID']]['FEATURES'], $_header);
                    $new_row[$_header] += $request['RUNS'][$index] * $copy_price;
                    $units += $request['RUNS'][$index];
                }
                // The mixed index prices is the lowest price of all production lines
                // so the customer can have some super nice and low price but first we
                // remove all prices that are 0-s
                foreach ($new_row as $_new_row_header => $_new_row) {
                    if (!$_new_row) {
                        unset($new_row[$_new_row_header]);
                    }
                }
                $new_row['MIXED'] = min($new_row);
                $rows[$index] = $new_row;
            }
        }
    }
    foreach ($locations as $_units) {
        $delivery += shipment_cost_estimate($_units);
    }
    // Adding the MIXED header to all calculations
    array_push($headers, 'MIXED');
    return array($headers, $rows, $variants, $copies, $delivery);
}
Example #2
0
/**
 * Returns the shipment costs for a certain amount of items
 *
 * @param integer $units the number of items ordered
 * @return array list with data about the deliveries
 */
function shipment_cost_estimate($units)
{
    // There is no price for no items
    if (!$units) {
        return 0.0;
    }
    $query = "SELECT sce.SHIPMENT_METHOD_TYPE_ID, sce.QUANTITY_BREAK_ID, sce.QUANTITY_UNIT_PRICE, sce.PRICE_UOM_ID, qb.FROM_QUANTITY, qb.THRU_QUANTITY\n\t\t\t  FROM shipment_cost_estimate sce JOIN quantity_break qb\n\t\t\t  ON sce.QUANTITY_BREAK_ID = qb.QUANTITY_BREAK_ID\n\t\t\t  WHERE qb.FROM_QUANTITY <= " . esc($units) . " AND qb.THRU_QUANTITY >= " . esc($units);
    $data = db_query_to_row($query);
    // It was so much quantity that it could not fit on a single delivery method
    if (empty($data)) {
        $max = shipment_get_max_break();
        $max_times = floor($units / $max);
        $left = $units % $max;
        $max_deliveries = shipment_cost_estimate($max);
        $left_delivery = shipment_cost_estimate($left);
        return (double) $max_deliveries * $max_times + (double) $left_delivery;
    }
    // We could find a single delivery method for this shipment
    return (double) $data['QUANTITY_UNIT_PRICE'];
}