public static function boot()
 {
     parent::boot();
     static::creating(function ($model) {
         $carpayment = CarPayment::find($model->carpaymentid);
         $model->provinceid = $carpayment->provinceid;
         $model->branchid = $carpayment->branchid;
         $model->createdby = Auth::user()->id;
         $model->createddate = date("Y-m-d H:i:s");
         $model->modifiedby = Auth::user()->id;
         $model->modifieddate = date("Y-m-d H:i:s");
     });
     static::created(function ($model) {
         Log::create(['employeeid' => Auth::user()->id, 'operation' => 'Add', 'date' => date("Y-m-d H:i:s"), 'model' => class_basename(get_class($model)), 'detail' => $model->toJson()]);
     });
     static::updating(function ($model) {
         $carpayment = CarPayment::find($model->carpaymentid);
         $model->provinceid = $carpayment->provinceid;
         $model->branchid = $carpayment->branchid;
         $model->accountingDetailReceiveAndPays()->delete();
         $model->modifiedby = Auth::user()->id;
         $model->modifieddate = date("Y-m-d H:i:s");
     });
     static::updated(function ($model) {
         Log::create(['employeeid' => Auth::user()->id, 'operation' => 'Update', 'date' => date("Y-m-d H:i:s"), 'model' => class_basename(get_class($model)), 'detail' => $model->toJson()]);
     });
     static::deleting(function ($model) {
         $model->accountingDetailReceiveAndPays()->delete();
     });
     static::deleted(function ($model) {
         Log::create(['employeeid' => Auth::user()->id, 'operation' => 'Delete', 'date' => date("Y-m-d H:i:s"), 'model' => class_basename(get_class($model)), 'detail' => $model->toJson()]);
     });
 }
 public function index()
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     if (Auth::user()->isadmin) {
         $redlabels = RedLabel::get(['id', 'no']);
     } else {
         $redlabels = RedLabel::where('provinceid', Auth::user()->provinceid)->get(['id', 'no']);
     }
     $redlabelselectlist = array();
     foreach ($redlabels as $item) {
         array_push($redlabelselectlist, $item->id . ':' . $item->no);
     }
     if (Auth::user()->isadmin) {
         $carpayments = CarPayment::whereHas('carpreemption', function ($q) {
             $q->whereHas('redlabelhistories', function ($q2) {
                 $q2->whereNotNull('returndate');
             });
         })->with('carpreemption.buyerCustomer', 'car')->get();
     } else {
         $carpayments = CarPayment::where('provinceid', Auth::user()->provinceid)->whereHas('carpreemption', function ($q) {
             $q->whereHas('redlabelhistories', function ($q2) {
                 $q2->whereNotNull('returndate');
             });
         })->with('carpreemption.buyerCustomer', 'car')->get();
     }
     $carselectlist = array();
     $customerselectlist = array();
     $cashpledgeselectlist = array();
     foreach ($carpayments as $item) {
         array_push($carselectlist, $item->carpreemption->id . ':' . $item->car->engineno . '/' . $item->car->chassisno);
         array_push($customerselectlist, $item->carpreemption->id . ':' . $item->carpreemption->buyerCustomer->title . $item->carpreemption->buyerCustomer->firstname . ' ' . $item->carpreemption->buyerCustomer->lastname);
         array_push($cashpledgeselectlist, $item->carpreemption->id . ':' . $item->carpreemption->cashpledgeredlabel);
     }
     return view('returncashpledgeredlabel', ['redlabelselectlist' => implode(";", $redlabelselectlist), 'carselectlist' => implode(";", $carselectlist), 'customerselectlist' => implode(";", $customerselectlist), 'cashpledgeselectlist' => implode(";", $cashpledgeselectlist)]);
 }
 public function getforaccountingdetailbyid($id, $donumberformat)
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $accountingdetail = new AccountingDetail();
     $accountingdetail->carpaymentid = $id;
     $carpayment = CarPayment::find($id);
     $branch = Branch::find($carpayment->branchid);
     $accountingdetail->branchname = $branch->name;
     $carpreemption = CarPreemption::find($carpayment->carpreemptionid);
     $customer = Customer::find($carpreemption->buyercustomerid);
     $accountingdetail->customername = $customer->title . $customer->firstname . ' ' . $customer->lastname;
     if ($carpayment->deliverycardate != null && $carpayment->deliverycardate != '') {
         $accountingdetail->date = date('d-m-Y', strtotime($carpayment->deliverycardate));
         $accountingdetail->deliverycardate = date('d-m-Y', strtotime($carpayment->deliverycardate));
     } else {
         $accountingdetail->date = null;
         $accountingdetail->deliverycardate = "-";
     }
     $pricelist = Pricelist::find($carpreemption->pricelistid);
     $accountingdetail->carpriceinpricelist = $pricelist->sellingpricewithaccessories;
     $accountingdetail->colorprice = $carpreemption->colorprice;
     $carwithcolorprice = $pricelist->sellingpricewithaccessories + $carpreemption->colorprice;
     $accountingdetail->carwithcolorprice = $carwithcolorprice;
     $accountingdetail->purchasetype = $carpreemption->purchasetype;
     if ($carpreemption->purchasetype == 0) {
         $openbill = $accountingdetail->carwithcolorprice - $carpreemption->discount;
     } else {
         $openbill = $accountingdetail->carwithcolorprice - $carpreemption->discount + $carpreemption->accessories + $carpayment->accessoriesfeeincludeinyodjud;
     }
     $accountingdetail->openbill = $openbill;
     $accountingdetail->accessoriesfeeincludeinyodjud = $carpayment->accessoriesfeeincludeinyodjud;
     $accountingdetail->fakeaccessories = $carpreemption->accessories;
     $accountingdetail->discount = $carpreemption->discount;
     $accountingdetail->subdown = $carpreemption->subdown;
     $realsalesprice = $carwithcolorprice + $carpayment->accessoriesfeeincludeinyodjud - $carpreemption->discount - $carpreemption->subdown;
     $accountingdetail->realsalesprice = $realsalesprice;
     $accountingdetail->accessoriesfeeactuallypaid = $carpayment->accessoriesfeeactuallypaid;
     $registrationfee = $carpreemption->registrationfeefree ? 0 : $carpreemption->registrationfee;
     $accountingdetail->registrationfee = $registrationfee;
     $accountingdetail->actualinsurancefee = $carpreemption->insurancefee;
     if ($carpreemption->purchasetype == 0) {
         $compulsorymotorinsurancefeecash = $carpreemption->compulsorymotorinsurancefeefree ? 0 : $carpreemption->compulsorymotorinsurancefee;
         $conditioncompulsorymotorinsurancefeecustomerpaid = $compulsorymotorinsurancefeecash;
         $accountingdetail->compulsorymotorinsurancefeecash = $compulsorymotorinsurancefeecash;
         $insurancefeecash = $carpreemption->insurancefeefree ? 0 : $carpreemption->insurancefee;
         $accountingdetail->insurancefeecash = $insurancefeecash;
         $accountingdetail->compulsorymotorinsurancefeefn = 0;
         $compulsorymotorinsurancefeefn = 0;
         $accountingdetail->insurancefeefn = 0;
         $insurancefeefn = 0;
         $accountingdetail->firstinstallmentpayamount = 0;
         $firstinstallmentpayamount = 0;
         $accountingdetail->installmentsinadvance = 0;
         $accountingdetail->amountperinstallment = 0;
         $accountingdetail->payinadvanceamount = 0;
         $payinadvanceamount = 0;
         $accountingdetail->insurancepremium = 0;
         $accountingdetail->totalinadvancefees = 0;
         $totalinadvancefees = 0;
         $accountingdetail->conditioninsurancefee = $carpreemption->insurancefee;
         $accountingdetail->conditioninsurancefeecustomerpaid = $insurancefeecash;
         $conditioninsurancefeecompanypaid = $carpreemption->insurancefee - $insurancefeecash;
         $accountingdetail->conditioninsurancefeecompanypaid = $conditioninsurancefeecompanypaid;
         $accountingdetail->conditioncompulsorymotorinsurancefeecustomerpaid = $compulsorymotorinsurancefeecash;
     } else {
         $compulsorymotorinsurancefeecash = 0;
         $insurancefeecash = 0;
         $compulsorymotorinsurancefeefn = $carpreemption->compulsorymotorinsurancefeefree ? 0 : $carpreemption->compulsorymotorinsurancefee;
         $conditioncompulsorymotorinsurancefeecustomerpaid = $compulsorymotorinsurancefeefn;
         $accountingdetail->compulsorymotorinsurancefeefn = $compulsorymotorinsurancefeefn;
         $insurancefeefn = $carpreemption->insurancefeefree ? 0 : $carpreemption->insurancefee;
         $accountingdetail->insurancefeefn = $insurancefeefn;
         $firstinstallmentpayamount = $carpayment->firstinstallmentpay ? $carpayment->amountperinstallment : 0;
         $accountingdetail->firstinstallmentpayamount = $firstinstallmentpayamount;
         $accountingdetail->installmentsinadvance = $carpayment->installmentsinadvance;
         $accountingdetail->amountperinstallment = $carpayment->amountperinstallment;
         $payinadvanceamount = $carpayment->installmentsinadvance * $carpayment->amountperinstallment;
         $accountingdetail->payinadvanceamount = $payinadvanceamount;
         $accountingdetail->insurancepremium = $carpayment->insurancepremium;
         $totalinadvancefees = $insurancefeefn + $compulsorymotorinsurancefeefn + $firstinstallmentpayamount + $payinadvanceamount + $carpayment->insurancepremium;
         $accountingdetail->totalinadvancefees = $totalinadvancefees;
         $accountingdetail->compulsorymotorinsurancefeecash = 0;
         $accountingdetail->insurancefeecash = 0;
         $accountingdetail->conditioninsurancefee = $carpreemption->insurancefee;
         $accountingdetail->conditioninsurancefeecustomerpaid = $insurancefeefn;
         $conditioninsurancefeecompanypaid = $carpreemption->insurancefee - $insurancefeefn;
         $accountingdetail->conditioninsurancefeecompanypaid = $conditioninsurancefeecompanypaid;
         $accountingdetail->conditioncompulsorymotorinsurancefeecustomerpaid = $compulsorymotorinsurancefeefn;
     }
     $implementfee = $carpreemption->implementfeefree ? 0 : $carpreemption->implementfee;
     $accountingdetail->implementfee = $implementfee;
     $subsidise = $carpreemption->subsidisefree ? 0 : $carpreemption->subsidise;
     $accountingdetail->subsidise = $subsidise;
     $accountingdetail->giveawaywithholdingtax = $carpreemption->giveawaywithholdingtax;
     $accountingdetail->otherfee = $carpreemption->otherfee;
     $accountingdetail->otherfeedetail = $carpreemption->otherfeedetail;
     $accountingdetail->otherfee2 = $carpreemption->otherfee2;
     $accountingdetail->otherfeedetail2 = $carpreemption->otherfeedetail2;
     $accountingdetail->otherfee3 = $carpreemption->otherfee3;
     $accountingdetail->otherfeedetail3 = $carpreemption->otherfeedetail3;
     $totalotherfee = $subsidise + $carpreemption->giveawaywithholdingtax + $carpreemption->otherfee + $carpreemption->otherfee2 + $carpreemption->otherfee3;
     $accountingdetail->totalotherfee = $totalotherfee;
     $totalotherfees = $carpayment->accessoriesfeeactuallypaid + $registrationfee + $compulsorymotorinsurancefeecash + $insurancefeecash + $implementfee + $totalotherfee;
     $accountingdetail->totalotherfees = $totalotherfees;
     $carmodel = CarModel::find($carpreemption->carmodelid);
     $carsubmodel = CarSubModel::find($carpreemption->carsubmodelid);
     $accountingdetail->submodel = $carmodel->name . '/' . $carsubmodel->name;
     $car = Car::find($carpayment->carid);
     $accountingdetail->carno = $car->no;
     $accountingdetail->engineno = $car->engineno;
     $accountingdetail->chassisno = $car->chassisno;
     $color = Color::find($car->colorid);
     $accountingdetail->color = $color->code;
     $accountingdetail->purchasetypetext = $carpreemption->purchasetype == 0 ? "C" : "F";
     $accountingdetail->down = $carpreemption->down;
     $insurancecompany = InsuranceCompany::find($carpayment->insurancecompanyid);
     if ($insurancecompany != null) {
         $accountingdetail->insurancecompany = $insurancecompany->name;
     }
     $accountingdetail->capitalinsurance = $carpayment->capitalinsurance;
     $compulsorymotorinsurancecompany = InsuranceCompany::find($carpayment->compulsorymotorinsurancecompanyid);
     if ($compulsorymotorinsurancecompany != null) {
         $accountingdetail->compulsorymotorinsurancecompany = $compulsorymotorinsurancecompany->name;
     }
     $cartype = CarType::find($carmodel->cartypeid);
     $accountingdetail->conditioncompulsorymotorinsurancefee = $cartype->actpaidincludevat;
     $accountingdetail->hascompulsorymotorinsurancefee = $cartype->actpaidincludevat == 0 ? 0 : 1;
     $conditioncompulsorymotorinsurancefeecompanypaid = $cartype->actpaidincludevat - $conditioncompulsorymotorinsurancefeecustomerpaid;
     $accountingdetail->conditioncompulsorymotorinsurancefeecompanypaid = $conditioncompulsorymotorinsurancefeecompanypaid;
     $note1insurancefee = $carpreemption->insurancefee * 100 / 107.0;
     $accountingdetail->note1insurancefee = $note1insurancefee;
     $accountingdetail->note1insurancefeeincludevat = $carpreemption->insurancefee;
     $note1insurancefeevat = $carpreemption->insurancefee - $note1insurancefee;
     $accountingdetail->note1insurancefeevat = $note1insurancefeevat;
     $accountingdetail->note1compulsorymotorinsurancefee = $cartype->actpaid;
     $accountingdetail->note1compulsorymotorinsurancefeevat = $cartype->actpaidincludevat - $cartype->actpaid;
     $accountingdetail->note1compulsorymotorinsurancefeeincludevat = $cartype->actpaidincludevat;
     $note1totalfee = $note1insurancefee + $cartype->actpaid;
     $accountingdetail->note1totalfee = $note1totalfee;
     $note1totalfeevat = $note1insurancefeevat + ($cartype->actpaidincludevat - $cartype->actpaid);
     $accountingdetail->note1totalfeevat = $note1totalfeevat;
     $note1totalfeeincludevat = $carpreemption->insurancefee + $cartype->actpaidincludevat;
     $accountingdetail->note1totalfeeincludevat = $note1totalfeeincludevat;
     $cashpledgeredlabel = $carpreemption->cashpledgeredlabel;
     $accountingdetail->cashpledgeredlabel = $cashpledgeredlabel;
     if ($cashpledgeredlabel == null || $cashpledgeredlabel == 0) {
         $accountingdetail->hascashpledgeredlabel = 0;
     } else {
         $accountingdetail->hascashpledgeredlabel = 1;
     }
     $redlabelhistory = Redlabelhistory::where('carpreemptionid', $carpreemption->id)->orderBy('id', 'desc')->first();
     if ($redlabelhistory != null) {
         if ($redlabelhistory->returndate != null) {
             $accountingdetail->redlabelreturncashpledgedate = date('d-m-Y', strtotime($redlabelhistory->returncashpledgedate));
         } else {
             $accountingdetail->redlabelreturncashpledgedate = "-";
         }
     } else {
         $accountingdetail->redlabelreturncashpledgedate = "ไม่เอาป้าย";
     }
     $cashpledge = $carpreemption->cashpledge;
     $accountingdetail->cashpledge = $cashpledge;
     $totalcashpledge = $cashpledgeredlabel - $cashpledge;
     $accountingdetail->totalcashpledge = $totalcashpledge;
     $totalcash = $realsalesprice + $totalotherfees + $totalinadvancefees + $totalcashpledge;
     $accountingdetail->totalcash = $totalcash;
     $finacecompany = FinaceCompany::find($carpreemption->finacecompanyid);
     if ($finacecompany != null) {
         $accountingdetail->finacecompany = $finacecompany->name;
     }
     $accountingdetail->incasefinace = $carpreemption->purchasetype;
     if ($carpreemption->purchasetype == 1) {
         $accountingdetail->interest = $carpreemption->interest;
         $accountingdetail->installments = $carpreemption->installments;
         $pricelist = Pricelist::find($carpreemption->pricelistid);
         $carprice = $pricelist->sellingpricewithaccessories + $carpreemption->colorprice;
         $yodjud = $carprice - $carpreemption->discount - $carpreemption->down + $carpreemption->accessories + $carpayment->accessoriesfeeincludeinyodjud;
         $yodjudwithinsurancepremium = $yodjud + $carpayment->insurancepremium;
         $accountingdetail->yodjud = $yodjudwithinsurancepremium;
         $yodjudwithinterest = $yodjudwithinsurancepremium * ($carpreemption->interest + 100) / 100.0;
         $accountingdetail->yodjudwithinterest = $yodjudwithinterest;
         $finaceprofit = $yodjudwithinterest - $yodjudwithinsurancepremium;
         $commissionfinace = CommissionFinace::where('finacecompanyid', $carpreemption->finacecompanyid)->where('interestratetypeid', $carpreemption->interestratetypeid)->where('finaceminimumprofit', '<=', $finaceprofit)->where('effectivefrom', '<=', $carpreemption->date)->where('effectiveto', '>=', $carpreemption->date)->whereHas('commissionFinaceCars', function ($query) use($carpreemption) {
             $query->where('carmodelid', $carpreemption->carmodelid)->Where(function ($query) use($carpreemption) {
                 $query->where('carsubmodelid', $carpreemption->carsubmodelid)->orWhere('carsubmodelid', 0);
             });
         })->first();
         $percentdown = $carpreemption->down * 100.0 / ($carprice - $carpreemption->discount + $carpreemption->accessories);
         $accountingdetail->comfinpercent = null;
         $comfinpercent = 0;
         $accountingdetail->comfinyear = null;
         $comfinyear = 0;
         if ($commissionfinace != null) {
             $commissionfinaceinterest = CommissionFinaceInterest::where('commissionfinaceid', $commissionfinace->id)->where('downfrom', '<=', $percentdown)->where('downto', '>=', $percentdown)->first();
             if ($commissionfinaceinterest != null) {
                 $commissionstandardinterest = 0;
                 switch ($carpreemption->installments) {
                     case 24:
                         $commissionstandardinterest = $commissionfinaceinterest->installment24;
                         break;
                     case 36:
                         $commissionstandardinterest = $commissionfinaceinterest->installment36;
                         break;
                     case 48:
                         $commissionstandardinterest = $commissionfinaceinterest->installment48;
                         break;
                     case 60:
                         $commissionstandardinterest = $commissionfinaceinterest->installment60;
                         break;
                     case 72:
                         $commissionstandardinterest = $commissionfinaceinterest->installment72;
                         break;
                     case 84:
                         $commissionstandardinterest = $commissionfinaceinterest->installment84;
                         break;
                 }
                 $commissionfinaceinterests = CommissionFinaceCom::where('commissionfinaceid', $commissionfinace->id)->orderBy('com', 'asc')->get();
                 $previousstepcom = null;
                 foreach ($commissionfinaceinterests as $item) {
                     if ($carpreemption->interestratemode == 0) {
                         $currentstepinterest = $commissionstandardinterest + $item->interestcalculationbeginning;
                     } else {
                         $currentstepinterest = $commissionstandardinterest + $item->interestcalculationending;
                     }
                     if ($carpreemption->interest == $currentstepinterest) {
                         $accountingdetail->comfinpercent = $item->com;
                         $accountingdetail->comfinyear = $commissionfinace->years;
                         $comfinpercent = $item->com;
                         $comfinyear = $commissionfinace->years;
                         break;
                     } else {
                         if ($carpreemption->interest < $currentstepinterest) {
                             if ($previousstepcom != null) {
                                 $accountingdetail->comfinpercent = $previousstepcom;
                                 $accountingdetail->comfinyear = $commissionfinace->years;
                                 $comfinpercent = $previousstepcom;
                                 $comfinyear = $commissionfinace->years;
                             } else {
                                 $accountingdetail->comfinpercent = null;
                                 $accountingdetail->comfinyear = null;
                                 $comfinpercent = 0;
                                 $comfinyear = 0;
                             }
                             break;
                         } else {
                             if ($carpreemption->interest > $currentstepinterest) {
                                 $previousstepcom = $item->com;
                             }
                         }
                     }
                 }
                 if ($accountingdetail->comfinpercent == null) {
                     if ($previousstepcom != null) {
                         $accountingdetail->comfinpercent = $previousstepcom;
                         $accountingdetail->comfinyear = $commissionfinace->years;
                         $comfinpercent = $previousstepcom;
                         $comfinyear = $commissionfinace->years;
                     }
                 }
             }
         }
         $employee = Employee::find($carpreemption->salesmanemployeeid);
         $accountingdetail->salename = $employee->title . $employee->firstname . ' ' . $employee->lastname;
         $accountingdetail->incasefinaceinsurancefee = $carpreemption->insurancefee;
         $accountingdetail->note2insurancefeewhtax = $note1insurancefee / 100.0;
         $accountingdetail->note2insurancefee = $insurancefeefn;
         $note2insurancefeeexpense = $conditioninsurancefeecompanypaid > 0 ? $conditioninsurancefeecompanypaid : 0;
         $accountingdetail->note2insurancefeeexpense = $note2insurancefeeexpense;
         $note2insurancefeeincome = $conditioninsurancefeecompanypaid < 0 ? $conditioninsurancefeecompanypaid * -1 : 0;
         $accountingdetail->note2insurancefeeincome = $note2insurancefeeincome;
         $accountingdetail->incasefinacecompulsorymotorinsurancefee = $cartype->actpaidincludevat;
         $accountingdetail->note2compulsorymotorinsurancefeewhtax = $cartype->actpaid / 100.0;
         $accountingdetail->note2compulsorymotorinsurancefee = $compulsorymotorinsurancefeefn;
         $note2compulsorymotorinsurancefeeexpense = $conditioncompulsorymotorinsurancefeecompanypaid > 0 ? $conditioncompulsorymotorinsurancefeecompanypaid : 0;
         $accountingdetail->note2compulsorymotorinsurancefeeexpense = $note2compulsorymotorinsurancefeeexpense;
         $note2compulsorymotorinsurancefeeincome = $conditioncompulsorymotorinsurancefeecompanypaid < 0 ? $conditioncompulsorymotorinsurancefeecompanypaid * -1 : 0;
         $accountingdetail->note2compulsorymotorinsurancefeeincome = $note2compulsorymotorinsurancefeeincome;
         $accountingdetail->incasefinacefirstinstallmentpayamount = $firstinstallmentpayamount;
         $accountingdetail->note2firstinstallmentpayamount = $firstinstallmentpayamount;
         $accountingdetail->incasefinacepayinadvanceamount = $payinadvanceamount;
         $accountingdetail->note2payinadvanceamount = $payinadvanceamount;
         $accountingdetail->incasefinaceinsurancepremium = $carpayment->insurancepremium;
         $accountingdetail->note2insurancepremium = $carpayment->insurancepremium;
         $totalincasefinace = $carpreemption->insurancefee + $cartype->actpaidincludevat + $firstinstallmentpayamount + $payinadvanceamount + $carpayment->insurancepremium;
         $accountingdetail->totalincasefinace = $totalincasefinace;
         $accountingdetail->incasefinacereceivedcash = $yodjudwithinsurancepremium - $totalincasefinace;
         $note2total1 = $insurancefeefn + $compulsorymotorinsurancefeefn + $firstinstallmentpayamount + $payinadvanceamount + $carpayment->insurancepremium;
         $accountingdetail->note2total1 = $note2total1;
         $note2total2 = $note2insurancefeeexpense + $note2compulsorymotorinsurancefeeexpense;
         $accountingdetail->note2total2 = $note2total2;
         $note2total3 = $note2insurancefeeincome + $note2compulsorymotorinsurancefeeincome;
         $accountingdetail->note2total3 = $note2total3;
         $accountingdetail->incasefinacesubsidise = $carpreemption->subsidise;
         $incasefinacesubsidisewithvat = $carpreemption->subsidise + $carpreemption->subsidise * 0.07000000000000001;
         $accountingdetail->incasefinacesubsidisewithvat = $incasefinacesubsidisewithvat;
         $accountingdetail->note2subsidisewhtax = $carpreemption->subsidise * 0.03;
         $note2subsidisetotal = $carpreemption->subsidise + $carpreemption->subsidise * 0.07000000000000001 - $carpreemption->subsidise * 0.03;
         $accountingdetail->note2subsidisetotal = $note2subsidisetotal;
         $incasefinacehassubsidisereceivedcash = $yodjudwithinsurancepremium - $totalincasefinace - $incasefinacesubsidisewithvat;
         $accountingdetail->incasefinacehassubsidisereceivedcash = $incasefinacehassubsidisereceivedcash;
         $note2totalwhtax = $note1insurancefee / 100.0 + $cartype->actpaid / 100.0 + $carpreemption->subsidise * 0.03;
         $accountingdetail->note2totalwhtax = $note2totalwhtax;
         //NLTH,AYCAL,KL
         if ($carpreemption->finacecompanyid == 1 || $carpreemption->finacecompanyid == 2 || $carpreemption->finacecompanyid == 4) {
             $incasefinacecomfinamountwithvat = $yodjud * ($carpreemption->interest / 100.0) * $comfinyear * ($comfinpercent / 100.0);
             $incasefinacecomfinamount = $incasefinacecomfinamountwithvat / 1.07;
             $incasefinacecomfinvat = $incasefinacecomfinamountwithvat - $incasefinacecomfinamount;
         } else {
             if ($carpreemption->finacecompanyid == 3 || $carpreemption->finacecompanyid == 5) {
                 $incasefinacecomfinamount = floor($yodjud / 1.07 * ($carpreemption->interest / 100.0) * $comfinyear * ($comfinpercent / 100.0));
                 $incasefinacecomfinvat = $incasefinacecomfinamount * 0.07000000000000001;
                 $incasefinacecomfinamountwithvat = $incasefinacecomfinamount + $incasefinacecomfinvat;
             } else {
                 if ($carpreemption->finacecompanyid == 6) {
                     $incasefinacecomfinamount = round($yodjud / 1.07 * ($carpreemption->interest / 100.0) * $comfinyear * ($comfinpercent / 100.0), 2);
                     $incasefinacecomfinvat = $incasefinacecomfinamount * 0.07000000000000001;
                     $incasefinacecomfinamountwithvat = $incasefinacecomfinamount + $incasefinacecomfinvat;
                 } else {
                     if ($carpreemption->finacecompanyid == 7) {
                         $incasefinacecomfinamount = ($carpayment->amountperinstallment / 1.07 * $carpreemption->installments - $yodjud / 1.07) * ($comfinpercent / 100.0) * 48 / $carpreemption->installments;
                         $incasefinacecomfinvat = $incasefinacecomfinamount * 0.07000000000000001;
                         $incasefinacecomfinamountwithvat = $incasefinacecomfinamount + $incasefinacecomfinvat;
                     }
                 }
             }
         }
         $accountingdetail->incasefinacecomfinamount = $incasefinacecomfinamount;
         $accountingdetail->incasefinacecomfinvat = $incasefinacecomfinvat;
         $accountingdetail->incasefinacecomfinamountwithvat = $incasefinacecomfinamountwithvat;
         $accountingdetail->incasefinacecomfinwhtax = $incasefinacecomfinamount * 0.03;
         $incasefinacecomfintotal = $incasefinacecomfinamountwithvat - $incasefinacecomfinamount * 0.03;
         $accountingdetail->incasefinacecomfintotal = $incasefinacecomfintotal;
         $commissionextra = CommissionExtra::where('finacecompanyid', $carpreemption->finacecompanyid)->where('effectivefrom', '<=', $carpreemption->date)->where('effectiveto', '>=', $carpreemption->date)->whereHas('commissionExtraCars', function ($query) use($carpreemption) {
             $query->where('carmodelid', $carpreemption->carmodelid)->Where(function ($query) use($carpreemption) {
                 $query->where('carsubmodelid', $carpreemption->carsubmodelid)->orWhere('carsubmodelid', 0);
             });
         })->first();
         if ($commissionfinace != null) {
             $accountingdetail->incasefinacecomextraamount = $commissionextra->amount;
             $incasefinacecomextraamount = $commissionextra->amount;
             $accountingdetail->incasefinacecomextravat = $commissionextra->amount * 0.07000000000000001;
             $incasefinacecomextravat = $commissionextra->amount * 0.07000000000000001;
             $incasefinacecomextraamountwithvat = $commissionextra->amount + $commissionextra->amount * 0.07000000000000001;
             $accountingdetail->incasefinacecomextraamountwithvat = $incasefinacecomextraamountwithvat;
             $accountingdetail->incasefinacecomextrawhtax = $commissionextra->amount * 0.03;
             $incasefinacecomextrawhtax = $commissionextra->amount * 0.03;
             $incasefinacecomextratotal = $incasefinacecomextraamountwithvat - $commissionextra->amount * 0.03;
             $accountingdetail->incasefinacecomextratotal = $incasefinacecomextratotal;
         } else {
             $accountingdetail->incasefinacecomextraamount = 0;
             $incasefinacecomextraamount = 0;
             $accountingdetail->incasefinacecomextravat = 0;
             $incasefinacecomextravat = 0;
             $accountingdetail->incasefinacecomextraamountwithvat = 0;
             $accountingdetail->incasefinacecomextrawhtax = 0;
             $incasefinacecomextrawhtax = 0;
             $accountingdetail->incasefinacecomextratotal = 0;
             $incasefinacecomextratotal = 0;
         }
         if ($carpayment->insurancepremium > 0) {
             $commissionpa = CommissionPA::where('finacecompanyid', $carpreemption->finacecompanyid)->where('effectivefrom', '<=', $carpreemption->date)->where('effectiveto', '>=', $carpreemption->date)->first();
             if ($commissionpa != null) {
                 $accountingdetail->incasefinacecompaamount = $commissionpa->amount;
                 $incasefinacecompaamount = $commissionpa->amount;
                 $accountingdetail->incasefinacecompavat = $commissionpa->amount * 0.07000000000000001;
                 $incasefinacecompavat = $commissionpa->amount * 0.07000000000000001;
                 $incasefinacecompaamountwithvat = $commissionpa->amount + $commissionpa->amount * 0.07000000000000001;
                 $accountingdetail->incasefinacecompaamountwithvat = $incasefinacecompaamountwithvat;
                 $accountingdetail->incasefinacecompawhtax = $commissionpa->amount * 0.03;
                 $incasefinacecompawhtax = $commissionpa->amount * 0.03;
                 $incasefinacecompatotal = $incasefinacecompaamountwithvat - $commissionpa->amount * 0.03;
                 $accountingdetail->incasefinacecompatotal = $incasefinacecompatotal;
             } else {
                 $accountingdetail->incasefinacecompaamount = 0;
                 $incasefinacecompaamount = 0;
                 $accountingdetail->incasefinacecompavat = 0;
                 $incasefinacecompavat = 0;
                 $accountingdetail->incasefinacecompaamountwithvat = 0;
                 $accountingdetail->incasefinacecompawhtax = 0;
                 $incasefinacecompawhtax = 0;
                 $accountingdetail->incasefinacecompatotal = 0;
                 $incasefinacecompatotal = 0;
             }
         } else {
             $accountingdetail->incasefinacecompaamount = 0;
             $incasefinacecompaamount = 0;
             $accountingdetail->incasefinacecompavat = 0;
             $incasefinacecompavat = 0;
             $accountingdetail->incasefinacecompaamountwithvat = 0;
             $accountingdetail->incasefinacecompawhtax = 0;
             $incasefinacecompawhtax = 0;
             $accountingdetail->incasefinacecompatotal = 0;
             $incasefinacecompatotal = 0;
         }
         $incasefinacetotalcomamount = $incasefinacecomfinamount + $incasefinacecomextraamount + $incasefinacecompaamount;
         $accountingdetail->incasefinacetotalcomamount = $incasefinacetotalcomamount;
         $incasefinacetotalcomvat = $incasefinacecomfinamount * 0.07000000000000001 + $incasefinacecomextravat + $incasefinacecompavat;
         $accountingdetail->incasefinacetotalcomvat = $incasefinacetotalcomvat;
         $incasefinacetotalcomamountwithvat = $incasefinacetotalcomamount + $incasefinacetotalcomvat;
         $accountingdetail->incasefinacetotalcomamountwithvat = $incasefinacetotalcomamountwithvat;
         $incasefinacetotalcomwhtax = $incasefinacecomfinamount * 0.03 + $incasefinacecomextrawhtax + $incasefinacecompawhtax;
         $accountingdetail->incasefinacetotalcomwhtax = $incasefinacetotalcomwhtax;
         $incasefinacetotalcomtotal = $incasefinacecomfintotal + $incasefinacecomextratotal + $incasefinacecompatotal;
         $accountingdetail->incasefinacetotalcomtotal = $incasefinacetotalcomtotal;
         $receivedcashfromfinace = round($incasefinacehassubsidisereceivedcash, 2) + round($note2totalwhtax, 2) + round($incasefinacetotalcomtotal, 2);
         $accountingdetail->receivedcashfromfinace = $receivedcashfromfinace;
         $accountingdetail->receivedcashfromfinacenet = $receivedcashfromfinace;
         $accountingdetail->receivedcashfromfinaceshort = $receivedcashfromfinace;
         $accountingdetail->receivedcashfromfinacenetshort = $receivedcashfromfinace;
         $accountingdetail->receivedcashfromfinaceover = 0;
         $accountingdetail->receivedcashfromfinacenetover = 0;
     } else {
         $yodjud = 0;
         $yodjudwithinsurancepremium = 0;
         $accountingdetail->incasefinacereceivedcash = 0;
     }
     $tradereceivableaccount2amount = $totalcash - $yodjudwithinsurancepremium;
     $accountingdetail->tradereceivableaccount2amount = $tradereceivableaccount2amount;
     $oldcarprice = $carpreemption->oldcarprice;
     $accountingdetail->oldcarprice = $oldcarprice;
     $overdue = $carpayment->overdue;
     $accountingdetail->overdue = $overdue;
     $tradereceivableaccount2remainingamount = $tradereceivableaccount2amount - $oldcarprice - $overdue;
     $accountingdetail->tradereceivableaccount2remainingamount = $tradereceivableaccount2remainingamount;
     $accountingdetail->ins = $accountingdetail->note1insurancefeeincludevat;
     //$carpreemption->insurancefee;
     $accountingdetail->prb = $accountingdetail->note1compulsorymotorinsurancefeeincludevat;
     //$carpreemption->compulsorymotorinsurancefee;
     $accountingdetail->dc = $carpreemption->accessories + $carpreemption->subdown;
     $accountingdetail->totalacc2 = $tradereceivableaccount2remainingamount;
     $accountingdetail->totalacc2short = $tradereceivableaccount2remainingamount;
     $accountingdetail->totalacc2over = 0;
     $accountingdetail->totalaccount2 = $tradereceivableaccount2remainingamount;
     $accountingdetail->totalaccount2short = $tradereceivableaccount2remainingamount;
     $accountingdetail->totalaccount2over = 0;
     $arrNotFormatted = array("id", "purchasetype", "carpaymentid", "hasinsurancefee", "hascompulsorymotorinsurancefee", "hascashpledgeredlabel", "systemcalincasefinacecomfinamount", "systemcalincasefinacecomfinvat", "systemcalincasefinacecomfinamountwithvat", "systemcalincasefinacecomfinwhtax", "systemcalincasefinacecomfintotal", "receivedcashfromfinacenet", "receivedcashfromfinacenetshort", "receivedcashfromfinacenetover", "totalaccount1", "totalaccount1short", "totalaccount1over", "totalaccount2", "totalaccount2short", "totalaccount2over", 'totalaccounts', 'totalaccountsshort', 'totalaccountsover', "invoiceno", "additionalopenbill", "deliverycardate", "cashpledgeredlabelreceiptbookno", "cashpledgeredlabelreceiptno", "cashpledgeredlabelreceiptdate", "redlabelreturncashpledgedate", "cashpledgereceiptbookno", "cashpledgereceiptno", "cashpledgereceiptdate", "incasefinacecomfinamount", "incasefinacecomfinvat", "incasefinacecomfinamountwithvat", "incasefinacecomfinwhtax", "incasefinacecomfintotal", "oldcarcomamount", 'oldcarcomdate', "adj", "insurancefeereceiptcondition", "compulsorymotorinsurancefeereceiptcondition", "payinadvanceamountreimbursementdate", "payinadvanceamountreimbursementdocno", 'insurancebilldifferent', "note1insurancefeereceiptcondition", "note1compulsorymotorinsurancefeereceiptcondition", 'insurancefeepayment', 'insurancefeepaidseparatelydate', 'insurancepremiumnet', 'insurancepremiumcom', 'insurancefeepaidseparatelytotal', 'compulsorymotorinsurancefeepayment', 'compulsorymotorinsurancefeepaidseparatelydate', 'compulsorymotorinsurancepremiumnet', 'compulsorymotorinsurancepremiumcom', 'compulsorymotorinsurancefeepaidseparatelytotal', "carno", "installmentsinadvance", "installments", "comfinyear", "createdby", "createddate", "modifiedby", "modifieddate", 'actualinsurancefee');
     if ($donumberformat == 1) {
         foreach ($accountingdetail->toArray() as $key => $value) {
             if (!in_array($key, $arrNotFormatted)) {
                 if (is_numeric($value) && (double) $value != 0.0) {
                     $value = number_format($value, 2, '.', ',');
                 } else {
                     if ($value == null || $value == '' || is_numeric($value) && (double) $value == 0.0) {
                         $value = '-';
                     }
                 }
                 $accountingdetail->{$key} = $value;
             }
         }
     }
     return $accountingdetail;
 }
 public function view($id)
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $tempModel = AccountingDetail::find($id);
     $accountingdetail = (new CarPaymentController())->getforaccountingdetailbyid($tempModel->carpaymentid, 0);
     $accountingdetail->id = $tempModel->id;
     $accountingdetail->invoiceno = $tempModel->invoiceno;
     $accountingdetail->date = date('d-m-Y', strtotime($tempModel->date));
     $accountingdetail->additionalopenbill = $tempModel->additionalopenbill;
     $accountingdetail->insurancefeereceiptcondition = $tempModel->insurancefeereceiptcondition;
     if ($tempModel->payinadvanceamountreimbursementdate != null) {
         $accountingdetail->payinadvanceamountreimbursementdate = date('d-m-Y', strtotime($tempModel->payinadvanceamountreimbursementdate));
     } else {
         $accountingdetail->payinadvanceamountreimbursementdate = null;
     }
     $accountingdetail->payinadvanceamountreimbursementdocno = $tempModel->payinadvanceamountreimbursementdocno;
     $accountingdetail->insurancebilldifferent = $tempModel->insurancebilldifferent;
     if ($tempModel->insurancebilldifferent > 0) {
         $accountingdetail->note1insurancefeeincludevat = $accountingdetail->note1insurancefeeincludevat + $tempModel->insurancebilldifferent;
         $accountingdetail->note1insurancefee = $accountingdetail->note1insurancefeeincludevat * 100 / 107.0;
         $accountingdetail->note1insurancefeevat = $accountingdetail->note1insurancefeeincludevat - $accountingdetail->note1insurancefee;
         $accountingdetail->incasefinaceinsurancefee = $accountingdetail->incasefinaceinsurancefee + $tempModel->insurancebilldifferent;
         $accountingdetail->totalincasefinace = $accountingdetail->totalincasefinace + $tempModel->insurancebilldifferent;
         $accountingdetail->incasefinacereceivedcash = $accountingdetail->incasefinacereceivedcash - $tempModel->insurancebilldifferent;
         $accountingdetail->incasefinacehassubsidisereceivedcash = $accountingdetail->incasefinacehassubsidisereceivedcash - $tempModel->insurancebilldifferent;
         $accountingdetail->note2insurancefeewhtax = $accountingdetail->note1insurancefee / 100.0;
         $accountingdetail->ins = $accountingdetail->ins + $tempModel->insurancebilldifferent;
     }
     $accountingdetail->note1insurancefeereceiptcondition = $tempModel->note1insurancefeereceiptcondition;
     $accountingdetail->insurancefeepayment = $tempModel->insurancefeepayment;
     if ($tempModel->insurancefeepayment == 1) {
         $accountingdetail->note2insurancefeewhtax = 0;
         $accountingdetail->totalincasefinace = $accountingdetail->totalincasefinace - $accountingdetail->incasefinaceinsurancefee;
         $accountingdetail->incasefinacereceivedcash = $accountingdetail->incasefinacereceivedcash + $accountingdetail->incasefinaceinsurancefee;
         $accountingdetail->incasefinacehassubsidisereceivedcash = $accountingdetail->incasefinacehassubsidisereceivedcash + $accountingdetail->incasefinaceinsurancefee;
         $accountingdetail->incasefinaceinsurancefee = 0;
     }
     if ($tempModel->insurancefeepaidseparatelydate != null) {
         $accountingdetail->insurancefeepaidseparatelydate = date('d-m-Y', strtotime($tempModel->insurancefeepaidseparatelydate));
     } else {
         $accountingdetail->insurancefeepaidseparatelydate = null;
     }
     $accountingdetail->insurancepremiumnet = $tempModel->insurancepremiumnet;
     $accountingdetail->insurancepremiumcom = $tempModel->insurancepremiumcom;
     $accountingdetail->insurancefeepaidseparatelytotal = $tempModel->insurancefeepaidseparatelytotal;
     if ($tempModel->insurancefeereceiptcondition == 0 || $tempModel->insurancefeereceiptcondition == 1) {
         $accountingdetail->hasinsurancefee = 1;
         if ($tempModel->insurancefeereceiptcondition == 0) {
             $accountingdetail->note2insurancefeewhtax = 0;
             $accountingdetail->ins = 0;
         }
     } else {
         $accountingdetail->hasinsurancefee = 0;
     }
     $accountingdetail->compulsorymotorinsurancefeereceiptcondition = $tempModel->compulsorymotorinsurancefeereceiptcondition;
     $accountingdetail->note1compulsorymotorinsurancefeereceiptcondition = $tempModel->note1compulsorymotorinsurancefeereceiptcondition;
     $accountingdetail->compulsorymotorinsurancefeepayment = $tempModel->compulsorymotorinsurancefeepayment;
     if ($tempModel->compulsorymotorinsurancefeepayment == 1) {
         $accountingdetail->note2compulsorymotorinsurancefeewhtax = 0;
         $accountingdetail->totalincasefinace = $accountingdetail->totalincasefinace - $accountingdetail->incasefinacecompulsorymotorinsurancefee;
         $accountingdetail->incasefinacereceivedcash = $accountingdetail->incasefinacereceivedcash + $accountingdetail->incasefinacecompulsorymotorinsurancefee;
         $accountingdetail->incasefinacehassubsidisereceivedcash = $accountingdetail->incasefinacehassubsidisereceivedcash + $accountingdetail->incasefinacecompulsorymotorinsurancefee;
         $accountingdetail->incasefinacecompulsorymotorinsurancefee = 0;
     }
     if ($tempModel->compulsorymotorinsurancefeepaidseparatelydate != null) {
         $accountingdetail->compulsorymotorinsurancefeepaidseparatelydate = date('d-m-Y', strtotime($tempModel->compulsorymotorinsurancefeepaidseparatelydate));
     } else {
         $accountingdetail->compulsorymotorinsurancefeepaidseparatelydate = null;
     }
     $accountingdetail->compulsorymotorinsurancepremiumnet = $tempModel->compulsorymotorinsurancepremiumnet;
     $accountingdetail->compulsorymotorinsurancepremiumcom = $tempModel->compulsorymotorinsurancepremiumcom;
     $accountingdetail->compulsorymotorinsurancefeepaidseparatelytotal = $tempModel->compulsorymotorinsurancefeepaidseparatelytotal;
     if ($tempModel->compulsorymotorinsurancefeereceiptcondition == 0 || $tempModel->compulsorymotorinsurancefeereceiptcondition == 1) {
         $accountingdetail->hascompulsorymotorinsurancefee = 1;
         if ($tempModel->compulsorymotorinsurancefeereceiptcondition == 0) {
             $accountingdetail->note2compulsorymotorinsurancefeewhtax = 0;
             $accountingdetail->prb = 0;
         }
     } else {
         $accountingdetail->hascompulsorymotorinsurancefee = 0;
     }
     $note2totalwhtax = $accountingdetail->note2insurancefeewhtax + $accountingdetail->note2compulsorymotorinsurancefeewhtax + $accountingdetail->note2subsidisewhtax;
     $accountingdetail->note2totalwhtax = $note2totalwhtax;
     $accountingdetail->cashpledgeredlabelreceiptbookno = $tempModel->cashpledgeredlabelreceiptbookno;
     $accountingdetail->cashpledgeredlabelreceiptno = $tempModel->cashpledgeredlabelreceiptno;
     if ($tempModel->cashpledgeredlabelreceiptdate != null) {
         $accountingdetail->cashpledgeredlabelreceiptdate = date('d-m-Y', strtotime($tempModel->cashpledgeredlabelreceiptdate));
     } else {
         $accountingdetail->cashpledgeredlabelreceiptdate = null;
     }
     $accountingdetail->cashpledgereceiptbookno = $tempModel->cashpledgereceiptbookno;
     $accountingdetail->cashpledgereceiptno = $tempModel->cashpledgereceiptno;
     $accountingdetail->cashpledgereceiptdate = date('d-m-Y', strtotime($tempModel->cashpledgereceiptdate));
     $accountingdetail->oldcarcomamount = $tempModel->oldcarcomamount;
     if ($tempModel->oldcarcomdate != null) {
         $accountingdetail->oldcarcomdate = date('d-m-Y', strtotime($tempModel->oldcarcomdate));
     } else {
         $accountingdetail->oldcarcomdate = null;
     }
     $accountingdetail->adj = $tempModel->adj;
     $accountingdetail->incasefinacecomfinamount = $tempModel->incasefinacecomfinamount;
     $accountingdetail->incasefinacecomfinvat = $tempModel->incasefinacecomfinvat;
     $accountingdetail->incasefinacecomfinamountwithvat = $tempModel->incasefinacecomfinamountwithvat;
     $accountingdetail->incasefinacecomfinwhtax = $tempModel->incasefinacecomfinwhtax;
     $accountingdetail->incasefinacecomfintotal = $tempModel->incasefinacecomfintotal;
     $accountingdetail->systemcalincasefinacecomfinamount = $tempModel->systemcalincasefinacecomfinamount;
     $accountingdetail->systemcalincasefinacecomfinvat = $tempModel->systemcalincasefinacecomfinvat;
     $accountingdetail->systemcalincasefinacecomfinamountwithvat = $tempModel->systemcalincasefinacecomfinamountwithvat;
     $accountingdetail->systemcalincasefinacecomfinwhtax = $tempModel->systemcalincasefinacecomfinwhtax;
     $accountingdetail->systemcalincasefinacecomfintotal = $tempModel->systemcalincasefinacecomfintotal;
     $accountingdetail->receivedcashfromfinace = $tempModel->receivedcashfromfinacenet;
     $accountingdetail->receivedcashfromfinacenet = $tempModel->receivedcashfromfinacenet;
     $accountingdetail->receivedcashfromfinaceshort = $tempModel->receivedcashfromfinacenetshort;
     $accountingdetail->receivedcashfromfinacenetshort = $tempModel->receivedcashfromfinacenetshort;
     $accountingdetail->receivedcashfromfinaceover = $tempModel->receivedcashfromfinacenetover;
     $accountingdetail->receivedcashfromfinacenetover = $tempModel->receivedcashfromfinacenetover;
     $accountingdetail->totalacc1 = $tempModel->totalaccount1;
     $accountingdetail->totalaccount1 = $tempModel->totalaccount1;
     $accountingdetail->totalacc1short = $tempModel->totalaccount1short;
     $accountingdetail->totalaccount1short = $tempModel->totalaccount1short;
     $accountingdetail->totalacc1over = $tempModel->totalaccount1over;
     $accountingdetail->totalaccount1over = $tempModel->totalaccount1over;
     $accountingdetail->totalacc2 = $tempModel->totalaccount2;
     $accountingdetail->totalaccount2 = $tempModel->totalaccount2;
     $accountingdetail->totalacc2short = $tempModel->totalaccount2short;
     $accountingdetail->totalaccount2short = $tempModel->totalaccount2short;
     $accountingdetail->totalacc2over = $tempModel->totalaccount2over;
     $accountingdetail->totalaccount2over = $tempModel->totalaccount2over;
     $accountingdetail->totalaccs = $tempModel->totalaccounts;
     $accountingdetail->totalaccounts = $tempModel->totalaccounts;
     $accountingdetail->totalaccsshort = $tempModel->totalaccountsshort;
     $accountingdetail->totalaccountsshort = $tempModel->totalaccountsshort;
     $accountingdetail->totalaccsover = $tempModel->totalaccountsover;
     $accountingdetail->totalaccountsover = $tempModel->totalaccountsover;
     $finalopenbill = $accountingdetail->openbill;
     if ($accountingdetail->additionalopenbill != null && $accountingdetail->additionalopenbill > 0) {
         $finalopenbill = $finalopenbill + $accountingdetail->additionalopenbill;
     }
     if ($finalopenbill == 0) {
         $accountingdetail->finalopenbill = 0;
         $accountingdetail->vatoffinalopenbill = 0;
         $accountingdetail->finalopenbillwithoutvat = 0;
         $accountingdetail->realsalespricewithoutvat = $accountingdetail->realsalesprice;
     } else {
         $accountingdetail->finalopenbill = $finalopenbill;
         $vat = $finalopenbill * 7.0 / 107.0;
         $accountingdetail->vatoffinalopenbill = $vat;
         $accountingdetail->finalopenbillwithoutvat = $finalopenbill - $vat;
         $realsalesprice = $accountingdetail->realsalesprice;
         $accountingdetail->realsalespricewithoutvat = $realsalesprice - $vat;
     }
     $incasefinacereceivedcash = $accountingdetail->incasefinacereceivedcash;
     $tradereceivableaccount1amount = $finalopenbill - $incasefinacereceivedcash;
     $accountingdetail->tradereceivableaccount1amount = $tradereceivableaccount1amount;
     $accountingdetail->ar = $tradereceivableaccount1amount;
     $cash = $tradereceivableaccount1amount - $accountingdetail->ins - $accountingdetail->prb - $accountingdetail->dc + $accountingdetail->adj;
     $accountingdetail->cash = $cash;
     $carpayment = CarPayment::where('id', $tempModel->carpaymentid)->with('carpreemption')->first();
     $carpaymentselectlist = array();
     $carpaymentselectlist[$carpayment->id] = $carpayment->carpreemption->bookno . '/' . $carpayment->carpreemption->no;
     $banks = Bank::orderBy('accountgroup', 'asc')->orderBy('name', 'asc')->get();
     $bankselectlist = array();
     $bankselectlist[null] = 'เลือกบัญชี';
     foreach ($banks as $item) {
         $bankselectlist[$item->id] = $item->name . ' ' . substr($item->accountno, -4);
     }
     $bankselectlist2 = array();
     array_push($bankselectlist2, ':เลือกบัญชี');
     foreach ($banks as $item) {
         array_push($bankselectlist2, $item->id . ':' . $item->name . ' ' . substr($item->accountno, -4));
     }
     $receiveAndPayDatas0 = array();
     $receiveAndPayData0 = SupportRequest::old('receiveAndPayData0');
     if ($receiveAndPayData0 != null && $receiveAndPayData0 != '') {
         $receiveAndPayData0 = json_decode($receiveAndPayData0, true);
         foreach ($receiveAndPayData0 as $data) {
             $obj = (object) array("id" => $data["id"], "date" => $data["date"], "type" => $data["type"], "amount" => $data["amount"], "accountgroup" => $data["accountgroup"], "bankid" => $data["bankid"], "note" => $data["note"]);
             array_push($receiveAndPayDatas0, $obj);
         }
     } else {
         $receiveAndPays0 = AccountingDetailReceiveAndPay::where('accountingdetailid', $id)->where('sectiontype', 0)->get(['id', 'date', 'type', 'amount', 'accountgroup', 'bankid', 'note']);
         foreach ($receiveAndPays0 as $data) {
             $obj = (object) array("id" => $data->id, "date" => $data->date, "type" => $data->type, "amount" => $data->amount, "accountgroup" => $data->accountgroup, "bankid" => $data->bankid, "note" => $data->note);
             array_push($receiveAndPayDatas0, $obj);
         }
     }
     $receiveAndPayDatas1 = array();
     $receiveAndPayData1 = SupportRequest::old('receiveAndPayData1');
     if ($receiveAndPayData1 != null && $receiveAndPayData1 != '') {
         $receiveAndPayData1 = json_decode($receiveAndPayData1, true);
         foreach ($receiveAndPayData1 as $data) {
             $obj = (object) array("id" => $data["id"], "date" => $data["date"], "type" => $data["type"], "amount" => $data["amount"], "accountgroup" => $data["accountgroup"], "bankid" => $data["bankid"], "note" => $data["note"]);
             array_push($receiveAndPayDatas1, $obj);
         }
     } else {
         $receiveAndPays1 = AccountingDetailReceiveAndPay::where('accountingdetailid', $id)->where('sectiontype', 1)->get(['id', 'date', 'type', 'amount', 'accountgroup', 'bankid', 'note']);
         foreach ($receiveAndPays1 as $data) {
             $obj = (object) array("id" => $data->id, "date" => $data->date, "type" => $data->type, "amount" => $data->amount, "accountgroup" => $data->accountgroup, "bankid" => $data->bankid, "note" => $data->note);
             array_push($receiveAndPayDatas1, $obj);
         }
     }
     foreach ($accountingdetail->toArray() as $key => $value) {
         if (!in_array($key, $this->arrNotFormatted)) {
             if (is_numeric($value) && (double) $value != 0.0) {
                 $value = number_format($value, 2, '.', ',');
             } else {
                 if ($value == null || $value == '' || is_numeric($value) && (double) $value == 0.0) {
                     $value = '-';
                 }
             }
             $accountingdetail->{$key} = $value;
         }
     }
     return view('accountingdetailform', ['oper' => 'view', 'pathPrefix' => '../../', 'accountingdetail' => $accountingdetail, 'carpaymentselectlist' => $carpaymentselectlist, 'bankselectlist' => $bankselectlist, 'bankselectlist2' => implode(";", $bankselectlist2), 'receiveAndPayDatas0' => $receiveAndPayDatas0, 'receiveAndPayDatas1' => $receiveAndPayDatas1]);
 }
 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]);
 }