public function postUpdateMproduct(Request $request){ $arr_return= array('status'=>'error','invest'=>0); $id = $request->has('id')?$request->input('id'):0; $log=""; if($id){ $mproduct = MProduct::find($id); $mproduct_po = Mproduct::find($mproduct->m_product_id); if($request->has('oum_id') && $mproduct->oum_id != $request->input('oum_id')){ $old = Oum::find($mproduct->oum_id); $new = Oum::find($request->input('oum_id')); if($old){ $log .= 'đơn vị từ "'.$old->name.'" thành "'.$new->name.'" '; }else{ $log .= 'đơn vị từ " " thành "'.$new->name.'" '; } } if($request->has('sell_price') && $mproduct->sell_price != $request->input('sell_price')){ $log .= 'giá bán từ "'.$mproduct->sell_price.'" thành "'.$request->input('sell_price').'" '; } if($request->has('specification') && $mproduct->specification != $request->input('specification')){ $log .= 'quy cách từ "'.$mproduct->specification.'" thành "'.$request->input('specification').'" '; } if($request->has('quantity') && $mproduct->quantity != $request->input('quantity')){ $log .= 'số lượng từ "'.$mproduct->quantity.'" thành "'.$request->input('quantity').'" '; } $mproduct->oum_id = $request->has('oum_id')?$request->input('oum_id'):0; $mproduct->origin_price = $request->has('origin_price')?$request->input('origin_price'):0; $mproduct->specification = $request->has('specification')?$request->input('specification'):0; $old_quantity = $mproduct->quantity ; $mproduct->quantity = $request->has('quantity')?$request->input('quantity'):0; $product_stock = ProductStock::where('m_product_id','=',$mproduct_po->id)->first(); $so_luong_con = $product_stock->in_stock; $product_stock->in_stock = $product_stock->in_stock - ($mproduct->quantity*$mproduct->specification); $mproduct->invest = $mproduct->specification* $mproduct->quantity* $mproduct->origin_price; if($product_stock->in_stock >=0){ if( !$mproduct->status){ if($mproduct->save()){ $product = Product::find($mproduct->product_id); Log::create_log(\Auth::user()->id,'App\ReturnPurchaseorder','cập nhật '.$log.' sản phẩm '.$product->sku.' đơn hàng trả nhà cung cấp số '.session('current_returnpurchaseorder')); $arr_return['status'] = 'success'; $arr_return['invest'] = number_format( $mproduct->invest ); }else{ $arr_return['message'] = 'Saving fail !'; } }else{ $arr_return['message'] = 'Đơn hàng đã hoàn thành không thể cập nhật'; } }else{ $arr_return['message'] = 'Số lượng trả '.$mproduct->name.' lớn hơn số lượng đã nhập<br/> Số lượng đã nhập là '.$so_luong_con.' cái'; } } $id = session('current_returnpurchaseorder'); //Init array $list_product = array(); //Get value $returnpurchaseorder = ReturnPurchaseorder::find(session('current_returnpurchaseorder')); $list_product = MProduct::select('m_products.*','products.sku','products.name')->where('module_type','=','App\ReturnPurchaseorder') ->where('module_id','=',$id) ->where('company_id','=',$returnpurchaseorder['company_id']) ->leftJoin('products','products.id','=','m_products.product_id') ->addSelect('oums.name as oum_name') ->leftJoin('oums','oums.id','=','m_products.oum_id') ->get()->toArray(); \Cache::put('list_product_rpo'.\Auth::user()->id, $list_product, 30); $returnpurchaseorder->updated_by = \Auth::user()->id; $returnpurchaseorder->save(); self::getListProduct(); return $arr_return; }
public function anySearchProductStock(Request $request){ // DB::enableQueryLog(); $arr_return = array(); $key = $request->has('key')?$request->input('key'):''; $key = '%'.$key.'%'; $module_id = session('current_saleorder'); $module_type = 'App\Saleorder'; $arr_product_of_so = Mproduct::where('module_id','=',$module_id) ->where('module_type','=',$module_type) ->lists('m_product_id'); $arr_product_of_so = array_values($arr_product_of_so); // pr($arr_product_of_so);die; $arr_return = MProduct::select( 'm_products.id', 'products.name', 'products.sku', 'm_products.product_id', 'm_products.oum_id', 'm_products.specification', 'm_products.origin_price', 'm_products.invest', 'product_stocks.in_stock', 'companies.name as company_name', 'oums.name as oum_name', 'm_products.module_id', 'purchaseorders.date' ) ->addSelect(DB::raw('product_stocks.in_stock/m_products.specification as real_in_stock')) ->leftJoin('oums','oums.id','=','m_products.oum_id') ->leftJoin('products','products.id','=','m_products.product_id') ->leftJoin('purchaseorders',function($join){ $join->on('purchaseorders.id','=','m_products.module_id') ->where('purchaseorders.status','=',1); }) ->where(function ($query){ $query->where('purchaseorders.status', '=', 1) ->orWhere('m_products.module_type', '=', 'in_stock'); }) ->where('product_stocks.in_stock','>',0) ->where(function($query) use ($key){ $query->where('products.sku','like',$key) ->orWhere('products.name','like',$key); }) ->whereNotIn('m_products.id',$arr_product_of_so) ->leftJoin('companies','companies.id','=','m_products.company_id') ->leftJoin('product_stocks','m_products.id','=','product_stocks.m_product_id') ->orderBy('products.name') ->orderBy('purchaseorders.date') ->limit(15) ->get()->toArray(); // pr(DB::getQueryLog()); foreach ($arr_return as $key => $value) { if($value['date']==''){ $arr_return[$key]['date'] = 'Tồn kho'; }else{ $arr_return[$key]['date'] = date('d/m/Y',strtotime($value['date'])); } } return $arr_return; }
public function postUpdateMproduct(Request $request){ $arr_return= array('status'=>'error','amount'=>0); $id = $request->has('id')?$request->input('id'):0; $log=""; if($id){ $mproduct = MProduct::find($id); $mproduct_po = Mproduct::find($mproduct->m_product_id); $mproduct_so = Mproduct::find($mproduct->m_product_id_so); $mproduct_rso_before = Mproduct::where('m_product_id_so','=',$mproduct_so->id) ->where('module_type','=','App\\ReturnSaleorder') ->where('company_id','=',$mproduct->company_id) ->get(); $mproduct_so->con_lai = $mproduct_so->quantity * $mproduct_so->specification; foreach ($mproduct_rso_before as $key => $value) { $mproduct_so->con_lai -= $value->quantity * $value->specification; } $so_luong_con = $mproduct_so->con_lai; if($request->has('oum_id') && $mproduct->oum_id != $request->input('oum_id')){ $old = Oum::find($mproduct->oum_id); $new = Oum::find($request->input('oum_id')); if($old){ $log .= 'đơn vị từ "'.$old->name.'" thành "'.$new->name.'" '; }else{ $log .= 'đơn vị từ " " thành "'.$new->name.'" '; } } if($request->has('sell_price') && $mproduct->sell_price != $request->input('sell_price')){ $log .= 'giá bán từ "'.$mproduct->sell_price.'" thành "'.$request->input('sell_price').'" '; } if($request->has('specification') && $mproduct->specification != $request->input('specification')){ $log .= 'quy cách từ "'.$mproduct->specification.'" thành "'.$request->input('specification').'" '; } if($request->has('quantity') && $mproduct->quantity != $request->input('quantity')){ $log .= 'số lượng từ "'.$mproduct->quantity.'" thành "'.$request->input('quantity').'" '; } $mproduct->oum_id = $request->has('oum_id')?$request->input('oum_id'):0; $mproduct->sell_price = $request->has('sell_price')?$request->input('sell_price'):0; $mproduct->specification = $request->has('specification')?$request->input('specification'):0; $old_quantity = $mproduct->quantity ; $mproduct->quantity = $request->has('quantity')?$request->input('quantity'):0; $check_return = ($mproduct_so->con_lai - $mproduct->quantity*$mproduct->specification) >=0; $mproduct->origin_price = $mproduct_so->origin_price; $mproduct->amount = $mproduct->specification* $mproduct->quantity* $mproduct->sell_price; $mproduct->invest = $mproduct->specification* $mproduct->quantity* $mproduct->origin_price; // if($product_stock->in_stock >=0){ if( $check_return ){ if($mproduct->save()){ $product = Product::find($mproduct->product_id); Log::create_log(\Auth::user()->id,'App\ReturnSaleorder','cập nhật '.$log.' sản phẩm '.$product->sku.' đơn hàng đại lý trả số '.session('current_returnsaleorder')); $arr_return['status'] = 'success'; $arr_return['amount'] = number_format( $mproduct->amount ); }else{ $arr_return['message'] = 'Saving fail !'; } }else{ $arr_return['message'] = 'Số lượng sản phẩm trả về lớn hơn số lượng còn lại trong kho <br/> Số lượng còn lại trong kho là '.$so_luong_con.' cái'; } // }else{ // $arr_return['message'] = 'Số lượng trả hàng lớn hơn số lượng đã bán'; // } } //Init array $list_product = array(); //Get value $returnsaleorder = ReturnSaleorder::find(session('current_returnsaleorder')); $list_product = MProduct::select('m_products.*','products.sku','products.name')->where('module_type','=','App\ReturnSaleorder') ->where('module_id','=',$id) ->where('company_id','=',$returnsaleorder['company_id']) ->leftJoin('products','products.id','=','m_products.product_id') ->addSelect('oums.name as oum_name') ->leftJoin('oums','oums.id','=','m_products.oum_id') ->get()->toArray(); \Cache::put('list_product_rso'.\Auth::user()->id, $list_product, 30); $returnsaleorder->updated_by = \Auth::user()->id; $returnsaleorder->save(); self::getListProduct(); return $arr_return; }