invoiceninja/app/Ninja/Import/Zoho/InvoiceTransformer.php

71 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2017-01-30 21:40:43 +02:00
<?php
namespace App\Ninja\Import\Zoho;
2015-12-13 22:12:54 +02:00
use App\Ninja\Import\BaseTransformer;
use League\Fractal\Resource\Item;
/**
2017-01-30 21:40:43 +02:00
* Class InvoiceTransformer.
*/
2015-12-13 22:12:54 +02:00
class InvoiceTransformer extends BaseTransformer
{
/**
* @param $data
2017-01-30 21:40:43 +02:00
*
* @return bool|Item
*/
2015-12-13 22:12:54 +02:00
public function transform($data)
{
2017-01-30 18:05:31 +02:00
if (! $this->getClientId($data->customer_name)) {
2015-12-13 22:12:54 +02:00
return false;
}
if ($this->hasInvoice($data->invoice_number)) {
return false;
}
return new Item($data, function ($data) {
2016-12-22 13:53:14 +02:00
$invoice = [
2015-12-13 22:12:54 +02:00
'client_id' => $this->getClientId($data->customer_name),
'invoice_number' => $this->getInvoiceNumber($data->invoice_number),
'paid' => (float) $data->total - (float) $data->balance,
2016-01-06 09:52:26 +02:00
'po_number' => $this->getString($data, 'purchaseorder'),
2015-12-13 22:12:54 +02:00
'due_date_sql' => $data->due_date,
'invoice_date_sql' => $data->invoice_date,
2016-12-22 13:53:14 +02:00
'custom_value1' => (float) $data->latefee_amount + (float) $data->adjustment + (float) $data->shipping_charge,
'custom_taxes1' => false,
2015-12-13 22:12:54 +02:00
'invoice_items' => [
[
2016-12-22 13:53:14 +02:00
'product_key' => $this->getString($data, 'item_name'),
2016-01-06 09:52:26 +02:00
'notes' => $this->getString($data, 'item_desc'),
2016-12-22 13:53:14 +02:00
'cost' => (float) $data->item_price,
'qty' => (float) $data->quantity,
'tax_name1' => (float) $data->item_tax1 ? trans('texts.tax') : '',
'tax_rate1' => (float) $data->item_tax1,
'tax_name2' => (float) $data->item_tax2 ? trans('texts.tax') : '',
'tax_rate2' => (float) $data->item_tax2,
2017-01-30 21:40:43 +02:00
],
2015-12-13 22:12:54 +02:00
],
];
2016-12-22 13:53:14 +02:00
// we don't support line item discounts so we need to include
// the discount as a separate line item
if ((float) $data->discount_amount) {
$invoice['invoice_items'][] = [
'product_key' => '',
'notes' => trans('texts.discount'),
'cost' => (float) $data->discount_amount * -1,
'qty' => 1,
'tax_name1' => (float) $data->item_tax1 ? trans('texts.tax') : '',
'tax_rate1' => (float) $data->item_tax1,
'tax_name2' => (float) $data->item_tax2 ? trans('texts.tax') : '',
'tax_rate2' => (float) $data->item_tax2,
];
}
return $invoice;
2015-12-13 22:12:54 +02:00
});
}
2016-12-22 13:53:14 +02:00
}