- 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>
125 lines
3.8 KiB
PHP
125 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Imports;
|
|
|
|
use App\Models\InvoiceLine;
|
|
use App\Models\Invoice;
|
|
use App\Models\Rfc;
|
|
use App\Models\InvoiceType;
|
|
use App\Models\PaymentType;
|
|
use App\Models\PaymentMethod;
|
|
use App\Models\Usage;
|
|
use App\Models\Currency;
|
|
use App\Models\CancellationType;
|
|
use App\Models\Taxpayer;
|
|
use App\Models\TaxRegime;
|
|
use App\Models\Status;
|
|
use Carbon\Carbon;
|
|
use Maatwebsite\Excel\Concerns\ToModel;
|
|
use Maatwebsite\Excel\Concerns\WithChunkReading;
|
|
use Maatwebsite\Excel\Concerns\WithStartRow;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
|
|
class LineItemsImport implements ToModel, WithChunkReading, WithHeadingRow, WithStartRow
|
|
{
|
|
/**
|
|
* @param array $row
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
*/
|
|
|
|
protected $rfc;
|
|
|
|
public function __construct($rfc)
|
|
{
|
|
$this->rfc = $rfc;
|
|
}
|
|
|
|
/**
|
|
* Método para indicar desde qué fila comenzar a leer.
|
|
*/
|
|
public function startRow(): int
|
|
{
|
|
return 2; // Si tienes encabezados, comienza en la fila 2
|
|
}
|
|
|
|
public function model(array $row)
|
|
{
|
|
|
|
$rfc = Rfc::where('rfc', $this->rfc)->firstOrCreate([
|
|
'rfc' => $this->rfc
|
|
]);
|
|
|
|
$invoice = Invoice::where('id', strip_tags($row['invoice_uuid']))->firstOrCreate([
|
|
'id' => $row['invoice_uuid']
|
|
]);
|
|
|
|
$type = InvoiceType::where('id', strip_tags($row['invoice_type']))->firstOrCreate([
|
|
'id' => $row['invoice_type']
|
|
]);
|
|
|
|
if ($this->emptyToNull($row['paymentType'])) {
|
|
$paymentType = PaymentType::where('id', strip_tags($row['paymentType']))->firstOrCreate([
|
|
'id' => $row['paymentType']
|
|
]);
|
|
}
|
|
|
|
if ($this->emptyToNull($row['invoice_status'])) {
|
|
$status = Status::where('description', strip_tags($row['status']))->firstOrCreatet([
|
|
'description' => $row['status']
|
|
]);
|
|
}
|
|
|
|
$issuerRfc = Rfc::where('rfc', strip_tags($row['issuer_rfc']))->firstOrCreate([
|
|
'rfc' => $row['issuer_rfc']
|
|
]);
|
|
|
|
$receiverRfc = Rfc::where('rfc', strip_tags($row['receiver_rfc']))->firstOrCreate([
|
|
'rfc' => $row['receiver_rfc']
|
|
]);
|
|
|
|
return InvoiceLine::updateOrCreate(
|
|
['id' => $invoiceLine_data['id']],
|
|
[
|
|
'rfc_id' => $rfc->id,
|
|
'invoice_id' => $invoice->id,
|
|
'invoice_type_id' => $type->id,
|
|
'payment_type_id' => $paymentType->id,
|
|
'status_id' => $status->id,
|
|
'issuer_rfc_id' => $issuerRfc->id,
|
|
'issuer_name' => $this->emptyToNull($row['issuername']),
|
|
'receiver_rfc_id' => $receiverRfc->id,
|
|
'receiver_name' => $this->emptyToNull($row['receivername']),
|
|
'issued_at' => Carbon::createFromFormat('Y-m-d H:i:s', $row['issuedAt']);
|
|
'identification_number' => $row['identificationNumber'];
|
|
'product_identification' => $row['productIdentification'];
|
|
'description' => $row['description'];
|
|
'unit_amount' => $row['unitAmount'];
|
|
'unit_code' => $row['unitCode'];
|
|
'quantity' => $row['quantity'];
|
|
'discount_amount' => $row['discountAmount'];
|
|
'total_amount' => $row['totalAmount'];
|
|
'retained_vat' => $row['rrowvalueAddedTax'];
|
|
'retained_income_tax' => $row['retainedTaxes']['incomeTax'];
|
|
'retained_sin_tax' => $row['retainedTaxes']['sinTax'];
|
|
'transferred_vat' => $row['transferredTaxes']['valueAddedTax'];
|
|
'transferred_sin_tax' => $row['transferredTaxes']['sinTax'];
|
|
]
|
|
);
|
|
|
|
}
|
|
|
|
private function emptyToNull($value)
|
|
{
|
|
return $value === '' ? null : $value;
|
|
}
|
|
|
|
/**
|
|
* Devuelve el tamaño del chunk (en filas) a leer.
|
|
*/
|
|
public function chunkSize(): int
|
|
{
|
|
return 5000; // Ajusta el tamaño según tus necesidades
|
|
}
|
|
}
|