Settlement.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Paypal\Model\ResourceModel\Report;
  7. /**
  8. * Report settlement resource model
  9. */
  10. class Settlement extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  11. {
  12. /**
  13. * Table name
  14. *
  15. * @var string
  16. */
  17. protected $_rowsTable;
  18. /**
  19. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  20. */
  21. protected $_coreDate;
  22. /**
  23. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  24. * @param \Magento\Framework\Stdlib\DateTime\DateTime $coreDate
  25. * @param string $connectionName
  26. */
  27. public function __construct(
  28. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  29. \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
  30. $connectionName = null
  31. ) {
  32. $this->_coreDate = $coreDate;
  33. parent::__construct($context, $connectionName);
  34. }
  35. /**
  36. * Init main table
  37. *
  38. * @return void
  39. */
  40. protected function _construct()
  41. {
  42. $this->_init('paypal_settlement_report', 'report_id');
  43. $this->_rowsTable = $this->getTable('paypal_settlement_report_row');
  44. }
  45. /**
  46. * Save report rows collected in settlement model
  47. *
  48. * @param \Magento\Framework\Model\AbstractModel|\Magento\Paypal\Model\Report\Settlement $object
  49. * @return $this
  50. */
  51. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  52. {
  53. $rows = $object->getRows();
  54. if (is_array($rows)) {
  55. $connection = $this->getConnection();
  56. $reportId = (int)$object->getId();
  57. try {
  58. $connection->beginTransaction();
  59. if ($reportId) {
  60. $connection->delete($this->_rowsTable, ['report_id = ?' => $reportId]);
  61. }
  62. foreach (array_keys($rows) as $key) {
  63. /**
  64. * Converting dates
  65. */
  66. $completionDate = new \DateTime($rows[$key]['transaction_completion_date']);
  67. $rows[$key]['transaction_completion_date'] = $completionDate->format('Y-m-d H:i:s');
  68. $initiationDate = new \DateTime($rows[$key]['transaction_initiation_date']);
  69. $rows[$key]['transaction_initiation_date'] = $initiationDate->format('Y-m-d H:i:s');
  70. /*
  71. * Converting numeric
  72. */
  73. $rows[$key]['fee_amount'] = (double)$rows[$key]['fee_amount'];
  74. /*
  75. * Setting reportId
  76. */
  77. $rows[$key]['report_id'] = $reportId;
  78. }
  79. if (!empty($rows)) {
  80. $connection->insertMultiple($this->_rowsTable, $rows);
  81. }
  82. $connection->commit();
  83. } catch (\Exception $e) {
  84. $connection->rollBack();
  85. }
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Check if report with same account and report date already fetched
  91. *
  92. * @param \Magento\Paypal\Model\Report\Settlement $report
  93. * @param string $accountId
  94. * @param string $reportDate
  95. * @return $this
  96. */
  97. public function loadByAccountAndDate(\Magento\Paypal\Model\Report\Settlement $report, $accountId, $reportDate)
  98. {
  99. $connection = $this->getConnection();
  100. $select = $connection->select()->from(
  101. $this->getMainTable()
  102. )->where(
  103. 'account_id = :account_id'
  104. )->where(
  105. 'report_date = :report_date'
  106. );
  107. $data = $connection->fetchRow($select, [':account_id' => $accountId, ':report_date' => $reportDate]);
  108. if ($data) {
  109. $report->addData($data);
  110. }
  111. return $this;
  112. }
  113. }