1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 01:11:34 +02:00
invoiceninja/app/Ninja/Import/BaseTransformer.php

203 lines
4.5 KiB
PHP
Raw Normal View History

2015-12-08 11:10:20 +01:00
<?php namespace App\Ninja\Import;
use Utils;
use DateTime;
use League\Fractal\TransformerAbstract;
/**
* Class BaseTransformer
*/
2015-12-08 11:10:20 +01:00
class BaseTransformer extends TransformerAbstract
{
/**
* @var
*/
2015-12-08 11:10:20 +01:00
protected $maps;
/**
* BaseTransformer constructor.
* @param $maps
*/
2015-12-08 11:10:20 +01:00
public function __construct($maps)
{
$this->maps = $maps;
}
/**
* @param $name
* @return bool
*/
2015-12-08 11:10:20 +01:00
protected function hasClient($name)
{
2016-05-31 22:15:55 +02:00
$name = trim(strtolower($name));
2015-12-08 11:10:20 +01:00
return isset($this->maps[ENTITY_CLIENT][$name]);
}
/**
* @param $key
* @return bool
*/
2016-05-31 22:15:55 +02:00
protected function hasProduct($key)
{
$key = trim(strtolower($key));
return isset($this->maps[ENTITY_PRODUCT][$key]);
}
/**
* @param $data
* @param $field
* @return string
*/
2015-12-31 16:09:45 +01:00
protected function getString($data, $field)
{
return (isset($data->$field) && $data->$field) ? $data->$field : '';
2015-12-31 16:09:45 +01:00
}
/**
* @param $data
* @param $field
* @return int
*/
2016-05-31 22:15:55 +02:00
protected function getNumber($data, $field)
{
return (isset($data->$field) && $data->$field) ? $data->$field : 0;
}
/**
* @param $name
* @return null
*/
2015-12-08 11:10:20 +01:00
protected function getClientId($name)
{
$name = strtolower($name);
return isset($this->maps[ENTITY_CLIENT][$name]) ? $this->maps[ENTITY_CLIENT][$name] : null;
}
/**
* @param $name
* @return null
*/
2016-05-31 22:15:55 +02:00
protected function getProductId($name)
{
$name = strtolower($name);
return isset($this->maps[ENTITY_PRODUCT][$name]) ? $this->maps[ENTITY_PRODUCT][$name] : null;
}
/**
* @param $name
* @return null
*/
2015-12-08 11:10:20 +01:00
protected function getCountryId($name)
{
$name = strtolower($name);
return isset($this->maps['countries'][$name]) ? $this->maps['countries'][$name] : null;
}
/**
* @param $name
* @return null
*/
2015-12-10 11:57:51 +01:00
protected function getCountryIdBy2($name)
{
$name = strtolower($name);
return isset($this->maps['countries2'][$name]) ? $this->maps['countries2'][$name] : null;
}
/**
* @param $name
* @return mixed
*/
2015-12-08 11:10:20 +01:00
protected function getFirstName($name)
{
$name = Utils::splitName($name);
return $name[0];
}
/**
* @param $date
* @param string $format
* @return null
*/
2015-12-08 11:10:20 +01:00
protected function getDate($date, $format = 'Y-m-d')
{
2015-12-14 21:07:48 +01:00
if ( ! $date instanceof DateTime) {
$date = DateTime::createFromFormat($format, $date);
}
2016-05-31 22:15:55 +02:00
2015-12-08 11:10:20 +01:00
return $date ? $date->format('Y-m-d') : null;
}
/**
* @param $name
* @return mixed
*/
2015-12-08 11:10:20 +01:00
protected function getLastName($name)
{
$name = Utils::splitName($name);
return $name[1];
}
/**
* @param $number
* @return string
*/
2015-12-10 14:35:40 +01:00
protected function getInvoiceNumber($number)
{
$number = strtolower($number);
return str_pad($number, 4, '0', STR_PAD_LEFT);
}
/**
* @param $invoiceNumber
* @return null
*/
protected function getInvoiceId($invoiceNumber)
{
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]) ? $this->maps[ENTITY_INVOICE][$invoiceNumber] : null;
}
/**
* @param $invoiceNumber
* @return bool
*/
2015-12-08 11:10:20 +01:00
protected function hasInvoice($invoiceNumber)
{
2015-12-10 14:35:40 +01:00
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
2015-12-08 11:10:20 +01:00
return isset($this->maps[ENTITY_INVOICE][$invoiceNumber]);
}
/**
* @param $invoiceNumber
* @return null
*/
protected function getInvoiceClientId($invoiceNumber)
{
$invoiceNumber = $this->getInvoiceNumber($invoiceNumber);
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
/**
* @param $name
* @return null
*/
2016-10-05 22:56:48 +02:00
public function getVendorId($name)
2016-01-06 15:23:58 +01:00
{
$name = strtolower($name);
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
* @return null
*/
public function getExpenseCategoryId($name)
{
$name = strtolower($name);
return isset($this->maps[ENTITY_EXPENSE_CATEGORY][$name]) ? $this->maps[ENTITY_EXPENSE_CATEGORY][$name] : null;
}
2016-05-31 22:15:55 +02:00
}