1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 00:11:35 +02:00
invoiceninja/app/Ninja/Import/CSV/InvoiceTransformer.php

57 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Import\CSV;
2015-11-24 20:45:38 +01:00
2015-12-08 11:10:20 +01:00
use App\Ninja\Import\BaseTransformer;
2015-11-24 20:45:38 +01:00
use League\Fractal\Resource\Item;
/**
2017-01-30 20:40:43 +01:00
* Class InvoiceTransformer.
*/
2015-12-08 11:10:20 +01:00
class InvoiceTransformer extends BaseTransformer
2015-11-24 20:45:38 +01:00
{
/**
* @param $data
2017-01-30 20:40:43 +01:00
*
* @return bool|Item
*/
2015-12-08 11:10:20 +01:00
public function transform($data)
2015-11-24 20:45:38 +01:00
{
2018-01-01 14:20:36 +01:00
$clientId = $this->getClientId($data->email) ?: $this->getClientId($data->name);
if (! $clientId) {
2015-11-24 20:45:38 +01:00
return false;
}
2015-11-24 21:41:08 +01:00
2015-12-08 11:10:20 +01:00
if (isset($data->invoice_number) && $this->hasInvoice($data->invoice_number)) {
2015-11-24 20:45:38 +01:00
return false;
}
2015-12-08 11:10:20 +01:00
2018-01-01 14:20:36 +01:00
return new Item($data, function ($data) use ($clientId) {
2015-11-24 20:45:38 +01:00
return [
2018-01-01 14:20:36 +01:00
'client_id' => $clientId,
2015-12-10 14:35:40 +01:00
'invoice_number' => isset($data->invoice_number) ? $this->getInvoiceNumber($data->invoice_number) : null,
2016-12-22 18:17:45 +01:00
'paid' => $this->getFloat($data, 'paid'),
2015-12-31 16:09:45 +01:00
'po_number' => $this->getString($data, 'po_number'),
'terms' => $this->getString($data, 'terms'),
'public_notes' => $this->getString($data, 'public_notes'),
2017-08-23 16:16:02 +02:00
'private_notes' => $this->getString($data, 'private_notes'),
2017-01-15 20:48:31 +01:00
'invoice_date_sql' => $this->getDate($data, 'invoice_date'),
'due_date_sql' => $this->getDate($data, 'due_date'),
2015-11-24 20:45:38 +01:00
'invoice_items' => [
[
2017-08-23 14:26:03 +02:00
'product_key' => $this->getString($data, 'item_product'),
'notes' => $this->getString($data, 'item_notes') ?: $this->getProduct($data, 'item_product', 'notes', ''),
2017-12-02 21:45:19 +01:00
'cost' => $this->getFloat($data, 'item_cost') ?: $this->getProduct($data, 'item_product', 'cost', 0),
2017-08-23 14:26:03 +02:00
'qty' => $this->getFloat($data, 'item_quantity') ?: 1,
2017-08-23 16:05:14 +02:00
'tax_name1' => $this->getTaxName($this->getString($data, 'item_tax1')) ?: $this->getProduct($data, 'item_product', 'tax_name1', ''),
'tax_rate1' => $this->getTaxRate($this->getString($data, 'item_tax1')) ?: $this->getProduct($data, 'item_product', 'tax_rate1', 0),
'tax_name2' => $this->getTaxName($this->getString($data, 'item_tax2')) ?: $this->getProduct($data, 'item_product', 'tax_name2', ''),
'tax_rate2' => $this->getTaxRate($this->getString($data, 'item_tax2')) ?: $this->getProduct($data, 'item_product', 'tax_rate2', 0),
2017-01-30 20:40:43 +01:00
],
2015-11-24 20:45:38 +01:00
],
];
});
}
2016-12-22 18:17:45 +01:00
}