public function manageAction($id = false)
 {
     if ($id) {
         $this->permission('edit');
     } else {
         $this->permission('add');
     }
     $commerce_orders = new \modules\commerce\models\Commerce_orders();
     if ($_POST) {
         $commerce_orders->attributes = ['subtotal' => $this->input->post('subtotal'), 'total' => $this->input->post('total'), 'commerce_payment_method_id' => $this->input->post('commerce_payment_method_id'), 'user_id' => $this->input->post('user_id'), 'billing_address' => $this->input->post('billing_address'), 'shipping_address' => $this->input->post('shipping_address')];
     }
     if ($id) {
         $commerce_orders->commerce_order_id = $id;
     }
     if (!$id) {
         $commerce_orders->created = date("Y-m-d H:i:s");
     }
     $users = Form_helper::queryToDropdown('users', 'user_id', 'fullname');
     $paymet_method = Form_helper::queryToDropdown('commerce_payment_method', 'commerce_payment_method_id', 'name');
     if ($commerce_orders->save()) {
         Uri_helper::redirect("management/commerce_orders");
     } else {
         return $this->render('commerce_orders/manage', ['item' => $commerce_orders->get(), 'users' => $users, 'payment_method' => $paymet_method]);
     }
 }
 public function indexAction()
 {
     $order = new \modules\commerce\models\Commerce_orders('order');
     $order_details = new \modules\commerce\models\Commerce_order_details(false);
     $product = new \modules\commerce\models\Commerce_products(false);
     $invoice = new \modules\commerce\models\Commerce_invoices(false);
     $payment_method = Form_helper::queryToDropdown('commerce_payment_method', 'commerce_payment_method_id', 'name', false);
     $addresses = Form_helper::queryToDropdown('user_addresses', 'user_address_id', 'address', false, "WHERE user_id = " . $this->user->user_id);
     $order->set('subtotal', $this->cart->total());
     $order->set('total', $this->cart->total());
     $order->set('commerce_payment_method_id', $this->input->post('commerce_payment_method_id'));
     $order->set('user_id', $this->user->user_id);
     $order->set('billing_address', $addresses[$this->input->post('billing_address')]);
     $order->set('shipping_address', $addresses[$this->input->post('shipping_address')]);
     $order->set('status', $this->input->post('commerce_payment_method_id') == 2 ? 'confirmed' : 'pending');
     if ($order_id = $order->save()) {
         foreach ($this->cart->get() as $item) {
             $order_details->commerce_order_id = $order_id;
             $order_details->product_total = $product->discount($item->price, $item->discount) * $item->qty;
             $order_details->price = $product->discount($item->price, $item->discount);
             $order_details->commerce_product_id = $item->commerce_product_id;
             $order_details->weight = $item->weight;
             $order_details->qty = $item->qty;
             $order_details->save();
         }
         // INVOICE
         $invoice->commerce_order_id = $order_id;
         $invoice->status = 'pending';
         $invoice->save();
         if ($this->input->post('commerce_payment_method_id') == '1') {
             $this->layout = 'ajax';
             return $this->render('payments/paypal', ['total' => $this->cart->total()]);
         }
         // THANK YOU FOR YOUR ORDER
         return $this->render('commerce_orders/commerce_order_confirmed');
     } else {
         return $this->render('commerce_orders/commerce_make_order', ['user' => $this->user, 'payment' => $payment_method, 'addresses' => $addresses, 'cart' => $this->cart]);
     }
 }