Passed
Push — master ( a098e0...40eb44 )
by Florian
03:06 queued 10s
created

Database::getStateByStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Helper;
28
29
use Magento\Framework\DB\Adapter\AdapterInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\DB\Adapter\AdapterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use Magento\Quote\Api\Data\AddressInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Api\Data\AddressInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use Magento\Store\Model\ScopeInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\ScopeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
33
/**
34
 * Helper class for everything that has to do with database connections
35
 */
36
class Database extends \Payone\Core\Helper\Base
37
{
38
    /**
39
     * Database connection resource
40
     *
41
     * @var \Magento\Framework\App\ResourceConnection
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\ResourceConnection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
     */
43
    protected $databaseResource;
44
45
    /**
46
     * Constructor
47
     *
48
     * @param \Magento\Framework\App\Helper\Context      $context
49
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
50
     * @param \Payone\Core\Helper\Shop                   $shopHelper
51
     * @param \Magento\Framework\App\ResourceConnection  $resource
52
     */
53
    public function __construct(
54
        \Magento\Framework\App\Helper\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Helper\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
56
        \Payone\Core\Helper\Shop $shopHelper,
57
        \Magento\Framework\App\ResourceConnection $resource
58
    ) {
59
        parent::__construct($context, $storeManager, $shopHelper);
60
        $this->databaseResource = $resource;
61
    }
62
63
    /**
64
     * Return database connection
65
     *
66
     * @return AdapterInterface
67
     */
68
    protected function getDb()
69
    {
70
        return $this->databaseResource->getConnection();
71
    }
72
73
    /**
74
     * Get the state for a given status from the database
75
     *
76
     * @param  string $sStatus
77
     * @return string
78
     */
79
    public function getStateByStatus($sStatus)
80
    {
81
        $oSelect = $this->getDb()
82
            ->select()
83
            ->from($this->databaseResource->getTableName('sales_order_status_state'), ['state'])
84
            ->where("status = :status")
85
            ->limit(1);
86
        return $this->getDb()->fetchOne($oSelect, ['status' => $sStatus]);
87
    }
88
89
    /**
90
     * Get the order increment id by the given TransactionStatus txid
91
     *
92
     * @param  string $sTxid
93
     * @return string
94
     */
95
    public function getOrderIncrementIdByTxid($sTxid)
96
    {
97
        $oSelect = $this->getDb()
98
            ->select()
99
            ->from($this->databaseResource->getTableName('payone_protocol_api'), ['order_id'])
100
            ->where("txid = :txid")
101
            ->limit(1);
102
        return $this->getDb()->fetchOne($oSelect, ['txid' => $sTxid]);
103
    }
104
105
    /**
106
     * Get module info from db
107
     *
108
     * @return array
109
     */
110
    public function getModuleInfo()
111
    {
112
        $oSelect = $this->getDb()
113
            ->select()
114
            ->from($this->databaseResource->getTableName('setup_module'), ['module', 'schema_version'])
115
            ->where("module NOT LIKE 'Magento_%'");
116
        return $this->getDb()->fetchAll($oSelect);
117
    }
118
119
    /**
120
     * Get increment_id from order by given order id
121
     *
122
     * @param  string $sOrderId
123
     * @return string|bool
124
     */
125
    public function getIncrementIdByOrderId($sOrderId)
126
    {
127
        $oSelect = $this->getDb()
128
            ->select()
129
            ->from($this->databaseResource->getTableName('sales_order'), ['increment_id'])
130
            ->where("entity_id = :entity_id")
131
            ->limit(1);
132
        return $this->getDb()->fetchOne($oSelect, ['entity_id' => $sOrderId]);
133
    }
134
135
    /**
136
     * Get payone user id by given customer nr
137
     *
138
     * @param  string $sCustNr
139
     * @return string
140
     */
141
    public function getPayoneUserIdByCustNr($sCustNr)
142
    {
143
        $oSelect = $this->getDb()
144
            ->select()
145
            ->from($this->databaseResource->getTableName('payone_protocol_transactionstatus'), ['userid'])
146
            ->where("customerid = :customerid")
147
            ->limit(1);
148
        return $this->getDb()->fetchOne($oSelect, ['customerid' => $sCustNr]);
149
    }
150
151
    /**
152
     * Get the next sequencenumber for this transaction
153
     *
154
     * @param  int $iTxid
155
     * @return int
156
     */
157
    public function getSequenceNumber($iTxid)
158
    {
159
        $oSelect = $this->getDb()
160
            ->select()
161
            ->from($this->databaseResource->getTableName('payone_protocol_transactionstatus'), ['MAX(sequencenumber)'])
162
            ->where("txid = :txid");
163
        $iCount = $this->getDb()->fetchOne($oSelect, ['txid' => $iTxid]);
164
        if ($iCount === null) {
165
            return 0;
166
        }
167
        return $iCount + 1;
168
    }
169
170
    /**
171
     * Helper method to get parameter from the config directly from db without cache
172
     *
173
     * @param  string $sKey
174
     * @param  string $sGroup
175
     * @param  string $sSection
176
     * @param  string $sScopeId
177
     * @return string
178
     */
179
    public function getConfigParamWithoutCache($sKey, $sGroup = 'global', $sSection = 'payone_general', $sScopeId = null)
180
    {
181
        $oSelect = $this->getDb()
182
            ->select()
183
            ->from($this->databaseResource->getTableName('core_config_data'), ['value'])
184
            ->where("path = :path")
185
            ->where("scope = :scope")
186
            ->where("scope_id = :scope_id");
187
        $sPath = $sSection."/".$sGroup."/".$sKey;
188
        $sScope = ScopeInterface::SCOPE_STORES;
189
        if (!$sScopeId) {
190
            $sScopeId = $this->storeManager->getStore()->getId();
191
        }
192
        return $this->getDb()->fetchOne($oSelect, ['path' => $sPath, 'scope' => $sScope, 'scope_id' => $sScopeId]);
193
    }
194
195
    /**
196
     * Get the address status from a previous order address
197
     *
198
     * @param  AddressInterface $oAddress
199
     * @param  bool             $blIsCreditrating
200
     * @return string
201
     */
202
    public function getOldAddressStatus(AddressInterface $oAddress, $blIsCreditrating = true)
203
    {
204
        $sSelectField = 'payone_protect_score';
205
        if ($blIsCreditrating === false) {
206
            $sSelectField = 'payone_addresscheck_score';
207
        }
208
209
        $aParams = [
210
            'firstname' => $oAddress->getFirstname(),
211
            'lastname' => $oAddress->getLastname(),
212
            'street' => $oAddress->getStreet()[0],
213
            'city' => $oAddress->getCity(),
214
            'region' => $oAddress->getRegion(),
215
            'postcode' => $oAddress->getPostcode(),
216
            'country_id' => $oAddress->getCountryId(),
217
        ];
218
219
        $oSelect = $this->getDb()
220
            ->select()
221
            ->from($this->databaseResource->getTableName('quote_address'), [$sSelectField])
222
            ->where("firstname = :firstname")
223
            ->where("lastname = :lastname")
224
            ->where("street = :street")
225
            ->where("city = :city")
226
            ->where("region = :region")
227
            ->where("postcode = :postcode")
228
            ->where("country_id = :country_id")
229
            ->where($sSelectField." != ''")
230
            ->order('address_id DESC')
231
            ->limit(1);
232
233
        if (!empty($oAddress->getId())) {
234
            $oSelect->where("address_id != :curr_id");
235
            $aParams['curr_id'] = $oAddress->getId();
236
        }
237
        if (!empty($oAddress->getCustomerId())) {
238
            $oSelect->where("customer_id = :cust_id");
239
            $aParams['cust_id'] = $oAddress->getCustomerId();
240
        }
241
        if (!empty($oAddress->getAddressType())) {
242
            $oSelect->where("address_type = :addr_type");
243
            $aParams['addr_type'] = $oAddress->getAddressType();
244
        }
245
        return $this->getDb()->fetchOne($oSelect, $aParams);
246
    }
247
248
    /**
249
     * Relabel sales payment transaction to substitute order
250
     *
251
     * @param  string $sOldOrderId
252
     * @param  string $sNewOrderId
253
     * @param  string $sNewPaymentId
254
     * @return int
255
     */
256
    public function relabelTransaction($sOldOrderId, $sNewOrderId, $sNewPaymentId)
257
    {
258
        $table = $this->databaseResource->getTableName('sales_payment_transaction');
259
        $data = [
260
            'order_id' => $sNewOrderId,
261
            'payment_id' => $sNewPaymentId
262
        ];
263
        $where = ['order_id = ?' => $sOldOrderId];
264
        return $this->getDb()->update($table, $data, $where);
265
    }
266
267
    /**
268
     * Relabel payone protocol api to substitute order
269
     *
270
     * @param  string $sOldIncrementId
271
     * @param  string $sNewIncrementId
272
     * @return int
273
     */
274
    public function relabelApiProtocol($sOldIncrementId, $sNewIncrementId)
275
    {
276
        $table = $this->databaseResource->getTableName('payone_protocol_api');
277
        $data = ['order_id' => $sNewIncrementId];
278
        $where = ['order_id = ?' => $sOldIncrementId];
279
        return $this->getDb()->update($table, $data, $where);
280
    }
281
282
    /**
283
     * Relabel sales order payment to substitute order
284
     *
285
     * @param  string $sOldIncrementId
286
     * @param  string $sNewOrderId
287
     * @return int
288
     */
289
    public function relabelOrderPayment($sOldIncrementId, $sNewOrderId)
290
    {
291
        $oSelect = $this->getDb()
292
            ->select()
293
            ->from(['a' => $this->databaseResource->getTableName('sales_order_payment')], ['last_trans_id'])
294
            ->joinInner(['b' => $this->databaseResource->getTableName('sales_order')], 'a.parent_id = b.entity_id')
295
            ->where("b.increment_id = :incrementId")
296
            ->limit(1);
297
        $sLastTransId = $this->getDb()->fetchOne($oSelect, ['incrementId' => $sOldIncrementId]);
298
299
        $table = $this->databaseResource->getTableName('sales_order_payment');
300
        $data = ['last_trans_id' => $sLastTransId];
301
        $where = ['parent_id = ?' => $sNewOrderId];
302
        return $this->getDb()->update($table, $data, $where);
303
    }
304
305
    /**
306
     * Return not handled transactions by order id
307
     *
308
     * @param  string $sOrderId
309
     * @return array
310
     */
311
    public function getNotHandledTransactionsByOrderId($sOrderId)
312
    {
313
        $oSelect = $this->getDb()
314
            ->select()
315
            ->from($this->databaseResource->getTableName('payone_protocol_transactionstatus'), ['id'])
316
            ->where("order_id = :orderId")
317
            ->where("has_been_handled = 0")
318
            ->order('id ASC');
319
        return $this->getDb()->fetchAll($oSelect, ['orderId' => $sOrderId]);
320
    }
321
322
    /**
323
     * Will search the DB for the incrementId of a substitute order by the given incrementId of the canceled order
324
     *
325
     * @param  string $sIncrementId
326
     * @return string
327
     */
328
    public function getSubstituteOrderIncrementId($sIncrementId)
329
    {
330
        $oSelect = $this->getDb()
331
            ->select()
332
            ->from($this->databaseResource->getTableName('sales_order'), ['increment_id'])
333
            ->where("payone_cancel_substitute_increment_id = :incrementId");
334
        return $this->getDb()->fetchOne($oSelect, ['incrementId' => $sIncrementId]);
335
    }
336
}
337