/**
  * Apply sign up fee or recurring fee discount before tax is calculated
  *
  *
  * @since 1.2
  */
 public static function apply_subscription_discount_before_tax($original_price, $product, $cart)
 {
     global $woocommerce;
     if (!WC_Subscriptions_Product::is_subscription($product['product_id'])) {
         return $original_price;
     }
     $price = $original_price;
     if (!empty($cart->applied_coupons)) {
         foreach ($cart->applied_coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax() && $coupon->is_valid()) {
                 // Sign up fee discount
                 if ('sign_up_fee' == WC_Subscriptions_Cart::get_recalculation_type() && 'sign_up_fee' == $coupon->type || 'base_recurring_fee' == WC_Subscriptions_Cart::get_recalculation_type() && 'recurring_fee' == $coupon->type || 0 == WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee() && 'recurring_fee' == $coupon->type) {
                     if ($original_price < $coupon->amount) {
                         $discount_amount = $original_price;
                     } else {
                         $discount_amount = $coupon->amount;
                     }
                     $price = $original_price - $coupon->amount;
                     if ($price < 0) {
                         $price = 0;
                     }
                     // add to discount totals
                     $woocommerce->cart->discount_cart = $woocommerce->cart->discount_cart + $discount_amount * $product['quantity'];
                 }
             }
         }
     }
     return $price;
 }
 /**
  * Apply sign up fee or recurring fee discount before tax is calculated
  *
  * @since 1.2
  */
 public static function apply_subscription_discount_before_tax($original_price, $cart_item, $cart)
 {
     global $woocommerce;
     $product_id = $cart_item['data']->is_type(array('subscription_variation')) ? $cart_item['data']->variation_id : $cart_item['data']->id;
     if (!WC_Subscriptions_Product::is_subscription($product_id)) {
         return $original_price;
     }
     $price = $original_price;
     $calculation_type = WC_Subscriptions_Cart::get_calculation_type();
     if (!empty($cart->applied_coupons)) {
         foreach ($cart->applied_coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax() && $coupon->is_valid()) {
                 $apply_sign_up_coupon = $apply_sign_up_percent_coupon = $apply_recurring_coupon = $apply_recurring_percent_coupon = $apply_initial_coupon = $apply_initial_percent_coupon = false;
                 // Apply sign-up fee discounts to sign-up total calculations
                 if ('sign_up_fee_total' == $calculation_type) {
                     $apply_sign_up_coupon = 'sign_up_fee' == $coupon->type ? true : false;
                     $apply_sign_up_percent_coupon = 'sign_up_fee_percent' == $coupon->type ? true : false;
                     // Apply recurring fee discounts to recurring total calculations
                 } elseif ('recurring_total' == $calculation_type) {
                     $apply_recurring_coupon = 'recurring_fee' == $coupon->type ? true : false;
                     $apply_recurring_percent_coupon = 'recurring_percent' == $coupon->type ? true : false;
                 }
                 if (in_array($calculation_type, array('combined_total', 'none'))) {
                     if (!WC_Subscriptions_Cart::cart_contains_free_trial()) {
                         // Apply recurring discounts to initial total
                         if ('recurring_fee' == $coupon->type) {
                             $apply_initial_coupon = true;
                         }
                         if ('recurring_percent' == $coupon->type) {
                             $apply_initial_percent_coupon = true;
                         }
                     }
                     if (WC_Subscriptions_Cart::get_cart_subscription_sign_up_fee() > 0) {
                         // Apply sign-up discounts to initial total
                         if ('sign_up_fee' == $coupon->type) {
                             $apply_initial_coupon = true;
                         }
                         if ('sign_up_fee_percent' == $coupon->type) {
                             $apply_initial_percent_coupon = true;
                         }
                     }
                 }
                 if ($apply_sign_up_coupon || $apply_recurring_coupon || $apply_initial_coupon) {
                     $discount_amount = $price < $coupon->amount ? $price : $coupon->amount;
                     // add to discount totals
                     $woocommerce->cart->discount_cart = $woocommerce->cart->discount_cart + $discount_amount * $cart_item['quantity'];
                     WC_Subscriptions_Cart::increase_coupon_discount_amount($coupon->code, $discount_amount * $cart_item['quantity']);
                     $price = $price - $discount_amount;
                     if ($price < 0) {
                         $price = 0;
                     }
                 } elseif ($apply_sign_up_percent_coupon || $apply_recurring_percent_coupon) {
                     $discount_amount = round($cart_item['data']->get_price() / 100 * $coupon->amount, $woocommerce->cart->dp);
                     $woocommerce->cart->discount_cart = $woocommerce->cart->discount_cart + $discount_amount;
                     WC_Subscriptions_Cart::increase_coupon_discount_amount($coupon->code, $discount_amount);
                     $price = $price - $discount_amount;
                 } elseif ($apply_initial_percent_coupon) {
                     // Need to calculate percent from base price
                     // We need to calculate the right amount to discount when the price is the combined sign-up fee and recurring amount
                     if ('combined_total' == $calculation_type && !WC_Subscriptions_Cart::cart_contains_free_trial() && isset($woocommerce->cart->base_sign_up_fees[$product_id]) && $woocommerce->cart->base_sign_up_fees[$product_id] > 0) {
                         $base_total = $woocommerce->cart->base_sign_up_fees[$product_id] + $woocommerce->cart->base_recurring_prices[$product_id];
                         if ('recurring_percent' == $coupon->type) {
                             $portion_of_total = $woocommerce->cart->base_recurring_prices[$product_id] / $base_total;
                         }
                         if ('sign_up_fee_percent' == $coupon->type) {
                             $portion_of_total = $woocommerce->cart->base_sign_up_fees[$product_id] / $base_total;
                         }
                         $amount_to_discount = WC_Subscriptions_Manager::get_amount_from_proportion($base_total, $portion_of_total);
                     } else {
                         $amount_to_discount = $cart_item['data']->get_price();
                     }
                     $discount_amount = round($amount_to_discount / 100 * $coupon->amount, $woocommerce->cart->dp);
                     $woocommerce->cart->discount_cart = $woocommerce->cart->discount_cart + $discount_amount;
                     WC_Subscriptions_Cart::increase_coupon_discount_amount($coupon->code, $discount_amount);
                     $price = $price - $discount_amount;
                 }
             }
         }
     }
     return $price;
 }
 /**
  * Get the coupon for the given ID
  *
  * @since 2.1
  * @param int $id the coupon ID
  * @param string $fields fields to include in response
  * @return array|WP_Error
  */
 public function get_coupon($id, $fields = null)
 {
     global $wpdb;
     $id = $this->validate_request($id, 'shop_coupon', 'read');
     if (is_wp_error($id)) {
         return $id;
     }
     // get the coupon code
     $code = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM {$wpdb->posts} WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $id));
     if (is_null($code)) {
         return new WP_Error('woocommerce_api_invalid_coupon_id', __('Invalid coupon ID', 'woocommerce'), array('status' => 404));
     }
     $coupon = new WC_Coupon($code);
     $coupon_post = get_post($coupon->id);
     $coupon_data = array('id' => $coupon->id, 'code' => $coupon->code, 'type' => $coupon->type, 'created_at' => $this->server->format_datetime($coupon_post->post_date_gmt), 'updated_at' => $this->server->format_datetime($coupon_post->post_modified_gmt), 'amount' => wc_format_decimal($coupon->amount, 2), 'individual_use' => 'yes' === $coupon->individual_use, 'product_ids' => array_map('absint', (array) $coupon->product_ids), 'exclude_product_ids' => array_map('absint', (array) $coupon->exclude_product_ids), 'usage_limit' => !empty($coupon->usage_limit) ? $coupon->usage_limit : null, 'usage_limit_per_user' => !empty($coupon->usage_limit_per_user) ? $coupon->usage_limit_per_user : null, 'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items, 'usage_count' => (int) $coupon->usage_count, 'expiry_date' => $this->server->format_datetime($coupon->expiry_date), 'apply_before_tax' => $coupon->apply_before_tax(), 'enable_free_shipping' => $coupon->enable_free_shipping(), 'product_category_ids' => array_map('absint', (array) $coupon->product_categories), 'exclude_product_category_ids' => array_map('absint', (array) $coupon->exclude_product_categories), 'exclude_sale_items' => $coupon->exclude_sale_items(), 'minimum_amount' => wc_format_decimal($coupon->minimum_amount, 2), 'customer_emails' => $coupon->customer_email);
     return array('coupon' => apply_filters('woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server));
 }
 /**
  * Apply sign up fee or recurring fee discount
  *
  * @since 1.2
  */
 public static function apply_subscription_discount($original_price, $cart_item, $cart)
 {
     $product_id = $cart_item['data']->is_type(array('subscription_variation')) ? $cart_item['data']->variation_id : $cart_item['data']->id;
     if (!WC_Subscriptions_Product::is_subscription($product_id)) {
         return $original_price;
     }
     $price = $calculation_price = $original_price;
     $calculation_type = WC_Subscriptions_Cart::get_calculation_type();
     if (!empty($cart->applied_coupons)) {
         foreach ($cart->applied_coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax() && $coupon->is_valid()) {
                 $apply_recurring_coupon = $apply_recurring_percent_coupon = $apply_initial_coupon = $apply_initial_percent_coupon = false;
                 // Apply recurring fee discounts to recurring total calculations
                 if ('recurring_total' == $calculation_type) {
                     $apply_recurring_coupon = 'recurring_fee' == $coupon->type ? true : false;
                     $apply_recurring_percent_coupon = 'recurring_percent' == $coupon->type ? true : false;
                 }
                 if ('none' == $calculation_type) {
                     // If all items have a free trial we don't need to apply recurring coupons to the initial total
                     if (!WC_Subscriptions_Cart::all_cart_items_have_free_trial()) {
                         if ('recurring_fee' == $coupon->type) {
                             $apply_initial_coupon = true;
                         }
                         if ('recurring_percent' == $coupon->type) {
                             $apply_initial_percent_coupon = true;
                         }
                     }
                     // Apply sign-up discounts to initial total
                     if (!empty($cart_item['data']->subscription_sign_up_fee)) {
                         if ('sign_up_fee' == $coupon->type) {
                             $apply_initial_coupon = true;
                         }
                         if ('sign_up_fee_percent' == $coupon->type) {
                             $apply_initial_percent_coupon = true;
                         }
                         $calculation_price = $cart_item['data']->subscription_sign_up_fee;
                     }
                 }
                 if ($apply_recurring_coupon || $apply_initial_coupon) {
                     $discount_amount = $calculation_price < $coupon->amount ? $calculation_price : $coupon->amount;
                     // Recurring coupons only apply when there is no free trial (carts can have a mix of free trial and non free trial items)
                     if ($apply_initial_coupon && 'recurring_fee' == $coupon->type && !empty($cart_item['data']->subscription_trial_length)) {
                         $discount_amount = 0;
                     }
                     $cart->discount_cart = $cart->discount_cart + $discount_amount * $cart_item['quantity'];
                     $cart = self::increase_coupon_discount_amount($cart, $coupon->code, $discount_amount * $cart_item['quantity']);
                     $price = $price - $discount_amount;
                 } elseif ($apply_recurring_percent_coupon) {
                     $discount_amount = round($calculation_price / 100 * $coupon->amount, WC()->cart->dp);
                     $cart->discount_cart = $cart->discount_cart + $discount_amount * $cart_item['quantity'];
                     $cart = self::increase_coupon_discount_amount($cart, $coupon->code, $discount_amount * $cart_item['quantity']);
                     $price = $price - $discount_amount;
                 } elseif ($apply_initial_percent_coupon) {
                     // Recurring coupons only apply when there is no free trial (carts can have a mix of free trial and non free trial items)
                     if ('recurring_percent' == $coupon->type && empty($cart_item['data']->subscription_trial_length)) {
                         $amount_to_discount = $cart_item['data']->subscription_price;
                     } else {
                         $amount_to_discount = 0;
                     }
                     // Sign up fee coupons only apply to sign up fees
                     if ('sign_up_fee_percent' == $coupon->type) {
                         $amount_to_discount = $cart_item['data']->subscription_sign_up_fee;
                     }
                     $discount_amount = round($amount_to_discount / 100 * $coupon->amount, WC()->cart->dp);
                     $cart->discount_cart = $cart->discount_cart + $discount_amount * $cart_item['quantity'];
                     $cart = self::increase_coupon_discount_amount($cart, $coupon->code, $discount_amount * $cart_item['quantity']);
                     $price = $price - $discount_amount;
                 }
             }
         }
         if ($price < 0) {
             $price = 0;
         }
     }
     return $price;
 }
 /**
  * gets the array of applied coupon codes
  */
 function remove_coupons($type = 0)
 {
     if ($type == 1) {
         if ($this->applied_coupons) {
             foreach ($this->applied_coupons as $index => $code) {
                 $coupon = new WC_Coupon($code);
                 if ($coupon->apply_before_tax()) {
                     unset($this->applied_coupons[$index]);
                 }
             }
         }
         $_SESSION['coupons'] = $this->applied_coupons;
     } elseif ($type == 2) {
         if ($this->applied_coupons) {
             foreach ($this->applied_coupons as $index => $code) {
                 $coupon = new WC_Coupon($code);
                 if (!$coupon->apply_before_tax()) {
                     unset($this->applied_coupons[$index]);
                 }
             }
         }
         $_SESSION['coupons'] = $this->applied_coupons;
     } else {
         unset($_SESSION['coupons']);
         $this->applied_coupons = array();
     }
 }
示例#6
0
 /**
  * Remove coupons from the cart of a defined type. Type 1 is before tax, type 2 is after tax.
  *
  * @params int type - 0 for all, 1 for before tax, 2 for after tax
  */
 public function remove_coupons($type = 0)
 {
     global $woocommerce;
     if (1 == $type) {
         if ($this->applied_coupons) {
             foreach ($this->applied_coupons as $index => $code) {
                 $coupon = new WC_Coupon($code);
                 if ($coupon->is_valid() && $coupon->apply_before_tax()) {
                     unset($this->applied_coupons[$index]);
                 }
             }
         }
         $woocommerce->session->coupon_codes = $this->applied_coupons;
     } elseif ($type == 2) {
         if ($this->applied_coupons) {
             foreach ($this->applied_coupons as $index => $code) {
                 $coupon = new WC_Coupon($code);
                 if ($coupon->is_valid() && !$coupon->apply_before_tax()) {
                     unset($this->applied_coupons[$index]);
                 }
             }
         }
         $woocommerce->session->coupon_codes = $this->applied_coupons;
     } else {
         unset($woocommerce->session->coupon_codes, $woocommerce->session->coupon_amounts);
         $this->applied_coupons = array();
     }
 }
 /**
  * Function to apply discounts to a product and get the discounted price (before tax is applied).
  *
  * @access public
  * @param mixed $values
  * @param mixed $price
  * @param bool $add_totals (default: false)
  * @return float price
  */
 public function get_discounted_price($values, $price, $add_totals = false)
 {
     if (!$price) {
         return $price;
     }
     if (!empty($this->applied_coupons)) {
         foreach ($this->applied_coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax() && $coupon->is_valid()) {
                 if ($coupon->is_valid_for_product($values['data']) || $coupon->is_valid_for_cart()) {
                     $discount_amount = $coupon->get_discount_amount($price, $values, $single = true);
                     $price = max($price - $discount_amount, 0);
                     if ($add_totals) {
                         $this->discount_cart += $discount_amount * $values['quantity'];
                         $this->increase_coupon_discount_amount($code, $discount_amount * $values['quantity']);
                         $this->increase_coupon_applied_count($code, $values['quantity']);
                     }
                 }
             }
         }
     }
     return apply_filters('woocommerce_get_discounted_price', $price, $values, $this);
 }
 /**
  * Function to apply discounts to a product and get the discounted price (before tax is applied)
  * 
  * @param mixed $values
  * @param mixed $price
  * @param bool $add_totals (default: false)
  * @return float price
  * @since 1.0
  */
 public static function get_discounted_price($values, $price, $add_totals = false)
 {
     global $woocommerce;
     if (!$price) {
         return $price;
     }
     if (!empty($woocommerce->cart->applied_coupons)) {
         foreach ($woocommerce->cart->applied_coupons as $code) {
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax() && $coupon->is_valid()) {
                 switch ($coupon->type) {
                     case "fixed_product":
                     case "percent_product":
                         $this_item_is_discounted = false;
                         $product_cats = wp_get_post_terms($values['product_id'], 'product_cat', array("fields" => "ids"));
                         // Specific products get the discount
                         if (sizeof($coupon->product_ids) > 0) {
                             if (in_array($values['product_id'], $coupon->product_ids) || in_array($values['variation_id'], $coupon->product_ids) || in_array($values['data']->get_parent(), $coupon->product_ids)) {
                                 $this_item_is_discounted = true;
                             }
                             // Category discounts
                         } elseif (sizeof($coupon->product_categories) > 0) {
                             if (sizeof(array_intersect($product_cats, $coupon->product_categories)) > 0) {
                                 $this_item_is_discounted = true;
                             }
                         } else {
                             // No product ids - all items discounted
                             $this_item_is_discounted = true;
                         }
                         // Specific product ID's excluded from the discount
                         if (sizeof($coupon->exclude_product_ids) > 0) {
                             if (in_array($values['product_id'], $coupon->exclude_product_ids) || in_array($values['variation_id'], $coupon->exclude_product_ids) || in_array($values['data']->get_parent(), $coupon->exclude_product_ids)) {
                                 $this_item_is_discounted = false;
                             }
                         }
                         // Specific categories excluded from the discount
                         if (sizeof($coupon->exclude_product_categories) > 0) {
                             if (sizeof(array_intersect($product_cats, $coupon->exclude_product_categories)) > 0) {
                                 $this_item_is_discounted = false;
                             }
                         }
                         // Apply filter
                         $this_item_is_discounted = apply_filters('woocommerce_item_is_discounted', $this_item_is_discounted, $values, $before_tax = true);
                         // Apply the discount
                         if ($this_item_is_discounted) {
                             if ($coupon->type == 'fixed_product') {
                                 if ($price < $coupon->amount) {
                                     $discount_amount = $price;
                                 } else {
                                     $discount_amount = $coupon->amount;
                                 }
                                 $price = $price - $coupon->amount;
                                 if ($price < 0) {
                                     $price = 0;
                                 }
                                 if ($add_totals) {
                                     $woocommerce->cart->sign_up_fee_discount_cart = $woocommerce->cart->sign_up_fee_discount_cart + $discount_amount * $values['quantity'];
                                 }
                             } elseif ($coupon->type == 'percent_product') {
                                 $percent_discount = WC_Subscriptions_Product::get_sign_up_fee_excluding_tax($values['data']) / 100 * $coupon->amount;
                                 if ($add_totals) {
                                     $woocommerce->cart->sign_up_fee_discount_cart = $woocommerce->cart->sign_up_fee_discount_cart + $percent_discount * $values['quantity'];
                                 }
                                 $price = $price - $percent_discount;
                             }
                         }
                         break;
                     case "fixed_cart":
                         /** 
                          * This is the most complex discount - we need to divide the discount between rows based on their price in
                          * proportion to the subtotal. This is so rows with different tax rates get a fair discount, and so rows
                          * with no price (free) don't get discount too.
                          */
                         // Get item discount by dividing item cost by subtotal to get a %
                         if ($woocommerce->cart->sign_up_fee_subtotal_ex_tax) {
                             $discount_percent = WC_Subscriptions_Product::get_sign_up_fee_excluding_tax($values['data']) * $values['quantity'] / $woocommerce->cart->sign_up_fee_subtotal_ex_tax;
                         } else {
                             $discount_percent = 0;
                         }
                         // Use pence to help prevent rounding errors
                         $coupon_amount_pence = $coupon->amount * 100;
                         // Work out the discount for the row
                         $item_discount = $coupon_amount_pence * $discount_percent;
                         // Work out discount per item
                         $item_discount = $item_discount / $values['quantity'];
                         // Pence
                         $price = $price * 100;
                         // Check if discount is more than price
                         if ($price < $item_discount) {
                             $discount_amount = $price;
                         } else {
                             $discount_amount = $item_discount;
                         }
                         // Take discount off of price (in pence)
                         $price = $price - $discount_amount;
                         // Back to pounds
                         $price = $price / 100;
                         // Cannot be below 0
                         if ($price < 0) {
                             $price = 0;
                         }
                         // Add coupon to discount total (once, since this is a fixed cart discount and we don't want rounding issues)
                         if ($add_totals) {
                             $woocommerce->cart->sign_up_fee_discount_cart = $woocommerce->cart->sign_up_fee_discount_cart + $discount_amount * $values['quantity'] / 100;
                         }
                         break;
                     case "percent":
                         $percent_discount = WC_Subscriptions_Product::get_sign_up_fee($values['data']->id) / 100 * $coupon->amount;
                         if ($add_totals) {
                             $woocommerce->cart->sign_up_fee_discount_cart = $woocommerce->cart->sign_up_fee_discount_cart + $percent_discount * $values['quantity'];
                         }
                         $price = $price - $percent_discount;
                         break;
                 }
             }
         }
     }
     return $price;
 }
 /**
  * Creates a n XML representation of an Order
  *
  * @access public
  * @param mixed $arr
  * @param $customerNumber
  * @return mixed
  */
 public function create($arr, $customerNumber)
 {
     $orderOptions = get_option('woocommerce_fortnox_order_settings');
     $freight_options = get_option('woocommerce_fortnox_freight_settings');
     $root = 'Order';
     $seq_order_number = get_post_meta($arr->id, '_order_number', true);
     if (!empty($seq_order_number)) {
         logthis($seq_order_number);
         $order['DocumentNumber'] = $seq_order_number;
     } else {
         $order['DocumentNumber'] = $arr->id;
     }
     $order['AdministrationFee'] = $orderOptions['admin-fee'];
     $order['OrderDate'] = substr($arr->order_date, 0, 10);
     $order['DeliveryDate'] = substr($arr->order_date, 0, 10);
     $order['Currency'] = $arr->get_order_currency();
     $order['CurrencyRate'] = '1';
     $order['CurrencyUnit'] = '1';
     $order['YourOrderNumber'] = $arr->id;
     $order['CustomerNumber'] = $customerNumber;
     $order['Address1'] = $arr->billing_address_1;
     $order['City'] = $arr->billing_city;
     $order['Country'] = $this->countries[$arr->billing_country];
     $order['Phone1'] = $arr->billing_phone;
     $order['DeliveryAddress1'] = $arr->shipping_address_1;
     $order['DeliveryCity'] = $arr->shipping_city;
     $order['DeliveryCountry'] = $this->countries[$arr->shipping_country];
     $order['DeliveryZipCode'] = $arr->shipping_postcode;
     $shipping_methods = $arr->get_shipping_methods();
     $shipping_method = reset($shipping_methods);
     if (!empty($shipping_method)) {
         if (!empty($shipping_method['method_id'])) {
             if (isset($freight_options[$shipping_method['method_id']])) {
                 $order['WayOfDelivery'] = $freight_options[$shipping_method['method_id']];
             }
         }
     }
     if ($arr->payment_method == 'klarna_checkout') {
         $order['ExternalInvoiceReference1'] = $arr->id;
     }
     if (isset($arr->billing_company) && $arr->billing_company != '') {
         $order['CustomerName'] = $arr->billing_company;
         $order['YourReference'] = $arr->billing_first_name . " " . $arr->billing_last_name;
     } else {
         $order['CustomerName'] = $arr->billing_first_name . " " . $arr->billing_last_name;
         $order['DeliveryName'] = $arr->billing_first_name . " " . $arr->billing_last_name;
     }
     if ($orderOptions['payment-options'] != '') {
         $order['TermsOfPayment'] = $orderOptions['payment-options'];
     }
     if ($orderOptions['cost-center'] != '') {
         $order['CostCenter'] = $orderOptions['cost-center'];
     }
     $include_freight_tax = get_option('woocommerce_prices_include_tax');
     if ($include_freight_tax == 'yes') {
         $order['Freight'] = $arr->get_total_shipping() * 0.8;
     } else {
         $order['Freight'] = $arr->get_total_shipping();
     }
     $order['VATIncluded'] = 'false';
     if ($orderOptions['add-payment-type'] == 'on') {
         $payment_method = get_post_meta($arr->id, '_payment_method_title');
         $order['Remarks'] = $payment_method[0];
     }
     $email = array();
     $email['EmailAddressTo'] = $arr->billing_email;
     $order['EmailInformation'] = $email;
     $invoicerows = array();
     //loop all items
     $index = 0;
     $pf = new WC_Product_Factory();
     foreach ($arr->get_items() as $item) {
         $key = "OrderRow" . $index;
         //if variable product there might be a different SKU
         $is_variation = false;
         if (empty($item['variation_id'])) {
             $productId = $item['product_id'];
         } else {
             $productId = $item['variation_id'];
             $is_variation = true;
         }
         $product = $pf->get_product($productId);
         //handles missing product
         $invoicerow = array();
         if (!($product == NULL)) {
             //!is_null($product)
             $invoicerow['ArticleNumber'] = $product->get_sku();
         }
         $invoicerow['Description'] = $this->get_item_name($item, $product, $is_variation);
         $invoicerow['Unit'] = 'st';
         $invoicerow['DeliveredQuantity'] = $item['qty'];
         $invoicerow['OrderedQuantity'] = $item['qty'];
         $invoicerow['Price'] = $this->get_product_price($item) / $item['qty'];
         $invoicerow['VAT'] = $this->get_tax_class_by_tax_name($product->get_tax_class(), $arr->shipping_country);
         $index += 1;
         $invoicerows[$key] = $invoicerow;
     }
     /****HANDLE FEES*****/
     foreach ($arr->get_fees() as $item) {
         $key = "OrderRow" . $index;
         $invoicerow['Description'] = $item['name'];
         $invoicerow['Unit'] = 'st';
         $invoicerow['DeliveredQuantity'] = 1;
         $invoicerow['OrderedQuantity'] = 1;
         $invoicerow['Price'] = $item['line_total'];
         $invoicerow['VAT'] = 25;
         $index += 1;
         $invoicerows[$key] = $invoicerow;
     }
     if ($arr->get_total_discount() > 0) {
         $coupon = $arr->get_used_coupons();
         $coupon = new WC_Coupon($coupon[0]);
         if (!$coupon->apply_before_tax()) {
             $key = "OrderRow" . $index;
             $invoicerow = array();
             $invoicerow['Description'] = "Rabatt";
             $invoicerow['Unit'] = 'st';
             $invoicerow['DeliveredQuantity'] = 1;
             $invoicerow['OrderedQuantity'] = 1;
             $invoicerow['Price'] = -1 * $arr->get_total_discount();
             $invoicerow['VAT'] = 0;
             $invoicerows[$key] = $invoicerow;
             $index += 1;
         }
     }
     /****HANDLE PRODUCT AS FREIGHT*****/
     if (!empty($orderOptions['freight-product-sku'])) {
         //RESET FREIGHT
         $order['Freight'] = 0;
         $product = $this->get_product_by_sku($orderOptions['freight-product-sku']);
         $key = "OrderRow" . $index;
         $invoicerow['ArticleNumber'] = $orderOptions['freight-product-sku'];
         $invoicerow['Description'] = $product->get_title();
         $invoicerow['Unit'] = 'st';
         $invoicerow['DeliveredQuantity'] = 1;
         $invoicerow['OrderedQuantity'] = 1;
         $invoicerow['Price'] = $arr->get_total_shipping();
         $invoicerow['VAT'] = $this->get_tax_class_by_tax_name($product->get_tax_class(), $arr->shipping_country);
         $invoicerows[$key] = $invoicerow;
     }
     $order['OrderRows'] = $invoicerows;
     logthis(print_r($order, true));
     return $this->generate($root, $order);
 }
 function __construct($plugin, $order, $cart = null)
 {
     global $woocommerce;
     $secretKey = $plugin->maksuturva_secretkey;
     $sellerId = $plugin->maksuturva_sellerid;
     $dueDate = date("d.m.Y");
     $id = $plugin->maksuturva_orderid_prefix . self::getMaksuturvaId($order->id);
     $pid = self::getMaksuturvaId($order->id);
     $products_rows = array();
     $discount_total = 0;
     $product_total = 0;
     $shipping_total = 0;
     foreach ($order->get_items() as $order_item_id => $item) {
         $product = $order->get_product_from_item($item);
         $desc = '';
         //$product->post->post_excerpt; // $product->get_title();
         if (defined('WOOCOMMERCE_VERSION') && version_compare(WOOCOMMERCE_VERSION, '2.4', '>')) {
             $woi = new WC_Order_Item_Meta($item);
         } else {
             $woi = new WC_Order_Item_Meta($order->get_item_meta($order_item_id, ''));
         }
         $gf = $woi->get_formatted();
         if ($gf) {
             foreach ($gf as $attr) {
                 $desc .= implode('=', $attr) . '|';
             }
         }
         $item_price_with_tax = $order->get_item_subtotal($item, true);
         $item_totalprice_with_tax = $order->get_item_total($item, true);
         $discount_pct = ($item_price_with_tax - $item_totalprice_with_tax) / $item_price_with_tax * 100.0;
         //( $line_price_with_tax - $line_totalprice_with_tax); //)) / $line_price_with_tax*100;
         if ($product->product_type == 'variable') {
             $desc .= implode(',', $product->get_variation_attributes()) . ' ';
         }
         $desc .= $product->post->post_excerpt;
         //apply_filters( 'woocommerce_short_description', $product->post->post_excerpt));
         //$product_total = $product_total + $item_totalprice_with_tax;
         //$discount_total = $discount_total - $line_price_with_tax - $item_totalprice_with_tax;
         $encoding = get_bloginfo('charset');
         //if($plugin->debug) {
         //	$plugin->log->add($plugin->id, $encoding.' Item total price='.$item_totalprice_with_tax." subtotal=".$item_price_with_tax." discount-%=".$discount_pct.":".$desc);
         //}
         $sku = '-';
         if ($product->get_sku()) {
             $sku = $product->get_sku();
         }
         $row = array('pmt_row_name' => $item['name'], 'pmt_row_desc' => strip_tags(html_entity_decode($desc)), 'pmt_row_quantity' => $item['qty'], 'pmt_row_articlenr' => $sku, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("%.2f", $order->get_item_subtotal($item, true))), 'pmt_row_vat' => str_replace('.', ',', sprintf("%.2f", $this->calc_tax_rate($product))), 'pmt_row_discountpercentage' => str_replace('.', ',', sprintf("%.2f", $discount_pct)), 'pmt_row_type' => 1);
         array_push($products_rows, $row);
     }
     //Coupons
     if ($cart) {
         foreach ($cart->get_coupons() as $code => $coupon) {
             $coupon_desc = $code;
             $coupon = new WC_Coupon($code);
             if ($coupon->apply_before_tax()) {
                 continue;
             }
             $coupon_post = get_post($coupon->id);
             $excerpt = $coupon_post->post_excerpt;
             if ($excerpt) {
                 $coupon_desc .= ' ' . $excerpt;
             }
             $row = array('pmt_row_name' => __('Discount', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($coupon_desc)), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("-%.2f", $cart->get_coupon_discount_amount($code))), 'pmt_row_vat' => "0,00", 'pmt_row_discountpercentage' => "00,00", 'pmt_row_type' => 6);
             array_push($products_rows, $row);
         }
     } elseif ($order->get_total_discount()) {
         $discount = $order->get_total_discount();
         $coupon_desc = implode(',', $order->get_used_coupons());
         $row = array('pmt_row_name' => __('Discount', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($coupon_desc)), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("-%.2f", $discount)), 'pmt_row_vat' => "0,00", 'pmt_row_discountpercentage' => "00,00", 'pmt_row_type' => 6);
         array_push($products_rows, $row);
     }
     //Shipping costs
     $shipping_cost = $order->get_total_shipping() + $order->get_shipping_tax();
     //- $order->get_shipping_tax( );
     if ($order->get_total_shipping() > 0) {
         $shipping_tax = 100 * $order->get_shipping_tax() / $order->get_total_shipping();
     } else {
         $shipping_tax = 0;
     }
     $row = array('pmt_row_name' => __('Shipping cost', $plugin->td), 'pmt_row_desc' => strip_tags(html_entity_decode($order->get_shipping_method())), 'pmt_row_quantity' => 1, 'pmt_row_deliverydate' => date("d.m.Y"), 'pmt_row_price_gross' => str_replace('.', ',', sprintf("%.2f", $shipping_cost)), 'pmt_row_vat' => str_replace('.', ',', sprintf("%.2f", $shipping_tax)), 'pmt_row_discountpercentage' => "0,00", 'pmt_row_type' => 2);
     array_push($products_rows, $row);
     $returnURL = $plugin->notify_url;
     if ($woocommerce->session && $woocommerce->session->id) {
         $sessionid = $woocommerce->session->id;
         $returnURL = add_query_arg('sessionid', $sessionid, $plugin->notify_url);
         //get_return_url( $order ));
         $returnURL = add_query_arg('orderid', $id, $returnURL);
     }
     //$returnURL = add_query_arg('gateway', 'wc_maksurva_emaksut', $returnURL);
     $billing_email = $order->billing_email;
     $billing_phone = $order->billing_phone;
     if (!empty($order->customer_user)) {
         $user = get_user_by('id', $order->customer_user);
         if (empty($order->billing_email)) {
             $billing_email = $user->user_email;
         }
         if (empty($order->billing_email)) {
             $billing_email = $user->user_phone;
         }
     }
     $locale = get_bloginfo('language');
     global $sitepress;
     if ($sitepress) {
         $locale = $sitepress->get_locale(ICL_LANGUAGE_CODE);
     }
     if ($locale != "fi_FI" || $locale != "sv_FI" || $locale != "en_FI") {
         if (substr($locale, 0, 2) == "fi") {
             $locale = "fi_FI";
         } elseif (strpos($locale, "sv") != FALSE) {
             $locale = "sv_FI";
         } else {
             $locale = "en_FI";
         }
     }
     $options = array("pmt_keygeneration" => $plugin->maksuturva_keyversion, "pmt_id" => $id, "pmt_orderid" => $order->id, "pmt_reference" => $pid, "pmt_sellerid" => $sellerId, "pmt_duedate" => $dueDate, "pmt_userlocale" => $locale, "pmt_okreturn" => add_query_arg('pmt_act', 'ok', $returnURL), "pmt_errorreturn" => add_query_arg('pmt_act', 'error', $returnURL), "pmt_cancelreturn" => add_query_arg('pmt_act', 'cancel', $returnURL), "pmt_delayedpayreturn" => add_query_arg('pmt_act', 'delay', $returnURL), "pmt_amount" => str_replace('.', ',', sprintf("%.2f", $order->get_total() - $shipping_cost)), "pmt_deliveryname" => trim($order->shipping_first_name . ' ' . $order->shipping_last_name), "pmt_deliveryaddress" => trim($order->shipping_address_1 . "  " . $order->shipping_address_2), "pmt_deliverypostalcode" => trim($this->clean($order->shipping_postcode, '000')), "pmt_deliverycity" => trim($this->clean($order->shipping_city)), "pmt_deliverycountry" => trim($this->clean($order->shipping_country)), "pmt_buyername" => trim($order->billing_first_name . ' ' . $order->billing_last_name), "pmt_buyeraddress" => trim($this->clean($order->billing_address_1 . "  " . $order->billing_address_2)), "pmt_buyerpostalcode" => trim($this->clean($order->billing_postcode, '000')), "pmt_buyercity" => trim($this->clean($order->billing_city)), "pmt_buyercountry" => trim($order->billing_country), "pmt_buyeremail" => trim($billing_email), "pmt_buyerphone" => trim($billing_phone), "pmt_escrow" => $plugin->maksuturva_emaksut == "no" ? "Y" : "N", "pmt_sellercosts" => str_replace('.', ',', sprintf("%.2f", $shipping_cost)), "pmt_rows" => count($products_rows), "pmt_rows_data" => $products_rows);
     parent::__construct($secretKey, $options, $plugin->maksuturva_encoding, $plugin->maksuturva_url);
 }