Ecuasolutions::saveInvoice()   B
last analyzed

Complexity

Conditions 7
Paths 20

Size

Total Lines 93
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 67
c 3
b 0
f 0
nc 20
nop 1
dl 0
loc 93
rs 7.7866

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Services;
4
5
use App\Interfaces\InvoicingInterface;
6
use App\Models\Book;
7
use App\Models\Enrollment;
8
use App\Models\Fee;
9
use App\Models\Invoice;
10
use GuzzleHttp\Client;
11
use Illuminate\Support\Facades\Log;
12
13
class Ecuasolutions implements InvoicingInterface
14
{
15
    public function status(): bool
16
    {
17
        // TODO : need to find another solution to ping the server before making the request
18
        return true;
19
    }
20
21
    public function saveInvoice(Invoice $invoice): ?string
22
    {
23
        $ivkardex = [];
24
        $pckardex = [];
25
26
        foreach ($invoice->payments as $p => $payment) {
27
            $pckardex[$p] = [
28
                'codforma' => $payment->payment_method,
29
                'valor' => $payment->value,
30
                'fechaemision' => $invoice->created_at,
31
                'fechavenci' => $invoice->created_at,
32
                'observacion' => $payment->comment,
33
                'codprovcli' => $invoice->client_idnumber,
34
            ];
35
        }
36
37
        foreach ($invoice->invoiceDetails as $po => $product) {
38
            if ($product->product instanceof Enrollment) {
39
                $ivkardex[] = [
40
                    'codinventario' => $product->product_code,
41
                    'codbodega' => 'MAT',
42
                    'cantidad' => 1,
43
                    'descuento' => 0,
44
                    'iva' => 0,
45
                    'preciototal' => $product->final_price,
46
                    'valoriva' => 0,
47
                ];
48
            } elseif ($product->product instanceof Fee) {
49
                $ivkardex[] = [
50
                    'codinventario' => $product->product_code,
51
                    'codbodega' => 'MAT',
52
                    'cantidad' => 1,
53
                    'descuento' => 0,
54
                    'iva' => 0,
55
                    'preciototal' => $product->final_price,
56
                    'valoriva' => 0,
57
                ];
58
            } elseif ($product->product instanceof Book) {
59
                $ivkardex[] = [
60
                    'codinventario' => $product->product_code,
61
                    'codbodega' => 'MAT',
62
                    'cantidad' => 1,
63
                    'descuento' => 0,
64
                    'iva' => 0,
65
                    'preciototal' => $product->final_price,
66
                    'valoriva' => 0,
67
                ];
68
            }
69
        }
70
71
        $body = [
72
            'codtrans' => 'FE',
73
            // was 'OP'
74
            'numtrans' => $invoice->id,
75
            'fechatrans' => $invoice->created_at,
76
            'horatrans' => $invoice->created_at,
77
            'descripcion' => 'Facturado desde el academico por '.backpack_user()->firstname.' '.backpack_user()->lastname,
0 ignored issues
show
Bug introduced by
Accessing firstname on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
Accessing lastname on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
78
            'codusuario' => 'web',
79
            'codprovcli' => $invoice->client_idnumber,
80
            // si existe, se busca el cliente. Si no lo creamos.
81
            'nombre' => $invoice->client_name,
82
            'direccion' => $invoice->client_address,
83
            'telefono' => $invoice->client_phone,
84
            'email' => $invoice->client_email,
85
            'codvendedor' => '',
86
            'ivkardex' => $ivkardex,
87
            'pckardex' => $pckardex,
88
        ];
89
90
        $client = new Client(['debug' => true,
91
            'connect_timeout' => 20, ]);
92
93
        $serverurl = config('invoicing.ecuasolutions.url');
94
95
        Log::info($body);
96
97
        $response = $client->post($serverurl, [
98
            'headers' => [
99
                'authorization' => config('invoicing.ecuasolutions.key'),
100
                'Content-Type' => 'application/json',
101
            ],
102
            'json' => $body,
103
        ]);
104
105
        Log::info('Sending data to accounting');
106
107
        if ($response->getBody()) {
108
            $code = json_decode(preg_replace('/[\\x00-\\x1F\\x80-\\xFF]/', '', $response->getBody()), true, 512, JSON_THROW_ON_ERROR);
109
        }
110
111
        Log::info($response->getBody());
112
113
        return $code['mensaje'] ?? null;
114
    }
115
}
116