Exemplo n.º 1
0
 public function anyEntry(Request $request, $id = null)
 {
     if (isset($id) && $id != null) {
         $company = Company::find($id);
         if ($company != null) {
             session(['current_company' => $company->id]);
         } else {
             return redirect('companies');
         }
     } else {
         $id = session('current_company') !== null ? session('current_company') : 0;
         $address_id = session('current_address') !== null ? session('current_address') : 0;
         if ($id) {
             $company = Company::find($id);
             session(["current_company" => $company->id]);
         } else {
             $company = Company::get()->last();
             if ($company) {
                 session(["current_company" => $company->id]);
             } else {
                 $company = new Company();
                 $company->save();
                 session(['current_company' => $company->id]);
             }
         }
     }
     $address = Address::where('module_id', '=', $company->id)->where('module_type', '=', 'App\\Company')->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();
         $company['province_name'] = $country_province['province_name'];
         $company['country_name'] = $country_province['country_name'];
     } else {
         $company['province_name'] = '';
         $company['country_name'] = '';
     }
     $countries = array();
     $company_type = array();
     $countries = Country::with('provinces')->get()->toArray();
     $company_type = CompanyType::get()->toArray();
     $company->toArray();
     $this->layout->content = view('company.entry', ['company' => $company, 'address' => $address, 'countries' => $countries, 'company_type' => $company_type]);
 }
	public function anyEntry(Request $request,$id=null)
	{
		\DB::enableQueryLog();
		if(isset($id) && $id!=null){
			$id_return_purchaseorder = $id;
			$returnpurchaseorder = ReturnPurchaseorder::find($id_return_purchaseorder);
			if($returnpurchaseorder != null){
				session(['current_returnpurchaseorder' => $returnpurchaseorder['id']]);
			}else{
				return redirect('returnpurchaseorders');
			}
		}else{
			$id_return_purchaseorder = session('current_returnpurchaseorder') !== null ? session('current_returnpurchaseorder') : 0;
			if($id_return_purchaseorder){
				$returnpurchaseorder = ReturnPurchaseorder::find($id_return_purchaseorder);
				session(['current_returnpurchaseorder' => $returnpurchaseorder['id']]);
			}else{
				$returnpurchaseorder = ReturnPurchaseorder::get()->last();
				if($returnpurchaseorder){
					session(['current_returnpurchaseorder' => $returnpurchaseorder['id']]);
				}else{
					$returnpurchaseorder = new ReturnPurchaseorder;
					$returnpurchaseorder->date = date("Y-m-d H:i:s");
					$returnpurchaseorder->created_by = \Auth::user()->id;
					$returnpurchaseorder->save();
					Log::create_log(\Auth::user()->id,'App\ReturnPurchaseorder','Tạo mới đơn hàng trả nhà cung cấp số '.$returnpurchaseorder->id);
					session(['current_returnpurchaseorder' => $returnpurchaseorder->id]);
				}
			}
		}
		$address = Address::where('module_id','=',$returnpurchaseorder->id)
					->where('module_type','=','App\ReturnPurchaseorder')->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();
		}
		$returnpurchaseorder = $returnpurchaseorder->toArray();
		$returnpurchaseorder['province_name'] = isset($country_province->province_name)?$country_province->province_name:'';
		$returnpurchaseorder['country_name'] = isset($country_province->country_name)?$country_province->country_name:'';
		// var_dump(DB::getQueryLog());
		// pr($returnpurchaseorder);die;

		//Init array
		$distributes = array();
		$oums = array();
		$users = array();
		$countries = array();
		$list_product = 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();

		$arr_product = MProduct::where('module_type','=','App\ReturnPurchaseorder')
						->where('module_id','=',$returnpurchaseorder['id'])
						->lists('product_id');

		Session::forget('product_of_rpo'.session('current_returnpurchaseorder'));
		foreach ($arr_product as $key => $value) {
			Session::put('product_of_rpo'.session('current_returnpurchaseorder').".".$value , $value);
		}

		$view_list_product = self::getListProduct();

		$arr_create = ReturnPurchaseorder::select('users.name','return_purchaseorders.created_at')
					->leftJoin('users','users.id','=','return_purchaseorders.created_by')
					->where('return_purchaseorders.id','=',$returnpurchaseorder['id'])
					->get()->first()->toArray();

		$arr_update = ReturnPurchaseorder::select('users.name','return_purchaseorders.updated_at')
					->leftJoin('users','users.id','=','return_purchaseorders.updated_by')
					->where('return_purchaseorders.id','=',$returnpurchaseorder['id'])
					->get()->first()->toArray();
		$this->layout->arr_create = $arr_create;
		$this->layout->arr_update = $arr_update;

		$this->layout->content=view('returnpurchaseorder.entry',[	'distributes'=>$distributes,
										'users'=>$users,
										'countries'=>$countries,
										'returnpurchaseorder'=>$returnpurchaseorder,
										'address'=>$address,
										'oums'=>$oums,
										'view_list_product'=>$view_list_product
									]);
	}