1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Import/Transformer/Freshbooks/InvoiceTransformer.php

109 lines
3.8 KiB
PHP
Raw Normal View History

2022-02-17 00:20:35 +01:00
<?php
/**
* client Ninja (https://clientninja.com).
*
* @link https://github.com/clientninja/clientninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. client Ninja LLC (https://clientninja.com)
2022-02-17 00:20:35 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Import\Transformer\Freshbooks;
use App\Import\ImportException;
use App\Import\Transformer\BaseTransformer;
use App\Models\Invoice;
/**
* Class InvoiceTransformer.
*/
class InvoiceTransformer extends BaseTransformer
{
/**
* @param $line_items_data
*
* @return bool|array
*/
public function transform($line_items_data)
{
$invoice_data = reset($line_items_data);
2022-02-17 00:20:35 +01:00
if ($this->hasInvoice($invoice_data['Invoice #'])) {
throw new ImportException('Invoice number already exists');
}
2022-02-17 00:20:35 +01:00
$invoiceStatusMap = [
'sent' => Invoice::STATUS_SENT,
'draft' => Invoice::STATUS_DRAFT,
];
2022-02-17 00:20:35 +01:00
$transformed = [
'company_id' => $this->company->id,
'client_id' => $this->getClient($this->getString($invoice_data, 'Client Name'), null),
'number' => $this->getString($invoice_data, 'Invoice #'),
'date' => isset($invoice_data['Date Issued']) ? date('Y-m-d', strtotime($invoice_data['Date Issued'])) : null,
// 'currency_id' => $this->getCurrencyByCode( $invoice_data, 'Currency' ),
'amount' => 0,
'status_id' => $invoiceStatusMap[$status =
strtolower($this->getString($invoice_data, 'Invoice Status'))] ?? Invoice::STATUS_SENT,
// 'viewed' => $status === 'viewed',
];
2022-02-17 00:20:35 +01:00
$line_items = [];
foreach ($line_items_data as $record) {
$line_items[] = [
'product_key' => $this->getString($record, 'Item Name'),
'notes' => $this->getString($record, 'Item Description'),
'cost' => $this->getFreshbookQuantityFloat($record, 'Rate'),
'quantity' => $this->getFreshbookQuantityFloat($record, 'Quantity'),
'discount' => $this->getFreshbookQuantityFloat($record, 'Discount Percentage'),
'is_amount_discount' => false,
'tax_name1' => $this->getString($record, 'Tax 1 Type'),
2023-01-05 12:09:25 +01:00
'tax_rate1' => $this->calcTaxRate($record, 'Tax 1 Amount'),
'tax_name2' => $this->getString($record, 'Tax 2 Type'),
2023-01-05 12:09:25 +01:00
'tax_rate2' => $this->calcTaxRate($record, 'Tax 2 Amount'),
];
$transformed['amount'] += $this->getFreshbookQuantityFloat($record, 'Line Total');
}
$transformed['line_items'] = $line_items;
2022-02-17 00:20:35 +01:00
if (! empty($invoice_data['Date Paid'])) {
$transformed['payments'] = [[
'date' => date('Y-m-d', strtotime($invoice_data['Date Paid'])),
'amount' => $transformed['amount'],
]];
}
2022-02-17 00:20:35 +01:00
return $transformed;
}
2022-02-17 00:20:35 +01:00
2023-01-05 12:09:25 +01:00
//Line Subtotal
public function calcTaxRate($record, $field)
{
2023-02-16 02:36:09 +01:00
if (isset($record['Line Subtotal']) && $record['Line Subtotal'] > 0) {
2023-01-05 12:09:25 +01:00
return ($record[$field] / $record['Line Subtotal']) * 100;
2023-02-16 02:36:09 +01:00
}
2023-01-05 12:09:25 +01:00
$tax_amount1 = isset($record['Tax 1 Amount']) ? $record['Tax 1 Amount'] : 0;
$tax_amount2 = isset($record['Tax 2 Amount']) ? $record['Tax 2 Amount'] : 0;
$line_total = isset($record['Line Total']) ? $record['Line Total'] : 0;
$subtotal = $line_total - $tax_amount2 - $tax_amount1;
2023-02-16 02:36:09 +01:00
if ($subtotal > 0) {
2023-01-05 12:09:25 +01:00
return $record[$field] / $subtotal * 100;
2023-02-16 02:36:09 +01:00
}
2023-01-05 12:09:25 +01:00
return 0;
}
/** @return float */
public function getFreshbookQuantityFloat($data, $field)
{
return $data[$field];
}
2022-02-17 00:20:35 +01:00
}