public function get_invoice()
 {
     $id = Request::segment(3);
     $order = Order::find($id);
     if ($order) {
         if ($order->user_id == Auth::user()->id && ($order->status = 'Delivered')) {
             $user = User::find($order->user_id);
             $products = OrderProduct::where('order_id', $order->id)->where(function ($query) {
                 $query->where('fulfilment_status', 'Fullfilled')->orWhere('fulfilment_status', 'Replacement');
             })->get();
             return View::make('invoice')->with('user', $user)->with('order', $order)->with('products', $products);
         }
     }
 }
 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');
     }
 }
 public function finalize_delivery()
 {
     $id1 = Input::get('orderID');
     $id2 = Input::get('deliverID');
     $orderproducts = OrderProduct::where('strOPOrdersID', '=', $id1)->get();
     $a = 1;
     $b = 1;
     foreach ($orderproducts as $orderproduct) {
         $details = DeliveryDetail::create(array('strDetID' => $id2, 'strDetProdID' => $orderproduct->strOPProdID, 'intDetQty' => $orderproduct->intOPQuantity));
         $details->save();
         $ids = DB::table('tblInventory')->select('strBatchID')->orderBy('strBatchID', 'desc')->take(1)->get();
         $ID = $ids["0"]->strBatchID;
         $newID = $this->smart($ID);
         $inv = Inventory::create(array('strBatchID' => $newID, 'strProdID' => $orderproduct->strOPProdID, 'strDlvryID' => $id2, 'intAvailQty' => $orderproduct->intOPQuantity, 'dblCurRetPrice' => Input::get($b + 'Rprice'), 'dblCurWPrice' => Input::get($a + 'Wprice')));
         $inv->save();
         $a++;
         $b++;
     }
     $notesID = $id1;
     $notes = OrderNotes::find($notesID);
     $notes->strOrdNotesStat = "Accepted";
     $notes->save();
     return Redirect::to('/delivery');
 }