Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Settlement.php
Go to the documentation of this file.
1 <?php
8 
13 {
19  protected $_rowsTable;
20 
24  protected $_coreDate;
25 
31  public function __construct(
32  \Magento\Framework\Model\ResourceModel\Db\Context $context,
33  \Magento\Framework\Stdlib\DateTime\DateTime $coreDate,
34  $connectionName = null
35  ) {
36  $this->_coreDate = $coreDate;
37  parent::__construct($context, $connectionName);
38  }
39 
45  protected function _construct()
46  {
47  $this->_init('paypal_settlement_report', 'report_id');
48  $this->_rowsTable = $this->getTable('paypal_settlement_report_row');
49  }
50 
57  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
58  {
59  $rows = $object->getRows();
60  if (is_array($rows)) {
61  $connection = $this->getConnection();
62  $reportId = (int)$object->getId();
63  try {
64  $connection->beginTransaction();
65  if ($reportId) {
66  $connection->delete($this->_rowsTable, ['report_id = ?' => $reportId]);
67  }
68 
69  foreach (array_keys($rows) as $key) {
73  $completionDate = new \DateTime($rows[$key]['transaction_completion_date']);
74  $rows[$key]['transaction_completion_date'] = $completionDate->format('Y-m-d H:i:s');
75  $initiationDate = new \DateTime($rows[$key]['transaction_initiation_date']);
76  $rows[$key]['transaction_initiation_date'] = $initiationDate->format('Y-m-d H:i:s');
77  /*
78  * Converting numeric
79  */
80  $rows[$key]['fee_amount'] = (double)$rows[$key]['fee_amount'];
81  /*
82  * Setting reportId
83  */
84  $rows[$key]['report_id'] = $reportId;
85  }
86  if (!empty($rows)) {
87  $connection->insertMultiple($this->_rowsTable, $rows);
88  }
89  $connection->commit();
90  } catch (\Exception $e) {
91  $connection->rollBack();
92  }
93  }
94 
95  return $this;
96  }
97 
106  public function loadByAccountAndDate(\Magento\Paypal\Model\Report\Settlement $report, $accountId, $reportDate)
107  {
108  $connection = $this->getConnection();
109  $select = $connection->select()->from(
110  $this->getMainTable()
111  )->where(
112  'account_id = :account_id'
113  )->where(
114  'report_date = :report_date'
115  );
116 
117  $data = $connection->fetchRow($select, [':account_id' => $accountId, ':report_date' => $reportDate]);
118  if ($data) {
119  $report->addData($data);
120  }
121 
122  return $this;
123  }
124 }
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Settlement.php:57
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\DateTime\DateTime $coreDate, $connectionName=null)
Definition: Settlement.php:31
loadByAccountAndDate(\Magento\Paypal\Model\Report\Settlement $report, $accountId, $reportDate)
Definition: Settlement.php:106
$connection
Definition: bulk.php:13