mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
add client and invoice transformers
This commit is contained in:
parent
fe9ae17943
commit
7a750f930a
91
app/Import/Transformer/Quickbooks/ClientTransformer.php
Normal file
91
app/Import/Transformer/Quickbooks/ClientTransformer.php
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://clientninja.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 App\Import\Transformer\BaseTransformer;
|
||||||
|
use App\Models\Client as Model;
|
||||||
|
use App\Models\ClientContact;
|
||||||
|
use App\Import\ImportException;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ClientTransformer.
|
||||||
|
*/
|
||||||
|
class ClientTransformer extends BaseTransformer
|
||||||
|
{
|
||||||
|
private $fillable = [
|
||||||
|
'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'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms a Customer array into a ClientContact model.
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
* @return array|bool
|
||||||
|
*/
|
||||||
|
public function transform($data)
|
||||||
|
{
|
||||||
|
$transformed_data = [];
|
||||||
|
// Assuming 'customer_name' is equivalent to 'CompanyName'
|
||||||
|
if (isset($data['CompanyName']) && $this->hasClient($data['CompanyName'])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($this->fillable as $key => $field) {
|
||||||
|
$transformed_data[$key] = method_exists($this, $method = sprintf("get%s", str_replace(".","",$field)) )? call_user_func([$this, $method],$data,$field) : $this->getString($data, $field);
|
||||||
|
}
|
||||||
|
|
||||||
|
$transformed_data = (new Model)->fillable(array_keys($this->fillable))->fill($transformed_data)->toArray() + $this->getContacts($data, $field);
|
||||||
|
|
||||||
|
return $transformed_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getString($data, $field)
|
||||||
|
{
|
||||||
|
return Arr::get($data, $field);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getContacts($data, $field = null) {
|
||||||
|
return [ 'contacts' => [
|
||||||
|
(new ClientContact())->fill([
|
||||||
|
'first_name' => $this->getString($data, 'GivenName'),
|
||||||
|
'last_name' => $this->getString($data, 'FamilyName'),
|
||||||
|
'phone' => $this->getString($data, 'PrimaryPhone.FreeFormNumber'),
|
||||||
|
'email' => $this->getString($data, 'PrimaryEmailAddr.Address'),
|
||||||
|
]) ]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getShipAddrCountry($data,$field) {
|
||||||
|
return is_null(($c = $this->getString($data,$field))) ? null : $this->getCountryId($c);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBillAddrCountry($data,$field) {
|
||||||
|
return is_null(($c = $this->getString($data,$field))) ? null : $this->getCountryId($c);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
103
app/Import/Transformer/Quickbooks/InvoiceTransformer.php
Normal file
103
app/Import/Transformer/Quickbooks/InvoiceTransformer.php
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class InvoiceTransformer.
|
||||||
|
*/
|
||||||
|
class InvoiceTransformer extends BaseTransformer
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
private $fillable = [
|
||||||
|
'amount' => "TotalAmt",
|
||||||
|
'line_items' => "Line",
|
||||||
|
'due_date' => "DueDate",
|
||||||
|
'partial' => "Deposit",
|
||||||
|
'balance' => "Balance",
|
||||||
|
'comments' => "CustomerMemo",
|
||||||
|
'number' => "DocNumber",
|
||||||
|
'created_at' => "CreateTime",
|
||||||
|
'updated_at' => "LastUpdatedTime"
|
||||||
|
];
|
||||||
|
|
||||||
|
public function transform($data)
|
||||||
|
{
|
||||||
|
$transformed = [];
|
||||||
|
|
||||||
|
foreach ($this->fillable as $key => $field) {
|
||||||
|
$transformed[$key] = is_null((($v = $this->getString($data, $field))))? null : (method_exists($this, ($method = "get{$field}")) ? call_user_func([$this, $method], $data, $field ) : $this->getString($data,$field));
|
||||||
|
}
|
||||||
|
|
||||||
|
return (new Model)->fillable(array_keys($this->fillable))->fill($transformed)->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
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'),
|
||||||
|
'quantity' => $this->getString($item,'SalesItemLineDetail.Qty'),
|
||||||
|
'unit_price' =>$this->getString($item,'SalesItemLineDetail.UnitPrice'),
|
||||||
|
'amount' => $this->getString($item,'Amount')
|
||||||
|
];
|
||||||
|
}, array_filter($this->getString($data,'Line'), function ($item) {
|
||||||
|
return $this->getString($item,'DetailType') === 'SalesItemLineDetail';
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getString($data,$field) {
|
||||||
|
return Arr::get($data,$field);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDueDate($data)
|
||||||
|
{
|
||||||
|
return $this->parseDateOrNull($data, 'DueDate');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDeposit($data)
|
||||||
|
{
|
||||||
|
return (float) $this->getString($data,'Deposit');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBalance($data)
|
||||||
|
{
|
||||||
|
return (float) $this->getString($data,'Balance');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCustomerMemo($data)
|
||||||
|
{
|
||||||
|
return $this->getString($data,'CustomerMemo.value');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCreateTime($data)
|
||||||
|
{
|
||||||
|
return $this->parseDateOrNull($this->getString($data,'MetaData.CreateTime'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLastUpdatedTime($data)
|
||||||
|
{
|
||||||
|
return $this->parseDateOrNull($this->getString($data,'MetaData.LastUpdatedTime'));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user