Conditions | 7 |
Paths | 20 |
Total Lines | 93 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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, |
||
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 | } |
||
116 |