1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Import/Transformer/Csv/QuoteTransformer.php

241 lines
9.0 KiB
PHP
Raw Normal View History

2022-02-08 05:14:26 +01:00
<?php
/**
2023-05-01 00:07:41 +02:00
* Invoice Ninja (https://invoiceninja.com).
2022-02-08 05:14:26 +01:00
*
2023-05-01 00:07:41 +02:00
* @link https://github.com/invoiceninja/invoiceninja source repository
2022-02-08 05:14:26 +01:00
*
2023-05-01 00:07:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2022-02-08 05:14:26 +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\Quote;
/**
* Class QuoteTransformer.
*/
class QuoteTransformer extends BaseTransformer
{
/**
* @param $data
*
* @return bool|array
*/
public function transform($line_items_data)
{
$quote_data = reset($line_items_data);
2022-02-08 05:14:26 +01:00
if ($this->hasQuote($quote_data['quote.number'])) {
throw new ImportException('Quote number already exists');
}
2022-02-08 05:14:26 +01:00
$quoteStatusMap = [
'sent' => Quote::STATUS_SENT,
'draft' => Quote::STATUS_DRAFT,
];
2022-02-08 05:14:26 +01:00
$transformed = [
'company_id' => $this->company->id,
'number' => $this->getString($quote_data, 'quote.number'),
'user_id' => $this->getString($quote_data, 'quote.user_id'),
'amount' => ($amount = $this->getFloat(
$quote_data,
'quote.amount'
)),
'balance' => isset($quote_data['quote.balance'])
? $this->getFloat($quote_data, 'quote.balance')
: $amount,
'client_id' => $this->getClient(
$this->getString($quote_data, 'client.name'),
$this->getString($quote_data, 'client.email')
),
'discount' => $this->getFloat($quote_data, 'quote.discount'),
'po_number' => $this->getString($quote_data, 'quote.po_number'),
'date' => isset($quote_data['quote.date'])
2023-02-16 02:36:09 +01:00
? date('Y-m-d', strtotime(str_replace("/", "-", $quote_data['quote.date'])))
: now()->format('Y-m-d'),
'due_date' => isset($quote_data['quote.due_date'])
2023-02-16 02:36:09 +01:00
? date('Y-m-d', strtotime(str_replace("/", "-", $quote_data['quote.due_date'])))
: null,
'terms' => $this->getString($quote_data, 'quote.terms'),
'public_notes' => $this->getString(
$quote_data,
'quote.public_notes'
),
'private_notes' => $this->getString(
$quote_data,
'quote.private_notes'
),
'tax_name1' => $this->getString($quote_data, 'quote.tax_name1'),
'tax_rate1' => $this->getFloat($quote_data, 'quote.tax_rate1'),
'tax_name2' => $this->getString($quote_data, 'quote.tax_name2'),
'tax_rate2' => $this->getFloat($quote_data, 'quote.tax_rate2'),
'tax_name3' => $this->getString($quote_data, 'quote.tax_name3'),
'tax_rate3' => $this->getFloat($quote_data, 'quote.tax_rate3'),
'custom_value1' => $this->getString(
$quote_data,
'quote.custom_value1'
),
'custom_value2' => $this->getString(
$quote_data,
'quote.custom_value2'
),
'custom_value3' => $this->getString(
$quote_data,
'quote.custom_value3'
),
'custom_value4' => $this->getString(
$quote_data,
'quote.custom_value4'
),
'footer' => $this->getString($quote_data, 'quote.footer'),
'partial' => $this->getFloat($quote_data, 'quote.partial'),
'partial_due_date' => $this->getString(
$quote_data,
'quote.partial_due_date'
),
'custom_surcharge1' => $this->getString(
$quote_data,
'quote.custom_surcharge1'
),
'custom_surcharge2' => $this->getString(
$quote_data,
'quote.custom_surcharge2'
),
'custom_surcharge3' => $this->getString(
$quote_data,
'quote.custom_surcharge3'
),
'custom_surcharge4' => $this->getString(
$quote_data,
'quote.custom_surcharge4'
),
2023-07-16 12:39:41 +02:00
'exchange_rate' => $this->getFloatOrOne(
$quote_data,
'quote.exchange_rate'
),
'status_id' => $quoteStatusMap[
($status = strtolower(
$this->getString($quote_data, 'quote.status')
))
] ?? Quote::STATUS_SENT,
'archived' => $status === 'archived',
];
2022-02-08 05:14:26 +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-08 05:14:26 +01:00
$transformed['client'] = $client_transformer->transform(
$quote_data
);
}
2022-02-08 05:14:26 +01:00
if (isset($quote_data['payment.amount'])) {
$transformed['payments'] = [
[
'date' => isset($quote_data['payment.date'])
? date(
'Y-m-d',
strtotime($quote_data['payment.date'])
)
: date('y-m-d'),
'transaction_reference' => $this->getString(
$quote_data,
'payment.transaction_reference'
),
'amount' => $this->getFloat(
$quote_data,
'payment.amount'
),
],
];
} elseif ($status === 'paid') {
$transformed['payments'] = [
[
'date' => isset($quote_data['payment.date'])
? date(
'Y-m-d',
strtotime($quote_data['payment.date'])
)
: date('y-m-d'),
'transaction_reference' => $this->getString(
$quote_data,
'payment.transaction_reference'
),
'amount' => $this->getFloat(
$quote_data,
'quote.amount'
),
],
];
} elseif (
isset($transformed['amount']) &&
isset($transformed['balance']) &&
$transformed['amount'] != $transformed['balance']
) {
$transformed['payments'] = [
[
'date' => isset($quote_data['payment.date'])
? date(
'Y-m-d',
strtotime($quote_data['payment.date'])
)
: date('y-m-d'),
'transaction_reference' => $this->getString(
$quote_data,
'payment.transaction_reference'
),
'amount' => $transformed['amount'] - $transformed['balance'],
],
];
}
2022-02-08 05:14:26 +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'
),
'type_id' => '1', //$this->getQuoteTypeId( $record, 'item.type_id' ),
];
}
$transformed['line_items'] = $line_items;
2022-02-08 05:14:26 +01:00
return $transformed;
}
2022-02-08 05:14:26 +01:00
}