CustomerCode.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @copyright Vertex. All rights reserved. https://www.vertexinc.com/
  4. * @author Mediotype https://www.mediotype.com/
  5. */
  6. namespace Vertex\Tax\Model\ResourceModel;
  7. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  8. use Magento\Framework\Model\ResourceModel\Db\Context;
  9. use Vertex\Tax\Model\ResourceModel\CustomerCode\Collection;
  10. use Vertex\Tax\Model\ResourceModel\CustomerCode\CollectionFactory;
  11. /**
  12. * Performs Datastore-related actions for the CustomerCode repository
  13. */
  14. class CustomerCode extends AbstractDb
  15. {
  16. const TABLE = 'vertex_customer_code';
  17. const FIELD_ID = 'customer_id';
  18. const FIELD_CODE = 'customer_code';
  19. /** @var CollectionFactory */
  20. private $collectionFactory;
  21. /**
  22. * @param Context $context
  23. * @param CollectionFactory $collectionFactory
  24. * @param string|null $connectionName
  25. */
  26. public function __construct(Context $context, CollectionFactory $collectionFactory, $connectionName = null)
  27. {
  28. parent::__construct($context, $connectionName);
  29. $this->collectionFactory = $collectionFactory;
  30. }
  31. /**
  32. * @inheritdoc
  33. *
  34. * MEQP2 Warning: Protected method. Needed to override AbstractDb's _construct
  35. */
  36. protected function _construct()
  37. {
  38. $this->_isPkAutoIncrement = false;
  39. $this->_init(static::TABLE, static::FIELD_ID);
  40. }
  41. /**
  42. * Retrieve a list of Customer Codes indexed by Customer ID
  43. *
  44. * @param int[] $customerIds
  45. * @return \Vertex\Tax\Model\Data\CustomerCode[]
  46. */
  47. public function getArrayByCustomerIds($customerIds)
  48. {
  49. /** @var Collection $collection */
  50. $collection = $this->collectionFactory->create();
  51. $collection->addFieldToFilter(static::FIELD_ID, ['in' => $customerIds]);
  52. $collection->load();
  53. $result = [];
  54. foreach ($collection->getItems() as $item) {
  55. /** @var \Vertex\Tax\Model\Data\CustomerCode $item */
  56. $result[$item->getCustomerId()] = $item;
  57. }
  58. return $result;
  59. }
  60. }