public function sale(SaleOrder $saleOrders, Request $request, SaleOrderDetail $saleOrderDetails, SaleOrderReceipt $saleOrderReceipts)
 {
     $exchangerate = DB::table('exchange_rates')->orderBy('id', 'desc')->first();
     $rate = $exchangerate->riel;
     $inputs = Input::all();
     $inputs['amount_riel'] = str_replace(",", "", $inputs['amount_riel']);
     // To save sale order table
     $exchangerate = DB::table('exchange_rates')->orderBy('id', 'desc')->first();
     $rate = $exchangerate->riel;
     $saleOrder = array();
     //$saleOrder['_token']    = $inputs['_token'];
     $saleOrder['total_amount_riel'] = $inputs['total_amount_riel'];
     $saleOrder['total_amount_us'] = $inputs['total_amount_us'];
     $saleOrder['discount_riel'] = $inputs['custom-discount-riel'];
     $saleOrder['discount_us'] = $inputs['custom-discount-us'];
     $saleOrder['balance'] = $inputs['total_amount_riel'] - ($inputs['amount_riel'] + $inputs['amount_us'] * $rate + $inputs['custom-discount-riel'] + $inputs['custom-discount-us'] * $rate);
     $saleOrder['order_date'] = date('Y-m-d');
     $saleOrder['due_date'] = date('Y-m-d');
     $saleOrder['updated_by'] = \Auth::user()->id;
     $saleOrders->whereId($inputs['sales_order_id'])->update($saleOrder);
     $sale_order_id = $inputs['sales_order_id'];
     $products = SaleOrderDetail::whereSalesOrderId($sale_order_id)->get();
     // Update old qty inventoryTotalDetail and InventoryTotal
     $location = Session::get('location_id');
     foreach ($products as $product) {
         $fields = ['product_id' => $product->product_id, 'location_id' => Session::get('location_id')];
         $checkIfSaleExistingProducts = InventoryTotal::where($fields)->first();
         $inventoryTotalDetail = $inventoryTotals = array();
         $inventoryTotals = new InventoryTotal();
         $inventoryTotal['total_qty'] = $checkIfSaleExistingProducts['total_qty'] + $product->qty * $product->conversion;
         $inventoryTotal['updated_by'] = \Auth::user()->id;
         $inventoryTotals->where($fields)->update($inventoryTotal);
         $inventoryTotalDetails = new InventoryTotalDetail();
         $fieldNews = ['product_id' => $product->product_id, 'location_id' => Session::get('location_id')];
         $checkIfSaleExistingProductInventoryDetail = InventoryTotalDetail::where($fieldNews)->first();
         $inventoryTotalDetail['total_pos'] = $checkIfSaleExistingProductInventoryDetail['total_qty'] - $product->qty * $product->conversion;
         $inventoryTotalDetail['updated_by'] = \Auth::user()->id;
         $inventoryTotalDetails->where($fieldNews)->update($inventoryTotalDetail);
     }
     // To save sale order receipts table
     $saleOrderReceipt = array();
     $saleOrderReceipt['sales_order_id'] = $sale_order_id;
     $saleOrderReceipt['exchange_rate_id'] = $exchangerate->id;
     $saleOrderReceipt['amount_us'] = $inputs['amount_us'];
     $saleOrderReceipt['amount_kh'] = $inputs['amount_riel'];
     $saleOrderReceipt['total_amount'] = $inputs['total_amount_riel'];
     $saleOrderReceipt['balance'] = $inputs['total_amount_riel'] - ($inputs['amount_riel'] + $inputs['amount_us'] * $rate + $inputs['custom-discount-riel'] + $inputs['custom-discount-us'] * $rate);
     $saleOrderReceipt['pay_date'] = date('Y-m-d');
     $saleOrderReceipt['due_date'] = date('Y-m-d');
     $saleOrderReceipt['updated_by'] = \Auth::user()->id;
     $saleOrderReceipts->whereId($inputs['sales_order_id'])->update($saleOrderReceipt);
     // Delete old saleOrderDetail
     $saleOrderDetails::whereSalesOrderId($sale_order_id)->delete();
     // To save sale order detail table
     for ($i = 0; $i < count($inputs['id']) - 1; $i++) {
         $saleOrderDetail = new SaleOrderDetail();
         $saleOrderDetail['sales_order_id'] = $sale_order_id;
         $saleOrderDetail['product_id'] = $inputs['id'][$i];
         $saleOrderDetail['discount_price_riel'] = $inputs['txt_discount'][$i];
         $saleOrderDetail['qty'] = $inputs['txt_qty'][$i];
         $saleOrderDetail['qty_uom_id'] = 1;
         $saleOrderDetail['conversion'] = 1;
         $saleOrderDetail['unit_price'] = $inputs['txt_unit_price'][$i];
         $saleOrderDetail['total_price_riel'] = $inputs['txt_total_by_item'][$i];
         $saleOrderDetail->save();
     }
     // Delete inventory by sales_order_id
     Inventory::wherePointOfSalesId($sale_order_id)->delete();
     // Save to inventory
     for ($k = 0; $k < count($inputs['id']) - 1; $k++) {
         $fields = ['product_id' => $inputs['id'][$k], 'location_id' => $location];
         $checkIfSaleExistingProduct = InventoryTotal::where($fields)->first();
         // Save to inventories table
         $inventory = new Inventory();
         $inventory['point_of_sales_id'] = $sale_order_id;
         $inventory['product_id'] = $inputs['id'][$k];
         $inventory['location_id'] = $location;
         $inventory['qty'] = $inputs['txt_qty'][$k];
         $inventory['sale_price'] = $inputs['txt_unit_price'][$k];
         $inventory['date'] = date('Y-m-d');
         $inventory['created_by'] = \Auth::user()->id;
         $inventory['updated_by'] = \Auth::user()->id;
         $inventory->save();
         if (count($checkIfSaleExistingProduct) > 0) {
             //for existing product in inventory
             // Save to inventory_totals table
             $inventoryTotals = new InventoryTotal();
             $inventoryTotal = array();
             $inventoryTotal['total_qty'] = $checkIfSaleExistingProduct['total_qty'] - $inputs['txt_qty'][$k];
             $inventoryTotal['created_by'] = \Auth::user()->id;
             $inventoryTotal['updated_by'] = \Auth::user()->id;
             $inventoryTotals->where($fields)->update($inventoryTotal);
             // Save to inventory_total_details table
             $inventoryTotalDetails = new InventoryTotalDetail();
             $inventoryTotalDetail = array();
             $fieldNews = ['product_id' => $inputs['id'][$k], 'location_id' => $location, 'date' => date('Y-m-d')];
             $checkIfSaleExistingProductInventoryDetail = InventoryTotalDetail::where($fieldNews)->first();
             $inventoryTotalDetail['total_pos'] = $checkIfSaleExistingProductInventoryDetail['total_qty'] - $inputs['txt_qty'][$k];
             $inventoryTotalDetail['created_by'] = \Auth::user()->id;
             $inventoryTotalDetail['updated_by'] = \Auth::user()->id;
             $inventoryTotalDetails->where($fieldNews)->update($inventoryTotalDetail);
         } else {
             // for a new product in inventory
             // Save to inventory_totals table
             $inventoryTotal = new InventoryTotal();
             $inventoryTotal['product_id'] = $inputs['id'][$k];
             $inventoryTotal['location_id'] = $location;
             $inventoryTotal['total_qty'] = -1 * $inputs['txt_qty'][$k];
             $inventoryTotal['created_by'] = \Auth::user()->id;
             $inventoryTotal['updated_by'] = \Auth::user()->id;
             $inventoryTotal->save();
             // Save to inventory_total_details table
             $inventoryTotalDetail = new InventoryTotalDetail();
             $inventoryTotalDetail['product_id'] = $inputs['id'][$k];
             $inventoryTotalDetail['location_id'] = $location;
             $inventoryTotalDetail['total_pos'] = -1 * $inputs['txt_qty'][$k];
             $inventoryTotalDetail['date'] = date('Y-m-d');
             $inventoryTotalDetail['created_by'] = \Auth::user()->id;
             $inventoryTotalDetail['updated_by'] = \Auth::user()->id;
             $inventoryTotalDetail->save();
         }
     }
     echo $sale_order_id;
     exit;
 }