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-09-19 08:39:53 +02:00
|
|
|
use App\Models\Credit;
|
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);
|
2024-09-19 08:39:53 +02:00
|
|
|
$payment->save();
|
|
|
|
|
2024-08-26 07:48:48 +02:00
|
|
|
$payment->client->service()->updatePaidToDate($payment->amount);
|
|
|
|
|
2024-09-19 08:39:53 +02:00
|
|
|
if($payment->amount == 0) {
|
|
|
|
//this is a credit memo, create a stub credit for this.
|
|
|
|
$payment = $this->createCredit($payment, $qb_data);
|
|
|
|
$payment->type_id = \App\Models\PaymentType::CREDIT;
|
|
|
|
$payment->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-26 07:48:48 +02:00
|
|
|
return $payment;
|
|
|
|
|
2024-09-19 08:39:53 +02:00
|
|
|
}
|
2024-08-26 07:48:48 +02:00
|
|
|
return null;
|
2024-08-17 17:24:08 +02:00
|
|
|
}
|
|
|
|
|
2024-09-19 08:39:53 +02:00
|
|
|
private function createCredit($payment, $qb_data)
|
|
|
|
{
|
|
|
|
$credit_line = null;
|
|
|
|
|
|
|
|
foreach($qb_data->Line as $item) {
|
|
|
|
|
|
|
|
if(data_get($item, 'LinkedTxn.TxnType', null) == 'CreditMemo') {
|
|
|
|
$credit_line = $item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$credit_line)
|
|
|
|
return $payment;
|
|
|
|
|
2024-09-23 05:36:12 +02:00
|
|
|
$credit = \App\Factory\CreditFactory::create($this->company->id, $this->company->owner()->id);
|
2024-09-19 08:39:53 +02:00
|
|
|
$credit->client_id = $payment->client_id;
|
|
|
|
|
|
|
|
$line = new \App\DataMapper\InvoiceItem();
|
|
|
|
$line->quantity = 1;
|
|
|
|
$line->cost = $credit_line->Amount;
|
|
|
|
$line->product_key = 'CREDITMEMO';
|
|
|
|
$line->notes = $payment->private_notes;
|
|
|
|
|
|
|
|
$credit->date = $qb_data->TxnDate;
|
|
|
|
$credit->status_id = 4;
|
|
|
|
$credit->amount = $credit_line->Amount;
|
|
|
|
$credit->paid_to_date = $credit_line->Amount;
|
|
|
|
$credit->balance = 0;
|
|
|
|
$credit->line_items = [$line];
|
|
|
|
$credit->save();
|
|
|
|
|
|
|
|
$paymentable = new \App\Models\Paymentable();
|
|
|
|
$paymentable->payment_id = $payment->id;
|
|
|
|
$paymentable->paymentable_id = $credit->id;
|
|
|
|
$paymentable->paymentable_type = \App\Models\Credit::class;
|
|
|
|
$paymentable->amount = $credit->amount;
|
|
|
|
$paymentable->created_at = $payment->date;
|
|
|
|
$paymentable->save();
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
]];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|