/** * [new_order description] * @param Model_User $user [description] * @param Model_Product $product [description] * @param boolean check_match_product, if set to false will update the order with the product if different * @return [type] [description] */ public static function new_order(Model_User $user, Model_Product $product, $match_product = TRUE) { $order = new Model_Order(); if ($user->loaded() and $product->loaded()) { //get if theres an unpaid order for this user we wwill use it.. $order->where('id_user', '=', $user->id_user)->where('status', '=', Model_Order::STATUS_CREATED); //also check that matches the product for the order if ($match_product === TRUE) { $order->where('id_product', '=', $product->id_product)->where('amount', '=', $product->final_price())->where('currency', '=', $product->currency); } $order->limit(1)->find(); //order didnt exist so lets create it. if ($order->loaded() === FALSE) { //create order $order = new Model_Order(); $order->id_user = $user->id_user; } // no matter what happens if product is different save! this will also save the order if its new ;) if ($order->id_product != $product->id_product) { $order->ip_address = ip2long(Request::$client_ip); $order->id_product = $product->id_product; $order->currency = $product->currency; //add coupon ID and discount if (Model_Coupon::current()->loaded()) { $order->id_coupon = Model_Coupon::current()->id_coupon; } $order->amount = $product->final_price(); $order->VAT = euvat::vat_percentage(); $order->VAT_number = $user->VAT_number; $order->country = $user->country; $order->city = $user->city; $order->postal_code = $user->postal_code; $order->address = $user->address; try { $order->save(); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } } } return $order; }