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'];
     // To save sale order receipts table
     $saleOrderReceipt = array();
     $saleOrderReceipt['sales_order_id'] = $sale_order_id;
     $saleOrderReceipt['exchange_rate_id'] = $exchangerate->id;
     $saleOrderReceipt['receipt_code'] = $this->generateAutoCode("sales_order_receipts", "receipt_code", 6, "RE");
     $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['created_by'] = \Auth::user()->id;
     $saleOrderReceipt['updated_by'] = \Auth::user()->id;
     $saleOrderReceipt['is_active'] = 1;
     $saleOrderReceipts->where('sales_order_id', $sale_order_id)->update($saleOrderReceipt);
     $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);
     }
     // 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;
 }
 public function save(CycleInventory $cycles, CycleProductDetail $cycleProducts)
 {
     $inputs = Input::all();
     // To save sale order table
     $cycle = array();
     $cycle['_token'] = $inputs['_token'];
     $cycle['date'] = $inputs['date'];
     $cycle['is_active'] = 1;
     $cycle['created_by'] = \Auth::user()->id;
     $cycle['updated_by'] = \Auth::user()->id;
     $cycles->fill($cycle)->save();
     $cycle_inventory_id = $cycles->id;
     for ($i = 0; $i < count($inputs['product_id']); $i++) {
         if ($inputs['amount'][$i] != "") {
             // To save sale order detail table
             $cycleProduct = new CycleProductDetail();
             $cycleProduct['cycle_inventory_id'] = $cycle_inventory_id;
             $cycleProduct['product_id'] = $inputs['product_id'][$i];
             $cycleProduct['current_qty'] = $inputs['current_qty'][$i];
             if ($inputs['actions'][$i] == 0) {
                 // actions = 1 it mean add stock
                 $cycleProduct['new_qty'] = $inputs['current_qty'][$i] + $inputs['amount'][$i];
                 $cycleProduct['actions'] = 0;
             } else {
                 // actions = 1 it mean update stock
                 $cycleProduct['new_qty'] = $inputs['amount'][$i];
                 $cycleProduct['actions'] = 1;
             }
             $cycleProduct['created_by'] = \Auth::user()->id;
             $cycleProduct['updated_by'] = \Auth::user()->id;
             $cycleProduct->save();
             // condition to check if product is exist in inventory
             $fields = ['product_id' => $inputs['product_id'][$i], 'location_id' => Session::get('location_id')];
             $checkIfSaleExistingProduct = InventoryTotal::where($fields)->first();
             // Save to inventories table
             $inventory = new Inventory();
             $inventory['cycle_inventory_id'] = $cycle_inventory_id;
             $inventory['product_id'] = $inputs['product_id'][$i];
             $inventory['location_id'] = Session::get('location_id');
             $inventory['qty'] = $inputs['amount'][$i];
             if ($inputs['actions'][$i] == 0) {
                 // actions = 1 it mean add stock
                 $inventory['qty'] = $inputs['amount'][$i];
                 $inventory['actions'] = 0;
             } else {
                 $inventory['qty'] = $inputs['amount'][$i] - $inputs['current_qty'][$i];
                 $inventory['actions'] = 1;
             }
             $inventory['date'] = $inputs['date'];
             $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();
                 if ($inputs['actions'][$i] == 0) {
                     // actions = 1 it mean add stock
                     $inventoryTotal['total_qty'] = $checkIfSaleExistingProduct['total_qty'] + $inputs['amount'][$i];
                 } else {
                     $inventoryTotal['total_qty'] = $inputs['amount'][$i];
                 }
                 $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['product_id'][$i], 'location_id' => Session::get('location_id'), 'date' => $inputs['date']];
                 $checkIfCycleExistingProduct = InventoryTotalDetail::where($fieldNews)->first();
                 if ($inputs['actions'][$i] == 0) {
                     // actions = 1 it mean add stock
                     $inventoryTotalDetail['total_cycle'] = $checkIfCycleExistingProduct['total_cycle'] + $inputs['amount'][$i];
                 } else {
                     $inventoryTotalDetail['total_cycle'] = $checkIfCycleExistingProduct['total_cycle'] + ($inputs['amount'][$i] - $inputs['current_qty'][$i]);
                 }
                 $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['product_id'][$i];
                 $inventoryTotal['location_id'] = Session::get('location_id');
                 $inventoryTotal['total_qty'] = $inputs['amount'][$i];
                 $inventoryTotal->save();
                 // Save to inventory_total_details table
                 $inventoryTotalDetail = new InventoryTotalDetail();
                 $inventoryTotalDetail['product_id'] = $inputs['product_id'][$i];
                 $inventoryTotalDetail['location_id'] = Session::get('location_id');
                 $inventoryTotalDetail['total_cycle'] = $inputs['amount'][$i];
                 $inventoryTotalDetail['date'] = $inputs['date'];
                 $inventoryTotalDetail->save();
             }
         }
     }
     // update list of inventory when process save inventory completed
     $qty = Input::get('qty');
     if ($qty == 1) {
         $inventory = Product::select('products.id AS product_id', 'products.name', 'products.code', 'pgroups.name AS product_group', 'total_qty')->leftJoin('inventory_totals', 'inventory_totals.product_id', '=', 'products.id')->join('pgroups', 'pgroups.id', '=', 'pgroup_id')->where('products.is_active', 1)->where('products.is_stock', 1)->where('total_qty', '>', 0)->orderBy('pgroups.name', 'ASC')->orderBy('products.name', 'ASC')->get();
     } else {
         if ($qty <= 0 && $qty != "") {
             $inventory = Product::select('products.id AS product_id', 'products.name', 'products.code', 'pgroups.name AS product_group', 'total_qty')->leftJoin('inventory_totals', 'inventory_totals.product_id', '=', 'products.id')->join('pgroups', 'pgroups.id', '=', 'pgroup_id')->where('products.is_active', 1)->where('products.is_stock', 1)->where(function ($query) {
                 $query->where('total_qty', '<', 0)->orWhere('total_qty', null);
             })->orderBy('pgroups.name', 'ASC')->orderBy('products.name', 'ASC')->get();
         } else {
             $inventory = Product::select('products.id AS product_id', 'products.name', 'products.code', 'pgroups.name AS product_group', 'total_qty')->leftJoin('inventory_totals', 'inventory_totals.product_id', '=', 'products.id')->join('pgroups', 'pgroups.id', '=', 'pgroup_id')->where('products.is_active', 1)->where('products.is_stock', 1)->orderBy('pgroups.name', 'ASC')->orderBy('products.name', 'ASC')->get();
         }
     }
     return Response::json($inventory);
 }