2021-02-17 00:15:42 +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)
|
2021-02-17 00:15:42 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-02-17 00:15:42 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Import\Transformers\Invoicely;
|
|
|
|
|
|
|
|
use App\Import\ImportException;
|
|
|
|
use App\Import\Transformers\BaseTransformer;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class InvoiceTransformer.
|
|
|
|
*/
|
|
|
|
class InvoiceTransformer extends BaseTransformer {
|
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
*
|
|
|
|
* @return bool|array
|
|
|
|
*/
|
|
|
|
public function transform( $data ) {
|
|
|
|
if ( $this->hasInvoice( $data['Details'] ) ) {
|
|
|
|
throw new ImportException( 'Invoice number already exists' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$transformed = [
|
|
|
|
'company_id' => $this->maps['company']->id,
|
|
|
|
'client_id' => $this->getClient( $this->getString( $data, 'Client' ), null ),
|
|
|
|
'number' => $this->getString( $data, 'Details' ),
|
|
|
|
'date' => isset( $data['Date'] ) ? date( 'Y-m-d', strtotime( $data['Date'] ) ) : null,
|
|
|
|
'due_date' => isset( $data['Due'] ) ? date( 'Y-m-d', strtotime( $data['Due'] ) ) : null,
|
|
|
|
'status_id' => Invoice::STATUS_SENT,
|
|
|
|
'line_items' => [
|
|
|
|
[
|
|
|
|
'cost' => $amount = $this->getFloat( $data, 'Total' ),
|
|
|
|
'quantity' => 1,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
if ( strtolower( $data['Status'] ) === 'paid' ) {
|
|
|
|
$transformed['payments'] = [
|
|
|
|
[
|
|
|
|
'date' => date( 'Y-m-d' ),
|
|
|
|
'amount' => $amount,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $transformed;
|
|
|
|
}
|
|
|
|
}
|