public function index()
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $models = FinaceCompany::orderBy('name', 'asc')->get(['id', 'name']);
     $finacecompanyselectlist = array();
     array_push($finacecompanyselectlist, ':เลือกไฟแนนซ์');
     foreach ($models as $model) {
         array_push($finacecompanyselectlist, $model->id . ':' . $model->name);
     }
     $models = InterestRateType::has('commissionSpecials')->orderBy('name', 'asc')->get(['id', 'name']);
     $interestratetypeselectlist = array();
     array_push($interestratetypeselectlist, ':เลือกประเภทอัตราดอกเบี้ย');
     foreach ($models as $model) {
         array_push($interestratetypeselectlist, $model->id . ':' . $model->name);
     }
     $carmodels = CarModel::whereHas("carbrand", function ($q) {
         $q->where('ismain', true);
     })->orderBy('name', 'asc')->get(['id', 'name']);
     $carmodelselectlist = array();
     array_push($carmodelselectlist, '0:ทุกรุ่น');
     foreach ($carmodels as $item) {
         array_push($carmodelselectlist, $item->id . ':' . $item->name);
     }
     $carsubmodels = CarSubModel::has('commissionSpecials')->orderBy('name', 'asc')->get(['id', 'name']);
     $carsubmodelselectlist = array();
     array_push($carsubmodelselectlist, '0:ทุกรุ่น');
     foreach ($carsubmodels as $item) {
         array_push($carsubmodelselectlist, $item->id . ':' . $item->name);
     }
     return view('settings.commissionspecial', ['finacecompanyselectlist' => implode(";", $finacecompanyselectlist), 'interestratetypeselectlist' => implode(";", $interestratetypeselectlist), 'carmodelselectlist' => implode(";", $carmodelselectlist), 'carsubmodelselectlist' => implode(";", $carsubmodelselectlist)]);
 }
 public function index()
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $models = FinaceCompany::orderBy('name', 'asc')->get(['id', 'name']);
     $finacecompanyselectlist = array();
     array_push($finacecompanyselectlist, ':เลือกไฟแนนซ์');
     foreach ($models as $model) {
         array_push($finacecompanyselectlist, $model->id . ':' . $model->name);
     }
     return view('settings.commissionpa', ['finacecompanyselectlist' => implode(";", $finacecompanyselectlist)]);
 }
 public function edit($id)
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $model = CarPreemption::find($id);
     $carPayment = CarPayment::where('carpreemptionid', $id)->first();
     if (!Auth::user()->isadmin && $carPayment != null && $carPayment->deliverycarbookno != null && $carPayment->deliverycarbookno != '') {
         return "ไม่สามารถแก้ไขข้อมูลการจองได้ เนื่องจากมีการส่งรถแล้ว!!";
     }
     $bookingcustomer = Customer::find($model->bookingcustomerid);
     $model->bookingcustomername = $bookingcustomer->title . ' ' . $bookingcustomer->firstname . ' ' . $bookingcustomer->lastname;
     $model->provincebranchid = $bookingcustomer->provinceid;
     $model->bookingcustomeraddress = $bookingcustomer->address;
     $model->bookingcustomerprovinceid = $bookingcustomer->addprovinceid;
     $model->bookingcustomeramphurid = $bookingcustomer->amphurid;
     $model->bookingcustomerdistrictid = $bookingcustomer->districtid;
     $model->bookingcustomerzipcode = $bookingcustomer->zipcode;
     $model->bookingcustomerphone1 = $bookingcustomer->phone1;
     $model->bookingcustomerphone2 = $bookingcustomer->phone2;
     $model->bookingcustomeroccupationid = $bookingcustomer->occupationid;
     if ($bookingcustomer->birthdate != null && $bookingcustomer->birthdate != '') {
         $model->bookingcustomerbirthdate = date('d-m-Y', strtotime($bookingcustomer->birthdate));
     }
     if ($model->bookingcustomerid != $model->buyercustomerid) {
         $model->buyertype = 1;
         $buyercustomer = Customer::find($model->buyercustomerid);
         $model->buyercustomeraddress = $buyercustomer->address;
         $model->buyercustomerprovinceid = $buyercustomer->addprovinceid;
         $model->buyercustomeramphurid = $buyercustomer->amphurid;
         $model->buyercustomerdistrictid = $buyercustomer->districtid;
         $model->buyercustomerzipcode = $buyercustomer->zipcode;
         $model->buyercustomerphone1 = $buyercustomer->phone1;
         $model->buyercustomerphone2 = $buyercustomer->phone2;
         $model->buyercustomeroccupationid = $buyercustomer->occupationid;
         if ($buyercustomer->birthdate != null && $buyercustomer->birthdate != '') {
             $model->buyercustomerbirthdate = date('d-m-Y', strtotime($buyercustomer->birthdate));
         }
     } else {
         $model->buyertype = 0;
     }
     $provincebranchs = Province::whereHas('branchs', function ($q) {
         $q->where('isheadquarter', true);
     })->orderBy('name', 'asc')->get(['id', 'name']);
     $provincebranchselectlist = array();
     foreach ($provincebranchs as $item) {
         $provincebranchselectlist[$item->id] = $item->name;
     }
     if (Auth::user()->isadmin) {
         $customers = Customer::orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     } else {
         $customers = Customer::where('provinceid', Auth::user()->provinceid)->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     }
     $customerselectlist = array();
     $customerselectlist[null] = 'เลือกชื่อลูกค้า';
     foreach ($customers as $item) {
         $customerselectlist[$item->id] = $item->title . ' ' . $item->firstname . ' ' . $item->lastname;
     }
     $provinces = Province::orderBy('name', 'asc')->get(['id', 'name']);
     $provinceselectlist = array();
     $provinceselectlist[null] = 'เลือกจังหวัด';
     foreach ($provinces as $item) {
         $provinceselectlist[$item->id] = $item->name;
     }
     $bookingcustomeramphurs = Amphur::where('provinceid', $model->bookingcustomerprovinceid)->orderBy('name', 'asc')->get(['id', 'name']);
     $bookingcustomeramphurselectlist = array();
     $bookingcustomeramphurselectlist[null] = 'เลือกเขต/อำเภอ';
     foreach ($bookingcustomeramphurs as $item) {
         $bookingcustomeramphurselectlist[$item->id] = $item->name;
     }
     $buyercustomeramphurselectlist = array();
     if ($model->bookingcustomerid != $model->buyercustomerid) {
         $buyercustomeramphurs = Amphur::where('provinceid', $model->buyercustomerprovinceid)->orderBy('name', 'asc')->get(['id', 'name']);
         $buyercustomeramphurselectlist[null] = 'เลือกเขต/อำเภอ';
         foreach ($buyercustomeramphurs as $item) {
             $buyercustomeramphurselectlist[$item->id] = $item->name;
         }
     }
     $bookingcustomerdistricts = District::where('amphurid', $model->bookingcustomeramphurid)->orderBy('name', 'asc')->get(['id', 'name']);
     $bookingcustomerdistrictselectlist = array();
     $bookingcustomerdistrictselectlist[null] = 'เลือกแขวง/ตำบล';
     foreach ($bookingcustomerdistricts as $item) {
         $bookingcustomerdistrictselectlist[$item->id] = $item->name;
     }
     $buyercustomerdistrictselectlist = array();
     if ($model->bookingcustomerid != $model->buyercustomerid) {
         $buyercustomerdistricts = District::where('amphurid', $model->buyercustomeramphurid)->orderBy('name', 'asc')->get(['id', 'name']);
         $buyercustomerdistrictselectlist[null] = 'เลือกแขวง/ตำบล';
         foreach ($buyercustomerdistricts as $item) {
             $buyercustomerdistrictselectlist[$item->id] = $item->name;
         }
     }
     $occupations = Occupation::orderBy('name', 'asc')->get(['id', 'name']);
     $occupationselectlist = array();
     $occupationselectlist[null] = 'เลือกอาชีพ';
     foreach ($occupations as $item) {
         $occupationselectlist[$item->id] = $item->name;
     }
     $carmodels = CarModel::whereHas("carbrand", function ($q) {
         $q->where('ismain', true);
     })->orderBy('name', 'asc')->get(['id', 'name']);
     $carmodelselectlist = array();
     $carmodelselectlist[null] = 'เลือกแบบ';
     foreach ($carmodels as $item) {
         $carmodelselectlist[$item->id] = $item->name;
     }
     $carsubmodels = CarSubModel::where('carmodelid', $model->carmodelid)->orderBy('name', 'asc')->get(['id', 'name']);
     $carsubmodelselectlist = array();
     $carsubmodelselectlist[null] = 'เลือกรุ่น';
     foreach ($carsubmodels as $item) {
         $carsubmodelselectlist[$item->id] = $item->name;
     }
     $colorprices = array();
     $colors = Color::with(['carModelColors' => function ($query) use($model) {
         $query->where('carmodelid', $model->carmodelid);
     }])->whereHas('carModelColors', function ($q) use($model) {
         $q->where('carmodelid', $model->carmodelid);
     })->orderBy('code', 'asc')->get(['id', 'code', 'name']);
     $colorselectlist = array();
     $colorselectlist[null] = 'เลือกสี';
     foreach ($colors as $item) {
         $colorselectlist[$item->id] = $item->code . ' - ' . $item->name;
         $obj = (object) array("colorid" => $item->id, "price" => $item->carModelColors[0]->price);
         array_push($colorprices, $obj);
     }
     $provinceregistrationfee = array();
     $provinces = Province::with(['carModelRegisters' => function ($query) use($model) {
         $query->where('carmodelid', $model->carmodelid);
     }])->whereHas('carModelRegisters', function ($q) use($model) {
         $q->where('carmodelid', $model->carmodelid);
     })->orderBy('name', 'asc')->get(['id', 'name']);
     $registerprovinceselectlist = array();
     $registerprovinceselectlist[null] = 'เลือกจังหวัด';
     foreach ($provinces as $item) {
         $registerprovinceselectlist[$item->id] = $item->name;
         $registrationfee = array();
         array_push($registrationfee, (object) array("type" => 0, "price" => $item->carModelRegisters[0]->individualregistercost));
         array_push($registrationfee, (object) array("type" => 1, "price" => $item->carModelRegisters[0]->companyregistercost));
         array_push($registrationfee, (object) array("type" => 2, "price" => $item->carModelRegisters[0]->governmentregistercost));
         $obj = (object) array("provinceid" => $item->id, "registrationfee" => $registrationfee);
         array_push($provinceregistrationfee, $obj);
     }
     $oldcarbrands = CarBrand::where('ismain', false)->orderBy('name', 'asc')->get(['id', 'name']);
     $oldcarbrandselectlist = array();
     $oldcarbrandselectlist[null] = 'เลือกยี่ห้อรถ';
     foreach ($oldcarbrands as $item) {
         $oldcarbrandselectlist[$item->id] = $item->name;
     }
     $oldcarmodels = CarModel::where('carbrandid', $model->oldcarbrandid)->orderBy('name', 'asc')->get(['id', 'name']);
     $oldcarmodelselectlist = array();
     $oldcarmodelselectlist[null] = 'เลือกแบบ';
     foreach ($oldcarmodels as $item) {
         $oldcarmodelselectlist[$item->id] = $item->name;
     }
     if (Auth::user()->isadmin) {
         $saleemployees = Employee::where('departmentid', 6)->where('teamid', '<>', 1)->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     } else {
         $saleemployees = Employee::where('provinceid', Auth::user()->provinceid)->where('departmentid', 6)->where('teamid', '<>', 1)->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     }
     $saleemployeeselectlist = array();
     $saleemployeeselectlist[null] = 'เลือกพนักงาน';
     foreach ($saleemployees as $item) {
         $saleemployeeselectlist[$item->id] = $item->title . ' ' . $item->firstname . ' ' . $item->lastname;
     }
     if (Auth::user()->isadmin) {
         $salemanageremployees = Employee::where('departmentid', 6)->where('teamid', 1)->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     } else {
         $salemanageremployees = Employee::where('provinceid', Auth::user()->provinceid)->where('departmentid', 6)->where('teamid', 1)->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     }
     $salemanageremployeeselectlist = array();
     $salemanageremployeeselectlist[null] = 'เลือกพนักงาน';
     foreach ($salemanageremployees as $item) {
         $salemanageremployeeselectlist[$item->id] = $item->title . ' ' . $item->firstname . ' ' . $item->lastname;
     }
     if (Auth::user()->isadmin) {
         $approveremployees = Employee::where('departmentid', 5)->orWhere(function ($query) {
             $query->where('departmentid', 6)->where('teamid', 1);
         })->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     } else {
         $approveremployees = Employee::where('provinceid', Auth::user()->provinceid)->where(function ($query) {
             $query->where('departmentid', 5)->orWhere(function ($query) {
                 $query->where('departmentid', 6)->where('teamid', 1);
             });
         })->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     }
     $approveremployeeselectlist = array();
     $approveremployeeselectlist[null] = 'เลือกพนักงาน';
     foreach ($approveremployees as $item) {
         $approveremployeeselectlist[$item->id] = $item->title . ' ' . $item->firstname . ' ' . $item->lastname;
     }
     $giveaways = Giveaway::orderBy('name', 'asc')->orderBy('saleprice', 'asc')->get(['id', 'name', 'saleprice']);
     $giveawayselectlist = array();
     array_push($giveawayselectlist, ':เลือกของแถม');
     foreach ($giveaways as $ct) {
         array_push($giveawayselectlist, $ct->id . ':' . $ct->name . ' (' . $ct->saleprice . ' บาท)');
     }
     $giveawayFreeDatas = array();
     $giveawayBuyDatas = array();
     $giveawayFreeData = SupportRequest::old('giveawayFreeData');
     $giveawayBuyData = SupportRequest::old('giveawayBuyData');
     if ($giveawayFreeData != null && $giveawayFreeData != '') {
         $giveawayFreeData = json_decode($giveawayFreeData, true);
         foreach ($giveawayFreeData as $data) {
             $obj = (object) array("id" => $data["id"], "giveawayid" => $data["giveawayid"], "price" => $data["price"]);
             array_push($giveawayFreeDatas, $obj);
         }
     } else {
         $giveawayFrees = CarPreemptionGiveaway::where('free', true)->where('carpreemptionid', $id)->get(['id', 'giveawayid', 'price']);
         foreach ($giveawayFrees as $data) {
             $obj = (object) array("id" => $data->id, "giveawayid" => $data->giveawayid, "price" => $data->price);
             array_push($giveawayFreeDatas, $obj);
         }
     }
     if ($giveawayBuyData != null && $giveawayBuyData != '') {
         $giveawayBuyData = json_decode($giveawayBuyData, true);
         foreach ($giveawayBuyData as $data) {
             $obj = (object) array("id" => $data["id"], "giveawayid" => $data["giveawayid"]);
             array_push($giveawayBuyDatas, $obj);
         }
     } else {
         $giveawayBuys = CarPreemptionGiveaway::where('free', false)->where('carpreemptionid', $id)->get(['id', 'giveawayid']);
         foreach ($giveawayBuys as $data) {
             $obj = (object) array("id" => $data->id, "giveawayid" => $data->giveawayid);
             array_push($giveawayBuyDatas, $obj);
         }
     }
     $finacecompanies = FinaceCompany::orderBy('name', 'asc')->get(['id', 'name']);
     $finacecompanyselectlist = array();
     $finacecompanyselectlist[null] = 'เลือกบริษัท';
     foreach ($finacecompanies as $item) {
         $finacecompanyselectlist[$item->id] = $item->name;
     }
     $interestratetypes = InterestRateType::where('finacecompanyid', $model->finacecompanyid)->orderBy('name', 'asc')->get(['id', 'name']);
     $interestratetypeselectlist = array();
     $interestratetypeselectlist[null] = 'เลือกประเภทอัตราดอกเบี้ย';
     foreach ($interestratetypes as $item) {
         $interestratetypeselectlist[$item->id] = $item->name;
     }
     $carprices = array();
     $priceselectlist = array();
     $pricelists = Pricelist::where('carsubmodelid', $model->carsubmodelid)->where('effectivefrom', '<=', $model->date)->where('effectiveTo', '>=', $model->date)->orderBy('sellingpricewithaccessories', 'asc')->get(['id', 'sellingpricewithaccessories', 'promotion']);
     foreach ($pricelists as $item) {
         if ($item->promotion != null && $item->promotion != '') {
             $priceselectlist[$item->id] = $item->sellingpricewithaccessories . ' (' . $item->promotion . ')';
         } else {
             $priceselectlist[$item->id] = $item->sellingpricewithaccessories;
         }
         $obj = (object) array("pricelistid" => $item->id, "price" => $item->sellingpricewithaccessories);
         array_push($carprices, $obj);
     }
     $model->date = date('d-m-Y', strtotime($model->date));
     $model->datewantgetcar = date('d-m-Y', strtotime($model->datewantgetcar));
     $model->approvaldate = date('d-m-Y', strtotime($model->approvaldate));
     if ($model->contractdate != null && $model->contractdate != '') {
         $model->contractdate = date('d-m-Y', strtotime($model->contractdate));
     }
     return view('carpreemptionform', ['oper' => 'edit', 'pathPrefix' => '../../', 'carpreemption' => $model, 'carprices' => $carprices, 'colorprices' => $colorprices, 'provinceregistrationfee' => $provinceregistrationfee, 'giveawayFreeDatas' => $giveawayFreeDatas, 'giveawayBuyDatas' => $giveawayBuyDatas, 'provincebranchselectlist' => $provincebranchselectlist, 'customerselectlist' => $customerselectlist, 'provinceselectlist' => $provinceselectlist, 'bookingcustomeramphurselectlist' => $bookingcustomeramphurselectlist, 'buyercustomeramphurselectlist' => $buyercustomeramphurselectlist, 'bookingcustomerdistrictselectlist' => $bookingcustomerdistrictselectlist, 'buyercustomerdistrictselectlist' => $buyercustomerdistrictselectlist, 'occupationselectlist' => $occupationselectlist, 'carmodelselectlist' => $carmodelselectlist, 'carsubmodelselectlist' => $carsubmodelselectlist, 'colorselectlist' => $colorselectlist, 'oldcarbrandselectlist' => $oldcarbrandselectlist, 'oldcarmodelselectlist' => $oldcarmodelselectlist, 'giveawayselectlist' => implode(";", $giveawayselectlist), 'saleemployeeselectlist' => $saleemployeeselectlist, 'salemanageremployeeselectlist' => $salemanageremployeeselectlist, 'approveremployeeselectlist' => $approveremployeeselectlist, 'finacecompanyselectlist' => $finacecompanyselectlist, 'interestratetypeselectlist' => $interestratetypeselectlist, 'priceselectlist' => $priceselectlist, 'registerprovinceselectlist' => $registerprovinceselectlist]);
 }