public function anyEntry(Request $request,$id=null) { \DB::enableQueryLog(); if(isset($id) && $id!=null){ $id_purchaseorder = $id; $purchaseorder = Purchaseorder::find($id_purchaseorder); if($purchaseorder != null){ session(['current_purchaseorder' => $purchaseorder['id']]); }else{ return redirect('purchaseorders'); } }else{ $id_purchaseorder = session('current_purchaseorder') !== null ? session('current_purchaseorder') : 0; if($id_purchaseorder){ $purchaseorder = Purchaseorder::find($id_purchaseorder); session(['current_purchaseorder' => $purchaseorder['id']]); }else{ $purchaseorder = Purchaseorder::get()->last(); if($purchaseorder){ session(['current_purchaseorder' => $purchaseorder['id']]); }else{ $purchaseorder = new Purchaseorder; $purchaseorder->date = date("Y-m-d H:i:s"); $purchaseorder->created_by = \Auth::user()->id; $purchaseorder->save(); session(['current_purchaseorder' => $purchaseorder->id]); Log::create_log(\Auth::user()->id,'App\Purchaseorder','Tạo mới đơn mua hàng số '.$purchaseorder->id); session(['current_purchaseorder' => $purchaseorder->id]); } } } $address = Address::where('module_id','=',$purchaseorder->id) ->where('module_type','=','App\Purchaseorder')->first(); $address_province = isset($address->province_id)?$address->province_id:0; $country_province = Province::addSelect('provinces.name as province_name') ->where('provinces.id','=',$address_province) ->addSelect('countries.name as country_name') ->leftJoin('countries','countries.id','=','provinces.country_id') ->first(); if($country_province){ $country_province->toArray(); $purchaseorder['province_name'] = $country_province['province_name']; $purchaseorder['country_name'] = $country_province['country_name']; }else{ $purchaseorder['province_name'] = ''; $purchaseorder['country_name'] = ''; } if($address){ $address = $address->toArray(); }else{ $address = array(); } $purchaseorder = $purchaseorder->toArray(); //Init array $distributes = array(); $oums = array(); $users = array(); $countries = array(); $list_product = array(); $producttypes = array(); //Get value array $distributes = Company::getDistributeList()->with('address')->get()->toArray(); $oums = Oum::orderBy('name')->get()->toArray(); $users = User::get(); $countries = Country::with('provinces')->get()->toArray(); $producttypes = ProductType::get()->toArray(); $arr_product = MProduct::where('module_type','=','App\Purchaseorder') ->where('module_id','=',$purchaseorder['id']) ->lists('product_id'); Session::forget('product_of_po'.session('current_purchaseorder')); foreach ($arr_product as $key => $value) { Session::put('product_of_po'.session('current_purchaseorder').".".$value , $value); } $view_list_product = self::getListProduct(); $arr_create = Purchaseorder::select('users.name','purchaseorders.created_at') ->leftJoin('users','users.id','=','purchaseorders.created_by') ->where('purchaseorders.id','=',$purchaseorder['id']) ->get()->first()->toArray(); $arr_update = Purchaseorder::select('users.name','purchaseorders.updated_at') ->leftJoin('users','users.id','=','purchaseorders.updated_by') ->where('purchaseorders.id','=',$purchaseorder['id']) ->get()->first()->toArray(); $this->layout->arr_create = $arr_create; $this->layout->arr_update = $arr_update; $this->layout->content=view('purchaseorder.entry',[ 'distributes'=>$distributes, 'users'=>$users, 'countries'=>$countries, 'purchaseorder'=>$purchaseorder, 'address'=>$address, 'oums'=>$oums, 'producttypes'=>$producttypes, 'view_list_product'=>$view_list_product ]); }