/** * Sets delivery address for an item. * * @param array|int $addressData * @param ApiItemInterface $item * @param ContactInterface $contact * @param AccountInterface $account * * @throws ItemDependencyNotFoundException * @throws ItemException */ protected function setItemDeliveryAddress($addressData, ApiItemInterface $item, ContactInterface $contact = null, AccountInterface $account = null) { if ($item->getDeliveryAddress() === null) { // Create new delivery address. $deliveryAddress = new $this->orderAddressEntity(); // Persist entities. $this->em->persist($deliveryAddress); // Assign to order. $item->setDeliveryAddress($deliveryAddress); } if (is_array($addressData)) { // Set order-address. $this->orderAddressManager->setOrderAddress($item->getDeliveryAddress(), $addressData, $contact, $account); } elseif (is_int($addressData)) { $contactAddressId = $addressData; // Create order-address and assign contact-address data. $deliveryAddress = $item->getEntity()->getDeliveryAddress(); $orderAddress = $this->orderAddressManager->getAndSetOrderAddressByContactAddressId($contactAddressId, $contact, $account, $deliveryAddress); // Set delivery address. $item->setDeliveryAddress($orderAddress); // If new delivery address persist. if (!$deliveryAddress) { $this->em->persist($orderAddress); } } }
/** * 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; }
/** * Function creates an empty cart. * This means an order with status 'in_cart' is created and all necessary data is set. * * @param UserInterface $user * @param bool $doPersist * @param string $locale * @param null|string $currencyCode * * @return OrderInterface */ protected function createEmptyCart(UserInterface $user, $doPersist, $locale, $currencyCode = null) { $cart = $this->orderRepository->createNew(); $cart->setCreator($user); $cart->setChanger($user); $cart->setCreated(new DateTime()); $cart->setChanged(new DateTime()); $cart->setOrderDate(new DateTime()); // set currency - if not defined use default $currencyCode = $currencyCode ?: $this->defaultCurrency; $cart->setCurrencyCode($currencyCode); // get address from contact and account $contact = $user->getContact(); $account = $contact->getMainAccount(); $cart->setCustomerContact($contact); $cart->setCustomerAccount($account); /** Account $account */ if ($account && $account->getResponsiblePerson()) { $cart->setResponsibleContact($account->getResponsiblePerson()); } $addressSource = $contact; if ($account) { $addressSource = $account; } // get billing address $invoiceOrderAddress = null; $invoiceAddress = $this->accountManager->getBillingAddress($addressSource, true); if ($invoiceAddress) { // convert to order-address $invoiceOrderAddress = $this->orderAddressManager->getAndSetOrderAddressByContactAddress($invoiceAddress, $contact, $account); $cart->setInvoiceAddress($invoiceOrderAddress); } $deliveryOrderAddress = null; $deliveryAddress = $this->accountManager->getDeliveryAddress($addressSource, true); if ($deliveryAddress) { // convert to order-address $deliveryOrderAddress = $this->orderAddressManager->getAndSetOrderAddressByContactAddress($deliveryAddress, $contact, $account); $cart->setDeliveryAddress($deliveryOrderAddress); } // TODO: anonymous order // set order type if ($user) { $name = $user->getContact()->getFullName(); $cart->setType($this->orderManager->getOrderTypeEntityById(OrderType::SHOP)); } else { $name = 'Anonymous'; $cart->setType($this->orderManager->getOrderTypeEntityById(OrderType::ANONYMOUS)); } $cart->setCustomerName($name); $apiCart = $this->orderFactory->createApiEntity($cart, $locale); $this->orderManager->convertStatus($apiCart, OrderStatus::STATUS_IN_CART, false, $doPersist); if ($doPersist) { $this->em->persist($cart); if ($invoiceOrderAddress) { $this->em->persist($invoiceOrderAddress); } if ($deliveryOrderAddress) { $this->em->persist($deliveryOrderAddress); } } return $cart; }