Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TblQuotations
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
10.03
0.00% covered (danger)
0.00%
0 / 1
 booted
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 newEloquentBuilder
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCommercialAttribute
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 quotationExist
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace App\Models;
4
5use App\Models\Builders\TblQuotationsBuilder;
6use Illuminate\Database\Eloquent\Factories\HasFactory;
7use Illuminate\Database\Eloquent\Model;
8
9class TblQuotations extends Model
10{
11    use HasFactory;
12
13    protected $table = 'tbl_quotations';
14
15    public $timestamps = false;
16
17    /**
18     * Keep updated_at current on every Eloquent write — the DWH only reloads
19     * quotations whose updated_at changed. Instance saves are handled here;
20     * query-builder updates in TblQuotationsBuilder. A write that sets
21     * updated_at itself keeps its own value.
22     */
23    protected static function booted(): void
24    {
25        static::saving(function (self $quotation): void {
26            if ($quotation->isDirty('updated_at')) {
27                return;
28            }
29            if (! $quotation->exists || $quotation->isDirty()) {
30                $quotation->updated_at = date('Y-m-d H:i:s');
31            }
32        });
33    }
34
35    public function newEloquentBuilder($query): TblQuotationsBuilder
36    {
37        return new TblQuotationsBuilder($query);
38    }
39
40    protected $fillable = [
41        'quote_id',
42        'internal_quote_id',
43        // FIRE-1733: Zeus → Titan sync columns. See ZeusSyncService.
44        'zeus_id',
45        'synced_from_zeus_at',
46        'deleted_at',
47        'company_id',
48        'customer_type_id',
49        'budget_type_id',
50        'budget_status_id',
51        'source_id',
52        'codigo_id',
53        'client',
54        'client_type',
55        'segment_id',
56        'phone_number',
57        'email',
58        'send_channel',
59        'direccion_postal',
60        'sent_date',
61        'wa_message_id',
62        'wa_status',
63        'wa_y_message_id',
64        'wa_y_status',
65        'issue_date',
66        'request_date',
67        'duration',
68        'order_number',
69        'acceptance_date',
70        'amount',
71        'reason_for_not_following_up',
72        'last_follow_up_date',
73        'last_follow_up_comment',
74        'reason_for_rejection_id',
75        'reason_for_rejection',
76        'commercial',
77        'follow_up_commercial',
78        'created_at',
79        'created_by',
80        'updated_at',
81        'updated_by',
82        'for_add',
83        'for_approval',
84        'approval_type',
85        'requires_technical_office',
86        'total_sent',
87        'people_assigned_to_the_job',
88        'duration_of_job_in_days',
89        'estimated_cost_of_materials',
90        'budget_margin_enabled',
91        'labor_manual_override',
92        'question_enabled',
93        'cost_of_labor',
94        'total_cost_of_job',
95        'invoice_margin',
96        'margin_for_the_company',
97        'margin_on_invoice_per_day_per_worker',
98        'revenue_per_date_per_worked',
99        'gross_margin',
100        'labor_percentage',
101        'question_ids',
102        'question_ids_no',
103        'approved_at',
104        'approved_by',
105        'rejected_at',
106        'rejected_by',
107        'approved_at_v2',
108        'approved_by_v2',
109        'rejected_at_v2',
110        'rejected_by_v2',
111        'accepted_at',
112        'accepted_by',
113        'x_message_id',
114        'x_status',
115        'from_company_id',
116        'sync_import',
117        'sync_import_edited',
118        'segment_by_g3w',
119        'source_by_g3w',
120        'status_by_g3w',
121        'type_by_g3w',
122        'from_company_id',
123        'box_work_g3w',
124        'user_create_by_g3w',
125        'user_commercial_by_g3w',
126        'g3w_warning',
127        'g3w_warning_fields',
128        'is_validated',
129        'maintenance_potential',
130        'coverage_contract_signed',
131        'resource_id',
132        'commission_cost',
133        'commission_pct',
134        'reason_id',
135        'reason_for_deletion',
136        'id_solicitud_duplicity',
137        'client_id',
138    ];
139
140    // FIRE-1743: prod rows exist with `commercial` corrupted to the same
141    // name repeated 4× separated by `\n` (e.g. "Solanas - Gallex\nSolanas - …").
142    // The exact culprit path isn't identified, but every legitimate writer
143    // (G3W sync, orders form save) assigns a single user name — a newline in
144    // `commercial` is always corruption. Normalising on write is a cheap
145    // defensive layer that stops the corruption from spreading regardless of
146    // which callsite is guilty, and it doubles as sanitisation for a future
147    // one-off cleanup migration. The dashboard queries (Bug 2b in the
148    // finding) group by `commercial` verbatim, so a corrupt value creates a
149    // ghost commercial that never matches a `tbl_users.name` — hence the
150    // "blank name" and "missing commercial" symptoms.
151    public function setCommercialAttribute(?string $value): void
152    {
153        if ($value === null) {
154            $this->attributes['commercial'] = null;
155
156            return;
157        }
158
159        $normalised = str_replace(["\r\n", "\r"], "\n", $value);
160        $firstLine = strtok($normalised, "\n");
161        $trimmed = $firstLine === false ? '' : trim($firstLine);
162
163        $this->attributes['commercial'] = $trimmed === '' ? null : $trimmed;
164    }
165
166    /**
167     * Check if there is a record with the `internal_quote_id` equal to `$g3w_id`.
168     *
169     * @param  $g3w_id  Int ID of the budget in G3W
170     * @return bool
171     */
172    public static function quotationExist($g3w_id)
173    {
174        return self::where('internal_quote_id', $g3w_id)->exists();
175    }
176}