public function index()
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $cartypes = CarType::orderBy('name', 'asc')->get(['id', 'name']);
     $cartypeselectlist = array();
     array_push($cartypeselectlist, ':เลือกประเภทรถ');
     foreach ($cartypes as $ct) {
         array_push($cartypeselectlist, $ct->id . ':' . $ct->name);
     }
     $carbrands = CarBrand::orderBy('name', 'asc')->get(['id', 'name']);
     $carbrandselectlist = array();
     array_push($carbrandselectlist, ':เลือกยี่ห้อรถ');
     foreach ($carbrands as $item) {
         array_push($carbrandselectlist, $item->id . ':' . $item->name);
     }
     $colors = Color::orderBy('code', 'asc')->orderBy('name', 'asc')->get(['id', 'code', 'name']);
     $colorselectlist = array();
     array_push($colorselectlist, ':เลือกสี');
     foreach ($colors as $item) {
         array_push($colorselectlist, $item->id . ':' . $item->code . ' - ' . $item->name);
     }
     $provinces = Province::orderBy('name', 'asc')->get(['id', 'name']);
     $provinceselectlist = array();
     array_push($provinceselectlist, ':เลือกจังหวัด');
     foreach ($provinces as $item) {
         array_push($provinceselectlist, $item->id . ':' . $item->name);
     }
     $defaultCarBrand = '';
     $carBrand = CarBrand::where('name', "NISSAN")->first();
     if ($carBrand != null) {
         $defaultCarBrand = $carBrand->id;
     }
     return view('settings.carmodel', ['cartypeselectlist' => implode(";", $cartypeselectlist), 'carbrandselectlist' => implode(";", $carbrandselectlist), 'colorselectlist' => implode(";", $colorselectlist), 'provinceselectlist' => implode(";", $provinceselectlist), 'defaultCarBrand' => $defaultCarBrand]);
 }
 public function import()
 {
     try {
         $file = Input::file('pricelist');
         //$path = Input::file('pricelist')->getRealPath();
         $temp = null;
         Excel::load($file, function ($reader) use($temp) {
             //$reader->dump();
             $reader->skip(1);
             // Loop through all rows
             $reader->each(function ($row) {
                 $carBrand = CarBrand::where('name', 'NISSAN')->first();
                 $carModel = CarModel::firstOrCreate(['name' => trim($row->d) . ' ' . trim($row->e), 'cartypeid' => $row->c, 'carbrandid' => $carBrand->id]);
                 $carSubModel = CarSubModel::firstOrCreate(['code' => trim($row->g), 'name' => trim($row->f), 'carmodelid' => $carModel->id]);
                 $pricelist = Pricelist::firstOrNew(['carmodelid' => $carModel->id, 'carsubmodelid' => $carSubModel->id, 'effectivefrom' => date('Y-m-d', strtotime(trim($row->a))), 'effectiveto' => date('Y-m-d', strtotime(trim($row->b))), 'sellingprice' => $row->i, 'accessoriesprice' => $row->j, 'sellingpricewithaccessories' => $row->h, 'margin' => $row->k, 'ws50' => $row->l, 'dms' => $row->m, 'wholesale' => $row->n, 'execusiveinternal' => $row->o, 'execusivecampaing' => $row->p, 'execusivetotalmargincampaing' => $row->q, 'internal' => $row->r, 'campaing' => $row->s, 'totalmargincampaing' => $row->t]);
                 $pricelist->effectivefrom = trim($row->a);
                 $pricelist->effectiveto = trim($row->b);
                 $pricelist->save();
             });
         });
     } catch (Exception $e) {
         return 'Message: ' . $e->getMessage();
     }
     return redirect()->action('Settings\\PricelistController@index');
 }
 public function import()
 {
     try {
         $file = Input::file('file');
         //$path = Input::file('pricelist')->getRealPath();
         $temp = null;
         Excel::load($file, function ($reader) use($temp) {
             //$reader->dump();
             $reader->skip(1);
             // Loop through all rows
             $reader->each(function ($row) {
                 $carBrand = CarBrand::where('name', 'NISSAN')->first();
                 $color = Color::where('code', trim($row->p))->first();
                 $carModel = CarModel::firstOrCreate(['name' => trim($row->c) . ' ' . trim($row->d), 'cartypeid' => $row->b, 'carbrandid' => $carBrand->id]);
                 $carSubModel = CarSubModel::firstOrCreate(['code' => trim($row->f), 'name' => trim($row->e), 'carmodelid' => $carModel->id]);
                 $car = Car::firstOrNew(['engineno' => trim($row->m), 'chassisno' => trim($row->n)]);
                 $car->datatype = 0;
                 $car->provinceid = trim($row->a);
                 $car->carmodelid = $carModel->id;
                 $car->carsubmodelid = $carSubModel->id;
                 $car->receivetype = trim($row->g);
                 $car->dealername = trim($row->h);
                 $car->no = trim($row->i);
                 $car->dodate = trim($row->j);
                 $car->dono = trim($row->k);
                 if ($row->l != null && $row->l != '') {
                     $car->receiveddate = trim($row->l);
                 }
                 if ($row->o != null && $row->o != '') {
                     $car->keyno = trim($row->o);
                 }
                 $car->colorid = $color->id;
                 $car->objective = trim($row->q);
                 if ($row->r != null && $row->r != '') {
                     $car->parklocation = trim($row->r);
                 }
                 if ($row->s != null && $row->s != '') {
                     $car->notifysolddate = trim($row->s);
                 }
                 if ($row->u != null && $row->u != '' && $row->v != null && $row->v != '') {
                     $customer = Customer::firstOrNew(['provinceid' => trim($row->a), 'title' => trim($row->u), 'firstname' => trim($row->v), 'lastname' => trim($row->w)]);
                     $customer->isreal = true;
                     if ($row->x != null && $row->x != '') {
                         $customer->phone1 = trim($row->x);
                     }
                     if ($row->y != null && $row->y != '') {
                         $customer->occupationid = trim($row->y);
                     }
                     if ($row->z != null && $row->z != '') {
                         $customer->birthdate = trim($row->z);
                     }
                     if ($row->aa != null && $row->aa != '') {
                         $customer->address = trim($row->aa);
                     }
                     $district = District::where('name', trim($row->ab))->first();
                     $amphur = Amphur::where('name', trim($row->ac))->first();
                     $province = Province::where('name', trim($row->ad))->first();
                     if ($district != null) {
                         $customer->districtid = $district->id;
                     }
                     if ($amphur != null) {
                         $customer->amphurid = $amphur->id;
                     }
                     if ($province != null) {
                         $customer->addprovinceid = $province->id;
                     }
                     if ($row->ad != null && $row->ad != '') {
                         $customer->zipcode = trim($row->ae);
                     }
                     $customer->save();
                     $car->issold = true;
                     $car->buyercustomerid = $customer->id;
                     if ($row->t != null && $row->t != '') {
                         $car->isdelivered = true;
                     }
                 }
                 $car->save();
             });
         });
     } catch (Exception $e) {
         return 'Message: ' . $e->getMessage();
     }
     return redirect()->action('CarController@index');
 }
 public function view($id)
 {
     if (!$this->hasPermission($this->menuPermissionName)) {
         return view($this->viewPermissiondeniedName);
     }
     $model = CarPreemption::find($id);
     $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) {
         $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));
         }
     }
     $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;
     }
     $customerselectlist = array();
     $customerselectlist[$bookingcustomer->id] = $bookingcustomer->title . ' ' . $bookingcustomer->firstname . ' ' . $bookingcustomer->lastname;
     if ($model->bookingcustomerid != $model->buyercustomerid) {
         $customerselectlist[$buyercustomer->id] = $buyercustomer->title . ' ' . $buyercustomer->firstname . ' ' . $buyercustomer->lastname;
     }
     $provinces = Province::orderBy('name', 'asc')->get(['id', 'name']);
     $provinceselectlist = array();
     $provinceselectlist[null] = 'เลือกจังหวัด';
     foreach ($provinces as $item) {
         $provinceselectlist[$item->id] = $item->name;
     }
     $bookingcustomeramphurselectlist = array();
     $bookingcustomeramphurselectlist[null] = 'เลือกเขต/อำเภอ';
     if ($model->bookingcustomeramphurid != null && $model->bookingcustomeramphurid != '') {
         $item = Amphur::find($model->bookingcustomeramphurid);
         $bookingcustomeramphurselectlist[$item->id] = $item->name;
     }
     $buyercustomeramphurselectlist = array();
     $buyercustomeramphurselectlist[null] = 'เลือกเขต/อำเภอ';
     if ($model->bookingcustomerid != $model->buyercustomerid && $model->buyercustomeramphurid != null && $model->buyercustomeramphurid != '') {
         $item = Amphur::find($model->buyercustomeramphurid);
         $buyercustomeramphurselectlist[$item->id] = $item->name;
     }
     $bookingcustomerdistrictselectlist = array();
     $bookingcustomerdistrictselectlist[null] = 'เลือกแขวง/ตำบล';
     if ($model->bookingcustomerdistrictid != null && $model->bookingcustomerdistrictid != '') {
         $item = District::find($model->bookingcustomerdistrictid);
         $bookingcustomerdistrictselectlist[$item->id] = $item->name;
     }
     $buyercustomerdistrictselectlist = array();
     $buyercustomerdistrictselectlist[null] = 'เลือกแขวง/ตำบล';
     if ($model->bookingcustomerid != $model->buyercustomerid && $model->buyercustomerdistrictid != null && $model->buyercustomerdistrictid != '') {
         $item = District::find($model->buyercustomerdistrictid);
         $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;
     }
     $carmodelselectlist = array();
     $item = CarModel::find($model->carmodelid);
     $carmodelselectlist[$item->id] = $item->name;
     $carsubmodelselectlist = array();
     $item = CarSubModel::find($model->carsubmodelid);
     $carsubmodelselectlist[$item->id] = $item->name;
     $colorselectlist = array();
     $item = Color::find($model->colorid);
     $colorselectlist[$item->id] = $item->code . ' - ' . $item->name;
     $registerprovinceselectlist = array();
     if ($model->registerprovinceid != null && $model->registerprovinceid != '') {
         $item = Province::find($model->registerprovinceid);
         $registerprovinceselectlist[$item->id] = $item->name;
     }
     $oldcarbrandselectlist = array();
     $oldcarbrandselectlist[null] = 'เลือกยี่ห้อรถ';
     if ($model->oldcarbrandid != null) {
         $item = CarBrand::find($model->oldcarbrandid);
         $oldcarbrandselectlist[$item->id] = $item->name;
     }
     $oldcarmodelselectlist = array();
     $oldcarmodelselectlist[null] = 'เลือกแบบ';
     if ($model->oldcarmodelid != null) {
         $item = CarModel::find($model->oldcarmodelid);
         $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 . ' บาท)');
     }
     $giveawayFrees = CarPreemptionGiveaway::where('free', true)->where('carpreemptionid', $id)->get(['id', 'giveawayid', 'price']);
     $giveawayFreeDatas = array();
     foreach ($giveawayFrees as $data) {
         $obj = (object) array("id" => $data->id, "giveawayid" => $data->giveawayid, "price" => $data->price);
         array_push($giveawayFreeDatas, $obj);
     }
     $giveawayBuys = CarPreemptionGiveaway::where('free', false)->where('carpreemptionid', $id)->get(['id', 'giveawayid']);
     $giveawayBuyDatas = array();
     foreach ($giveawayBuys as $data) {
         $obj = (object) array("id" => $data->id, "giveawayid" => $data->giveawayid);
         array_push($giveawayBuyDatas, $obj);
     }
     $finacecompanyselectlist = array();
     array_push($finacecompanyselectlist, ':เลือกบริษัท');
     if ($model->purchasetype == 1) {
         $item = FinaceCompany::find($model->finacecompanyid);
         $finacecompanyselectlist[$item->id] = $item->name;
     }
     $interestratetypeselectlist = array();
     array_push($interestratetypeselectlist, ':เลือกประเภทอัตราดอกเบี้ย');
     if ($model->purchasetype == 1) {
         $item = InterestRateType::find($model->interestratetypeid);
         $interestratetypeselectlist[$item->id] = $item->name;
     }
     $priceselectlist = array();
     $item = Pricelist::find($model->pricelistid);
     if ($item->promotion != null && $item->promotion != '') {
         $priceselectlist[$item->id] = $item->sellingpricewithaccessories . ' (' . $item->promotion . ')';
     } else {
         $priceselectlist[$item->id] = $item->sellingpricewithaccessories;
     }
     $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' => 'view', 'pathPrefix' => '../../', 'carpreemption' => $model, 'carprices' => array(), 'colorprices' => array(), 'provinceregistrationfee' => array(), '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]);
 }