Issues (104)

Helper/Data.php (7 issues)

1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\SubSellerMagento\Helper;
10
11
use Getnet\SubSellerMagento\Api\Data\SubSellerInterface;
12
use Getnet\SubSellerMagento\Model\Config;
13
use Magento\Directory\Model\Region;
14
use Magento\Directory\Model\RegionFactory;
15
use Magento\Store\Model\Store;
16
17
/**
18
 * Sub Seller Helper.
19
 */
20
class Data extends \Magento\Framework\App\Helper\AbstractHelper
21
{
22
    /**
23
     * @var Config
24
     */
25
    protected $configSubSeller;
26
27
    /**
28
     * @var RegionFactory
29
     */
30
    protected $regionFactory;
31
32
    /**
33
     * @var Region
34
     */
35
    protected $directoryRegion;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param Config        $configSubSeller
41
     * @param RegionFactory $regionFactory
42
     * @param Region        $directoryRegion
43
     */
44
    public function __construct(
45
        Config $configSubSeller,
46
        RegionFactory $regionFactory,
47
        Region $directoryRegion
48
    ) {
49
        $this->configSubSeller = $configSubSeller;
50
        $this->regionFactory = $regionFactory;
51
        $this->directoryRegion = $directoryRegion;
52
    }
53
54
    /**
55
     * Get Merchant Id.
56
     *
57
     * @param null|string|bool|int|Store $store
58
     *
59
     * @return string
60
     */
61
    public function getMerchantId($store = null)
62
    {
63
        return $this->configSubSeller->getMerchantId($store);
64
    }
65
66
    /**
67
     * Get Region Name By Region Id.
68
     *
69
     * @param string $regionId
70
     *
71
     * @return string
72
     */
73
    public function getRegionNameByRegionId(string $regionId)
74
    {
75
        return $this->directoryRegion->load($regionId)->getCode();
0 ignored issues
show
$regionId of type string is incompatible with the type integer expected by parameter $modelId of Magento\Framework\Model\AbstractModel::load(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        return $this->directoryRegion->load(/** @scrutinizer ignore-type */ $regionId)->getCode();
Loading history...
Deprecated Code introduced by
The function Magento\Framework\Model\AbstractModel::load() has been deprecated: 100.1.0 because entities must not be responsible for their own loading. Service contracts should persist entities. Use resource model "load" or collections to implement service contract model loading operations. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

75
        return /** @scrutinizer ignore-deprecated */ $this->directoryRegion->load($regionId)->getCode();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
76
    }
77
78
    /**
79
     * Get Region Id By Region Id.
80
     *
81
     * @param string      $regionName
82
     * @param string|null $countryId
83
     *
84
     * @return string
85
     */
86
    public function getRegionIdByRegionName(string $regionName, string $countryId = 'BR')
87
    {
88
        return $this->regionFactory->create()
89
            ->loadByCode($regionName, $countryId)
90
            ->getId();
91
    }
92
93
    /**
94
     * Get List Commissions Formated.
95
     *
96
     * @param null|string|bool|int|Store $store
97
     *
98
     * @return array|string
99
     */
100
    public function getListCommissionsFormated($store = null)
101
    {
102
        $list = $this->configSubSeller->getListCommissions($store);
103
        $listCommissions = [];
104
105
        foreach ($list as $commission) {
106
            $listCommissions[] = [
107
                'brand'                 => $commission['brand'],
108
                'product'               => $commission['product'],
109
                'commission_percentage' => (int) $commission['commission_percentage'],
110
                'payment_plan'          => (int) $commission['payment_plan'],
111
            ];
112
        }
113
114
        return $listCommissions;
115
    }
116
117
    /**
118
     * Get List Commissions Formated.
119
     *
120
     * @param string $text
121
     *
122
     * @return array
123
     */
124
    public function removeAcento(string $text): string
125
    {
126
        return iconv('UTF-8', 'ASCII//TRANSLIT', $text);
0 ignored issues
show
Bug Best Practice introduced by
The expression return iconv('UTF-8', 'ASCII//TRANSLIT', $text) returns the type string which is incompatible with the documented return type array.
Loading history...
127
    }
128
129
    /**
130
     * Format Data to Send PF.
131
     *
132
     * @param SubSellerInterface $subSeller
133
     * @param string             $typeFormat
134
     *
135
     * @return array
136
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
137
     * @SuppressWarnings(PHPMD.NPathComplexity)
138
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
139
     */
140
    public function formatedDataPJ(
141
        SubSellerInterface $subSeller,
142
        string $typeFormat = null
143
    ) {
144
        $data = [];
0 ignored issues
show
The assignment to $data is dead and can be removed.
Loading history...
145
146
        $addresses = $subSeller->getAddresses();
147
        $businessAddress = $addresses['addresses']['business_address'];
148
        $bankAccounts = $subSeller->getBankAccounts();
149
        $bankAccounts = array_merge($bankAccounts['bank_accounts'], ['type_accounts' => 'unique']);
150
151
        $phone = preg_replace('/[^0-9]/', '', $subSeller->getTelephone());
152
        $namePhone = 'phone';
153
        if (strlen($phone) === 11) {
154
            $namePhone = 'cellphone';
155
        }
156
157
        $data = [
158
            'merchant_id'                  => $subSeller->getMerchantId(),
159
            'subsellerid_ext'              => $subSeller->getCode(),
160
            'email'                        => $subSeller->getEmail(),
161
            'legal_document_number'        => (int) preg_replace('/[^0-9]/', '', $subSeller->getLegalDocumentNumber()),
162
            'legal_name'                   => $this->removeAcento($subSeller->getLegalName()),
0 ignored issues
show
It seems like $subSeller->getLegalName() can also be of type null; however, parameter $text of Getnet\SubSellerMagento\...er\Data::removeAcento() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

162
            'legal_name'                   => $this->removeAcento(/** @scrutinizer ignore-type */ $subSeller->getLegalName()),
Loading history...
163
            'trade_name'                   => $this->removeAcento($subSeller->getLegalName()),
164
            $namePhone                     => [
165
                'area_code'    => $this->getNumberOrDDD($phone, true),
166
                'phone_number' => $this->getNumberOrDDD($phone, false),
167
            ],
168
            'business_address' => [
169
                'mailing_address_equals' => 'S',
170
                'street'                 => $businessAddress['address_street'],
171
                'number'                 => $businessAddress['address_street_number'],
172
                'district'               => $businessAddress['address_street_district'],
173
                'suite'                  => $businessAddress['address_street_complement'],
174
                'city'                   => $businessAddress['address_city'],
175
                'state'                  => $businessAddress['address_region'],
176
                'postal_code'            => preg_replace('/[^0-9]/', '', $businessAddress['address_postcode']),
177
                'country'                => $businessAddress['address_country_id'],
178
            ],
179
            'bank_accounts'                => $bankAccounts,
180
            'payment_plan'                 => $subSeller->getPaymentPlan(),
181
            'accepted_contract'            => $subSeller->getAcceptedContract() ? 'S' : 'N',
182
            'marketplace_store'            => $subSeller->getMarketplaceStore() ? 'S' : 'N',
183
            'occupation'                   => $this->removeAcento($subSeller->getOccupation()),
184
            'list_commissions'             => $this->getListCommissionsFormated(),
185
            'state_fiscal_document_number' => 'ISENTO',
186
            'federal_registration_status'  => 'active',
187
        ];
188
189
        if ($subSeller->getIdExt()) {
190
            $data = array_merge($data, [
191
                'subseller_id' => $subSeller->getIdExt(),
192
            ]);
193
        }
194
195
        if ($typeFormat) {
196
            $data = $this->replaceKeyInData($data, $typeFormat);
197
        }
198
199
        return $data;
200
    }
201
202
    /**
203
     * Format Data to Send PF.
204
     *
205
     * @param SubSellerInterface $subSeller
206
     * @param string             $typeFormat
207
     *
208
     * @return array
209
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
210
     * @SuppressWarnings(PHPMD.NPathComplexity)
211
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
212
     */
213
    public function formatedDataPF(
214
        SubSellerInterface $subSeller,
215
        string $typeFormat = null
216
    ) {
217
        $data = [];
0 ignored issues
show
The assignment to $data is dead and can be removed.
Loading history...
218
219
        $addresses = $subSeller->getAddresses();
220
        $businessAddress = $addresses['addresses']['business_address'];
221
        $bankAccounts = $subSeller->getBankAccounts();
222
        $bankAccounts = array_merge($bankAccounts['bank_accounts'], ['type_accounts' => 'unique']);
223
        $phone = preg_replace('/[^0-9]/', '', $subSeller->getTelephone());
224
        $namePhone = 'phone';
225
        if (strlen($phone) === 11) {
226
            $namePhone = 'cellphone';
227
        }
228
229
        $data = [
230
            'merchant_id'           => $subSeller->getMerchantId(),
231
            'subsellerid_ext'       => $subSeller->getCode(),
232
            'email'                 => $subSeller->getEmail(),
233
            'legal_document_number' => (int) preg_replace('/[^0-9]/', '', $subSeller->getLegalDocumentNumber()),
234
            'legal_name'            => $this->removeAcento($subSeller->getLegalName()),
0 ignored issues
show
It seems like $subSeller->getLegalName() can also be of type null; however, parameter $text of Getnet\SubSellerMagento\...er\Data::removeAcento() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

234
            'legal_name'            => $this->removeAcento(/** @scrutinizer ignore-type */ $subSeller->getLegalName()),
Loading history...
235
            'birth_date'            => $subSeller->getBirthDate(),
236
            'business_address'      => [
237
                'mailing_address_equals' => 'S',
238
                'street'                 => $businessAddress['address_street'],
239
                'number'                 => $businessAddress['address_street_number'],
240
                'district'               => $businessAddress['address_street_district'],
241
                'suite'                  => $businessAddress['address_street_complement'],
242
                'city'                   => $businessAddress['address_city'],
243
                'state'                  => $businessAddress['address_region'],
244
                'postal_code'            => preg_replace('/[^0-9]/', '', $businessAddress['address_postcode']),
245
                'country'                => $businessAddress['address_country_id'],
246
            ],
247
            $namePhone => [
248
                'area_code'    => $this->getNumberOrDDD($phone, true),
249
                'phone_number' => $this->getNumberOrDDD($phone, false),
250
            ],
251
            'bank_accounts'          => $bankAccounts,
252
            'payment_plan'           => $subSeller->getPaymentPlan(),
253
            'accepted_contract'      => $subSeller->getAcceptedContract() ? 'S' : 'N',
254
            'marketplace_store'      => $subSeller->getMarketplaceStore() ? 'S' : 'N',
255
            'occupation'             => $this->removeAcento($subSeller->getOccupation()),
256
            'list_commissions'       => $this->getListCommissionsFormated(),
257
        ];
258
259
        if ($subSeller->getIdExt()) {
260
            $data = array_merge($data, [
261
                'subseller_id' => $subSeller->getIdExt(),
262
            ]);
263
        }
264
265
        if ($typeFormat) {
266
            $data = $this->replaceKeyInData($data, $typeFormat);
267
        }
268
269
        return $data;
270
    }
271
272
    /**
273
     * Format Data to Send.
274
     *
275
     * @param SubSellerInterface $subSeller
276
     * @param bool               $type
277
     * @param string             $typeFormat
278
     *
279
     * @return array
280
     */
281
    public function formatedData(
282
        SubSellerInterface $subSeller,
283
        bool $type,
284
        string $typeFormat = null
285
    ) {
286
        if (!$type) {
287
            return $this->formatedDataPF($subSeller, $typeFormat);
288
        }
289
290
        return $this->formatedDataPJ($subSeller, $typeFormat);
291
    }
292
293
    /**
294
     * Replace Key in Data.
295
     *
296
     * @param array  $data
297
     * @param string $newTypeFormat
298
     *
299
     * @return array
300
     */
301
    public function replaceKeyInData(array $data, string $newTypeFormat): array
302
    {
303
        if ($newTypeFormat === 'seller_update') {
304
            $data['naturalized'] = 'S';
305
            $data['ppe_indication'] = 'not_applied';
306
            $data['ppe_description'] = 'NAO APLICADO';
307
            unset($data['business_address']['mailing_address_equals']);
308
            unset($data['identification_document']['document_issuer_state_id']);
309
            $data['residential_address'] = $data['business_address'];
310
        }
311
312
        return $data;
313
    }
314
315
    /**
316
     * Get Number or DDD.
317
     *
318
     * @param string $param_telefone
319
     * @param bool   $return_ddd
320
     *
321
     * @return string
322
     */
323
    public function getNumberOrDDD($param_telefone, $return_ddd = false)
324
    {
325
        $cust_ddd = '11';
326
        $cust_telephone = preg_replace('/[^0-9]/', '', $param_telefone);
327
        if (strlen($cust_telephone) == 11) {
328
            $str = strlen($cust_telephone) - 9;
329
            $indice = 9;
330
        } else {
331
            $str = strlen($cust_telephone) - 8;
332
            $indice = 8;
333
        }
334
335
        if ($str > 0) {
336
            $cust_ddd = substr($cust_telephone, 0, 2);
337
            $cust_telephone = substr($cust_telephone, $str, $indice);
338
        }
339
        if ($return_ddd === false) {
340
            $number = $cust_telephone;
341
        } else {
342
            $number = $cust_ddd;
343
        }
344
345
        return preg_replace('/[^0-9]/', '', $number);
346
    }
347
}
348