1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-19 17:42:44 +01:00
invoiceninja/app/Import/Transformers/BaseTransformer.php

401 lines
8.4 KiB
PHP
Raw Normal View History

2020-12-16 11:06:20 +01:00
<?php
/**
* client Ninja (https://clientninja.com).
*
* @link https://github.com/clientninja/clientninja source repository
*
* @copyright Copyright (c) 2020. client Ninja LLC (https://clientninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2020-12-16 11:06:20 +01:00
namespace App\Import\Transformers;
2020-12-19 05:11:15 +01:00
use App\Models\ClientContact;
2020-12-20 10:02:10 +01:00
use App\Utils\Number;
2020-12-16 11:06:20 +01:00
use Carbon;
use Exception;
/**
* Class BaseTransformer.
*/
2020-12-16 12:52:40 +01:00
class BaseTransformer
2020-12-16 11:06:20 +01:00
{
/**
* @var
*/
protected $maps;
/**
* BaseTransformer constructor.
*
* @param $maps
*/
public function __construct($maps)
{
$this->maps = $maps;
}
/**
* @param $data
* @param $field
*
* @return string
*/
public function getString($data, $field)
{
return (isset($data[$field]) && $data[$field]) ? $data[$field] : '';
}
2020-12-19 08:28:58 +01:00
public function getInvoiceTypeId($data, $field)
{
return (isset($data[$field]) && $data[$field]) ? $data[$field] : '1';
}
2020-12-16 11:06:20 +01:00
public function getCurrencyByCode($data)
{
2020-12-16 11:13:02 +01:00
$code = array_key_exists('client.currency_id', $data) ? $data['client.currency_id'] : false;
2020-12-16 11:06:20 +01:00
2020-12-16 12:52:40 +01:00
if ($code) {
2020-12-19 04:49:15 +01:00
$currency = $this->maps['currencies']->where('code', $code)->first();
if($currency_id)
return $currency->id;
2020-12-16 12:52:40 +01:00
}
2020-12-16 11:06:20 +01:00
2020-12-16 12:52:40 +01:00
return $this->maps['company']->settings->currency_id;
2020-12-16 11:06:20 +01:00
}
2020-12-20 08:02:58 +01:00
public function getClient($client_name, $client_email)
2020-12-19 04:49:15 +01:00
{
2020-12-20 10:02:10 +01:00
2020-12-19 04:49:15 +01:00
$clients = $this->maps['company']->clients;
2020-12-20 08:02:58 +01:00
$clients = $clients->where('name', $client_name);
2020-12-19 04:49:15 +01:00
if($clients->count() >= 1)
return $clients->first()->id;
2020-12-20 08:02:58 +01:00
2020-12-19 04:49:15 +01:00
$contacts = ClientContact::where('company_id', $this->maps['company']->id)
2020-12-20 08:02:58 +01:00
->where('email', $client_email);
2020-12-19 04:49:15 +01:00
if($contacts->count() >=1)
2020-12-20 08:02:58 +01:00
return $contacts->first()->client_id;
2020-12-19 04:49:15 +01:00
return NULL;
}
///////////////////////////////////////////////////////////////////////////////////
2020-12-16 11:06:20 +01:00
/**
* @param $name
*
* @return bool
*/
public function hasClient($name)
{
$name = trim(strtolower($name));
return isset($this->maps[ENTITY_CLIENT][$name]);
}
/**
* @param $name
*
* @return bool
*/
public function hasVendor($name)
{
$name = trim(strtolower($name));
return isset($this->maps[ENTITY_VENDOR][$name]);
}
/**
* @param $key
*
* @return bool
*/
public function hasProduct($key)
{
$key = trim(strtolower($key));
return isset($this->maps[ENTITY_PRODUCT][$key]);
}
/**
* @param $data
* @param $field
*
* @return int
*/
public function getNumber($data, $field)
{
return (isset($data->$field) && $data->$field) ? $data->$field : 0;
}
/**
* @param $data
* @param $field
*
* @return float
*/
public function getFloat($data, $field)
2020-12-20 10:02:10 +01:00
{
if(array_key_exists($field, $data))
$number = preg_replace('/[^0-9-.]+/', '', $data[$field]);
else
$number = 0;
return Number::parseStringFloat($number);
2020-12-16 11:06:20 +01:00
}
/**
* @param $name
*
* @return null
*/
public function getClientId($name)
{
$name = strtolower(trim($name));
return isset($this->maps[ENTITY_CLIENT][$name]) ? $this->maps[ENTITY_CLIENT][$name] : null;
}
/**
* @param $name
*
* @return null
*/
public function getProduct($data, $key, $field, $default = false)
{
$productKey = trim(strtolower($data->$key));
if (! isset($this->maps['product'][$productKey])) {
return $default;
}
$product = $this->maps['product'][$productKey];
return $product->$field ?: $default;
}
/**
* @param $name
*
* @return null
*/
public function getContact($email)
{
$email = trim(strtolower($email));
if (! isset($this->maps['contact'][$email])) {
return false;
}
return $this->maps['contact'][$email];
}
/**
* @param $name
*
* @return null
*/
public function getCustomer($key)
{
$key = trim($key);
if (! isset($this->maps['customer'][$key])) {
return false;
}
return $this->maps['customer'][$key];
}
/**
* @param $name
*
* @return null
*/
public function getCountryId($name)
{
$name = strtolower(trim($name));
return isset($this->maps['countries'][$name]) ? $this->maps['countries'][$name] : null;
}
/**
* @param $name
*
* @return null
*/
public function getCountryIdBy2($name)
{
$name = strtolower(trim($name));
return isset($this->maps['countries2'][$name]) ? $this->maps['countries2'][$name] : null;
}
/**
* @param $name
*
* @return null
*/
public function getTaxRate($name)
{
$name = strtolower(trim($name));
return isset($this->maps['tax_rates'][$name]) ? $this->maps['tax_rates'][$name] : 0;
}
/**
* @param $name
*
* @return null
*/
public function getTaxName($name)
{
$name = strtolower(trim($name));
return isset($this->maps['tax_names'][$name]) ? $this->maps['tax_names'][$name] : '';
}
/**
* @param $name
*
* @return mixed
*/
public function getFirstName($name)
{
$name = Utils::splitName($name);
return $name[0];
}
/**
* @param $date
* @param string $format
* @param mixed $data
* @param mixed $field
*
* @return null
*/
public function getDate($data, $field)
{
if ($date = data_get($data, $field)) {
try {
$date = new Carbon($date);
} catch (Exception $e) {
// if we fail to parse return blank
$date = false;
}
}
return $date ? $date->format('Y-m-d') : null;
}
/**
* @param $name
*
* @return mixed
*/
public function getLastName($name)
{
$name = Utils::splitName($name);
return $name[1];
}
/**
* @param $number
*
* @return string
*/
public function getInvoiceNumber($number)
{
return $number ? str_pad(trim($number), 4, '0', STR_PAD_LEFT) : null;
}
/**
* @param $invoiceNumber
*
* @return null
*/
public function getInvoiceId($invoiceNumber)
{
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
$invoiceNumber = strtolower($invoiceNumber);
return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]) ? $this->maps[ENTITY_INVOICE][$invoiceNumber] : null;
}
/**
* @param $invoiceNumber
*
* @return null
*/
public function getInvoicePublicId($invoiceNumber)
{
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
$invoiceNumber = strtolower($invoiceNumber);
return isset($this->maps['invoices'][$invoiceNumber]) ? $this->maps['invoices'][$invoiceNumber]->public_id : null;
}
/**
* @param $invoiceNumber
*
* @return bool
*/
public function hasInvoice($invoiceNumber)
{
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
$invoiceNumber = strtolower($invoiceNumber);
return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]);
}
/**
* @param $invoiceNumber
*
* @return null
*/
public function getInvoiceClientId($invoiceNumber)
{
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
$invoiceNumber = strtolower($invoiceNumber);
return isset($this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber]) ? $this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber] : null;
}
/**
* @param $name
*
* @return null
*/
public function getVendorId($name)
{
$name = strtolower(trim($name));
return isset($this->maps[ENTITY_VENDOR][$name]) ? $this->maps[ENTITY_VENDOR][$name] : null;
}
/**
* @param $name
*
* @return null
*/
public function getExpenseCategoryId($name)
{
$name = strtolower(trim($name));
return isset($this->maps[ENTITY_EXPENSE_CATEGORY][$name]) ? $this->maps[ENTITY_EXPENSE_CATEGORY][$name] : null;
}
}