Customer.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Apiconnector;
  3. /**
  4. * Manages the Customer data as datafields for contact.
  5. *
  6. * @SuppressWarnings(PHPMD.TooManyFields)
  7. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  10. */
  11. class Customer extends ContactData
  12. {
  13. /**
  14. * @var \Magento\Customer\Model\Customer
  15. */
  16. public $model;
  17. /**
  18. * @var \Magento\Review\Model\ResourceModel\Review\CollectionFactory
  19. */
  20. public $reviewCollection;
  21. /**
  22. * @var array
  23. */
  24. public $mappingHash;
  25. /**
  26. * @var \Dotdigitalgroup\Email\Helper\Data
  27. */
  28. public $helper;
  29. /**
  30. * @var \Magento\Customer\Model\GroupFactory
  31. */
  32. public $groupFactory;
  33. /**
  34. * @var \Magento\Newsletter\Model\SubscriberFactory
  35. */
  36. public $subscriberFactory;
  37. /**
  38. * @var \Magento\Catalog\Api\Data\CategoryInterfaceFactory
  39. */
  40. public $categoryFactory;
  41. /**
  42. * @var \Magento\Catalog\Api\Data\ProductInterfaceFactory
  43. */
  44. public $productFactory;
  45. /**
  46. * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
  47. */
  48. public $orderCollection;
  49. /**
  50. * @var object
  51. */
  52. public $contactFactory;
  53. /**
  54. * @var array
  55. */
  56. public $subscriberStatus
  57. = [
  58. \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED => 'Subscribed',
  59. \Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE => 'Not Active',
  60. \Magento\Newsletter\Model\Subscriber::STATUS_UNSUBSCRIBED => 'Unsubscribed',
  61. \Magento\Newsletter\Model\Subscriber::STATUS_UNCONFIRMED => 'Unconfirmed',
  62. ];
  63. /**
  64. * @var \Magento\Customer\Model\ResourceModel\Group
  65. */
  66. private $groupResource;
  67. /**
  68. * Customer constructor.
  69. *
  70. * @param \Magento\Catalog\Model\ResourceModel\Product $productResource
  71. * @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
  72. * @param \Magento\Customer\Model\ResourceModel\Group $groupResource
  73. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  74. * @param \Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollectionFactory
  75. * @param \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory
  76. * @param \Magento\Customer\Model\GroupFactory $groupFactory
  77. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  78. * @param \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory
  79. * @param \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory
  80. * @param \Magento\Sales\Model\OrderFactory $orderFactory
  81. * @param \Magento\Sales\Model\ResourceModel\Order $resourceOrder
  82. * @param \Magento\Eav\Model\ConfigFactory $eavConfigFactory
  83. * @param \Dotdigitalgroup\Email\Helper\Config $configHelper
  84. */
  85. public function __construct(
  86. \Magento\Catalog\Model\ResourceModel\Product $productResource,
  87. \Magento\Catalog\Model\ResourceModel\Category $categoryResource,
  88. \Magento\Customer\Model\ResourceModel\Group $groupResource,
  89. \Magento\Store\Model\StoreManagerInterface $storeManager,
  90. \Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollectionFactory,
  91. \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $collectionFactory,
  92. \Magento\Customer\Model\GroupFactory $groupFactory,
  93. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  94. \Magento\Catalog\Api\Data\CategoryInterfaceFactory $categoryFactory,
  95. \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
  96. \Magento\Sales\Model\OrderFactory $orderFactory,
  97. \Magento\Sales\Model\ResourceModel\Order $resourceOrder,
  98. \Magento\Eav\Model\ConfigFactory $eavConfigFactory,
  99. \Dotdigitalgroup\Email\Helper\Config $configHelper
  100. ) {
  101. $this->reviewCollection = $reviewCollectionFactory;
  102. $this->orderCollection = $collectionFactory;
  103. $this->groupFactory = $groupFactory;
  104. $this->subscriberFactory = $subscriberFactory;
  105. $this->groupResource = $groupResource;
  106. parent::__construct(
  107. $storeManager,
  108. $productFactory,
  109. $productResource,
  110. $orderFactory,
  111. $resourceOrder,
  112. $categoryFactory,
  113. $categoryResource,
  114. $eavConfigFactory,
  115. $configHelper
  116. );
  117. }
  118. /**
  119. * Set customer data.
  120. *
  121. * @param \Magento\Customer\Model\Customer customer
  122. *
  123. * @return $this
  124. *
  125. */
  126. public function setContactData($customer)
  127. {
  128. $this->model = $customer;
  129. $this->setReviewCollection();
  130. parent::setContactData($customer);
  131. return $this;
  132. }
  133. /**
  134. * @param string $email
  135. *
  136. * @return null
  137. */
  138. public function setEmail($email)
  139. {
  140. $this->contactData['email'] = $email;
  141. }
  142. /**
  143. * @param string $emailType
  144. *
  145. * @return null
  146. */
  147. public function setEmailType($emailType)
  148. {
  149. $this->contactData['email_type'] = $emailType;
  150. }
  151. /**
  152. * Customer reviews.
  153. *
  154. * @return $this
  155. */
  156. public function setReviewCollection()
  157. {
  158. $customerId = $this->model->getId();
  159. $collection = $this->reviewCollection->create()
  160. ->addCustomerFilter($customerId)
  161. ->setOrder('review_id', 'DESC');
  162. $this->reviewCollection = $collection;
  163. return $this;
  164. }
  165. /**
  166. * Number of reviews.
  167. *
  168. * @return int
  169. */
  170. public function getReviewCount()
  171. {
  172. return count($this->reviewCollection);
  173. }
  174. /**
  175. * Last review date.
  176. *
  177. * @return string
  178. */
  179. public function getLastReviewDate()
  180. {
  181. if ($this->reviewCollection->getSize()) {
  182. $this->reviewCollection->getSelect()->limit(1);
  183. $createdAt = $this->reviewCollection
  184. ->getFirstItem()
  185. ->getCreatedAt();
  186. return $createdAt;
  187. }
  188. return '';
  189. }
  190. /**
  191. * Get customer id.
  192. *
  193. * @return int
  194. */
  195. public function getCustomerId()
  196. {
  197. return $this->model->getId();
  198. }
  199. /**
  200. * Get first name.
  201. *
  202. * @return string
  203. */
  204. public function getFirstname()
  205. {
  206. return $this->model->getFirstname();
  207. }
  208. /**
  209. * Get last name.
  210. *
  211. * @return string
  212. */
  213. public function getLastname()
  214. {
  215. return $this->model->getLastname();
  216. }
  217. /**
  218. * Get date of birth.
  219. *
  220. * @return string
  221. */
  222. public function getDob()
  223. {
  224. return $this->model->getDob();
  225. }
  226. /**
  227. * Get customer gender.
  228. *
  229. * @return bool|string
  230. */
  231. public function getGender()
  232. {
  233. return $this->getCustomerGender();
  234. }
  235. /**
  236. * Get customer prefix.
  237. *
  238. * @return string
  239. */
  240. public function getPrefix()
  241. {
  242. return $this->model->getPrefix();
  243. }
  244. /**
  245. * Get customer suffix.
  246. *
  247. * @return string
  248. */
  249. public function getSuffix()
  250. {
  251. return $this->model->getSuffix();
  252. }
  253. /**
  254. * Get customer created at date.
  255. *
  256. * @return string
  257. */
  258. public function getCreatedAt()
  259. {
  260. return $this->model->getCreatedAt();
  261. }
  262. /**
  263. * Get customer last logged in date.
  264. *
  265. * @return string
  266. */
  267. public function getLastLoggedDate()
  268. {
  269. return $this->model->getLastLoggedDate();
  270. }
  271. /**
  272. * Get billing address line 1.
  273. *
  274. * @return string
  275. */
  276. public function getBillingAddress1()
  277. {
  278. return $this->getStreet($this->model->getBillingStreet(), 1);
  279. }
  280. /**
  281. * Get billing address line 2.
  282. *
  283. * @return string
  284. */
  285. public function getBillingAddress2()
  286. {
  287. return $this->getStreet($this->model->getBillingStreet(), 2);
  288. }
  289. /**
  290. * Get billing city.
  291. *
  292. * @return string
  293. */
  294. public function getBillingCity()
  295. {
  296. return $this->model->getBillingCity();
  297. }
  298. /**
  299. * Get billing country.
  300. *
  301. * @return string
  302. */
  303. public function getBillingCountry()
  304. {
  305. return $this->model->getBillingCountryCode();
  306. }
  307. /**
  308. * Get billing state.
  309. *
  310. * @return string
  311. */
  312. public function getBillingState()
  313. {
  314. return $this->model->getBillingRegion();
  315. }
  316. /**
  317. * Get billing postcode.
  318. *
  319. * @return string
  320. */
  321. public function getBillingPostcode()
  322. {
  323. return $this->model->getBillingPostcode();
  324. }
  325. /**
  326. * Get billing phone.
  327. *
  328. * @return string
  329. */
  330. public function getBillingTelephone()
  331. {
  332. return $this->model->getBillingTelephone();
  333. }
  334. /**
  335. * Get delivery address line 1.
  336. *
  337. * @return string
  338. */
  339. public function getDeliveryAddress1()
  340. {
  341. return $this->getStreet($this->model->getShippingStreet(), 1);
  342. }
  343. /**
  344. * Get delivery addrss line 2.
  345. *
  346. * @return string
  347. */
  348. public function getDeliveryAddress2()
  349. {
  350. return $this->getStreet($this->model->getShippingStreet(), 2);
  351. }
  352. /**
  353. * Get delivery city.
  354. *
  355. * @return string
  356. */
  357. public function getDeliveryCity()
  358. {
  359. return $this->model->getShippingCity();
  360. }
  361. /**
  362. * Get delivery country.
  363. *
  364. * @return string
  365. */
  366. public function getDeliveryCountry()
  367. {
  368. return $this->model->getShippingCountryCode();
  369. }
  370. /**
  371. * Get delivery state.
  372. *
  373. * @return string
  374. */
  375. public function getDeliveryState()
  376. {
  377. return $this->model->getShippingRegion();
  378. }
  379. /**
  380. * Get delivery postcode.
  381. *
  382. * @return string
  383. */
  384. public function getDeliveryPostcode()
  385. {
  386. return $this->model->getShippingPostcode();
  387. }
  388. /**
  389. * Get delivery phone.
  390. *
  391. * @return string
  392. */
  393. public function getDeliveryTelephone()
  394. {
  395. return $this->model->getShippingTelephone();
  396. }
  397. /**
  398. * Get last quote id.
  399. *
  400. * @return int
  401. */
  402. public function getLastQuoteId()
  403. {
  404. return $this->model->getLastQuoteId();
  405. }
  406. /**
  407. * Get customer title.
  408. *
  409. * @return string
  410. */
  411. public function getTitle()
  412. {
  413. return $this->model->getPrefix();
  414. }
  415. /**
  416. * Total value refunded for the customer.
  417. *
  418. * @return float|int
  419. */
  420. public function getTotalRefund()
  421. {
  422. //filter by customer id
  423. $customerOrders = $this->orderCollection->create()
  424. ->addAttributeToFilter('customer_id', $this->model->getId());
  425. $totalRefunded = 0;
  426. //calculate total refunded
  427. foreach ($customerOrders as $order) {
  428. $refunded = $order->getTotalRefunded();
  429. $totalRefunded += $refunded;
  430. }
  431. return $totalRefunded;
  432. }
  433. /**
  434. * customer gender.
  435. *
  436. * @return bool|string
  437. */
  438. public function getCustomerGender()
  439. {
  440. $genderId = $this->model->getGender();
  441. if (is_numeric($genderId)) {
  442. $gender = $this->model->getAttribute('gender')
  443. ->getSource()->getOptionText($genderId);
  444. return $gender;
  445. }
  446. return '';
  447. }
  448. /**
  449. * @param string $street
  450. * @param int $line
  451. * @return void
  452. */
  453. public function getStreet($street, $line)
  454. {
  455. $street = explode("\n", $street);
  456. if (isset($street[$line - 1])) {
  457. return $street[$line - 1];
  458. }
  459. return '';
  460. }
  461. /**
  462. * @return string
  463. */
  464. public function getCustomerGroup()
  465. {
  466. $groupId = $this->model->getGroupId();
  467. $groupModel = $this->groupFactory->create();
  468. $this->groupResource->load($groupModel, $groupId);
  469. if ($groupModel) {
  470. return $groupModel->getCode();
  471. }
  472. return '';
  473. }
  474. /**
  475. * Subscriber status for Customer.
  476. *
  477. * @return boolean|string
  478. */
  479. public function getSubscriberStatus()
  480. {
  481. $subscriberModel = $this->subscriberFactory->create()
  482. ->loadByCustomerId($this->model->getId());
  483. if ($subscriberModel->getCustomerId()) {
  484. return $this->subscriberStatus[$subscriberModel->getSubscriberStatus()];
  485. }
  486. return false;
  487. }
  488. /**
  489. * Get billing company name.
  490. *
  491. * @return string
  492. */
  493. public function getBillingCompany()
  494. {
  495. return $this->model->getBillingCompany();
  496. }
  497. /**
  498. * Get shipping company name.
  499. *
  500. * @return string
  501. */
  502. public function getDeliveryCompany()
  503. {
  504. return $this->model->getShippingCompany();
  505. }
  506. /**
  507. * @param string $method
  508. * @param array $args
  509. * @return mixed
  510. */
  511. public function __call($method, $args)
  512. {
  513. return call_user_func_array([$this->model, $method], $args);
  514. }
  515. }