mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 04:07:32 -05:00 
			
		
		
		
	group common methods into trait
This commit is contained in:
		
							parent
							
								
									43b95125f0
								
							
						
					
					
						commit
						73e596d29c
					
				@ -12,18 +12,23 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace App\Import\Transformer\Quickbooks;
 | 
					namespace App\Import\Transformer\Quickbooks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Import\Transformer\Quickbooks\CommonTrait;
 | 
				
			||||||
use App\Import\Transformer\BaseTransformer;
 | 
					use App\Import\Transformer\BaseTransformer;
 | 
				
			||||||
use App\Models\Client as Model;
 | 
					use App\Models\Client as Model;
 | 
				
			||||||
use App\Models\ClientContact;
 | 
					use App\Models\ClientContact;
 | 
				
			||||||
use App\Import\ImportException;
 | 
					use App\Import\ImportException;
 | 
				
			||||||
use Illuminate\Support\Str;
 | 
					use Illuminate\Support\Str;
 | 
				
			||||||
use Illuminate\Support\Arr;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Class ClientTransformer.
 | 
					 * Class ClientTransformer.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class ClientTransformer extends BaseTransformer
 | 
					class ClientTransformer extends BaseTransformer
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    use CommonTrait {
 | 
				
			||||||
 | 
					        transform as preTransform;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private $fillable = [
 | 
					    private $fillable = [
 | 
				
			||||||
        'name'              => 'CompanyName',
 | 
					        'name'              => 'CompanyName',
 | 
				
			||||||
        'phone'             => 'PrimaryPhone.FreeFormNumber',
 | 
					        'phone'             => 'PrimaryPhone.FreeFormNumber',
 | 
				
			||||||
@ -40,6 +45,14 @@ class ClientTransformer extends BaseTransformer
 | 
				
			|||||||
        'public_notes'      => 'Notes'
 | 
					        'public_notes'      => 'Notes'
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function __construct($company)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        parent::__construct($company);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->model = new Model;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Transforms a Customer array into a ClientContact model.
 | 
					     * Transforms a Customer array into a ClientContact model.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
@ -54,18 +67,10 @@ class ClientTransformer extends BaseTransformer
 | 
				
			|||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach($this->fillable as $key => $field) {
 | 
					        $transformed_data = $this->preTransform($data);
 | 
				
			||||||
            $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['contacts'][0] = $this->getContacts($data)->toArray()+['company_id' => $this->company->id ];
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        $transformed_data = (new Model)->fillable(array_keys($this->fillable))->fill($transformed_data);
 | 
					        return $transformed_data;
 | 
				
			||||||
        $transformed_data->contacts[0] = $this->getContacts($data)->toArray()+['company_id' => $this->company->id ];
 | 
					 | 
				
			||||||
        return $transformed_data->toArray() + ['company_id' => $this->company->id ] ;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function getString($data, $field)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        return Arr::get($data, $field);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function getContacts($data) {
 | 
					    protected function getContacts($data) {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										36
									
								
								app/Import/Transformer/Quickbooks/CommonTrait.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								app/Import/Transformer/Quickbooks/CommonTrait.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace App\Import\Transformer\Quickbooks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Illuminate\Support\Arr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					trait CommonTrait
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    protected $model;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getString($data,$field) {
 | 
				
			||||||
 | 
					        return Arr::get($data,$field);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getCreateTime($data, $field = null)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return $this->parseDateOrNull($data, 'MetaData.CreateTime');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getLastUpdatedTime($data, $field = null)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return $this->parseDateOrNull($data,'MetaData.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 $this->model->fillable(array_keys($this->fillable))->fill($transformed)->toArray() + ['company_id' => $this->company->id ] ;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -17,6 +17,7 @@ use App\Import\ImportException;
 | 
				
			|||||||
use App\DataMapper\InvoiceItem;
 | 
					use App\DataMapper\InvoiceItem;
 | 
				
			||||||
use App\Models\Invoice as Model;
 | 
					use App\Models\Invoice as Model;
 | 
				
			||||||
use App\Import\Transformer\BaseTransformer;
 | 
					use App\Import\Transformer\BaseTransformer;
 | 
				
			||||||
 | 
					use App\Import\Transformer\Quickbooks\CommonTrait;
 | 
				
			||||||
use App\Import\Transformer\Quickbooks\ClientTransformer;
 | 
					use App\Import\Transformer\Quickbooks\ClientTransformer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@ -24,7 +25,9 @@ use App\Import\Transformer\Quickbooks\ClientTransformer;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
class InvoiceTransformer extends BaseTransformer
 | 
					class InvoiceTransformer extends BaseTransformer
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    
 | 
					    use CommonTrait {
 | 
				
			||||||
 | 
					        transform as preTransform;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private $fillable = [
 | 
					    private $fillable = [
 | 
				
			||||||
        'amount' => "TotalAmt",
 | 
					        'amount' => "TotalAmt",
 | 
				
			||||||
@ -32,21 +35,30 @@ class InvoiceTransformer extends BaseTransformer
 | 
				
			|||||||
        'due_date' => "DueDate",
 | 
					        'due_date' => "DueDate",
 | 
				
			||||||
        'partial' => "Deposit",
 | 
					        'partial' => "Deposit",
 | 
				
			||||||
        'balance' => "Balance",
 | 
					        'balance' => "Balance",
 | 
				
			||||||
        'comments' => "CustomerMemo",
 | 
					        'private_notes' => "CustomerMemo",
 | 
				
			||||||
 | 
					        'public_notes' => "CustomerMemo",
 | 
				
			||||||
        'number' => "DocNumber",
 | 
					        'number' => "DocNumber",
 | 
				
			||||||
        'created_at' => "CreateTime",
 | 
					        'created_at' => "CreateTime",
 | 
				
			||||||
        'updated_at' => "LastUpdatedTime"
 | 
					        'updated_at' => "LastUpdatedTime",
 | 
				
			||||||
 | 
					        'payments' => 'LinkedTxn',
 | 
				
			||||||
 | 
					        'status_id' => 'InvoiceStatus',
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function __construct($company)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        parent::__construct($company);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->model = new Model;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function getInvoiceStatus($data)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        return Invoice::STATUS_SENT;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function transform($data)
 | 
					    public function transform($data)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $transformed = [];
 | 
					       return $this->preTransform($data) + $this->getInvoiceClient($data);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        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() + $this->getInvoiceClient($data);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getTotalAmt($data)
 | 
					    public function getTotalAmt($data)
 | 
				
			||||||
@ -61,8 +73,11 @@ class InvoiceTransformer extends BaseTransformer
 | 
				
			|||||||
                'description' => $this->getString($item,'Description'),
 | 
					                'description' => $this->getString($item,'Description'),
 | 
				
			||||||
                'product_key' => $this->getString($item,'Description'),
 | 
					                'product_key' => $this->getString($item,'Description'),
 | 
				
			||||||
                'quantity' => (int) $this->getString($item,'SalesItemLineDetail.Qty'),
 | 
					                'quantity' => (int) $this->getString($item,'SalesItemLineDetail.Qty'),
 | 
				
			||||||
                'unit_price' =>(float) $this->getString($item,'SalesItemLineDetail.UnitPrice'),
 | 
					                'unit_price' =>(double) $this->getString($item,'SalesItemLineDetail.UnitPrice'),
 | 
				
			||||||
                'amount' => (float) $this->getString($item,'Amount')
 | 
					                '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'),
 | 
				
			||||||
            ];
 | 
					            ];
 | 
				
			||||||
        }, array_filter($this->getString($data,'Line'), function ($item) {
 | 
					        }, array_filter($this->getString($data,'Line'), function ($item) {
 | 
				
			||||||
            return $this->getString($item,'DetailType') !== 'SubTotalLineDetail';
 | 
					            return $this->getString($item,'DetailType') !== 'SubTotalLineDetail';
 | 
				
			||||||
@ -139,10 +154,6 @@ class InvoiceTransformer extends BaseTransformer
 | 
				
			|||||||
        return ['client'=> (new ClientTransformer($this->company))->transform($client), 'client_id'=> $client_id ];
 | 
					        return ['client'=> (new ClientTransformer($this->company))->transform($client), 'client_id'=> $client_id ];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getString($data,$field) {
 | 
					 | 
				
			||||||
        return Arr::get($data,$field);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function getDueDate($data)
 | 
					    public function getDueDate($data)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return $this->parseDateOrNull($data, 'DueDate');
 | 
					        return $this->parseDateOrNull($data, 'DueDate');
 | 
				
			||||||
@ -150,12 +161,12 @@ class InvoiceTransformer extends BaseTransformer
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function getDeposit($data)
 | 
					    public function getDeposit($data)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return (float) $this->getString($data,'Deposit');
 | 
					        return (double) $this->getString($data,'Deposit');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getBalance($data)
 | 
					    public function getBalance($data)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return (float) $this->getString($data,'Balance');
 | 
					        return (double) $this->getString($data,'Balance');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getCustomerMemo($data)
 | 
					    public function getCustomerMemo($data)
 | 
				
			||||||
@ -163,13 +174,23 @@ class InvoiceTransformer extends BaseTransformer
 | 
				
			|||||||
        return $this->getString($data,'CustomerMemo.value');
 | 
					        return $this->getString($data,'CustomerMemo.value');
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getCreateTime($data)
 | 
					    public function getDocNumber($data, $field = null) 
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return $this->parseDateOrNull($data['MetaData'], 'CreateTime');
 | 
					        return sprintf("%s-%s", 
 | 
				
			||||||
 | 
					                    $this->getString($data, 'DocNumber'), 
 | 
				
			||||||
 | 
					                    $this->getString($data, 'Id.value')
 | 
				
			||||||
 | 
					                );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getLastUpdatedTime($data)
 | 
					    public function getLinkedTxn($data)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return $this->parseDateOrNull($data['MetaData'],'LastUpdatedTime');
 | 
					        $payments = $this->getString($data,'LinkedTxn');
 | 
				
			||||||
 | 
					        if(empty($payments)) return [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					       return [[
 | 
				
			||||||
 | 
					            'amount' => $this->getTotalAmt($data),
 | 
				
			||||||
 | 
					            'date' => $this->parseDateOrNull($data, 'TxnDate')
 | 
				
			||||||
 | 
					        ]];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -12,11 +12,10 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace App\Import\Transformer\Quickbooks;
 | 
					namespace App\Import\Transformer\Quickbooks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Import\Transformer\Quickbooks\CommonTrait;
 | 
				
			||||||
use App\Import\Transformer\BaseTransformer;
 | 
					use App\Import\Transformer\BaseTransformer;
 | 
				
			||||||
use App\Models\Product as Model;
 | 
					use App\Models\Product as Model;
 | 
				
			||||||
use App\Import\ImportException;
 | 
					use App\Import\ImportException;
 | 
				
			||||||
use Illuminate\Support\Str;
 | 
					 | 
				
			||||||
use Illuminate\Support\Arr;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Class ProductTransformer.
 | 
					 * Class ProductTransformer.
 | 
				
			||||||
@ -24,6 +23,8 @@ use Illuminate\Support\Arr;
 | 
				
			|||||||
class ProductTransformer extends BaseTransformer
 | 
					class ProductTransformer extends BaseTransformer
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    use CommonTrait;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    protected $fillable = [
 | 
					    protected $fillable = [
 | 
				
			||||||
        'product_key' => 'Name',
 | 
					        'product_key' => 'Name',
 | 
				
			||||||
        'notes' => 'Description',
 | 
					        'notes' => 'Description',
 | 
				
			||||||
@ -34,33 +35,13 @@ class ProductTransformer extends BaseTransformer
 | 
				
			|||||||
        'created_at' => 'CreateTime',
 | 
					        'created_at' => 'CreateTime',
 | 
				
			||||||
        'updated_at' => 'LastUpdatedTime',
 | 
					        'updated_at' => 'LastUpdatedTime',
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
    /**
 | 
					
 | 
				
			||||||
     * Transforms the JSON data into a ProductModel object.
 | 
					
 | 
				
			||||||
     *
 | 
					    public function __construct($company)
 | 
				
			||||||
     * @param array $data
 | 
					 | 
				
			||||||
     * @return ProductModel
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     * Transforms a Customer array into a Product model.
 | 
					 | 
				
			||||||
     *
 | 
					 | 
				
			||||||
     * @param array $data
 | 
					 | 
				
			||||||
     * @return array|bool
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function transform($data)
 | 
					 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $transformed_data = [];
 | 
					        parent::__construct($company);
 | 
				
			||||||
        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);
 | 
					        $this->model = new Model;
 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        return $transformed_data->toArray() + ['company_id' => $this->company->id ] ;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function getString($data, $field)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        return Arr::get($data, $field);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getQtyOnHand($data, $field = null) {
 | 
					    public function getQtyOnHand($data, $field = null) {
 | 
				
			||||||
@ -68,21 +49,11 @@ class ProductTransformer extends BaseTransformer
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getPurchaseCost($data, $field = null) {
 | 
					    public function getPurchaseCost($data, $field = null) {
 | 
				
			||||||
        return (float) $this->getString($data, $field);
 | 
					        return (double) $this->getString($data, $field);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function getUnitPrice($data, $field = null) {
 | 
					    public function getUnitPrice($data, $field = null) {
 | 
				
			||||||
        return (float) $this->getString($data, $field);
 | 
					        return (float) $this->getString($data, $field);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function getCreateTime($data, $field = null)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        return $this->parseDateOrNull($data['MetaData'], 'CreateTime');
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public function getLastUpdatedTime($data, $field = null)
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        return $this->parseDateOrNull($data['MetaData'],'LastUpdatedTime');
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user