/**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $saleOrders = SaleOrder::where('id', $id)->first();
     $products = SaleOrderDetail::whereSalesOrderId($id)->get();
     if ($saleOrders->is_pos == 1) {
         foreach ($products as $product) {
             Inventory::wherePointOfSalesId($id)->delete();
             $fields = ['product_id' => $product->product_id, 'location_id' => \Auth::user()->location];
             $checkIfSaleExistingProduct = InventoryTotal::where($fields)->first();
             $inventoryTotalDetail = $inventoryTotals = array();
             $inventoryTotals = new InventoryTotal();
             $inventoryTotal['total_qty'] = $checkIfSaleExistingProduct['total_qty'] + $product->qty * $product->conversion;
             $inventoryTotal['created_by'] = \Auth::user()->id;
             $inventoryTotal['updated_by'] = \Auth::user()->id;
             $inventoryTotals->where($fields)->update($inventoryTotal);
             $inventoryTotalDetails = new InventoryTotalDetail();
             $fieldNews = ['product_id' => $product->product_id, 'location_id' => \Auth::user()->location, 'date' => date('Y-m-d')];
             $checkIfSaleExistingProductInventoryDetail = InventoryTotalDetail::where($fieldNews)->first();
             $inventoryTotalDetail['total_pos'] = $checkIfSaleExistingProductInventoryDetail['total_qty'] - $product->qty * $product->conversion;
             $inventoryTotalDetail['created_by'] = \Auth::user()->id;
             $inventoryTotalDetail['updated_by'] = \Auth::user()->id;
             $inventoryTotalDetails->where($fieldNews)->update($inventoryTotalDetail);
         }
     } else {
         Inventory::whereSalesOrderId($id)->delete();
     }
     SaleOrderReceipt::whereSalesOrderId($id)->delete();
     SaleOrderDetail::whereSalesOrderId($id)->delete();
     $saleOrders = SaleOrder::find($id);
     $saleOrders->delete();
     return Redirect::route('saleOrders.index')->with('flash_notice', 'You are successfully delete!');
 }