2022-02-10 03:02:02 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/clientninja/clientninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://clientninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
2022-02-10 04:53:30 +01:00
|
|
|
namespace App\Import\Transformer\Wave;
|
2022-02-10 03:02:02 +01:00
|
|
|
|
|
|
|
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 );
|
|
|
|
|
|
|
|
if ( $this->hasInvoice( $invoice_data['Invoice Number'] ) ) {
|
|
|
|
throw new ImportException( 'Invoice number already exists' );
|
|
|
|
}
|
|
|
|
|
2022-03-02 23:22:31 +01:00
|
|
|
if(array_key_exists('Invoice Date', $invoice_data))
|
|
|
|
$date_key = 'Invoice Date';
|
|
|
|
else
|
|
|
|
$date_key = 'Transaction Date';
|
|
|
|
|
|
|
|
if(array_key_exists('Customer Name', $invoice_data))
|
|
|
|
$customer_key = 'Customer Name';
|
|
|
|
else
|
|
|
|
$customer_key = 'Customer';
|
|
|
|
|
2022-02-10 03:02:02 +01:00
|
|
|
$transformed = [
|
|
|
|
'company_id' => $this->company->id,
|
2022-03-02 23:22:31 +01:00
|
|
|
'client_id' => $this->getClient( $customer_name = $this->getString( $invoice_data, $customer_key ), null ),
|
2022-02-10 03:02:02 +01:00
|
|
|
'number' => $invoice_number = $this->getString( $invoice_data, 'Invoice Number' ),
|
2022-03-02 23:22:31 +01:00
|
|
|
'date' => date( 'Y-m-d', strtotime( $invoice_data[$date_key] ) ) ?: now()->format('Y-m-d'), //27-01-2022
|
2022-02-10 03:02:02 +01:00
|
|
|
'currency_id' => $this->getCurrencyByCode( $invoice_data, 'Currency' ),
|
|
|
|
'status_id' => Invoice::STATUS_SENT,
|
2022-03-02 23:22:31 +01:00
|
|
|
'due_date' => array_key_exists('Due Date', $invoice_data) ? date( 'Y-m-d', strtotime( $invoice_data['Due Date'] ) ) : null,
|
2022-02-10 03:02:02 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$line_items = [];
|
|
|
|
$payments = [];
|
|
|
|
foreach ( $line_items_data as $record ) {
|
2022-03-02 23:22:31 +01:00
|
|
|
if (array_key_exists('Account Type', $record) && $record['Account Type'] === 'Income' ) {
|
2022-02-10 03:02:02 +01:00
|
|
|
$description = $this->getString( $record, 'Transaction Line Description' );
|
|
|
|
|
|
|
|
// Remove duplicate data from description
|
|
|
|
if ( substr( $description, 0, strlen( $customer_name ) + 3 ) === $customer_name . ' - ' ) {
|
|
|
|
$description = substr( $description, strlen( $customer_name ) + 3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( substr( $description, 0, strlen( $invoice_number ) + 3 ) === $invoice_number . ' - ' ) {
|
|
|
|
$description = substr( $description, strlen( $invoice_number ) + 3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$line_items[] = [
|
|
|
|
'notes' => $description,
|
|
|
|
'cost' => $this->getFloat( $record, 'Amount Before Sales Tax' ),
|
|
|
|
'tax_name1' => $this->getString( $record, 'Sales Tax Name' ),
|
|
|
|
'tax_rate1' => $this->getFloat( $record, 'Sales Tax Amount' ),
|
|
|
|
|
|
|
|
'quantity' => 1,
|
|
|
|
];
|
2022-03-02 23:22:31 +01:00
|
|
|
} elseif (array_key_exists('Account Type', $record) && $record['Account Type'] === 'System Receivable Invoice' ) {
|
2022-02-10 03:02:02 +01:00
|
|
|
// This is a payment
|
|
|
|
$payments[] = [
|
2022-03-02 23:22:31 +01:00
|
|
|
'date' => date( 'Y-m-d', strtotime( $invoice_data[$date_key] ) ),
|
2022-02-10 03:02:02 +01:00
|
|
|
'amount' => $this->getFloat( $record, 'Amount (One column)' ),
|
|
|
|
];
|
|
|
|
}
|
2022-03-02 23:22:31 +01:00
|
|
|
else {
|
|
|
|
//could be a generate invoices.csv file
|
|
|
|
$line_items[] = [
|
|
|
|
'notes' => 'Imported Invoice',
|
|
|
|
'cost' => $this->getFloat( $record, 'Invoice Total' ),
|
|
|
|
'tax_name1' => 'Tax',
|
|
|
|
'tax_rate1' => round($this->getFloat( $record, 'Invoice Tax Total' ) / $this->getFloat( $record, 'Invoice Total' ) * 100,2),
|
|
|
|
'quantity' => 1,
|
|
|
|
];
|
|
|
|
|
|
|
|
if($record['Invoice Paid'] > 0){
|
|
|
|
$payments[] = [
|
|
|
|
'date' => date( 'Y-m-d', strtotime( $record['Last Payment Date'] ) ),
|
|
|
|
'amount' => $this->getFloat( $record, 'Invoice Paid' ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-02-10 03:02:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$transformed['line_items'] = $line_items;
|
|
|
|
$transformed['payments'] = $payments;
|
|
|
|
|
|
|
|
return $transformed;
|
|
|
|
}
|
|
|
|
}
|