1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Import/Transformer/Csv/InvoiceTransformer.php

234 lines
9.0 KiB
PHP
Raw Normal View History

2022-02-01 10:04:03 +01:00
<?php
/**
2023-05-01 00:07:41 +02:00
* client Ninja (https://invoiceninja.com).
2022-02-01 10:04:03 +01:00
*
2023-05-01 00:07:41 +02:00
* @link https://github.com/invoiceninja/invoiceninja source repository
2022-02-01 10:04:03 +01:00
*
2023-05-01 00:07:41 +02:00
* @copyright Copyright (c) 2022. client Ninja LLC (https://invoiceninja.com)
2022-02-01 10:04:03 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Import\Transformer\Csv;
use App\Import\ImportException;
use App\Import\Transformer\BaseTransformer;
use App\Models\Invoice;
/**
* Class InvoiceTransformer.
*/
2022-02-03 00:14:54 +01:00
class InvoiceTransformer extends BaseTransformer
{
/**
* @param $data
*
* @return bool|array
*/
public function transform($line_items_data)
{
$invoice_data = reset($line_items_data);
2022-02-01 10:04:03 +01:00
if ($this->hasInvoice($invoice_data['invoice.number'])) {
throw new ImportException('Invoice number already exists');
}
2022-02-01 10:04:03 +01:00
$invoiceStatusMap = [
'sent' => Invoice::STATUS_SENT,
'draft' => Invoice::STATUS_DRAFT,
];
2022-02-01 10:04:03 +01:00
$transformed = [
'company_id' => $this->company->id,
'number' => $this->getString($invoice_data, 'invoice.number'),
'user_id' => $this->getString($invoice_data, 'invoice.user_id'),
'amount' => ($amount = $this->getFloat(
$invoice_data,
'invoice.amount'
)),
'balance' => isset($invoice_data['invoice.balance'])
? $this->getFloat($invoice_data, 'invoice.balance')
: $amount,
'client_id' => $this->getClient(
$this->getString($invoice_data, 'client.name'),
$this->getString($invoice_data, 'client.email')
),
'discount' => $this->getFloat($invoice_data, 'invoice.discount'),
'po_number' => $this->getString($invoice_data, 'invoice.po_number'),
'date' => isset($invoice_data['invoice.date'])
2022-11-01 23:49:40 +01:00
? $this->parseDate($invoice_data['invoice.date'])
: now()->format('Y-m-d'),
'due_date' => isset($invoice_data['invoice.due_date'])
2022-11-01 23:49:40 +01:00
? $this->parseDate($invoice_data['invoice.due_date'])
: null,
'terms' => $this->getString($invoice_data, 'invoice.terms'),
'public_notes' => $this->getString(
$invoice_data,
'invoice.public_notes'
),
'private_notes' => $this->getString(
$invoice_data,
'invoice.private_notes'
),
'tax_name1' => $this->getString($invoice_data, 'invoice.tax_name1'),
'tax_rate1' => $this->getFloat($invoice_data, 'invoice.tax_rate1'),
'tax_name2' => $this->getString($invoice_data, 'invoice.tax_name2'),
'tax_rate2' => $this->getFloat($invoice_data, 'invoice.tax_rate2'),
'tax_name3' => $this->getString($invoice_data, 'invoice.tax_name3'),
'tax_rate3' => $this->getFloat($invoice_data, 'invoice.tax_rate3'),
'custom_value1' => $this->getString(
$invoice_data,
'invoice.custom_value1'
),
'custom_value2' => $this->getString(
$invoice_data,
'invoice.custom_value2'
),
'custom_value3' => $this->getString(
$invoice_data,
'invoice.custom_value3'
),
'custom_value4' => $this->getString(
$invoice_data,
'invoice.custom_value4'
),
'footer' => $this->getString($invoice_data, 'invoice.footer'),
2022-11-06 09:03:27 +01:00
'partial' => $this->getFloat($invoice_data, 'invoice.partial') > 0 ?: null,
'partial_due_date' => $this->getString(
$invoice_data,
'invoice.partial_due_date'
),
'custom_surcharge1' => $this->getString(
$invoice_data,
'invoice.custom_surcharge1'
),
'custom_surcharge2' => $this->getString(
$invoice_data,
'invoice.custom_surcharge2'
),
'custom_surcharge3' => $this->getString(
$invoice_data,
'invoice.custom_surcharge3'
),
'custom_surcharge4' => $this->getString(
$invoice_data,
'invoice.custom_surcharge4'
),
2023-07-16 12:39:41 +02:00
'exchange_rate' => $this->getFloatOrOne(
$invoice_data,
'invoice.exchange_rate'
),
'status_id' => $invoiceStatusMap[
($status = strtolower(
$this->getString($invoice_data, 'invoice.status')
))
] ?? Invoice::STATUS_SENT,
// 'archived' => $status === 'archived',
];
2022-02-01 10:04:03 +01:00
/* If we can't find the client, then lets try and create a client */
if (! $transformed['client_id']) {
$client_transformer = new ClientTransformer($this->company);
2022-02-02 05:56:37 +01:00
$transformed['client'] = $client_transformer->transform(
$invoice_data
);
}
2022-02-02 05:56:37 +01:00
if (isset($invoice_data['payment.amount'])) {
$transformed['payments'] = [
[
'date' => isset($invoice_data['payment.date'])
2022-11-01 23:49:40 +01:00
? $this->parseDate($invoice_data['payment.date'])
: date('y-m-d'),
'transaction_reference' => $this->getString(
$invoice_data,
'payment.transaction_reference'
),
'amount' => $this->getFloat(
$invoice_data,
'payment.amount'
),
],
];
} elseif ($status === 'paid') {
$transformed['payments'] = [
[
'date' => isset($invoice_data['payment.date'])
2022-11-01 23:49:40 +01:00
? $this->parseDate($invoice_data['payment.date'])
: date('y-m-d'),
'transaction_reference' => $this->getString(
$invoice_data,
'payment.transaction_reference'
),
'amount' => $this->getFloat(
$invoice_data,
'invoice.amount'
),
],
];
2023-10-26 04:57:44 +02:00
}
2023-05-10 00:58:04 +02:00
// elseif (
// isset($transformed['amount']) &&
// isset($transformed['balance']) &&
// $transformed['amount'] != $transformed['balance']
// ) {
// $transformed['payments'] = [
// [
// 'date' => isset($invoice_data['payment.date'])
// ? $this->parseDate($invoice_data['payment.date'])
// : date('y-m-d'),
// 'transaction_reference' => $this->getString(
// $invoice_data,
// 'payment.transaction_reference'
// ),
// 'amount' => $transformed['amount'] - $transformed['balance'],
// ],
// ];
// }
2022-02-01 10:04:03 +01:00
$line_items = [];
foreach ($line_items_data as $record) {
$line_items[] = [
'quantity' => $this->getFloat($record, 'item.quantity'),
'cost' => $this->getFloat($record, 'item.cost'),
'product_key' => $this->getString($record, 'item.product_key'),
'notes' => $this->getString($record, 'item.notes'),
'discount' => $this->getFloat($record, 'item.discount'),
'is_amount_discount' => filter_var(
$this->getString($record, 'item.is_amount_discount'),
FILTER_VALIDATE_BOOLEAN,
FILTER_NULL_ON_FAILURE
),
'tax_name1' => $this->getString($record, 'item.tax_name1'),
'tax_rate1' => $this->getFloat($record, 'item.tax_rate1'),
'tax_name2' => $this->getString($record, 'item.tax_name2'),
'tax_rate2' => $this->getFloat($record, 'item.tax_rate2'),
'tax_name3' => $this->getString($record, 'item.tax_name3'),
'tax_rate3' => $this->getFloat($record, 'item.tax_rate3'),
'custom_value1' => $this->getString(
$record,
'item.custom_value1'
),
'custom_value2' => $this->getString(
$record,
'item.custom_value2'
),
'custom_value3' => $this->getString(
$record,
'item.custom_value3'
),
'custom_value4' => $this->getString(
$record,
'item.custom_value4'
),
2023-02-16 02:36:09 +01:00
'type_id' => $this->getInvoiceTypeId($record, 'item.type_id'),
];
}
2023-02-01 23:15:11 +01:00
$transformed['line_items'] = $line_items;
2022-02-01 10:04:03 +01:00
return $transformed;
}
2022-02-01 10:04:03 +01:00
}