/** * Returns the customer class id * * @return integer|null */ protected function getCustomerClassId() { try { return PlentymarketsMappingController::getCustomerClassByShopwareID($this->Customer->getGroup()->getId()); } catch (Exception $E) { PyLog()->debug($E->getMessage()); return null; } }
/** * Deletes all dummy customer entity */ private function deleteDummyCustomer(\Shopware\Models\Customer\Customer $customer) { $billingId = Shopware()->Db()->fetchOne('SELECT id FROM s_user_billingaddress WHERE userID = ?', array($customer->getId())); $shippingId = Shopware()->Db()->fetchOne('SELECT id FROM s_user_shippingaddress WHERE userID = ?', array($customer->getId())); if ($billingId) { Shopware()->Db()->delete('s_user_billingaddress_attributes', 'billingID = ' . $billingId); Shopware()->Db()->delete('s_user_billingaddress', 'id = ' . $billingId); } if ($shippingId) { Shopware()->Db()->delete('s_user_shippingaddress_attributes', 'shippingID = ' . $shippingId); Shopware()->Db()->delete('s_user_shippingaddress', 'id = ' . $shippingId); } Shopware()->Db()->delete('s_core_payment_data', 'user_id = ' . $customer->getId()); Shopware()->Db()->delete('s_user_attributes', 'userID = ' . $customer->getId()); Shopware()->Db()->delete('s_user', 'id = ' . $customer->getId()); }
/** * @depends testCanCreateEntity */ public function testCanUpdateEntityWithReference(Shopware\Models\Customer\Customer $customer) { $groupId = $customer->getGroup()->getId(); $groupKey = $customer->getGroup()->getKey(); $customer = new Customer(); $customer->setEmail('*****@*****.**'); $this->em->persist($customer); $this->em->flush(); $customerId = $customer->getId(); $this->em->clear(); $customer = $this->em->find('Shopware\\Models\\Customer\\Customer', $customerId); $group = $this->em->getReference('Shopware\\Models\\Customer\\Group', $groupId); $customer->setGroup($group); $this->assertNotEmpty($customer->getId()); $this->assertEquals($group, $customer->getGroup()); $this->assertNotEmpty($customer->getGroup()->getId()); $this->assertEquals($groupKey, $customer->getGroup()->getKey()); return $customer; }
/** * Helper method to prepare the customer for saving * * @param array $params * @param Shopware\Models\Customer\Customer $customer * @param array $paymentData * @return array */ private function prepareCustomerData($params, Shopware\Models\Customer\Customer $customer, $paymentData) { if (!empty($params['groupKey'])) { $params['group'] = $this->getGroupRepository()->findOneBy(array('key' => $params['groupKey'])); } else { unset($params['group']); } if (!empty($params['languageId'])) { /** @var $shopRepository \Shopware\Models\Shop\Repository */ $shopRepository = $this->getShopRepository(); $params['languageSubShop'] = $shopRepository->find($params['languageId']); } else { unset($params['languageSubShop']); unset($params['shop']); } if (!empty($params['priceGroupId'])) { $params['priceGroup'] = Shopware()->Models()->find('Shopware\\Models\\Customer\\PriceGroup', $params['priceGroupId']); } else { $params['priceGroup'] = null; } //If a different payment method is selected, it must also be placed in the "paymentPreset" so that the risk management that does not reset. if ($customer->getPaymentId() !== $params['paymentId']) { $params['paymentPreset'] = $params['paymentId']; } if (empty($id) && empty($params['shipping'][0]["firstName"]) && empty($params['shipping'][0]["lastName"])) { //shipping params are empty use the billing ones $params['shipping'][0] = $params['billing'][0]; } if (!empty($params['paymentData']) && $paymentData) { $paymentData->fromArray(array_shift($params['paymentData'])); } unset($params['paymentData']); /** * Temporary support for deprecated s_user_debit table * Can be removed after the table is removed */ if ($paymentData && $paymentData->getPaymentMean()->getName() == 'debit') { $debitData = array('account' => $paymentData->getAccountNumber(), 'accountHolder' => $paymentData->getAccountHolder(), 'bankName' => $paymentData->getBankName(), 'bankCode' => $paymentData->getBankCode()); $params['debit'] = $debitData; } $params['billing'] = $params['billing'][0]; $params['shipping'] = $params['shipping'][0]; $params['attribute'] = $params['attribute'][0]; $params['billing']['attribute'] = $params['billingAttribute'][0]; $params['shipping']['attribute'] = $params['shippingAttribute'][0]; return $params; }
/** * Saves a single customer. If no customer id passed, * the save function creates a new customer model and persist * it by the shopware model manager. * The sub models billing, shipping and debit will be filled * by the passed parameter arrays billing, shipping and debit. */ public function saveAction() { $id = $this->Request()->getParam('id', null); $paymentId = $this->Request()->getParam('paymentId', null); /** @var $namespace Enlight_Components_Snippet_Namespace */ $namespace = Shopware()->Snippets()->getNamespace('backend/customer'); //customer id passed? If this is the case the customer was edited if (!empty($id)) { //check if the user has the rights to update an existing customer if (!$this->_isAllowed('update', 'customer')) { $this->View()->assign(array('success' => false, 'data' => $this->Request()->getParams(), 'message' => $namespace->get('no_edit_rights', 'You do not have sufficient rights to edit a customer.'))); return; } $customer = $this->getRepository()->find($id); $paymentData = $this->getManager()->getRepository('Shopware\\Models\\Customer\\PaymentData')->findOneBy(array('customer' => $customer, 'paymentMean' => $paymentId)); } else { //check if the user has the rights to create a new customer if (!$this->_isAllowed('create', 'customer')) { $this->View()->assign(array('success' => false, 'data' => $this->Request()->getParams(), 'message' => $namespace->get('no_create_rights', 'You do not have sufficient rights to view create a customer.'))); return; } $customer = new Customer(); } try { $params = $this->Request()->getParams(); if (!$paymentData instanceof PaymentData && !empty($params['paymentData']) && array_filter($params['paymentData'][0])) { $paymentData = new PaymentData(); $customer->addPaymentData($paymentData); $paymentData->setPaymentMean($this->getManager()->getRepository('Shopware\\Models\\Payment\\Payment')->find($paymentId)); } $params = $this->prepareCustomerData($params, $customer, $paymentData); //set parameter to the customer model. $customer->fromArray($params); $password = $this->Request()->getParam('newPassword', null); //encode the password with md5 if (!empty($password)) { $customer->setPassword($password); } $this->getManager()->persist($customer); $this->getManager()->flush(); $this->View()->assign(array('success' => true, 'data' => $this->getCustomer($customer->getId()))); } catch (\Doctrine\ORM\ORMException $e) { $this->View()->assign(array('success' => false, 'data' => $this->Request()->getParams(), 'message' => $e->getMessage())); } }
/** * @param $mail * @param null|\Shopware\Models\Customer\Customer $customer * @param null|int $shopId * @return bool */ public function isEmailUnique($mail, $customer = null, $shopId = null) { $customerId = null; if ($customer) { $customerId = $customer->getId(); if ($customer->getShop()) { $shopId = $customer->getShop()->getId(); } // If accountmode is 1 (no real user account), email is allowed to be non-unique if ($customer->getAccountMode() == 1) { return true; } } $query = $this->getRepository()->getValidateEmailQuery($mail, $customerId, $shopId); $customer = $query->getArrayResult(); return empty($customer); }
/** * @param array $params * @return \Shopware\Models\Customer\Customer * @throws \Shopware\Components\Api\Exception\CustomValidationException * @throws \Shopware\Components\Api\Exception\ValidationException * @throws \Exception */ public function create(array $params) { $this->checkPrivilege('create'); if (isset($params['email']) && !$this->isEmailUnique($params['email'], null, $params['shopId'])) { throw new ApiException\CustomValidationException(sprintf("Emailaddress %s is not unique", $params['email'])); } $params = $this->prepareCustomerData($params); $customer = new CustomerModel(); $customer->fromArray($params); $violations = $this->getManager()->validate($customer); if ($violations->count() > 0) { throw new ApiException\ValidationException($violations); } $this->getManager()->persist($customer); $this->flush(); return $customer; }
/** * @param array $records * @throws \Enlight_Event_Exception * @throws \Exception * @throws \Zend_Db_Adapter_Exception */ public function write($records) { $customerCount = 0; if (empty($records)) { $message = SnippetsHelper::getNamespace()->get('adapters/customer/no_records', 'No customer records were found.'); throw new \Exception($message); } $records = $this->eventManager->filter('Shopware_Components_SwagImportExport_DbAdapters_CustomerDbAdapter_Write', $records, ['subject' => $this]); $defaultValues = $this->getDefaultValues(); foreach ($records['default'] as $record) { try { $customerCount++; $record = $this->validator->filterEmptyString($record); $customer = $this->findExistingEntries($record); $createNewCustomer = false; if (!$customer instanceof Customer) { $createNewCustomer = true; $record = $this->dataManager->setDefaultFieldsForCreate($record, $defaultValues); $this->validator->checkRequiredFieldsForCreate($record); $customer = new Customer(); } $this->preparePassword($record); $this->validator->checkRequiredFields($record); $this->validator->validate($record, CustomerDataType::$mapper); $customerData = $this->prepareCustomer($record); $customerData['billing'] = $this->prepareBilling($record); $customerData['shipping'] = $this->prepareShipping($record, $createNewCustomer, $customerData['billing']); $customer->fromArray($customerData); if (isset($customerData['subshopID'])) { $shop = $this->manager->getRepository(Shop::class)->find($customerData['subshopID']); if (!$shop) { $message = SnippetsHelper::getNamespace()->get('adapters/shop_not_found', 'Shop with id %s was not found'); throw new AdapterException(sprintf($message, $customerData['subshopID'])); } $customer->setShop($shop); } if (isset($customerData['languageId'])) { $languageSubShop = $this->manager->getRepository(Shop::class)->find($customerData['languageId']); if (!$languageSubShop) { $message = SnippetsHelper::getNamespace()->get('adapters/language_shop_not_found', 'Language-Shop with id %s was not found'); throw new AdapterException(sprintf($message, $customerData['languageId'])); } $customer->setLanguageSubShop($languageSubShop); } $billing = $customer->getDefaultBillingAddress(); if (!$billing instanceof Address) { $billing = new Address(); $billing->setCustomer($customer); } if (isset($customerData['billing']['countryId'])) { $customerData['billing']['country'] = $this->manager->find(Country::class, $customerData['billing']['countryId']); } if (isset($customerData['billing']['stateId'])) { $customerData['billing']['state'] = $this->manager->find(State::class, $customerData['billing']['stateId']); } $billing->fromArray($customerData['billing']); $shipping = $customer->getDefaultShippingAddress(); if (!$shipping instanceof Address) { $shipping = new Address(); $shipping->setCustomer($customer); } if (isset($customerData['shipping']['countryId'])) { $customerData['shipping']['country'] = $this->manager->find(Country::class, $customerData['shipping']['countryId']); } if (isset($customerData['shipping']['stateId'])) { $customerData['shipping']['state'] = $this->manager->find(State::class, $customerData['shipping']['stateId']); } $shipping->fromArray($customerData['shipping']); $customer->setFirstname($billing->getFirstname()); $customer->setLastname($billing->getLastname()); $customer->setSalutation($billing->getSalutation()); $customer->setTitle($billing->getTitle()); $violations = $this->manager->validate($customer); if ($violations->count() > 0) { $message = SnippetsHelper::getNamespace()->get('adapters/customer/no_valid_customer_entity', 'No valid user entity for email %s'); $message = sprintf($message, $customer->getEmail()); foreach ($violations as $violation) { $message .= "\n" . $violation->getPropertyPath() . ': ' . $violation->getMessage(); } throw new AdapterException($message); } $this->manager->persist($customer); if ($createNewCustomer) { $this->manager->flush(); } $customer->setDefaultBillingAddress($billing); $this->manager->persist($billing); $customer->setDefaultShippingAddress($shipping); $this->manager->persist($shipping); $this->insertCustomerAttributes($customerData, $customer->getId(), $createNewCustomer); if ($customerCount % 20 === 0) { $this->manager->flush(); } } catch (AdapterException $e) { $message = $e->getMessage(); $this->saveMessage($message); } } $this->manager->flush(); }