Ejemplo n.º 1
0
         $gift_cert_data = $_REQUEST['gift_cert_data'];
         fn_correct_gift_certificate($gift_cert_data);
         if (!isset($gift_cert_data['products'])) {
             $gift_cert_data['products'] = array();
         }
         // Change certfificate status
         fn_change_gift_certificate_status($_REQUEST['gift_cert_id'], $gift_cert_data['status'], $_REQUEST['certificate_status'], fn_get_notification_rules(array(), false));
         //if difference then add line in log
         $debit_info = db_get_row("SELECT debit AS amount, debit_products AS products FROM ?:gift_certificates_log WHERE gift_cert_id = ?i ORDER BY timestamp DESC", $_REQUEST['gift_cert_id']);
         if (empty($debit_info)) {
             $debit_info = db_get_row("SELECT amount, products FROM ?:gift_certificates WHERE gift_cert_id = ?i", $_REQUEST['gift_cert_id']);
         }
         $is_diff = $gift_cert_data['amount'] - $debit_info['amount'] != 0 || md5(serialize($gift_cert_data['products'])) != md5($debit_info['products']) ? 'Y' : 'N';
         if ($is_diff == 'Y') {
             $_info = array('amount' => $gift_cert_data['amount'], 'products' => serialize($gift_cert_data['products']));
             fn_add_gift_certificate_log_record($_REQUEST['gift_cert_id'], $debit_info, $_info);
         }
         //Update certificate data
         $_data = $gift_cert_data;
         $_data['products'] = !empty($_data['products']) ? serialize($_data['products']) : '';
         db_query("UPDATE ?:gift_certificates SET ?u WHERE gift_cert_id = ?i", $_data, $_REQUEST['gift_cert_id']);
         $gift_cert_data['gift_cert_id'] = $_REQUEST['gift_cert_id'];
         fn_gift_certificate_notification($gift_cert_data, fn_get_notification_rules($_REQUEST));
         $suffix = ".update?gift_cert_id={$_REQUEST['gift_cert_id']}";
     }
 }
 if ($mode == 'preview') {
     if (!empty($_REQUEST['gift_cert_data'])) {
         fn_show_postal_card($_REQUEST['gift_cert_data']);
         exit;
     }
Ejemplo n.º 2
0
function fn_update_gift_certificate($gift_cert_data, $gift_cert_id = 0, $params = array())
{
    fn_correct_gift_certificate($gift_cert_data);
    $gift_cert_data['products'] = !empty($gift_cert_data['products']) ? serialize($gift_cert_data['products']) : '';
    if (empty($gift_cert_id)) {
        do {
            $code = fn_generate_gift_certificate_code();
        } while (true == fn_check_gift_certificate_code($code));
        $gift_cert_data = fn_array_merge($gift_cert_data, array('gift_cert_code' => $code, 'timestamp' => TIME));
        if (fn_allowed_for('ULTIMATE') && Registry::get('runtime.company_id')) {
            $gift_cert_data['company_id'] = Registry::get('runtime.company_id');
        }
        $gift_cert_id = db_query("INSERT INTO ?:gift_certificates ?e", $gift_cert_data);
    } else {
        // Change certfificate status
        fn_change_gift_certificate_status($gift_cert_id, $gift_cert_data['status'], '', fn_get_notification_rules(array(), false));
        //if difference then add line in log
        $debit_info = db_get_row("SELECT debit AS amount, debit_products AS products FROM ?:gift_certificates_log WHERE gift_cert_id = ?i ORDER BY timestamp DESC", $gift_cert_id);
        if (empty($debit_info)) {
            $debit_info = db_get_row("SELECT amount, products FROM ?:gift_certificates WHERE gift_cert_id = ?i", $gift_cert_id);
        }
        $is_diff = $gift_cert_data['amount'] - $debit_info['amount'] != 0 || md5($gift_cert_data['products']) != md5($debit_info['products']);
        if ($is_diff == true) {
            $_info = array('amount' => $gift_cert_data['amount'], 'products' => $gift_cert_data['products']);
            fn_add_gift_certificate_log_record($gift_cert_id, $debit_info, $_info);
        }
        //Update certificate data
        $_data = $gift_cert_data;
        db_query("UPDATE ?:gift_certificates SET ?u WHERE gift_cert_id = ?i", $gift_cert_data, $gift_cert_id);
    }
    $gc_data = fn_get_gift_certificate_info($gift_cert_id);
    fn_gift_certificate_notification($gc_data, fn_get_notification_rules($params));
    return $gift_cert_id;
}
Ejemplo n.º 3
0
function fn_gift_certificates_place_order(&$order_id, &$action, &$order_status, &$cart)
{
    if (!empty($order_id)) {
        $is_parent_order = db_get_field("SELECT is_parent_order FROM ?:orders WHERE order_id = ?i", $order_id);
        if ($is_parent_order == 'Y') {
            return false;
        }
        if (defined('ORDER_MANAGEMENT')) {
            // If the purchased certificate was deleted when editing, then it should be updated in the database
            if (!empty($cart['gift_certificates_previous_state'])) {
                $flip_gcps = array_flip(array_keys($cart['gift_certificates_previous_state']));
                $flip_gc = array_flip(array_keys(!empty($cart['gift_certificates']) ? $cart['gift_certificates'] : array()));
                $diff = array_diff_key($flip_gcps, $flip_gc);
                if (!empty($diff)) {
                    foreach ($diff as $gift_cert_cart_id => $v) {
                        db_query("UPDATE ?:gift_certificates SET order_ids = ?p WHERE gift_cert_id = ?i", fn_remove_from_set('order_ids', $order_id), $cart['gift_certificates_previous_state'][$gift_cert_cart_id]['gift_cert_id']);
                    }
                    db_query("DELETE FROM ?:order_data WHERE order_id = ?i AND type = ?s", $order_id, ORDER_DATA_PURCHASED_GIFT_CERTIFICATES);
                }
            }
        }
        if (isset($cart['reset_use_gift_certificates'])) {
            foreach ($cart['reset_use_gift_certificates'] as $v) {
                db_query("UPDATE ?:gift_certificates SET order_ids = ?p WHERE gift_cert_id = ?i", fn_remove_from_set('order_ids', $order_id), $v);
            }
            unset($cart['reset_use_gift_certificates']);
        }
        if (isset($cart['gift_certificates'])) {
            static $certificates = array();
            foreach ($cart['gift_certificates'] as $k => $v) {
                if (defined('ORDER_MANAGEMENT') && !empty($v['gift_cert_code'])) {
                    $code = $v['gift_cert_code'];
                } else {
                    do {
                        $code = fn_generate_gift_certificate_code();
                    } while (true == fn_check_gift_certificate_code($code));
                }
                if (empty($cart['parent_order_id']) || empty($certificates[$cart['parent_order_id']])) {
                    $_data = $v;
                    $_data['gift_cert_code'] = $code;
                    $_data['timestamp'] = TIME;
                    $_data['status'] = 'P';
                    $_data['products'] = !empty($v['products']) ? serialize($v['products']) : '';
                    $company_id = Registry::get('runtime.company_id');
                    if (!empty($company_id)) {
                        $_data['company_id'] = $company_id;
                    }
                    $gift_cert_id = db_query('REPLACE INTO ?:gift_certificates ?e', $_data);
                    $_data['gift_cert_id'] = $gift_cert_id;
                    if (!empty($cart['parent_order_id'])) {
                        $certificates[$cart['parent_order_id']] = $_data;
                    }
                } else {
                    $_data = $certificates[$cart['parent_order_id']];
                    $gift_cert_id = $_data['gift_cert_id'];
                }
                $cart['gift_certificates'][$k] = fn_array_merge($v, array('gift_cert_id' => $gift_cert_id, 'gift_cert_code' => $_data['gift_cert_code']));
                db_query("UPDATE ?:gift_certificates SET order_ids = ?p WHERE gift_cert_id = ?i", fn_add_to_set('order_ids', $order_id), $gift_cert_id);
                if (defined('ORDER_MANAGEMENT')) {
                    //If the certificate was not removed from the order, it is necessary to check  whether the products and amount have been changed and modify the log.
                    $debit_info = db_get_row("SELECT debit AS amount, debit_products AS products FROM ?:gift_certificates_log WHERE gift_cert_id = ?i ORDER BY timestamp DESC", $gift_cert_id);
                    if (empty($debit_info)) {
                        $debit_info = db_get_row("SELECT amount, products FROM ?:gift_certificates WHERE gift_cert_id = ?i", $gift_cert_id);
                    }
                    if ($_data['amount'] - $debit_info['amount'] != 0 || md5($_data['products']) != md5($debit_info['products'])) {
                        $_info = array('amount' => $_data['amount'], 'products' => $_data['products']);
                        fn_add_gift_certificate_log_record($gift_cert_id, $debit_info, $_info);
                    }
                }
            }
            $order_data = array('order_id' => $order_id, 'type' => ORDER_DATA_PURCHASED_GIFT_CERTIFICATES, 'data' => serialize($cart['gift_certificates']));
            db_query("REPLACE INTO ?:order_data ?e", $order_data);
        }
        //--> FIXME: optimize this code:
        if (!empty($cart['use_gift_certificates_previous_state'])) {
            $flip_ugcps = array_flip(array_keys($cart['use_gift_certificates_previous_state']));
            $flip_ugc = array_flip(array_keys(!empty($cart['use_gift_certificates']) ? $cart['use_gift_certificates'] : array()));
            $diff = array_diff_key($flip_ugcps, $flip_ugc);
            if (!empty($diff)) {
                foreach ($diff as $gift_cert_code => $v) {
                    $gc_data = $cart['use_gift_certificates_previous_state'][$gift_cert_code]['previous_state'];
                    $log_records = db_get_array("SELECT log_id, amount, debit, products, debit_products FROM ?:gift_certificates_log WHERE log_id >= ?i AND gift_cert_id = ?i ORDER BY timestamp ASC", $gc_data['log_id'], $gc_data['gift_cert_id']);
                    foreach ($log_records as $record) {
                        if (!empty($gc_data['products'])) {
                            if ($record['log_id'] != $gc_data['log_id']) {
                                $record['products'] = unserialize($record['products']);
                                foreach ($gc_data['products'] as $po_product_id => $po_quantity) {
                                    if (!isset($record['products'][$po_product_id])) {
                                        $record['products'][$po_product_id] = $po_quantity;
                                    } else {
                                        $record['products'][$po_product_id] += $po_quantity;
                                    }
                                    if (empty($record['products'][$po_product_id])) {
                                        unset($record['products'][$po_product_id]);
                                    }
                                }
                                $record['products'] = serialize($record['products']);
                            }
                            $record['debit_products'] = unserialize($record['debit_products']);
                            foreach ($gc_data['products'] as $po_product_id => $po_quantity) {
                                if (!isset($record['debit_products'][$po_product_id])) {
                                    $record['debit_products'][$po_product_id] = $po_quantity;
                                } else {
                                    $record['debit_products'][$po_product_id] += $po_quantity;
                                }
                                if (empty($record['debit_products'][$po_product_id])) {
                                    unset($record['debit_products'][$po_product_id]);
                                }
                            }
                            $record['debit_products'] = serialize($record['debit_products']);
                        }
                        if ($record['log_id'] != $gc_data['log_id']) {
                            $record['amount'] += $gc_data['cost'];
                        }
                        $record['debit'] += $gc_data['cost'];
                        db_query("UPDATE ?:gift_certificates_log SET ?u WHERE log_id = ?i", $record, $record['log_id']);
                        if (floatval($record['debit']) > 0 || unserialize($record['debit_products']) != array() && db_get_field("SELECT status FROM ?:gift_certificates WHERE gift_cert_id = ?", $gc_data['gift_cert_id']) == 'U') {
                            fn_change_gift_certificate_status($gc_data['gift_cert_id'], 'A');
                        }
                    }
                }
            }
        }
        if (isset($cart['use_gift_certificates'])) {
            $debit_products = array();
            $use_gift_certificates = array();
            if (!empty($cart['deleted_exclude_products'][GIFT_CERTIFICATE_EXCLUDE_PRODUCTS])) {
                foreach ($cart['deleted_exclude_products'][GIFT_CERTIFICATE_EXCLUDE_PRODUCTS] as $cart_id => $v) {
                    foreach ($v['in_use_certificate'] as $gift_cert_code => $amount) {
                        $debit_products[$gift_cert_code]['products'][$v['product_id']] = $amount;
                    }
                }
            }
            $use_gift_certificate_products = array();
            if (!empty($cart['products'])) {
                foreach ($cart['products'] as $product) {
                    if (!empty($product['extra']['exclude_from_calculate']) && $product['extra']['exclude_from_calculate'] == GIFT_CERTIFICATE_EXCLUDE_PRODUCTS) {
                        foreach ($product['extra']['in_use_certificate'] as $gift_cert_code => $quantity) {
                            $use_gift_certificate_products[$gift_cert_code][$product['product_id']] = $quantity;
                        }
                    }
                }
            }
            foreach ($cart['use_gift_certificates'] as $k => $v) {
                if (!empty($v['log_id'])) {
                    $product_odds = array();
                    $amount_odds = $v['previous_state']['cost'] - $v['cost'];
                    $current_state_products = !empty($use_gift_certificate_products[$k]) ? $use_gift_certificate_products[$k] : array();
                    if (sizeof($v['previous_state']['products']) != sizeof($current_state_products) || serialize($v['previous_state']['products']) != serialize($current_state_products)) {
                        if (!empty($v['previous_state']['products'])) {
                            foreach ($v['previous_state']['products'] as $product_id => $quantity) {
                                if (!isset($current_state_products[$product_id])) {
                                    $product_odds[$product_id] = $quantity;
                                } else {
                                    $product_odds[$product_id] = $quantity - $current_state_products[$product_id];
                                }
                                if (empty($product_odds[$product_id])) {
                                    unset($product_odds[$product_id]);
                                }
                            }
                        } elseif (!empty($current_state_products)) {
                            foreach ($current_state_products as $product_id => $quantity) {
                                $product_odds[$product_id] = -$quantity;
                            }
                        }
                    }
                    if ($amount_odds != 0 || !empty($product_odds)) {
                        $log_records = db_get_array("SELECT log_id, amount, debit, products, debit_products FROM ?:gift_certificates_log WHERE log_id >= ?i AND gift_cert_id = ?i ORDER BY timestamp ASC", $v['log_id'], $v['gift_cert_id']);
                        foreach ($log_records as $record) {
                            if (!empty($product_odds)) {
                                if ($record['log_id'] != $v['log_id']) {
                                    $record['products'] = unserialize($record['products']);
                                    foreach ($product_odds as $po_product_id => $po_quantity) {
                                        if (!isset($record['products'][$po_product_id])) {
                                            $record['products'][$po_product_id] = $po_quantity;
                                        } else {
                                            $record['products'][$po_product_id] += $po_quantity;
                                        }
                                        if (empty($record['products'][$po_product_id])) {
                                            unset($record['products'][$po_product_id]);
                                        }
                                    }
                                    $record['products'] = serialize($record['products']);
                                }
                                $record['debit_products'] = unserialize($record['debit_products']);
                                foreach ($product_odds as $po_product_id => $po_quantity) {
                                    if (!isset($record['debit_products'][$po_product_id])) {
                                        $record['debit_products'][$po_product_id] = $po_quantity;
                                    } else {
                                        $record['debit_products'][$po_product_id] += $po_quantity;
                                    }
                                    if (empty($record['debit_products'][$po_product_id])) {
                                        unset($record['debit_products'][$po_product_id]);
                                    }
                                }
                                $record['debit_products'] = serialize($record['debit_products']);
                            } else {
                                if ($record['log_id'] != $v['log_id']) {
                                    $record['amount'] += $amount_odds;
                                }
                                $record['debit'] += $amount_odds;
                            }
                            db_query("UPDATE ?:gift_certificates_log SET ?u WHERE log_id = ?i", $record, $record['log_id']);
                            $use_gift_certificates[$k] = array('gift_cert_id' => $v['gift_cert_id'], 'amount' => $v['previous_state']['amount'], 'cost' => $v['cost'], 'log_id' => $v['log_id']);
                            if (floatval($record['debit']) <= 0 && unserialize($record['debit_products']) == array()) {
                                fn_change_gift_certificate_status($v['gift_cert_id'], 'U');
                            } elseif (floatval($record['debit']) > 0 || unserialize($record['debit_products']) != array() && db_get_field("SELECT status FROM ?:gift_certificates WHERE gift_cert_id = ?i", $v['gift_cert_id']) == 'U') {
                                fn_change_gift_certificate_status($v['gift_cert_id'], 'A');
                            }
                        }
                    }
                    //<-- FIXME: optimize this code
                } else {
                    $before_info = array('amount' => $v['amount'], 'products' => serialize(!empty($v['products']) ? $v['products'] : array()));
                    $after_info = array('amount' => fn_format_price($v['amount'] - $v['cost']), 'products' => serialize(!empty($debit_products[$k]['products']) ? $debit_products[$k]['products'] : array()));
                    $log_id = fn_add_gift_certificate_log_record($v['gift_cert_id'], $before_info, $after_info, $order_id);
                    $use_gift_certificates[$k] = array('gift_cert_id' => $v['gift_cert_id'], 'amount' => $v['amount'], 'cost' => $v['cost'], 'log_id' => $log_id);
                    if (floatval($v['amount'] - $v['cost']) <= 0 && !isset($debit_products[$k]['products'])) {
                        fn_change_gift_certificate_status($v['gift_cert_id'], 'U');
                    }
                }
                db_query("UPDATE ?:gift_certificates SET order_ids = ?p  WHERE gift_cert_id = ?i", fn_add_to_set('order_ids', $order_id), $v['gift_cert_id']);
            }
            $order_data = array('order_id' => $order_id, 'type' => 'U', 'data' => serialize($use_gift_certificates));
            db_query("REPLACE INTO ?:order_data ?e", $order_data);
        }
    }
}