- Created ExampleTest and RoutesTest for basic application response and route testing. - Implemented IsolatedDatabaseTestCase for isolated database testing. - Added unit tests for various models and services including Contract, CPE, and LeadDetail. - Developed request validation tests for CreateClientRequest, UpdateClientRequest, and ContractRequest. - Introduced Vite configuration for asset management in the Laravel application.
117 lines
2.9 KiB
PHP
117 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
class ContractHistory extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $table = 'contract_histories';
|
|
|
|
// Campos que podem ser preenchidos em massa
|
|
protected $fillable = [
|
|
'contract_id',
|
|
'previous_supplier_id',
|
|
'current_supplier_id',
|
|
'annual_consumption',
|
|
'seller_id',
|
|
'contract_type_id',
|
|
'typology_id',
|
|
'contract_date',
|
|
'activation_date',
|
|
'renewal_date',
|
|
'renewal_type',
|
|
'go_ki',
|
|
'validity',
|
|
'contract_value',
|
|
'commission_value',
|
|
'profit_value',
|
|
'chargeback',
|
|
'chargeback_value',
|
|
'chargeback_date',
|
|
'profit_margin',
|
|
'total_installments',
|
|
'paid_installments',
|
|
'notes',
|
|
'contract_snapshot'
|
|
];
|
|
|
|
|
|
public function contract()
|
|
{
|
|
return $this->belongsTo(Contract::class);
|
|
}
|
|
|
|
public function previousSupplier()
|
|
{
|
|
return $this->belongsTo(Supplier::class, 'previous_supplier_id');
|
|
}
|
|
|
|
public function currentSupplier()
|
|
{
|
|
return $this->belongsTo(Supplier::class, 'current_supplier_id');
|
|
}
|
|
|
|
public function seller()
|
|
{
|
|
return $this->belongsTo(User::class, 'seller_id');
|
|
}
|
|
|
|
public function contractType()
|
|
{
|
|
return $this->belongsTo(ContractType::class);
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function validity(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
// Getter: returns a Carbon instance or null
|
|
get: fn ($value) => $value ? Carbon::parse($value) : null,
|
|
|
|
// Setter: accepts 'd/m/Y' from form, converts to 'Y-m-d' for DB
|
|
set: fn ($value) => $this->convertDateToDbFormat($value)
|
|
);
|
|
}
|
|
|
|
protected function chargebackDate(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn ($value) => $value ? Carbon::parse($value) : null,
|
|
set: fn ($value) => $this->convertDateToDbFormat($value)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Converts a date string from 'd/m/Y' or other formats to 'Y-m-d' for DB storage.
|
|
*
|
|
* @param string|null $value
|
|
* @return string|null
|
|
*/
|
|
private function convertDateToDbFormat($value)
|
|
{
|
|
$value = trim($value);
|
|
if (!$value) return null;
|
|
|
|
try {
|
|
// Try parsing d/m/Y format
|
|
return Carbon::createFromFormat('d/m/Y', $value)->format('Y-m-d');
|
|
} catch (\Exception $e) {
|
|
try {
|
|
// Fallback: parse standard date formats (e.g., Y-m-d)
|
|
return Carbon::parse($value)->format('Y-m-d');
|
|
} catch (\Exception $e) {
|
|
// If parsing fails, store null
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|