2024-07-12 17:01:27 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Import\Transformer\Quickbooks;
|
|
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Support\Arr;
|
|
|
|
use App\Import\ImportException;
|
|
|
|
use App\DataMapper\InvoiceItem;
|
|
|
|
use App\Models\Invoice as Model;
|
|
|
|
use App\Import\Transformer\BaseTransformer;
|
2024-08-17 17:24:53 +02:00
|
|
|
use App\Import\Transformer\Quickbooks\CommonTrait;
|
2024-07-16 19:02:59 +02:00
|
|
|
use App\Import\Transformer\Quickbooks\ClientTransformer;
|
2024-07-12 17:01:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class InvoiceTransformer.
|
|
|
|
*/
|
|
|
|
class InvoiceTransformer extends BaseTransformer
|
|
|
|
{
|
2024-08-17 17:24:53 +02:00
|
|
|
use CommonTrait {
|
|
|
|
transform as preTransform;
|
|
|
|
}
|
2024-07-12 17:01:27 +02:00
|
|
|
|
|
|
|
private $fillable = [
|
|
|
|
'amount' => "TotalAmt",
|
|
|
|
'line_items' => "Line",
|
|
|
|
'due_date' => "DueDate",
|
|
|
|
'partial' => "Deposit",
|
|
|
|
'balance' => "Balance",
|
2024-08-17 17:24:53 +02:00
|
|
|
'private_notes' => "CustomerMemo",
|
|
|
|
'public_notes' => "CustomerMemo",
|
2024-07-12 17:01:27 +02:00
|
|
|
'number' => "DocNumber",
|
|
|
|
'created_at' => "CreateTime",
|
2024-08-17 17:24:53 +02:00
|
|
|
'updated_at' => "LastUpdatedTime",
|
|
|
|
'payments' => 'LinkedTxn',
|
|
|
|
'status_id' => 'InvoiceStatus',
|
2024-07-12 17:01:27 +02:00
|
|
|
];
|
|
|
|
|
2024-08-17 17:24:53 +02:00
|
|
|
public function __construct($company)
|
2024-07-12 17:01:27 +02:00
|
|
|
{
|
2024-08-17 17:24:53 +02:00
|
|
|
parent::__construct($company);
|
2024-07-12 17:01:27 +02:00
|
|
|
|
2024-08-17 17:24:53 +02:00
|
|
|
$this->model = new Model;
|
|
|
|
}
|
2024-07-12 17:01:27 +02:00
|
|
|
|
2024-08-17 17:24:53 +02:00
|
|
|
public function getInvoiceStatus($data)
|
|
|
|
{
|
|
|
|
return Invoice::STATUS_SENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function transform($data)
|
|
|
|
{
|
|
|
|
return $this->preTransform($data) + $this->getInvoiceClient($data);
|
2024-07-12 17:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTotalAmt($data)
|
|
|
|
{
|
|
|
|
return (float) $this->getString($data,'TotalAmt');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLine($data)
|
|
|
|
{
|
|
|
|
return array_map(function ($item) {
|
|
|
|
return [
|
|
|
|
'description' => $this->getString($item,'Description'),
|
2024-08-16 02:53:11 +02:00
|
|
|
'product_key' => $this->getString($item,'Description'),
|
|
|
|
'quantity' => (int) $this->getString($item,'SalesItemLineDetail.Qty'),
|
2024-08-17 17:24:53 +02:00
|
|
|
'unit_price' =>(double) $this->getString($item,'SalesItemLineDetail.UnitPrice'),
|
|
|
|
'line_total' => (double) $this->getString($item,'Amount'),
|
|
|
|
'cost' =>(double) $this->getString($item,'SalesItemLineDetail.UnitPrice'),
|
|
|
|
'product_cost' => (double) $this->getString($item,'SalesItemLineDetail.UnitPrice'),
|
|
|
|
'tax_amount' => (double) $this->getString($item,'TxnTaxDetail.TotalTax'),
|
2024-07-12 17:01:27 +02:00
|
|
|
];
|
|
|
|
}, array_filter($this->getString($data,'Line'), function ($item) {
|
2024-07-16 19:02:59 +02:00
|
|
|
return $this->getString($item,'DetailType') !== 'SubTotalLineDetail';
|
2024-07-12 17:01:27 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2024-07-16 19:02:59 +02:00
|
|
|
public function getInvoiceClient($data, $field = null) {
|
|
|
|
/**
|
|
|
|
* "CustomerRef": {
|
|
|
|
"value": "23",
|
|
|
|
"name": ""Barnett Design
|
|
|
|
},
|
|
|
|
"CustomerMemo": {
|
|
|
|
"value": "Thank you for your business and have a great day!"
|
|
|
|
},
|
|
|
|
"BillAddr": {
|
|
|
|
"Id": "58",
|
|
|
|
"Line1": "Shara Barnett",
|
|
|
|
"Line2": "Barnett Design",
|
|
|
|
"Line3": "19 Main St.",
|
|
|
|
"Line4": "Middlefield, CA 94303",
|
|
|
|
"Lat": "37.4530553",
|
|
|
|
"Long": "-122.1178261"
|
|
|
|
},
|
|
|
|
"ShipAddr": {
|
|
|
|
"Id": "24",
|
|
|
|
"Line1": "19 Main St.",
|
|
|
|
"City": "Middlefield",
|
|
|
|
"CountrySubDivisionCode": "CA",
|
|
|
|
"PostalCode": "94303",
|
|
|
|
"Lat": "37.445013",
|
|
|
|
"Long": "-122.1391443"
|
|
|
|
},"BillEmail": {
|
|
|
|
"Address": "Design@intuit.com"
|
|
|
|
},
|
|
|
|
[
|
|
|
|
'name' => 'CompanyName',
|
|
|
|
'phone' => 'PrimaryPhone.FreeFormNumber',
|
|
|
|
'country_id' => 'BillAddr.Country',
|
|
|
|
'state' => 'BillAddr.CountrySubDivisionCode',
|
|
|
|
'address1' => 'BillAddr.Line1',
|
|
|
|
'city' => 'BillAddr.City',
|
|
|
|
'postal_code' => 'BillAddr.PostalCode',
|
|
|
|
'shipping_country_id' => 'ShipAddr.Country',
|
|
|
|
'shipping_state' => 'ShipAddr.CountrySubDivisionCode',
|
|
|
|
'shipping_address1' => 'ShipAddr.Line1',
|
|
|
|
'shipping_city' => 'ShipAddr.City',
|
|
|
|
'shipping_postal_code' => 'ShipAddr.PostalCode',
|
|
|
|
'public_notes' => 'Notes'
|
|
|
|
];
|
|
|
|
|
|
|
|
*/
|
|
|
|
$bill_address = (object) $this->getString($data, 'BillAddr');
|
|
|
|
$ship_address = $this->getString($data, 'ShipAddr');
|
|
|
|
$customer = explode(" ", $this->getString($data, 'CustomerRef.name'));
|
|
|
|
$customer = ['GivenName' => $customer[0], 'FamilyName' => $customer[1]];
|
|
|
|
$has_company = property_exists($bill_address, 'Line4');
|
2024-07-17 18:34:09 +02:00
|
|
|
$address = $has_company? $bill_address->Line4 : $bill_address->Line3;
|
|
|
|
$address_1 = substr($address, 0, stripos($address,','));
|
|
|
|
$address =array_filter( [$address_1] + (explode(' ', substr($address, stripos($address,",") + 1 ))));
|
2024-08-16 02:53:11 +02:00
|
|
|
$client_id = null;
|
2024-07-16 19:02:59 +02:00
|
|
|
$client =
|
|
|
|
[
|
|
|
|
"CompanyName" => $has_company? $bill_address->Line2 : $bill_address->Line1,
|
2024-08-16 02:53:11 +02:00
|
|
|
"BillAddr" => array_combine(['City','CountrySubDivisionCode','PostalCode'], array_pad($address,3,'N/A') ) + ['Line1' => $has_company? $bill_address->Line3 : $bill_address->Line2 ],
|
2024-07-16 19:02:59 +02:00
|
|
|
"ShipAddr" => $ship_address
|
|
|
|
] + $customer + ['PrimaryEmailAddr' => ['Address' => $this->getString($data, 'BillEmail.Address') ]];
|
2024-08-16 02:53:11 +02:00
|
|
|
if($this->hasClient($client['CompanyName']))
|
|
|
|
{
|
|
|
|
$client_id = $this->getClient($client['CompanyName'],$this->getString($client, 'PrimaryEmailAddr.Address'));
|
|
|
|
}
|
|
|
|
|
2024-07-16 19:02:59 +02:00
|
|
|
|
|
|
|
return ['client'=> (new ClientTransformer($this->company))->transform($client), 'client_id'=> $client_id ];
|
|
|
|
}
|
|
|
|
|
2024-07-12 17:01:27 +02:00
|
|
|
public function getDueDate($data)
|
|
|
|
{
|
|
|
|
return $this->parseDateOrNull($data, 'DueDate');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDeposit($data)
|
|
|
|
{
|
2024-08-17 17:24:53 +02:00
|
|
|
return (double) $this->getString($data,'Deposit');
|
2024-07-12 17:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getBalance($data)
|
|
|
|
{
|
2024-08-17 17:24:53 +02:00
|
|
|
return (double) $this->getString($data,'Balance');
|
2024-07-12 17:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getCustomerMemo($data)
|
|
|
|
{
|
|
|
|
return $this->getString($data,'CustomerMemo.value');
|
|
|
|
}
|
|
|
|
|
2024-08-17 17:24:53 +02:00
|
|
|
public function getDocNumber($data, $field = null)
|
2024-07-12 17:01:27 +02:00
|
|
|
{
|
2024-08-17 17:24:53 +02:00
|
|
|
return sprintf("%s-%s",
|
|
|
|
$this->getString($data, 'DocNumber'),
|
|
|
|
$this->getString($data, 'Id.value')
|
|
|
|
);
|
2024-07-12 17:01:27 +02:00
|
|
|
}
|
|
|
|
|
2024-08-17 17:24:53 +02:00
|
|
|
public function getLinkedTxn($data)
|
2024-07-12 17:01:27 +02:00
|
|
|
{
|
2024-08-17 17:24:53 +02:00
|
|
|
$payments = $this->getString($data,'LinkedTxn');
|
|
|
|
if(empty($payments)) return [];
|
|
|
|
|
|
|
|
return [[
|
|
|
|
'amount' => $this->getTotalAmt($data),
|
|
|
|
'date' => $this->parseDateOrNull($data, 'TxnDate')
|
|
|
|
]];
|
|
|
|
|
2024-07-12 17:01:27 +02:00
|
|
|
}
|
|
|
|
}
|