Exemplo n.º 1
0
function fn_rma_recalculate_order($order_id, $recalculate_type, $return_id, $is_refund, $ex_data)
{
    if (empty($recalculate_type) || empty($return_id) || empty($order_id) || !is_array($ex_data) || $recalculate_type == 'M' && !isset($ex_data['total'])) {
        return false;
    }
    $order = db_get_row("SELECT total, subtotal, discount, shipping_cost, status FROM ?:orders WHERE order_id = ?i", $order_id);
    $order_items = db_get_hash_array("SELECT * FROM ?:order_details WHERE ?:order_details.order_id = ?i", 'item_id', $order_id);
    $additional_data = db_get_hash_single_array("SELECT type, data FROM ?:order_data WHERE order_id = ?i", array('type', 'data'), $order_id);
    $order_return_info = @unserialize(@$additional_data[ORDER_DATA_RETURN]);
    $order_tax_info = @unserialize(@$additional_data['T']);
    $status_order = $order['status'];
    unset($order['status']);
    if ($recalculate_type == 'R') {
        $product_groups = @unserialize(@$additional_data['G']);
        if ($is_refund == 'Y') {
            $sign = $ex_data['inventory_to'] == 'I' ? -1 : 1;
            // What for is this section ???
            if (!empty($order_return_info['returned_products'])) {
                foreach ($order_return_info['returned_products'] as $item_id => $item) {
                    if (isset($item['extra']['returns'][$return_id])) {
                        $r_item = $o_item = $item;
                        unset($r_item['extra']['returns'][$return_id]);
                        $r_item['amount'] = $item['amount'] - $item['extra']['returns'][$return_id]['amount'];
                        fn_rma_recalculate_order_routine($order, $r_item, $item, 'O-', $ex_data);
                        if (empty($r_item['amount'])) {
                            unset($order_return_info['returned_products'][$item_id]);
                        } else {
                            $order_return_info['returned_products'][$item_id] = $r_item;
                        }
                        $o_item['primordial_amount'] = (isset($order_items[$item_id]) ? $order_items[$item_id]['amount'] : 0) + $item['extra']['returns'][$return_id]['amount'];
                        $o_item['primordial_discount'] = @$o_item['extra']['discount'];
                        fn_rma_recalculate_order_routine($order, $o_item, $item, 'M+', $ex_data);
                        $o_item['amount'] = (isset($order_items[$item_id]) ? $order_items[$item_id]['amount'] : 0) + $item['extra']['returns'][$return_id]['amount'];
                        if (isset($order_items[$item_id]['extra'])) {
                            $o_item['extra'] = @unserialize($order_items[$item_id]['extra']);
                        }
                        $o_item['extra']['returns'][$return_id] = $item['extra']['returns'][$return_id];
                        $o_item['extra'] = serialize($o_item['extra']);
                        if (!isset($order_items[$item_id])) {
                            db_query("REPLACE INTO ?:order_details ?e", $o_item);
                        } else {
                            db_query("UPDATE ?:order_details SET ?u WHERE item_id = ?i AND order_id = ?i", $o_item, $item_id, $order_id);
                        }
                    }
                }
            }
            // Check all the products and update their amount and cost.
            foreach ($order_items as $item_id => $item) {
                $item['extra'] = @unserialize($item['extra']);
                if (isset($item['extra']['returns'][$return_id])) {
                    $o_item = $item;
                    $o_item['amount'] = $o_item['amount'] + $sign * $item['extra']['returns'][$return_id]['amount'];
                    unset($o_item['extra']['returns'][$return_id]);
                    if (empty($o_item['extra']['returns'])) {
                        unset($o_item['extra']['returns']);
                    }
                    fn_rma_recalculate_order_routine($order, $o_item, $item, '', $ex_data);
                    if (empty($o_item['amount'])) {
                        db_query("DELETE FROM ?:order_details WHERE item_id = ?i AND order_id = ?i", $item_id, $order_id);
                    } else {
                        $o_item['extra'] = serialize($o_item['extra']);
                        db_query("UPDATE ?:order_details SET ?u WHERE item_id = ?i AND order_id = ?i", $o_item, $item_id, $order_id);
                    }
                    if (!isset($order_return_info['returned_products'][$item_id])) {
                        $r_item = $item;
                        unset($r_item['extra']['returns']);
                        $r_item['amount'] = $item['extra']['returns'][$return_id]['amount'];
                    } else {
                        $r_item = $order_return_info['returned_products'][$item_id];
                        $r_item['amount'] = $r_item['amount'] + $item['extra']['returns'][$return_id]['amount'];
                    }
                    fn_rma_recalculate_order_routine($order, $r_item, $item, 'M-O+', $ex_data);
                    $r_item['extra']['returns'][$return_id] = $item['extra']['returns'][$return_id];
                    $order_return_info['returned_products'][$item_id] = $r_item;
                    fn_rma_update_order_taxes($order_tax_info, $item_id, $item['amount'], $o_item['amount'], $order);
                }
            }
            $_ori_data = array('order_id' => $order_id, 'type' => ORDER_DATA_RETURN, 'data' => $order_return_info);
        }
        $shipping_info = array();
        if ($product_groups) {
            $_total = 0;
            foreach ($product_groups as $key_group => $group) {
                if (isset($group['chosen_shippings'])) {
                    foreach ($group['chosen_shippings'] as $key_shipping => $shipping) {
                        $_total += $shipping['rate'];
                    }
                }
            }
            foreach ($product_groups as $key_group => $group) {
                if (isset($group['chosen_shippings'])) {
                    foreach ((array) $ex_data['shipping_costs'] as $shipping_id => $cost) {
                        foreach ($group['chosen_shippings'] as $key_shipping => $shipping) {
                            $shipping_id = $shipping['shipping_id'];
                            $product_groups[$key_group]['chosen_shippings'][$key_shipping]['rate'] = fn_format_price($_total ? $shipping['rate'] / $_total * $cost : $cost / count($product_groups));
                            $product_groups[$key_group]['shippings'][$shipping_id]['rate'] = fn_format_price($_total ? $shipping['rate'] / $_total * $cost : $cost / count($product_groups));
                            if (empty($shipping_info[$shipping_id])) {
                                $shipping_info[$shipping_id] = $product_groups[$key_group]['shippings'][$shipping_id];
                            }
                            $shipping_info[$shipping_id]['rates'][$key_group] = $product_groups[$key_group]['shippings'][$shipping_id]['rate'];
                        }
                    }
                }
            }
            db_query("UPDATE ?:order_data SET ?u WHERE order_id = ?i AND type = 'G'", array('data' => serialize($product_groups)), $order_id);
            fn_update_shipping_taxes($order_tax_info, $shipping_info, $order);
        }
        $order['total'] -= $order['shipping_cost'];
        $order['shipping_cost'] = isset($ex_data['shipping_costs']) ? array_sum($ex_data['shipping_costs']) : $order['shipping_cost'];
        $order['total'] += $order['shipping_cost'];
        $order['total'] = $order['total'] < 0 ? 0 : $order['total'];
        if (!empty($order_tax_info)) {
            db_query("UPDATE ?:order_data SET ?u WHERE order_id = ?i AND type = 'T'", array('data' => serialize($order_tax_info)), $order_id);
        }
    } elseif ($recalculate_type == 'M') {
        $order['total'] = $order['total'] + $ex_data['total'];
        $_ori_data = array('order_id' => $order_id, 'type' => ORDER_DATA_RETURN, 'data' => array('return' => fn_format_price((isset($order_return_info['return']) ? $order_return_info['return'] : 0) - $ex_data['total']), 'returned_products' => isset($order_return_info['returned_products']) ? $order_return_info['returned_products'] : ''));
        $return_products = db_get_hash_array("SELECT * FROM ?:rma_return_products WHERE return_id = ?i AND type = ?s", 'item_id', $return_id, RETURN_PRODUCT_ACCEPTED);
        foreach ((array) $return_products as $item_id => $v) {
            $v['extra']['product_options'] = @unserialize($v['extra']['product_options']);
            if ($ex_data['inventory_to'] == 'D' || $ex_data['status_to'] == RMA_DEFAULT_STATUS) {
                fn_update_product_amount($v['product_id'], $v['amount'], @$v['extra']['product_options'], '-');
            } elseif ($ex_data['inventory_to'] == 'I') {
                fn_update_product_amount($v['product_id'], $v['amount'], $v['extra']['product_options'], '+');
            }
        }
    }
    if ($is_refund == 'Y') {
        if (isset($_ori_data['data']['return']) && floatval($_ori_data['data']['return']) == 0) {
            unset($_ori_data['data']['return']);
        }
        if (empty($_ori_data['data']['returned_products'])) {
            unset($_ori_data['data']['returned_products']);
        }
        if (!empty($_ori_data['data'])) {
            $_ori_data['data'] = serialize($_ori_data['data']);
            db_query("REPLACE INTO ?:order_data ?e", $_ori_data);
        } else {
            db_query("DELETE FROM ?:order_data WHERE order_id = ?i AND type = ?s", $order_id, ORDER_DATA_RETURN);
        }
    }
    foreach ($order as $k => $v) {
        $order[$k] = fn_format_price($v);
    }
    db_query("UPDATE ?:orders SET ?u WHERE order_id = ?i", $order, $order_id);
    if (fn_allowed_for('MULTIVENDOR')) {
        $_SESSION['cart'] = isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
        $cart =& $_SESSION['cart'];
        $action = 'save';
        fn_mve_place_order($order_id, $action, $status_order, $cart);
    }
}
Exemplo n.º 2
0
function fn_rma_recalculate_order($order_id, $recalculate_type, $return_id, $is_refund, $ex_data)
{
    if (empty($recalculate_type) || empty($return_id) || empty($order_id) || !is_array($ex_data) || $recalculate_type == 'R' && !isset($ex_data['shipping_costs']) || $recalculate_type == 'M' && !isset($ex_data['total'])) {
        return false;
    }
    $order = db_get_row("SELECT total, subtotal, discount, shipping_cost FROM ?:orders WHERE order_id = ?i", $order_id);
    $order_items = db_get_hash_array("SELECT * FROM ?:order_details WHERE ?:order_details.order_id = ?i", 'item_id', $order_id);
    $additional_data = db_get_hash_single_array("SELECT type, data FROM ?:order_data WHERE order_id = ?i", array('type', 'data'), $order_id);
    $order_return_info = @unserialize(@$additional_data[ORDER_DATA_RETURN]);
    $order_tax_info = @unserialize(@$additional_data['T']);
    if ($recalculate_type == 'R') {
        $shipping_info = @unserialize(@$additional_data['L']);
        if ($is_refund == 'Y') {
            $sign = $ex_data['inventory_to'] == 'I' ? -1 : 1;
            // What for is this section ???
            if (!empty($order_return_info['returned_products'])) {
                foreach ($order_return_info['returned_products'] as $item_id => $item) {
                    if (isset($item['extra']['returns'][$return_id])) {
                        $r_item = $o_item = $item;
                        unset($r_item['extra']['returns'][$return_id]);
                        $r_item['amount'] = $item['amount'] - $item['extra']['returns'][$return_id]['amount'];
                        fn_rma_recalculate_order_routine($order, $r_item, $item, 'O-', $ex_data);
                        if (empty($r_item['amount'])) {
                            unset($order_return_info['returned_products'][$item_id]);
                        } else {
                            $order_return_info['returned_products'][$item_id] = $r_item;
                        }
                        $o_item['primordial_amount'] = (isset($order_items[$item_id]) ? $order_items[$item_id]['amount'] : 0) + $item['extra']['returns'][$return_id]['amount'];
                        $o_item['primordial_discount'] = @$o_item['extra']['discount'];
                        fn_rma_recalculate_order_routine($order, $o_item, $item, 'M+');
                        $o_item['amount'] = (isset($order_items[$item_id]) ? $order_items[$item_id]['amount'] : 0) + $item['extra']['returns'][$return_id]['amount'];
                        if (isset($order_items[$item_id]['extra'])) {
                            $o_item['extra'] = @unserialize($order_items[$item_id]['extra']);
                        }
                        $o_item['extra']['returns'][$return_id] = $item['extra']['returns'][$return_id];
                        $o_item['extra'] = serialize($o_item['extra']);
                        $o_item = fn_check_table_fields($o_item, 'order_details');
                        if (!isset($order_items[$item_id])) {
                            db_query("REPLACE INTO ?:order_details ?e", $o_item);
                        } else {
                            db_query("UPDATE ?:order_details SET ?u WHERE item_id = ?i AND order_id = ?i", $o_item, $item_id, $order_id);
                        }
                    }
                }
            }
            // Check all the products and update their amount and cost.
            foreach ($order_items as $item_id => $item) {
                $item['extra'] = @unserialize($item['extra']);
                if (isset($item['extra']['returns'][$return_id])) {
                    $o_item = $item;
                    $o_item['amount'] = $o_item['amount'] + $sign * $item['extra']['returns'][$return_id]['amount'];
                    unset($o_item['extra']['returns'][$return_id]);
                    if (empty($o_item['extra']['returns'])) {
                        unset($o_item['extra']['returns']);
                    }
                    fn_rma_recalculate_order_routine($order, $o_item, $item);
                    if (empty($o_item['amount'])) {
                        db_query("DELETE FROM ?:order_details WHERE item_id = ?i AND order_id = ?i", $item_id, $order_id);
                    } else {
                        $o_item['extra'] = serialize($o_item['extra']);
                        $o_item = fn_check_table_fields($o_item, 'order_details');
                        db_query("UPDATE ?:order_details SET ?u WHERE item_id = ?i AND order_id = ?i", $o_item, $item_id, $order_id);
                    }
                    if (!isset($order_return_info['returned_products'][$item_id])) {
                        $r_item = $item;
                        unset($r_item['extra']['returns']);
                        $r_item['amount'] = $item['extra']['returns'][$return_id]['amount'];
                    } else {
                        $r_item = $order_return_info['returned_products'][$item_id];
                        $r_item['amount'] = $r_item['amount'] + $item['extra']['returns'][$return_id]['amount'];
                    }
                    fn_rma_recalculate_order_routine($order, $r_item, $item, 'M-O+', $ex_data);
                    $r_item['extra']['returns'][$return_id] = $item['extra']['returns'][$return_id];
                    $order_return_info['returned_products'][$item_id] = $r_item;
                    fn_rma_update_order_taxes($order_tax_info, $item_id, $item['amount'], $o_item['amount'], $order);
                }
            }
            $_ori_data = array('order_id' => $order_id, 'type' => ORDER_DATA_RETURN, 'data' => $order_return_info);
        }
        if ($shipping_info) {
            foreach ((array) $ex_data['shipping_costs'] as $shipping_id => $cost) {
                $_total = array_sum($shipping_info[$shipping_id]['rates']);
                foreach ($shipping_info[$shipping_id]['rates'] as $s_id => $rate) {
                    $shipping_info[$shipping_id]['rates'][$s_id] = fn_format_price($_total ? $rate / $_total * $cost : $cost / count($shipping_info[$shipping_id]['rates']));
                }
                // Correct cost FIXME
                if ($cost != ($new_total = array_sum($shipping_info[$shipping_id]['rates']))) {
                    $deviation = $new_total - $cost;
                    foreach ($shipping_info[$shipping_id]['rates'] as $s_id => $rate) {
                        $shipping_info[$shipping_id]['rates'][$s_id] = fn_format_price($rate + $deviation);
                        break;
                    }
                }
            }
            db_query("UPDATE ?:order_data SET ?u WHERE order_id = ?i AND type = 'L'", array('data' => serialize($shipping_info)), $order_id);
            fn_update_shipping_taxes($order_tax_info, $shipping_info, $order);
        }
        $order['total'] -= $order['shipping_cost'];
        $order['shipping_cost'] = array_sum($ex_data['shipping_costs']);
        $order['total'] += $order['shipping_cost'];
        if (!empty($order_tax_info)) {
            db_query("UPDATE ?:order_data SET ?u WHERE order_id = ?i AND type = 'T'", array('data' => serialize($order_tax_info)), $order_id);
        }
    } elseif ($recalculate_type == 'M') {
        $order['total'] = $order['total'] + $ex_data['total'];
        $_ori_data = array('order_id' => $order_id, 'type' => ORDER_DATA_RETURN, 'data' => array('return' => fn_format_price((isset($order_return_info['return']) ? $order_return_info['return'] : 0) - $ex_data['total']), 'returned_products' => isset($order_return_info['returned_products']) ? $order_return_info['returned_products'] : ''));
        $return_products = db_get_hash_array("SELECT * FROM ?:rma_return_products WHERE return_id = ?i AND type = ?s", 'item_id', $return_id, RETURN_PRODUCT_ACCEPTED);
        foreach ((array) $return_products as $item_id => $v) {
            $v['extra']['product_options'] = @unserialize($v['extra']['product_options']);
            if ($ex_data['inventory_to'] == 'D' || $ex_data['status_to'] == RMA_DEFAULT_STATUS) {
                fn_update_product_amount($v['product_id'], $v['amount'], @$v['extra']['product_options'], '-');
            } elseif ($ex_data['inventory_to'] == 'I') {
                fn_update_product_amount($v['product_id'], $v['amount'], $v['extra']['product_options'], '+');
            }
        }
    }
    if ($is_refund == 'Y') {
        if (isset($_ori_data['data']['return']) && floatval($_ori_data['data']['return']) == 0) {
            unset($_ori_data['data']['return']);
        }
        if (empty($_ori_data['data']['returned_products'])) {
            unset($_ori_data['data']['returned_products']);
        }
        if (!empty($_ori_data['data'])) {
            $_ori_data['data'] = serialize($_ori_data['data']);
            db_query("REPLACE INTO ?:order_data ?e", $_ori_data);
        } else {
            db_query("DELETE FROM ?:order_data WHERE order_id = ?i AND type = ?s", $order_id, ORDER_DATA_RETURN);
        }
    }
    foreach ($order as $k => $v) {
        $order[$k] = fn_format_price($v);
    }
    db_query("UPDATE ?:orders SET ?u WHERE order_id = ?i", $order, $order_id);
}