/**
  * Add product if is queried and recalcule cart informations
  * @param array $product_list
  * @param string $custom_order_information
  * @param array $current_cart
  * @param boolean $from_admin
  * @return array
  */
 function calcul_cart_information($product_list, $custom_order_information = '', $current_cart = array(), $from_admin = false)
 {
     // Price piloting option
     $price_piloting = get_option('wpshop_shop_price_piloting');
     // Init vars
     $cart_infos = !empty($current_cart) ? $current_cart : (!empty($_SESSION) && !empty($_SESSION['cart']) && !$from_admin ? $_SESSION['cart'] : array());
     $cart_items = !empty($current_cart) && !empty($current_cart['order_items']) ? $current_cart['order_items'] : array();
     $cart_items = !empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items']) && !$from_admin ? $_SESSION['cart']['order_items'] : $cart_items;
     $order_total_ht = $order_total_ttc = $total_vat = 0;
     $order_tva = array();
     $total_weight = $nb_of_items = $order_shipping_cost_by_article = 0;
     $order_discount_rate = $order_discount_amount = $order_items_discount_amount = $order_total_discount_amount = 0;
     // If Product list is not empty, add products to order
     if (!empty($product_list)) {
         foreach ($product_list as $product_id => $d) {
             $product_key = $product_id;
             if (isset($d['product_qty'])) {
                 // Formate datas
                 $product_id = $head_product_id = $d['product_id'];
                 $product_qty = $d['product_qty'];
                 $product_variation = !empty($d['product_variation']) ? $d['product_variation'] : null;
                 // If product is a single variation product
                 if (!empty($product_variation) && count($product_variation) == 1) {
                     $product_id = $product_variation[0];
                 }
                 // Construct final product
                 $product = wpshop_products::get_product_data($d['product_id'], true, '"publish", "free_product"');
                 $the_product = array_merge(array('product_id' => $d['product_id'], 'product_qty' => $product_qty), $product);
                 //	Add variation to product into cart for storage
                 if (!empty($product_variation)) {
                     $the_product = wpshop_products::get_variation_price_behaviour($the_product, $product_variation, $head_product_id, array('type' => $d['product_variation_type']));
                 }
                 // Free Variations Checking
                 if (!empty($d['free_variation'])) {
                     $the_product['item_meta']['free_variation'] = $d['free_variation'];
                     $head_product_id = $the_product['product_id'];
                 }
                 // If product is a variation, we check parent product general
                 if (get_post_type($the_product['product_id']) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
                     $parent_def = wpshop_products::get_parent_variation($the_product['product_id']);
                     if (!empty($parent_def) && !empty($parent_def['parent_post'])) {
                         $variation_def = get_post_meta($parent_def['parent_post']->ID, '_wpshop_variation_defining', true);
                         $parent_meta = $parent_def['parent_post_meta'];
                         if (!empty($variation_def) && !empty($variation_def['options']) && !empty($variation_def['options']['priority']) && in_array('combined', $variation_def['options']['priority']) && !empty($variation_def['options']['price_behaviour']) && in_array('addition', $variation_def['options']['price_behaviour']) && !empty($variation_def['attributes']) && count($variation_def['attributes']) > 1) {
                             $the_product['product_price'] += number_format(str_replace(',', '.', $parent_meta['product_price']), 2, '.', '');
                             $the_product['price_ht'] += number_format(str_replace(',', '.', $parent_meta['price_ht']), 2, '.', '');
                             $the_product['tva'] += number_format(str_replace(',', '.', $parent_meta['tva']), 2, '.', '');
                         }
                     }
                 }
                 // Delete product if its qty is equals to zero, else add this product to order
                 if (empty($d['product_qty'])) {
                     unset($cart_items[$product_key]);
                     unset($cart_infos['order_items'][$product_key]);
                 } else {
                     $wps_orders = new wps_orders_ctr();
                     $cart_items[$product_key] = $wps_orders->add_product_to_order($the_product);
                 }
             }
         }
     }
     // Add automaticaly Add-to-cart Products
     $cart_items = $this->add_automaticaly_product_to_cart($cart_items);
     // Calcul Cart Informations
     if (!empty($cart_items) && is_array($cart_items)) {
         foreach ($cart_items as $item_id => $item) {
             $order_total_ht += $item['item_total_ht'];
             $order_total_ttc += $item['item_total_ttc'];
             // VAT
             if (!empty($order_tva[$item['item_tva_rate']])) {
                 $order_tva[$item['item_tva_rate']] += $item['item_tva_total_amount'];
             } else {
                 $order_tva[$item['item_tva_rate']] = $item['item_tva_total_amount'];
             }
         }
     } else {
         return array();
     }
     // Apply informations to cart
     $cart_infos['order_items'] = $cart_items;
     $cart_infos['order_total_ht'] = $order_total_ht;
     $cart_infos['order_total_ttc'] = $order_total_ttc;
     // Calcul Shipping cost
     if (!$from_admin) {
         $wps_shipping = new wps_shipping();
         $total_cart_ht_or_ttc_regarding_config = !empty($price_piloting) && $price_piloting == 'HT' ? $cart_infos['order_total_ht'] : $cart_infos['order_total_ttc'];
         $cart_weight = $wps_shipping->calcul_cart_weight($cart_infos['order_items']);
         $total_shipping_cost_for_products = $wps_shipping->calcul_cart_items_shipping_cost($cart_infos['order_items']);
         $cart_infos['order_shipping_cost'] = $wps_shipping->get_shipping_cost(count($cart_infos['order_items']), $total_cart_ht_or_ttc_regarding_config, $total_shipping_cost_for_products, $cart_weight);
     }
     // If Price piloting is ET, calcul VAT on Shipping cost
     if (!empty($price_piloting) && $price_piloting == 'HT') {
         $shipping_cost_vat = !empty($cart_infos['order_shipping_cost']) ? WPSHOP_VAT_ON_SHIPPING_COST / 100 * number_format($cart_infos['order_shipping_cost'], 2, '.', '') : 0;
         $order_tva['VAT_shipping_cost'] = $shipping_cost_vat;
     }
     // Calcul VAT Total
     if (!empty($order_tva)) {
         foreach ($order_tva as $vat_rate => $vat_value) {
             $total_vat += $vat_value;
         }
     }
     // Recap totals
     $cart_infos['order_total_ttc'] = $cart_infos['order_total_ht'] + (!empty($cart_infos) && !empty($cart_infos['order_shipping_cost']) ? $cart_infos['order_shipping_cost'] : 0) + $total_vat;
     $cart_infos['order_grand_total_before_discount'] = $cart_infos['order_amount_to_pay_now'] = $cart_infos['order_grand_total'] = $cart_infos['order_total_ttc'];
     // Apply coupons
     if (!empty($_SESSION['cart']) && !$from_admin) {
         if (!empty($_SESSION['cart']['coupon_id'])) {
             $wps_coupon_mdl = new wps_coupon_model();
             $coupon = $wps_coupon_mdl->get_coupon_data($_SESSION['cart']['coupon_id']);
             if (!empty($coupon) && !empty($coupon['wpshop_coupon_code'])) {
                 $wps_coupon = new wps_coupon_ctr();
                 $coupon_checking = $wps_coupon->applyCoupon($coupon['wpshop_coupon_code']);
                 // If Coupon conditions are Ok
                 if (!empty($coupon_checking) && !empty($coupon_checking['status']) && $coupon_checking['status']) {
                     $cart_infos['order_discount_type'] = $coupon['wpshop_coupon_discount_type'];
                     $cart_infos['order_discount_value'] = $coupon['wpshop_coupon_discount_value'];
                 }
             }
         }
     }
     // Checking Discounts
     if (!empty($cart_infos['order_discount_type']) && $cart_infos['order_discount_value']) {
         // Calcul discount on Order
         switch ($cart_infos['order_discount_type']) {
             case 'amount':
                 $cart_infos['order_discount_amount_total_cart'] = number_format(str_replace(',', '.', $cart_infos['order_discount_value']), 2, '.', '');
                 break;
             case 'percent':
                 $cart_infos['order_discount_amount_total_cart'] = number_format($cart_infos['order_grand_total'], 2, '.', '') * (number_format(str_replace(',', '.', $cart_infos['order_discount_value']), 2, '.', '') / 100);
                 break;
         }
         $cart_infos['order_grand_total'] -= number_format($cart_infos['order_discount_amount_total_cart'], 2, '.', '');
         $cart_infos['order_amount_to_pay_now'] = number_format($cart_infos['order_grand_total'], 2, '.', '');
     }
     // Apply Partial Payments
     $wpshop_payment = new wpshop_payment();
     $partial_payment = $wpshop_payment->partial_payment_calcul($cart_infos['order_grand_total']);
     if (!empty($partial_payment['amount_to_pay'])) {
         unset($partial_payment['display']);
         $cart_infos['order_partial_payment'] = number_format(str_replace(',', '.', $partial_payment['amount_to_pay']), 2, '.', '');
         $cart_infos['order_amount_to_pay_now'] = number_format(str_replace(',', '.', $partial_payment['amount_to_pay']), 2, '.', '');
     }
     // Cart Type
     if (isset($_SESSION['cart']['cart_type'])) {
         $cart_infos['cart_type'] = $_SESSION['cart']['cart_type'];
     }
     // Apply Extra actions on cart infos
     $cart_infos = apply_filters('wps_extra_calcul_in_cart', $cart_infos, $_SESSION);
     return $cart_infos;
 }
 public static function process_checkout($paymentMethod = 'paypal', $order_id = 0, $customer_id = 0, $customer_billing_address_id = 0, $customer_shipping_address_id = 0)
 {
     global $wpdb, $wpshop, $wpshop_cart;
     $wps_message = new wps_message_ctr();
     $shipping_address_option = get_option('wpshop_shipping_address_choice');
     if (is_user_logged_in()) {
         $user_id = get_current_user_id();
         if ($customer_id != 0) {
             $user_id = $customer_id;
         }
         // If the order is already created in the db
         if (!empty($order_id) && is_numeric($order_id)) {
             $order = get_post_meta($order_id, '_order_postmeta', true);
             if (!empty($order)) {
                 if ($order['customer_id'] == $user_id) {
                     $order['payment_method'] = $paymentMethod;
                     $_SESSION['order_id'] = wpshop_tools::varSanitizer($order_id);
                     // Store cart in session
                     //wpshop_cart::store_cart_in_session($order);
                     // Add a payment
                     $order['order_payment']['received'][] = array('method' => $paymentMethod, 'waited_amount' => $order['order_amount_to_pay_now'], 'status' => 'waiting_payment', 'author' => get_current_user_id());
                     // On enregistre la commande
                     update_post_meta($order_id, '_order_postmeta', $order);
                     update_post_meta($order_id, '_wpshop_order_customer_id', $user_id);
                 } else {
                     $wpshop->add_error(__('You don\'t own the order', 'wpshop'));
                 }
             } else {
                 $wpshop->add_error(__('The order doesn\'t exist.', 'wpshop'));
             }
         } else {
             $order_data = array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ORDER, 'post_title' => sprintf(__('Order - %s', 'wpshop'), mysql2date('d M Y\\, H:i:s', current_time('mysql', 0), true)), 'post_status' => 'publish', 'post_excerpt' => !empty($_POST['wps-customer-comment']) ? $_POST['wps-customer-comment'] : '', 'post_author' => $user_id, 'comment_status' => 'closed');
             // Cart items
             $order_items = array();
             $order_tva = array();
             //$cart = (array)$wpshop_cart->cart;
             if (!empty($_SESSION['cart']) && !empty($_SESSION['cart']['shipping_method'])) {
                 $_SESSION['cart']['shipping_method'] = __('Standard shipping method', 'wpshop');
             }
             $cart = (array) $_SESSION['cart'];
             $download_codes = array();
             // Nouvelle commande
             $order_id = wp_insert_post($order_data);
             $_SESSION['order_id'] = $order_id;
             // Cr�ation des codes de t�l�chargement si il y a des produits t�l�chargeable dans le panier
             if (!empty($cart['order_items'])) {
                 foreach ($cart['order_items'] as $c) {
                     $product = wpshop_products::get_product_data($c['item_id']);
                     /** Check if it's a variation and check the parent product **/
                     if (get_post_type($c['item_id']) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) {
                         $parent_def = wpshop_products::get_parent_variation($c['item_id']);
                         if (!empty($parent_def) && !empty($parent_def['parent_post_meta']) && !empty($parent_def['parent_post_meta']['is_downloadable_'])) {
                             $product['is_downloadable_'] = $parent_def['parent_post_meta']['is_downloadable_'];
                         }
                     }
                     if (!empty($product['is_downloadable_'])) {
                         $download_codes[$c['item_id']] = array('item_id' => $c['item_id'], 'download_code' => uniqid('', true));
                     }
                 }
             }
             if (!empty($download_codes)) {
                 update_user_meta($user_id, '_order_download_codes_' . $order_id, $download_codes);
             }
             // Informations de commande � stocker
             $currency = wpshop_tools::wpshop_get_currency(true);
             $order = array_merge(array('order_key' => NULL, 'customer_id' => $user_id, 'order_status' => 'awaiting_payment', 'order_date' => current_time('mysql', 0), 'order_shipping_date' => null, 'order_invoice_ref' => '', 'order_currency' => $currency, 'order_payment' => array('customer_choice' => array('method' => $paymentMethod), 'received' => array('0' => array('method' => $paymentMethod, 'waited_amount' => $cart['order_amount_to_pay_now'], 'status' => 'waiting_payment', 'author' => $user_id)), 'shipping_method' => !empty($_SESSION['shipping_method']) ? wpshop_tools::varSanitizer($_SESSION['shipping_method']) : __('Standard shipping method', 'wpshop'))), $cart);
             // Si c'est un devis
             if ($paymentMethod == 'quotation') {
                 $order['order_temporary_key'] = wpshop_orders::get_new_pre_order_reference();
             } else {
                 $order['order_key'] = wpshop_orders::get_new_order_reference();
             }
             //Round final amount
             $order['order_grand_total'] = number_format(round($order['order_grand_total'], 2), 2, '.', '');
             $order['order_total_ttc'] = number_format(round($order['order_total_ttc'], 2), 2, '.', '');
             $order['order_amount_to_pay_now'] = number_format(round($order['order_amount_to_pay_now'], 2), 2, '.', '');
             /** On enregistre la commande	*/
             update_post_meta($order_id, '_order_postmeta', $order);
             update_post_meta($order_id, '_wpshop_order_customer_id', $order['customer_id']);
             update_post_meta($order_id, '_wpshop_order_shipping_date', $order['order_shipping_date']);
             update_post_meta($order_id, '_wpshop_order_status', $order['order_status']);
             do_action('wps_order_extra_save', $order_id);
             //Add an action to extra actions on order save
             $args = array('order_id' => $order_id, 'posted_data' => $_REQUEST);
             wpshop_tools::create_custom_hook('wps_order_extra_save_action', $args);
             /**	Set custmer information for the order	*/
             $shipping_address = !empty($shipping_address_option) && !empty($shipping_address_option['activate']) ? !empty($_SESSION['shipping_address']) ? wpshop_tools::varSanitizer($_SESSION['shipping_address']) : $customer_shipping_address_id : '';
             $billing_address = !empty($_SESSION['billing_address']) ? wpshop_tools::varSanitizer($_SESSION['billing_address']) : $customer_billing_address_id;
             if (!empty($billing_address)) {
                 wpshop_orders::set_order_customer_addresses($user_id, $order_id, $shipping_address, $billing_address);
             }
             if (!empty($_SESSION['shipping_address_to_save'])) {
                 $order_infos_postmeta = get_post_meta($order_id, '_order_info', true);
                 $order_infos_postmeta['shipping']['address'] = $_SESSION['shipping_address_to_save'];
                 $order_infos_postmeta['shipping']['address_id'] = '';
                 update_post_meta($order_id, '_order_info', $order_infos_postmeta);
                 unset($_SESSION['shipping_address_to_save']);
             }
             /** Save Coupon use **/
             if (!empty($_SESSION['cart']['coupon_id'])) {
                 $wps_coupon_mdl = new wps_coupon_model();
                 $wps_coupon_mdl->save_coupon_use($_SESSION['cart']['coupon_id']);
             }
             /**	Notify the customer as the case	*/
             $user_info = get_userdata($user_id);
             $email = $user_info->user_email;
             $first_name = $user_info->user_firstname;
             $last_name = $user_info->user_lastname;
             // Envoie du message de confirmation de commande au client
             $order_meta = get_post_meta($order_id, '_order_postmeta', true);
             $shipping_mode_option = get_option('wps_shipping_mode');
             $shipping_method = !empty($order_meta['order_payment']['shipping_method']) && !empty($shipping_mode_option) && !empty($shipping_mode_option['modes']) && is_array($shipping_mode_option['modes']) && array_key_exists($order_meta['order_payment']['shipping_method'], $shipping_mode_option['modes']) ? $shipping_mode_option['modes'][$order_meta['order_payment']['shipping_method']]['name'] : (!empty($order_meta['order_payment']['shipping_method']) ? $order_meta['order_payment']['shipping_method'] : '');
             if (!empty($order_meta) && !empty($order_meta['cart_type']) && $order_meta['cart_type'] == 'quotation' && empty($order_meta['order_key'])) {
                 $wps_message->wpshop_prepared_email($email, 'WPSHOP_QUOTATION_CONFIRMATION_MESSAGE', array('order_id' => $order_id, 'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'customer_email' => $email, 'order_date' => current_time('mysql', 0), 'order_content' => '', 'order_addresses' => '', 'order_customer_comments' => '', 'order_billing_address' => '', 'order_shipping_address' => '', 'order_shipping_method' => $shipping_method, 'order_personnal_informations' => ''));
             } else {
                 $email_option = get_option('wpshop_emails');
                 if (empty($email_option['send_confirmation_order_message'])) {
                     $payment_method_option = get_option('wps_payment_mode');
                     $order_payment_method = !empty($payment_method_option) && !empty($payment_method_option['mode']) && !empty($order_meta['order_payment']['customer_choice']['method']) && !empty($payment_method_option['mode'][$order_meta['order_payment']['customer_choice']['method']]) ? $payment_method_option['mode'][$order_meta['order_payment']['customer_choice']['method']]['name'] : $order_meta['order_payment']['customer_choice']['method'];
                     $wps_message->wpshop_prepared_email($email, 'WPSHOP_ORDER_CONFIRMATION_MESSAGE', array('order_id' => $order_id, 'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'customer_email' => $email, 'order_key' => !empty($order_meta['order_key']) ? $order_meta['order_key'] : '', 'order_date' => current_time('mysql', 0), 'order_payment_method' => $order_payment_method, 'order_content' => '', 'order_addresses' => '', 'order_customer_comments' => '', 'order_billing_address' => '', 'order_shipping_address' => '', 'order_shipping_method' => $shipping_method, 'order_personnal_informations' => ''));
                 }
             }
             if (empty($_SESSION['wps-pos-addon'])) {
                 $email_option = get_option('wpshop_emails');
                 if (empty($email_option) || !empty($email_option) && empty($email_option['send_confirmation_order_message'])) {
                     self::send_order_email_to_administrator($order_id, $user_info);
                 }
             }
             /** IF Order amount is 0, Finish the Order **/
             if ($cart['order_amount_to_pay_now'] == 0) {
                 $order_meta = get_post_meta($order_id, '_order_postmeta', true);
                 $payment_status = 'completed';
                 $params_array = array('method' => 'free', 'waited_amount' => $order_meta['order_amount_to_pay_now'], 'status' => 'payment_received', 'author' => $order_meta['customer_id'], 'payment_reference' => 'FREE_ORDER', 'date' => current_time('mysql', 0), 'received_amount' => $order_meta['order_amount_to_pay_now']);
                 wpshop_payment::check_order_payment_total_amount($order_id, $params_array, $payment_status);
             }
             apply_filters('wpshop_finish_order_extra_actions', $order_id);
         }
     }
     return $order_id;
 }
 function getCoupons()
 {
     $wps_coupon_mdl = new wps_coupon_model();
     $result = $wps_coupon_mdl->get_coupons();
     unset($wps_coupon_mdl);
     return $result;
 }