public function edit($id)
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $model = CarPayment::find($id);
     if (!Auth::user()->isadmin && $model->deliverycarbookno != null && $model->deliverycarbookno != '') {
         return "ไม่สามารถแก้ไขข้อมูลการชำระเงินได้ เนื่องจากมีการส่งรถแล้ว!!";
     }
     $carpreemption = CarPreemption::find($model->carpreemptionid);
     $carpreemptionselectlist = array();
     $carpreemptionselectlist[$carpreemption->id] = $carpreemption->bookno . '/' . $carpreemption->no;
     $model->bookno = $carpreemption->bookno;
     $model->no = $carpreemption->no;
     $model->purchasetype = $carpreemption->purchasetype;
     if ($carpreemption->purchasetype == 0) {
         $purchasetype0 = true;
         $purchasetype1 = false;
     } elseif ($carpreemption->purchasetype == 1) {
         $purchasetype0 = false;
         $purchasetype1 = true;
     }
     if ($carpreemption->carobjectivetype == 0) {
         $carobjectivetype0 = true;
         $carobjectivetype1 = false;
     } elseif ($carpreemption->carobjectivetype == 1) {
         $carobjectivetype0 = false;
         $carobjectivetype1 = true;
     }
     if ($carpreemption->registrationtype == 0) {
         $registrationtype0 = true;
         $registrationtype1 = false;
         $registrationtype2 = false;
     } elseif ($carpreemption->registrationtype == 1) {
         $registrationtype0 = false;
         $registrationtype1 = true;
         $registrationtype2 = false;
     } elseif ($carpreemption->registrationtype == 2) {
         $registrationtype0 = false;
         $registrationtype1 = false;
         $registrationtype2 = true;
     }
     $customer = Customer::find($carpreemption->buyercustomerid);
     $model->customer = $customer->title . ' ' . $customer->firstname . ' ' . $customer->lastname;
     $model->customer2 = $customer->title . ' ' . $customer->firstname . ' ' . $customer->lastname;
     $carmodel = CarModel::find($carpreemption->carmodelid);
     $carsubmodel = CarSubModel::find($carpreemption->carsubmodelid);
     $model->carmodel = $carmodel->name . '/' . $carsubmodel->name;
     $color = Color::find($carpreemption->colorid);
     $model->carcolor = $color->code . ' - ' . $color->name;
     $pricelist = Pricelist::find($carpreemption->pricelistid);
     $model->carprice = $pricelist->sellingpricewithaccessories + $carpreemption->colorprice;
     $carselectlist = array();
     $carselectlist[null] = 'เลือกรถ';
     if (Auth::user()->isadmin) {
         $cars = Car::doesntHave('carPayment')->where('carmodelid', $carpreemption->carmodelid)->where('carsubmodelid', $carpreemption->carsubmodelid)->where('colorid', $carpreemption->colorid)->orWhere(function ($query) use($model) {
             $query->where('id', $model->carid);
         })->orderBy('chassisno', 'asc')->orderBy('engineno', 'asc')->get(['id', 'chassisno', 'engineno']);
     } else {
         $cars = Car::where('provinceid', Auth::user()->provinceid)->doesntHave('carPayment')->where('carmodelid', $carpreemption->carmodelid)->where('carsubmodelid', $carpreemption->carsubmodelid)->where('colorid', $carpreemption->colorid)->orWhere(function ($query) use($model) {
             $query->where('id', $model->carid);
         })->orderBy('chassisno', 'asc')->orderBy('engineno', 'asc')->get(['id', 'chassisno', 'engineno']);
     }
     foreach ($cars as $item) {
         $carselectlist[$item->id] = $item->chassisno . '/' . $item->engineno;
     }
     $model->installments = $carpreemption->installments;
     $model->interest = $carpreemption->interest;
     if ($carpreemption->purchasetype == 1) {
         $finacecompany = FinaceCompany::find($carpreemption->finacecompanyid);
         $model->finacecompany = $finacecompany->name;
     }
     if ($carpreemption->purchasetype == 0) {
         $model->down = number_format($model->carprice - $carpreemption->discount, 2, '.', '');
         $model->yodjud = number_format(0, 2, '.', '');
         $model->yodjudwithinsurancepremium = number_format(0, 2, '.', '');
         if ($model->overrideopenbill != null && $model->overrideopenbill != '') {
             $model->openbill = $model->overrideopenbill;
         } else {
             $model->openbill = number_format($model->carprice - $carpreemption->discount, 2, '.', '');
         }
         $model->realprice = number_format($model->carprice - $carpreemption->discount, 2, '.', '');
         $model->payinadvanceamount = number_format(0, 2, '.', '');
     } else {
         $model->down = $carpreemption->down;
         $model->yodjud = number_format($model->carprice - $carpreemption->discount - $model->down + $carpreemption->accessories + $model->accessoriesfeeincludeinyodjud, 2, '.', '');
         $model->yodjudwithinsurancepremium = number_format($model->yodjud + $model->insurancepremium, 2, '.', '');
         $model->openbill = number_format($model->yodjud + $model->down, 2, '.', '');
         $model->realprice = number_format($model->carprice - $carpreemption->discount - $carpreemption->subdown, 2, '.', '');
         if ($model->firstinstallmentpay) {
             $model->firstinstallmentpayamount = number_format($model->amountperinstallment, 2, '.', '');
         } else {
             $model->firstinstallmentpayamount = number_format(0, 2, '.', '');
         }
         $model->payinadvanceamount = number_format($model->installmentsinadvance * $model->amountperinstallment, 2, '.', '');
         if ($carpreemption->subsidisefree) {
             $model->subsidise = number_format(0, 2, '.', '');
         } else {
             $model->subsidise = $carpreemption->subsidise;
         }
     }
     $model->accessoriesfee = $carpreemption->accessoriesfee;
     $insurancecompanies = InsuranceCompany::orderBy('name', 'asc')->get(['id', 'name']);
     $insurancecompanyselectlist = array();
     $insurancecompanyselectlist[null] = 'เลือกบริษัท';
     foreach ($insurancecompanies as $item) {
         $insurancecompanyselectlist[$item->id] = $item->name;
     }
     if ($carpreemption->insurancefeefree) {
         $model->insurancefee = number_format(0, 2, '.', '');
     } else {
         $model->insurancefee = $carpreemption->insurancefee;
     }
     if ($carpreemption->compulsorymotorinsurancefeefree) {
         $model->compulsorymotorinsurancefee = number_format(0, 2, '.', '');
     } else {
         $model->compulsorymotorinsurancefee = $carpreemption->compulsorymotorinsurancefee;
     }
     if ($carpreemption->carobjectivetype == 0) {
         $registerprovince = Province::find($carpreemption->registerprovinceid);
         $model->registerprovince = $registerprovince->name;
     } else {
         $model->registerprovince = null;
     }
     $model->registrationtype = $carpreemption->registrationtype;
     if ($carpreemption->registrationfeefree) {
         $model->registrationfee = number_format(0, 2, '.', '');
     } else {
         $model->registrationfee = $carpreemption->registrationfee;
     }
     if ($carpreemption->carobjectivetype == 0) {
         $redlabelhistory = Redlabelhistory::where('carpreemptionid', $carpreemption->id)->first();
         if ($redlabelhistory != null) {
             $redlabel = Redlabel::find($redlabelhistory->redlabelid);
             $model->redlabel = $redlabel->no;
         } else {
             $model->redlabel = "ไม่มีป้าย";
         }
     } else {
         $model->redlabel = null;
     }
     $model->cashpledgeredlabel = $carpreemption->cashpledgeredlabel;
     if ($carpreemption->implementfeefree) {
         $model->implementfee = number_format(0, 2, '.', '');
     } else {
         $model->implementfee = $carpreemption->implementfee;
     }
     $model->giveawaywithholdingtax = $carpreemption->giveawaywithholdingtax;
     $model->otherfee = $carpreemption->otherfee;
     $model->otherfeedetail = $carpreemption->otherfeedetail;
     $model->otherfee2 = $carpreemption->otherfee2;
     $model->otherfeedetail2 = $carpreemption->otherfeedetail2;
     $model->otherfee3 = $carpreemption->otherfee3;
     $model->otherfeedetail3 = $carpreemption->otherfeedetail3;
     if ($model->firstinstallmentpay) {
         $model->total = number_format($model->down + $model->amountperinstallment + $model->payinadvanceamount + $model->accessoriesfeeactuallypaid + $model->insurancefee + $model->compulsorymotorinsurancefee + $model->registrationfee + $model->cashpledgeredlabel + $model->implementfee + $model->giveawaywithholdingtax + $model->otherfee + $model->otherfee2 + $model->otherfee3, 2, '.', '');
     } else {
         $model->total = number_format($model->down + $model->payinadvanceamount + $model->accessoriesfeeactuallypaid + $model->insurancefee + $model->compulsorymotorinsurancefee + $model->registrationfee + $model->cashpledgeredlabel + $model->implementfee + $model->giveawaywithholdingtax + $model->otherfee + $model->otherfee2 + $model->otherfee3, 2, '.', '');
     }
     $model->subdown = $carpreemption->subdown;
     $model->cashpledge = $carpreemption->cashpledge;
     $model->oldcarprice = $carpreemption->oldcarprice;
     $model->totalpayments = number_format($model->total - $model->subdown - $model->cashpledge - $model->oldcarprice, 2, '.', '');
     $salesmanemployee = Employee::find($carpreemption->salesmanemployeeid);
     $model->salesmanemployee = $salesmanemployee->title . ' ' . $salesmanemployee->firstname . ' ' . $salesmanemployee->lastname;
     $approversemployee = Employee::find($carpreemption->approversemployeeid);
     $model->approversemployee = $approversemployee->title . ' ' . $approversemployee->firstname . ' ' . $approversemployee->lastname;
     if (Auth::user()->isadmin) {
         $payeeemployees = Employee::where('departmentid', 4)->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     } else {
         $payeeemployees = Employee::where('provinceid', Auth::user()->provinceid)->where('departmentid', 4)->orderBy('firstname', 'asc')->orderBy('lastname', 'asc')->get(['id', 'title', 'firstname', 'lastname']);
     }
     $payeeemployeeselectlist = array();
     $payeeemployeeselectlist[null] = 'เลือกพนักงาน';
     foreach ($payeeemployees as $item) {
         $payeeemployeeselectlist[$item->id] = $item->title . ' ' . $item->firstname . ' ' . $item->lastname;
     }
     $model->date = date('d-m-Y', strtotime($model->date));
     if ($model->overdueinstallmentdate1 != null && $model->overdueinstallmentdate1 != '') {
         $model->overdueinstallmentdate1 = date('d-m-Y', strtotime($model->overdueinstallmentdate1));
     }
     if ($model->overdueinstallmentdate2 != null && $model->overdueinstallmentdate2 != '') {
         $model->overdueinstallmentdate2 = date('d-m-Y', strtotime($model->overdueinstallmentdate2));
     }
     if ($model->overdueinstallmentdate3 != null && $model->overdueinstallmentdate3 != '') {
         $model->overdueinstallmentdate3 = date('d-m-Y', strtotime($model->overdueinstallmentdate3));
     }
     if ($model->overdueinstallmentdate4 != null && $model->overdueinstallmentdate4 != '') {
         $model->overdueinstallmentdate4 = date('d-m-Y', strtotime($model->overdueinstallmentdate4));
     }
     if ($model->overdueinstallmentdate5 != null && $model->overdueinstallmentdate5 != '') {
         $model->overdueinstallmentdate5 = date('d-m-Y', strtotime($model->overdueinstallmentdate5));
     }
     if ($model->overdueinstallmentdate6 != null && $model->overdueinstallmentdate6 != '') {
         $model->overdueinstallmentdate6 = date('d-m-Y', strtotime($model->overdueinstallmentdate6));
     }
     if ($model->oldcarpaydate != null && $model->oldcarpaydate != '') {
         $model->oldcarpaydate = date('d-m-Y', strtotime($model->oldcarpaydate));
     }
     if ($model->deliverycardate != null && $model->deliverycardate != '') {
         $model->deliverycardate = date('d-m-Y', strtotime($model->deliverycardate));
     }
     return view('carpaymentform', ['oper' => 'edit', 'pathPrefix' => '../../', 'carpayment' => $model, 'carpreemptionselectlist' => $carpreemptionselectlist, 'carselectlist' => $carselectlist, 'insurancecompanyselectlist' => $insurancecompanyselectlist, 'payeeemployeeselectlist' => $payeeemployeeselectlist, 'purchasetype0' => $purchasetype0, 'purchasetype1' => $purchasetype1, 'carobjectivetype0' => $carobjectivetype0, 'carobjectivetype1' => $carobjectivetype1, 'registrationtype0' => $registrationtype0, 'registrationtype1' => $registrationtype1, 'registrationtype2' => $registrationtype2]);
 }
 public function getbyid($id)
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $model = CarPreemption::find($id);
     $customer = Customer::find($model->buyercustomerid);
     $model->customer = $customer->title . ' ' . $customer->firstname . ' ' . $customer->lastname;
     $carmodel = CarModel::find($model->carmodelid);
     $carsubmodel = CarSubModel::find($model->carsubmodelid);
     $model->carmodel = $carmodel->name . '/' . $carsubmodel->name;
     $color = Color::find($model->colorid);
     $model->carcolor = $color->code . ' - ' . $color->name;
     $pricelist = Pricelist::find($model->pricelistid);
     $model->carprice = $pricelist->sellingpricewithaccessories + $model->colorprice;
     if (Auth::user()->isadmin) {
         if ($model->carobjectivetype == 0) {
             $cars = Car::doesntHave('carPayment')->where('objective', 0)->where('carmodelid', $model->carmodelid)->where('carsubmodelid', $model->carsubmodelid)->where('colorid', $model->colorid)->orderBy('chassisno', 'asc')->orderBy('engineno', 'asc')->get(['id', 'chassisno', 'engineno']);
         } else {
             $cars = Car::doesntHave('carPayment')->where('objective', '!=', 0)->where('carmodelid', $model->carmodelid)->where('carsubmodelid', $model->carsubmodelid)->where('colorid', $model->colorid)->orderBy('chassisno', 'asc')->orderBy('engineno', 'asc')->get(['id', 'chassisno', 'engineno']);
         }
     } else {
         if ($model->carobjectivetype == 0) {
             $cars = Car::where('provinceid', Auth::user()->provinceid)->doesntHave('carPayment')->where('objective', 0)->where('carmodelid', $model->carmodelid)->where('carsubmodelid', $model->carsubmodelid)->where('colorid', $model->colorid)->orderBy('chassisno', 'asc')->orderBy('engineno', 'asc')->get(['id', 'chassisno', 'engineno']);
         } else {
             $cars = Car::where('provinceid', Auth::user()->provinceid)->doesntHave('carPayment')->where('objective', '!=', 0)->where('carmodelid', $model->carmodelid)->where('carsubmodelid', $model->carsubmodelid)->where('colorid', $model->colorid)->orderBy('chassisno', 'asc')->orderBy('engineno', 'asc')->get(['id', 'chassisno', 'engineno']);
         }
     }
     $model->cars = $cars;
     if ($model->finacecompanyid != null && $model->finacecompanyid != '') {
         $finacecompany = FinaceCompany::find($model->finacecompanyid);
         $model->finacecompany = $finacecompany->name;
     }
     if ($model->interestratetypeid != null && $model->interestratetypeid != '') {
         $interestratetype = InterestRateType::find($model->interestratetypeid);
         $model->interestratetype = $interestratetype->name;
     }
     if ($model->purchasetype == 0) {
         $model->yodjud = 0;
         $model->realprice = $model->carprice - $model->discount;
     } else {
         $model->yodjud = $model->carprice - $model->discount - $model->down + $model->accessories;
         $model->realprice = $model->carprice - $model->discount - $model->subdown;
     }
     $salesmanemployee = Employee::find($model->salesmanemployeeid);
     $model->salesmanemployee = $salesmanemployee->title . ' ' . $salesmanemployee->firstname . ' ' . $salesmanemployee->lastname;
     $approversemployee = Employee::find($model->approversemployeeid);
     $model->approversemployee = $approversemployee->title . ' ' . $approversemployee->firstname . ' ' . $approversemployee->lastname;
     if ($model->carobjectivetype == 0) {
         $registerprovince = Province::find($model->registerprovinceid);
         $model->registerprovince = $registerprovince->name;
         $redlabelhistory = Redlabelhistory::where('carpreemptionid', $id)->first();
         if ($redlabelhistory != null) {
             $redlabel = Redlabel::find($redlabelhistory->redlabelid);
             $model->redlabel = $redlabel->no;
         } else {
             $model->redlabel = "ไม่มีป้าย";
         }
     } else {
         $model->redlabel = null;
     }
     //if($cust->birthdate != null)
     //$cust->birthdate = date('d-m-Y', strtotime($cust->birthdate));
     return $model;
 }