1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
<?php
/* --------------------------------------------------------------
CustomerWriteService.inc.php 2016-06-27
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
MainFactory::load_class('CustomerWriteServiceInterface');
/**
* Class CustomerWriteService
*
* This class provides methods for creating and deleting customer data
*
* @category System
* @package Customer
* @implements CustomerWriteServiceInterface
*/
class CustomerWriteService implements CustomerWriteServiceInterface
{
/**
* Address book service.
* @var AddressBookServiceInterface
*/
protected $addressBookService;
/**
* Customer repository.
* @var CustomerRepositoryInterface
*/
protected $customerRepository;
/**
* Customer service settings.
* @var CustomerServiceSettingsInterface
*/
protected $customerServiceSettings;
/**
* VAT number validator.
* @var VatNumberValidatorInterface
*/
protected $vatNumberValidator;
/**
* Constructor of the class CustomerService.
*
* @param AddressBookServiceInterface $addressBookService Address book service.
* @param CustomerRepositoryInterface $customerRepository Customer repository.
* @param CustomerServiceSettingsInterface $customerServiceSettings Customer service settings.
* @param VatNumberValidatorInterface $vatNumberValidator VAT number validator.
*/
public function __construct(AddressBookServiceInterface $addressBookService,
CustomerRepositoryInterface $customerRepository,
CustomerServiceSettingsInterface $customerServiceSettings,
VatNumberValidatorInterface $vatNumberValidator)
{
$this->addressBookService = $addressBookService;
$this->customerRepository = $customerRepository;
$this->customerServiceSettings = $customerServiceSettings;
$this->vatNumberValidator = $vatNumberValidator;
}
/**
* Creates a new customer with the given parameters.
*
* @param CustomerEmailInterface $email Customer's E-Mail address.
* @param CustomerPasswordInterface $password Customer's password.
* @param DateTime $dateOfBirth Customer's date of birth.
* @param CustomerVatNumberInterface $vatNumber Customer's VAT number.
* @param CustomerCallNumberInterface $telephoneNumber Customer's telephone number.
* @param CustomerCallNumberInterface $faxNumber Customer's fax number.
* @param AddressBlockInterface $addressBlock Customer's address.
* @param KeyValueCollection $addonValues Customer's additional values.
*
* @return Customer Created customer.
* @throws UnexpectedValueException On invalid arguments.
*
* TODO Replaced by Vat Check
* TODO Rename to createNewRegistree
*/
public function createNewRegistree(CustomerEmailInterface $email,
CustomerPasswordInterface $password,
DateTime $dateOfBirth,
CustomerVatNumberInterface $vatNumber,
CustomerCallNumberInterface $telephoneNumber,
CustomerCallNumberInterface $faxNumber,
AddressBlockInterface $addressBlock,
KeyValueCollection $addonValues)
{
if($this->customerRepository->getRegistreeByEmail($email) != null)
{
throw new UnexpectedValueException('E-Mail already used in existing customer.');
}
/* @var Customer $customer */
$customer = $this->customerRepository->getNewCustomer();
$customer->setStatusId($this->customerServiceSettings->getDefaultCustomerStatusId()); // TODO: replaced by vat check?
$customer->setCustomerNumber(MainFactory::create('CustomerNumber', (string)$customer->getId()));
$customer->setGender($addressBlock->getGender());
$customer->setFirstname($addressBlock->getFirstname());
$customer->setLastname($addressBlock->getLastname());
$customer->setEmail($email);
$customer->setPassword($password);
$customer->setDateOfBirth($dateOfBirth);
$customer->setTelephoneNumber($telephoneNumber);
$customer->setFaxNumber($faxNumber);
// import addressBlock data into empty default address
$this->addressBookService->updateAddress($addressBlock, $customer->getDefaultAddress());
$vatNumberStatus = $this->vatNumberValidator->getVatNumberStatusCodeId($vatNumber,
$addressBlock->getCountry()->getId(),
false);
$customer->setVatNumber($vatNumber);
$customer->setVatNumberStatus($vatNumberStatus);
$vatCustomerStatus = $this->vatNumberValidator->getCustomerStatusId($vatNumber,
$addressBlock->getCountry()->getId(),
false);
$customer->setStatusId($vatCustomerStatus);
$customer->addAddonValues($addonValues);
$this->customerRepository->store($customer);
return $customer;
}
/**
* Creates a new guest account with the given parameters.
*
* @param CustomerEmailInterface $email Customer's E-Mail address.
* @param DateTime $dateOfBirth Customer's date of birth.
* @param CustomerVatNumberInterface $vatNumber Customer's VAT number.
* @param CustomerCallNumberInterface $telephoneNumber Customer's telephone number.
* @param CustomerCallNumberInterface $faxNumber Customer's fax number.
* @param AddressBlockInterface $addressBlock Customer's address.
* @param KeyValueCollection $addonValues Customer's additional values.
*
* @return Customer Created guest customer.
* @throws UnexpectedValueException On invalid arguments.
*/
public function createNewGuest(CustomerEmailInterface $email,
DateTime $dateOfBirth,
CustomerVatNumberInterface $vatNumber,
CustomerCallNumberInterface $telephoneNumber,
CustomerCallNumberInterface $faxNumber,
AddressBlockInterface $addressBlock,
KeyValueCollection $addonValues)
{
$this->customerRepository->deleteGuestByEmail($email);
if($this->customerRepository->getRegistreeByEmail($email) != null)
{
throw new UnexpectedValueException('E-Mail already used in existing customer.');
}
/* @var Customer $customer */
$customer = $this->customerRepository->getNewCustomer();
$customer->setGuest(true);
$customer->setStatusId($this->customerServiceSettings->getDefaultGuestStatusId());
$customer->setCustomerNumber(MainFactory::create('CustomerNumber', (string)$customer->getId()));
$customer->setGender($addressBlock->getGender());
$customer->setFirstname($addressBlock->getFirstname());
$customer->setLastname($addressBlock->getLastname());
$customer->setEmail($email);
$customer->setDateOfBirth($dateOfBirth);
$customer->setTelephoneNumber($telephoneNumber);
$customer->setFaxNumber($faxNumber);
// import addressBlock data into empty default address
$this->addressBookService->updateAddress($addressBlock, $customer->getDefaultAddress());
$vatNumberStatus = $this->vatNumberValidator->getVatNumberStatusCodeId($vatNumber,
$addressBlock->getCountry()->getId(),
true);
$customer->setVatNumber($vatNumber);
$customer->setVatNumberStatus($vatNumberStatus);
$vatCustomerStatus = $this->vatNumberValidator->getCustomerStatusId($vatNumber,
$addressBlock->getCountry()->getId(), true);
$customer->setStatusId($vatCustomerStatus);
$customer->addAddonValues($addonValues);
$this->customerRepository->store($customer);
return $customer;
}
/**
* Deletes the customer with the provided ID.
*
* @param IdType $customerId Customer's ID.
*/
public function deleteCustomerById(IdType $customerId)
{
$this->customerRepository->deleteCustomerById($customerId);
}
/**
* Updates customer data.
*
* @param CustomerInterface $customer Customer.
*
* @return CustomerInterface Updated customer.
*
* TODO check if the new email address is used by another record
*/
public function updateCustomer(CustomerInterface $customer)
{
$vatNumberStatus = $this->vatNumberValidator->getVatNumberStatusCodeId($customer->getVatNumber(),
$customer->getDefaultAddress()
->getCountry()
->getId(), false);
$customer->setVatNumberStatus($vatNumberStatus);
$this->customerRepository->store($customer);
return $customer;
}
}