Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1.  
  2. $company = $user->company()->first();
  3. $district = $company->districts->first();
  4. $districtCode = $district ? $district->getCode() : null;
  5. $order = new Order();
  6. $order->company_id = $company->id;
  7. $order->name = Order::createOrderName($districtCode, Carbon::now(), Company::CONTRACTOR, false);
  8. $order->user_id = $user->id;
  9. $order->after_registration = 1;
  10. $order->status = Order::DRAFT;
  11. $order->start_date = Carbon::now()->setTime(0, 0, 0);
  12. $order->save();
  13.  
  14. if ($order->hasTransactions()) {
  15. return Response::json([], 409);
  16. }
  17.  
  18. Logentry::saveEntry('user.orderCreated', $order->id, "order");
  19.  
  20. $data['order'] = $order;
  21.  
  22. $drivers = $order->company->drivers()->get();
  23.  
  24. $nDrivers = count($drivers);
  25. if ($nDrivers == 0) {
  26. $driverOptions = [0 => '-- Hittade inga förare --'];
  27. } else {
  28. if ($nDrivers > 1 && count($order->orderItems) > 1) {
  29. $driverOptions = [
  30. 0 => '-- Välj förare --',
  31. ];
  32. } else {
  33. $driverOptions = [];
  34. }
  35.  
  36. foreach ($drivers as $driver) {
  37. $driverOptions[$driver->id] = $driver->fullname() . ' (' . $driver->roleName() . ')';
  38. }
  39. }
  40.  
  41. $data['driverOptionsTop'] = $driverOptions;
  42. unset($driverOptions[0], $driverOptions[-1]);
  43. $data['driverOptions'] = $driverOptions;
  44.  
  45. $people = $order->people;
  46. if (!$people->isEmpty()) {
  47. $data['currentPersons'] = $people->lists('id')->toArray();
  48. } else {
  49. $currentPerson = $user->person()->isDriver()->first();
  50. $data['currentPersons'] = $currentPerson != null ? $currentPerson->id : null;
  51. }
  52.  
  53.  
  54. $packageSelect = [];
  55. $areaSelect = [];
  56.  
  57. if ($order->after_registration) {
  58. $order->load('orderItems.customerOrderItem', 'orderItems.customerOrderItem.product',
  59. 'orderItems.customerOrderItem.area', 'orderItems.customerOrderItem.order');
  60. foreach ($order->orderItems as $item) {
  61. $company = $item->customerOrderItem->order->company;
  62. $cooperation = $company->cooperationInArea(
  63. $item->customerOrderItem->area_id
  64. );
  65.  
  66. if (is_null($cooperation)) {
  67. return Response::json([], 401);
  68. }
  69.  
  70. $contract = $cooperation->contract;
  71.  
  72. $packageSelect[$item->id] = Product::typeInContract(Product::PACKAGE, $contract)->listed();
  73. }
  74. }
  75.  
  76. $contracts = [];
  77. $jsContractsData = [];
  78. $allMaterialIds = [];
  79. $orderItemsData = [];
  80. $reRegisterData = [];
  81.  
  82. $reRegisterData['transport'] = (bool)$order->transport;
  83. $reRegisterData['product_id'] = $order->product_id;
  84. $reRegisterData['person_id'] = $order->person_id;
  85.  
  86. if ($order->person_id === null) {
  87. if ($user->person !== null) {
  88. $reRegisterData['person_id'] = $user->person->id;
  89. }
  90. }
  91.  
  92. $company = $order->company;
  93. $loadServices = 'services';
  94.  
  95. $order->load('orderItems',
  96. 'orderItems.customerOrderItem',
  97. 'orderItems.customerOrderItem.product');
  98. foreach ($order->orderItems as $orderItem) {
  99. $cooperation = $company->cooperationInArea($orderItem->area_id);
  100. if (!$cooperation) {
  101. continue;
  102. }
  103.  
  104. $contractId = $cooperation->contract_id;
  105.  
  106. // lazy load info for contracts
  107. if (!isset($contracts[$contractId])) {
  108. $contract = $cooperation->contract;
  109. $services = $contract->{$loadServices};
  110. $services->load('materials');
  111.  
  112. foreach ($services as $service) {
  113. $materialIds = $service->materials->lists('product_id')->all();
  114. $service->materialIds = $materialIds;
  115. // save all the material ids, we will use them later to load
  116. // all the materials needed for this setup.
  117. $allMaterialIds = array_merge($allMaterialIds, $materialIds);
  118. }
  119.  
  120. $allMaterialIds = array_unique($allMaterialIds);
  121. $contracts[$contractId] = $contract;
  122.  
  123. $servicesMap = [];
  124. foreach ($services as $service) {
  125. $servicesMap[$service->id] = [
  126. 'name' => $service->name,
  127. 'copy_of' => $service->copy_of,
  128. 'multiple_drivers' => $service->multiple_drivers,
  129. 'materialIds' => $service->materialIds,
  130. ];
  131. }
  132.  
  133. $jsContractsData[$contractId] = [
  134. 'name' => $contract->display_name,
  135. 'services' => $servicesMap
  136. ];
  137. }
  138.  
  139. $orderItem->contract_id = $contractId; // use this in the view.
  140.  
  141. $customerPackage = null;
  142. if (isset($orderItem->customerOrderItem->id)) {
  143. $customerPackage = $orderItem->customerOrderItem->product_id;
  144. }
  145.  
  146. $materials = [];
  147. $orderItemMaterials = $orderItem->productMaterials;//->pivot->lists('amount', 'product_material_id');
  148. foreach ($orderItemMaterials as $orderItemMaterial) {
  149. $materials[] = [
  150. $orderItemMaterial->pivot->product_material_id => round($orderItemMaterial->pivot->amount, 3)
  151. ];
  152. }
  153.  
  154. $start_datetime = $orderItem->start_datetime
  155. ? $orderItem->start_datetime->format('H:i')
  156. : null;
  157.  
  158. $stop_datetime = $orderItem->stop_datetime
  159. ? $orderItem->stop_datetime->format('H:i')
  160. : null;
  161.  
  162. // always set order item data for this contract
  163. $orderItemsData[$orderItem->id] = (object)[
  164. 'id' => $orderItem->id,
  165. 'contractId' => $contractId,
  166. 'areaId' => $orderItem->area_id,
  167. 'datetime' => (string)$orderItem->datetime,
  168. 'timeType' => $orderItem->time_type,
  169. 'package_id' => ($customerPackage ? $customerPackage : null),
  170.  
  171. // This is for when re-registering an order
  172. 'description' => $orderItem->description,
  173. 'start_time' => $start_datetime,
  174. 'stop_time' => $stop_datetime,
  175. 'materials' => $materials
  176. ];
  177. } // End foreach
  178.  
  179. $materials = [];
  180. if (count($allMaterialIds)) {
  181. $rows = DB::table('products')->whereIn('id', $allMaterialIds)->get(['name', 'unit', 'id']);
  182. foreach ($rows as &$row) {
  183. $row->unitName = Product::$productUnits[$row->unit] ?? "";
  184. $materials[$row->id] = $row;
  185. unset($row->id); // We don't need this again.
  186. }
  187. }
  188.  
  189. // Drop down with all base products in the two contracts.
  190. $productServiceOptions = [];
  191. $baseToCopyMapping = []; // mapping from copy to base product
  192. $copyToBaseMapping = [];
  193. $productServiceAccessories = [];
  194. $hasAccessory = false;
  195.  
  196. // Preselect the ordered product/service
  197. $orderedProductService = null;
  198. $productServiceBaseId = null;
  199. $selectedProductService = null;
  200. if (!$order->orderItems->isEmpty()) {
  201. if ($order->orderItems->first()->product_id) {
  202. $productServiceBaseId = $order->orderItems->first()->product->copy_of;
  203. }
  204.  
  205. foreach ($order->orderItems as $orderItem) {
  206. if (!$orderItem->productAccessories->isEmpty()) {
  207. $hasAccessory = true;
  208. }
  209. }
  210. }
  211.  
  212. foreach ($contracts as $contractId => $contract) {
  213. $services = $contract->services()->inSeason($order->season)->get();
  214. foreach ($services as $service) {
  215. if ($service->copy_of == $productServiceBaseId) {
  216. $selectedProductService = $service->id;
  217. }
  218. $productServiceOptions[$service->id] = $service->name;
  219. $baseToCopyMapping[$service->copy_of] = $service->id;
  220. $copyToBaseMapping[$service->id] = $service->copy_of;
  221.  
  222. foreach ($service->service->accessories()->get() as $accessory) {
  223. $productServiceAccessories[$service->id][$accessory->product->id] = $accessory->product->name;
  224. }
  225. }
  226. }
  227.  
  228. $startDate = $order->start_date ? $order->start_date->format('Y-m-d') : Carbon::now()->format('Y-m-d');
  229.  
  230. $jsData = [
  231. 'contracts' => $jsContractsData,
  232. 'materials' => &$materials,
  233. 'orderItems' => $orderItemsData,
  234. 'reRegisterData' => $reRegisterData,
  235. 'startDate' => $startDate,
  236. 'baseToCopyMapping' => $baseToCopyMapping,
  237. 'copyToBaseMapping' => $copyToBaseMapping
  238. ];
  239.  
  240. $data += [
  241. 'jsData' => $jsData,
  242. 'selectedProductService' => $selectedProductService,
  243. 'productServiceOptions' => $productServiceOptions,
  244. 'productServiceAccessories' => $productServiceAccessories,
  245. 'startDate' => $startDate,
  246. 'hasAccessory' => $hasAccessory,
  247. 'packageSelect' => $packageSelect,
  248. 'areaSelect' => $areaSelect
  249. ];
  250.  
  251. $cooperation = Cooperation::where('company_id', $user->company_id)->first();
  252.  
  253. $doRegisterWithNoItems = $order->after_registration;
  254. $data['doRegisterWithNoItems'] = $doRegisterWithNoItems;
  255. if($doRegisterWithNoItems) {
  256. $data['areaOptions'] = ['' => '-- Välj område --'] + $order->company->areasInActiveAcceptedCooperations();
  257. }
  258.  
  259. return Response::json($data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement