public function updateTotalOrder($orderProductID = 0)
 {
     try {
         if (0 !== $orderProductID) {
             $OrderModel = new Order();
             $OrderProductModel = new OrderProduct();
             $orderProduct = $OrderProductModel->read(null, $orderProductID);
             if (!empty($orderProduct)) {
                 $order = $OrderModel->read(null, $orderProduct["OrderProduct"]["order_id"]);
                 $allProducts = $orderProducts = $OrderProductModel->find('all', array('conditions' => array('OrderProduct.id >=' => 1, 'OrderProduct.order_id' => $orderProduct["OrderProduct"]["order_id"], 'OrderProduct.status' => 'active')));
                 $pSum = 0;
                 $pUnit = 0;
                 $tTax = 0;
                 $tDisc = 0;
                 $tSub = 0;
                 foreach ($allProducts as $value) {
                     $orderProduct = $value["OrderProduct"];
                     $pUnit = $orderProduct["product_price"] * $orderProduct["product_qty"];
                     $pUnit = $pUnit - $pUnit * $orderProduct["product_disc"] / 100;
                     $pUnit = $pUnit + $pUnit * $orderProduct["product_tax"] / 100;
                     $pSum += $pUnit;
                 }
                 $order["Order"]["price"] = $pSum;
                 $order["Order"]["tax"] = 0;
                 $order["Order"]["tax_amt"] = 0;
                 $order["Order"]["subtotal_amt"] = $pSum;
                 $order["Order"]["total_amt"] = $pSum;
                 if (0 < $pSum) {
                     $order["Order"]["status"] = 'pending';
                 }
                 $order["Order"]["disc"] = 0;
                 $order["Order"]["disc_amt"] = 0;
                 $OrderModel->recursive = -1;
                 if (!$OrderModel->save($order["Order"])) {
                     $this->log('No se pudo actualizar el total de la orden');
                     $this->log($OrderModel->validationErrors);
                 }
             }
         }
     } catch (Exception $ex) {
         $this->log('Error al guardar el total de productos a la orden');
         $this->log($ex->getMessage());
     }
 }
 public function update_order_product()
 {
     $id = Input::get('id');
     $order_product = OrderProduct::find($id);
     if ($order_product) {
         $order_id = $order_product->order_id;
         $store_id = $order_product->store_id;
         if (Input::has('quantity') && !Input::has('product_name')) {
             $order_product->quantity = Input::get('quantity');
         }
         if (Input::has('price') && !Input::has('product_name')) {
             $order_product->price = Input::get('price');
         }
         if (Input::has('status') && !Input::has('product_name')) {
             $order_product->fulfilment_status = Input::get('status');
         }
         if (Input::has('product_name')) {
             $order_product->fulfilment_status = 'Replacement';
             $order_product->save();
             $order_product = new OrderProduct();
             $order_product->order_id = $order_id;
             $order_product->price = Input::get('price');
             $order_product->quantity = Input::get('quantity');
             $order_product->fulfilment_status = 'Fullfilled';
             $order_product->type = 'Replacement';
             $order_product->parent_id = $id;
             $order_product->product_name = Input::get('product_name');
             $image = Input::file('product_image_url');
             $validator = Validator::make(array('ima' => $image), array('ima' => 'required|mimes:jpeg,bmp,png'));
             if ($validator->fails()) {
                 $error_messages = $validator->messages();
                 $response_array = array('success' => false, 'error' => 'Invalid Input', 'error_code' => 401);
                 $response_code = 200;
                 $message = "Invalid Input File";
                 $type = "failed";
                 return Redirect::to('/admin/order/' . $order_id . '/details')->with('type', $type)->with('message', $message);
             } else {
                 if (Input::hasFile('product_image_url')) {
                     $order_product->product_image_url = upload_image(Input::file('product_image_url'));
                 }
                 $order_product->product_quantity = Input::get('product_quantity');
                 $order_product->product_unit = Input::get('product_unit');
                 $order_product->store_id = $store_id;
             }
             $order_product->save();
         }
         // update order data
         $products = OrderProduct::where('order_id', $order_id)->where('fulfilment_status', 'Fullfilled')->get();
         $total_products = 0;
         $total_amount = 0.0;
         foreach ($products as $product) {
             $total_products++;
             $total_amount = $total_amount + $product->quantity * $product->price;
             echo $total_amount;
         }
         $order = Order::find($order_id);
         $order->total_products = $total_products;
         $order->total_amount = $total_amount;
         $order->save();
         return Redirect::to('/admin/order/' . $order_id . '/details');
     }
 }