Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $company = $user->company()->first();
- $district = $company->districts->first();
- $districtCode = $district ? $district->getCode() : null;
- $order = new Order();
- $order->company_id = $company->id;
- $order->name = Order::createOrderName($districtCode, Carbon::now(), Company::CONTRACTOR, false);
- $order->user_id = $user->id;
- $order->after_registration = 1;
- $order->status = Order::DRAFT;
- $order->start_date = Carbon::now()->setTime(0, 0, 0);
- $order->save();
- if ($order->hasTransactions()) {
- return Response::json([], 409);
- }
- Logentry::saveEntry('user.orderCreated', $order->id, "order");
- $data['order'] = $order;
- $drivers = $order->company->drivers()->get();
- $nDrivers = count($drivers);
- if ($nDrivers == 0) {
- $driverOptions = [0 => '-- Hittade inga förare --'];
- } else {
- if ($nDrivers > 1 && count($order->orderItems) > 1) {
- $driverOptions = [
- 0 => '-- Välj förare --',
- ];
- } else {
- $driverOptions = [];
- }
- foreach ($drivers as $driver) {
- $driverOptions[$driver->id] = $driver->fullname() . ' (' . $driver->roleName() . ')';
- }
- }
- $data['driverOptionsTop'] = $driverOptions;
- unset($driverOptions[0], $driverOptions[-1]);
- $data['driverOptions'] = $driverOptions;
- $people = $order->people;
- if (!$people->isEmpty()) {
- $data['currentPersons'] = $people->lists('id')->toArray();
- } else {
- $currentPerson = $user->person()->isDriver()->first();
- $data['currentPersons'] = $currentPerson != null ? $currentPerson->id : null;
- }
- $packageSelect = [];
- $areaSelect = [];
- if ($order->after_registration) {
- $order->load('orderItems.customerOrderItem', 'orderItems.customerOrderItem.product',
- 'orderItems.customerOrderItem.area', 'orderItems.customerOrderItem.order');
- foreach ($order->orderItems as $item) {
- $company = $item->customerOrderItem->order->company;
- $cooperation = $company->cooperationInArea(
- $item->customerOrderItem->area_id
- );
- if (is_null($cooperation)) {
- return Response::json([], 401);
- }
- $contract = $cooperation->contract;
- $packageSelect[$item->id] = Product::typeInContract(Product::PACKAGE, $contract)->listed();
- }
- }
- $contracts = [];
- $jsContractsData = [];
- $allMaterialIds = [];
- $orderItemsData = [];
- $reRegisterData = [];
- $reRegisterData['transport'] = (bool)$order->transport;
- $reRegisterData['product_id'] = $order->product_id;
- $reRegisterData['person_id'] = $order->person_id;
- if ($order->person_id === null) {
- if ($user->person !== null) {
- $reRegisterData['person_id'] = $user->person->id;
- }
- }
- $company = $order->company;
- $loadServices = 'services';
- $order->load('orderItems',
- 'orderItems.customerOrderItem',
- 'orderItems.customerOrderItem.product');
- foreach ($order->orderItems as $orderItem) {
- $cooperation = $company->cooperationInArea($orderItem->area_id);
- if (!$cooperation) {
- continue;
- }
- $contractId = $cooperation->contract_id;
- // lazy load info for contracts
- if (!isset($contracts[$contractId])) {
- $contract = $cooperation->contract;
- $services = $contract->{$loadServices};
- $services->load('materials');
- foreach ($services as $service) {
- $materialIds = $service->materials->lists('product_id')->all();
- $service->materialIds = $materialIds;
- // save all the material ids, we will use them later to load
- // all the materials needed for this setup.
- $allMaterialIds = array_merge($allMaterialIds, $materialIds);
- }
- $allMaterialIds = array_unique($allMaterialIds);
- $contracts[$contractId] = $contract;
- $servicesMap = [];
- foreach ($services as $service) {
- $servicesMap[$service->id] = [
- 'name' => $service->name,
- 'copy_of' => $service->copy_of,
- 'multiple_drivers' => $service->multiple_drivers,
- 'materialIds' => $service->materialIds,
- ];
- }
- $jsContractsData[$contractId] = [
- 'name' => $contract->display_name,
- 'services' => $servicesMap
- ];
- }
- $orderItem->contract_id = $contractId; // use this in the view.
- $customerPackage = null;
- if (isset($orderItem->customerOrderItem->id)) {
- $customerPackage = $orderItem->customerOrderItem->product_id;
- }
- $materials = [];
- $orderItemMaterials = $orderItem->productMaterials;//->pivot->lists('amount', 'product_material_id');
- foreach ($orderItemMaterials as $orderItemMaterial) {
- $materials[] = [
- $orderItemMaterial->pivot->product_material_id => round($orderItemMaterial->pivot->amount, 3)
- ];
- }
- $start_datetime = $orderItem->start_datetime
- ? $orderItem->start_datetime->format('H:i')
- : null;
- $stop_datetime = $orderItem->stop_datetime
- ? $orderItem->stop_datetime->format('H:i')
- : null;
- // always set order item data for this contract
- $orderItemsData[$orderItem->id] = (object)[
- 'id' => $orderItem->id,
- 'contractId' => $contractId,
- 'areaId' => $orderItem->area_id,
- 'datetime' => (string)$orderItem->datetime,
- 'timeType' => $orderItem->time_type,
- 'package_id' => ($customerPackage ? $customerPackage : null),
- // This is for when re-registering an order
- 'description' => $orderItem->description,
- 'start_time' => $start_datetime,
- 'stop_time' => $stop_datetime,
- 'materials' => $materials
- ];
- } // End foreach
- $materials = [];
- if (count($allMaterialIds)) {
- $rows = DB::table('products')->whereIn('id', $allMaterialIds)->get(['name', 'unit', 'id']);
- foreach ($rows as &$row) {
- $row->unitName = Product::$productUnits[$row->unit] ?? "";
- $materials[$row->id] = $row;
- unset($row->id); // We don't need this again.
- }
- }
- // Drop down with all base products in the two contracts.
- $productServiceOptions = [];
- $baseToCopyMapping = []; // mapping from copy to base product
- $copyToBaseMapping = [];
- $productServiceAccessories = [];
- $hasAccessory = false;
- // Preselect the ordered product/service
- $orderedProductService = null;
- $productServiceBaseId = null;
- $selectedProductService = null;
- if (!$order->orderItems->isEmpty()) {
- if ($order->orderItems->first()->product_id) {
- $productServiceBaseId = $order->orderItems->first()->product->copy_of;
- }
- foreach ($order->orderItems as $orderItem) {
- if (!$orderItem->productAccessories->isEmpty()) {
- $hasAccessory = true;
- }
- }
- }
- foreach ($contracts as $contractId => $contract) {
- $services = $contract->services()->inSeason($order->season)->get();
- foreach ($services as $service) {
- if ($service->copy_of == $productServiceBaseId) {
- $selectedProductService = $service->id;
- }
- $productServiceOptions[$service->id] = $service->name;
- $baseToCopyMapping[$service->copy_of] = $service->id;
- $copyToBaseMapping[$service->id] = $service->copy_of;
- foreach ($service->service->accessories()->get() as $accessory) {
- $productServiceAccessories[$service->id][$accessory->product->id] = $accessory->product->name;
- }
- }
- }
- $startDate = $order->start_date ? $order->start_date->format('Y-m-d') : Carbon::now()->format('Y-m-d');
- $jsData = [
- 'contracts' => $jsContractsData,
- 'materials' => &$materials,
- 'orderItems' => $orderItemsData,
- 'reRegisterData' => $reRegisterData,
- 'startDate' => $startDate,
- 'baseToCopyMapping' => $baseToCopyMapping,
- 'copyToBaseMapping' => $copyToBaseMapping
- ];
- $data += [
- 'jsData' => $jsData,
- 'selectedProductService' => $selectedProductService,
- 'productServiceOptions' => $productServiceOptions,
- 'productServiceAccessories' => $productServiceAccessories,
- 'startDate' => $startDate,
- 'hasAccessory' => $hasAccessory,
- 'packageSelect' => $packageSelect,
- 'areaSelect' => $areaSelect
- ];
- $cooperation = Cooperation::where('company_id', $user->company_id)->first();
- $doRegisterWithNoItems = $order->after_registration;
- $data['doRegisterWithNoItems'] = $doRegisterWithNoItems;
- if($doRegisterWithNoItems) {
- $data['areaOptions'] = ['' => '-- Välj område --'] + $order->company->areasInActiveAcceptedCooperations();
- }
- return Response::json($data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement