- 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>
461 lines
19 KiB
PHP
461 lines
19 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\InvoicePayment;
|
|
use App\Models\User;
|
|
use App\Models\Rfc;
|
|
use App\Models\Invoice;
|
|
use App\Models\InvoiceRequest;
|
|
use App\Models\InvoiceType;
|
|
use App\Models\PaymentMethod;
|
|
use App\Models\Currency;
|
|
use App\Models\Usage;
|
|
use App\Models\CancellationType;
|
|
use App\Models\Taxpayer;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Http\Client\RequestException;
|
|
|
|
class InvoicePaymentController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function obtenerComplementosPago(Request $request)
|
|
{
|
|
$start = microtime(true);
|
|
set_time_limit(900);
|
|
|
|
$user = $request->user();
|
|
//$user = User::where('email', strip_tags($request->email))->first();
|
|
$rfc = Rfc::where('rfc', strip_tags($request->rfc))->first();
|
|
$now = Carbon::now();
|
|
$invoice_request = InvoiceRequest::where('rfc_id', $rfc->id)->where('type', 'payment')->first();
|
|
if ($invoice_request) {
|
|
$last_request = new Carbon($invoice_request->requested_at);
|
|
$min_days = ($last_request->diff($now)->days > 1) ? true : null;
|
|
} else {
|
|
$first_time = true;
|
|
$min_days = true;
|
|
$invoice_request = new InvoiceRequest();
|
|
}
|
|
|
|
if (in_array($rfc->id, $user->rfcs->pluck('id')->toArray()) && $min_days == true) {
|
|
$dataCounter = 1;
|
|
$arrayCount = 999;
|
|
$header_id = null;
|
|
do {
|
|
try {
|
|
if (isset($header_id) && $dataCounter >= $arrayCount) {
|
|
$dataCounter = 1;
|
|
// Realizar una petición GET con encabezados
|
|
$response = Http::withHeaders([
|
|
'accept' => 'application/ld+json',
|
|
'X-API-Key' => 'b16ec9be960cdf3a8302e92a7aec84d2'
|
|
])->get('https://api.syntage.com/taxpayers/'. $request->rfc .'/invoices/payments?id[lt]='. $header_id .'&itemsPerPage=1000');
|
|
} else {
|
|
// Realizar una petición GET con encabezados
|
|
$response = Http::withHeaders([
|
|
'accept' => 'application/ld+json',
|
|
'X-API-Key' => 'b16ec9be960cdf3a8302e92a7aec84d2'
|
|
])->get('https://api.syntage.com/taxpayers/'. $request->rfc .'/invoices/payments?itemsPerPage=1000');
|
|
}
|
|
|
|
// Verificar si la petición fue exitosa
|
|
if ($response->successful()) {
|
|
// Manejar la respuesta exitosa
|
|
$data = $response->json();
|
|
$invoicePayments = $data['hydra:member'];
|
|
$arrayCount = count($invoicePayments);
|
|
|
|
foreach ($invoicePayments as $invoicePayment_data) {
|
|
// code...
|
|
$invoicePayment = $this->setInvoicePayment($invoicePayment_data, $rfc);
|
|
$dataCounter++;
|
|
if ($dataCounter == 999) {
|
|
$header_id = $invoicePayment_data['id'];
|
|
}
|
|
}
|
|
|
|
$time = microtime(true) - $start;
|
|
} else {
|
|
// Manejar la respuesta fallida
|
|
return response()->json(['error' => 'Error al obtener los datos'], $response->status());
|
|
}
|
|
} catch (RequestException $e) {
|
|
// Manejar excepciones específicas de solicitudes HTTP
|
|
return response()->json(['error' => 'Excepción al obtener los datos: ' . $e->getMessage()], 500);
|
|
}
|
|
|
|
|
|
} while ($arrayCount >= 999);
|
|
|
|
$invoice_request->rfc_id = $rfc->id;
|
|
$invoice_request->type = 'payment';
|
|
$invoice_request->requested_at = Carbon::now();
|
|
$invoice_request->save();
|
|
|
|
return response()->json(['success' => 'Datos almacenados con exito', 'time' => $time]);
|
|
} else {
|
|
$time = microtime(true) - $start;
|
|
return response()->json(['success' => 'Datos almacenados con exito', 'time' => $time]);
|
|
}
|
|
}
|
|
|
|
public function ingresoPpd(Request $request) {
|
|
$start = microtime(true);
|
|
set_time_limit(1000);
|
|
|
|
$user = $request->user();
|
|
//$user = User::where('email', strip_tags($request->email))->first();
|
|
$rfc = Rfc::where('rfc', strip_tags($request->rfc))->first();
|
|
$dateCST = Carbon::createFromFormat('Y-m-d H:i:s', $request->date, 'CST');
|
|
|
|
// Definir inicio y fin en CST (Central Standard Time)
|
|
$startDateCST = $dateCST->copy()->firstOfMonth();
|
|
$endDateCST = $dateCST->copy()->endOfMonth();
|
|
// Convertir las fechas a UTC
|
|
$startDateUTC = $startDateCST->copy()->setTimezone('UTC');
|
|
$endDateUTC = $endDateCST->copy()->setTimezone('UTC');
|
|
|
|
$taxpayer = Taxpayer::where('rfc_id', $rfc->id)->first();
|
|
|
|
$naturalPerson = ['612', '621', '626'];
|
|
|
|
if (in_array($rfc->id, $user->rfcs->pluck('id')->toArray())) {
|
|
if(count(array_intersect($naturalPerson, $taxpayer->taxRegimes->pluck('id')->toArray())) > 0) {
|
|
if ($request->conciliated) {
|
|
$invoicePayments = InvoicePayment::where('rfc_id', $rfc->id)
|
|
->whereNull('cancelled_at')
|
|
->whereHas('invoice', function($query) use ($rfc, $startDateUTC, $endDateUTC) {
|
|
$query->where('issuer_rfc_id', $rfc->id)
|
|
//Para Gasto PPD es cuando es receptor
|
|
//->where('receiver_rfc_id', $rfc->id);
|
|
->whereIn('cancellation_status_id', ['1', '6', '7'])
|
|
->whereHas('conciliations', function($query) use ($rfc, $startDateUTC, $endDateUTC) {
|
|
$query->where('rfc_id', $rfc->id);
|
|
$query->where('is_conciliated', true);
|
|
$query->whereBetween('conciliated_at', [$startDateUTC, $endDateUTC]);
|
|
});
|
|
})
|
|
->get();
|
|
} else {
|
|
$invoicePayments = InvoicePayment::where('rfc_id', $rfc->id)
|
|
->whereNull('cancelled_at')
|
|
->whereBetween('date', [$startDateUTC, $endDateUTC])
|
|
->whereHas('invoice', function($query) use ($rfc) {
|
|
$query->where('issuer_rfc_id', $rfc->id)
|
|
//Para Gasto PPD es cuando es receptor
|
|
//->where('receiver_rfc_id', $rfc->id);
|
|
->whereIn('cancellation_status_id', ['1', '6', '7']);
|
|
})
|
|
->get();
|
|
}
|
|
} else {
|
|
return response()->json([
|
|
'total' => 0,
|
|
'amount' => 0,
|
|
'transferred_vat' => 0,
|
|
'transferred_vat_ppd' => 0,
|
|
'retained_vat' => 0,
|
|
'retained_vat_ppd' => 0,
|
|
'no_natural' => 'No es persona fisica'
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'total' => round($invoicePayments->sum('total_in_mxn'), 2),
|
|
'transferred_vat' => round($invoicePayments->sum('transferred_vat_in_mxn'), 2),
|
|
'retained_vat' => round($invoicePayments->sum('retained_vat_in_mxn'), 2),
|
|
'amount' => round($invoicePayments->sum('amount_in_mxn'), 2),
|
|
'transferred_vat_ppd' => round($invoicePayments->sum('transferred_vat_ppd'), 2),
|
|
'retained_vat_ppd' => round($invoicePayments->sum('retained_vat_ppd'), 2),
|
|
'invoicesPpd' => $invoicePayments
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function gastoPpd(Request $request) {
|
|
$start = microtime(true);
|
|
set_time_limit(1000);
|
|
|
|
$user = $request->user();
|
|
//$user = User::where('email', strip_tags($request->email))->first();
|
|
$rfc = Rfc::where('rfc', strip_tags($request->rfc))->first();
|
|
$dateCST = Carbon::createFromFormat('Y-m-d H:i:s', $request->date, 'CST');
|
|
|
|
// Definir inicio y fin en CST (Central Standard Time)
|
|
$startDateCST = $dateCST->copy()->firstOfMonth();
|
|
$endDateCST = $dateCST->copy()->endOfMonth();
|
|
// Convertir las fechas a UTC
|
|
$startDateUTC = $startDateCST->copy()->setTimezone('UTC');
|
|
$endDateUTC = $endDateCST->copy()->setTimezone('UTC');
|
|
|
|
if (in_array($rfc->id, $user->rfcs->pluck('id')->toArray())) {
|
|
if ($request->conciliated) {
|
|
$invoicePayments = InvoicePayment::where('rfc_id', $rfc->id)
|
|
->whereHas('invoice', function($query) use ($rfc, $startDateUTC, $endDateUTC) {
|
|
$query->where('receiver_rfc_id', $rfc->id)
|
|
->whereIn('cancellation_status_id', ['1', '6', '7'])
|
|
->whereHas('conciliations', function($query) use ($rfc, $startDateUTC, $endDateUTC) {
|
|
$query->where('rfc_id', $rfc->id);
|
|
$query->where('is_conciliated', true);
|
|
$query->whereBetween('conciliated_at', [$startDateUTC, $endDateUTC]);
|
|
});
|
|
})
|
|
->get();
|
|
} else {
|
|
$invoicePayments = InvoicePayment::where('rfc_id', $rfc->id)
|
|
->whereBetween('date', [$startDateUTC, $endDateUTC])
|
|
->whereHas('invoice', function($query) use ($rfc) {
|
|
$query->where('receiver_rfc_id', $rfc->id)
|
|
->whereIn('cancellation_status_id', ['1', '6', '7']);
|
|
})
|
|
->get();
|
|
}
|
|
|
|
return response()->json([
|
|
'total' => round($invoicePayments->sum('total_in_mxn'), 2),
|
|
'transferred_vat' => round($invoicePayments->sum('transferred_vat_in_mxn'), 2),
|
|
'retained_vat' => round($invoicePayments->sum('retained_vat_in_mxn'), 2),
|
|
'amount' => round($invoicePayments->sum('amount_in_mxn'), 2),
|
|
'transferred_vat_ppd' => round($invoicePayments->sum('transferred_vat_ppd'), 2),
|
|
'retained_vat_ppd' => round($invoicePayments->sum('retained_vat_ppd'), 2),
|
|
'retained_income_tax' => round($invoicePayments->sum('retained_income_tax_in_mxn'), 2),
|
|
'invoicesPpd' => $invoicePayments,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function adquisicionPpd(Request $request) {
|
|
$start = microtime(true);
|
|
set_time_limit(1000);
|
|
|
|
$user = $request->user();
|
|
//$user = User::where('email', strip_tags($request->email))->first();
|
|
$rfc = Rfc::where('rfc', strip_tags($request->rfc))->first();
|
|
$dateCST = Carbon::createFromFormat('Y-m-d H:i:s', $request->date, 'CST');
|
|
|
|
// Definir inicio y fin en CST (Central Standard Time)
|
|
$startDateCST = $dateCST->copy()->firstOfMonth();
|
|
$endDateCST = $dateCST->copy()->endOfMonth();
|
|
// Convertir las fechas a UTC
|
|
$startDateUTC = $startDateCST->copy()->setTimezone('UTC');
|
|
$endDateUTC = $endDateCST->copy()->setTimezone('UTC');
|
|
|
|
if (in_array($rfc->id, $user->rfcs->pluck('id')->toArray())) {
|
|
if ($request->conciliated) {
|
|
$invoicePayments = InvoicePayment::where('rfc_id', $rfc->id)
|
|
->whereHas('invoice', function($query) use ($rfc, $startDateUTC, $endDateUTC) {
|
|
$query->where('receiver_rfc_id', $rfc->id)
|
|
->whereIn('cancellation_status_id', ['1', '6', '7'])
|
|
->where('usage_id', 'G01')
|
|
->whereHas('conciliations', function($query) use ($rfc, $startDateUTC, $endDateUTC) {
|
|
$query->where('rfc_id', $rfc->id);
|
|
$query->where('is_conciliated', true);
|
|
$query->whereBetween('conciliated_at', [$startDateUTC, $endDateUTC]);
|
|
});
|
|
})
|
|
->get();
|
|
} else {
|
|
$invoicePayments = InvoicePayment::where('rfc_id', $rfc->id)
|
|
->whereBetween('date', [$startDateUTC, $endDateUTC])
|
|
->whereHas('invoice', function($query) use ($rfc) {
|
|
$query->where('receiver_rfc_id', $rfc->id)
|
|
->whereIn('cancellation_status_id', ['1', '6', '7'])
|
|
->where('usage_id', 'G01');
|
|
})
|
|
->get();
|
|
}
|
|
|
|
return response()->json([
|
|
'total' => round($invoicePayments->sum('total_in_mxn'), 2),
|
|
'tax' => round($invoicePayments->sum('tax_in_mxn'), 2),
|
|
'transferred_vat' => round($invoicePayments->sum('transferred_vat_in_mxn'), 2),
|
|
'retained_vat' => round($invoicePayments->sum('retained_vat_in_mxn'), 2),
|
|
'amount' => round($invoicePayments->sum('amount_in_mxn'), 2),
|
|
'vat_ppd' => round($invoicePayments->sum('vat_ppd'), 2),
|
|
'invoicesPpd' => $invoicePayments
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function ivaIngresoPpd(Request $request) {
|
|
$start = microtime(true);
|
|
set_time_limit(1000);
|
|
|
|
$user = $request->user();
|
|
//$user = User::where('email', strip_tags($request->email))->first();
|
|
$rfc = Rfc::where('rfc', strip_tags($request->rfc))->first();
|
|
$dateCST = Carbon::createFromFormat('Y-m-d H:i:s', $request->date, 'CST');
|
|
|
|
// Definir inicio y fin en CST (Central Standard Time)
|
|
$startDateCST = $dateCST->copy()->firstOfMonth();
|
|
$endDateCST = $dateCST->copy()->endOfMonth();
|
|
// Convertir las fechas a UTC
|
|
$startDateUTC = $startDateCST->copy()->setTimezone('UTC');
|
|
$endDateUTC = $endDateCST->copy()->setTimezone('UTC');
|
|
|
|
$taxpayer = Taxpayer::where('rfc_id', $rfc->id)->first();
|
|
|
|
$naturalPerson = ['612', '621', '626'];
|
|
|
|
if (in_array($rfc->id, $user->rfcs->pluck('id')->toArray())) {
|
|
if ($request->conciliated) {
|
|
$invoicePayments = InvoicePayment::where('rfc_id', $rfc->id)
|
|
->whereNull('cancelled_at')
|
|
->whereHas('invoice', function($query) use ($rfc, $startDateUTC, $endDateUTC) {
|
|
$query->where('issuer_rfc_id', $rfc->id)
|
|
//Para Gasto PPD es cuando es receptor
|
|
//->where('receiver_rfc_id', $rfc->id);
|
|
->whereIn('cancellation_status_id', ['1', '6', '7'])
|
|
->whereHas('conciliations', function($query) use ($rfc, $startDateUTC, $endDateUTC) {
|
|
$query->where('rfc_id', $rfc->id);
|
|
$query->where('is_conciliated', true);
|
|
$query->whereBetween('conciliated_at', [$startDateUTC, $endDateUTC]);
|
|
});
|
|
})
|
|
->get();
|
|
} else {
|
|
$invoicePayments = InvoicePayment::where('rfc_id', $rfc->id)
|
|
->whereNull('cancelled_at')
|
|
->whereBetween('date', [$startDateUTC, $endDateUTC])
|
|
->whereHas('invoice', function($query) use($rfc) {
|
|
$query->where('issuer_rfc_id', $rfc->id)
|
|
//Para Gasto PPD es cuando es receptor
|
|
//->where('receiver_rfc_id', $rfc->id);
|
|
->whereIn('cancellation_status_id', ['1', '6', '7']);
|
|
})
|
|
->get();
|
|
}
|
|
|
|
return response()->json([
|
|
'total' => round($invoicePayments->sum('total_in_mxn'), 2),
|
|
'transferred_vat' => round($invoicePayments->sum('transferred_vat_in_mxn'), 2),
|
|
'retained_vat' => round($invoicePayments->sum('retained_vat_in_mxn'), 2),
|
|
'amount' => round($invoicePayments->sum('amount_in_mxn'), 2),
|
|
'transferred_vat_ppd' => round($invoicePayments->sum('transferred_vat_ppd'), 2),
|
|
'retained_vat_ppd' => round($invoicePayments->sum('retained_vat_ppd'), 2)
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param \App\Models\InvoicePayment $invoicePayment
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(InvoicePayment $invoicePayment)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param \App\Models\InvoicePayment $invoicePayment
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function edit(InvoicePayment $invoicePayment)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \App\Models\InvoicePayment $invoicePayment
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function update(Request $request, InvoicePayment $invoicePayment)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param \App\Models\InvoicePayment $invoicePayment
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function destroy(InvoicePayment $invoicePayment)
|
|
{
|
|
//
|
|
}
|
|
|
|
public function setInvoicePayment($invoicePayment_data, $rfc)
|
|
{
|
|
//
|
|
$invoicePayment = InvoicePayment::firstOrNew(['id' => $invoicePayment_data['id']]);
|
|
|
|
$invoicePayment->rfc_id = $rfc->id;
|
|
$invoicePayment->date = $invoicePayment_data['date'];
|
|
$invoice = Invoice::where('id', $invoicePayment_data['invoiceUuid'])->first();
|
|
if($invoice) {
|
|
$invoicePayment->invoice_id = $invoicePayment_data['invoiceUuid'];
|
|
} else {
|
|
$invoicePayment->invoice_id = null;
|
|
}
|
|
if ($invoicePayment_data['paymentMethod']) {
|
|
$payment_method = PaymentMethod::where('id', strip_tags($invoicePayment_data['paymentMethod']))->first();
|
|
if (!$payment_method) {
|
|
$payment_method = new PaymentMethod();
|
|
$payment_method->id = $invoicePayment_data['paymentMethod'];
|
|
$payment_method->save();
|
|
}
|
|
}
|
|
$invoicePayment->payment_method_id = $invoicePayment_data['paymentMethod'];
|
|
if ($invoicePayment_data['currency']) {
|
|
$currency = Currency::where('id', strip_tags($invoicePayment_data['currency']))->first();
|
|
if (!$currency) {
|
|
$currency = new Currency();
|
|
$currency->id = $invoicePayment_data['currency'];
|
|
$currency->save();
|
|
}
|
|
}
|
|
$invoicePayment->currency_id = $invoicePayment_data['currency'];
|
|
$invoicePayment->exchange_rate = $invoicePayment_data['exchangeRate'];
|
|
$invoicePayment->installment = $invoicePayment_data['installment'];
|
|
$invoicePayment->previous_balance = $invoicePayment_data['previousBalance'];
|
|
$invoicePayment->amount = $invoicePayment_data['amount'];
|
|
$invoicePayment->outstanding_balance = $invoicePayment_data['outstandingBalance'];
|
|
if ($invoicePayment_data['canceledAt']){
|
|
$invoicePayment->cancelled_at = Carbon::createFromFormat('Y-m-d H:i:s', $invoicePayment_data['canceledAt']);
|
|
}
|
|
$invoicePayment->save();
|
|
return $invoicePayment;
|
|
}
|
|
}
|