2022-02-17 01:51:22 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2023-05-01 00:07:41 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2022-02-17 01:51:22 +01:00
|
|
|
*
|
2023-05-01 00:07:41 +02:00
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
2022-02-17 01:51:22 +01:00
|
|
|
*
|
2023-05-01 00:07:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-02-17 01:51:22 +01:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Import\Transformer\Invoice2Go;
|
|
|
|
|
|
|
|
use App\Import\ImportException;
|
|
|
|
use App\Import\Transformer\BaseTransformer;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class InvoiceTransformer.
|
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
class InvoiceTransformer extends BaseTransformer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param $line_items_data
|
|
|
|
*
|
|
|
|
* @return bool|array
|
|
|
|
*/
|
|
|
|
public function transform($invoice_data)
|
|
|
|
{
|
2023-01-12 01:08:32 +01:00
|
|
|
if (!isset($invoice_data['DocumentNumber'])) {
|
|
|
|
throw new ImportException('DocumentNumber key not found in this import file.');
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->hasInvoice($invoice_data['DocumentNumber'])) {
|
|
|
|
throw new ImportException('Invoice number already exists');
|
|
|
|
}
|
2022-02-17 01:51:22 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$invoiceStatusMap = [
|
|
|
|
'unsent' => Invoice::STATUS_DRAFT,
|
|
|
|
'sent' => Invoice::STATUS_SENT,
|
|
|
|
];
|
2022-02-17 01:51:22 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$transformed = [
|
|
|
|
'company_id' => $this->company->id,
|
|
|
|
'number' => $this->getString($invoice_data, 'DocumentNumber'),
|
|
|
|
'notes' => $this->getString($invoice_data, 'Comment'),
|
2024-05-04 23:36:54 +02:00
|
|
|
'date' => isset($invoice_data['DocumentDate']) ? $this->parseDate($invoice_data['DocumentDate']) : null,
|
2022-06-21 11:57:17 +02:00
|
|
|
// 'currency_id' => $this->getCurrencyByCode( $invoice_data, 'Currency' ),
|
|
|
|
'amount' => $this->getFloat($invoice_data, 'TotalAmount'),
|
|
|
|
'status_id' => $invoiceStatusMap[$status =
|
|
|
|
strtolower($this->getString($invoice_data, 'DocumentStatus'))] ?? Invoice::STATUS_SENT,
|
|
|
|
// 'viewed' => $status === 'viewed',
|
|
|
|
'line_items' => [
|
|
|
|
[
|
|
|
|
'cost' => $this->getFloat($invoice_data, 'TotalAmount'),
|
|
|
|
'quantity' => 1,
|
|
|
|
'discount' => $this->getFloat($invoice_data, 'DiscountValue'),
|
|
|
|
'is_amount_discount' => false,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2022-02-17 01:51:22 +01:00
|
|
|
|
2023-04-13 08:00:59 +02:00
|
|
|
$client_id = null;
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($this->hasClient($this->getString($invoice_data, 'Name') || $this->getContact($this->getString($invoice_data, 'EmailRecipient')))) {
|
2023-04-13 08:00:59 +02:00
|
|
|
|
|
|
|
$client_id = $this->getClient($this->getString($invoice_data, 'Name'), $this->getString($invoice_data, 'EmailRecipient'));
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-04-13 08:00:59 +02:00
|
|
|
}
|
2022-02-17 01:51:22 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($client_id) {
|
|
|
|
$transformed['client_id'] = $client_id;
|
|
|
|
} else {
|
2024-01-14 05:05:00 +01:00
|
|
|
$settings = new \stdClass();
|
2022-06-21 11:57:17 +02:00
|
|
|
$settings->currency_id = $this->getCurrencyByCode($invoice_data, 'Currency');
|
2022-02-17 01:51:22 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$transformed['client'] = [
|
|
|
|
'name' => $this->getString($invoice_data, 'Name'),
|
|
|
|
'address1' => $this->getString($invoice_data, 'DocumentRecipientAddress'),
|
|
|
|
'shipping_address1' => $this->getString($invoice_data, 'ShipAddress'),
|
|
|
|
'credit_balance' => 0,
|
|
|
|
'settings' => $settings,
|
|
|
|
'client_hash' => Str::random(40),
|
|
|
|
'contacts' => [
|
|
|
|
[
|
|
|
|
'email' => $this->getString($invoice_data, 'EmailRecipient'),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if (! empty($invoice_data['Date Paid'])) {
|
|
|
|
$transformed['payments'] = [
|
|
|
|
[
|
2024-05-04 23:36:54 +02:00
|
|
|
'date' => $this->parseDate($invoice_data['DatePaid']),
|
2022-06-21 11:57:17 +02:00
|
|
|
'amount' => $this->getFloat($invoice_data, 'Payments'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2022-02-17 01:51:22 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return $transformed;
|
|
|
|
}
|
2022-02-17 01:51:22 +01:00
|
|
|
}
|