2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ninja\Import;
|
2015-12-08 11:10:20 +01:00
|
|
|
|
2017-01-15 20:48:31 +01:00
|
|
|
use Carbon;
|
2015-12-08 11:10:20 +01:00
|
|
|
use League\Fractal\TransformerAbstract;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Utils;
|
2017-07-02 19:41:13 +02:00
|
|
|
use Exception;
|
2015-12-08 11:10:20 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class BaseTransformer.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-12-08 11:10:20 +01:00
|
|
|
class BaseTransformer extends TransformerAbstract
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
2015-12-08 11:10:20 +01:00
|
|
|
protected $maps;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* BaseTransformer constructor.
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @param $maps
|
|
|
|
*/
|
2015-12-08 11:10:20 +01:00
|
|
|
public function __construct($maps)
|
|
|
|
{
|
|
|
|
$this->maps = $maps;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function hasClient($name)
|
2015-12-08 11:10:20 +01:00
|
|
|
{
|
2016-05-31 22:15:55 +02:00
|
|
|
$name = trim(strtolower($name));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-12-08 11:10:20 +01:00
|
|
|
return isset($this->maps[ENTITY_CLIENT][$name]);
|
|
|
|
}
|
|
|
|
|
2018-03-30 13:33:28 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasVendor($name)
|
|
|
|
{
|
|
|
|
$name = trim(strtolower($name));
|
|
|
|
|
|
|
|
return isset($this->maps[ENTITY_VENDOR][$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $key
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function hasProduct($key)
|
2016-05-31 22:15:55 +02:00
|
|
|
{
|
|
|
|
$key = trim(strtolower($key));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-05-31 22:15:55 +02:00
|
|
|
return isset($this->maps[ENTITY_PRODUCT][$key]);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @param $field
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getString($data, $field)
|
2015-12-31 16:09:45 +01:00
|
|
|
{
|
2016-01-01 09:34:32 +01:00
|
|
|
return (isset($data->$field) && $data->$field) ? $data->$field : '';
|
2015-12-31 16:09:45 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @param $field
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getNumber($data, $field)
|
2016-05-31 22:15:55 +02:00
|
|
|
{
|
|
|
|
return (isset($data->$field) && $data->$field) ? $data->$field : 0;
|
|
|
|
}
|
|
|
|
|
2016-12-22 18:17:45 +01:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @param $field
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-12-22 18:17:45 +01:00
|
|
|
* @return float
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getFloat($data, $field)
|
2016-12-22 18:17:45 +01:00
|
|
|
{
|
|
|
|
return (isset($data->$field) && $data->$field) ? Utils::parseFloat($data->$field) : 0;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getClientId($name)
|
2015-12-08 11:10:20 +01:00
|
|
|
{
|
2017-01-08 15:02:52 +01:00
|
|
|
$name = strtolower(trim($name));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-12-08 11:10:20 +01:00
|
|
|
return isset($this->maps[ENTITY_CLIENT][$name]) ? $this->maps[ENTITY_CLIENT][$name] : null;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2017-08-03 21:31:22 +02:00
|
|
|
public function getProduct($data, $key, $field, $default = false)
|
2016-05-31 22:15:55 +02:00
|
|
|
{
|
2017-08-03 21:31:22 +02:00
|
|
|
$productKey = trim(strtolower($data->$key));
|
2016-05-31 22:15:55 +02:00
|
|
|
|
2017-08-03 21:31:22 +02:00
|
|
|
if (! isset($this->maps['product'][$productKey])) {
|
|
|
|
return $default;
|
|
|
|
}
|
2017-07-07 12:25:51 +02:00
|
|
|
|
2017-08-03 21:31:22 +02:00
|
|
|
$product = $this->maps['product'][$productKey];
|
2017-07-07 12:25:51 +02:00
|
|
|
|
2017-08-03 21:31:22 +02:00
|
|
|
return $product->$field ?: $default;
|
2017-07-07 12:25:51 +02:00
|
|
|
}
|
|
|
|
|
2017-08-31 14:55:15 +02:00
|
|
|
/**
|
|
|
|
* @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];
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getCountryId($name)
|
2015-12-08 11:10:20 +01:00
|
|
|
{
|
2017-01-08 15:02:52 +01:00
|
|
|
$name = strtolower(trim($name));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-12-08 11:10:20 +01:00
|
|
|
return isset($this->maps['countries'][$name]) ? $this->maps['countries'][$name] : null;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getCountryIdBy2($name)
|
2015-12-10 11:57:51 +01:00
|
|
|
{
|
2017-01-08 15:02:52 +01:00
|
|
|
$name = strtolower(trim($name));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-12-10 11:57:51 +01:00
|
|
|
return isset($this->maps['countries2'][$name]) ? $this->maps['countries2'][$name] : null;
|
|
|
|
}
|
|
|
|
|
2017-07-31 18:22:29 +02:00
|
|
|
/**
|
|
|
|
* @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] : '';
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getFirstName($name)
|
2015-12-08 11:10:20 +01:00
|
|
|
{
|
|
|
|
$name = Utils::splitName($name);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-12-08 11:10:20 +01:00
|
|
|
return $name[0];
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $date
|
|
|
|
* @param string $format
|
2017-01-30 20:49:42 +01:00
|
|
|
* @param mixed $data
|
|
|
|
* @param mixed $field
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2017-01-15 20:48:31 +01:00
|
|
|
public function getDate($data, $field)
|
2015-12-08 11:10:20 +01:00
|
|
|
{
|
2017-01-15 20:48:31 +01:00
|
|
|
if ($date = data_get($data, $field)) {
|
|
|
|
try {
|
|
|
|
$date = new Carbon($date);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// if we fail to parse return blank
|
2017-07-02 19:41:13 +02:00
|
|
|
$date = false;
|
2017-01-15 20:48:31 +01:00
|
|
|
}
|
2015-12-14 21:07:48 +01:00
|
|
|
}
|
2016-05-31 22:15:55 +02:00
|
|
|
|
2015-12-08 11:10:20 +01:00
|
|
|
return $date ? $date->format('Y-m-d') : null;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getLastName($name)
|
2015-12-08 11:10:20 +01:00
|
|
|
{
|
|
|
|
$name = Utils::splitName($name);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-12-08 11:10:20 +01:00
|
|
|
return $name[1];
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $number
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getInvoiceNumber($number)
|
2015-12-10 14:35:40 +01:00
|
|
|
{
|
2017-08-23 16:05:14 +02:00
|
|
|
return $number ? str_pad(trim($number), 4, '0', STR_PAD_LEFT) : null;
|
2015-12-10 14:35:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $invoiceNumber
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getInvoiceId($invoiceNumber)
|
2015-12-31 15:11:44 +01:00
|
|
|
{
|
|
|
|
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
2016-12-22 16:56:35 +01:00
|
|
|
$invoiceNumber = strtolower($invoiceNumber);
|
2015-12-31 15:11:44 +01:00
|
|
|
return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]) ? $this->maps[ENTITY_INVOICE][$invoiceNumber] : null;
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:04:52 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $invoiceNumber
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function hasInvoice($invoiceNumber)
|
2015-12-08 11:10:20 +01:00
|
|
|
{
|
2015-12-10 14:35:40 +01:00
|
|
|
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
2016-12-22 16:56:35 +01:00
|
|
|
$invoiceNumber = strtolower($invoiceNumber);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2015-12-08 11:10:20 +01:00
|
|
|
return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $invoiceNumber
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2017-01-10 09:50:17 +01:00
|
|
|
public function getInvoiceClientId($invoiceNumber)
|
2015-12-31 15:11:44 +01:00
|
|
|
{
|
|
|
|
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
|
2016-12-22 16:56:35 +01:00
|
|
|
$invoiceNumber = strtolower($invoiceNumber);
|
2015-12-31 15:11:44 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
return isset($this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber]) ? $this->maps[ENTITY_INVOICE.'_'.ENTITY_CLIENT][$invoiceNumber] : null;
|
|
|
|
}
|
2016-05-31 22:15:55 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
2016-10-05 22:56:48 +02:00
|
|
|
public function getVendorId($name)
|
2016-01-06 15:23:58 +01:00
|
|
|
{
|
2017-01-08 15:02:52 +01:00
|
|
|
$name = strtolower(trim($name));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-01-06 15:23:58 +01:00
|
|
|
return isset($this->maps[ENTITY_VENDOR][$name]) ? $this->maps[ENTITY_VENDOR][$name] : null;
|
|
|
|
}
|
2016-05-31 22:15:55 +02:00
|
|
|
|
2016-10-05 22:46:15 +02:00
|
|
|
/**
|
|
|
|
* @param $name
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-10-05 22:46:15 +02:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function getExpenseCategoryId($name)
|
|
|
|
{
|
2017-01-08 15:02:52 +01:00
|
|
|
$name = strtolower(trim($name));
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-10-05 22:46:15 +02:00
|
|
|
return isset($this->maps[ENTITY_EXPENSE_CATEGORY][$name]) ? $this->maps[ENTITY_EXPENSE_CATEGORY][$name] : null;
|
|
|
|
}
|
2016-05-31 22:15:55 +02:00
|
|
|
}
|