/** * Creates new item for test purpose. * * @return ItemInterface */ public function createNewTestItem() { $item = $this->itemFactory->createEntity(); $item->setName('Product1'); $item->setNumber('123'); $item->setQuantity(2); $item->setQuantityUnit('Pcs'); $item->setUseProductsPrice(true); $item->setTax(20); $item->setPrice($this->productPrice->getPrice()); $item->setDiscount(10); $item->setDescription('This is a description'); $item->setWeight(15.8); $item->setWidth(5); $item->setHeight(6); $item->setLength(7); $item->setCreated(new DateTime()); $item->setChanged(new DateTime()); $item->setProduct($this->product); $item->setSupplier($this->account); $item->setSupplierName($this->account->getName()); return $item; }
/** * Creates an item, but does not flush. * * @param array $data * @param string $locale * @param int|null $userId * @param ApiItemInterface|ItemInterface|null $item * @param int|null $itemStatusId * @param ContactInterface|null $contact The contact that should be used for order-address * @param ItemInterface|null $lastProcessedProductItem * * @return ApiItemInterface * * @throws ItemException * @throws ProductException * @throws ProductNotFoundException * @throws \Exception */ public function save(array $data, $locale, $userId = null, $item = null, $itemStatusId = null, ContactInterface $contact = null, ItemInterface $lastProcessedProductItem = null) { $itemEntity = $this->itemFactory->createEntity(); $isNewItem = !$item; // Check if required data for creating an item is set. if ($isNewItem) { $this->checkRequiredData($data, true); $item = $this->itemFactory->createApiEntity($this->itemFactory->createEntity(), $locale); } if ($item instanceof ItemInterface) { $item = $this->itemFactory->createApiEntity($item, $locale); } $user = $userId ? $this->userRepository->findUserById($userId) : null; $account = null; // If no contact is given take user as fallback. if (!$contact && !!$user) { $contact = $user->getContact(); } if ($contact) { $account = $contact->getMainAccount(); } $item->setQuantity($this->getProperty($data, 'quantity')); $item->setUseProductsPrice($this->getProperty($data, 'useProductsPrice', true)); $this->setDate($data, 'deliveryDate', null, [$item, 'setDeliveryDate']); $item->setType($this->getProperty($data, 'type', BaseItem::TYPE_PRODUCT)); // Check if the product or addon relation on a saved item still exists, else set type custom. if (!$isNewItem) { if ($item->getType() === BaseItem::TYPE_PRODUCT && $item->getProduct() === null) { $item->setType(BaseItem::TYPE_CUSTOM); } elseif ($item->getType() === BaseItem::TYPE_ADDON && $item->getAddon() === null) { $item->setType(BaseItem::TYPE_CUSTOM); } } switch ($item->getType()) { case BaseItem::TYPE_PRODUCT: if ($isNewItem) { $productData = $this->getProperty($data, 'product'); if ($productData) { // Set Product's data to item. $this->setItemByProductData($productData, $item, $locale); } } break; case BaseItem::TYPE_CUSTOM: if ($isNewItem) { $item->setUseProductsPrice(false); } $item->setName($this->getProperty($data, 'name', $item->getName())); $item->setTax($this->getProperty($data, 'tax', $item->getTax())); $item->setNumber($this->getProperty($data, 'number', $item->getNumber())); $item->setDescription($this->getProperty($data, 'description', $item->getDescription())); $item->setQuantityUnit($this->getProperty($data, 'quantityUnit', $item->getQuantityUnit())); break; case BaseItem::TYPE_ADDON: if ($isNewItem) { $productData = $this->getProperty($data, 'product'); $parent = null; if ($productData) { // Set Product's data to item. $product = $this->setItemByProductData($productData, $item, $locale); // Check if product has a parent. $parent = $product->getParent(); } if ($lastProcessedProductItem) { $this->setAddonData($lastProcessedProductItem, $item->getEntity(), $parent); } } break; default: throw new ItemException('Unhandled item type found'); break; } if (method_exists($item, 'getSupplierName')) { $item->setSupplierName($this->getProperty($data, 'supplierName', $item->getSupplierName())); } // Update prices. $this->updatePrices($item, $data); $item->setDiscount($this->getProperty($data, 'discount', $item->getDiscount())); $item->setIsRecurringPrice($this->getProperty($data, 'isRecurringPrice', $item->isRecurringPrice())); $item->setCostCentre($this->getProperty($data, 'costCentre')); // Set delivery-address for item. if (isset($data['deliveryAddress'])) { $this->setItemDeliveryAddress($data['deliveryAddress'], $item, $contact, $account); } // Create new item. if ($item->getId() == null) { $item->setCreated(new DateTime()); $item->setCreator($user); $this->em->persist($item->getEntity()); if (!($itemStatusId = null)) { $itemStatusId = $itemEntity::STATUS_CREATED; } } if ($itemStatusId) { $this->addStatus($item, $itemStatusId); } // Handle attributes. $this->processAttributes($data, $item, $locale); $item->setChanged(new DateTime()); $item->setChanger($user); return $item; }