public function updateOrderStatus($orders_id, $orders_status, $mail_customer = 0)
 {
     if (!is_numeric($orders_id)) {
         return false;
     }
     $continue = 1;
     //hook to let other plugins further manipulate
     if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_befe.php']['updateOrderStatusPreProc'])) {
         $params = array('orders_id' => &$orders_id, 'orders_status' => &$orders_status, 'mail_customer' => &$mail_customer, 'continue' => &$continue);
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_befe.php']['updateOrderStatusPreProc'] as $funcRef) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
         }
     }
     if ($continue) {
         $order = mslib_fe::getOrder($orders_id);
         if ($order['orders_id']) {
             // dynamic variables
             if (isset($order['language_id'])) {
                 // Switch to language that is stored in the order
                 mslib_befe::setSystemLanguage($order['language_id']);
             }
             $billing_address = '';
             $delivery_address = '';
             $full_customer_name = $order['billing_first_name'];
             if ($order['billing_middle_name']) {
                 $full_customer_name .= ' ' . $order['billing_middle_name'];
             }
             if ($order['billing_last_name']) {
                 $full_customer_name .= ' ' . $order['billing_last_name'];
             }
             $delivery_full_customer_name = $order['delivery_first_name'];
             if ($order['delivery_middle_name']) {
                 $delivery_full_customer_name .= ' ' . $order['delivery_middle_name'];
             }
             if ($order['delivery_last_name']) {
                 $delivery_full_customer_name .= ' ' . $order['delivery_last_name'];
             }
             $full_customer_name = preg_replace('/\\s+/', ' ', $full_customer_name);
             $delivery_full_customer_name = preg_replace('/\\s+/', ' ', $delivery_full_customer_name);
             if (!$order['delivery_address'] or !$order['delivery_city']) {
                 $order['delivery_company'] = $order['billing_company'];
                 $order['delivery_address'] = $order['billing_address'];
                 $order['delivery_street_name'] = $order['billing_street_name'];
                 $order['delivery_address_number'] = $order['billing_address_number'];
                 $order['delivery_address_ext'] = $order['billing_address_ext'];
                 $order['delivery_building'] = $order['billing_building'];
                 $order['delivery_zip'] = $order['billing_zip'];
                 $order['delivery_city'] = $order['billing_city'];
                 $order['delivery_telephone'] = $order['billing_telephone'];
                 $order['delivery_mobile'] = $order['billing_mobile'];
             }
             if ($order['delivery_company']) {
                 $delivery_address = $order['delivery_company'] . "<br />";
             }
             if ($delivery_full_customer_name) {
                 $delivery_address .= $delivery_full_customer_name . "<br />";
             }
             if ($order['delivery_building']) {
                 $delivery_address .= $order['delivery_building'] . "<br />";
             }
             if ($order['delivery_address']) {
                 $delivery_address .= $order['delivery_address'] . "<br />";
             }
             if ($order['delivery_zip'] and $order['delivery_city']) {
                 $delivery_address .= $order['delivery_zip'] . " " . $order['delivery_city'];
             }
             //if ($order['delivery_telephone']) 		$delivery_address.=ucfirst($this->pi_getLL('telephone')).': '.$order['delivery_telephone']."<br />";
             //if ($order['delivery_mobile']) 			$delivery_address.=ucfirst($this->pi_getLL('mobile')).': '.$order['delivery_mobile']."<br />";
             if ($order['billing_company']) {
                 $billing_address = $order['billing_company'] . "<br />";
             }
             if ($full_customer_name) {
                 $billing_address .= $full_customer_name . "<br />";
             }
             if ($order['billing_building']) {
                 $billing_address .= $order['billing_building'] . "<br />";
             }
             if ($order['billing_address']) {
                 $billing_address .= $order['billing_address'] . "<br />";
             }
             if ($order['billing_zip'] and $order['billing_city']) {
                 $billing_address .= $order['billing_zip'] . " " . $order['billing_city'];
             }
             //if ($order['billing_telephone']) 		$billing_address.=ucfirst($this->pi_getLL('telephone')).': '.$order['billing_telephone']."<br />";
             //if ($order['billing_mobile']) 			$billing_address.=ucfirst($this->pi_getLL('mobile')).': '.$order['billing_mobile']."<br />";
             $array1 = array();
             $array2 = array();
             $array1[] = '###GENDER_SALUTATION###';
             $array2[] = mslib_fe::genderSalutation($order['billing_gender']);
             $array1[] = '###DELIVERY_FIRST_NAME###';
             $array2[] = $order['delivery_first_name'];
             $array1[] = '###DELIVERY_LAST_NAME###';
             $array2[] = preg_replace('/\\s+/', ' ', $order['delivery_middle_name'] . ' ' . $order['delivery_last_name']);
             $array1[] = '###BILLING_FIRST_NAME###';
             $array2[] = $order['billing_first_name'];
             $array1[] = '###BILLING_LAST_NAME###';
             $array2[] = preg_replace('/\\s+/', ' ', $order['billing_middle_name'] . ' ' . $order['billing_last_name']);
             $array1[] = '###BILLING_TELEPHONE###';
             $array2[] = $order['billing_telephone'];
             $array1[] = '###DELIVERY_TELEPHONE###';
             $array2[] = $order['delivery_telephone'];
             $array1[] = '###BILLING_MOBILE###';
             $array2[] = $order['billing_mobile'];
             $array1[] = '###DELIVERY_MOBILE###';
             $array2[] = $order['delivery_mobile'];
             $array1[] = '###BILLING_FULL_NAME###';
             $array2[] = $full_customer_name;
             $array1[] = '###FULL_NAME###';
             $array2[] = $full_customer_name;
             $array1[] = '###DELIVERY_FULL_NAME###';
             $array2[] = $delivery_full_customer_name;
             $array1[] = '###BILLING_NAME###';
             $array2[] = $order['billing_name'];
             $array1[] = '###BILLING_EMAIL###';
             $array2[] = $order['billing_email'];
             $array1[] = '###DELIVERY_EMAIL###';
             $array2[] = $order['delivery_email'];
             $array1[] = '###DELIVERY_NAME###';
             $array2[] = $order['delivery_name'];
             $array1[] = '###CUSTOMER_EMAIL###';
             $array2[] = $order['billing_email'];
             $array1[] = '###STORE_NAME###';
             $array2[] = $this->ms['MODULES']['STORE_NAME'];
             $array1[] = '###TOTAL_AMOUNT###';
             $array2[] = mslib_fe::amount2Cents($order['total_amount']);
             $ORDER_DETAILS = mslib_fe::printOrderDetailsTable($order, 'email');
             $array1[] = '###ORDER_DETAILS###';
             $array2[] = $ORDER_DETAILS;
             $array1[] = '###BILLING_ADDRESS###';
             $array2[] = $billing_address;
             $array1[] = '###DELIVERY_ADDRESS###';
             $array2[] = $delivery_address;
             $array1[] = '###CUSTOMER_ID###';
             $array2[] = $order['customer_id'];
             $array1[] = '###SHIPPING_METHOD###';
             $array2[] = $order['shipping_method_label'];
             $array1[] = '###PAYMENT_METHOD###';
             $array2[] = $order['payment_method_label'];
             $invoice = mslib_fe::getOrderInvoice($order['orders_id'], 0);
             $invoice_id = '';
             $invoice_link = '';
             if (is_array($invoice)) {
                 $invoice_id = $invoice['invoice_id'];
                 $invoice_link = '<a href="' . $this->FULL_HTTP_URL . mslib_fe::typolink($this->shop_pid . ',2002', 'tx_multishop_pi1[page_section]=download_invoice&tx_multishop_pi1[hash]=' . $invoice['hash']) . '">' . $invoice['invoice_id'] . '</a>';
             }
             $array1[] = '###INVOICE_NUMBER###';
             $array2[] = $invoice_id;
             $array1[] = '###INVOICE_LINK###';
             $array2[] = $invoice_link;
             $time = $order['crdate'];
             $long_date = strftime($this->pi_getLL('full_date_format'), $time);
             $array1[] = '###ORDER_DATE_LONG###';
             // ie woensdag 23 juni, 2010
             $array2[] = $long_date;
             // backwards compatibility
             $array1[] = '###ORDER_DATE###';
             // 21-12-2010 in localized format
             $array2[] = strftime("%x", $time);
             $array1[] = '###LONG_DATE###';
             // ie woensdag 23 juni, 2010
             $array2[] = $long_date;
             $time = time();
             $long_date = strftime($this->pi_getLL('full_date_format'), $time);
             $array1[] = '###CURRENT_DATE_LONG###';
             // ie woensdag 23 juni, 2010
             $array2[] = $long_date;
             $array1[] = '###STORE_NAME###';
             $array2[] = $this->ms['MODULES']['STORE_NAME'];
             $array1[] = '###TOTAL_AMOUNT###';
             $array2[] = mslib_fe::amount2Cents($order['total_amount']);
             $array1[] = '###PROPOSAL_NUMBER###';
             $array2[] = $order['orders_id'];
             $array1[] = '###ORDER_NUMBER###';
             $array2[] = $order['orders_id'];
             $array1[] = '###ORDER_LINK###';
             $array2[] = '';
             $array1[] = '###CUSTOMER_ID###';
             $array2[] = $order['customer_id'];
             $array1[] = '###CUSTOMER_COMMENTS###';
             $array2[] = $order['customer_comments'];
             $array1[] = '###MESSAGE###';
             $array2[] = $this->post['comments'];
             $array1[] = '###OLD_ORDER_STATUS###';
             $array2[] = mslib_fe::getOrderStatusName($order['status'], $order['language_id']);
             $array1[] = '###ORDER_STATUS###';
             $array2[] = mslib_fe::getOrderStatusName($orders_status, $order['language_id']);
             $array1[] = '###EXPECTED_DELIVERY_DATE###';
             if ($order['expected_delivery_date'] > 0) {
                 $array2[] = strftime("%x", $order['expected_delivery_date']);
             } else {
                 $array2[] = '';
             }
             $array1[] = '###EXPECTED_DELIVERY_DATE_LONG###';
             if ($order['expected_delivery_date'] > 0) {
                 $array2[] = strftime($this->pi_getLL('full_date_no_time_format'), $order['expected_delivery_date']);
             } else {
                 $array2[] = '';
             }
             $array1[] = '###TRACK_AND_TRACE_CODE###';
             $array2[] = $order['track_and_trace_code'];
             $array1[] = '###BILLING_STREET_NAME###';
             $array2[] = $order['billing_street_name'];
             $array1[] = '###BILLING_ADDRESS_NUMBER###';
             $array2[] = $order['billing_address_number'];
             $array1[] = '###BILLING_ADDRESS_EXT###';
             $array2[] = $order['billing_address_ext'];
             $array1[] = '###BILLING_ZIP###';
             $array2[] = $order['billing_zip'];
             $array1[] = '###BILLING_CITY###';
             $array2[] = $order['billing_city'];
             $array1[] = '###BILLING_COUNTRY###';
             $array2[] = mslib_fe::getTranslatedCountryNameByEnglishName($this->lang, $order['billing_country']);
             $array1[] = '###BILLING_COUNTRY_CODE###';
             $array2[] = mslib_fe::getCountryCnIsoByEnglishName($order['billing_country']);
             $array1[] = '###DELIVERY_STREET_NAME###';
             $array2[] = $order['delivery_street_name'];
             $array1[] = '###DELIVERY_ADDRESS_NUMBER###';
             $array2[] = $order['delivery_address_number'];
             $array1[] = '###DELIVERY_ADDRESS_EXT###';
             $array2[] = $order['delivery_address_ext'];
             $array1[] = '###DELIVERY_ZIP###';
             $array2[] = $order['delivery_zip'];
             $array1[] = '###DELIVERY_CITY###';
             $array2[] = $order['delivery_city'];
             $array1[] = '###DELIVERY_COUNTRY###';
             $array2[] = mslib_fe::getTranslatedCountryNameByEnglishName($this->lang, $order['delivery_country']);
             $array1[] = '###DELIVERY_COUNTRY_CODE###';
             $array2[] = mslib_fe::getCountryCnIsoByEnglishName($order['delivery_country']);
             // dynamic variablese eof
             //hook to let other plugins further manipulate
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_befe.php']['updateOrderStatusMarkerReplacerProc'])) {
                 $params = array('array1' => &$array1, 'array2' => &$array2, 'order' => &$order);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_befe.php']['updateOrderStatusMarkerReplacerProc'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
             }
             if ($this->post['comments']) {
                 $this->post['comments'] = str_replace($array1, $array2, $this->post['comments']);
             }
             $status_last_modified = time();
             $updateArray = array();
             $updateArray['orders_id'] = $order['orders_id'];
             $updateArray['old_value'] = $order['status'];
             $updateArray['comments'] = $this->post['comments'];
             $updateArray['customer_notified'] = $mail_customer;
             $updateArray['crdate'] = $status_last_modified;
             $updateArray['new_value'] = $orders_status;
             $updateArray['requester_ip_addr'] = $this->REMOTE_ADDR;
             $query = $GLOBALS['TYPO3_DB']->INSERTquery('tx_multishop_orders_status_history', $updateArray);
             if ($orders_status == $order['status']) {
                 if (!empty($this->post['comments']) && $mail_customer) {
                     // always save the order status history even when order status is the same as the old one when e-mail to client is filled
                     $res = $GLOBALS['TYPO3_DB']->sql_query($query);
                 }
                 $returnTrue = 0;
             } else {
                 // save if new order status history is different than the old status
                 $res = $GLOBALS['TYPO3_DB']->sql_query($query);
                 $returnTrue = 1;
             }
             $updateArray = array();
             $updateArray['status'] = $orders_status;
             $updateArray['status_last_modified'] = $status_last_modified;
             $order['old_status'] = $order['status'];
             $order['status'] = $orders_status;
             $updateArray['orders_last_modified'] = time();
             $query = $GLOBALS['TYPO3_DB']->UPDATEquery('tx_multishop_orders', 'orders_id=\'' . $orders_id . '\'', $updateArray);
             $res = $GLOBALS['TYPO3_DB']->sql_query($query);
             //send e-mail
             if ($mail_customer) {
                 $subject = $this->ms['MODULES']['STORE_NAME'];
                 $message = $this->post['comments'];
                 if ($orders_status) {
                     $orders_status_name = mslib_fe::getOrderStatusName($orders_status, 0);
                     $keys = array();
                     $keys[] = 'email_order_status_changed_' . mslib_befe::strtolower($orders_status_name);
                     $keys[] = 'email_order_status_changed';
                     foreach ($keys as $key) {
                         //$page=mslib_fe::getCMScontent($key,$GLOBALS['TSFE']->sys_language_uid);
                         $page = mslib_fe::getCMScontent($key, $order['language_id']);
                         if ($page[0]) {
                             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_befe.php']['updateOrderStatusMarkerReplacerPostProc'])) {
                                 $params = array('array1' => &$array1, 'array2' => &$array2, 'page' => &$page);
                                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_befe.php']['updateOrderStatusMarkerReplacerPostProc'] as $funcRef) {
                                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                                 }
                             }
                             if ($page[0]['content']) {
                                 $page[0]['content'] = str_replace($array1, $array2, $page[0]['content']);
                             }
                             if ($page[0]['name']) {
                                 $page[0]['name'] = str_replace($array1, $array2, $page[0]['name']);
                             }
                             $user = array();
                             $user['email'] = $order['billing_email'];
                             $user['name'] = $order['billing_name'];
                             if ($user['email']) {
                                 mslib_fe::mailUser($user, $page[0]['name'], $page[0]['content'], $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME']);
                             }
                             break;
                         }
                     }
                 }
             }
             if (isset($order['language_id'])) {
                 // Switch back to default language
                 mslib_befe::resetSystemLanguage();
             }
             //hook to let other plugins further manipulate
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_befe.php']['updateOrderStatusPostProc'])) {
                 $params = array('orders_id' => &$orders_id, 'orders_status' => &$orders_status, 'mail_customer' => &$mail_customer, 'order' => &$order, 'array1' => &$array1, 'array2' => &$array2);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_befe.php']['updateOrderStatusPostProc'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
             }
             if ($returnTrue) {
                 return true;
             }
         }
     }
 }
 function convertCartToOrder($cart)
 {
     // var for total amount
     $tax_separation = array();
     $total_price = 0;
     $order = array();
     $address = $cart['user'];
     // check for NULL, convert to empty string - typo3 v6.x related bug
     if (is_array($address) && count($address)) {
         foreach ($address as $key => $val) {
             if ($val == null || $val == null) {
                 $address[$key] = '';
             }
         }
     }
     // if store country is different from customer country and user provided valid VAT id, change VAT rate to zero
     $this->ms['MODULES']['DISABLE_VAT_RATE'] = 0;
     if ($this->ms['MODULES']['DISABLE_VAT_FOR_FOREIGN_CUSTOMERS_WITH_COMPANY_VAT_ID'] and $address['tx_multishop_vat_id']) {
         if (strtolower($address['country']) != strtolower($this->tta_shop_info['country'])) {
             $this->ms['MODULES']['DISABLE_VAT_RATE'] = 1;
         }
     }
     /*
      * always use *_tax and *_total_tax_rate, unless need different calc for country/region
      * WARNING: *_country_* and *_region_* not always have value, depends on the tax ruleset
      * -----------------------------------------------------------------------------------------
      */
     $orders_tax['shipping_tax'] = (string) $address['shipping_tax'];
     $orders_tax['shipping_country_tax'] = (string) $address['shipping_country_tax'];
     $orders_tax['shipping_region_tax'] = (string) $address['shipping_region_tax'];
     $orders_tax['shipping_total_tax_rate'] = (string) $address['shipping_total_tax_rate'];
     $orders_tax['shipping_country_tax_rate'] = (string) $address['shipping_country_tax_rate'];
     $orders_tax['shipping_region_tax_rate'] = (string) $address['shipping_region_tax_rate'];
     if ($this->ms['MODULES']['DISABLE_VAT_RATE']) {
         $orders_tax['shipping_tax'] = 0;
         $orders_tax['shipping_country_tax'] = 0;
         $orders_tax['shipping_region_tax'] = 0;
         $orders_tax['shipping_total_tax_rate'] = 0;
         $orders_tax['shipping_country_tax_rate'] = 0;
         $orders_tax['shipping_region_tax_rate'] = 0;
     }
     // ----------------------------------------------------------------------------------------
     $orders_tax['payment_tax'] = (string) $address['payment_tax'];
     $orders_tax['payment_country_tax'] = (string) $address['payment_country_tax'];
     $orders_tax['payment_region_tax'] = (string) $address['payment_region_tax'];
     $orders_tax['payment_total_tax_rate'] = (string) $address['payment_total_tax_rate'];
     $orders_tax['payment_country_tax_rate'] = (string) $address['payment_country_tax_rate'];
     $orders_tax['payment_region_tax_rate'] = (string) $address['payment_region_tax_rate'];
     if ($this->ms['MODULES']['DISABLE_VAT_RATE']) {
         $orders_tax['payment_tax'] = 0;
         $orders_tax['payment_country_tax'] = 0;
         $orders_tax['payment_region_tax'] = 0;
         $orders_tax['payment_total_tax_rate'] = 0;
         $orders_tax['payment_country_tax_rate'] = 0;
         $orders_tax['payment_region_tax_rate'] = 0;
     }
     // ----------------------------------------------------------------------------------------
     $grand_total = array();
     $grand_total['shipping_tax'] = $orders_tax['shipping_tax'];
     $grand_total['payment_tax'] = $orders_tax['payment_tax'];
     $tax_separation[$orders_tax['shipping_total_tax_rate'] * 100]['shipping_tax'] += $orders_tax['shipping_tax'];
     $tax_separation[$orders_tax['payment_total_tax_rate'] * 100]['payment_tax'] += $orders_tax['payment_tax'];
     if ($this->ms['MODULES']['DISABLE_VAT_RATE']) {
         $grand_total['shipping_tax'] = 0;
         $grand_total['payment_tax'] = 0;
         $tax_separation[$orders_tax['shipping_total_tax_rate'] * 100]['shipping_tax'] = 0;
         $tax_separation[$orders_tax['payment_total_tax_rate'] * 100]['payment_tax'] = 0;
         if (!$this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT']) {
             $address['shipping_method_costs'] = mslib_fe::taxDecimalCrop($address['shipping_method_costs'], 2, false);
             $address['payment_method_costs'] = mslib_fe::taxDecimalCrop($address['payment_method_costs'], 2, false);
         }
     }
     // add shipping & payment costs
     if ($address['shipping_method_costs']) {
         $grand_total['shipping_cost'] = $address['shipping_method_costs'];
         $total_price = $total_price + $address['shipping_method_costs'];
         $tax_separation[$orders_tax['shipping_total_tax_rate'] * 100]['shipping_costs'] = $address['shipping_method_costs'];
     }
     if ($address['payment_method_costs']) {
         $grand_total['payment_cost'] = $address['payment_method_costs'];
         $total_price = $total_price + $address['payment_method_costs'];
         $tax_separation[$orders_tax['payment_total_tax_rate'] * 100]['payment_costs'] = $address['payment_method_costs'];
     }
     $customer_id = '';
     // first the account
     if ($GLOBALS['TSFE']->fe_user->user['uid']) {
         $customer_id = $GLOBALS['TSFE']->fe_user->user['uid'];
     } else {
         $tmp_user = mslib_fe::getUser($address['email'], 'email');
         if ($tmp_user['uid']) {
             $customer_id = $tmp_user['uid'];
         }
     }
     //hook to let other plugins further manipulate the create table query
     if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.tx_mslib_user.php']['convertCartToOrderGetCustomerIdPreProc'])) {
         $params = array('address' => &$address, 'cart' => &$cart, 'customer_id' => &$customer_id);
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.tx_mslib_user.php']['convertCartToOrderGetCustomerIdPreProc'] as $funcRef) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
         }
     }
     if (!$customer_id) {
         // add new account
         $insertArray = array();
         $insertArray['company'] = $address['company'];
         $insertArray['name'] = $address['first_name'] . ' ' . $address['middle_name'] . ' ' . $address['last_name'];
         $insertArray['name'] = preg_replace('/\\s+/', ' ', $insertArray['name']);
         $insertArray['first_name'] = $address['first_name'];
         $insertArray['middle_name'] = $address['middle_name'];
         $insertArray['last_name'] = $address['last_name'];
         $insertArray['username'] = $address['email'];
         $insertArray['email'] = $address['email'];
         if (!$address['street_name']) {
             // fallback for old custom checkouts
             $insertArray['building'] = $address['building'];
             $insertArray['street_name'] = $address['address'];
             $insertArray['address_number'] = $address['address_number'];
             $insertArray['address_ext'] = $address['address_ext'];
             $insertArray['address'] = $insertArray['street_name'] . ' ' . $insertArray['address_number'] . ($insertArray['address_ext'] ? '-' . $insertArray['address_ext'] : '');
             $insertArray['address'] = preg_replace('/\\s+/', ' ', $insertArray['address']);
         } else {
             $insertArray['building'] = $address['building'];
             $insertArray['street_name'] = $address['street_name'];
             $insertArray['address_number'] = $address['address_number'];
             $insertArray['address_ext'] = $address['address_ext'];
             $insertArray['address'] = $address['address'];
         }
         $insertArray['zip'] = $address['zip'];
         $insertArray['telephone'] = $address['telephone'];
         $insertArray['city'] = $address['city'];
         $insertArray['country'] = $address['country'];
         $insertArray['tx_multishop_code'] = md5(uniqid('', true));
         $insertArray['tstamp'] = time();
         $insertArray['crdate'] = time();
         if (isset($address['tx_multishop_newsletter']) && !empty($address['tx_multishop_newsletter'])) {
             $insertArray['tx_multishop_newsletter'] = $address['tx_multishop_newsletter'];
         } else {
             $insertArray['tx_multishop_newsletter'] = '';
         }
         $insertArray['page_uid'] = $this->shop_pid;
         if (isset($address['password']) && !empty($address['password'])) {
             $insertArray['password'] = mslib_befe::getHashedPassword($address['password']);
             $insertArray['disable'] = 1;
         } else {
             $insertArray['password'] = mslib_befe::getHashedPassword(mslib_befe::generateRandomPassword(10));
         }
         $insertArray['usergroup'] = $this->conf['fe_customer_usergroup'];
         $insertArray['pid'] = $this->conf['fe_customer_pid'];
         if (isset($this->cookie['HTTP_REFERER']) && !empty($this->cookie['HTTP_REFERER'])) {
             $insertArray['http_referer'] = $this->cookie['HTTP_REFERER'];
         } else {
             $insertArray['http_referer'] = '';
         }
         $insertArray['ip_address'] = $this->server['REMOTE_ADDR'];
         if ($this->ms['MODULES']['CHECKOUT_DISPLAY_VAT_ID_INPUT'] && !empty($address['tx_multishop_vat_id'])) {
             $insertArray['tx_multishop_vat_id'] = $address['tx_multishop_vat_id'];
         }
         if ($this->ms['MODULES']['CHECKOUT_DISPLAY_COC_ID_INPUT'] && !empty($address['tx_multishop_coc_id'])) {
             $insertArray['tx_multishop_coc_id'] = $address['tx_multishop_coc_id'];
         }
         $insertArray['tx_multishop_quick_checkout'] = 1;
         if ($address['gender'] == 'm' or $address['gender'] == '0') {
             $insertArray['gender'] = '0';
         } elseif ($address['gender'] == 'f' or $address['gender'] == '1') {
             $insertArray['gender'] = '1';
         }
         $insertArray = mslib_befe::rmNullValuedKeys($insertArray);
         $query = $GLOBALS['TYPO3_DB']->INSERTquery('fe_users', $insertArray);
         $res = $GLOBALS['TYPO3_DB']->sql_query($query);
         if ($res) {
             $customer_id = $GLOBALS['TYPO3_DB']->sql_insert_id();
             // ADD TT_ADDRESS RECORD
             $insertArray = array();
             $insertArray['tstamp'] = time();
             $insertArray['company'] = $address['company'];
             $insertArray['name'] = $address['first_name'] . ' ' . $address['middle_name'] . ' ' . $address['last_name'];
             $insertArray['name'] = preg_replace('/\\s+/', ' ', $insertArray['name']);
             $insertArray['first_name'] = $address['first_name'];
             $insertArray['middle_name'] = $address['middle_name'];
             $insertArray['last_name'] = $address['last_name'];
             $insertArray['email'] = $address['email'];
             if (!$address['street_name']) {
                 // fallback for old custom checkouts
                 $insertArray['building'] = $address['building'];
                 $insertArray['street_name'] = $address['address'];
                 $insertArray['address_number'] = $address['address_number'];
                 $insertArray['address_ext'] = $address['address_ext'];
                 $insertArray['address'] = $insertArray['street_name'] . ' ' . $insertArray['address_number'] . ($insertArray['address_ext'] ? '-' . $insertArray['address_ext'] : '');
                 $insertArray['address'] = preg_replace('/\\s+/', ' ', $insertArray['address']);
             } else {
                 $insertArray['building'] = $address['building'];
                 $insertArray['street_name'] = $address['street_name'];
                 $insertArray['address_number'] = $address['address_number'];
                 $insertArray['address_ext'] = $address['address_ext'];
                 $insertArray['address'] = $address['address'];
             }
             $insertArray['zip'] = $address['zip'];
             $insertArray['phone'] = $address['telephone'];
             $insertArray['mobile'] = $address['mobile'];
             $insertArray['city'] = $address['city'];
             $insertArray['country'] = $address['country'];
             $insertArray['gender'] = $address['gender'];
             $insertArray['birthday'] = strtotime($address['birthday']);
             if ($address['gender'] == 'm') {
                 $insertArray['title'] = 'Mr.';
             } else {
                 if ($address['gender'] == 'f') {
                     $insertArray['title'] = 'Mrs.';
                 }
             }
             $insertArray['region'] = $address['state'];
             $insertArray['pid'] = $this->conf['fe_customer_pid'];
             $insertArray['page_uid'] = $this->shop_pid;
             $insertArray['tstamp'] = time();
             $insertArray['tx_multishop_address_type'] = 'billing';
             $insertArray['tx_multishop_default'] = 1;
             $insertArray['tx_multishop_customer_id'] = $customer_id;
             $insertArray = mslib_befe::rmNullValuedKeys($insertArray);
             $query = $GLOBALS['TYPO3_DB']->INSERTquery('tt_address', $insertArray);
             $res = $GLOBALS['TYPO3_DB']->sql_query($query);
             // send out the create account confirmation e-mail
             if (isset($address['password']) && !empty($address['password'])) {
                 mslib_fe::sendCreateAccountConfirmationLetter($customer_id, $address['password']);
             }
             // insert delivery into tt_address
             if (!$address['different_delivery_address']) {
                 $insertArray = array();
                 $insertArray['tstamp'] = time();
                 $insertArray['company'] = $address['company'];
                 $insertArray['name'] = $address['first_name'] . ' ' . $address['middle_name'] . ' ' . $address['last_name'];
                 $insertArray['name'] = preg_replace('/\\s+/', ' ', $insertArray['name']);
                 $insertArray['first_name'] = $address['first_name'];
                 $insertArray['middle_name'] = $address['middle_name'];
                 $insertArray['last_name'] = $address['last_name'];
                 $insertArray['email'] = $address['email'];
                 if (!$address['street_name']) {
                     // fallback for old custom checkouts
                     $insertArray['building'] = $address['building'];
                     $insertArray['street_name'] = $address['address'];
                     $insertArray['address_number'] = $address['address_number'];
                     $insertArray['address_ext'] = $address['address_ext'];
                     $insertArray['address'] = $insertArray['street_name'] . ' ' . $insertArray['address_number'] . ($insertArray['address_ext'] ? '-' . $insertArray['address_ext'] : '');
                     $insertArray['address'] = preg_replace('/\\s+/', ' ', $insertArray['address']);
                 } else {
                     $insertArray['building'] = $address['building'];
                     $insertArray['street_name'] = $address['street_name'];
                     $insertArray['address_number'] = $address['address_number'];
                     $insertArray['address_ext'] = $address['address_ext'];
                     $insertArray['address'] = $address['address'];
                 }
                 $insertArray['zip'] = $address['zip'];
                 $insertArray['phone'] = $address['telephone'];
                 $insertArray['mobile'] = $address['mobile'];
                 $insertArray['city'] = $address['city'];
                 $insertArray['country'] = $address['country'];
                 $insertArray['gender'] = $address['gender'];
                 $insertArray['birthday'] = strtotime($address['birthday']);
                 if ($address['gender'] == 'm') {
                     $insertArray['title'] = 'Mr.';
                 } else {
                     if ($address['gender'] == 'f') {
                         $insertArray['title'] = 'Mrs.';
                     }
                 }
                 $insertArray['region'] = $address['state'];
             } else {
                 $insertArray = array();
                 $insertArray['tx_multishop_customer_id'] = $customer_id;
                 $insertArray['tstamp'] = time();
                 $insertArray['company'] = $address['delivery_company'];
                 $insertArray['name'] = $address['delivery_first_name'] . ' ' . $address['delivery_middle_name'] . ' ' . $address['delivery_last_name'];
                 $insertArray['name'] = preg_replace('/\\s+/', ' ', $insertArray['name']);
                 $insertArray['first_name'] = $address['delivery_first_name'];
                 $insertArray['middle_name'] = $address['delivery_middle_name'];
                 $insertArray['last_name'] = $address['delivery_last_name'];
                 $insertArray['email'] = $address['delivery_email'];
                 if (!$address['delivery_street_name']) {
                     // fallback for old custom checkouts
                     $insertArray['building'] = $address['delivery_building'];
                     $insertArray['street_name'] = $address['delivery_address'];
                     $insertArray['address_number'] = $address['delivery_address_number'];
                     $insertArray['address_ext'] = $address['delivery_address_ext'];
                     $insertArray['address'] = $insertArray['street_name'] . ' ' . $insertArray['address_number'] . ($insertArray['address_ext'] ? '-' . $insertArray['address_ext'] : '');
                     $insertArray['address'] = preg_replace('/\\s+/', ' ', $insertArray['address']);
                 } else {
                     $insertArray['building'] = $address['delivery_building'];
                     $insertArray['street_name'] = $address['delivery_street_name'];
                     $insertArray['address_number'] = $address['delivery_address_number'];
                     $insertArray['address_ext'] = $address['delivery_address_ext'];
                     $insertArray['address'] = $address['delivery_address'];
                 }
                 $insertArray['zip'] = $address['delivery_zip'];
                 $insertArray['phone'] = $address['delivery_telephone'];
                 $insertArray['mobile'] = $address['delivery_mobile'];
                 $insertArray['city'] = $address['delivery_city'];
                 $insertArray['country'] = $address['delivery_country'];
                 $insertArray['gender'] = $address['delivery_gender'];
                 $insertArray['birthday'] = strtotime($address['delivery_birthday']);
                 if ($address['delivery_gender'] == 'm') {
                     $insertArray['title'] = 'Mr.';
                 } else {
                     if ($address['delivery_gender'] == 'f') {
                         $insertArray['title'] = 'Mrs.';
                     }
                 }
                 $insertArray['region'] = $address['delivery_state'];
             }
             $insertArray['pid'] = $this->conf['fe_customer_pid'];
             $insertArray['page_uid'] = $this->shop_pid;
             $insertArray['tstamp'] = time();
             $insertArray['tx_multishop_customer_id'] = $customer_id;
             $insertArray['tx_multishop_address_type'] = 'delivery';
             $insertArray['tx_multishop_default'] = 0;
             $insertArray = mslib_befe::rmNullValuedKeys($insertArray);
             $query = $GLOBALS['TYPO3_DB']->INSERTquery('tt_address', $insertArray);
             $res = $GLOBALS['TYPO3_DB']->sql_query($query);
             // ADD TT_ADDRESS RECORD EOF
             //hook to let other plugins further manipulate the create table query
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.tx_mslib_user.php']['createUserPostProc'])) {
                 $params = array('customer_id' => &$customer_id);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.tx_mslib_user.php']['createUserPostProc'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
             }
         }
     } else {
         // insert tt_address for existing customer if no record found
         if (!mslib_fe::getFeUserTTaddressDetails($customer_id, 'billing')) {
             // ADD TT_ADDRESS RECORD
             $insertArray = array();
             $insertArray['tstamp'] = time();
             $insertArray['company'] = $address['company'];
             $insertArray['name'] = $address['first_name'] . ' ' . $address['middle_name'] . ' ' . $address['last_name'];
             $insertArray['name'] = preg_replace('/\\s+/', ' ', $insertArray['name']);
             $insertArray['first_name'] = $address['first_name'];
             $insertArray['middle_name'] = $address['middle_name'];
             $insertArray['last_name'] = $address['last_name'];
             $insertArray['email'] = $address['email'];
             if (!$address['street_name']) {
                 // fallback for old custom checkouts
                 $insertArray['building'] = $address['building'];
                 $insertArray['street_name'] = $address['address'];
                 $insertArray['address_number'] = $address['address_number'];
                 $insertArray['address_ext'] = $address['address_ext'];
                 $insertArray['address'] = $insertArray['street_name'] . ' ' . $insertArray['address_number'] . ($insertArray['address_ext'] ? '-' . $insertArray['address_ext'] : '');
                 $insertArray['address'] = preg_replace('/\\s+/', ' ', $insertArray['address']);
             } else {
                 $insertArray['building'] = $address['delivery_building'];
                 $insertArray['street_name'] = $address['street_name'];
                 $insertArray['address_number'] = $address['address_number'];
                 $insertArray['address_ext'] = $address['address_ext'];
                 $insertArray['address'] = $address['address'];
             }
             $insertArray['zip'] = $address['zip'];
             $insertArray['phone'] = $address['telephone'];
             $insertArray['mobile'] = $address['mobile'];
             $insertArray['city'] = $address['city'];
             $insertArray['country'] = $address['country'];
             $insertArray['gender'] = $address['gender'];
             $insertArray['birthday'] = strtotime($address['birthday']);
             if ($address['gender'] == 'm') {
                 $insertArray['title'] = 'Mr.';
             } else {
                 if ($address['gender'] == 'f') {
                     $insertArray['title'] = 'Mrs.';
                 }
             }
             $insertArray['region'] = $address['state'];
             $insertArray['pid'] = $this->conf['fe_customer_pid'];
             $insertArray['page_uid'] = $this->shop_pid;
             $insertArray['tstamp'] = time();
             $insertArray['tx_multishop_address_type'] = 'billing';
             $insertArray['tx_multishop_default'] = 1;
             $insertArray['tx_multishop_customer_id'] = $customer_id;
             $insertArray = mslib_befe::rmNullValuedKeys($insertArray);
             $query = $GLOBALS['TYPO3_DB']->INSERTquery('tt_address', $insertArray);
             $res = $GLOBALS['TYPO3_DB']->sql_query($query);
         }
         if (!mslib_fe::getFeUserTTaddressDetails($customer_id, 'delivery')) {
             // insert delivery into tt_address
             if (!$address['different_delivery_address']) {
                 $insertArray = array();
                 $insertArray['tstamp'] = time();
                 $insertArray['company'] = $address['company'];
                 $insertArray['name'] = $address['first_name'] . ' ' . $address['middle_name'] . ' ' . $address['last_name'];
                 $insertArray['name'] = preg_replace('/\\s+/', ' ', $insertArray['name']);
                 $insertArray['first_name'] = $address['first_name'];
                 $insertArray['middle_name'] = $address['middle_name'];
                 $insertArray['last_name'] = $address['last_name'];
                 $insertArray['email'] = $address['email'];
                 if (!$address['street_name']) {
                     // fallback for old custom checkouts
                     $insertArray['building'] = $address['building'];
                     $insertArray['street_name'] = $address['address'];
                     $insertArray['address_number'] = $address['address_number'];
                     $insertArray['address_ext'] = $address['address_ext'];
                     $insertArray['address'] = $insertArray['street_name'] . ' ' . $insertArray['address_number'] . ($insertArray['address_ext'] ? '-' . $insertArray['address_ext'] : '');
                     $insertArray['address'] = preg_replace('/\\s+/', ' ', $insertArray['address']);
                 } else {
                     $insertArray['building'] = $address['building'];
                     $insertArray['street_name'] = $address['street_name'];
                     $insertArray['address_number'] = $address['address_number'];
                     $insertArray['address_ext'] = $address['address_ext'];
                     $insertArray['address'] = $address['address'];
                 }
                 $insertArray['zip'] = $address['zip'];
                 $insertArray['phone'] = $address['telephone'];
                 $insertArray['mobile'] = $address['mobile'];
                 $insertArray['city'] = $address['city'];
                 $insertArray['country'] = $address['country'];
                 $insertArray['gender'] = $address['gender'];
                 $insertArray['birthday'] = strtotime($address['birthday']);
                 if ($address['gender'] == 'm') {
                     $insertArray['title'] = 'Mr.';
                 } else {
                     if ($address['gender'] == 'f') {
                         $insertArray['title'] = 'Mrs.';
                     }
                 }
                 $insertArray['region'] = $address['state'];
             } else {
                 $insertArray = array();
                 $insertArray['tx_multishop_customer_id'] = $customer_id;
                 $insertArray['tstamp'] = time();
                 $insertArray['company'] = $address['delivery_company'];
                 $insertArray['name'] = $address['delivery_first_name'] . ' ' . $address['delivery_middle_name'] . ' ' . $address['delivery_last_name'];
                 $insertArray['name'] = preg_replace('/\\s+/', ' ', $insertArray['name']);
                 $insertArray['first_name'] = $address['delivery_first_name'];
                 $insertArray['middle_name'] = $address['delivery_middle_name'];
                 $insertArray['last_name'] = $address['delivery_last_name'];
                 $insertArray['email'] = $address['delivery_email'];
                 if (!$address['delivery_street_name']) {
                     // fallback for old custom checkouts
                     $insertArray['building'] = $address['building'];
                     $insertArray['street_name'] = $address['delivery_address'];
                     $insertArray['address_number'] = $address['delivery_address_number'];
                     $insertArray['address_ext'] = $address['delivery_address_ext'];
                     $insertArray['address'] = $insertArray['street_name'] . ' ' . $insertArray['address_number'] . ($insertArray['address_ext'] ? '-' . $insertArray['address_ext'] : '');
                     $insertArray['address'] = preg_replace('/\\s+/', ' ', $insertArray['address']);
                 } else {
                     $insertArray['building'] = $address['building'];
                     $insertArray['street_name'] = $address['delivery_street_name'];
                     $insertArray['address_number'] = $address['delivery_address_number'];
                     $insertArray['address_ext'] = $address['delivery_address_ext'];
                     $insertArray['address'] = $address['delivery_address'];
                 }
                 $insertArray['zip'] = $address['delivery_zip'];
                 $insertArray['phone'] = $address['delivery_telephone'];
                 $insertArray['mobile'] = $address['delivery_mobile'];
                 $insertArray['city'] = $address['delivery_city'];
                 $insertArray['country'] = $address['delivery_country'];
                 $insertArray['gender'] = $address['delivery_gender'];
                 $insertArray['birthday'] = strtotime($address['delivery_birthday']);
                 if ($address['delivery_gender'] == 'm') {
                     $insertArray['title'] = 'Mr.';
                 } else {
                     if ($address['delivery_gender'] == 'f') {
                         $insertArray['title'] = 'Mrs.';
                     }
                 }
                 $insertArray['region'] = $address['delivery_state'];
             }
             $insertArray['pid'] = $this->conf['fe_customer_pid'];
             $insertArray['page_uid'] = $this->shop_pid;
             $insertArray['tstamp'] = time();
             $insertArray['tx_multishop_customer_id'] = $customer_id;
             $insertArray['tx_multishop_address_type'] = 'delivery';
             $insertArray['tx_multishop_default'] = 0;
             $insertArray = mslib_befe::rmNullValuedKeys($insertArray);
             $query = $GLOBALS['TYPO3_DB']->INSERTquery('tt_address', $insertArray);
             $res = $GLOBALS['TYPO3_DB']->sql_query($query);
         }
     }
     if ($customer_id) {
         // now add the order
         $insertArray = array();
         $insertArray['customer_id'] = $customer_id;
         $insertArray['page_uid'] = $this->shop_pid;
         if (isset($GLOBALS['TSFE']->fe_user->user['uid']) && !empty($GLOBALS['TSFE']->fe_user->user['uid'])) {
             $insertArray['cruser_id'] = $GLOBALS['TSFE']->fe_user->user['uid'];
         } else {
             $insertArray['cruser_id'] = '';
         }
         $insertArray['customer_comments'] = $this->post['customer_comments'];
         $insertArray['billing_company'] = $address['company'];
         $insertArray['billing_first_name'] = $address['first_name'];
         $insertArray['billing_middle_name'] = $address['middle_name'];
         $insertArray['billing_last_name'] = $address['last_name'];
         $insertArray['billing_name'] = preg_replace('/ +/', ' ', $address['first_name'] . ' ' . $address['middle_name'] . ' ' . $address['last_name']);
         $insertArray['billing_email'] = $address['email'];
         $insertArray['billing_gender'] = $address['gender'];
         $insertArray['billing_birthday'] = strtotime($address['birthday']);
         if (!$address['street_name']) {
             // fallback for old custom checkouts
             $insertArray['billing_building'] = $address['building'];
             $insertArray['billing_street_name'] = $address['address'];
             $insertArray['billing_address_number'] = $address['address_number'];
             $insertArray['billing_address_ext'] = $address['address_ext'];
             $insertArray['billing_address'] = $insertArray['billing_street_name'] . ' ' . $insertArray['billing_address_number'] . ($insertArray['billing_address_ext'] ? '-' . $insertArray['billing_address_ext'] : '');
             $insertArray['billing_address'] = preg_replace('/\\s+/', ' ', $insertArray['billing_address']);
         } else {
             $insertArray['billing_building'] = $address['building'];
             $insertArray['billing_street_name'] = $address['street_name'];
             $insertArray['billing_address_number'] = $address['address_number'];
             $insertArray['billing_address_ext'] = $address['address_ext'];
             $insertArray['billing_address'] = $address['address'];
         }
         /*
         			$insertArray['billing_street_name']			=	$address['street_name'];
         			$insertArray['billing_address_number']		=	$address['address_number'];
         			$insertArray['billing_address_ext']			=	$address['address_ext'];
         			$insertArray['billing_address']				=	$insertArray['billing_street_name'].' '.$insertArray['billing_address_number'].($insertArray['billing_address_ext']? '-'.$insertArray['billing_address_ext']:'');
         			$insertArray['billing_address'] 			=	preg_replace('/\s+/', ' ', $insertArray['billing_address']);
         */
         $insertArray['billing_room'] = '';
         $insertArray['billing_city'] = $address['city'];
         $insertArray['billing_zip'] = $address['zip'];
         $insertArray['billing_region'] = $address['state'];
         $insertArray['billing_country'] = $address['country'];
         $insertArray['billing_telephone'] = $address['telephone'];
         $insertArray['billing_mobile'] = $address['mobile'];
         $insertArray['billing_fax'] = '';
         if ($this->ms['MODULES']['CHECKOUT_DISPLAY_VAT_ID_INPUT'] && !empty($address['tx_multishop_vat_id'])) {
             $insertArray['billing_vat_id'] = $address['tx_multishop_vat_id'];
         }
         if ($this->ms['MODULES']['CHECKOUT_DISPLAY_COC_ID_INPUT'] && !empty($address['tx_multishop_coc_id'])) {
             $insertArray['billing_coc_id'] = $address['tx_multishop_coc_id'];
         }
         if (!$address['different_delivery_address']) {
             $insertArray['delivery_email'] = $insertArray['billing_email'];
             $insertArray['delivery_company'] = $insertArray['billing_company'];
             $insertArray['delivery_first_name'] = $insertArray['billing_first_name'];
             $insertArray['delivery_middle_name'] = $insertArray['billing_middle_name'];
             $insertArray['delivery_last_name'] = $insertArray['billing_last_name'];
             $insertArray['delivery_telephone'] = $insertArray['billing_telephone'];
             $insertArray['delivery_mobile'] = $insertArray['billing_mobile'];
             $insertArray['delivery_gender'] = $insertArray['billing_gender'];
             $insertArray['delivery_building'] = $insertArray['billing_building'];
             $insertArray['delivery_street_name'] = $insertArray['billing_street_name'];
             $insertArray['delivery_address_number'] = $insertArray['billing_address_number'];
             $insertArray['delivery_address_ext'] = $insertArray['billing_address_ext'];
             $insertArray['delivery_address'] = $insertArray['billing_address'];
             $insertArray['delivery_zip'] = $insertArray['billing_zip'];
             $insertArray['delivery_city'] = $insertArray['billing_city'];
             $insertArray['delivery_country'] = $insertArray['billing_country'];
             $insertArray['delivery_telephone'] = $insertArray['billing_telephone'];
             $insertArray['delivery_region'] = $insertArray['billing_region'];
             $insertArray['delivery_name'] = $insertArray['billing_name'];
             $insertArray['delivery_birthday'] = $insertArray['billing_birthday'];
         } else {
             $insertArray['delivery_company'] = $address['delivery_company'];
             $insertArray['delivery_first_name'] = $address['delivery_first_name'];
             $insertArray['delivery_middle_name'] = $address['delivery_middle_name'];
             $insertArray['delivery_last_name'] = $address['delivery_last_name'];
             $insertArray['delivery_name'] = preg_replace('/ +/', ' ', $address['delivery_first_name'] . ' ' . $address['delivery_middle_name'] . ' ' . $address['delivery_last_name']);
             $insertArray['delivery_email'] = $address['delivery_email'];
             $insertArray['delivery_gender'] = $address['delivery_gender'];
             if (!$address['street_name']) {
                 // fallback for old custom checkouts
                 $insertArray['delivery_building'] = $address['delivery_building'];
                 $insertArray['delivery_street_name'] = $address['delivery_address'];
                 $insertArray['delivery_address_number'] = $address['delivery_address_number'];
                 $insertArray['delivery_address_ext'] = $address['delivery_address_ext'];
                 $insertArray['delivery_address'] = $insertArray['delivery_street_name'] . ' ' . $insertArray['delivery_address_number'] . ($insertArray['delivery_address_ext'] ? '-' . $insertArray['delivery_address_ext'] : '');
                 $insertArray['delivery_address'] = preg_replace('/\\s+/', ' ', $insertArray['delivery_address']);
             } else {
                 $insertArray['delivery_building'] = $address['delivery_building'];
                 $insertArray['delivery_street_name'] = $address['delivery_street_name'];
                 $insertArray['delivery_address_number'] = $address['delivery_address_number'];
                 $insertArray['delivery_address_ext'] = $address['delivery_address_ext'];
                 $insertArray['delivery_address'] = $address['delivery_address'];
             }
             /*
             				$insertArray['delivery_street_name']		=	$address['delivery_street_name'];
             				$insertArray['delivery_address_number']		=	$address['delivery_address_number'];
             				$insertArray['delivery_address_ext']		=	$address['delivery_address_ext'];
             				$insertArray['delivery_address']			=	$insertArray['delivery_street_name'].' '.$insertArray['delivery_address_number'].($insertArray['delivery_address_ext']? '-'.$insertArray['delivery_address_ext']:'');
             				$insertArray['delivery_address'] 			=	preg_replace('/\s+/', ' ', $insertArray['delivery_address']);
             */
             $insertArray['delivery_city'] = $address['delivery_city'];
             $insertArray['delivery_zip'] = $address['delivery_zip'];
             $insertArray['delivery_room'] = '';
             $insertArray['delivery_region'] = $address['delivery_state'];
             $insertArray['delivery_country'] = $address['delivery_country'];
             $insertArray['delivery_telephone'] = $address['delivery_telephone'];
             $insertArray['delivery_mobile'] = $address['delivery_mobile'];
             $insertArray['delivery_fax'] = '';
             $insertArray['delivery_vat_id'] = '';
             $insertArray['delivery_birthday'] = strtotime($address['delivery_birthday']);
         }
         $insertArray['bill'] = 1;
         $insertArray['crdate'] = time();
         $insertArray['shipping_method'] = $address['shipping_method'];
         $insertArray['shipping_method_label'] = $address['shipping_method_label'];
         $insertArray['payment_method'] = $address['payment_method'];
         $insertArray['payment_method_label'] = $address['payment_method_label'];
         $insertArray['shipping_method_costs'] = $address['shipping_method_costs'];
         $insertArray['payment_method_costs'] = $address['payment_method_costs'];
         $insertArray['hash'] = md5(uniqid('', true));
         $insertArray['store_currency'] = $this->ms['MODULES']['CURRENCY_ARRAY']['cu_iso_3'];
         if (isset($this->ms['MODULES']['CUSTOMER_CURRENCY_ARRAY']['cu_iso_3']) && !empty($this->ms['MODULES']['CUSTOMER_CURRENCY_ARRAY']['cu_iso_3'])) {
             $insertArray['customer_currency'] = $this->ms['MODULES']['CUSTOMER_CURRENCY_ARRAY']['cu_iso_3'];
         } else {
             $insertArray['customer_currency'] = $this->ms['MODULES']['CURRENCY_ARRAY']['cu_iso_3'];
         }
         if (isset($this->cookie['currency_rate']) && !empty($this->cookie['currency_rate'])) {
             $insertArray['currency_rate'] = $this->cookie['currency_rate'];
         } else {
             $insertArray['currency_rate'] = 1;
         }
         $insertArray['language_id'] = $this->sys_language_uid;
         // get default orders status
         $status = mslib_fe::getDefaultOrdersStatus($this->sys_language_uid);
         $default_order_status_id = 0;
         if (is_array($status) && isset($status['id']) && $status['id'] > 0) {
             $default_order_status_id = $status['id'];
         }
         // set the order status based on payment method settings
         $payment_method = mslib_fe::loadPaymentMethod($address['payment_method']);
         $payment_method_vars = unserialize($payment_method['vars']);
         $payment_method_vars['default_order_status'] = (int) $payment_method_vars['default_order_status'];
         if (isset($payment_method_vars['default_order_status']) && is_numeric($payment_method_vars['default_order_status']) && $payment_method_vars['default_order_status'] > 0) {
             $default_order_status_id = $payment_method_vars['default_order_status'];
         }
         if ($default_order_status_id > 0) {
             $insertArray['status'] = $default_order_status_id;
         } else {
             $insertArray['status'] = '';
         }
         if (isset($this->cookie['HTTP_REFERER']) && !empty($this->cookie['HTTP_REFERER'])) {
             $insertArray['http_referer'] = $this->cookie['HTTP_REFERER'];
         } else {
             $insertArray['http_referer'] = '';
         }
         $insertArray['ip_address'] = $this->server['REMOTE_ADDR'];
         $insertArray['user_agent'] = $this->server['HTTP_USER_AGENT'];
         if (isset($address['expected_delivery_date'])) {
             $insertArray['expected_delivery_date'] = $address['expected_delivery_date'];
         }
         $user = mslib_fe::getUser($customer_id);
         $insertArray['payment_condition'] = '';
         if (is_numeric($user['tx_multishop_payment_condition']) && $user['tx_multishop_payment_condition'] > 0) {
             $insertArray['payment_condition'] = $user['tx_multishop_payment_condition'];
         }
         //$insertArray['orders_tax_data']			=	serialize($orders_tax);
         if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderPreProc'])) {
             // hook
             $params = array('ms' => $this->ms, 'address' => &$address, 'insertArray' => &$insertArray);
             foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderPreProc'] as $funcRef) {
                 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
             }
             // hook oef
         }
         $insertArray = mslib_befe::rmNullValuedKeys($insertArray);
         $query = $GLOBALS['TYPO3_DB']->INSERTquery('tx_multishop_orders', $insertArray);
         $res = $GLOBALS['TYPO3_DB']->sql_query($query);
         // now add the order eof
         $orders_id = $GLOBALS['TYPO3_DB']->sql_insert_id();
         if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderPostProc'])) {
             // hook
             $params = array('orders_id' => &$orders_id, 'insertArray' => &$insertArray, 'cart' => &$cart);
             foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderPostProc'] as $funcRef) {
                 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
             }
             // hook oef
         }
         if (!$orders_id) {
             $subject = $this->FULL_HTTP_URL . ' - Convert cart to order failed';
             $body = 'Warning. Convert cart to order failed.<br/>Website: ' . $this->FULL_HTTP_URL . '<br/>Error: ' . $GLOBALS['TYPO3_DB']->sql_error() . '<br/>Query:<br/>' . $query;
             $mailuser = array();
             $mailuser['name'] = $this->ms['MODULES']['STORE_NAME'];
             $mailuser['email'] = $this->ms['MODULES']['STORE_EMAIL'];
             if ($this->ms['MODULES']['DEVELOPER_EMAIL']) {
                 $mailuser['email'] = $this->ms['MODULES']['DEVELOPER_EMAIL'];
             }
             if ($mailuser['email']) {
                 mslib_fe::mailUser($mailuser, $subject, $body, $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME']);
             }
         } else {
             // now add the orders products
             if ($cart['user']['payment_method']) {
                 $this->ms['payment_method'] = $cart['user']['payment_method'];
             } elseif ($cart['user']['shipping_method']) {
                 $this->ms['shipping_method'] = $cart['user']['shipping_method'];
             }
             if (is_array($cart['products']) && count($cart['products'])) {
                 foreach ($cart['products'] as $shopping_cart_item => $value) {
                     if (is_numeric($value['products_id'])) {
                         if ($this->ms['MODULES']['DISABLE_VAT_RATE']) {
                             $value['tax_rate'] = 0;
                         }
                         $insertArray = array();
                         $insertArray['orders_id'] = $orders_id;
                         $insertArray['products_id'] = $value['products_id'];
                         $insertArray['categories_id'] = $value['categories_id'];
                         // get all cats
                         $cats = mslib_fe::Crumbar($value['categories_id']);
                         $cats = array_reverse($cats);
                         if (count($cats) > 0) {
                             $i = 0;
                             foreach ($cats as $cat) {
                                 $insertArray['categories_id_' . $i] = $cat['id'];
                                 $insertArray['categories_name_' . $i] = $cat['name'];
                                 $i++;
                             }
                         }
                         // get all cats eof
                         if (isset($value['manufacturers_id']) && !empty($value['manufacturers_id'])) {
                             $insertArray['manufacturers_id'] = $value['manufacturers_id'];
                         } else {
                             $insertArray['manufacturers_id'] = '';
                         }
                         if (isset($value['order_unit_id']) && !empty($value['order_unit_id'])) {
                             $insertArray['order_unit_id'] = $value['order_unit_id'];
                         } else {
                             $insertArray['order_unit_id'] = '';
                         }
                         if (isset($value['order_unit_name']) && !empty($value['order_unit_name'])) {
                             $insertArray['order_unit_name'] = $value['order_unit_name'];
                         } else {
                             $insertArray['order_unit_name'] = '';
                         }
                         if (isset($value['order_unit_code']) && !empty($value['order_unit_code'])) {
                             $insertArray['order_unit_code'] = $value['order_unit_code'];
                         } else {
                             $insertArray['order_unit_code'] = '';
                         }
                         $insertArray['qty'] = $value['qty'];
                         $insertArray['products_tax'] = $value['tax_rate'] * 100;
                         $insertArray['products_name'] = $value['products_name'];
                         $insertArray['products_model'] = $value['products_model'];
                         /*
                         $insertArray['products_description']=$value['products_shortdescription'];
                         if (is_array($value['attributes'])) {
                         	// loading the attributes
                         	//$insertArray['products_description'].="\n".strip_tags(mslib_fe::showAttributes($value['products_id'], '', $sessionData, 1));
                         	$insertArray['products_description'].="\n".mslib_fe::showAttributes($value['products_id'], '', $sessionData, 1);
                         	// loading the attributes eof
                         }
                         */
                         $insertArray['products_price'] = $value['products_price'];
                         $insertArray['final_price'] = $value['final_price'];
                         $insertArray['product_capital_price'] = $value['product_capital_price'];
                         $insertArray['type'] = 'P';
                         // P for Product, S for Subscription (returning-costs)
                         $insertArray['ean_code'] = $value['ean_code'];
                         $insertArray['sku_code'] = $value['sku_code'];
                         $insertArray['vendor_code'] = $value['vendor_code'];
                         $insertArray['products_tax_id'] = $value['tax_id'];
                         // micro download
                         if ($value['file_location'] || $value['file_remote_location']) {
                             $insertArray['file_label'] = $value['file_label'];
                             $insertArray['file_location'] = $value['file_location'];
                             $insertArray['file_remote_location'] = $value['file_remote_location'];
                             $insertArray['file_number_of_downloads'] = $value['file_number_of_downloads'];
                             $insertArray['file_download_code'] = md5(uniqid(rand()) . uniqid(rand()));
                         }
                         // micro download eof
                         /*
                          * always use total_tax and total_tax_rate, unless need different calc for country/region
                          * WARNING: country_* and region_* not always have value, depends on the tax ruleset
                          * -----------------------------------------------------------------------------------------
                          */
                         $product_tax['country_tax_rate'] = (string) $value['country_tax_rate'];
                         $product_tax['region_tax_rate'] = (string) $value['region_tax_rate'];
                         $product_tax['total_tax_rate'] = (string) $value['tax_rate'];
                         // -----------------------------------------------------------------------------------------
                         $product_tax['country_tax'] = (string) $value['country_tax'];
                         $product_tax['region_tax'] = (string) $value['region_tax'];
                         $product_tax['total_tax'] = (string) $value['tax'];
                         $product_tax['total_attributes_tax'] = (string) $value['total_attributes_tax'];
                         // -----------------------------------------------------------------------------------------
                         if (!$this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT'] || $this->ms['MODULES']['FORCE_CHECKOUT_SHOW_PRICES_INCLUDING_VAT']) {
                             //$product_tax['total_tax']=mslib_fe::taxDecimalCrop($product_tax['total_tax'], 2, false);
                             //$product_tax['total_attributes_tax']=mslib_fe::taxDecimalCrop($product_tax['total_attributes_tax'], 2, false);
                             $product_tax['total_tax'] = round($product_tax['total_tax'], 2);
                             $product_tax['total_attributes_tax'] = round($product_tax['total_attributes_tax'], 2);
                         }
                         if ($this->ms['MODULES']['DISABLE_VAT_RATE']) {
                             $product_tax['country_tax_rate'] = 0;
                             $product_tax['region_tax_rate'] = 0;
                             $product_tax['total_tax_rate'] = 0;
                             $product_tax['country_tax'] = 0;
                             $product_tax['region_tax'] = 0;
                             $product_tax['total_tax'] = 0;
                             $product_tax['total_attributes_tax'] = 0;
                         }
                         // bugfixes bas
                         $sub_total_excluding_vat['final_price'] = $sub_total['final_price'] + $value['final_price'] * $value['qty'];
                         $sub_total['final_price'] = $sub_total['final_price'] + $value['final_price'] * $value['qty'];
                         if (!$this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT'] || $this->ms['MODULES']['FORCE_CHECKOUT_SHOW_PRICES_INCLUDING_VAT']) {
                             $sub_total['total_tax'] = $sub_total['total_tax'] + round($product_tax['total_tax'] * $value['qty'], 2);
                             $sub_total['attributes_tax'] = $sub_total['attributes_tax'] + round($product_tax['total_attributes_tax'] * $value['qty'], 2);
                             $total_order_tax['total_tax'] = $total_order_tax['total_tax'] + round($product_tax['total_tax'] * $value['qty'], 2);
                             $total_order_tax['total_attributes_tax'] = $total_order_tax['total_attributes_tax'] + round($product_tax['total_attributes_tax'] * $value['qty'], 2);
                         } else {
                             $sub_total['total_tax'] = $sub_total['total_tax'] + $product_tax['total_tax'] * $value['qty'];
                             $sub_total['attributes_tax'] = $sub_total['attributes_tax'] + $product_tax['total_attributes_tax'] * $value['qty'];
                             $total_order_tax['total_tax'] = $total_order_tax['total_tax'] + $product_tax['total_tax'] * $value['qty'];
                             $total_order_tax['total_attributes_tax'] = $total_order_tax['total_attributes_tax'] + $product_tax['total_attributes_tax'] * $value['qty'];
                         }
                         $insertArray['products_tax_data'] = serialize($product_tax);
                         // separation of tax
                         $tax_separation[$value['tax_rate'] * 100]['products_total_tax'] += $product_tax['total_tax'] * $value['qty'] + $product_tax['total_attributes_tax'] * $value['qty'];
                         $tax_separation[$value['tax_rate'] * 100]['products_sub_total_excluding_vat'] += $value['final_price'] * $value['qty'];
                         $tax_separation[$value['tax_rate'] * 100]['products_sub_total'] += ($value['final_price'] + $product_tax['total_tax'] + $product_tax['total_attributes_tax']) * $value['qty'];
                         if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrdersProductPreProc'])) {
                             // hook
                             $params = array('ms' => $this->ms, 'value' => $value, 'insertArray' => &$insertArray);
                             foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrdersProductPreProc'] as $funcRef) {
                                 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                             }
                             // hook oef
                         }
                         // TYPO3 6.2 LTS NULL FIX
                         $insertArray = mslib_befe::rmNullValuedKeys($insertArray);
                         $query = $GLOBALS['TYPO3_DB']->INSERTquery('tx_multishop_orders_products', $insertArray);
                         $res = $GLOBALS['TYPO3_DB']->sql_query($query);
                         $orders_products_id = $GLOBALS['TYPO3_DB']->sql_insert_id();
                         if (!$orders_products_id) {
                             error_log('ERROR:' . $GLOBALS['TYPO3_DB']->sql_error());
                         }
                         // update orders_products sort_order
                         $updateOrderProductsSortOrder = array();
                         $updateOrderProductsSortOrder['sort_order'] = $orders_products_id;
                         $query = $GLOBALS['TYPO3_DB']->UPDATEquery('tx_multishop_orders_products', 'orders_products_id=\'' . $orders_products_id . '\'', $updateOrderProductsSortOrder);
                         $res = $GLOBALS['TYPO3_DB']->sql_query($query);
                         if ($this->ms['MODULES']['SUBTRACT_STOCK']) {
                             $continue_update_stock = true;
                             // hook to manipulate the continuity of update stock
                             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_mslib_cart.php']['updateStockPreProc'])) {
                                 // hook
                                 $params = array('ms' => $this->ms, 'value' => $value, 'continue_update_stock' => &$continue_update_stock);
                                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_mslib_cart.php']['updateStockPreProc'] as $funcRef) {
                                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                                 }
                                 // hook oef
                             }
                             //
                             if ($continue_update_stock) {
                                 if ($this->ms['MODULES']['PRODUCT_ATTRIBUTES_STOCK']) {
                                     $sql_as_data = array();
                                     $attributes_count = count($value['attributes']);
                                     foreach ($value['attributes'] as $attribute_key => $attribute_values) {
                                         $sql_as_data[] = '(pas.options_id = ' . $attribute_values['options_id'] . ' and pas.options_values_id = ' . $attribute_values['options_values_id'] . ')';
                                     }
                                     $sql_as = "select pasg.group_id, pasg.attributes_stock from tx_multishop_products_attributes_stock_group pasg, tx_multishop_products_attributes_stock pas where pasg.products_id = " . $value['products_id'] . " and (" . implode(' or ', $sql_as_data) . ") and pasg.group_id = pas.group_id";
                                     $res = $GLOBALS['TYPO3_DB']->sql_query($sql_as);
                                     $total_rows = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
                                     $used_group = 0;
                                     if ($total_rows > 1) {
                                         $group_counter = array();
                                         while ($rs_as = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
                                             $group_counter[$rs_as['group_id']] += 1;
                                         }
                                         foreach ($group_counter as $ref_group_id => $group_ctr_result) {
                                             if ($group_ctr_result == $attributes_count) {
                                                 $used_group = $ref_group_id;
                                                 break;
                                             }
                                         }
                                     } else {
                                         $rs_as = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                                         $used_group = $rs_as['group_id'];
                                     }
                                     if ($used_group > 0) {
                                         $str = "update tx_multishop_products_attributes_stock_group set attributes_stock=(attributes_stock-" . $value['qty'] . ") where group_id='" . $used_group . "'";
                                         $res = $GLOBALS['TYPO3_DB']->sql_query($str);
                                     }
                                     $str = "update tx_multishop_products set products_quantity=(products_quantity-" . $value['qty'] . ") where products_id='" . $value['products_id'] . "'";
                                     $res = $GLOBALS['TYPO3_DB']->sql_query($str);
                                     $str = "select products_quantity, alert_quantity_threshold from tx_multishop_products where products_id='" . $value['products_id'] . "'";
                                     $res = $GLOBALS['TYPO3_DB']->sql_query($str);
                                     $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                                     if ($row['products_quantity'] <= $row['alert_quantity_threshold']) {
                                         $page = mslib_fe::getCMScontent('email_alert_quantity_threshold_letter', $GLOBALS['TSFE']->sys_language_uid);
                                         if ($page[0]['content']) {
                                             // loading the email confirmation letter eof
                                             // replacing the variables with dynamic values
                                             $array1 = array();
                                             $array2 = array();
                                             $array1[] = '###ORDERED_QTY###';
                                             $array2[] = $value['qty'];
                                             $array1[] = '###CURRENT_PRODUCT_QUANTITY###';
                                             $array2[] = $row['products_id'];
                                             $array1[] = '###PRODUCT_ID###';
                                             $array2[] = $row['products_quantity'];
                                             $array1[] = '###PRODUCT_NAME###';
                                             $array2[] = $value['products_name'];
                                             $link_edit_prod = $this->FULL_HTTP_URL . mslib_fe::typolink($this->shop_pid . ',2003', 'tx_multishop_pi1[page_section]=edit_product&pid=' . $value['products_id'] . '&cid=' . $value['categories_id'] . '&action=edit_product');
                                             $array1[] = '###DIRECT_EDIT_PRODUCT_LINK###';
                                             $array2[] = '<a href="' . $link_edit_prod . '" target="_blank">' . htmlspecialchars($this->pi_getLL('admin_edit_product')) . '</a>';
                                             // now mail a copy to the merchant
                                             $merchant = array();
                                             $merchant['name'] = $this->ms['MODULES']['STORE_NAME'];
                                             $merchant['email'] = $this->ms['MODULES']['STORE_EMAIL'];
                                             $mailTo = array();
                                             $mailTo[] = $merchant;
                                             //hook to let other plugins further manipulate the replacers
                                             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.tx_mslib_cart.php']['mailAlertQuantityThresholdPostProc'])) {
                                                 $params = array('array1' => &$array1, 'array2' => &$array2, 'page' => &$page, 'mailTo' => &$mailTo);
                                                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.tx_mslib_cart.php']['mailAlertQuantityThresholdPostProc'] as $funcRef) {
                                                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                                                 }
                                             }
                                             //end of hook to let other plugins further manipulate the replacers
                                             if ($page[0]['content']) {
                                                 $page[0]['content'] = str_replace($array1, $array2, $page[0]['content']);
                                             }
                                             if ($page[0]['name']) {
                                                 $page[0]['name'] = str_replace($array1, $array2, $page[0]['name']);
                                             }
                                             foreach ($mailTo as $mailuser) {
                                                 mslib_fe::mailUser($mailuser, $page[0]['name'], $page[0]['content'], $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME']);
                                             }
                                         }
                                     }
                                     if ($row['products_quantity'] < 1) {
                                         // stock is negative or zero. lets disable the product
                                         $str = "update tx_multishop_products set products_status=0 where products_id='" . $value['products_id'] . "'";
                                         $res = $GLOBALS['TYPO3_DB']->sql_query($str);
                                     }
                                 } else {
                                     // now decrease the stocklevel
                                     $str = "update tx_multishop_products set products_quantity=(products_quantity-" . $value['qty'] . ") where products_id='" . $value['products_id'] . "'";
                                     $res = $GLOBALS['TYPO3_DB']->sql_query($str);
                                     $str = "select products_quantity, alert_quantity_threshold from tx_multishop_products where products_id='" . $value['products_id'] . "'";
                                     $res = $GLOBALS['TYPO3_DB']->sql_query($str);
                                     $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                                     if ($row['products_quantity'] <= $row['alert_quantity_threshold']) {
                                         $page = mslib_fe::getCMScontent('email_alert_quantity_threshold_letter', $GLOBALS['TSFE']->sys_language_uid);
                                         if ($page[0]['content']) {
                                             // loading the email confirmation letter eof
                                             // replacing the variables with dynamic values
                                             $array1 = array();
                                             $array2 = array();
                                             $array1[] = '###ORDERED_QTY###';
                                             $array2[] = $value['qty'];
                                             $array1[] = '###CURRENT_PRODUCT_QUANTITY###';
                                             $array2[] = $row['products_quantity'];
                                             $array1[] = '###PRODUCT_NAME###';
                                             $array2[] = $value['products_name'];
                                             $link_edit_prod = $this->FULL_HTTP_URL . mslib_fe::typolink($this->shop_pid . ',2003', 'tx_multishop_pi1[page_section]=edit_product&pid=' . $value['products_id'] . '&cid=' . $value['categories_id'] . '&action=edit_product');
                                             $array1[] = '###DIRECT_EDIT_PRODUCT_LINK###';
                                             $array2[] = '<a href="' . $link_edit_prod . '" target="_blank">edit product stock</a>';
                                             // now mail a copy to the merchant
                                             $merchant = array();
                                             $merchant['name'] = $this->ms['MODULES']['STORE_NAME'];
                                             $merchant['email'] = $this->ms['MODULES']['STORE_EMAIL'];
                                             $mailTo = array();
                                             $mailTo[] = $merchant;
                                             //hook to let other plugins further manipulate the replacers
                                             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.tx_mslib_cart.php']['mailAlertQuantityThresholdPostProc'])) {
                                                 $params = array('array1' => &$array1, 'array2' => &$array2, 'page' => &$page, 'mailTo' => &$mailTo);
                                                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.tx_mslib_cart.php']['mailAlertQuantityThresholdPostProc'] as $funcRef) {
                                                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                                                 }
                                             }
                                             //end of hook to let other plugins further manipulate the replacers
                                             if ($page[0]['content']) {
                                                 $page[0]['content'] = str_replace($array1, $array2, $page[0]['content']);
                                             }
                                             if ($page[0]['name']) {
                                                 $page[0]['name'] = str_replace($array1, $array2, $page[0]['name']);
                                             }
                                             foreach ($mailTo as $mailuser) {
                                                 mslib_fe::mailUser($mailuser, $page[0]['name'], $page[0]['content'], $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME']);
                                             }
                                         }
                                     }
                                     if ($row['products_quantity'] < 1) {
                                         if ($this->ms['MODULES']['DISABLE_PRODUCT_WHEN_NEGATIVE_STOCK']) {
                                             if (!$this->ms['MODULES']['ALLOW_ORDER_OUT_OF_STOCK_PRODUCT']) {
                                                 // stock is negative or zero. lets turn off the product
                                                 mslib_befe::disableProduct($value['products_id']);
                                             }
                                         }
                                     }
                                     // now decrease the stocklevel eof
                                 }
                             }
                         }
                         if ($orders_products_id and is_array($value['attributes'])) {
                             foreach ($value['attributes'] as $attribute_key => $attribute_values) {
                                 $str = "SELECT products_options_name,listtype from tx_multishop_products_options o where o.products_options_id='" . $attribute_key . "' ";
                                 $qry = $GLOBALS['TYPO3_DB']->sql_query($str);
                                 $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($qry);
                                 //								print_r($row['listtype']);
                                 switch ($row['listtype']) {
                                     case 'checkbox':
                                         $items = $attribute_values;
                                         break;
                                     default:
                                         $items = array($attribute_values);
                                         break;
                                 }
                                 foreach ($items as $item) {
                                     $attributes_tax['country_tax'] = (string) $item['country_tax'];
                                     $attributes_tax['region_tax'] = (string) $item['region_tax'];
                                     $attributes_tax['tax'] = (string) $item['tax'];
                                     if ($this->ms['MODULES']['DISABLE_VAT_RATE']) {
                                         $attributes_tax['country_tax'] = 0;
                                         $attributes_tax['region_tax'] = 0;
                                         $attributes_tax['tax'] = 0;
                                     }
                                     $insertAttributes = array();
                                     $insertAttributes['orders_id'] = $orders_id;
                                     $insertAttributes['orders_products_id'] = $orders_products_id;
                                     $insertAttributes['products_options'] = $item['products_options_name'];
                                     $insertAttributes['products_options_values'] = $item['products_options_values_name'];
                                     $insertAttributes['options_values_price'] = $item['options_values_price'];
                                     $insertAttributes['price_prefix'] = $item['price_prefix'];
                                     $insertAttributes['products_options_id'] = $item['options_id'];
                                     $insertAttributes['products_options_values_id'] = $item['options_values_id'];
                                     $sub_total_excluding_vat['attributes_price'] += $item['price_prefix'] . $item['options_values_price'] * $value['qty'];
                                     $sub_total['attributes_price'] += $item['price_prefix'] . $item['options_values_price'] * $value['qty'];
                                     $insertAttributes['attributes_tax_data'] = serialize($attributes_tax);
                                     $insertAttributes = mslib_befe::rmNullValuedKeys($insertAttributes);
                                     $query = $GLOBALS['TYPO3_DB']->INSERTquery('tx_multishop_orders_products_attributes', $insertAttributes);
                                     $res = $GLOBALS['TYPO3_DB']->sql_query($query);
                                 }
                             }
                         }
                         if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrdersProductsPostProc'])) {
                             // hook
                             $params = array('ms' => $this->ms, 'orders_products_id' => $orders_products_id, 'insertArray' => $insertArray, 'insertAttributes' => $insertAttributes, 'cart' => $cart, 'cart_value' => $value);
                             foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrdersProductsPostProc'] as $funcRef) {
                                 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                             }
                             // hook eof
                         }
                     }
                 }
             }
             $updateArray = array();
             $orders_tax['sub_total_excluding_vat'] = (string) array_sum($sub_total_excluding_vat);
             $orders_tax['sub_total'] = (string) array_sum($sub_total);
             if (!$this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT'] || $this->ms['MODULES']['FORCE_CHECKOUT_SHOW_PRICES_INCLUDING_VAT']) {
                 $orders_tax['total_orders_tax'] = (string) round(array_sum($total_order_tax), 2);
             } else {
                 $orders_tax['total_orders_tax'] = (string) array_sum($total_order_tax);
             }
             $orders_tax['total_orders_tax_including_discount'] = $orders_tax['total_orders_tax'];
             $grand_total['sub_total'] = array_sum($sub_total);
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrdersTotalProc'])) {
                 // hook
                 $params = array('sub_total_excluding_vat' => &$sub_total_excluding_vat, 'sub_total' => &$sub_total, 'total_order_tax' => &$total_order_tax, 'orders_tax' => &$orders_tax, 'grand_total' => &$grand_total, 'cart' => $cart);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrdersTotalProc'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
                 // hook eof
             }
             if ($cart['discount_type']) {
                 switch ($cart['discount_type']) {
                     case 'percentage':
                         if (!$this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT'] || $this->ms['MODULES']['FORCE_CHECKOUT_SHOW_PRICES_INCLUDING_VAT']) {
                             $discount_amount = $orders_tax['sub_total_excluding_vat'] / 100 * $cart['discount'];
                         } else {
                             $discount_amount = $orders_tax['sub_total'] / 100 * $cart['discount'];
                         }
                         $discount_percentage = $cart['discount'];
                         break;
                     case 'price':
                         $discount_amount = $cart['discount'];
                         if (!$this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT'] || $this->ms['MODULES']['FORCE_CHECKOUT_SHOW_PRICES_INCLUDING_VAT']) {
                             $discount_percentage = $discount_amount / $orders_tax['sub_total_excluding_vat'] * 100;
                         } else {
                             $discount_percentage = $discount_amount / $orders_tax['sub_total'] * 100;
                         }
                         break;
                 }
                 if ($discount_amount) {
                     if (!$this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT'] || $this->ms['MODULES']['FORCE_CHECKOUT_SHOW_PRICES_INCLUDING_VAT']) {
                         $grand_total['sub_total_excluding_vat'] = $grand_total['sub_total_excluding_vat'] - $discount_amount;
                         if ($this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT']) {
                             $orders_tax['total_orders_tax_including_discount'] = round((1 - $discount_amount / $orders_tax['sub_total']) * $orders_tax['total_orders_tax_including_discount'], 2);
                         } else {
                             $orders_tax['total_orders_tax_including_discount'] = $orders_tax['total_orders_tax_including_discount'] / 100 * (100 - $discount_percentage);
                         }
                     } else {
                         $grand_total['sub_total'] = $grand_total['sub_total'] - $discount_amount;
                         if ($this->ms['MODULES']['SHOW_PRICES_INCLUDING_VAT']) {
                             $orders_tax['total_orders_tax_including_discount'] = round((1 - $discount_amount / $orders_tax['sub_total']) * $orders_tax['total_orders_tax_including_discount'], 2);
                         } else {
                             $orders_tax['total_orders_tax_including_discount'] = $orders_tax['total_orders_tax_including_discount'] / 100 * (100 - $discount_percentage);
                         }
                     }
                 }
                 $updateArray['discount'] = $discount_amount;
             }
             $orders_tax['total_orders_tax'] += $orders_tax['shipping_tax'];
             $orders_tax['total_orders_tax'] += $orders_tax['payment_tax'];
             $orders_tax['total_orders_tax_including_discount'] += $orders_tax['shipping_tax'];
             $orders_tax['total_orders_tax_including_discount'] += $orders_tax['payment_tax'];
             $orders_tax['tax_separation'] = $tax_separation;
             if ($this->ms['MODULES']['DISABLE_VAT_RATE']) {
                 $orders_tax['total_orders_tax'] = 0;
             }
             $orders_tax['grand_total'] = (string) array_sum($grand_total);
             $updateArray['orders_tax_data'] = serialize($orders_tax);
             $updateArray['grand_total'] = $orders_tax['grand_total'];
             if (!empty($cart['coupon_code'])) {
                 $updateArray['coupon_code'] = $cart['coupon_code'];
                 $updateArray['coupon_discount_type'] = $cart['discount_type'];
                 $updateArray['coupon_discount_value'] = $cart['discount'];
             }
             $updateArray['orders_last_modified'] = time();
             $query = $GLOBALS['TYPO3_DB']->UPDATEquery('tx_multishop_orders', 'orders_id=\'' . $orders_id . '\'', $updateArray);
             $res = $GLOBALS['TYPO3_DB']->sql_query($query);
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderDiscountPreProc'])) {
                 // hook
                 $params = array('ms' => $this->ms, 'orders_id' => $orders_id, 'cart' => &$cart);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderDiscountPreProc'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
                 // hook oef
             }
             if ($cart['discount']) {
                 if ($cart['discount']) {
                     if ($cart['coupon_code']) {
                         $str = "update tx_multishop_coupons set times_used=(times_used+1) where code='" . addslashes($cart['coupon_code']) . "'";
                         $res = $GLOBALS['TYPO3_DB']->sql_query($str);
                         $cart['coupon_code'] = '';
                     }
                     $cart['discount'] = '';
                     $cart['discount_type'] = '';
                 }
             }
             mslib_befe::storeCustomerCartContent($cart, $customer_id, 1);
             // debug
             /*
             $order=mslib_fe::getOrder($orders_id);
             $ORDER_DETAILS=mslib_fe::printOrderDetailsTable($order,'email');
             echo $ORDER_DETAILS;
             die();
             */
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderPostHook'])) {
                 // hook
                 $params = array('cart' => &$cart, 'orders_id' => &$orders_id);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderPostHook'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
                 // hook oef
             }
             $cart['products'] = array();
             //unset($cart['user']);
             unset($cart['discount_type']);
             unset($cart['discount_amount']);
             //$GLOBALS['TSFE']->fe_user->setKey('ses', $this->cart_page_uid, $cart);
             //$GLOBALS['TSFE']->storeSessionData();
             tx_mslib_cart::storeCart($cart);
             // unset the cart with original key
             $plain_cart_key = 'tx_multishop_cart';
             if ($this->ms['MODULES']['CART_PAGE_UID']) {
                 $plain_cart_key .= '_' . $this->ms['MODULES']['CART_PAGE_UID'];
             }
             $cart2 = $GLOBALS['TSFE']->fe_user->getKey('ses', $plain_cart_key);
             $cart2['products'] = array();
             //unset($cart2['user']);
             unset($cart2['discount_type']);
             unset($cart2['discount_amount']);
             //TODO: plain cart key?
             //$GLOBALS['TSFE']->fe_user->setKey('ses', $plain_cart_key, $cart2);
             //$GLOBALS['TSFE']->storeSessionData();
             tx_mslib_cart::storeCart($cart2);
             // custom error script for checkout
             if ($this->ms['MODULES']['ORDERS_CUSTOM_EXPORT_SCRIPT']) {
                 if (strstr($this->ms['MODULES']['ORDERS_CUSTOM_EXPORT_SCRIPT'], "..")) {
                     die('error in ORDERS_CUSTOM_EXPORT_SCRIPT value');
                 } else {
                     require $this->DOCUMENT_ROOT . $this->ms['MODULES']['ORDERS_CUSTOM_EXPORT_SCRIPT'] . '.php';
                 }
             }
             require_once \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('multishop') . 'pi1/classes/class.tx_mslib_order.php';
             $mslib_order = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_mslib_order');
             $mslib_order->init($this);
             $mslib_order->repairOrder($orders_id);
             // if grand total is zero we have to activate directly
             $order = mslib_fe::getOrder($orders_id);
             if ($order['orders_id'] and $order['grand_total'] < 0.001) {
                 mslib_fe::updateOrderStatusToPaid($order['orders_id']);
             }
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderLastPostHook'])) {
                 // hook
                 $params = array('orders_id' => &$orders_id);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/class.tx_multishop_pi1.php']['insertOrderLastPostHook'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
                 // hook oef
             }
             return $orders_id;
         }
     }
 }
Exemple #3
0
                     if ($page[0]['content']) {
                         $page[0]['content'] = str_replace($array1, $array2, $page[0]['content']);
                         $reminder_cms_content .= $page[0]['content'];
                     }
                     $full_customer_name = $tmpArray['billing_first_name'];
                     if ($order['billing_middle_name']) {
                         $full_customer_name .= ' ' . $tmpArray['billing_middle_name'];
                     }
                     if ($order['billing_last_name']) {
                         $full_customer_name .= ' ' . $tmpArray['billing_last_name'];
                     }
                     $user = array();
                     $user['name'] = $full_customer_name;
                     $user['email'] = $tmpArray['billing_email'];
                     if ($user['email']) {
                         mslib_fe::mailUser($user, $page[0]['name'], $page[0]['content'], $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME']);
                     }
                 }
             }
         }
     }
     break;
 default:
     // post processing by third party plugins
     if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/scripts/admin_pages/admin_orders.php']['adminOrdersPostHookProc'])) {
         $params = array('content' => &$content, 'postErno' => &$postErno);
         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/scripts/admin_pages/admin_orders.php']['adminOrdersPostHookProc'] as $funcRef) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
         }
     }
     break;
 function sendCreateAccountConfirmationLetter($customer_id, $password = '')
 {
     if (!is_numeric($customer_id)) {
         return false;
     }
     $page = mslib_fe::getCMScontent('email_create_account_confirmation', $GLOBALS['TSFE']->sys_language_uid);
     if ($page[0]['content']) {
         $newCustomer = mslib_fe::getUser($customer_id);
         // loading the email confirmation letter eof
         // replacing the variables with dynamic values
         $array1 = array();
         $array2 = array();
         $array1[] = '###GENDER_SALUTATION###';
         $array2[] = mslib_fe::genderSalutation($this->post['gender']);
         $array1[] = '###BILLING_COMPANY###';
         $array2[] = $newCustomer['company'];
         $array1[] = '###FULL_NAME###';
         $array2[] = $newCustomer['name'];
         $array1[] = '###BILLING_NAME###';
         $array2[] = $newCustomer['name'];
         $array1[] = '###BILLING_FIRST_NAME###';
         $array2[] = $newCustomer['first_name'];
         $array1[] = '###BILLING_LAST_NAME###';
         $last_name = $newCustomer['last_name'];
         if ($newCustomer['middle_name']) {
             $last_name = $newCustomer['middle_name'] . ' ' . $last_name;
         }
         $array2[] = $last_name;
         $array1[] = '###CUSTOMER_EMAIL###';
         $array2[] = $newCustomer['email'];
         $array1[] = '###BILLING_EMAIL###';
         $array2[] = $newCustomer['email'];
         $array1[] = '###BILLING_ADDRESS###';
         $array2[] = $newCustomer['address'];
         $array1[] = '###BILLING_TELEPHONE###';
         $array2[] = $newCustomer['telephone'];
         $array1[] = '###BILLING_MOBILE###';
         $array2[] = $newCustomer['mobile'];
         $array1[] = '###LONG_DATE###';
         // ie woensdag 23 juni, 2010
         $long_date = strftime($this->pi_getLL('full_date_format'));
         $array2[] = $long_date;
         $array1[] = '###CURRENT_DATE_LONG###';
         // ie woensdag 23 juni, 2010
         $long_date = strftime($this->pi_getLL('full_date_format'));
         $array2[] = $long_date;
         $array1[] = '###STORE_NAME###';
         $array2[] = $this->ms['MODULES']['STORE_NAME'];
         $array1[] = '###CUSTOMER_ID###';
         $array2[] = $customer_id;
         $link = $this->FULL_HTTP_URL . mslib_fe::typolink($this->shop_pid . ',2002', '&tx_multishop_pi1[page_section]=confirm_create_account&tx_multishop_pi1[hash]=' . $newCustomer['tx_multishop_code']);
         $array1[] = '###LINK###';
         $array2[] = '<a href="' . $link . '" rel="noreferrer">' . htmlspecialchars($this->pi_getLL('click_here_to_confirm_registration')) . '</a>';
         $array1[] = '###CONFIRMATION_LINK###';
         $array2[] = '<a href="' . $link . '" rel="noreferrer">' . htmlspecialchars($this->pi_getLL('click_here_to_confirm_registration')) . '</a>';
         $array1[] = '###USERNAME###';
         $array2[] = $newCustomer['email'];
         $array1[] = '###PASSWORD###';
         $array2[] = $password;
         if ($page[0]['content']) {
             $page[0]['content'] = str_replace($array1, $array2, $page[0]['content']);
         }
         if ($page[0]['name']) {
             $page[0]['name'] = str_replace($array1, $array2, $page[0]['name']);
         }
         $user = array();
         $user['name'] = $newCustomer['first_name'];
         $user['email'] = $newCustomer['email'];
         mslib_fe::mailUser($user, $page[0]['name'], $page[0]['content'], $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME']);
         return true;
     }
 }
 function mailOrder($orders_id, $copy_to_merchant = 1, $custom_email_address = '', $mail_template = '')
 {
     $order = mslib_fe::getOrder($orders_id);
     if ($order['orders_id']) {
         $order['mail_template'] = $mail_template;
         if (isset($order['language_id'])) {
             // Switch to language that is stored in the order
             mslib_befe::setSystemLanguage($order['language_id']);
         }
         if (!$custom_email_address) {
             $custom_email_address = $order['billing_email'];
         }
         $billing_address = '';
         $delivery_address = '';
         $full_customer_name = $order['billing_first_name'];
         if ($order['billing_middle_name']) {
             $full_customer_name .= ' ' . $order['billing_middle_name'];
         }
         if ($order['billing_last_name']) {
             $full_customer_name .= ' ' . $order['billing_last_name'];
         }
         $delivery_full_customer_name = $order['delivery_first_name'];
         if ($order['delivery_middle_name']) {
             $delivery_full_customer_name .= ' ' . $order['delivery_middle_name'];
         }
         if ($order['delivery_last_name']) {
             $delivery_full_customer_name .= ' ' . $order['delivery_last_name'];
         }
         $full_customer_name = preg_replace('/\\s+/', ' ', $full_customer_name);
         $delivery_full_customer_name = preg_replace('/\\s+/', ' ', $delivery_full_customer_name);
         if (!$order['delivery_address'] or !$order['delivery_city']) {
             $order['delivery_company'] = $order['billing_company'];
             $order['delivery_street_name'] = $order['billing_street_name'];
             $order['delivery_building'] = $order['billing_building'];
             $order['delivery_address'] = $order['billing_address'];
             $order['delivery_address_number'] = $order['billing_address_number'];
             $order['delivery_address_ext'] = $order['billing_address_ext'];
             $order['delivery_zip'] = $order['billing_zip'];
             $order['delivery_city'] = $order['billing_city'];
             $order['delivery_telephone'] = $order['billing_telephone'];
             $order['delivery_mobile'] = $order['billing_mobile'];
         }
         if ($order['delivery_company']) {
             $delivery_address = $order['delivery_company'] . "<br />";
         }
         if ($delivery_full_customer_name) {
             $delivery_address .= $delivery_full_customer_name . "<br />";
         }
         if ($order['delivery_building']) {
             $delivery_address .= $order['delivery_building'] . "<br />";
         }
         if ($order['delivery_address']) {
             $delivery_address .= $order['delivery_address'] . "<br />";
         }
         if ($order['delivery_zip'] and $order['delivery_city']) {
             $delivery_address .= $order['delivery_zip'] . " " . $order['delivery_city'];
         }
         if ($order['delivery_country'] && mslib_befe::strtolower($order['delivery_country']) != mslib_befe::strtolower($this->tta_shop_info['country'])) {
             // ONLY PRINT COUNTRY IF THE COUNTRY OF THE CUSTOMER IS DIFFERENT THAN FROM THE SHOP
             $delivery_address .= '<br />' . mslib_fe::getTranslatedCountryNameByEnglishName($this->lang, $order['delivery_country']);
         }
         //		if ($order['delivery_telephone']) 		$delivery_address.=ucfirst($this->pi_getLL('telephone')).': '.$order['delivery_telephone']."<br />";
         //		if ($order['delivery_mobile']) 			$delivery_address.=ucfirst($this->pi_getLL('mobile')).': '.$order['delivery_mobile']."<br />";
         if ($order['billing_company']) {
             $billing_address = $order['billing_company'] . "<br />";
         }
         if ($full_customer_name) {
             $billing_address .= $full_customer_name . "<br />";
         }
         if ($order['billing_building']) {
             $billing_address .= $order['billing_building'] . "<br />";
         }
         if ($order['billing_address']) {
             $billing_address .= $order['billing_address'] . "<br />";
         }
         if ($order['billing_zip'] and $order['billing_city']) {
             $billing_address .= $order['billing_zip'] . " " . $order['billing_city'];
         }
         if ($order['billing_country'] && mslib_befe::strtolower($order['billing_country']) != mslib_befe::strtolower($this->tta_shop_info['country'])) {
             // ONLY PRINT COUNTRY IF THE COUNTRY OF THE CUSTOMER IS DIFFERENT THAN FROM THE SHOP
             $billing_address .= '<br />' . mslib_fe::getTranslatedCountryNameByEnglishName($this->lang, $order['billing_country']);
         }
         $loadFromPids = array();
         if ($this->conf['masterShop']) {
             $loadFromPids[] = $order['page_uid'];
             $loadFromPids[] = $this->shop_pid;
             if ($this->showCatalogFromPage and $this->showCatalogFromPage != $this->shop_pid) {
                 $loadFromPids[] = $this->showCatalogFromPage;
             }
         }
         // psp email template
         $psp_mail_template = array();
         if ($order['payment_method']) {
             $psp_data = mslib_fe::loadPaymentMethod($order['payment_method']);
             $psp_vars = unserialize($psp_data['vars']);
             if (isset($psp_vars['order_confirmation'])) {
                 $psp_mail_template['order_confirmation'] = '';
                 if ($psp_vars['order_confirmation'] > 0) {
                     $psp_mail_template['order_confirmation'] = mslib_fe::getCMSType($psp_vars['order_confirmation']);
                 }
             }
             if (isset($psp_vars['order_paid'])) {
                 $psp_mail_template['order_paid'] = '';
                 if ($psp_vars['order_paid'] > 0) {
                     $psp_mail_template['order_paid'] = mslib_fe::getCMSType($psp_vars['order_paid']);
                 }
             }
         }
         // loading the email template
         $page = array();
         //hook to let other plugins further manipulate the replacers
         if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrderPreCMSContent'])) {
             $params = array('page' => &$page, 'order' => &$order, 'mail_template' => $mail_template, 'psp_mail_template' => $psp_mail_template, 'loadFromPids' => $loadFromPids);
             foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrderPreCMSContent'] as $funcRef) {
                 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
             }
         }
         if ($mail_template) {
             switch ($mail_template) {
                 case 'email_order_paid_letter':
                     if (isset($psp_mail_template['order_paid'])) {
                         $page = array();
                         if (!empty($psp_mail_template['order_paid'])) {
                             $page = mslib_fe::getCMScontent($psp_mail_template['order_paid'], $GLOBALS['TSFE']->sys_language_uid, $loadFromPids);
                         }
                     } else {
                         if ($order['payment_method']) {
                             $page = mslib_fe::getCMScontent('email_order_paid_letter_' . $order['payment_method'], $GLOBALS['TSFE']->sys_language_uid, $loadFromPids);
                         }
                         if (!count($page[0])) {
                             $page = mslib_fe::getCMScontent('email_order_paid_letter', $GLOBALS['TSFE']->sys_language_uid, $loadFromPids);
                         }
                     }
                     break;
                 default:
                     $page = mslib_fe::getCMScontent($mail_template, $GLOBALS['TSFE']->sys_language_uid, $loadFromPids);
                     break;
             }
         } else {
             if ($order['is_proposal']) {
                 // proposal template
                 $mail_template = 'email_order_proposal';
                 $page = mslib_fe::getCMScontent($mail_template, $GLOBALS['TSFE']->sys_language_uid, $loadFromPids);
             } else {
                 // normal order template
                 if (isset($psp_mail_template['order_confirmation'])) {
                     $page = array();
                     if (!empty($psp_mail_template['order_confirmation'])) {
                         $page = mslib_fe::getCMScontent($psp_mail_template['order_confirmation'], $GLOBALS['TSFE']->sys_language_uid, $loadFromPids);
                     }
                 } else {
                     if ($order['payment_method']) {
                         $page = mslib_fe::getCMScontent('email_order_confirmation_' . $order['payment_method'], $GLOBALS['TSFE']->sys_language_uid, $loadFromPids);
                     }
                     if (!count($page[0])) {
                         $page = mslib_fe::getCMScontent('email_order_confirmation', $GLOBALS['TSFE']->sys_language_uid, $loadFromPids);
                     }
                 }
             }
         }
         //hook to let other plugins further manipulate the replacers
         if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrderPostCMSContent'])) {
             $params = array('page' => &$page, 'order' => &$order, 'mail_template' => $mail_template, 'psp_mail_template' => $psp_mail_template, 'loadFromPids' => $loadFromPids);
             foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrderPostCMSContent'] as $funcRef) {
                 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
             }
         }
         if ($page[0]['content']) {
             // loading the email confirmation letter eof
             // replacing the variables with dynamic values
             $array1 = array();
             $array2 = array();
             $array1[] = '###GENDER_SALUTATION###';
             $array2[] = mslib_fe::genderSalutation($order['billing_gender']);
             // full billing name
             $array1[] = '###BILLING_FULL_NAME###';
             $array2[] = $full_customer_name;
             $array1[] = '###FULL_NAME###';
             $array2[] = $full_customer_name;
             $array1[] = '###BILLING_NAME###';
             $array2[] = $order['billing_name'];
             $array1[] = '###BILLING_COMPANY###';
             $array2[] = $order['billing_company'];
             $array1[] = '###BILLING_FIRST_NAME###';
             $array2[] = $order['billing_first_name'];
             $array1[] = '###BILLING_LAST_NAME###';
             $array2[] = preg_replace('/\\s+/', ' ', $order['billing_middle_name'] . ' ' . $order['billing_last_name']);
             $array1[] = '###BILLING_EMAIL###';
             $array2[] = $order['billing_email'];
             $array1[] = '###BILLING_TELEPHONE###';
             $array2[] = $order['billing_telephone'];
             $array1[] = '###BILLING_MOBILE###';
             $array2[] = $order['billing_mobile'];
             // full delivery name
             $array1[] = '###DELIVERY_NAME###';
             $array2[] = $order['delivery_name'];
             $array1[] = '###DELIVERY_FULL_NAME###';
             $array2[] = $delivery_full_customer_name;
             $array1[] = '###DELIVERY_COMPANY###';
             $array2[] = $order['delivery_company'];
             $array1[] = '###DELIVERY_FIRST_NAME###';
             $array2[] = $order['delivery_first_name'];
             $array1[] = '###DELIVERY_LAST_NAME###';
             $array2[] = preg_replace('/\\s+/', ' ', $order['delivery_middle_name'] . ' ' . $order['delivery_last_name']);
             $array1[] = '###DELIVERY_EMAIL###';
             $array2[] = $order['delivery_email'];
             $array1[] = '###DELIVERY_TELEPHONE###';
             $array2[] = $order['delivery_telephone'];
             $array1[] = '###DELIVERY_MOBILE###';
             $array2[] = $order['delivery_mobile'];
             $array1[] = '###CUSTOMER_EMAIL###';
             $array2[] = $order['billing_email'];
             if ($order['cruser_id'] && is_numeric($order['cruser_id'])) {
                 $crUser = mslib_fe::getUser($order['cruser_id']);
                 $array1[] = '###CREATED_BY_FIRST_NAME###';
                 $array2[] = preg_replace('/\\s+/', ' ', $crUser['first_name']);
                 $array1[] = '###CREATED_BY_LAST_NAME###';
                 $array2[] = preg_replace('/\\s+/', ' ', $crUser['middle_name'] . ' ' . $crUser['last_name']);
                 $array1[] = '###CREATED_BY_FULL_NAME###';
                 $array2[] = preg_replace('/\\s+/', ' ', $crUser['first_name'] . ' ' . $crUser['middle_name'] . ' ' . $crUser['last_name']);
             } else {
                 $array1[] = '###CREATED_BY_FIRST_NAME###';
                 $array2[] = '';
                 $array1[] = '###CREATED_BY_LAST_NAME###';
                 $array2[] = '';
                 $array1[] = '###CREATED_BY_FULL_NAME###';
                 $array2[] = '';
             }
             $time = $order['crdate'];
             $long_date = strftime($this->pi_getLL('full_date_format'), $time);
             $array1[] = '###ORDER_DATE_LONG###';
             // ie woensdag 23 juni, 2010
             $array2[] = $long_date;
             $array1[] = '###ORDER_DATE###';
             // 21-12-2010 in localized format
             $array2[] = strftime("%x", $time);
             // backwards compatibility
             $array1[] = '###LONG_DATE###';
             // ie woensdag 23 juni, 2010
             $array2[] = $long_date;
             $time = time();
             $long_date = strftime($this->pi_getLL('full_date_format'), $time);
             $array1[] = '###CURRENT_DATE_LONG###';
             // ie woensdag 23 juni, 2010
             $array2[] = $long_date;
             $array1[] = '###STORE_NAME###';
             $array2[] = $this->ms['MODULES']['STORE_NAME'];
             $array1[] = '###TOTAL_AMOUNT###';
             $array2[] = mslib_fe::amount2Cents($order['total_amount']);
             $array1[] = '###PROPOSAL_NUMBER###';
             $array2[] = $order['orders_id'];
             $array1[] = '###ORDER_NUMBER###';
             $array2[] = $order['orders_id'];
             $array1[] = '###ORDER_LINK###';
             $array2[] = '';
             $array1[] = '###ORDER_STATUS###';
             $array2[] = $order['orders_status'];
             $array1[] = '###TRACK_AND_TRACE_CODE###';
             $array2[] = $order['track_and_trace_code'];
             $array1[] = '###BILLING_ADDRESS###';
             $array2[] = $billing_address;
             $array1[] = '###DELIVERY_ADDRESS###';
             $array2[] = $delivery_address;
             $array1[] = '###BILLING_STREET_NAME###';
             $array2[] = $order['billing_street_name'];
             $array1[] = '###BILLING_ADDRESS_NUMBER###';
             $array2[] = $order['billing_address_number'];
             $array1[] = '###BILLING_ADDRESS_EXT###';
             $array2[] = $order['billing_address_ext'];
             $array1[] = '###BILLING_ZIP###';
             $array2[] = $order['billing_zip'];
             $array1[] = '###BILLING_CITY###';
             $array2[] = $order['billing_city'];
             $array1[] = '###BILLING_COUNTRY###';
             $array2[] = mslib_fe::getTranslatedCountryNameByEnglishName($this->lang, $order['billing_country']);
             $array1[] = '###DELIVERY_STREET_NAME###';
             $array2[] = $order['delivery_street_name'];
             $array1[] = '###DELIVERY_ADDRESS_NUMBER###';
             $array2[] = $order['delivery_address_number'];
             $array1[] = '###DELIVERY_ADDRESS_EXT###';
             $array2[] = $order['delivery_address_ext'];
             $array1[] = '###DELIVERY_ZIP###';
             $array2[] = $order['delivery_zip'];
             $array1[] = '###DELIVERY_CITY###';
             $array2[] = $order['delivery_city'];
             $array1[] = '###DELIVERY_COUNTRY###';
             $array2[] = mslib_fe::getTranslatedCountryNameByEnglishName($this->lang, $order['delivery_country']);
             $array1[] = '###CUSTOMER_ID###';
             $array2[] = $order['customer_id'];
             $ORDER_DETAILS = self::printOrderDetailsTable($order, 'email');
             if ($this->ms['MODULES']['CREATE_INVOICE_DIRECTLY_AFTER_CREATING_ORDER']) {
                 // FORCE CREATE INVOICE IF NOT ALREADY EXISTING
                 $invoice = mslib_fe::getOrderInvoice($order['orders_id'], 1);
             } else {
                 $invoice = mslib_fe::getOrderInvoice($order['orders_id'], 0);
             }
             $invoice_id = '';
             $invoice_link = '';
             if (is_array($invoice)) {
                 $invoice_id = $invoice['invoice_id'];
                 $invoice_link = '<a href="' . $this->FULL_HTTP_URL . mslib_fe::typolink($this->shop_pid . ',2002', '&tx_multishop_pi1[page_section]=download_invoice&tx_multishop_pi1[hash]=' . $invoice['hash']) . '">' . $invoice['invoice_id'] . '</a>';
             }
             $array1[] = '###INVOICE_NUMBER###';
             $array2[] = $invoice_id;
             $array1[] = '###INVOICE_LINK###';
             $array2[] = $invoice_link;
             $array1[] = '###ORDER_DETAILS###';
             $array2[] = $ORDER_DETAILS;
             $array1[] = '###SHIPPING_METHOD###';
             $array2[] = $order['shipping_method_label'];
             $array1[] = '###PAYMENT_METHOD###';
             $array2[] = $order['payment_method_label'];
             $array1[] = '###EXPECTED_DELIVERY_DATE###';
             if ($order['expected_delivery_date'] > 0) {
                 $array2[] = strftime("%x", $order['expected_delivery_date']);
             } else {
                 $array2[] = '';
             }
             $array1[] = '###EXPECTED_DELIVERY_DATE_LONG###';
             if ($order['expected_delivery_date'] > 0) {
                 $array2[] = strftime($this->pi_getLL('full_date_format'), $order['expected_delivery_date']);
             } else {
                 $array2[] = '';
             }
             $array1[] = '###CUSTOMER_COMMENTS###';
             $array2[] = $order['customer_comments'];
             if ($order['customer_id'] > 0) {
                 $user = mslib_fe::getUser($order['customer_id']);
                 $array1[] = '###USERNAME###';
                 $array2[] = $user['username'];
                 if (isset($this->post['password']) && !empty($this->post['password']) && $order['customer_id'] > 0) {
                     $array1[] = '###PASSWORD###';
                     $array2[] = $this->post['password'];
                 }
             }
             //hook to let other plugins further manipulate the replacers
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrderReplacersPostProc'])) {
                 $params = array('array1' => &$array1, 'array2' => &$array2, 'order' => &$order, 'mail_template' => $mail_template, 'loadFromPids' => $loadFromPids);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrderReplacersPostProc'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
             }
             // Copy
             $pageCopyToMerchant = $page;
             if ($page[0]['content']) {
                 $page[0]['content'] = str_replace($array1, $array2, $page[0]['content']);
             }
             if ($page[0]['name']) {
                 $page[0]['name'] = str_replace($array1, $array2, $page[0]['name']);
             }
             // replacing the variables with dynamic values eof
             $user = array();
             $user['name'] = $full_customer_name;
             $user['email'] = $custom_email_address;
             //hook
             $send_mail = 1;
             $mail_attachment = array();
             $add_invoice_attachment_on_templates = array();
             $add_invoice_attachment_on_templates[] = 'email_order_paid_letter';
             if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrder'])) {
                 $params = array('this' => &$this, 'page' => &$page, 'content' => &$content, 'send_mail' => &$send_mail, 'user' => $user, 'order' => $order, 'order_details' => $ORDER_DETAILS, 'copy_to_merchant' => $copy_to_merchant, 'mail_attachment' => &$mail_attachment, 'loadFromPids' => $loadFromPids, 'add_invoice_attachment_on_templates' => &$add_invoice_attachment_on_templates);
                 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrder'] as $funcRef) {
                     \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $params, $this);
                 }
             }
             if ($send_mail) {
                 if ($user['email']) {
                     if (in_array($mail_template, $add_invoice_attachment_on_templates) && $this->ms['MODULES']['ATTACH_INVOICE_PDF_IN_PAID_LETTER_EMAIL'] > 0) {
                         $filterInvoice = array();
                         $filterInvoice[] = 'orders_id=' . $order['orders_id'];
                         $invoices = mslib_befe::getRecords('', 'tx_multishop_invoices', '', $filterInvoice, '', 'id desc');
                         $invoice = $invoices[0];
                         $pdfFileName = $invoice['invoice_id'] . '_' . $invoice['orders_id'] . '.pdf';
                         $pdfFilePath = $this->DOCUMENT_ROOT . 'uploads/tx_multishop/tmp/' . $pdfFileName;
                         // generate the invoice PDF
                         // Get Language code (ie nl, en, de)
                         $language_code = mslib_befe::getLanguageIso2ByLanguageUid($order['language_id']);
                         $language_code = strtolower($language_code);
                         // Download invoice in the language of the order
                         $invoice_data = mslib_fe::file_get_contents($this->FULL_HTTP_URL . mslib_fe::typolink($this->shop_pid . ',2002', 'tx_multishop_pi1[page_section]=download_invoice&tx_multishop_pi1[hash]=' . $invoice['hash'] . '&tx_multishop_pi1[forceRecreate]=1&language=' . $language_code));
                         file_put_contents($pdfFilePath, $invoice_data);
                         $mail_attachment[] = $pdfFilePath;
                     }
                     mslib_fe::mailUser($user, $page[0]['name'], $page[0]['content'], $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME'], $mail_attachment);
                     // moved to cleaning up section
                     //if (strpos($mail_template, 'email_order_paid_letter')!==false && $this->ms['MODULES']['ATTACH_INVOICE_PDF_IN_PAID_LETTER_EMAIL']>0 && file_exists($pdfFilePath)) {
                     //	unlink($pdfFilePath);
                     //}
                 }
                 if ($copy_to_merchant) {
                     $mailSubject = '';
                     if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrderToMerchant'])) {
                         $conf = array('this' => &$this, 'page' => &$page, 'content' => &$content, 'send_mail' => &$send_mail, 'user' => $user, 'order' => $order, 'order_details' => $ORDER_DETAILS, 'mail_attachment' => &$mail_attachment, 'mail_template' => $mail_template, 'psp_mail_template' => $psp_mail_template, 'loadFromPids' => $loadFromPids, 'mailSubject' => &$mailSubject, 'pageCopyToMerchant' => &$pageCopyToMerchant, 'array1' => &$array1, 'array2' => &$array2);
                         foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/multishop/pi1/classes/class.mslib_fe.php']['mailOrderToMerchant'] as $funcRef) {
                             \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($funcRef, $conf, $this);
                         }
                     }
                     if ($pageCopyToMerchant[0]['content']) {
                         $pageCopyToMerchant[0]['content'] = str_replace($array1, $array2, $pageCopyToMerchant[0]['content']);
                     }
                     if ($pageCopyToMerchant[0]['name']) {
                         $pageCopyToMerchant[0]['name'] = str_replace($array1, $array2, $pageCopyToMerchant[0]['name']);
                     }
                     if ($mailSubject == '') {
                         $mailSubject = $this->pi_getLL('copy_for_merchant') . ': ' . $pageCopyToMerchant[0]['name'];
                     }
                     // now mail a copy to the merchant
                     $merchant = array();
                     $merchant['name'] = $this->ms['MODULES']['STORE_NAME'];
                     $merchant['email'] = $this->ms['MODULES']['STORE_EMAIL'];
                     mslib_fe::mailUser($merchant, $mailSubject, $pageCopyToMerchant[0]['content'], $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME'], $mail_attachment);
                     if ($this->ms['MODULES']['SEND_ORDER_CONFIRMATION_LETTER_ALSO_TO']) {
                         $email = array();
                         if (!strstr($this->ms['MODULES']['SEND_ORDER_CONFIRMATION_LETTER_ALSO_TO'], ",")) {
                             $email[] = $this->ms['MODULES']['SEND_ORDER_CONFIRMATION_LETTER_ALSO_TO'];
                         } else {
                             $email = explode(',', $this->ms['MODULES']['SEND_ORDER_CONFIRMATION_LETTER_ALSO_TO']);
                         }
                         if (count($email)) {
                             foreach ($email as $item) {
                                 $merchant = array();
                                 $merchant['name'] = $this->ms['MODULES']['STORE_NAME'];
                                 $merchant['email'] = $item;
                                 mslib_fe::mailUser($merchant, $mailSubject, $pageCopyToMerchant[0]['content'], $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME'], $mail_attachment);
                             }
                         }
                     }
                 }
                 // cleaning up the temporary attachment files
                 if (is_array($mail_attachment) && count($mail_attachment)) {
                     foreach ($mail_attachment as $filepath) {
                         if (strpos($filepath, 'tx_multishop/tmp') !== false) {
                             if (file_exists($filepath)) {
                                 @unlink($filepath);
                             }
                         }
                     }
                 }
             }
         }
         if (isset($order['language_id'])) {
             // Switch back to default language
             mslib_befe::resetSystemLanguage();
         }
         return 1;
     }
 }
             if (file_exists($combinedPdfFile)) {
                 header("Content-type:application/pdf");
                 readfile($combinedPdfFile);
                 // delete temporary invoice from disk
                 unlink($combinedPdfFile);
                 foreach ($attachments as $attachment) {
                     unlink($attachment);
                 }
                 exit;
             }
             break;
         case 'mail_selected_invoices_to_merchant':
             $user = array();
             $user['name'] = $this->ms['MODULES']['STORE_NAME'];
             $user['email'] = $this->ms['MODULES']['STORE_EMAIL'];
             if (mslib_fe::mailUser($user, $this->ms['MODULES']['STORE_NAME'] . ' invoices', $this->ms['MODULES']['STORE_NAME'] . ' invoices', $this->ms['MODULES']['STORE_EMAIL'], $this->ms['MODULES']['STORE_NAME'], array($combinedPdfFile))) {
                 $postErno[] = array('status' => 'info', 'message' => 'The following invoices are mailed to ' . $user['email'] . ':<ul><li>' . implode('</li><li>', array_keys($attachments)) . '</li></ul>');
             } else {
                 $postErno[] = array('status' => 'error', 'message' => 'Failed to mail invoices to: ' . $user['email']);
             }
             break;
     }
     // delete temporary invoice from disk
     unlink($combinedPdfFile);
     foreach ($attachments as $attachment) {
         unlink($attachment);
     }
 } else {
     echo 'gs binary cannot be found. This is needed for merging multiple PDF files as one file.';
     exit;
 }