/** * Creates a new Order Entity. * * @param array $data The data array, which will be used for setting the orders data * @param string $locale Locale * @param int $userId Id of the User, which is is saved as creator / changer * @param int|null $id If defined, the Order with the given ID will be updated * @param int|null $statusId if defined, the status will be set to the given value * @param bool $flush Defines if a flush should be performed * @param bool $patch * * @throws EntityNotFoundException * @throws MissingOrderAttributeException * @throws OrderDependencyNotFoundException * @throws OrderException * @throws OrderNotFoundException * * @return null|Order */ public function save(array $data, $locale, $userId = null, $id = null, $statusId = null, $flush = true, $patch = true) { $isNewOrder = !$id; if (!$isNewOrder) { $order = $this->findByIdAndLocale($id, $locale); if (!$order) { throw new OrderNotFoundException($id); } } else { $order = $this->orderFactory->createApiEntity($this->orderFactory->createEntity(), $locale); $this->checkRequiredData($data, $id === null); } $user = $userId ? $this->userRepository->findUserById($userId) : null; $order->setOrderNumber($this->getPropertyBasedOnPatch($data, 'orderNumber', $order->getOrderNumber(), $patch)); $order->setCurrencyCode($this->getPropertyBasedOnPatch($data, 'currencyCode', $order->getCurrencyCode(), $patch)); $order->setCostCentre($this->getPropertyBasedOnPatch($data, 'costCentre', $order->getCostCentre(), $patch)); $order->setCommission($this->getPropertyBasedOnPatch($data, 'commission', $order->getCommission(), $patch)); $order->setTaxfree($this->getPropertyBasedOnPatch($data, 'taxfree', $order->getTaxfree(), $patch)); $order->setNetShippingCosts($this->getPropertyBasedOnPatch($data, 'netShippingCosts', $order->getNetShippingCosts(), $patch)); $order->setInternalNote($this->getPropertyBasedOnPatch($data, 'internalNote', $order->getInternalNote(), $patch)); // Set type of order (if set). $this->setOrderType($data, $order, $patch); $this->setDate($data, 'desiredDeliveryDate', $order->getDesiredDeliveryDate(), array($order, 'setDesiredDeliveryDate')); $this->setDate($data, 'orderDate', $order->getOrderDate(), array($order, 'setOrderDate')); $this->setTermsOfDelivery($data, $order, $patch); $this->setTermsOfPayment($data, $order, $patch); $account = $this->setCustomerAccount($data, $order, $patch); // Set session - id. $sessionId = $this->session->getId(); $order->setSessionId($sessionId); // Add contact. $contact = $this->addContactRelation($data, 'customerContact', function ($contact) use($order) { $order->setCustomerContact($contact); }); // Add contact. $this->addContactRelation($data, 'responsibleContact', function ($contact) use($order) { $order->setResponsibleContact($contact); }); // Create order (POST). if ($order->getId() == null) { $order->setCreated(new DateTime()); $order->setCreator($user); $this->em->persist($order->getEntity()); // Set status to created if not defined. if ($statusId === null) { $statusId = OrderStatus::STATUS_CREATED; } // Create OrderAddress. $deliveryAddress = new OrderAddress(); $invoiceAddress = new OrderAddress(); // Persist entities. $this->em->persist($deliveryAddress); $this->em->persist($invoiceAddress); // Assign to order. $order->setDeliveryAddress($deliveryAddress); $order->setInvoiceAddress($invoiceAddress); } // Set order status. if ($statusId !== null) { $this->convertStatus($order, $statusId); } // If not new and contact is not set, use old contact. if (!$isNewOrder && !$contact) { $contact = $order->getEntity()->getCustomerContact(); } $contactFullName = null; if ($contact) { $contactFullName = $contact->getFullName(); } if (isset($data['invoiceAddress'])) { // Set customer name to account if set, otherwise to contact. $contactFullName = $this->orderAddressManager->getContactData($data['invoiceAddress'], $contact)['fullName']; // Set OrderAddress data. $this->orderAddressManager->setOrderAddress($order->getEntity()->getInvoiceAddress(), $data['invoiceAddress'], $contact, $account); } if (isset($data['deliveryAddress'])) { $this->orderAddressManager->setOrderAddress($order->getEntity()->getDeliveryAddress(), $data['deliveryAddress'], $contact, $account); } // Set customer name. $customerName = $account !== null ? $account->getName() : $contactFullName; if ($customerName) { $order->setCustomerName($customerName); } // Handle items. if (!$this->processItems($data, $order, $locale, $userId)) { throw new OrderException('Error while processing items'); } $order->setChanged(new DateTime()); $order->setChanger($user); $this->updateApiEntity($order, $locale); if ($flush) { $this->em->flush(); } return $order; }