/** * Links the items (similar/accessory) */ public function link() { $Request_AddLinkedItems = new PlentySoapRequest_AddLinkedItems(); $Request_AddLinkedItems->CrosssellingList = array(); foreach ($this->SHOPWARE_Article->getSimilar() as $Similar) { $Object_AddLinkedItems = new PlentySoapObject_AddLinkedItems(); $Object_AddLinkedItems->Relationship = 'Similar'; // string $Object_AddLinkedItems->CrossItemSKU = PlentymarketsMappingController::getItemByShopwareID($Similar->getId()); // string $Request_AddLinkedItems->CrosssellingList[] = $Object_AddLinkedItems; } foreach ($this->SHOPWARE_Article->getRelated() as $Related) { $Object_AddLinkedItems = new PlentySoapObject_AddLinkedItems(); $Object_AddLinkedItems->Relationship = 'Accessory'; // string $Object_AddLinkedItems->CrossItemSKU = PlentymarketsMappingController::getItemByShopwareID($Related->getId()); $Request_AddLinkedItems->CrosssellingList[] = $Object_AddLinkedItems; } if (!count($Request_AddLinkedItems->CrosssellingList)) { return; } $Request_AddLinkedItems->MainItemSKU = PlentymarketsMappingController::getItemByShopwareID($this->SHOPWARE_Article->getId()); // string // Do the request PlentymarketsSoapClient::getInstance()->AddLinkedItems($Request_AddLinkedItems); }
/** * Exports the item bundle head item * * @throws PlentymarketsExportException */ protected function exportHead() { // The shopware item on which the bundle is based on $shopwareBundleHead = $this->SHOPWARE_bundle->getArticle(); $shopwareBundleHeadIsVariant = !is_null($shopwareBundleHead->getConfiguratorSet()); // If the bundle head is a variant, the bundle can't be exported // since that feature is not provided by plentymarkets if ($shopwareBundleHeadIsVariant) { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the master item is a variant.', 2230); } if ($this->SHOPWARE_bundle->getDiscountType() != 'abs') { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the discount type is not supported.', 2240); } if ($this->SHOPWARE_bundle->getType() != 1) { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« can not be exported because the bundle type is not supported.', 2250); } // The shopware bundle head needs to be added as a plenty-bundle-item // The bundle head in plentymarkets is a "special" item $bundleItemId = PlentymarketsMappingController::getItemByShopwareID($shopwareBundleHead->getId()); $sku = sprintf('%d-0', $bundleItemId); // If this item is also a bundle item in shopware, // we need to increase the quantity of it if (isset($this->PLENTY_bundleSkuList[$sku])) { $this->PLENTY_bundleSkuList[$sku] += 1; } else { $this->PLENTY_bundleSkuList[$sku] = 1; } // Create the bundle head $Request_AddItemsBase = new PlentySoapRequest_AddItemsBase(); $Request_AddItemsBase->BaseItems = array(); $Object_AddItemsBaseItemBase = new PlentySoapObject_AddItemsBaseItemBase(); $Object_ItemAvailability = new PlentySoapObject_ItemAvailability(); $validTo = $this->SHOPWARE_bundle->getValidTo(); if ($validTo instanceof DateTime) { $Object_ItemAvailability->AvailableUntil = $this->SHOPWARE_bundle->getValidTo()->getTimestamp(); } $Object_ItemAvailability->WebAPI = 1; $Object_ItemAvailability->Inactive = (int) $this->SHOPWARE_bundle->getActive(); $Object_ItemAvailability->Webshop = (int) $this->SHOPWARE_bundle->getActive(); $Object_AddItemsBaseItemBase->Availability = $Object_ItemAvailability; $storeIds = array(); $Object_AddItemsBaseItemBase->Categories = array(); $Object_AddItemsBaseItemBase->StoreIDs = array(); foreach ($shopwareBundleHead->getCategories() as $category) { /** @var Shopware\Models\Category\Category $category */ try { $categoryPath = PlentymarketsMappingController::getCategoryByShopwareID($category->getId()); } catch (PlentymarketsMappingExceptionNotExistant $E) { continue; } $Object_ItemCategory = new PlentySoapObject_ItemCategory(); $Object_ItemCategory->ItemCategoryPath = $categoryPath; // string $Object_AddItemsBaseItemBase->Categories[] = $Object_ItemCategory; // Get the store for this category $rootId = PlentymarketsUtils::getRootIdByCategory($category); $shops = PlentymarketsUtils::getShopIdByCategoryRootId($rootId); foreach ($shops as $shopId) { try { $storeId = PlentymarketsMappingController::getShopByShopwareID($shopId); } catch (PlentymarketsMappingExceptionNotExistant $E) { continue; } if (!isset($storeIds[$storeId])) { // Activate the item for this store $Object_Integer = new PlentySoapObject_Integer(); $Object_Integer->intValue = $storeId; $Object_AddItemsBaseItemBase->StoreIDs[] = $Object_Integer; // Cache $storeIds[$storeId] = true; } } } $Object_AddItemsBaseItemBase->ExternalItemID = 'Swag/Bundle/' . $this->SHOPWARE_bundle->getId(); // string $Object_AddItemsBaseItemBase->ItemNo = $this->SHOPWARE_bundle->getNumber(); // string $Object_ItemPriceSet = new PlentySoapObject_ItemPriceSet(); $defaultCustomerGroupKey = PlentymarketsConfig::getInstance()->get('DefaultCustomerGroupKey'); $price = null; $isPriceFound = false; foreach ($this->SHOPWARE_bundle->getPrices() as $price) { /** @var Shopware\CustomModels\Bundle\Price $price */ if ($price->getCustomerGroup()->getKey() == $defaultCustomerGroupKey) { $isPriceFound = true; break; } } if ($isPriceFound && $price instanceof Shopware\CustomModels\Bundle\Price) { $tax = $this->SHOPWARE_bundle->getArticle()->getTax()->getTax(); $priceNet = $price->getPrice(); $price = $priceNet + $priceNet / 100 * $tax; $Object_ItemPriceSet->Price = $price; $Object_ItemPriceSet->VAT = $tax; } else { // If there is no price, we have to set one anyway. // Otherwise the re-import will crash $Object_ItemPriceSet->Price = 1; } $Object_AddItemsBaseItemBase->PriceSet = $Object_ItemPriceSet; $Object_AddItemsBaseItemBase->VATInternalID = PlentymarketsMappingController::getVatByShopwareID($this->SHOPWARE_bundle->getArticle()->getTax()->getId()); $Object_AddItemsBaseItemBase->ProducerID = PlentymarketsMappingController::getProducerByShopwareID($shopwareBundleHead->getSupplier()->getId()); // int $Object_AddItemsBaseItemBase->Published = null; // int $Object_ItemTexts = new PlentySoapObject_ItemTexts(); $Object_ItemTexts->Name = $this->SHOPWARE_bundle->getName(); // string $Object_AddItemsBaseItemBase->Texts = $Object_ItemTexts; $Request_AddItemsBase->BaseItems[] = $Object_AddItemsBaseItemBase; $Response_AddItemsBase = PlentymarketsSoapClient::getInstance()->AddItemsBase($Request_AddItemsBase); $ResponseMessage = $Response_AddItemsBase->ResponseMessages->item[0]; if (!$Response_AddItemsBase->Success || $ResponseMessage->Code != 100) { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« could not be exported', 2210); } $PLENTY_priceID = null; foreach ($ResponseMessage->SuccessMessages->item as $SubMessage) { if ($SubMessage->Key == 'ItemID') { $this->PLENTY_bundleHeadId = (int) $SubMessage->Value; } else { if ($SubMessage->Key == 'PriceID') { $PLENTY_priceID = (int) $SubMessage->Value; } } } if ($this->PLENTY_bundleHeadId && $PLENTY_priceID) { PlentymarketsLogger::getInstance()->message('Export:Initial:Item:Bundle', 'The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« has been created with the id »' . $this->PLENTY_bundleHeadId . '«.'); PlentymarketsMappingController::addItemBundle($this->SHOPWARE_bundle->getId(), $this->PLENTY_bundleHeadId); } else { throw new PlentymarketsExportException('The item bundle with the number »' . $this->SHOPWARE_bundle->getNumber() . '« could not be exported', 2210); } }
/** * Deletes a page of invalid data * * @param integer $start * @param integer $offset */ public function deleteInvalidData($start, $offset) { // Controller $controller = new PlentymarketsImportControllerItem(); // StoreIds $stores = Shopware()->Db()->fetchAll(' SELECT plentyID FROM plenty_mapping_shop '); // Customer group $customerGroupKey = PlentymarketsConfig::getInstance()->getDefaultCustomerGroupKey(); $customerGroupRepository = Shopware()->Models()->getRepository('Shopware\\Models\\Customer\\Group'); $customerGroups = $customerGroupRepository->findBy(array('key' => $customerGroupKey)); $customerGroup = array_pop($customerGroups); // foreach ($this->getInvalidData($start, $offset) as $data) { PyLog()->message('Fix:Item:Price', 'Start of fixing corrupt prices of the item id ' . $data['itemId']); // Search foreach (explode(',', $data['detailIds']) as $detailId) { try { /** @var \Shopware\Models\Article\Detail $Detail */ $Detail = Shopware()->Models()->find('\\Shopware\\Models\\Article\\Detail', $detailId); $price = new Shopware\Models\Article\Price(); $price->setFrom(1); $price->setPrice(1); $price->setPercent(0); $price->setArticle($Detail->getArticle()); $price->setDetail($Detail); $price->setCustomerGroup($customerGroup); Shopware()->Models()->persist($price); } catch (Exception $E) { PyLog()->debug($E->getMessage()); } } Shopware()->Models()->flush(); PyLog()->message('Fix:Item:Price', 'Finished with the fixing corrupt prices of the item id »' . $data['itemId'] . '«'); try { foreach ($stores as $store) { // Update the complete item from plenty $controller->importItem(PlentymarketsMappingController::getItemByShopwareID($data['itemId']), $store['plentyID']); } } catch (Exception $e) { PyLog()->error('Fix:Item:Price', $e->getMessage()); } // Stop after the first break; } }
/** * Exports images, variants, properties item data and items base to make sure, that the corresponding items data exist. */ protected function export() { // Query builder $QueryBuilder = Shopware()->Models()->createQueryBuilder(); $QueryBuilder->select('item.id')->from('Shopware\\Models\\Article\\Article', 'item'); do { // Log the chunk PlentymarketsLogger::getInstance()->message('Export:Initial:Item', 'Chunk: ' . ($this->currentChunk + 1)); // Set Limit and Offset $QueryBuilder->setFirstResult($this->currentChunk * $this->sizeOfChunk)->setMaxResults($this->sizeOfChunk); // Get the items $items = $QueryBuilder->getQuery()->getArrayResult(); // $itemsAlreadyExported = 0; foreach ($items as $item) { try { // If there is a plenty id for this shopware id, // the item has already been exported to plentymarkets PlentymarketsMappingController::getItemByShopwareID($item['id']); // ++$itemsAlreadyExported; // already done continue; } catch (PlentymarketsMappingExceptionNotExistant $E) { } $PlentymarketsExportEntityItem = new PlentymarketsExportEntityItem(Shopware()->Models()->find('Shopware\\Models\\Article\\Article', $item['id'])); $PlentymarketsExportEntityItem->export(); } // Remember the chunk $this->Config->setItemExportLastChunk($this->currentChunk); if ($this->maxChunks > 0) { // Increase number of chunks if every item has already been exported if ($itemsAlreadyExported == $this->sizeOfChunk) { ++$this->maxChunks; PlentymarketsLogger::getInstance()->message('Export:Initial:Item', 'Increasing number of chunks per run to ' . $this->maxChunks . ' since every item of chunk ' . ($this->currentChunk + 1) . ' has already been exported'); } // Quit when the maximum number of chunks is reached if (++$this->chunksDone >= $this->maxChunks) { $this->toBeContinued = true; break; } } // Next chunk ++$this->currentChunk; } while (!empty($items) && count($items) == $this->sizeOfChunk); }
/** * Export the order */ protected function exportOrder() { $VariantResource = self::getVariantApi(); // Build the Request $Request_AddOrders = new PlentySoapRequest_AddOrders(); $Request_AddOrders->Orders = array(); // $Object_Order = new PlentySoapObject_Order(); // $methodOfPayment = $this->getMethodOfPaymentId(); if ($methodOfPayment == MOP_AMAZON_PAYMENT) { $externalOrderID = sprintf('Swag/%d/%s/%s', $this->Order->getId(), $this->Order->getNumber(), $this->Order->getTransactionId()); } else { $externalOrderID = sprintf('Swag/%d/%s', $this->Order->getId(), $this->Order->getNumber()); } $isOrderNet = (bool) $this->Order->getNet() || (bool) $this->Order->getTaxFree(); // Order head $Object_OrderHead = new PlentySoapObject_OrderHead(); $Object_OrderHead->Currency = PlentymarketsMappingController::getCurrencyByShopwareID($this->Order->getCurrency()); $Object_OrderHead->CustomerID = $this->PLENTY_customerID; $Object_OrderHead->DeliveryAddressID = $this->PLENTY_addressDispatchID; $Object_OrderHead->ExternalOrderID = $externalOrderID; $Object_OrderHead->IsNetto = $isOrderNet; $Object_OrderHead->Marking1ID = PlentymarketsConfig::getInstance()->getOrderMarking1(null); $Object_OrderHead->MethodOfPaymentID = $this->getMethodOfPaymentId(); $Object_OrderHead->OrderTimestamp = $this->getOrderTimestamp(); $Object_OrderHead->OrderType = 'order'; $Object_OrderHead->ResponsibleID = PlentymarketsConfig::getInstance()->getOrderUserID(null); $Object_OrderHead->ShippingCosts = $this->getShippingCosts(); $Object_OrderHead->ShippingProfileID = $this->getParcelServicePresetId(); $Object_OrderHead->StoreID = $this->getShopId(); $Object_OrderHead->ReferrerID = $this->getReferrerId(); $Object_Order->OrderHead = $Object_OrderHead; // Order infos $Object_OrderHead->OrderInfos = array(); if ($Object_OrderHead->MethodOfPaymentID == MOP_DEBIT) { $Customer = $this->Order->getCustomer(); if ($Customer) { $Debit = $Customer->getDebit(); if ($Debit && $Debit->getAccountHolder()) { $info = 'Account holder: ' . $Debit->getAccountHolder() . chr(10); $info .= 'Bank name: ' . $Debit->getBankName() . chr(10); $info .= 'Bank code: ' . $Debit->getBankCode() . chr(10); $info .= 'Account number: ' . $Debit->getAccount() . chr(10); $Object_OrderInfo = new PlentySoapObject_OrderInfo(); $Object_OrderInfo->Info = $info; $Object_OrderInfo->InfoCustomer = 0; $Object_OrderInfo->InfoDate = $this->getOrderTimestamp(); $Object_OrderHead->OrderInfos[] = $Object_OrderInfo; } $PaymentInstances = $Customer->getPaymentInstances(); if ($PaymentInstances) { foreach ($PaymentInstances as $PaymentInstance) { if ($this->Order->getId() === $PaymentInstance->getOrder()->getId()) { $Object_BankData = new PlentySoapObject_BankData(); $Object_BankData->OwnerFirstname = $PaymentInstance->getFirstName(); $Object_BankData->OwnerLastname = $PaymentInstance->getLastName(); $Object_BankData->BankName = $PaymentInstance->getBankName(); $Object_BankData->IBAN = $PaymentInstance->getIban(); $Object_BankData->BIC = $PaymentInstance->getBic(); $Object_SetBankCreditCardData = new PlentySoapObject_SetBankCreditCardData(); $Object_SetBankCreditCardData->CustomerID = $this->PLENTY_customerID; $Object_SetBankCreditCardData->BankData = $Object_BankData; $Request_SetBankCreditCardData = new PlentySoapRequest_SetBankCreditCardData(); $Request_SetBankCreditCardData->CustomerData[] = $Object_SetBankCreditCardData; $Response_SetBankCreditCardData = PlentymarketsSoapClient::getInstance()->SetBankCreditCardData($Request_SetBankCreditCardData); if (!$Response_SetBankCreditCardData->Success) { // Set the error end quit $this->setError(self::CODE_ERROR_SOAP); throw new PlentymarketsExportEntityException('The order with the number »' . $this->Order->getNumber() . '« could not be exported'); } } } } } } if ($this->Order->getInternalComment()) { $Object_OrderInfo = new PlentySoapObject_OrderInfo(); $Object_OrderInfo->Info = $this->Order->getInternalComment(); $Object_OrderInfo->InfoCustomer = 0; $Object_OrderInfo->InfoDate = $this->getOrderTimestamp(); $Object_OrderHead->OrderInfos[] = $Object_OrderInfo; } if ($this->Order->getCustomerComment()) { $Object_OrderInfo = new PlentySoapObject_OrderInfo(); $Object_OrderInfo->Info = $this->Order->getCustomerComment(); $Object_OrderInfo->InfoCustomer = 1; $Object_OrderInfo->InfoDate = $this->getOrderTimestamp(); $Object_OrderHead->OrderInfos[] = $Object_OrderInfo; } if ($this->Order->getComment()) { $Object_OrderInfo = new PlentySoapObject_OrderInfo(); $Object_OrderInfo->Info = $this->Order->getComment(); $Object_OrderInfo->InfoCustomer = 1; $Object_OrderInfo->InfoDate = $this->getOrderTimestamp(); $Object_OrderHead->OrderInfos[] = $Object_OrderInfo; } $Object_Order->OrderItems = array(); /** @var Shopware\Models\Order\Detail $Item */ foreach ($this->Order->getDetails() as $Item) { $number = $Item->getArticleNumber(); $itemText = ''; // Variant try { $itemId = null; try { // get the detail id by the order number $articleDetailID = $VariantResource->getIdFromNumber($Item->getArticleNumber()); } catch (Exception $E) { $articleDetailID = -1; } // get the sku from the detail id $sku = PlentymarketsMappingController::getItemVariantByShopwareID($articleDetailID); } catch (PlentymarketsMappingExceptionNotExistant $E) { // Base item try { $itemId = PlentymarketsMappingController::getItemByShopwareID($Item->getArticleId()); $sku = null; } catch (PlentymarketsMappingExceptionNotExistant $E) { $itemId = -2; $sku = null; // Mandatory because there will be no mapping to any item $itemText = $Item->getArticleName(); } } // if ($itemId > 0 || !empty($sku)) { if (PlentymarketsConfig::getInstance()->getOrderItemTextSyncActionID(EXPORT_ORDER_ITEM_TEXT_SYNC) == EXPORT_ORDER_ITEM_TEXT_SYNC) { $itemText = $Item->getArticleName(); } else { $itemText = null; } } // Coupon if ($Item->getMode() == 2) { $itemId = -1; $rowType = 'Coupon'; } // Additional coupon identifiers für 3rd party plugins $couponIdentifiers = PyConf()->get('OrderAdditionalCouponIdentifiers', ''); $couponIdentifiers = explode('|', $couponIdentifiers); if (in_array($number, $couponIdentifiers)) { $itemId = -1; $rowType = 'Coupon'; } else { // PAYONE fix if ($number == 'SHIPPING' && !$Object_OrderHead->ShippingCosts) { $Object_OrderHead->ShippingCosts = $Item->getPrice(); continue; } $discountNumber = Shopware()->Config()->get('discountnumber'); $surchargeNumber = Shopware()->Config()->get('surchargenumber'); $paymentSurchargeNumber = Shopware()->Config()->get('paymentsurchargenumber'); $paymentSurchargeAbsoluteNumber = Shopware()->Config()->get('paymentSurchargeAbsoluteNumber'); $shippingDiscountNumber = Shopware()->Config()->get('shippingdiscountnumber'); switch ($number) { case $paymentSurchargeNumber: case $paymentSurchargeAbsoluteNumber: $rowType = 'SurchargeForPaymentMethod'; break; case $discountNumber: $rowType = 'Discount'; break; case $surchargeNumber: $rowType = 'Surcharge'; break; case $shippingDiscountNumber: $rowType = 'SurchargeForShippingMethod'; break; default: $rowType = 'Default'; break; } } if ($isOrderNet) { // Calculate the gross amount (needed by plentymakets even though it is a net sales order) $itemPrice = $Item->getPrice() * ((100 + (double) $Item->getTaxRate()) / 100); } else { $itemPrice = $Item->getPrice(); } $Object_OrderItem = new PlentySoapObject_OrderItem(); $Object_OrderItem->ExternalOrderItemID = $number; $Object_OrderItem->ItemID = $itemId; $Object_OrderItem->ReferrerID = $Object_OrderHead->ReferrerID; $Object_OrderItem->ItemText = $itemText; $Object_OrderItem->Price = $itemPrice; $Object_OrderItem->Quantity = $Item->getQuantity(); $Object_OrderItem->SKU = $sku; $Object_OrderItem->VAT = $Item->getTaxRate(); $Object_OrderItem->RowType = $rowType; $Object_Order->OrderItems[] = $Object_OrderItem; } $Request_AddOrders->Orders[] = $Object_Order; // Do the request $Response_AddOrders = PlentymarketsSoapClient::getInstance()->AddOrders($Request_AddOrders); if (!$Response_AddOrders->Success) { // Set the error end quit $this->setError(self::CODE_ERROR_SOAP); throw new PlentymarketsExportEntityException('The order with the number »' . $this->Order->getNumber() . '« could not be exported', 4010); } // $plentyOrderID = null; $plentyOrderStatus = 0.0; foreach ($Response_AddOrders->ResponseMessages->item[0]->SuccessMessages->item as $SuccessMessage) { switch ($SuccessMessage->Key) { case 'OrderID': $plentyOrderID = (int) $SuccessMessage->Value; break; case 'Status': $plentyOrderStatus = (double) $SuccessMessage->Value; break; } } if ($plentyOrderID && $plentyOrderStatus) { $this->setSuccess($plentyOrderID, $plentyOrderStatus); } else { // Set the error end quit $this->setError(self::CODE_ERROR_SOAP); throw new PlentymarketsExportEntityException('The order with the number »' . $this->Order->getNumber() . '« could not be exported (no order id or order status respectively)', 4020); } $paymentStatusPaid = explode('|', PlentymarketsConfig::getInstance()->getOrderPaidStatusID(12)); // Directly book the incoming payment if ($this->Order->getPaymentStatus() && in_array($this->Order->getPaymentStatus()->getId(), $paymentStatusPaid)) { // May throw an exception $IncomingPayment = new PlentymarketsExportEntityOrderIncomingPayment($this->Order->getId()); $IncomingPayment->book(); } }