Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.60 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Classes\RouteCalc\CalcOptions;
  6. use App\Mail\LeadMail;
  7. use App\Mail\OrderMail;
  8. use App\Models\Entity;
  9. use App\Models\ExternalFormSettings;
  10. use App\Classes\Settings;
  11. use App\Models\Vehicle;
  12. use App\Models\VehicleType;
  13. use App\Services\ConvertDateService;
  14. use App\Services\ExternalFormService;
  15. use App\Services\RouteCalcService;
  16. use App\Services\VehicleService;
  17. use Carbon\Carbon;
  18. use Illuminate\Http\JsonResponse;
  19. use Illuminate\Http\Request;
  20. use App\Models\City;
  21. use Illuminate\Support\Facades\Mail;
  22. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  23. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  24.  
  25. /**
  26. * Class ExternalFormController
  27. *
  28. * @package App\Http\Controllers
  29. * @author Sergey Lazarev <sergey.lazarev@attractgroup.com>
  30. */
  31. class ExternalFormController extends Controller
  32. {
  33. private $path = 'external_form.';
  34.  
  35. private $service = null;
  36.  
  37. /**
  38. * ParseEmail constructor.
  39. *
  40. * @param ExternalFormService $service
  41. */
  42. public function __construct(
  43. ExternalFormService $service
  44. ) {
  45. $this->service = $service;
  46. \View::share('footerView', 'external_form._partials.footer');
  47. }
  48.  
  49. public function getIndex()
  50. {
  51. return view($this->path . 'index');
  52. }
  53.  
  54. public function getProcess($type)
  55. {
  56. $data = $this->service->getTypeConfig($type);
  57.  
  58. return view($this->path . 'process-vehicle', $data);
  59. }
  60.  
  61. public function postCheckInfo(Request $request)
  62. {
  63. if (!$request->filled('step')) {
  64. throw new AccessDeniedHttpException();
  65. }
  66.  
  67. if($request->get('anti_spm') && !empty($request->get('anti_spm'))){
  68. throw new AccessDeniedHttpException();
  69. }
  70.  
  71. $requestClass = $request->get('step');
  72. $split = explode('-', $requestClass);
  73. if (count($split) == 2) {
  74. $requestClass = ucfirst($split[0]) . ucfirst($split[1]) . 'Request';
  75. /**
  76. * @var $requestObject \App\Http\Requests\Request
  77. */
  78. $requestObject = app('App\\Http\\Requests\\External\\' . $requestClass);
  79. $requestObject->replace($request->all());
  80. $requestObject->validateResolved();
  81.  
  82. return new JsonResponse(['status' => 'OK'], 200);
  83. } else {
  84. throw new BadRequestHttpException();
  85. }
  86. }
  87.  
  88. public function postSave($type, Request $request, ConvertDateService $convertDateService)
  89. {
  90. $convertDateService->convertDates($request, ['ship_date']);
  91.  
  92. $saveData = $request->except([
  93. 'pickup_zip',
  94. 'pickup_city',
  95. 'pickup_state_id',
  96. 'delivery_zip',
  97. 'delivery_city',
  98. 'delivery_state_id',
  99. ]);
  100.  
  101. $this->service->getFeetInchString($saveData, 'height');
  102. $this->service->getFeetInchString($saveData, 'width');
  103. $this->service->getFeetInchString($saveData, 'length');
  104.  
  105. if (isset($saveData['weight_lbs']) && $saveData['weight_lbs'] > 0) {
  106. $saveData['weight'] = $saveData['weight_lbs'];
  107. $saveData['weight_str'] = $saveData['weight_lbs'] . ' lbs';
  108. }
  109.  
  110. $saveData['bike_type'] = ($saveData['bike_trike'] ?? 0) == 1
  111. ? 'trike'
  112. : (
  113. ($saveData['bike_sidecar'] ?? 0) == 1
  114. ? 'sidecar'
  115. : null
  116. );
  117.  
  118. $fieldsToNote = [
  119. 'length',
  120. 'width',
  121. 'height',
  122. 'weight',
  123. 'bike_type',
  124. 'trailer_not_road_worth',
  125. ];
  126.  
  127. $fieldsToNoteNames = [
  128. 'length' => 'Length',
  129. 'width' => 'Width',
  130. 'height' => 'Height',
  131. 'weight' => 'Weight',
  132. 'bike_type' => 'Bike type',
  133. 'trailer_not_road_worth' => 'Trailer not road worth',
  134. ];
  135.  
  136. // $processType = $saveData['process_type'] ?? 'car';
  137. $config = $this->service->getTypeConfig($type);
  138.  
  139. $vehicles = [];
  140.  
  141. if ($config['vehicleBlock'] == 'input') {
  142. for ($i = 1; $i <= ($saveData['vehicles_count'] ?? 1); $i++) {
  143. $fieldsToNote[] = 'vehicle_' . $i;
  144. $saveData["vehicle_{$i}_year"] = Carbon::now()->format('Y');
  145. $saveData["vehicle_{$i}_make"] = $config['make'];
  146. $saveData["vehicle_{$i}_model"] = $config['model'];
  147. $saveData["vehicle_{$i}_type"] = $config['vehicle_type'];
  148.  
  149. $vehicles[] = [
  150. 'year' => $saveData["vehicle_{$i}_year"],
  151. 'make' => $saveData["vehicle_{$i}_make"],
  152. 'model' => $saveData["vehicle_{$i}_model"],
  153. 'type' => $saveData["vehicle_{$i}_type"],
  154. ];
  155. }
  156. } else {
  157. for ($i = 1; $i <= ($saveData['vehicles_count'] ?? 1); $i++) {
  158. if (0 == $config['vehicle_type']) {
  159. /** @var VehicleService $vehicleService */
  160. $vehicleService = app(VehicleService::class);
  161. $realVehicleType = $vehicleService->detectVehicleType(
  162. $saveData["vehicle_{$i}_make"],
  163. $saveData["vehicle_{$i}_model"],
  164. VehicleType::TYPE_OTHER
  165. );
  166. } else {
  167. $realVehicleType = $config['vehicle_type'];
  168. }
  169.  
  170. $saveData["vehicle_{$i}_type"] = $realVehicleType;
  171.  
  172. $vehicles[] = [
  173. 'year' => $saveData["vehicle_{$i}_year"],
  174. 'make' => $saveData["vehicle_{$i}_make"],
  175. 'model' => $saveData["vehicle_{$i}_model"],
  176. 'type' => $saveData["vehicle_{$i}_type"],
  177. ];
  178. }
  179. }
  180.  
  181. $shipperNote = '';
  182. foreach ($fieldsToNote as $item) {
  183. if (isset($saveData[$item . '_str'])) {
  184. $shipperNote .= ($fieldsToNoteNames[$item] ?? $item) . ": " . $saveData[$item . '_str'] . "\n";
  185. } else if (isset($saveData[$item])) {
  186. $shipperNote .= ($fieldsToNoteNames[$item] ?? $item) . ": " . $saveData[$item] . "\n";
  187. }
  188. }
  189.  
  190. $saveData['notes_from_shipper'] = $shipperNote;
  191.  
  192. $pickupCity = City::find($saveData['pickup_zip_id']);
  193. $deliveryCity = City::find($saveData['delivery_zip_id']);
  194.  
  195. $saveData['pickup_city'] = $pickupCity->city;
  196. $saveData['pickup_zip'] = $pickupCity->zip;
  197. $saveData['pickup_state_id'] = optional($pickupCity->stateInfo)->id ?? '';
  198. $saveData['pickup_country_id'] = 233;
  199.  
  200. $saveData['delivery_city'] = $deliveryCity->city;
  201. $saveData['delivery_zip'] = $deliveryCity->zip;
  202. $saveData['delivery_state_id'] = optional($deliveryCity->stateInfo)->id ?? '';
  203. $saveData['delivery_country_id'] = 233;
  204.  
  205. $dataForCalc = [
  206. 'pickup_zip' => $saveData['pickup_zip'],
  207. 'delivery_zip' => $saveData['delivery_zip'],
  208. 'vehicle_types' => array_column($vehicles, 'type'),
  209. 'ship_via' => Entity::SHIP_VIA_OPEN,
  210. 'vehicle_run' => $saveData['vehicle_run'] ?? Entity::VEHICLE_RUN_YES,
  211. 'height' => $saveData['height'] ?? 0,
  212. 'width' => $saveData['width'] ?? 0,
  213. 'length' => $saveData['length'] ?? 0,
  214. 'boat_not_on_trailer' => $saveData['boat_not_on_trailer'] ?? 0,
  215. 'motocycle_type' => $saveData['bike_type'],
  216. 'trailer_not_road_worth' => ($saveData['trailer_not_road_worth'] ?? 'yes') == 'yes' ? 1 : 2,
  217. ];
  218.  
  219. /**
  220. * @var $routeCalcService RouteCalcService
  221. */
  222. $routeCalcService = app(RouteCalcService::class);
  223. $calcOptions = new CalcOptions();
  224. $calcOptions->returnFormatted = false;
  225. $calcOptions->returnSum = false;
  226. $calcResult = $routeCalcService->calcQuote($dataForCalc, $calcOptions);
  227.  
  228. $forOutput = [];
  229. if (!$calcResult->returnMessage) {
  230. $sumQuote = $calcResult->sumQuoteNoFormat;
  231.  
  232. $settings = ExternalFormSettings::firstOrNew([]);
  233.  
  234. $forOutput = [
  235. 'flexible' => ceil($sumQuote * $settings->quote_coefficients['flexible']),
  236. 'recommended' => ceil($sumQuote * $settings->quote_coefficients['recommended']),
  237. 'expedited' => ceil($sumQuote * $settings->quote_coefficients['expedited']),
  238. 'enclosed' => ceil($sumQuote * $settings->quote_coefficients['enclosed']),
  239. ];
  240.  
  241. $saveData['recommended_price'] = $sumQuote;
  242. }
  243.  
  244. /**
  245. * @var $oEntity Entity
  246. */
  247. $oEntity = new Entity();
  248. $oEntity->fill($saveData);
  249.  
  250.  
  251. $userId = null;
  252. $queue_name = null;
  253.  
  254. $oEntity->save();
  255.  
  256. for ($i = 1; $i <= ($saveData['vehicles_count'] ?? 1); $i++) {
  257. $oVehicle = new Vehicle();
  258. $oVehicle->year = $saveData['vehicle_' . $i . '_year'];
  259. $oVehicle->make = $saveData['vehicle_' . $i . '_make'];
  260. $oVehicle->model = $saveData['vehicle_' . $i . '_model'];
  261. $oVehicle->type = $saveData['vehicle_' . $i . '_type'];
  262.  
  263. $oVehicle->recommended_price = $calcResult->calcResults[$i - 1] ?? 0;
  264.  
  265. $oVehicle->name = $oEntity->id . '_V' . ($i);
  266. $oVehicle->entity_vehicle_num = $i;
  267. $oVehicle->entity_id = $oEntity->id;
  268.  
  269. $oVehicle->save();
  270. }
  271.  
  272. $oEntity = $oEntity->fresh(['vehicles_info']);
  273.  
  274. if($_SESSION['utm_sourcee'] == 'google') Mail::to(env('UTMLEAD_TO'))->send(new LeadMail($oEntity->id));
  275. else Mail::to(env('LEAD_TO'))->send(new LeadMail($oEntity->id));
  276.  
  277.  
  278. $termsConditionsText = Settings::$termCondition;;
  279.  
  280. if ($calcResult->returnMessage) {
  281. return view(
  282. $this->path . 'thankpage',
  283. [
  284. 'email' => $oEntity->shipper_email,
  285. ]
  286. );
  287. } else {
  288. \View::share('footerView', 'external_form._partials.footer_prices');
  289.  
  290. return view(
  291. $this->path . 'prices',
  292. [
  293. 'prices' => $forOutput,
  294. 'entity' => $oEntity,
  295. 'dateType' => $request->get('date_type', 'flexible'),
  296. 'termsConditionsText' => $termsConditionsText,
  297. ]
  298. );
  299. }
  300. }
  301.  
  302. public function getCheckout($name, $type = 'recommended', Request $request)
  303. {
  304. $oEntity = Entity::where('name', '=', $name)->first();
  305.  
  306. $termsAndConditions = Settings::$termCondition;
  307. \View::share('termsAndConditions', $termsAndConditions);
  308.  
  309. if ($oEntity) {
  310. $settings = ExternalFormSettings::firstOrNew([]);
  311.  
  312. $forOutput = [
  313. 'flexible' => ceil($oEntity->recommended_price * $settings->quote_coefficients['flexible']),
  314. 'recommended' => ceil($oEntity->recommended_price * $settings->quote_coefficients['recommended']),
  315. 'expedited' => ceil($oEntity->recommended_price * $settings->quote_coefficients['expedited']),
  316. 'enclosed' => ceil($oEntity->recommended_price * $settings->quote_coefficients['enclosed']),
  317. ];
  318.  
  319. if ($oEntity->type == Entity::TYPE_LEAD) {
  320. $oEntity->type = Entity::TYPE_QUOTE;
  321. }
  322.  
  323. $oEntity->flag = $type;
  324. $oEntity->quote_tariff = $forOutput[$type];
  325. $oEntity->save();
  326.  
  327. foreach($oEntity->vehicles_info as $vehicle) {
  328. $vehicle->tariff = ceil($vehicle->recommended_price * $settings->quote_coefficients[$type]);
  329. $vehicle->save();
  330. }
  331.  
  332. return view($this->path . 'checkout2', ['entity' => $oEntity, 'type' => $type]);
  333. } else {
  334. abort(404);
  335. }
  336. }
  337.  
  338. public function postCheckout($name, $type = 'recommended', Request $request)
  339. {
  340. $oEntity = Entity::where('name', '=', $name)->first();
  341.  
  342. $saveData = $request->all();
  343.  
  344. if ($oEntity) {
  345. $oEntity->fill($request->all());
  346.  
  347. $fieldsToNote = [
  348. 'pickup_phone1_ext',
  349. 'delivery_phone1_ext',
  350. ];
  351.  
  352. $fieldsToNoteNames = [
  353. 'pickup_phone1_ext' => 'Pickup phone ext',
  354. 'delivery_phone1_ext' => 'Delivery phone ext',
  355. ];
  356.  
  357. $shipperNote = $oEntity->notes_from_shipper ?? '';
  358. foreach ($fieldsToNote as $item) {
  359. if (isset($saveData[$item . '_str']) && trim($saveData[$item . '_str'])) {
  360. $shipperNote .= ($fieldsToNoteNames[$item] ?? $item) . ": " . $saveData[$item . '_str'] . "\n";
  361. } else if (isset($saveData[$item]) && trim($saveData[$item])) {
  362. $shipperNote .= ($fieldsToNoteNames[$item] ?? $item) . ": " . $saveData[$item] . "\n";
  363. }
  364. }
  365.  
  366. $shipperNote = 'Selected Package: ' . $type . "\n" . $shipperNote;
  367. $oEntity->notes_from_shipper = $shipperNote;
  368.  
  369. if ($oEntity->type != Entity::TYPE_ORDER) {
  370. $oEntity->type = Entity::TYPE_ORDER;
  371. }
  372. $oEntity->save();
  373.  
  374.  
  375. if($oEntity->type == Entity::TYPE_ORDER) {
  376.  
  377. if($_SESSION['utm_sourcee'] == 'google') Mail::to(env('UTMLEAD_TO'))->send(new OrderMail($oEntity->id));
  378. else Mail::to(env('LEAD_TO'))->send(new OrderMail($oEntity->id));
  379.  
  380. return view(
  381. $this->path . 'thankpage',
  382. [
  383. 'email' => $oEntity->shipper_email,
  384. 'entity' => $oEntity,
  385. ]
  386. );
  387. }
  388.  
  389.  
  390. return \Redirect::route(
  391. 'external_form_get_checkout',
  392. [
  393. 'name' => $name,
  394. 'type' => $type,
  395. ]
  396. );
  397. } else {
  398. abort(404);
  399. }
  400. }
  401.  
  402. // public function postCheckout2($name, Request $request)
  403. // {
  404. // $oEntity = Entity::where('name', '=', $name)->first();
  405. // dd($request);
  406. //
  407. // if ($oEntity) {
  408. // $oEntity->fill($request->all());
  409. // $oEntity->save();
  410. //
  411. // return view(
  412. // $this->path . 'thankpage',
  413. // [
  414. // 'email' => $oEntity->shipper_email,
  415. // 'entity' => $oEntity,
  416. // ]
  417. // );
  418. // } else {
  419. // abort(404);
  420. // }
  421. // }
  422.  
  423. public function getAutocompleteCity(Request $request)
  424. {
  425. $term = $request->input('name');
  426.  
  427. $limit = 10;
  428.  
  429. if (strlen($term) == City::ZIP_LENGHT && preg_match('/\d+/', $term)) {
  430. $aCities = City::where("zip", $term)->limit($limit)->get();
  431. } else {
  432. $params = [
  433. 'multi_match' => [
  434. 'query' => $term,
  435. "type" => "cross_fields",
  436. "operator" => "and",
  437. "fields" => ["city", "state", 'zip'],
  438. ],
  439. ];
  440.  
  441. if(!isset($aCities)) {
  442. $aCities = City::whereRaw("city LIKE '%" . $term . "%'")
  443. ->orWhereRaw("zip LIKE '%" . $term . "%'")
  444. ->limit($limit)->get();
  445. }
  446. }
  447.  
  448. return response()->json($aCities);
  449. }
  450.  
  451. public function getThankPage($name = '')
  452. {
  453. $oEntity = null;
  454. if ($name) {
  455. $oEntity = Entity::where('name', '=', $name)->first();
  456. }
  457.  
  458. if ($oEntity) {
  459. $oEntity = $oEntity->load(
  460. [
  461. 'pickupState',
  462. 'deliveryState',
  463. ]
  464. );
  465.  
  466. return view(
  467. $this->path . 'thankpage',
  468. [
  469. 'entity' => $oEntity,
  470. ]
  471. );
  472. } else {
  473. return view($this->path . 'thankpage');
  474. }
  475. }
  476. }
  477.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement