Example #1
0
function fn_export($pattern, $export_fields, $options)
{
    if (empty($pattern) || empty($export_fields)) {
        return false;
    }
    // Languages
    if (!empty($options['lang_code'])) {
        $multi_lang = $options['lang_code'];
        $count_langs = count($multi_lang);
    } else {
        $multi_lang = array(DEFAULT_LANGUAGE);
        $count_langs = 1;
        $options['lang_code'] = $multi_lang;
    }
    $can_continue = true;
    if (!empty($pattern['export_pre_moderation'])) {
        $data_export_pre_moderation = array('pattern' => &$pattern, 'export_fields' => &$export_fields, 'options' => &$options, 'can_continue' => &$can_continue);
        fn_exim_processing('export', $pattern['export_pre_moderation'], $options, $data_export_pre_moderation);
    }
    if (!$can_continue) {
        return false;
    }
    if (!empty($pattern['pre_processing'])) {
        fn_exim_processing('export', $pattern['pre_processing'], $options);
    }
    if (isset($options['fields_names'])) {
        if ($options['fields_names']) {
            $fields_names = $export_fields;
            $export_fields = array_keys($export_fields);
        }
    }
    $primary_key = array();
    $_primary_key = $pattern['key'];
    foreach ($_primary_key as $key) {
        $primary_key[$key] = $key;
    }
    array_walk($primary_key, 'fn_attach_value_helper', $pattern['table'] . '.');
    $table_fields = $primary_key;
    $processes = array();
    // Build list of fields that should be retrieved from the database
    fn_export_build_retrieved_fields($processes, $table_fields, $pattern, $export_fields);
    if (empty($pattern['export_fields']['multilang'])) {
        $multi_lang = array(DEFAULT_LANGUAGE);
        $count_langs = 1;
        $options['lang_code'] = $multi_lang;
    }
    // Build the list of joins
    $joins = fn_export_build_joins($pattern, $options, $primary_key, $multi_lang);
    // Add retrieve conditions
    $conditions = fn_export_build_conditions($pattern, $options);
    if (!empty($pattern['pre_export_process'])) {
        $pre_export_process_data = array('pattern' => &$pattern, 'export_fields' => &$export_fields, 'options' => &$options, 'conditions' => &$conditions, 'joins' => &$joins, 'table_fields' => &$table_fields, 'processes' => &$processes);
        fn_exim_processing('export', $pattern['pre_export_process'], $options, $pre_export_process_data);
    }
    $total = db_get_field("SELECT COUNT(*) FROM ?:" . $pattern['table'] . " as " . $pattern['table'] . ' ' . implode(' ', $joins) . (!empty($conditions) ? ' WHERE ' . implode(' AND ', $conditions) : ''));
    fn_set_progress('parts', $total);
    fn_set_progress('step_scale', 1);
    $sorting = '';
    if (!empty($pattern['order_by'])) {
        $sorting = ' ORDER BY ' . $pattern['order_by'];
    }
    // Build main query
    $query = "SELECT " . implode(', ', $table_fields) . " FROM ?:" . $pattern['table'] . " as " . $pattern['table'] . ' ' . implode(' ', $joins) . (!empty($conditions) ? ' WHERE ' . implode(' AND ', $conditions) : '') . $sorting;
    $step = fn_floor_to_step(DB_LIMIT_SELECT_ROW, $count_langs);
    // define number of rows to get from database
    $iterator = 0;
    // start retrieving from
    $progress = 0;
    $data_exported = false;
    $main_lang = reset($multi_lang);
    $manual_multilang = true;
    $field_lang = '';
    foreach ($pattern['export_fields']['multilang'] as $key => $value) {
        if (array_search('languages', $value, true)) {
            if (!isset($value['linked']) || $value['linked'] === true) {
                $manual_multilang = false;
            }
            $field_lang = $key;
            break;
        }
    }
    if (empty($field_lang) || !in_array($field_lang, $export_fields)) {
        $multi_lang = array($main_lang);
        $count_langs = 1;
    }
    while ($data = db_get_array($query . " LIMIT {$iterator}, {$step}")) {
        $data_exported = true;
        if ($manual_multilang) {
            $data_lang = $data;
            $data = array();
            foreach ($data_lang as $data_key => $data_value) {
                $data[] = array_combine($multi_lang, array_fill(0, $count_langs, $data_value));
            }
        } else {
            $data_lang = array_chunk($data, $count_langs);
            $data = array();
            foreach ($data_lang as $data_key => $data_value) {
                // Sort
                foreach ($multi_lang as $lang_code) {
                    foreach ($data_value as $v) {
                        if (array_search($lang_code, $v, true)) {
                            $data[$data_key][$lang_code] = $v;
                        }
                    }
                }
            }
        }
        $result = array();
        foreach ($data as $k => $v) {
            $progress += $count_langs;
            fn_set_progress('echo', __('exporting_data') . ':&nbsp;<b>' . $progress . '</b>');
            fn_export_fill_fields($result[$k], $v, $processes, $pattern, $options);
        }
        $_result = array();
        foreach ($result as $k => $v) {
            foreach ($multi_lang as $lang_code) {
                $_data = array();
                foreach ($export_fields as $field) {
                    if (isset($fields_names[$field])) {
                        $_data[$fields_names[$field]] = $v[$lang_code][$field];
                    } else {
                        $_data[$field] = isset($v[$lang_code][$field]) ? $v[$lang_code][$field] : '';
                    }
                }
                $_result[] = $_data;
            }
        }
        // Put data
        $enclosure = isset($pattern['enclosure']) ? $pattern['enclosure'] : '"';
        fn_echo(' .');
        if (isset($pattern['func_save_content_to_file']) && is_callable($pattern['func_save_content_to_file'])) {
            call_user_func($pattern['func_save_content_to_file'], $_result, $options, $enclosure);
        } else {
            fn_put_csv($_result, $options, $enclosure);
        }
        $iterator += $step;
    }
    if (!empty($pattern['post_processing'])) {
        fn_set_progress('echo', __('processing'), false);
        if ($data_exported && file_exists(fn_get_files_dir_path() . $options['filename'])) {
            $data_exported = fn_exim_processing('export', $pattern['post_processing'], $options);
        }
    }
    return $data_exported;
}
Example #2
0
function fn_check_amount_in_stock($product_id, $amount, $product_options, $cart_id, $is_edp, $original_amount, &$cart, $update_id = 0)
{
    fn_set_hook('check_amount_in_stock', $product_id, $amount, $product_options, $cart_id, $is_edp, $original_amount, $cart);
    // If the product is EDP don't track the inventory
    if ($is_edp == 'Y') {
        return 1;
    }
    $product = db_get_row("SELECT ?:products.tracking, ?:products.amount, ?:products.min_qty, ?:products.max_qty, ?:products.qty_step, ?:products.list_qty_count, ?:product_descriptions.product FROM ?:products LEFT JOIN ?:product_descriptions ON ?:product_descriptions.product_id = ?:products.product_id AND lang_code = ?s WHERE ?:products.product_id = ?i", CART_LANGUAGE, $product_id);
    if (isset($product['tracking']) && Registry::get('settings.General.inventory_tracking') == 'Y' && $product['tracking'] != ProductTracking::DO_NOT_TRACK) {
        // Track amount for ordinary product
        if ($product['tracking'] == ProductTracking::TRACK_WITHOUT_OPTIONS) {
            $current_amount = $product['amount'];
            // Track amount for product with options
        } elseif ($product['tracking'] == ProductTracking::TRACK_WITH_OPTIONS) {
            $selectable_cart_id = fn_generate_cart_id($product_id, array('product_options' => $product_options), true);
            $current_amount = db_get_field("SELECT amount FROM ?:product_options_inventory WHERE combination_hash = ?i", $selectable_cart_id);
            $current_amount = intval($current_amount);
        }
        if (!empty($cart['products']) && is_array($cart['products'])) {
            $product_not_in_cart = true;
            foreach ($cart['products'] as $k => $v) {
                // Check if the product with the same selectable options already exists ( for tracking = O)
                if ($k != $cart_id) {
                    if (isset($product['tracking']) && ($product['tracking'] == ProductTracking::TRACK_WITHOUT_OPTIONS && $v['product_id'] == $product_id) || $product['tracking'] == ProductTracking::TRACK_WITH_OPTIONS && @$v['selectable_cart_id'] == $selectable_cart_id) {
                        $current_amount -= $v['amount'];
                    }
                } else {
                    $product_not_in_cart = false;
                }
            }
            if ($product['tracking'] == ProductTracking::TRACK_WITHOUT_OPTIONS && !empty($update_id) && $product_not_in_cart && !empty($cart['products'][$update_id])) {
                $current_amount += $cart['products'][$update_id]['amount'];
            }
            if ($product['tracking'] == ProductTracking::TRACK_WITH_OPTIONS) {
                // Store cart_id for selectable options in cart variable, so if the same product is added to
                // the cart with the same selectable options, but different text options,
                // the total amount will be tracked anyway as it is the one product
                if (!empty($selectable_cart_id) && isset($cart['products'][$cart_id])) {
                    $cart['products'][$cart_id]['selectable_cart_id'] = $selectable_cart_id;
                }
            }
        }
    }
    $min_qty = 1;
    if (!empty($product['min_qty']) && $product['min_qty'] > $min_qty) {
        $min_qty = fn_ceil_to_step($product['min_qty'], $product['qty_step']);
    }
    if (!empty($product['qty_step']) && $product['qty_step'] > $min_qty) {
        $min_qty = $product['qty_step'];
    }
    $cart_amount_changed = false;
    // Step parity check
    if (!empty($product['qty_step']) && $amount % $product['qty_step']) {
        $amount = fn_ceil_to_step($amount, $product['qty_step']);
        $cart_amount_changed = true;
    }
    if (isset($current_amount) && $current_amount >= 0 && $current_amount - $amount < 0 && Registry::get('settings.General.allow_negative_amount') != 'Y') {
        // For order edit: add original amount to existent amount
        $current_amount += $original_amount;
        if ($current_amount > 0 && $current_amount - $amount < 0 && Registry::get('settings.General.allow_negative_amount') != 'Y') {
            if (!defined('ORDER_MANAGEMENT')) {
                fn_set_notification('W', __('important'), __('text_cart_amount_corrected', array('[product]' => $product['product'])));
                $amount = fn_ceil_to_step($current_amount, $product['qty_step']);
            } else {
                if ($product['tracking'] == ProductTracking::TRACK_WITH_OPTIONS) {
                    fn_set_notification('E', __('warning'), __('text_combination_out_of_stock'));
                } else {
                    fn_set_notification('W', __('warning'), __('text_cart_not_enough_inventory'));
                }
            }
        } elseif ($current_amount - $amount < 0 && Registry::get('settings.General.allow_negative_amount') != 'Y') {
            if ($product['tracking'] == ProductTracking::TRACK_WITH_OPTIONS) {
                fn_set_notification('E', __('notice'), __('text_combination_out_of_stock'));
            } else {
                fn_set_notification('E', __('notice'), __('text_cart_zero_inventory', array('[product]' => $product['product'])));
            }
            return false;
        } elseif ($current_amount <= 0 && $amount <= 0 && Registry::get('settings.General.allow_negative_amount') != 'Y') {
            fn_set_notification('E', __('notice'), __('text_cart_zero_inventory_and_removed', array('[product]' => $product['product'])));
            return false;
        }
    }
    if ($amount < $min_qty || isset($current_amount) && $amount > $current_amount && Registry::get('settings.General.allow_negative_amount') != 'Y' && Registry::get('settings.General.inventory_tracking') == 'Y' && isset($product_not_in_cart) && !$product_not_in_cart) {
        if (($current_amount < $min_qty || $current_amount == 0) && Registry::get('settings.General.allow_negative_amount') != 'Y' && Registry::get('settings.General.inventory_tracking') == 'Y') {
            if ($product['tracking'] == ProductTracking::TRACK_WITH_OPTIONS) {
                fn_set_notification('E', __('warning'), __('text_combination_out_of_stock'));
            } else {
                fn_set_notification('W', __('warning'), __('text_cart_not_enough_inventory'));
            }
            if (!defined('ORDER_MANAGEMENT')) {
                $amount = false;
            }
        } elseif ($amount > $current_amount && Registry::get('settings.General.allow_negative_amount') != 'Y' && Registry::get('settings.General.inventory_tracking') == 'Y') {
            if ($product['tracking'] == ProductTracking::TRACK_WITH_OPTIONS) {
                fn_set_notification('E', __('warning'), __('text_combination_out_of_stock'));
            } else {
                fn_set_notification('W', __('warning'), __('text_cart_not_enough_inventory'));
            }
            if (!defined('ORDER_MANAGEMENT')) {
                $amount = fn_floor_to_step($current_amount, $product['qty_step']);
            }
        } elseif ($amount < $min_qty) {
            fn_set_notification('W', __('notice'), __('text_cart_min_qty', array('[product]' => $product['product'], '[quantity]' => $min_qty)));
            $cart_amount_changed = false;
            if (!defined('ORDER_MANAGEMENT')) {
                $amount = $min_qty;
            }
        }
    }
    $max_qty = fn_floor_to_step($product['max_qty'], $product['qty_step']);
    if (!empty($max_qty) && $amount > $max_qty) {
        fn_set_notification('W', __('notice'), __('text_cart_max_qty', array('[product]' => $product['product'], '[quantity]' => $max_qty)));
        $cart_amount_changed = false;
        if (!defined('ORDER_MANAGEMENT')) {
            $amount = $max_qty;
        }
    }
    if ($cart_amount_changed) {
        fn_set_notification('W', __('important'), __('text_cart_amount_changed', array('[product]' => $product['product'])));
    }
    fn_set_hook('post_check_amount_in_stock', $product_id, $amount, $product_options, $cart_id, $is_edp, $original_amount, $cart);
    return empty($amount) ? false : $amount;
}
/**
 * Forms a drop-down list of possible product quantity values with the given quantity step
 *
 * @param array $product Product data
 * @param char $allow_negative_amount Flag: allow or disallow negative product quantity(Y - allow, N - disallow)
 * @param char $inventory_tracking Flag: track product qiantity or not (Y - track, N - do not track)
 * @return array qty_content List of available quantity values with the given step
 */
function fn_get_product_qty_content($product, $allow_negative_amount, $inventory_tracking)
{
    $qty_content = array();
    if (!empty($product['qty_step'])) {
        $default_list_qty_count = 100;
        if (empty($product['min_qty'])) {
            $min_qty = $product['qty_step'];
        } else {
            $min_qty = fn_ceil_to_step($product['min_qty'], $product['qty_step']);
        }
        if (!empty($product['list_qty_count'])) {
            $max_list_qty = $product['list_qty_count'] * $product['qty_step'] + $min_qty - $product['qty_step'];
        } else {
            $max_list_qty = $default_list_qty_count * $product['qty_step'] + $min_qty - $product['qty_step'];
        }
        // max amount
        if ($product['tracking'] != 'D' && $allow_negative_amount != 'Y' && $inventory_tracking == 'Y') {
            if (isset($product['in_stock'])) {
                $max_qty = fn_floor_to_step($product['in_stock'], $product['qty_step']);
            } elseif (isset($product['inventory_amount'])) {
                $max_qty = fn_floor_to_step($product['inventory_amount'], $product['qty_step']);
            } elseif ($product['amount'] < $product['qty_step']) {
                $max_qty = $product['qty_step'];
            } else {
                $max_qty = fn_floor_to_step($product['amount'], $product['qty_step']);
            }
            if (!empty($product['list_qty_count'])) {
                $max_qty = min($max_qty, $max_list_qty);
            }
        } else {
            $max_qty = $max_list_qty;
        }
        if (!empty($product['max_qty'])) {
            $max_qty = min($max_qty, fn_floor_to_step($product['max_qty'], $product['qty_step']));
        }
        for ($qty = $min_qty; $qty <= $max_qty; $qty += $product['qty_step']) {
            $qty_content[] = $qty;
        }
    }
    return $qty_content;
}
Example #4
0
/**
 * Forms a drop-down list of possible product quantity values with the given quantity step
 *
 * @param array $product Product data
 * @param char $allow_negative_amount Flag: allow or disallow negative product quantity(Y - allow, N - disallow)
 * @param char $inventory_tracking Flag: track product qiantity or not (Y - track, N - do not track)
 * @return array qty_content List of available quantity values with the given step
 */
function fn_get_product_qty_content($product, $allow_negative_amount, $inventory_tracking)
{
    if (empty($product['qty_step'])) {
        return array();
    }
    $qty_content = array();
    $default_list_qty_count = 100;
    $max_allowed_qty_steps = 50;
    if (empty($product['min_qty'])) {
        $min_qty = $product['qty_step'];
    } else {
        $min_qty = fn_ceil_to_step($product['min_qty'], $product['qty_step']);
    }
    if (!empty($product['list_qty_count'])) {
        $max_list_qty = $product['list_qty_count'] * $product['qty_step'] + $min_qty - $product['qty_step'];
    } else {
        $max_list_qty = $default_list_qty_count * $product['qty_step'] + $min_qty - $product['qty_step'];
    }
    if ($product['tracking'] != ProductTracking::DO_NOT_TRACK && $allow_negative_amount != 'Y' && $inventory_tracking == 'Y') {
        if (isset($product['in_stock'])) {
            $max_qty = fn_floor_to_step($product['in_stock'], $product['qty_step']);
        } elseif (isset($product['inventory_amount'])) {
            $max_qty = fn_floor_to_step($product['inventory_amount'], $product['qty_step']);
        } elseif ($product['amount'] < $product['qty_step']) {
            $max_qty = $product['qty_step'];
        } else {
            $max_qty = fn_floor_to_step($product['amount'], $product['qty_step']);
        }
        if (!empty($product['list_qty_count'])) {
            $max_qty = min($max_qty, $max_list_qty);
        }
    } else {
        $max_qty = $max_list_qty;
    }
    if (!empty($product['max_qty'])) {
        $max_qty = min($max_qty, fn_floor_to_step($product['max_qty'], $product['qty_step']));
    }
    $total_steps_count = 1 + ($max_qty - $min_qty) / $product['qty_step'];
    if ($total_steps_count > $max_allowed_qty_steps) {
        return array();
    }
    for ($qty = $min_qty; $qty <= $max_qty; $qty += $product['qty_step']) {
        $qty_content[] = $qty;
    }
    return $qty_content;
}