1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Import/Transformers/Invoice2Go/InvoiceTransformer.php
Shift 19080933b6
Apply Laravel coding style
Shift automatically applies the Laravel coding style - which uses the PSR-2 coding style as a base with some minor additions.

You may customize the code style applied by adding a [PHP CS Fixer][1] or [PHP CodeSniffer][2] ruleset to your project root. Feel free to use [Shift's Laravel ruleset][3] to help you get started.

For more information on customizing the code style applied by Shift, [watch this short video][4].

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://github.com/squizlabs/PHP_CodeSniffer
[3]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200
[4]: https://laravelshift.com/videos/shift-code-style
2022-06-21 09:57:17 +00:00

92 lines
3.2 KiB
PHP

<?php
/**
* client Ninja (https://clientninja.com).
*
* @link https://github.com/clientninja/clientninja source repository
*
* @copyright Copyright (c) 2022. client Ninja LLC (https://clientninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Import\Transformers\Invoice2Go;
use App\Import\ImportException;
use App\Import\Transformers\BaseTransformer;
use App\Models\Invoice;
use Illuminate\Support\Str;
/**
* Class InvoiceTransformer.
*/
class InvoiceTransformer extends BaseTransformer
{
/**
* @param $line_items_data
*
* @return bool|array
*/
public function transform($invoice_data)
{
if ($this->hasInvoice($invoice_data['DocumentNumber'])) {
throw new ImportException('Invoice number already exists');
}
$invoiceStatusMap = [
'unsent' => Invoice::STATUS_DRAFT,
'sent' => Invoice::STATUS_SENT,
];
$transformed = [
'company_id' => $this->maps['company']->id,
'number' => $this->getString($invoice_data, 'DocumentNumber'),
'notes' => $this->getString($invoice_data, 'Comment'),
'date' => isset($invoice_data['DocumentDate']) ? date('Y-m-d', strtotime($invoice_data['DocumentDate'])) : null,
'currency_id' => $this->getCurrencyByCode($invoice_data, 'Currency'),
'amount' => 0,
'status_id' => $invoiceStatusMap[$status =
strtolower($this->getString($invoice_data, 'DocumentStatus'))] ?? Invoice::STATUS_SENT,
// 'viewed' => $status === 'viewed',
'line_items' => [
[
'amount' => $amount = $this->getFloat($invoice_data, 'TotalAmount'),
'quantity' => 1,
'discount' => $this->getFloat($invoice_data, 'DiscountValue'),
'is_amount_discount' => false,
],
],
];
$client_id =
$this->getClient($this->getString($invoice_data, 'Name'), $this->getString($invoice_data, 'EmailRecipient'));
if ($client_id) {
$transformed['client_id'] = $client_id;
} else {
$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' => new \stdClass,
'client_hash' => Str::random(40),
'contacts' => [
[
'email' => $this->getString($invoice_data, 'Email'),
],
],
];
}
if (! empty($invoice_data['Date Paid'])) {
$transformed['payments'] = [
[
'date' => date('Y-m-d', strtotime($invoice_data['DatePaid'])),
'amount' => $this->getFloat($invoice_data, 'Payments'),
],
];
}
return $transformed;
}
}