示例#1
0
 public function pay(SaleOrder $saleOrders, SaleOrderReceipt $saleOrderReceipts)
 {
     $inputs = Input::all();
     $saleOrders = SaleOrder::where('id', $inputs['sales_order_id'])->first();
     $exchangerate = DB::table('exchange_rates')->orderBy('id', 'desc')->first();
     // To save sale order receipts table
     $saleOrderReceipt = array();
     $saleOrderReceipt['sales_order_id'] = $saleOrders->id;
     $saleOrderReceipt['exchange_rate_id'] = $exchangerate->id;
     $saleOrderReceipt['receipt_code'] = $this->generateAutoCode("sales_order_receipts", "receipt_code", 6, "RE");
     $saleOrderReceipt['amount_us'] = 0;
     $saleOrderReceipt['amount_kh'] = $saleOrders->balance;
     $saleOrderReceipt['total_amount'] = $saleOrders->total_amount_riel;
     $saleOrderReceipt['balance'] = 0;
     $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->fill($saleOrderReceipt)->save();
     $saleOrder = array();
     $saleOrder['is_book'] = 0;
     $saleOrder['balance'] = 0;
     $saleOrders->whereId($saleOrders->id)->update($saleOrder);
     echo $saleOrders->id;
     exit;
 }
示例#2
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;
 }