Esempio n. 1
0
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 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']);
     $user = \Auth::user()->id;
     // To save sale order table
     $saleOrder = array();
     $saleOrder['_token'] = $inputs['_token'];
     $saleOrder['location_id'] = Session::get('location_id');
     $saleOrder['customer_id'] = 1;
     $saleOrder['so_code'] = $this->generateAutoCode("sales_orders", "so_code", 6, "SO");
     $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['is_active'] = 1;
     $saleOrder['is_pos'] = 1;
     $saleOrder['created_by'] = $user;
     $saleOrder['updated_by'] = $user;
     $saleOrders->fill($saleOrder)->save();
     $sale_order_id = $saleOrders->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'] = $user;
     $saleOrderReceipt['updated_by'] = $user;
     $saleOrderReceipt['is_active'] = 1;
     $saleOrderReceipts->fill($saleOrderReceipt)->save();
     // 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();
     }
     // Save to inventory
     for ($k = 0; $k < count($inputs['id']) - 1; $k++) {
         $fields = ['product_id' => $inputs['id'][$k], 'location_id' => Session::get('location_id')];
         $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'] = Session::get('location_id');
         $inventory['qty'] = $inputs['txt_qty'][$k];
         $inventory['sale_price'] = $inputs['txt_unit_price'][$k];
         $inventory['date'] = date('Y-m-d');
         $inventory['created_by'] = $user;
         $inventory['updated_by'] = $user;
         $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'] = $user;
             $inventoryTotal['updated_by'] = $user;
             $inventoryTotals->where($fields)->update($inventoryTotal);
             // Save to inventory_total_details table
             $inventoryTotalDetails = new InventoryTotalDetail();
             $inventoryTotalDetail = array();
             $fieldNews = ['product_id' => $inputs['id'][$k], 'location_id' => Session::get('location_id'), 'date' => date('Y-m-d')];
             $checkIfSaleExistingProductInventoryDetail = InventoryTotalDetail::where($fieldNews)->first();
             $inventoryTotalDetail['total_pos'] = $checkIfSaleExistingProductInventoryDetail['total_qty'] - $inputs['txt_qty'][$k];
             $inventoryTotalDetail['created_by'] = $user;
             $inventoryTotalDetail['updated_by'] = $user;
             $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'] = Session::get('location_id');
             $inventoryTotal['total_qty'] = -1 * $inputs['txt_qty'][$k];
             $inventoryTotal['created_by'] = $user;
             $inventoryTotal['updated_by'] = $user;
             $inventoryTotal->save();
             // Save to inventory_total_details table
             $inventoryTotalDetail = new InventoryTotalDetail();
             $inventoryTotalDetail['product_id'] = $inputs['id'][$k];
             $inventoryTotalDetail['location_id'] = Session::get('location_id');
             $inventoryTotalDetail['total_pos'] = -1 * $inputs['txt_qty'][$k];
             $inventoryTotalDetail['date'] = date('Y-m-d');
             $inventoryTotalDetail['created_by'] = $user;
             $inventoryTotalDetail['updated_by'] = $user;
             $inventoryTotalDetail->save();
         }
     }
     echo $sale_order_id;
     exit;
 }
Esempio n. 2
0
 /**
  * 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!');
 }
 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);
 }