|
@@ -86,6 +86,8 @@ class RecordOUT extends Component
|
|
|
public $selectedCausal = '';
|
|
public $selectedCausal = '';
|
|
|
public $importCausals = array();
|
|
public $importCausals = array();
|
|
|
|
|
|
|
|
|
|
+ public $vats = array();
|
|
|
|
|
+
|
|
|
|
|
|
|
|
protected $rules = [
|
|
protected $rules = [
|
|
|
'supplier_id' => 'required',
|
|
'supplier_id' => 'required',
|
|
@@ -135,7 +137,7 @@ class RecordOUT extends Component
|
|
|
'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')),
|
|
'when' => array(array('month' => date("n"), 'year' => date("Y"), 'period' => '')),
|
|
|
'amount' => null,
|
|
'amount' => null,
|
|
|
'imponibile' => null,
|
|
'imponibile' => null,
|
|
|
- 'aliquota_iva' => null,
|
|
|
|
|
|
|
+ 'aliquota_iva' => '',
|
|
|
'note' => '',
|
|
'note' => '',
|
|
|
'commercial' => 0
|
|
'commercial' => 0
|
|
|
);
|
|
);
|
|
@@ -173,6 +175,9 @@ class RecordOUT extends Component
|
|
|
if (empty($this->importCausals)) {
|
|
if (empty($this->importCausals)) {
|
|
|
$this->loadImportCausals();
|
|
$this->loadImportCausals();
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ $this->vats = \App\Models\Vat::select('id', 'name', 'value')->orderBy('value')->get();
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function mount()
|
|
public function mount()
|
|
@@ -199,6 +204,7 @@ class RecordOUT extends Component
|
|
|
|
|
|
|
|
$this->suppliers = \App\Models\Supplier::select('name', 'id')->orderBy('name')->get();
|
|
$this->suppliers = \App\Models\Supplier::select('name', 'id')->orderBy('name')->get();
|
|
|
$this->payments = \App\Models\PaymentMethod::select('id', 'name')->whereIn('type', array('ALL', 'OUT'))->where('enabled', true)->orderBy('name')->get();
|
|
$this->payments = \App\Models\PaymentMethod::select('id', 'name')->whereIn('type', array('ALL', 'OUT'))->where('enabled', true)->orderBy('name')->get();
|
|
|
|
|
+ $this->vats = \App\Models\Vat::select('id', 'name', 'value')->orderBy('value')->get();
|
|
|
|
|
|
|
|
$this->importCausals = [];
|
|
$this->importCausals = [];
|
|
|
$this->loadImportCausals();
|
|
$this->loadImportCausals();
|
|
@@ -483,16 +489,40 @@ class RecordOUT extends Component
|
|
|
'amount' => formatPrice($recordRow->amount)
|
|
'amount' => formatPrice($recordRow->amount)
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- // Add imponibile field if available in the database
|
|
|
|
|
if (isset($recordRow->imponibile)) {
|
|
if (isset($recordRow->imponibile)) {
|
|
|
$rowData['imponibile'] = formatPrice($recordRow->imponibile);
|
|
$rowData['imponibile'] = formatPrice($recordRow->imponibile);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Add aliquota_iva field if available in the database
|
|
|
|
|
|
|
+ $vatLookup = [];
|
|
|
|
|
+ foreach ($this->vats as $vat) {
|
|
|
|
|
+ $vatLookup[(string)$vat->value] = $vat->value;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if (isset($recordRow->aliquota_iva)) {
|
|
if (isset($recordRow->aliquota_iva)) {
|
|
|
- $rowData['aliquota_iva'] = $recordRow->aliquota_iva . '%';
|
|
|
|
|
|
|
+ $dbVatValue = (string)$recordRow->aliquota_iva;
|
|
|
|
|
+ if (isset($vatLookup[$dbVatValue])) {
|
|
|
|
|
+ $rowData['aliquota_iva'] = (string)$vatLookup[$dbVatValue];
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $dbVatValueFloat = floatval($dbVatValue);
|
|
|
|
|
+ $closestMatch = null;
|
|
|
|
|
+ $minDiff = PHP_FLOAT_MAX;
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($vatLookup as $vatValue) {
|
|
|
|
|
+ $diff = abs($dbVatValueFloat - floatval($vatValue));
|
|
|
|
|
+ if ($diff < $minDiff && $diff < 0.1) {
|
|
|
|
|
+ $minDiff = $diff;
|
|
|
|
|
+ $closestMatch = $vatValue;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($closestMatch !== null) {
|
|
|
|
|
+ $rowData['aliquota_iva'] = (string)$closestMatch;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $rowData['aliquota_iva'] = $dbVatValue;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
$this->rows[] = $rowData;
|
|
$this->rows[] = $rowData;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -1173,6 +1203,12 @@ class RecordOUT extends Component
|
|
|
'period' => $period
|
|
'period' => $period
|
|
|
]];
|
|
]];
|
|
|
|
|
|
|
|
|
|
+ $vatId = null;
|
|
|
|
|
+ if (isset($linea['aliquotaIva']) && $linea['aliquotaIva'] > 0) {
|
|
|
|
|
+ $vatId = $this->findOrCreateVat($linea['aliquotaIva']);
|
|
|
|
|
+ Log::info("Using VAT ID: $vatId for rate: {$linea['aliquotaIva']}%");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$recordRow = new \App\Models\RecordRow();
|
|
$recordRow = new \App\Models\RecordRow();
|
|
|
$recordRow->record_id = $recordId;
|
|
$recordRow->record_id = $recordId;
|
|
|
$recordRow->causal_id = $this->selectedCausal;
|
|
$recordRow->causal_id = $this->selectedCausal;
|
|
@@ -1224,6 +1260,12 @@ class RecordOUT extends Component
|
|
|
'year' => $year,
|
|
'year' => $year,
|
|
|
'period' => $period
|
|
'period' => $period
|
|
|
]];
|
|
]];
|
|
|
|
|
+
|
|
|
|
|
+ $vatId = null;
|
|
|
|
|
+ if (isset($riepilogo['aliquotaIva']) && $riepilogo['aliquotaIva'] > 0) {
|
|
|
|
|
+ $vatId = $this->findOrCreateVat($riepilogo['aliquotaIva']);
|
|
|
|
|
+ Log::info("Using VAT ID: $vatId for rate: {$riepilogo['aliquotaIva']}%");
|
|
|
|
|
+ }
|
|
|
$total_amount = $riepilogo['imponibile'] + $riepilogo['imposta'];
|
|
$total_amount = $riepilogo['imponibile'] + $riepilogo['imposta'];
|
|
|
$recordRow = new \App\Models\RecordRow();
|
|
$recordRow = new \App\Models\RecordRow();
|
|
|
$recordRow->record_id = $recordId;
|
|
$recordRow->record_id = $recordId;
|
|
@@ -1417,4 +1459,58 @@ class RecordOUT extends Component
|
|
|
// Per altri errori, conserva il messaggio originale ma semplificalo
|
|
// Per altri errori, conserva il messaggio originale ma semplificalo
|
|
|
return $errorMessage;
|
|
return $errorMessage;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public function getVats()
|
|
|
|
|
+ {
|
|
|
|
|
+ $vats = array();
|
|
|
|
|
+ foreach($this->rows as $r)
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($r["amount"] != null && $r["amount"] != "" && $r["vat_id"] > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ $vat = getVatValue($this->currencyToDouble($r["amount"]), $r["vat_id"]);
|
|
|
|
|
+ $vatName = "";
|
|
|
|
|
+ foreach($this->vats as $v)
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($v->id == $r["vat_id"])
|
|
|
|
|
+ $vatName = $v->name;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (isset($vats[$vatName]))
|
|
|
|
|
+ $vats[$vatName] += $vat;
|
|
|
|
|
+ else
|
|
|
|
|
+ $vats[$vatName] = $vat;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return $vats;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private function findOrCreateVat($vatRate)
|
|
|
|
|
+ {
|
|
|
|
|
+ $vatRate = (float)$vatRate;
|
|
|
|
|
+ Log::info("Searching for VAT rate: $vatRate%");
|
|
|
|
|
+
|
|
|
|
|
+ $existingVat = \App\Models\Vat::where(function ($query) use ($vatRate) {
|
|
|
|
|
+ $query->whereRaw('ABS(value - ?) < 0.01', [$vatRate]);
|
|
|
|
|
+ })->first();
|
|
|
|
|
+
|
|
|
|
|
+ if ($existingVat) {
|
|
|
|
|
+ Log::info("Found existing VAT rate: ID={$existingVat->id}, Name={$existingVat->name}, Value={$existingVat->value}");
|
|
|
|
|
+ return $existingVat->id;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Log::info("Creating new VAT rate: $vatRate%");
|
|
|
|
|
+
|
|
|
|
|
+ $vatName = "IVA $vatRate%";
|
|
|
|
|
+
|
|
|
|
|
+ $newVat = new \App\Models\Vat();
|
|
|
|
|
+ $newVat->name = $vatName;
|
|
|
|
|
+ $newVat->value = $vatRate;
|
|
|
|
|
+ $newVat->enabled = 1;
|
|
|
|
|
+ $newVat->save();
|
|
|
|
|
+
|
|
|
|
|
+ Log::info("Created new VAT rate: ID={$newVat->id}, Name={$newVat->name}, Value={$newVat->value}");
|
|
|
|
|
+
|
|
|
|
|
+ $this->vats = \App\Models\Vat::select('id', 'name', 'value')->orderBy('value')->get();
|
|
|
|
|
+
|
|
|
|
|
+ return $newVat->id;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|