Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 108
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
IfexController
0.00% covered (danger)
0.00%
0 / 108
0.00% covered (danger)
0.00%
0 / 4
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 syncQuotations
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
30
 createQuotation
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
30
 updateQuotation
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Http\Controllers\Controller;
6use App\Models\ifex\TblClientes;
7use App\Models\ifex\TblPresupuestos;
8use App\Models\TblIfexLastUpdate;
9use App\Models\TblQuotations;
10use App\Services\PresupuestosService;
11use Carbon\Carbon;
12use Illuminate\Support\Facades\Log;
13use Mockery\Exception;
14
15
16class IfexController extends Controller
17{
18    protected $presupuestosService;
19    protected $statusNormalize;
20
21    public function __construct(PresupuestosService $presupuestosService)
22    {
23        $this->presupuestosService = $presupuestosService;
24
25        $this->statusNormalize = [
26            "O" => 1, //Oportunidad => Nuevo
27            "P" => 11, //Pendiente => Listo para enviar
28            "C" => 2, //Pendiente Cliente => Enviado
29            "A" => 3, //Aceptado => Aceptado
30            "E" => 12, //En ejecución => En proceso
31            "R" => 4, //Rechazado => Rechazado
32            "N" => 5, //Anulado => Anulado
33            "F" => 3 //Finalizado => Aceptado
34        ];
35
36    }
37
38    public function syncQuotations(){
39        try{
40            $lastUpdate = TblIfexLastUpdate::where("id", 1)->first();
41            $lastDateOnly = Carbon::parse($lastUpdate->last_date)->format('Y-m-d');
42
43            $quotationsToSync = TblPresupuestos::where('Fecha_Presupuesto', '>=', $lastDateOnly)
44                ->orWhere('Ultima_Modificacion', '>=', $lastUpdate->last_date)
45                ->get();
46
47            foreach($quotationsToSync as $quotation){
48                $client = TblClientes::where("Codigo", $quotation->Id_Cliente)->first();
49                if(TblQuotations::where("internal_quote_id", $quotation->Codigo)->exists()){
50                    $this->updateQuotation($quotation, $client);
51                }
52                $this->createQuotation($quotation, $client);
53
54            }
55
56            TblIfexLastUpdate::where("id", 1)->first()->update(
57                array(
58                    "last_date" => Carbon::now()->format('Y-m-d H:i:s')
59                )
60            );
61
62            return ['success' => true, 'quotations' => $quotationsToSync];
63        }catch (\Exception $e){
64            Log::channel('ifex')->error('Error updating quotations: ' . $e->getMessage(), [
65                'exception' => $e
66            ]);
67
68            return response()->json([
69                'success' => false,
70                'message' => 'Failed to update ifex quotations',
71                'error' => $e->getMessage(),
72                'error_code' => $e->getCode() ?: 500
73            ], 500);
74        }
75    }
76
77    public function createQuotation($quotation, $client){
78        //instalaciones 6 25O
79        //correctivos 3 25C
80        //mantenimiento 1 25P
81        $type = null;
82        if (strpos($quotation->Codigo, "25O") !== false) {
83            $type = 6;
84        }
85
86        if (strpos($quotation->Codigo, "25C") !== false) {
87            $type = 3;
88        }
89
90        if (strpos($quotation->Codigo, "25P") !== false) {
91            $type = 1;
92        }
93        if (strpos($quotation->Codigo, "G25") !== false) {
94            return;
95        }
96
97        $generateNumber = $this->presupuestosService->generateQuoteId(30);
98
99        $g3wNewId = $generateNumber['id'];
100        $newQuoteId = $generateNumber['number'];
101
102        $ifexArray = array(
103            'internal_quote_id' => $quotation->Codigo,
104            'quote_id' => $newQuoteId,
105            'company_id' => 30,
106            'customer_type_id' => 2,
107            'segment_id' => null,
108            'budget_type_id' => $type,
109            'budget_status_id' => $this->statusNormalize[$quotation->Estado],
110            'source_id' => 55,
111            'client' => $client->Nombre?? null,
112            'phone_number' => $client->Persona_Contacto_Telefono?? null,
113            'email' => $client->PersonaContactoMail?? null,
114            'issue_date' => $quotation->Fecha_Presupuesto?? null,
115            'request_date' => $quotation->Fecha_Alta?? null,
116            'acceptance_date' => $quotation->Fecha_Aceptacion?? null,
117            'amount' => $quotation->Base_Imponibe?? null,
118            'last_follow_up_date' => null,
119            'commercial' => "Ifex",
120            'created_at' => $quotation->Fecha_Presupuesto?? null,
121            'created_by' => "Ifex",
122            'has_attachment' => 0,
123            'cost_of_labor' => 0,
124            'total_cost_of_job' => 0,
125            'invoice_margin' => 0,
126            'margin_for_the_company' => 0,
127            'revenue_per_date_per_worked' => 0,
128            'gross_margin' => 100,
129            'labor_percentage' => 0,
130            'sync_import' => 1,
131            'box_work_g3w' => null,
132            'for_add' => 0,
133            'for_approval' => 0,
134        );
135
136        TblQuotations::where('id', $g3wNewId)->update($ifexArray);
137    }
138
139    public function updateQuotation($quotation, $client){
140        $fstQuotation = TblQuotations::where("internal_quote_id", $quotation->Codigo)->first();
141
142        //instalaciones 6 25O
143        //correctivos 3 25C
144        //mantenimiento 1 25P
145        $type = null;
146        if (strpos($quotation->Codigo, "25O") !== false) {
147            $type = 6;
148        }
149
150        if (strpos($quotation->Codigo, "25C") !== false) {
151            $type = 3;
152        }
153
154        if (strpos($quotation->Codigo, "25P") !== false) {
155            $type = 1;
156        }
157        if (strpos($quotation->Codigo, "G25") !== false) {
158            return;
159        }
160
161        $ifexArray = array(
162            'internal_quote_id' => $quotation->Codigo,
163            'budget_type_id' => $type,
164            'budget_status_id' => $this->statusNormalize[$quotation->Estado],
165            'client' => $client->Nombre?? null,
166            'phone_number' => $client->Persona_Contacto_Telefono?? null,
167            'email' => $client->PersonaContactoMail?? null,
168            'issue_date' => $quotation->Fecha_Presupuesto?? null,
169            'request_date' => $quotation->Fecha_Alta?? null,
170            'acceptance_date' => $quotation->Fecha_Aceptacion?? null,
171            'amount' => $quotation->Base_Imponibe?? null,
172            'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
173            'updated_by' => "Ifex"
174        );
175
176        $fstQuotation->update($ifexArray);
177
178    }
179
180}