public function postUpdate(Request $request) { $arr_return = array( 'status' => 'error', 'message'=>'' ); $time =date('H:i:s', time()); $id_purchaseorder = session('current_purchaseorder') !== null ? session('current_purchaseorder') : 0; if($id_purchaseorder){ $purchaseorder = Purchaseorder::find($id_purchaseorder); session(['current_purchaseorder' => $purchaseorder['id']]); $time = date('H:i:s', strtotime($purchaseorder->date)); }else{ $purchaseorder = new Purchaseorder; $purchaseorder->date = date("Y-m-d H:i:s"); $purchaseorder->created_by = \Auth::user()->id; $purchaseorder->save(); Log::create_log(\Auth::user()->id,'App\Purchaseorder','Tạo mới đơn hàng mua số '.$purchaseorder->id); session(['current_purchaseorder' => $purchaseorder->id]); } $log = ''; if($purchaseorder->status == 0){ $address = Address::where('module_id','=',$purchaseorder->id) ->where('module_type','=','App\Purchaseorder')->first(); if(!$address){ $address = new Address; } if($request->has('company_id') && $purchaseorder->company_id != $request->input('company_id')){ $old = Company::find($purchaseorder->company_id); if(!$old){ $old = (object) ['name'=>'']; } $new = Company::find($request->input('company_id')); $log .= 'công ty từ "'.$old->name.'" thành "'.$new->name.'" '; } if($purchaseorder->company_id == $request->input('company_id')){ if($request->has('user_id') && $purchaseorder->user_id != $request->input('user_id')){ $old = User::find($purchaseorder->user_id); if(!$old){ $old = (object) ['name'=>'']; } $new = User::find($request->input('user_id')); $log .= 'người liên hệ từ "'.$old->name.'" thành "'.$new->name.'" '; } $old_date=date("Y-m-d",strtotime($purchaseorder->date)); $new_date = date("Y-m-d",strtotime($request->input('date'))); if($request->has('date') && $old_date != $new_date){ $log .= 'ngày từ "'.$old_date.'" thành "'.$new_date.'" '; } if($request->has('company_phone') && $purchaseorder->company_phone != $request->input('company_phone')){ $log .= 'số điện thoại từ "'.$purchaseorder->company_phone.'" thành "'.$request->input('company_phone').'" '; } if($request->has('company_email') && $purchaseorder->company_email != $request->input('company_email')){ $log .= 'email từ "'.$purchaseorder->company_email.'" thành "'.$request->input('company_email').'" '; } if($request->has('address') && $address->address != $request->input('address')){ $log .= 'địa chỉ từ "'.$address->address.'" thành "'.$request->input('address').'" '; } if($request->has('town_city') && $address->town_city != $request->input('town_city')){ $log .= 'quận huyện từ "'.$address->town_city.'" thành "'.$request->input('town_city').'" '; } if($request->has('province_id') && $address->province_id != $request->input('province_id')){ $old = Province::find($address->province_id); $new = Province::find($request->input('province_id')); if(!$old){ $old = (object) ['name'=>'']; } $log .= 'tỉnh thành từ "'.$old->name.'" thành "'.$new->name.'" '; } if($request->has('country_id') && $address->country_id != $request->input('country_id')){ $old = Country::find($address->country_id); $new = Country::find($request->input('country_id')); if(!$old){ $old = (object) ['name'=>'']; } $log .= 'quốc gia từ "'.$old->name.'" thành "'.$new->name.'" '; } } $purchaseorder->company_id = $request->has('company_id') ? $request->input('company_id') : 0; $purchaseorder->user_id = $request->has('user_id') ? $request->input('user_id') : 0; $purchaseorder->date = $request->has('date') ? date("Y-m-d H:i:s",strtotime($request->input('date').' '.$time)) : date("Y-m-d H:i:s"); $purchaseorder->company_phone = $request->has('company_phone') ? $request->input('company_phone') : ''; $purchaseorder->company_email = $request->has('company_email') ? $request->input('company_email') : ''; $address->module_id = $purchaseorder->id; $address->module_type = 'App\\Purchaseorder'; $address->address = $request->has('address') ? $request->input('address') : ''; $address->town_city = $request->has('town_city') ? $request->input('town_city') : ''; $address->zip_postcode = $request->has('zip_postcode') ? $request->input('zip_postcode') : ''; $address->country_id = $request->has('country_id') ? $request->input('country_id') : 0; $address->province_id = $request->has('province_id') ? $request->input('province_id') : 0; $address->save(); $purchaseorder->address_id = $address->id; }else{ $purchaseorder->sum_amount = 0; $purchaseorder->sum_invest = 0; } $old_status = $purchaseorder->status; if($purchaseorder->status != $request->has('status')){ $log .= 'trạng thái từ "'.($purchaseorder->status?'Hoàn thành':'Mới').'" thành "'.($request->has('status')?'Hoàn thành':'Mới').'" '; } $purchaseorder->status = $request->has('status')?1:0; $check_save_in_stock = true; if($purchaseorder->status){ $arr_mproduct = Mproduct::select('m_products.id','quantity','specification','name','invest') ->where('module_id', '=', $purchaseorder->id) ->where('module_type', '=', 'App\Purchaseorder') ->leftJoin('products','products.id','=','m_products.product_id') ->get()->toArray(); foreach ($arr_mproduct as $key => $mproduct) { $purchaseorder->sum_amount = $purchaseorder->sum_amount + $mproduct['invest']; $product_stock = ProductStock::where('m_product_id','=',$mproduct['id'])->first(); if(!$product_stock){ $product_stock = new ProductStock; $product_stock->in_stock = 0; } $product_stock->in_stock = $product_stock->in_stock + ($mproduct['quantity']*$mproduct['specification']); if($product_stock->in_stock < 0){ $check_save_in_stock = false; $arr_return['message'] .= 'Số lượng sản phẩm '.$mproduct['name'].' nhập vào thấp hơn số lượng đã bán ra<br/><br/>'; } } }else{ if($old_status != $purchaseorder->status){ $arr_mproduct = Mproduct::select('m_products.id','quantity','specification','name') ->where('module_id', '=', $purchaseorder->id) ->where('module_type', '=', 'App\Purchaseorder') ->leftJoin('products','products.id','=','m_products.product_id') ->get()->toArray(); foreach ($arr_mproduct as $key => $mproduct) { $product_stock = ProductStock::where('m_product_id','=',$mproduct['id'])->first(); if(!$product_stock){ $product_stock = new ProductStock; $product_stock->in_stock = 0; } $product_stock->in_stock = $product_stock->in_stock - ($mproduct['quantity']*$mproduct['specification']); $product_stock->save(); } } } if($check_save_in_stock){ $purchaseorder->updated_by = \Auth::user()->id; if($purchaseorder->save()){ Log::create_log(\Auth::user()->id,'App\Purchaseorder','Cập nhật '.$log.' đơn hàng mua số '.$purchaseorder->id); if($purchaseorder->status){ foreach ($arr_mproduct as $key => $mproduct) { $product_stock = ProductStock::where('m_product_id','=',$mproduct['id'])->first(); if(!$product_stock){ $product_stock = new ProductStock; $product_stock->in_stock = 0; } $product_stock->in_stock = $product_stock->in_stock + ($mproduct['quantity']*$mproduct['specification']); $product_stock->save(); } } Mproduct::where('module_id', '=', $purchaseorder->id) ->where('module_type', '=', 'App\Purchaseorder') ->update(['company_id' => $purchaseorder->company_id ]); $arr_return['status']= 'success'; }else{ $arr_return['message']= 'Saving fail !'; } } return $arr_return; }