1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Import/Transformer/BaseTransformer.php

447 lines
10 KiB
PHP
Raw Normal View History

2022-02-01 07:14:27 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Import\Transformer;
use App\Models\ClientContact;
use App\Models\Country;
use App\Models\PaymentType;
2022-02-01 10:04:03 +01:00
use App\Models\User;
2022-02-01 07:14:27 +01:00
use App\Utils\Number;
use Exception;
use Illuminate\Support\Carbon;
2022-02-01 07:35:16 +01:00
use Illuminate\Support\Facades\Cache;
2022-02-01 07:14:27 +01:00
/**
* Class BaseTransformer.
*/
class BaseTransformer
{
protected $company;
public function __construct($company)
{
$this->company = $company;
}
public function getString($data, $field)
{
2022-02-03 00:14:54 +01:00
return isset($data[$field]) && $data[$field] ? $data[$field] : '';
2022-02-01 07:14:27 +01:00
}
2022-02-03 00:14:54 +01:00
public function getCurrencyByCode($data, $key = 'client.currency_id')
2022-02-01 07:14:27 +01:00
{
2022-02-03 00:14:54 +01:00
$code = array_key_exists($key, $data) ? $data[$key] : false;
2022-02-01 07:14:27 +01:00
2022-02-01 07:35:16 +01:00
$currencies = Cache::get('currencies');
2022-02-03 00:14:54 +01:00
$currency = $currencies
->filter(function ($item) use ($code) {
return $item->code == $code;
})
->first();
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
return $currency
? $currency->id
: $this->company->settings->currency_id;
2022-02-01 07:35:16 +01:00
}
2022-02-03 00:14:54 +01:00
public function getClient($client_name, $client_email)
2022-02-02 05:56:37 +01:00
{
// nlog("searching for {$client_name} with email {$client_email}");
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
$client_id_search = $this->company
->clients()
->where('id_number', $client_name);
if ($client_id_search->count() >= 1) {
2022-02-02 05:56:37 +01:00
// nlog("found via id number => {$client_id_search->first()->id}");
2022-02-03 00:14:54 +01:00
return $client_id_search->first()->id;
}
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
$client_name_search = $this->company
->clients()
->where('name', $client_name);
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
if ($client_name_search->count() >= 1) {
2022-02-02 05:56:37 +01:00
// nlog("found via name {$client_name_search->first()->id}");
2022-02-01 07:14:27 +01:00
return $client_name_search->first()->id;
}
2022-02-03 00:14:54 +01:00
if (!empty($client_email)) {
$contacts = ClientContact::where(
'company_id',
$this->company->id
)->where('email', $client_email);
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
if ($contacts->count() >= 1) {
2022-02-02 05:56:37 +01:00
// nlog("found via contact {$contacts->first()->client_id}");
2022-02-03 00:14:54 +01:00
return $contacts->first()->client_id;
}
}
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
// nlog("did not find client");
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
return null;
}
2022-02-01 07:14:27 +01:00
///////////////////////////////////////////////////////////////////////////////////
/**
* @param $name
*
* @return bool
*/
public function hasClient($name)
{
2022-02-03 00:14:54 +01:00
return $this->company
->clients()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->exists();
2022-02-01 07:14:27 +01:00
}
/**
* @param $name
*
* @return bool
*/
public function hasVendor($name)
{
2022-02-03 00:14:54 +01:00
return $this->company
->vendors()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->exists();
2022-02-01 07:14:27 +01:00
}
2022-02-03 08:53:39 +01:00
/**
* @param $name
*
* @return bool
*/
public function hasProject($name)
{
return $this->company
->projects()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->exists();
}
2022-02-01 07:14:27 +01:00
/**
* @param $key
*
* @return bool
*/
public function hasProduct($key)
{
2022-02-03 00:14:54 +01:00
return $this->company
->products()
->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $key)),
])
->exists();
2022-02-01 07:14:27 +01:00
}
/**
* @param $data
* @param $field
*
* @return float
*/
public function getFloat($data, $field)
{
if (array_key_exists($field, $data)) {
$number = preg_replace('/[^0-9-.]+/', '', $data[$field]);
} else {
$number = 0;
}
return Number::parseFloat($number);
}
/**
* @param $name
*
* @return int|null
*/
public function getClientId($name)
{
2022-02-03 00:14:54 +01:00
$client = $this->company
->clients()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->first();
2022-02-01 07:14:27 +01:00
return $client ? $client->id : null;
}
/**
* @param $name
*
* @return string
*/
public function getProduct($data, $key, $field, $default = false)
{
2022-02-03 00:14:54 +01:00
$product = $this->company
->products()
->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $data->{$key})),
])
->first();
if ($product) {
2022-02-01 07:14:27 +01:00
return $product->{$field} ?: $default;
2022-02-03 00:14:54 +01:00
}
2022-02-01 07:14:27 +01:00
return $default;
}
/**
* @param $email
*
* @return ?Contact
*/
public function getContact($email)
{
2022-02-03 00:14:54 +01:00
$contact = $this->company
->client_contacts()
->whereRaw("LOWER(REPLACE(`email`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $email)),
])
->first();
if (!$contact) {
2022-02-01 07:14:27 +01:00
return null;
2022-02-03 00:14:54 +01:00
}
2022-02-01 07:14:27 +01:00
return $contact;
}
/**
* @param $name
*
* @return int|null
*/
public function getCountryId($name)
{
2022-02-03 00:14:54 +01:00
if (strlen($name) == 2) {
2022-02-01 07:14:27 +01:00
return $this->getCountryIdBy2($name);
2022-02-03 00:14:54 +01:00
}
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
$country = Country::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])->first();
2022-02-01 07:14:27 +01:00
return $country ? $country->id : null;
}
/**
* @param $name
*
* @return int|null
*/
public function getCountryIdBy2($name)
{
2022-02-03 00:14:54 +01:00
return Country::where('iso_3166_2', $name)->exists()
? Country::where('iso_3166_2', $name)->first()->id
: null;
2022-02-01 07:14:27 +01:00
}
/**
* @param $name
*
* @return int
*/
public function getTaxRate($name)
{
$name = strtolower(trim($name));
2022-02-03 00:14:54 +01:00
$tax_rate = $this->company
->tax_rates()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->first();
2022-02-01 07:14:27 +01:00
return $tax_rate ? $tax_rate->rate : 0;
}
/**
* @param $name
*
* @return string
*/
public function getTaxName($name)
{
$name = strtolower(trim($name));
2022-02-03 00:14:54 +01:00
$tax_rate = $this->company
->tax_rates()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->first();
2022-02-01 07:14:27 +01:00
return $tax_rate ? $tax_rate->name : '';
}
/**
* @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 $number
*
* @return ?string
*/
public function getInvoiceNumber($number)
{
2022-02-03 00:14:54 +01:00
return $number ? ltrim(trim($number), '0') : null;
2022-02-01 07:14:27 +01:00
}
/**
* @param $invoice_number
*
* @return int|null
*/
public function getInvoiceId($invoice_number)
{
2022-02-03 00:14:54 +01:00
$invoice = $this->company
->invoices()
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $invoice_number)),
])
->first();
return $invoice ? $invoice->id : null;
2022-02-01 07:14:27 +01:00
}
/**
* @param $invoice_number
*
* @return bool
*/
public function hasInvoice($invoice_number)
{
2022-02-03 00:14:54 +01:00
return $this->company
->invoices()
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $invoice_number)),
])
->exists();
2022-02-01 07:14:27 +01:00
}
/**
* @param $invoice_number
*
* @return int|null
*/
public function getInvoiceClientId($invoice_number)
{
2022-02-03 00:14:54 +01:00
$invoice = $this->company
->invoices()
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $invoice_number)),
])
->first();
2022-02-01 07:14:27 +01:00
return $invoice ? $invoice->client_id : null;
}
/**
* @param $name
*
* @return int|null
*/
public function getVendorId($name)
{
2022-02-03 00:14:54 +01:00
$vendor = $this->company
->vendors()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->first();
2022-02-01 07:14:27 +01:00
return $vendor ? $vendor->id : null;
}
2022-02-03 00:14:54 +01:00
/**
* @param $name
*
* @return int|null
*/
public function getExpenseCategoryId($name)
{
$ec = $this->company
->expense_categories()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->first();
2022-02-01 07:14:27 +01:00
return $ec ? $ec->id : null;
2022-02-03 00:14:54 +01:00
}
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
/**
* @param $name
*
* @return int|null
*/
public function getProjectId($name)
{
$project = $this->company
->projects()
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])
->first();
2022-02-01 07:14:27 +01:00
return $project ? $project->id : null;
2022-02-03 00:14:54 +01:00
}
2022-02-01 07:14:27 +01:00
2022-02-03 00:14:54 +01:00
/**
* @param $name
*
* @return int|null
*/
public function getPaymentTypeId($name)
{
$pt = PaymentType::whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)),
])->first();
2022-02-01 07:14:27 +01:00
return $pt ? $pt->id : null;
2022-02-03 00:14:54 +01:00
}
2022-02-01 07:14:27 +01:00
}