Initial commit: Horux Backend API
- API REST para gestion de facturas electronicas mexicanas (CFDI) - Laravel 9 con autenticacion OAuth 2.0 (Passport) - Integracion con Syntage, Clerk y Facturama - 30 modelos Eloquent, 39 controladores - Documentacion completa en /docs Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
162
app/Models/InvoicePayment.php
Normal file
162
app/Models/InvoicePayment.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class InvoicePayment extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'cancelled_at'
|
||||
];
|
||||
|
||||
public function invoice(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Invoice::class);
|
||||
}
|
||||
|
||||
public function rfc(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Rfc::class);
|
||||
}
|
||||
|
||||
public function paymentMethod(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(PaymentMethod::class);
|
||||
}
|
||||
|
||||
public function currency(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Currency::class);
|
||||
}
|
||||
|
||||
public function getIssuerRfcAttribute()
|
||||
{
|
||||
return $this->invoice()->first()->issuer_rfc;
|
||||
}
|
||||
|
||||
public function getReceiverRfcAttribute()
|
||||
{
|
||||
return $this->invoice()->first()->receiver_rfc;
|
||||
}
|
||||
|
||||
public function getTotalInMxnAttribute()
|
||||
{
|
||||
if ($this->invoice()->first()->exchange_rate) {
|
||||
$totalMXN = $this->invoice()->first()->total * $this->invoice()->first()->exchange_rate;
|
||||
} else {
|
||||
$totalMXN = $this->invoice()->first()->total;
|
||||
}
|
||||
return $totalMXN;
|
||||
}
|
||||
|
||||
public function getAmountInMxnAttribute()
|
||||
{
|
||||
if ($this->exchange_rate) {
|
||||
$amountMXN = abs($this->amount * $this->exchange_rate);
|
||||
} else {
|
||||
$amountMXN = abs($this->amount);
|
||||
}
|
||||
return $amountMXN;
|
||||
}
|
||||
|
||||
public function getTransferredVatInMxnAttribute()
|
||||
{
|
||||
if ($this->invoice()->first()->exchange_rate) {
|
||||
$transferredVatMXN = $this->invoice()->first()->transferred_vat * $this->invoice()->first()->exchange_rate;
|
||||
} else {
|
||||
$transferredVatMXN = $this->invoice()->first()->transferred_vat;
|
||||
}
|
||||
return $transferredVatMXN;
|
||||
}
|
||||
|
||||
public function getRetainedVatInMxnAttribute()
|
||||
{
|
||||
if ($this->invoice()->first()->exchange_rate) {
|
||||
$retainedVatMXN = $this->invoice()->first()->retained_vat * $this->invoice()->first()->exchange_rate;
|
||||
} else {
|
||||
$retainedVatMXN = $this->invoice()->first()->retained_vat;
|
||||
}
|
||||
return $retainedVatMXN;
|
||||
}
|
||||
|
||||
public function getRetainedIncomeTaxInMxnAttribute()
|
||||
{
|
||||
if ($this->invoice()->first()->exchange_rate) {
|
||||
$retainedIncomeTaxMXN = $this->invoice()->first()->retained_income_tax * $this->invoice()->first()->exchange_rate;
|
||||
} else {
|
||||
$retainedIncomeTaxMXN = $this->invoice()->first()->retained_income_tax;
|
||||
}
|
||||
return $retainedIncomeTaxMXN;
|
||||
}
|
||||
|
||||
public function getTransferredVatPercentageAttribute()
|
||||
{
|
||||
$totalMXN = $this->getTotalInMxnAttribute();
|
||||
$transferredVatMXN = $this->getTransferredVatInMxnAttribute();
|
||||
$retainedVatMXN = $this->getRetainedVatInMxnAttribute();
|
||||
|
||||
$transferredVatPercentage = round(100*(($transferredVatMXN)/($totalMXN - $transferredVatMXN)));
|
||||
|
||||
return $transferredVatPercentage;
|
||||
}
|
||||
|
||||
public function getTransferredVatPpdAttribute()
|
||||
{
|
||||
$transferredVatPercentage = $this->getTransferredVatPercentageAttribute();
|
||||
|
||||
if ($this->exchange_rate) {
|
||||
$transferredVatPPD = round((abs($this->amount*$this->exchange_rate)/(1+($transferredVatPercentage*0.01)))*($transferredVatPercentage*0.01), 2);
|
||||
} else {
|
||||
$transferredVatPPD = round((abs($this->amount)/(1+($transferredVatPercentage*0.01)))*($transferredVatPercentage*0.01), 2);
|
||||
}
|
||||
|
||||
return $transferredVatPPD;
|
||||
}
|
||||
|
||||
public function getRetainedVatPercentageAttribute()
|
||||
{
|
||||
$totalMXN = $this->getTotalInMxnAttribute();
|
||||
$transferredVatMXN = $this->getTransferredVatInMxnAttribute();
|
||||
$retainedVatMXN = $this->getRetainedVatInMxnAttribute();
|
||||
|
||||
$retainedVatPercentage = round(100*(($retainedVatMXN)/($totalMXN - $transferredVatMXN)));
|
||||
|
||||
return $retainedVatPercentage;
|
||||
}
|
||||
|
||||
public function getRetainedVatPpdAttribute()
|
||||
{
|
||||
$retainedVatPercentage = $this->getRetainedVatPercentageAttribute();
|
||||
$transferredVatPercentage = $this->getTransferredVatPercentageAttribute();
|
||||
|
||||
if ($this->exchange_rate) {
|
||||
$retainedVatPPD = round((abs($this->amount*$this->exchange_rate)/(1+($transferredVatPercentage*0.01)))*($retainedVatPercentage*0.01), 2);
|
||||
} else {
|
||||
$retainedVatPPD = round((abs($this->amount)/(1+($transferredVatPercentage*0.01)))*($retainedVatPercentage*0.01), 2);
|
||||
}
|
||||
|
||||
return $retainedVatPPD;
|
||||
}
|
||||
|
||||
public function getAbsAmountAttribute()
|
||||
{
|
||||
$absAmount = abs($this->amount);
|
||||
|
||||
return $absAmount;
|
||||
}
|
||||
|
||||
protected $appends = [
|
||||
'issuer_rfc', 'receiver_rfc', 'total_in_mxn',
|
||||
'transferred_vat_in_mxn', 'retained_vat_in_mxn',
|
||||
'retained_income_tax_in_mxn',
|
||||
'transferred_vat_percentage', 'transferred_vat_ppd',
|
||||
'retained_vat_percentage', 'retained_vat_ppd',
|
||||
'abs_amount', 'amount_in_mxn'
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user