2024-08-17 17:24:08 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://Paymentninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
2024-08-26 12:17:51 +02:00
|
|
|
namespace App\Services\Quickbooks\Transformers;
|
2024-08-17 17:24:08 +02:00
|
|
|
|
2024-08-26 07:48:48 +02:00
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Factory\PaymentFactory;
|
2024-08-17 17:24:08 +02:00
|
|
|
|
|
|
|
/**
|
2024-08-22 08:45:06 +02:00
|
|
|
*
|
2024-08-17 17:24:08 +02:00
|
|
|
* Class PaymentTransformer.
|
|
|
|
*/
|
|
|
|
class PaymentTransformer extends BaseTransformer
|
|
|
|
{
|
|
|
|
|
2024-08-26 07:48:48 +02:00
|
|
|
public function qbToNinja(mixed $qb_data)
|
2024-08-17 17:24:08 +02:00
|
|
|
{
|
2024-08-26 07:48:48 +02:00
|
|
|
return $this->transform($qb_data);
|
2024-08-17 17:24:08 +02:00
|
|
|
}
|
|
|
|
|
2024-08-26 07:48:48 +02:00
|
|
|
public function ninjaToQb()
|
2024-08-22 08:45:06 +02:00
|
|
|
{
|
2024-08-17 17:24:08 +02:00
|
|
|
}
|
|
|
|
|
2024-08-26 07:48:48 +02:00
|
|
|
public function transform(mixed $qb_data)
|
2024-08-17 17:24:08 +02:00
|
|
|
{
|
|
|
|
|
2024-08-26 07:48:48 +02:00
|
|
|
return [
|
|
|
|
'date' => data_get($qb_data, 'TxnDate', now()->format('Y-m-d')),
|
|
|
|
'amount' => floatval(data_get($qb_data, 'TotalAmt', 0)),
|
|
|
|
'applied' => data_get($qb_data, 'TotalAmt', 0) - data_get($qb_data, 'UnappliedAmt', 0),
|
|
|
|
'number' => data_get($qb_data, 'DocNumber', null),
|
|
|
|
'private_notes' => data_get($qb_data, 'PrivateNote', null),
|
|
|
|
'currency_id' => (string) $this->resolveCurrency(data_get($qb_data, 'CurrencyRef.value')),
|
|
|
|
'client_id' => $this->getClientId(data_get($qb_data, 'CustomerRef.value', null)),
|
|
|
|
];
|
2024-08-17 17:24:08 +02:00
|
|
|
}
|
2024-08-26 07:48:48 +02:00
|
|
|
|
|
|
|
public function buildPayment($qb_data): ?Payment
|
2024-08-17 17:24:08 +02:00
|
|
|
{
|
2024-08-26 07:48:48 +02:00
|
|
|
$ninja_payment_data = $this->transform($qb_data);
|
|
|
|
|
|
|
|
if($ninja_payment_data['client_id'])
|
|
|
|
{
|
|
|
|
$payment = PaymentFactory::create($this->company->id, $this->company->owner()->id,$ninja_payment_data['client_id']);
|
|
|
|
$payment->amount = $ninja_payment_data['amount'];
|
|
|
|
$payment->applied = $ninja_payment_data['applied'];
|
|
|
|
$payment->status_id = 4;
|
|
|
|
$payment->fill($ninja_payment_data);
|
|
|
|
|
|
|
|
$payment->client->service()->updatePaidToDate($payment->amount);
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2024-08-17 17:24:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getLine($data, $field = null)
|
|
|
|
{
|
|
|
|
$invoices = [];
|
2024-08-22 08:45:06 +02:00
|
|
|
$invoice = $this->getString($data, 'Line.LinkedTxn.TxnType');
|
|
|
|
if(is_null($invoice) || $invoice !== 'Invoice') {
|
|
|
|
return $invoices;
|
|
|
|
}
|
|
|
|
if(is_null(($invoice_id = $this->getInvoiceId($this->getString($data, 'Line.LinkedTxn.TxnId.value'))))) {
|
|
|
|
return $invoices;
|
|
|
|
}
|
|
|
|
|
2024-08-17 17:24:08 +02:00
|
|
|
return [[
|
|
|
|
'amount' => (float) $this->getString($data, 'Line.Amount'),
|
|
|
|
'invoice_id' => $invoice_id
|
|
|
|
]];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|