static function createOrder($basket, $payment_method)
 {
     //create a new order and set a temp value
     $order = ORM::factory("bp_order");
     $order->text = t("processing");
     //cost calculation
     $product_cost = $basket->product_cost();
     $postage = $basket->postage_cost();
     $pickup = $basket->pickup;
     if ($pickup) {
         //no postage cost
         $total_cost = $product_cost;
     } else {
         $total_cost = $product_cost + $postage;
     }
     $delivery_method = basket_plus::getDeliveryMethod($pickup, $postage);
     //create order and customer
     $customer = basket_plus::createCustomer($basket);
     $full_name = basket_plus::createFullName($basket);
     //fill the order record and save it
     $order->name = $full_name;
     $order->email = $basket->email;
     $order->cost = $total_cost;
     $order->payment_method = $payment_method;
     $order->delivery_method = $delivery_method;
     $order->customerid = $customer->id;
     $order->save();
     //----------------------------------------------------------------
     // Create  the order confirmation e-mail text to the customer
     if ($basket->paypal) {
         $email_template_name = "order_paypal";
     } else {
         $email_template_name = "order";
     }
     // get order Email template
     $email_template = ORM::factory("bp_email_template")->where("name", "=", $email_template_name)->find();
     $body = $email_template->email_html;
     $order_number = basket_plus::getBasketVar(ORDER_PREFIX) . $order->id;
     $order_datetime = date(basket_plus::getBasketVar(DATE_TIME_FORMAT));
     //-------------
     // The mail has many parts, each containing variables that need to be replaced.
     // Replaced variables may contain other variables, so the replacement takes more than one step
     //-------------
     /** 
     	* first replace: all variables in the order Email template 
     %meta_tag: SYSTEM VAR: <meta> tag; workaround for Kohana limitations
     %email_template_style: VAR: CSS style definitions for e-mails
     %order_email_title: fixed email title
     %order_email_header: fixed email header
     %order_email_logo: VAR: path to the logo in the email
     %order_email_footer: VAR: optional text in the email footer
     %order_thankyou: VAR: default 'thank you' message
     %webshop_details: VAR: webshop name, adddress and contact
     %order_datetime: calc order date/time in user chosen format
     %customer_details: VAR: the customer name and address
     %payment_details: VAR: the payment details
     %cost_details: function: all amounts leading to total amount
     %delivery_method: function: how the order is delivered
     %order_lines: function: the actual order
     */
     $body = basket_plus::replaceStrings($body, array(META_TAG => basket_plus::getBasketVar(META_TAG), EMAIL_TEMPLATE_STYLE => basket_plus::getBasketVar(EMAIL_TEMPLATE_STYLE), "order_email_title" => t("Order confirmation"), "order_email_header" => t("Order confirmation"), ORDER_EMAIL_LOGO => basket_plus::getBasketVar(ORDER_EMAIL_LOGO), ORDER_THANKYOU => basket_plus::getBasketVar(ORDER_THANKYOU), ORDER_EMAIL_FOOTER => basket_plus::getBasketVar(ORDER_EMAIL_FOOTER), WEBSHOP_DETAILS => basket_plus::getBasketVar(WEBSHOP_DETAILS), "order_datetime" => $order_datetime, CUSTOMER_DETAILS => basket_plus::getBasketVar(CUSTOMER_DETAILS), PAYMENT_DETAILS => basket_plus::getBasketVar(PAYMENT_DETAILS), ORDER_DETAILS => basket_plus::getBasketVar(ORDER_DETAILS), "cost_details" => basket_plus::getCostDetailsHtml($postage, $product_cost, $pickup), "delivery_method" => basket_plus::getDeliveryMethodHtml($delivery_method, false), "order_lines" => basket_plus::getOrderLinesHtml($basket)));
     /** 
     * second replace: all remaining variables in the resulting body variable 
     		%email_order: VAR
     		%email_from: VAR
     		%email_contact: VAR
     		%order_number: calc: prefix+id
     		%total_cost: from order: total order amount incl delivery
     		%order_bank_account: VAR
     		%order_bank_account_owner: VAR
     		%order_email_closing: VAR: email closing message
     */
     $body = basket_plus::replaceStrings($body, array(EMAIL_ORDER => basket_plus::getBasketVar(EMAIL_ORDER), EMAIL_FROM => basket_plus::getBasketVar(EMAIL_FROM), EMAIL_CONTACT => basket_plus::getBasketVar(EMAIL_CONTACT), "order_number" => $order_number, "total_cost" => basket_plus::formatMoneyForWeb($total_cost), ORDER_BANK_ACCOUNT_OWNER => basket_plus::getBasketVar(ORDER_BANK_ACCOUNT_OWNER), ORDER_BANK_ACCOUNT => basket_plus::getBasketVar(ORDER_BANK_ACCOUNT), ORDER_EMAIL_CLOSING => basket_plus::getBasketVar(ORDER_EMAIL_CLOSING), "GT" => "<", "full_name" => $full_name, "address" => basket_plus::getAddressHtml($basket), "phone" => $basket->phone, "email" => $basket->email, "comments" => basket_plus::getOrderCommentsHtml($basket)));
     /** third replace: all remaining variables in the resulting body variable 
      * %website: VAR: website URL
      * %webshop_XX: VARS with webshop info
      */
     $body = basket_plus::replaceStrings($body, array(WEBSITE => basket_plus::getBasketVar(WEBSITE), WEBSHOP_ADDRESS => basket_plus::getBasketVar(WEBSHOP_ADDRESS), WEBSHOP_POSTALCODE => basket_plus::getBasketVar(WEBSHOP_POSTALCODE), WEBSHOP_CITY => basket_plus::getBasketVar(WEBSHOP_CITY), WEBSHOP_PHONE => basket_plus::getBasketVar(WEBSHOP_PHONE), WEBSHOP_OWNER => basket_plus::getBasketVar(WEBSHOP_OWNER), WEBSHOP => basket_plus::getBasketVar(WEBSHOP)));
     //convert to valid html and save in the order record
     $order->text = htmlspecialchars_decode($body);
     $order->save();
     //----------------------------------------------------------------
     // Create the internal order e-mail
     $email_template = ORM::factory("bp_email_template")->where("name", "=", $email_template_name)->find();
     $body = $email_template->email_text;
     $title = t("Internal Order confirmation");
     //	first replace: all variables in the order Email template
     $body = basket_plus::replaceStrings($body, array("order_number" => $order_number, "total_cost" => basket_plus::formatMoneyForWeb($total_cost), "order_datetime" => $order_datetime, CUSTOMER_DETAILS => basket_plus::html2txt(basket_plus::getBasketVar(CUSTOMER_DETAILS)), "delivery_method" => basket_plus::html2txt(basket_plus::getDeliveryMethodHtml($delivery_method, false)), "order_lines" => basket_plus::getOrderDetailsText($basket)));
     //	second replace: all remaining variables in the resulting body variable
     $body = basket_plus::replaceStrings($body, array(EMAIL_ORDER => basket_plus::getBasketVar(EMAIL_ORDER), EMAIL_FROM => basket_plus::getBasketVar(EMAIL_FROM), "order_email_title" => $title, "order_email_header" => $title, "full_name" => $full_name, "address" => basket_plus::html2txt(basket_plus::getAddressHtml($basket)), "phone" => $basket->phone, "email" => $basket->email, "comments" => basket_plus::html2txt(str_replace(array("<br/>", "<br>"), "\n", basket_plus::getOrderCommentsHtml($basket)))));
     $body = str_replace("nbsp;", " ", $body);
     //put the internal mail text and order status in the order
     $order->internal_text = $body;
     $order->status = Bp_Order_Model::WAITING_PAYMENT;
     //save the order record
     $order->save();
     return $order;
 }