1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Paypal
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * Report settlement resource model
30: *
31: * @category Mage
32: * @package Mage_Paypal
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Paypal_Model_Resource_Report_Settlement extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * Table name
39: *
40: * @var string
41: */
42: protected $_rowsTable;
43:
44: /**
45: * Init main table
46: *
47: */
48: protected function _construct()
49: {
50: $this->_init('paypal/settlement_report', 'report_id');
51: $this->_rowsTable = $this->getTable('paypal/settlement_report_row');
52: }
53:
54: /**
55: * Save report rows collected in settlement model
56: *
57: * @param Mage_Paypal_Model_Report_Settlement $object
58: * @return Mage_Paypal_Model_Resource_Report_Settlement
59: */
60: protected function _afterSave(Mage_Core_Model_Abstract $object)
61: {
62: $rows = $object->getRows();
63: if (is_array($rows)) {
64: $adapter = $this->_getWriteAdapter();
65: $reportId = (int)$object->getId();
66: try {
67: $adapter->beginTransaction();
68: if ($reportId) {
69: $adapter->delete($this->_rowsTable, array('report_id = ?' => $reportId));
70: }
71: /** @var $date Mage_Core_Model_Date */
72: $date = Mage::getSingleton('core/date');
73:
74: foreach ($rows as $key => $row) {
75: /*
76: * Converting dates
77: */
78: $completionDate = new Zend_Date($rows[$key]['transaction_completion_date']);
79: $rows[$key]['transaction_completion_date'] = $date->date(null, $completionDate->getTimestamp());
80: $initiationDate = new Zend_Date($rows[$key]['transaction_initiation_date']);
81: $rows[$key]['transaction_initiation_date'] = $date->date(null, $initiationDate->getTimestamp());
82: /*
83: * Converting numeric
84: */
85: $rows[$key]['fee_amount'] = (float)$rows[$key]['fee_amount'];
86: /*
87: * Setting reportId
88: */
89: $rows[$key]['report_id'] = $reportId;
90: }
91: if (!empty($rows)) {
92: $adapter->insertMultiple($this->_rowsTable, $rows);
93: }
94: $adapter->commit();
95: } catch (Exception $e) {
96: $adapter->rollback();
97: }
98: }
99:
100: return $this;
101: }
102:
103: /**
104: * Check if report with same account and report date already fetched
105: *
106: * @param Mage_Paypal_Model_Report_Settlement $report
107: * @param string $accountId
108: * @param string $reportDate
109: * @return Mage_Paypal_Model_Resource_Report_Settlement
110: */
111: public function loadByAccountAndDate(Mage_Paypal_Model_Report_Settlement $report, $accountId, $reportDate)
112: {
113: $adapter = $this->_getReadAdapter();
114: $select = $adapter->select()
115: ->from($this->getMainTable())
116: ->where('account_id = :account_id')
117: ->where('report_date = :report_date');
118:
119: $data = $adapter->fetchRow($select, array(':account_id' => $accountId, ':report_date' => $reportDate));
120: if ($data) {
121: $report->addData($data);
122: }
123:
124: return $this;
125: }
126: }
127: