public function updateCompany(Request $request)
 {
     $company = Company::find(Auth::user()->company_id);
     if ($request->has('company-name')) {
         $company->name = $request->input('company-name');
     }
     if ($request->has('address')) {
         $company->address = $request->input('address');
     }
     if ($request->has('city')) {
         $company->city = $request->input('city');
     }
     if ($request->has('state')) {
         $company->state = $request->input('state');
     }
     if ($request->has('zip')) {
         $company->zip = $request->input('zip');
     }
     if ($request->has('email')) {
         $company->email = $request->input('email');
     }
     if ($request->has('phone')) {
         $company->phone = $request->input('phone');
     }
     $request->session()->flash('success', 'Company Updated!');
     $company->save();
     $team = User::where('company_id', $company->id)->where('user_type', 2)->get();
     return view('manage.company', ['company' => Company::find(Auth::user()->company_id), 'team' => $team]);
 }
Beispiel #2
0
 public function showSettings()
 {
     $companies = \App\Company::all()->lists('name', 'id');
     $current_company = \App\Company::find(Auth::user()->current_company);
     //        dd($current_company);
     return view('settings.settings', compact('companies', 'current_company'));
 }
 /**
  * Update the specified resource in storage.
  *
  * @param int $id
  *
  * @return Response
  */
 public function update(Request $request, $id)
 {
     $data = $request->except(['created_at', 'deleted_at']);
     $company = Company::find($id);
     $company->update($data);
     return redirect()->to('wpanel/profile');
 }
Beispiel #4
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     // $this->validate($request, $this->form_rules);
     $data = ['name' => $request->input('name'), 'website_name' => $request->input('website_name'), 'slogan' => $request->input('slogan'), 'phone_number' => $request->input('phone_number'), 'cell_phone' => $request->input('cell_phone'), 'address' => $request->input('address'), 'state' => $request->input('state'), 'city' => $request->input('city'), 'facebook' => $request->input('facebook'), 'facebook_app_id' => $request->input('facebook_app_id'), 'twitter' => $request->input('twitter'), 'zip_code' => $request->input('zip_code'), 'google_maps_key_api' => $request->input('google_maps_key_api'), 'email' => $request->input('email'), 'contact_email' => $request->input('contact_email'), 'sales_email' => $request->input('sales_email'), 'support_email' => $request->input('support_email'), 'description' => $request->input('description'), 'keywords' => $request->input('keywords'), 'about_us' => $request->input('about_us'), 'refund_policy' => $request->input('refund_policy'), 'privacy_policy' => $request->input('privacy_policy'), 'terms_of_service' => $request->input('terms_of_service')];
     $company = Company::find($id);
     $company->update($data);
     return redirect()->to('wpanel/profile');
 }
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     //
     $company = Company::find($id);
     $data = $request->except(['_token', '_method']);
     //dd($data);
     $company->update($data);
     return redirect()->to('company');
 }
Beispiel #6
0
 public static function loadCompany($id)
 {
     $company = Company::find($id);
     if (!empty($company)) {
         if (intval(Auth::id()) !== intval($company->user_id)) {
             $company = null;
         }
     }
     return $company;
 }
 public function edit($user_id, Request $request)
 {
     $start_time = $request->get('start') ? date("Y-m-d", strtotime($request->get('start') . " -1 day")) : date("Y-m-d", strtotime("-1 month -1 day"));
     $end_time = $request->get('end') ? date("Y-m-d", strtotime($request->get('end') . " +1 day")) : date("Y-m-d", strtotime("+1 day"));
     $company_id = Auth::user()->role == "supadmin" ? $request->get('company_id') : $this->company_id[0];
     $email = User::where('id', $user_id)->select('email')->pluck('email');
     $currency = Company::find($company_id)->currency->title;
     $data = Payment::shifts($user_id, $company_id, $start_time, $end_time);
     return view('payment.shifts')->with(['data' => $data, 'currency' => $currency, 'user_email' => $email, 'user_id' => $user_id, 'user_company_id' => $company_id]);
 }
 public function getApprove($id)
 {
     if (!$this->entrust->hasRole("super_admin")) {
         return Redirect::back()->with("error", "You are Not authorized to perform This action");
     }
     $company = Company::find($id);
     $company->status = "approved";
     $company->save();
     return Redirect::back()->with("success", "Company Has Been Approved successfully");
 }
 public function test_new_company_is_suspended()
 {
     $company = factory(Company::class)->create();
     $this->seeInDatabase('companies', ['id' => $company->id, 'is_suspended' => true]);
     $company->is_suspended = false;
     $company->save();
     $this->seeInDatabase('companies', ['id' => $company->id, 'is_suspended' => false]);
     $companyAgain = Company::find($company->id);
     $this->assertEquals(false, (bool) $companyAgain->is_suspended);
 }
Beispiel #10
0
 public function delete(Request $request)
 {
     try {
         $this->validate($request, ['id' => 'required']);
         $company = Company::find($request->id);
         $company->forceDelete();
         return response(1, 200);
     } catch (\Exception $e) {
         return response(0, 400);
     }
 }
 public function getEdit($id)
 {
     $agreement = Agreement::find($id);
     $company = Company::find($agreement->company_id);
     $clients = $company->clients()->selectRaw('CONCAT(name, " (Cli)") name_type, id')->lists('name_type', 'id')->all();
     $providers = $company->providers()->selectRaw('CONCAT(name, " (Pro)") name_type, id')->lists('name_type', 'id')->all();
     $companies = $clients + $providers;
     $companies_selected = $agreement->companies()->getRelatedIds()->all();
     $data = array('agreement' => $agreement, 'companies' => $companies, 'companies_selected' => $companies_selected);
     return view('admin.agreement.edit', $data);
 }
 public function update($id)
 {
     $input = \Input::all();
     $v = \Validator::make($input['company'], \App\Company::$updateRules);
     if ($v->fails()) {
         return \Redirect::route('companies.edit', ['id' => $id])->withErrors($v->errors())->withInput();
     }
     $company = \App\Company::find($id);
     $company->update($input['company']);
     return \Redirect::route('companies.show', ['company' => $company]);
 }
 public function destroy($id)
 {
     $company = Company::find($id);
     if (!$company) {
         return response()->json(['message' => 'Record not found'], 404);
     }
     if (\Auth::user()->id != $company->id) {
         return response()->json(['message' => 'You haven\'t permission to delete this entry'], 401);
     }
     return response()->json($company->delete(), 204);
 }
Beispiel #14
0
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     Blade::directive('hasrole', function ($expression) {
         $expression = explode(',', str_replace(['(', ')', "'"], '', $expression));
         return '<?php if(' . (Auth::user() && Auth::user()->hasRole($expression) ? 1 : 0) . '): ?>';
     });
     Blade::directive('endhasrole', function () {
         return '<?php endif; ?>';
     });
     $table = 'company';
     if (\Schema::hasTable($table)) {
         $main_company = Company::find(1);
         \View::share('main_company', $main_company);
     }
 }
Beispiel #15
0
 /**
  * Bootstrap any application services.
  *
  * @return void
  */
 public function boot()
 {
     $table = "company";
     if (\Schema::hasTable($table)) {
         $main_company = Company::find(1)->toArray();
         $categories_menu = \Cache::remember('categories_mothers', 25, function () {
             return Category::select('id', 'name')->childsOf('mothers')->actives()->get()->toArray();
         });
         $menu = [];
         foreach ($categories_menu as $value) {
             $menu[$value['id']] = $value;
         }
         \View::share('main_company', $main_company);
         \View::share('categories_menu', $menu);
     }
 }
 public function index(Request $request)
 {
     if (Auth::user()->role == "supadmin") {
         $company_id = $request->get('company_id');
     } elseif (Auth::user()->role == "worker") {
         $company_id = implode(",", $this->company_id);
     } else {
         $company_id = $this->company_id[0];
     }
     $start_shift = $end_shift = "00:00";
     if ($company_id && Auth::user()->role != "worker") {
         $start_shift = Company::find($company_id)->shift_day_start;
         $end_shift = Company::find($company_id)->shift_night_start;
     }
     $companies = Company::listAll();
     $workers = Company::workers($company_id);
     return view('rosters.list')->with(['company_id' => $company_id, 'shift_start' => $start_shift, 'shift_end' => $end_shift, 'workers' => $workers, 'companies' => $companies]);
 }
Beispiel #17
0
 public function destroy($id)
 {
     $company = Company::find($id);
     $campains = Campain::where('company_id', '=', $id)->get();
     if (count($campains) > 0) {
         foreach ($campains as $campain) {
             $links = Link::where('campain_id', '=', $campain->id)->get();
             if (count($links) > 0) {
                 foreach ($links as $link) {
                     $link->delete();
                 }
             }
             $campain->delete();
         }
     }
     $company->delete();
     return redirect('company/');
 }
 public function moveEmployee(Request $request)
 {
     if (!$request->has('team_id') || !$request->has('origin_team_id') || !$request->has('employee_id')) {
         return $this->returnJsonError('missing_params');
     }
     $input = $request->only('team_id', 'origin_team_id', 'employee_id');
     $company_id = (int) $input['team_id'];
     $employee_id = (int) $input['employee_id'];
     $employee = Employee::find($employee_id);
     $company = Company::find($company_id);
     if ($employee && $company) {
         $employee->company_id = $company_id;
         $employee->save();
         return $this->returnJsonSuccess();
     } else {
         if (!$employee) {
             return $this->returnJsonError('employee.miss');
         }
         if (!$company) {
             return $this->returnJsonError('company.miss');
         }
     }
 }
 public function shiftsUpdate($company_id, Request $request)
 {
     $this->validate($request, ['shift_day_start' => ['regex:/[0-9]{2}:[0-9]{2}(AM|PM)/'], 'shift_night_start' => ['regex:/[0-9]{2}:[0-9]{2}(AM|PM)/']]);
     $shift = Company::find($company_id);
     $shift->shift_day_start = Common::formatTimeForSQL($request->input('shift_day_start'));
     $shift->shift_night_start = Common::formatTimeForSQL($request->input('shift_night_start'));
     $shift->save();
     return redirect('/companies/' . $company_id . '/shifts#')->with(['message' => 'Shifts time is updated successfully.']);
 }
        if (($na{1}=='0')&&($na{2}=='0'))
            return $res . ' ' . $this->mag[$this->magind];
        $res .= $res==''? '' : ' and';
        $t = (int)$na{1}; $u = (int)$na{2};
        switch ($t) {
            case 0: $res .= ' ' . $this->units[$u]; break;
            case 1: $res .= ' ' . $this->teens[$u]; break;
            default:$res .= ' ' . $this->tens[$t] . ' ' . $this->units[$u] ; break;
        }
        $res .= ' ' . $this->mag[$this->magind];
        return $res;
    }


}*/
$company = \App\Company::find($company_id);
?>

<!DOCTYPE html>
<html>
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Robert Johnson Holdings | Invoice</title>
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="{{url()}}/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <!--<link rel="stylesheet" href="{{url()}}/bootstrap/font-awesome/css/font-awesome.min.css">
     Ionicons
    <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">-->
 public function publish($company_id, $id)
 {
     $job = Job::find($id);
     $job->status_id = 1;
     // active this job
     $job->save();
     $company = Company::find($company_id);
     return view('jobs.publish', compact('company', 'job'));
 }
Beispiel #22
0
//});
// Resource routes for User, Company, Creative, Job
Route::resource('coupons', 'CouponController');
Route::resource('users', 'UserController');
Route::resource('companies', 'CompanyController');
Route::resource('creatives', 'CreativeController');
Route::resource('companies.jobs', 'JobController');
Route::get('companies/{company}/jobs/{job}/publish', ['uses' => 'JobController@publish']);
// Company routing
Route::get('companies/{id}/account-setting', function ($id) {
    $company = Company::find($id);
    //$user = $company->getUser();
    return view('companies.account-setting', compact('company'));
});
Route::get('companies/{id}/dashboard', function ($id) {
    $company = Company::find($id);
    return view('companies.dashboard', compact('company'));
});
// Creative routing
Route::get('creatives/{id}/dashboard', function ($id) {
    $creative = Creative::find($id);
    return view('creatives.dashboard', compact('creative'));
});
Route::get('creatives/{id}/account-setting', function ($id) {
    $creative = Creative::find($id);
    //$user = $creative->getUser();
    return view('creatives.account-setting', compact('creative'));
});
/* Routing footer pages */
Route::get('pricing-plan', function () {
    return view('pricing-plan');
 public function store(Request $request)
 {
     if (Auth::user()->role != "supadmin" && $request->input('type') == "supadmin") {
         abort(401);
     }
     $rules = ['email' => 'required|email|unique:users', 'type' => 'required', 'names' => 'required', 'coordinates' => 'required', 'address' => 'required', 'mobile' => 'required', 'birth_date' => 'regex:/[0-9]{2}\\/[0-9]{2}\\/[0-9]{4}/', 'avatar' => 'image|mimes:jpg,jpeg,bmp,png,gif,tiff', 'cv' => 'mimes:doc,docx,ppt,pps,pptx,ppsx,xls,xlsx'];
     if (Auth::user()->role == "supadmin") {
         $rules['company'] = 'required';
     }
     $this->validate($request, $rules);
     $avatar_file_name = null;
     $cv_file_name = null;
     if ($request->file('avatar')) {
         $avatar_file_name = Common::randString(6) . "." . $request->file('avatar')->getClientOriginalExtension();
         Storage::put('avatars/' . $avatar_file_name, file_get_contents($request->file('avatar')->getRealPath()));
         ImageResize::load(storage_path("app") . '/avatars/' . $avatar_file_name);
         ImageResize::resizeToWidth(300);
         ImageResize::save();
     }
     if ($request->file('cv')) {
         $cv_file_name = Common::randString(6) . "." . $request->file('cv')->getClientOriginalExtension();
         Storage::put('cv/' . $cv_file_name, file_get_contents($request->file('cv')->getRealPath()));
     }
     $password = Common::generateStrongPassword();
     if (Auth::user()->role == "supadmin") {
         $company_id = $request->input('company');
     } else {
         $company_id = $this->company_id[0];
     }
     $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
     $user = new User();
     $user->role = $request->input('type');
     $user->password = Hash::make($password);
     $user->email = $request->input('email');
     $user->events_color = '#' . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)] . $rand[rand(0, 15)];
     $user->save();
     $info = new PersonalInfo();
     $info->names = $request->input('names');
     $info->address = $request->input('address');
     $info->coordinates = $request->input('coordinates');
     $info->mobile = $request->input('mobile');
     $info->gender = $request->input('gender');
     $info->birth_date = $request->input('birth_date');
     $info->home_phone = $request->input('home_phone');
     $info->work_phone = $request->input('work_phone');
     $info->fax = $request->input('fax');
     $info->other = $request->input('other');
     $info->avatar = $avatar_file_name;
     $info->cv = $cv_file_name;
     $user->info()->save($info);
     Company::find($company_id)->users()->attach($user->id);
     Notification::add($user->id, 'USER_ADD_BY_ADMIN', ['admin_id' => Auth::user()->id]);
     Mail::send('emails.newUserPassword', ['user' => $user, 'info' => $info, 'password' => $password, 'company' => Company::find($company_id)->pluck('name')], function ($m) use($user) {
         $m->from('*****@*****.**', 'TIMELINE');
         $m->to($user->email, $user->names)->subject('You have been added to TIMELINE');
     });
     return redirect('/users')->with(['message' => "User added successfully. Generated password with further login information is sent to his/her email ({$request->input('email')})."]);
 }
Beispiel #24
0
 public function getPrice(Request $request, $compid)
 {
     //
     $mcompany = Company::find($compid);
     $jobprices = \DB::table("prices")->where("company_id", $compid)->get();
     return View("settings.price", ["companies" => Company::all(), "mcompany" => $mcompany, 'prices' => $jobprices, "companyname" => $mcompany->name, 'papers' => Paper::all(), 'jobs' => Job::all(), 'title' => 'Job Price Setting']);
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $company = Company::find('1');
     CompanyFeatures::create(['company_id' => $company->id, 'description' => trans('globals.freeproducts')]);
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     //
     $companyId = Request::get('id');
     $company = Company::find($companyId);
     $userId = $company->user->id;
     User::where('id', $userId)->delete();
     return redirect("companies");
 }
	public function postListRevenueDistribute(Request $request){
		$list_order = array();
		$company_id = $request->has('company_id')?$request->input('company_id'):0;
		$month = $request->has('month')?$request->input('month'):0;
		$year = $request->has('year')?$request->input('year'):0;
		if($month!='all'){
			$begin = date('Y-m-d H:i:s',strtotime('1'.'-'.$month.'-'.$year));
			$end = date('Y-m-d H:i:s',strtotime('1'.'-'.($month+1).'-'.$year));
		}else{
			$begin = date('Y-m-d H:i:s',strtotime('1-1-'.$year));
			$end = date('Y-m-d H:i:s',strtotime('1-1-'.($year+1)));
		}
		$key_order = 1;
		$list_so = Mproduct::select('m_products.*','products.name','products.id','saleorders.date')
					->addSelect(DB::raw(' sum(amount) as sum_amount , sum(invest) as sum_invest'))
					->leftJoin('products','products.id','=','m_products.product_id')
					->leftJoin('saleorders',function($join){
						$join->on('saleorders.id','=','m_products.module_id')
							->where('module_type','=','App\Saleorder');
					})
					->leftJoin(DB::raw('(select id,company_id from m_products where module_type="App\\\\Purchaseorder" or module_type="in_stock") as ncc'),'ncc.id','=','m_products.m_product_id')
					->where('ncc.company_id','=',$company_id)
					->where('saleorders.date','>=',$begin)
					->where('saleorders.date','<',$end)
					->where('saleorders.status','=',1)
					->groupBy('product_id')
					->get()->toArray();
		foreach ($list_so as $key => $value) {
			$list_order[$key_order]['id'] = $value['id'];
			$list_order[$key_order]['name'] = $value['name'];
			$list_order[$key_order]['date'] =  $value['date'];
			$list_order[$key_order]['sum_amount'] =  $value['sum_amount'];
			$list_order[$key_order]['sum_invest'] =  $value['sum_invest'];
			$list_order[$key_order]['khoang_giam'] =  0;
			$list_order[$key_order]['lai'] =  $list_order[$key_order]['sum_amount'] - $list_order[$key_order]['sum_invest'];
			$list_order[$key_order]['loi_nhuan'] = $list_order[$key_order]['lai'] - $list_order[$key_order]['khoang_giam'];
			$list_order[$key_order]['updated_at'] = $value['updated_at'];
			$arr_product[$key_order] = $value['id'];
			$key_order++;
		}
		$list_rso = Mproduct::select('m_products.*','products.name','products.id','return_saleorders.date')
					->addSelect(DB::raw(' sum(amount) as sum_amount , sum(invest) as sum_invest'))
					->leftJoin('products','products.id','=','m_products.product_id')
					->leftJoin('return_saleorders',function($join){
						$join->on('return_saleorders.id','=','m_products.module_id')
							->where('module_type','=','App\ReturnSaleorder');
					})
					->leftJoin(DB::raw('(select id,company_id from m_products where module_type="App\\\\Purchaseorder" or module_type="in_stock") as ncc'),'ncc.id','=','m_products.m_product_id')
					->where('ncc.company_id','=',$company_id)
					->where('return_saleorders.date','>=',$begin)
					->where('return_saleorders.date','<',$end)
					->where('return_saleorders.status','=',1)
					->groupBy('product_id')
					->get()->toArray();
		foreach ($list_rso as $key => $value) {
			$check_in_array = array_search($value['id'], $arr_product);
			if($check_in_array){
				$list_order[$check_in_array]['sum_amount'] -=  $value['sum_amount'];
				$list_order[$check_in_array]['sum_invest'] -=  $value['sum_invest'];
				$list_order[$check_in_array]['khoang_giam'] -=  (abs($value['sum_amount']) - abs($value['sum_invest']));
				$list_order[$check_in_array]['loi_nhuan'] = $list_order[$check_in_array]['lai'] + $list_order[$check_in_array]['khoang_giam'];
			}else{
				$list_order[$key_order]['id'] = $value['id'];
				$list_order[$key_order]['name'] = $value['name'];
				$list_order[$key_order]['date'] =  $value['date'];
				$list_order[$key_order]['sum_amount'] =  -$value['sum_amount'];
				$list_order[$key_order]['sum_invest'] =  -$value['sum_invest'];
				$list_order[$key_order]['khoang_giam'] =  - (abs($list_order[$key_order]['sum_amount']) - abs($list_order[$key_order]['sum_invest']));
				$list_order[$key_order]['lai'] =  0;
				$list_order[$key_order]['loi_nhuan'] = $list_order[$key_order]['lai'] + $list_order[$key_order]['khoang_giam'];
				$list_order[$key_order]['updated_at'] = $value['updated_at'];
				$key_order++;
			}
			
		}
		$date = array();
		foreach ($list_order as $key => $value) {
			$date[$key] = $value['date'];
		}
		array_multisort($date,SORT_ASC,$list_order);
		$company_name = Company::find($company_id);
		$company_name = $company_name->name;

		$arr_cache = [
					'month'=>$month,
					'year'=>$year,
					'company_name'=>$company_name,
					'list_order'=>$list_order
				];
		\Cache::put('list_revenue_distribute'.\Auth::user()->id, $arr_cache, 30);
		return view('revenue.list-revenue-distribute',[
				'list_order' => $list_order
			]);
	}
 /**
  * redirect user to view the pageredirect
  * @param  integer $id
  * @return Response
  */
 public function editShowProfile($id)
 {
     //authorization
     if (!$this->adminAuth() && !$this->companyAuth(Auth::User()->id)) {
         return view('errors.404');
     }
     $countries = Country::all();
     $company = Company::find($id);
     return view('companies.editprofile', compact('company', 'countries'));
 }
	public function postUpdate(Request $request)
	{
		$arr_return = array(
					'status' => 'error',
					'message'=>''
				);
		$time =date('H:i:s', time());
		$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']]);
			$time = date('H:i:s', strtotime($returnpurchaseorder->date));
		}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]);
		}
		$log = '';
		$old_company_id = $returnpurchaseorder->company_id;
		if($returnpurchaseorder->status == 0){
			$address = Address::where('module_id','=',$returnpurchaseorder->id)
						->where('module_type','=','App\ReturnPurchaseorder')->first();
			if(!$address){
				$address = new Address;
			}
			if($request->has('company_id')  && $returnpurchaseorder->company_id != $request->input('company_id')){
				$old = Company::find($returnpurchaseorder->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($returnpurchaseorder->company_id == $request->input('company_id')){

				if($request->has('user_id')  && $returnpurchaseorder->user_id != $request->input('user_id')){
					$old = User::find($returnpurchaseorder->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($returnpurchaseorder->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')  && $returnpurchaseorder->company_phone != $request->input('company_phone')){
					$log .= 'số điện thoại từ "'.$returnpurchaseorder->company_phone.'" thành "'.$request->input('company_phone').'" ';
				}

				if($request->has('company_email')  && $returnpurchaseorder->company_email != $request->input('company_email')){
					$log .= 'email từ "'.$returnpurchaseorder->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.'" ';
				}
			}
			$returnpurchaseorder->company_id = $request->has('company_id') ? $request->input('company_id') : 0;
			$returnpurchaseorder->user_id = $request->has('user_id') ? $request->input('user_id') : 0;
			$returnpurchaseorder->date = $request->has('date') ? date("Y-m-d H:i:s",strtotime($request->input('date').' '.$time)) : date("Y-m-d H:i:s");
			$returnpurchaseorder->company_phone = $request->has('company_phone') ? $request->input('company_phone') : '';
			$returnpurchaseorder->company_email = $request->has('company_email') ? $request->input('company_email') : '';
			$address_id = isset($returnpurchaseorder->address_id) ? $returnpurchaseorder->address_id : 0;

			

			$address->module_id  = $returnpurchaseorder->id;
			$address->module_type  = 'App\\ReturnPurchaseorder';
			$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();
			$returnpurchaseorder->address_id = $address->id;
		}else{
			$returnpurchaseorder->sum_amount = 0;
			$returnpurchaseorder->sum_invest = 0;
		}
		$old_status = $returnpurchaseorder->status;
		$returnpurchaseorder->status = $request->has('status')?1:0;
		$check_save_in_stock = true;
		if($returnpurchaseorder->status){
			$arr_mproduct = Mproduct::select('m_products.id','quantity','specification','name','m_product_id','invest')
							->where('module_id', '=', $returnpurchaseorder->id)
							->where('module_type', '=', 'App\ReturnPurchaseorder')
							->leftJoin('products','products.id','=','m_products.product_id')
							->get()->toArray();
			foreach ($arr_mproduct as $key => $mproduct) {
				$returnpurchaseorder->sum_amount = $returnpurchaseorder->sum_amount + $mproduct['invest'];
				$mproduct_po = Mproduct::find($mproduct['m_product_id']);
				$product_stock = ProductStock::where('m_product_id','=',$mproduct_po->id)->first();
				$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 lớn hơn số lượng đã nhập<br/><br/>';
				}
			}
		}else{
			if($old_status != $returnpurchaseorder->status){
				$arr_mproduct = Mproduct::select('m_products.id','quantity','specification','name','m_product_id')
								->where('module_id', '=', $returnpurchaseorder->id)
								->where('module_type', '=', 'App\ReturnPurchaseorder')
								->leftJoin('products','products.id','=','m_products.product_id')
								->get()->toArray();
				foreach ($arr_mproduct as $key => $mproduct) {
					$mproduct_po = Mproduct::find($mproduct['m_product_id']);
					$product_stock = ProductStock::where('m_product_id','=',$mproduct_po->id)->first();
					$product_stock->in_stock = $product_stock->in_stock + ($mproduct['quantity']*$mproduct['specification']);
					$product_stock->save();
				}
			}
		}

		if($check_save_in_stock){
			$returnpurchaseorder->updated_by = \Auth::user()->id;
			if($returnpurchaseorder->save()){
				Log::create_log(\Auth::user()->id,'App\ReturnPurchaseorder','Cập nhật '.$log.' đơn hàng trả nhà cung cấp số '.$returnpurchaseorder->id);
				if($returnpurchaseorder->status){
					foreach ($arr_mproduct as $key => $mproduct) {
						$mproduct_po = Mproduct::find($mproduct['m_product_id']);
						$product_stock = ProductStock::where('m_product_id','=',$mproduct_po->id)->first();
						$product_stock->in_stock = $product_stock->in_stock -  ($mproduct['quantity']*$mproduct['specification']);
						$product_stock->save();
					}
				}

				if($old_company_id != $returnpurchaseorder->company_id){
					Mproduct::where('module_id', '=', $returnpurchaseorder->id)
						->where('module_type', '=', 'App\ReturnPurchaseorder')
						->where('company_id','=',$old_company_id)
						->delete();
				}else{
					Mproduct::where('module_id', '=', $returnpurchaseorder->id)
						->where('module_type', '=', 'App\ReturnPurchaseorder')
						->update(['company_id' => $returnpurchaseorder->company_id ]);
				}
				$arr_return['status']= 'success';
			}else{
				$arr_return['message']= 'Saving fail !';
			}
		}
		return $arr_return;
	}
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $company = Company::find($id);
     $friendly_url = $company->friendly_url;
     $company->fill($request->all());
     // if has file
     if ($request->hasFile('image')) {
         $image_name = $this->uploadImage($request->file('image'), 'companies', $friendly_url);
         $company->image = $image_name;
     }
     $company->save();
     Session::flash('message', 'Compañía actualizada Correctamente');
     return Redirect::to('companies');
 }