// Prepare customer info
 //
 $profile_fields = fn_get_profile_fields('O', $customer_auth);
 $cart['profile_id'] = empty($cart['profile_id']) ? 0 : $cart['profile_id'];
 Registry::get('view')->assign('profile_fields', $profile_fields);
 //Get user profiles
 $user_profiles = fn_get_user_profiles($customer_auth['user_id']);
 Registry::get('view')->assign('user_profiles', $user_profiles);
 //Get countries and states
 Registry::get('view')->assign('countries', fn_get_simple_countries(true, CART_LANGUAGE));
 Registry::get('view')->assign('states', fn_get_all_states());
 Registry::get('view')->assign('usergroups', fn_get_usergroups('C', DESCR_SL));
 if (!empty($customer_auth['user_id']) && (empty($cart['user_data']) || !empty($_REQUEST['profile_id']) && $cart['profile_id'] != $_REQUEST['profile_id'])) {
     $cart['profile_id'] = !empty($_REQUEST['profile_id']) ? $_REQUEST['profile_id'] : 0;
     $cart['user_data'] = fn_get_user_info($customer_auth['user_id'], true, $cart['profile_id']);
     fn_filter_hidden_profile_fields($cart['user_data'], 'O');
 }
 if (!empty($cart['user_data'])) {
     $cart['ship_to_another'] = fn_check_shipping_billing($cart['user_data'], $profile_fields);
 }
 //
 // Get products info
 // and shipping rates
 //
 // Clean up saved shipping rates
 // unset($_SESSION['shipping_rates']);
 if (!empty($shipping_rates)) {
     define('CACHED_SHIPPING_RATES', true);
 }
 $cart['calculate_shipping'] = true;
 // calculate cart - get products with options, full shipping rates info and promotions
Ejemplo n.º 2
0
function fn_update_order(&$cart, $order_id = 0)
{
    fn_set_hook('pre_update_order', $cart, $order_id);
    $cart['user_data'] = isset($cart['user_data']) ? $cart['user_data'] : array();
    $order = fn_array_merge($cart, $cart['user_data']);
    unset($order['user_data']);
    // filter hidden fields, which were hidden to checkout
    fn_filter_hidden_profile_fields($order, 'O');
    $order = fn_fill_contact_info_from_address($order);
    if (empty($order['profile_id'])) {
        $order['profile_id'] = 0;
    }
    if (!empty($cart['shipping'])) {
        $order['shipping_ids'] = fn_create_set(array_keys($cart['shipping']));
    }
    if (!empty($cart['payment_surcharge'])) {
        $cart['total'] += $cart['payment_surcharge'];
        $order['total'] = $cart['total'];
        if (fn_allowed_for('MULTIVENDOR')) {
            $cart['companies'] = fn_get_products_companies($cart['products']);
            $take_payment_surcharge_from_vendor = fn_take_payment_surcharge_from_vendor($cart['products']);
            if (Registry::get('settings.Vendors.include_payment_surcharge') == 'Y' && $take_payment_surcharge_from_vendor) {
                $cart['companies_count'] = count($cart['companies']);
                $cart['total'] -= $cart['payment_surcharge'];
                $order['total'] = $cart['total'];
            }
        }
    }
    if (empty($order_id)) {
        $ip = fn_get_ip();
        $order['ip_address'] = fn_ip_to_db($ip['host']);
        $order['timestamp'] = TIME;
        $order['lang_code'] = CART_LANGUAGE;
        $order['company_id'] = 0;
        $order['status'] = STATUS_INCOMPLETED_ORDER;
        // incomplete by default to increase inventory
        $order_status = $order['status'];
        if (count($order['product_groups']) > 1 && !$order['parent_order_id']) {
            $order['is_parent_order'] = 'Y';
            $order['status'] = STATUS_PARENT_ORDER;
        } elseif (!empty($order['product_groups'][0]['company_id'])) {
            $order['is_parent_order'] = 'N';
            $order['company_id'] = $order['product_groups'][0]['company_id'];
        }
        if (fn_allowed_for('ULTIMATE')) {
            if (Registry::get('runtime.company_id') == 0) {
                fn_set_notification('E', __('error'), __('text_select_vendor'));
                return false;
            } else {
                $order['company_id'] = Registry::get('runtime.company_id');
            }
        }
        if (defined('CART_LOCALIZATION')) {
            $order['localization_id'] = CART_LOCALIZATION;
        }
        if (!empty($cart['rewrite_order_id'])) {
            $order['order_id'] = array_shift($cart['rewrite_order_id']);
        }
        $order['promotions'] = serialize(!empty($cart['promotions']) ? $cart['promotions'] : array());
        if (!empty($cart['promotions'])) {
            $order['promotion_ids'] = fn_create_set(array_keys($cart['promotions']));
        }
        fn_set_hook('create_order', $order);
        $order_id = db_query("INSERT INTO ?:orders ?e", $order);
    } else {
        unset($order['order_id'], $order['timestamp']);
        // We're editing existing order
        if (isset($cart['promotions'])) {
            $order['promotions'] = serialize($cart['promotions']);
            $order['promotion_ids'] = fn_create_set(array_keys($cart['promotions']));
        }
        $old_order = db_get_row("SELECT company_id, payment_id, status FROM ?:orders WHERE order_id = ?i", $order_id);
        $order['status'] = $old_order['status'];
        $order['company_id'] = $old_order['company_id'];
        $order_status = $order['status'];
        if (!empty($cart['payment_id']) && $cart['payment_id'] == $old_order['payment_id']) {
            $payment_info = db_get_field("SELECT data FROM ?:order_data WHERE order_id = ?i AND type = 'P'", $order_id);
            if (!empty($payment_info)) {
                $payment_info = unserialize(fn_decrypt_text($payment_info));
                $cart['payment_info'] = array_merge($payment_info, !empty($cart['payment_info']) ? $cart['payment_info'] : array());
            }
        }
        // incomplete the order to increase inventory amount.
        fn_change_order_status($order_id, STATUS_INCOMPLETED_ORDER, $old_order['status'], fn_get_notification_rules(array(), false));
        if (fn_allowed_for('MULTIVENDOR') && empty($order['parent_order_id'])) {
            $order['status'] = STATUS_PARENT_ORDER;
        } else {
            $order['status'] = STATUS_INCOMPLETED_ORDER;
        }
        fn_set_hook('edit_place_order', $order_id);
        $profile_fields = db_get_hash_array("SELECT field_id, value FROM ?:profile_fields_data WHERE object_id = ?i AND object_type = 'O'", 'field_id', $order_id);
        foreach ($profile_fields as $k => $v) {
            if (!isset($cart['user_data']['fields'][$k])) {
                $cart['user_data']['fields'][$k] = $v['value'];
            }
        }
        fn_set_hook('update_order', $order, $order_id);
        db_query("UPDATE ?:orders SET ?u WHERE order_id = ?i", $order, $order_id);
        if (!empty($order['products'])) {
            db_query("DELETE FROM ?:order_details WHERE order_id = ?i", $order_id);
        }
    }
    fn_store_profile_fields($cart['user_data'], $order_id, 'O');
    fn_create_order_details($order_id, $cart);
    fn_update_order_data($order_id, $cart);
    // Log order creation/update
    $log_action = !empty($order['order_id']) ? 'update' : 'create';
    fn_log_event('orders', $log_action, array('order_id' => $order_id));
    //
    // Place the order_id to new_orders table for all admin profiles
    //
    if (empty($order['parent_order_id'])) {
        $condition = "";
        if (fn_allowed_for('ULTIMATE')) {
            $condition = " AND user_type = 'A'";
        }
        if (fn_allowed_for('MULTIVENDOR')) {
            $condition = db_quote(" AND (user_type = 'A' OR (user_type = 'V' AND ?:users.company_id = ?i))", $order['company_id']);
        }
        $admins = db_get_fields("SELECT user_id FROM ?:users WHERE 1 {$condition}");
        foreach ($admins as $k => $v) {
            db_query("REPLACE INTO ?:new_orders (order_id, user_id) VALUES (?i, ?i)", $order_id, $v);
        }
    }
    return array($order_id, $order_status);
}
Ejemplo n.º 3
0
/**
 * Order placing function
 *
 * @param array $cart
 * @param array $auth
 * @param string $action
 * @return int order_id or bool FALSE
 */
function fn_place_order(&$cart, &$auth, $action = '', $parent_order_id = 0)
{
    $allow = true;
    fn_set_hook('pre_place_order', $cart, $allow);
    if ($allow == true && !fn_cart_is_empty($cart)) {
        $ip = fn_get_ip();
        $__order_status = STATUS_INCOMPLETED_ORDER;
        $order = fn_check_table_fields($cart, 'orders');
        $order = fn_array_merge($order, fn_check_table_fields($cart['user_data'], 'orders'));
        // filter hidden fields, which were hidden to checkout
        fn_filter_hidden_profile_fields($order, 'O');
        // If the contact information fields were disabled, fill the information from the billing/shipping
        Registry::get('settings.General.address_position') == 'billing_first' ? $address_zone = 'b' : ($address_zone = 's');
        if (!empty($order['firstname']) || !empty($order[$address_zone . '_firstname'])) {
            $order['firstname'] = empty($order['firstname']) && !empty($order[$address_zone . '_firstname']) ? $order[$address_zone . '_firstname'] : $order['firstname'];
        }
        if (!empty($order['lastname']) || !empty($order[$address_zone . '_lastname'])) {
            $order['lastname'] = empty($order['lastname']) && !empty($order[$address_zone . '_lastname']) ? $order[$address_zone . '_lastname'] : $order['lastname'];
        }
        if (!empty($order['phone']) || !empty($order[$address_zone . '_phone'])) {
            $order['phone'] = empty($order['phone']) && !empty($order[$address_zone . '_phone']) ? $order[$address_zone . '_phone'] : $order['phone'];
        }
        $order['user_id'] = $auth['user_id'];
        $order['timestamp'] = TIME;
        $order['lang_code'] = CART_LANGUAGE;
        $order['tax_exempt'] = $auth['tax_exempt'];
        $order['status'] = STATUS_INCOMPLETED_ORDER;
        // incomplete by default to increase inventory
        $order['ip_address'] = $ip['host'];
        $cart['companies'] = fn_get_products_companies($cart['products']);
        $order['is_parent_order'] = 'N';
        if (PRODUCT_TYPE == 'MULTIVENDOR') {
            $order['parent_order_id'] = $parent_order_id;
            if (count($cart['companies']) > 1) {
                $order['is_parent_order'] = 'Y';
                $__order_status = $order['status'] = STATUS_PARENT_ORDER;
            } else {
                $order['company_id'] = key($cart['companies']);
            }
        }
        $order['promotions'] = serialize(!empty($cart['promotions']) ? $cart['promotions'] : array());
        if (!empty($cart['promotions'])) {
            $order['promotion_ids'] = implode(', ', array_keys($cart['promotions']));
        }
        $order['shipping_ids'] = !empty($cart['shipping']) ? fn_create_set(array_keys($cart['shipping'])) : '';
        if (!empty($cart['payment_surcharge'])) {
            $cart['total'] += $cart['payment_surcharge'];
            $order['total'] = $cart['total'];
        }
        if (!empty($cart['payment_info'])) {
            $ccards = fn_get_static_data_section('C', true);
            if (!empty($cart['payment_info']['card']) && !empty($ccards[$cart['payment_info']['card']])) {
                // Check if cvv2 number required and unset it if not
                if ($ccards[$cart['payment_info']['card']]['param_2'] != 'Y') {
                    unset($cart['payment_info']['cvv2']);
                }
                // Check if start date exists and required and convert it to string
                if ($ccards[$cart['payment_info']['card']]['param_3'] != 'Y') {
                    unset($cart['payment_info']['start_year'], $cart['payment_info']['start_month']);
                }
                // Check if issue number required
                if ($ccards[$cart['payment_info']['card']]['param_4'] != 'Y') {
                    unset($cart['payment_info']['issue_number']);
                }
            }
        }
        // We're editing existing order
        if (!empty($order['order_id']) && $order['is_parent_order'] != 'Y') {
            $_tmp = db_get_row("SELECT status, ip_address, details, timestamp, lang_code FROM ?:orders WHERE order_id = ?i", $order['order_id']);
            $order['ip_address'] = $_tmp['ip_address'];
            // Leave original customers IP address
            $order['details'] = $_tmp['details'];
            // Leave order details
            $order['timestamp'] = $_tmp['timestamp'];
            // Leave the original date
            $order['lang_code'] = $_tmp['lang_code'];
            // Leave the original language
            if ($action == 'save') {
                $__order_status = $_tmp['status'];
                // Get the original order status
            }
            fn_change_order_status($order['order_id'], STATUS_INCOMPLETED_ORDER, $_tmp['status'], fn_get_notification_rules(array(), false));
            // incomplete the order to increase inventory amount.
            db_query("DELETE FROM ?:orders WHERE order_id = ?i", $order['order_id']);
            db_query("DELETE FROM ?:order_details WHERE order_id = ?i", $order['order_id']);
            db_query("DELETE FROM ?:profile_fields_data WHERE object_id = ?i AND object_type = 'O'", $order['order_id']);
            db_query("DELETE FROM ?:order_data WHERE order_id = ?i AND type IN ('T', 'C', 'P')", $order['order_id']);
            fn_set_hook('edit_place_order', $order['order_id']);
        }
        if (!empty($cart['rewrite_order_id'])) {
            $order['order_id'] = array_shift($cart['rewrite_order_id']);
        }
        $order['referer'] = $_SESSION['referer'];
        $order_id = db_query("INSERT INTO ?:orders ?e", $order);
        // Log order creation
        fn_log_event('orders', 'create', array('order_id' => $order_id));
        fn_store_profile_fields($cart['user_data'], $order_id, 'O');
        $order['order_id'] = $order_id;
        // If customer is not logged in, store order ids in the session
        if (empty($auth['user_id'])) {
            $auth['order_ids'][] = $order_id;
        }
        // Add order details data
        if (!empty($order_id)) {
            if (!empty($cart['products'])) {
                foreach ((array) $cart['products'] as $k => $v) {
                    $product_code = '';
                    $extra = empty($v['extra']) ? array() : $v['extra'];
                    $v['discount'] = empty($v['discount']) ? 0 : $v['discount'];
                    $extra['product'] = empty($v['product']) ? fn_get_product_name($v['product_id']) : $v['product'];
                    $extra['company_id'] = $v['company_id'];
                    if (isset($v['is_edp'])) {
                        $extra['is_edp'] = $v['is_edp'];
                    }
                    if (isset($v['edp_shipping'])) {
                        $extra['edp_shipping'] = $v['edp_shipping'];
                    }
                    if (!empty($v['discount'])) {
                        $extra['discount'] = $v['discount'];
                    }
                    if (isset($v['base_price'])) {
                        $extra['base_price'] = floatval($v['base_price']);
                    }
                    if (!empty($v['promotions'])) {
                        $extra['promotions'] = $v['promotions'];
                    }
                    if (!empty($v['stored_price']) && $v['stored_price'] == 'Y') {
                        $extra['stored_price'] = 'Y';
                    }
                    if (!empty($v['product_options'])) {
                        $_options = fn_get_product_options($v['product_id']);
                        if (!empty($_options)) {
                            foreach ($_options as $option_id => $option) {
                                if (!isset($v['product_options'][$option_id])) {
                                    $v['product_options'][$option_id] = '';
                                }
                            }
                        }
                        $extra['product_options'] = $v['product_options'];
                        $cart_id = fn_generate_cart_id($v['product_id'], array('product_options' => $v['product_options']), true);
                        $tracking = db_get_field("SELECT tracking FROM ?:products WHERE product_id = ?i", $v['product_id']);
                        if ($tracking == 'O') {
                            $product_code = db_get_field("SELECT product_code FROM ?:product_options_inventory WHERE combination_hash = ?i", $cart_id);
                        }
                        $extra['product_options_value'] = fn_get_selected_product_options_info($v['product_options']);
                    } else {
                        $v['product_options'] = array();
                    }
                    if (empty($product_code)) {
                        $product_code = db_get_field("SELECT product_code FROM ?:products WHERE product_id = ?i", $v['product_id']);
                    }
                    // Check the cart custom files
                    if (isset($extra['custom_files'])) {
                        $dir_path = DIR_CUSTOM_FILES . 'order_data/' . $order_id;
                        $sess_dir_path = DIR_CUSTOM_FILES . 'sess_data';
                        if (!is_dir($dir_path)) {
                            fn_mkdir($dir_path);
                        }
                        foreach ($extra['custom_files'] as $option_id => $files) {
                            if (is_array($files)) {
                                foreach ($files as $file_id => $file) {
                                    $file['path'] = $sess_dir_path . '/' . basename($file['path']);
                                    fn_copy($file['path'], $dir_path . '/' . $file['file']);
                                    fn_rm($file['path']);
                                    fn_rm($file['path'] . '_thumb');
                                    $extra['custom_files'][$option_id][$file_id]['path'] = $dir_path . '/' . $file['file'];
                                }
                            }
                        }
                    }
                    $order_details = array('item_id' => $k, 'order_id' => $order_id, 'product_id' => $v['product_id'], 'product_code' => $product_code, 'price' => !empty($v['stored_price']) && $v['stored_price'] == 'Y' ? $v['price'] - $v['discount'] : $v['price'], 'amount' => $v['amount'], 'extra' => serialize($extra));
                    db_query("INSERT INTO ?:order_details ?e", $order_details);
                    // Increase product popularity
                    $_data = array('product_id' => $v['product_id'], 'bought' => 1, 'total' => POPULARITY_BUY);
                    db_query("INSERT INTO ?:product_popularity ?e ON DUPLICATE KEY UPDATE bought = bought + 1, total = total + ?i", $_data, POPULARITY_BUY);
                }
            }
            // Save shipping information
            if (!empty($cart['shipping'])) {
                if ($_SESSION['selfService']) {
                    foreach ($cart['shipping'] as $sh_id => $_d) {
                        $cart['shipping'][$sh_id]['selfService'] = $_SESSION['selfService'];
                    }
                }
                // Get carriers and tracking number
                $data = db_get_field("SELECT data FROM ?:order_data WHERE order_id = ?i AND type = 'L'", $order_id);
                if (!empty($data)) {
                    $data = unserialize($data);
                    foreach ($cart['shipping'] as $sh_id => $_d) {
                        if (!empty($data[$sh_id]['carrier'])) {
                            $cart['shipping'][$sh_id]['carrier'] = $data[$sh_id]['carrier'];
                        }
                        if (!empty($data[$sh_id]['tracking_number'])) {
                            $cart['shipping'][$sh_id]['tracking_number'] = $data[$sh_id]['tracking_number'];
                        }
                    }
                }
                $_data = array('order_id' => $order_id, 'type' => 'L', 'data' => serialize($cart['shipping']));
                db_query("REPLACE INTO ?:order_data ?e", $_data);
            }
            // Save taxes
            if (!empty($cart['taxes'])) {
                $_data = array('order_id' => $order_id, 'type' => 'T', 'data' => serialize($cart['taxes']));
                db_query("REPLACE INTO ?:order_data ?e", $_data);
            }
            // Save payment information
            if (!empty($cart['payment_info'])) {
                $_data = array('order_id' => $order_id, 'type' => 'P', 'data' => fn_encrypt_text(serialize($cart['payment_info'])));
                db_query("REPLACE INTO ?:order_data ?e", $_data);
            }
            // Save coupons information
            if (!empty($cart['coupons'])) {
                $_data = array('order_id' => $order_id, 'type' => 'C', 'data' => serialize($cart['coupons']));
                db_query("REPLACE INTO ?:order_data ?e", $_data);
            }
            // Save secondary currency (for order notifications from payments with feedback requests)
            $_data = array('order_id' => $order_id, 'type' => 'R', 'data' => serialize(CART_SECONDARY_CURRENCY));
            db_query("REPLACE INTO ?:order_data ?e", $_data);
            //
            // Place the order_id to new_orders table for all admin profiles
            //
            $admins = db_get_fields("SELECT user_id FROM ?:users WHERE user_type = 'A'");
            foreach ($admins as $k => $v) {
                db_query("REPLACE INTO ?:new_orders (order_id, user_id) VALUES (?i, ?i)", $order_id, $v);
            }
            fn_set_hook('place_order', $order_id, $action, $__order_status, $cart);
            // If order total is zero, just save the order without any processing procedures
            if (floatval($cart['total']) == 0) {
                $action = 'save';
                $__order_status = 'P';
            }
            list($is_processor_script, ) = fn_check_processor_script($cart['payment_id'], $action, true);
            if (!$is_processor_script && $__order_status == STATUS_INCOMPLETED_ORDER) {
                $__order_status = 'O';
            }
            // Set new order status
            fn_change_order_status($order_id, $__order_status, '', $is_processor_script || $__order_status == STATUS_PARENT_ORDER ? fn_get_notification_rules(array(), true) : fn_get_notification_rules(array()), true);
            $cart['processed_order_id'] = array();
            $cart['processed_order_id'][] = $order_id;
            if (!$parent_order_id && count($cart['companies']) > 1 && PRODUCT_TYPE == 'MULTIVENDOR') {
                fn_companies_place_suborders($order_id, $cart, $auth, $action);
                $child_orders = db_get_fields("SELECT order_id FROM ?:orders WHERE parent_order_id = ?i", $order_id);
                array_unshift($child_orders, $order_id);
                $cart['processed_order_id'] = $child_orders;
            }
            return array($order_id, $action != 'save');
        }
    }
    return array(false, false);
}