1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Models/Presenters/ClientPresenter.php

205 lines
5.4 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Models\Presenters;
use App\Models\Country;
2019-01-27 00:22:57 +01:00
/**
* Class ClientPresenter.
2019-01-27 00:22:57 +01:00
*/
class ClientPresenter extends EntityPresenter
{
2019-01-27 00:22:57 +01:00
/**
* @return string
*/
public function name()
{
2023-10-18 01:14:14 +02:00
if (strlen($this->entity->name) > 1) {
2020-06-08 23:55:31 +02:00
return $this->entity->name;
}
2020-06-08 23:55:31 +02:00
$contact = $this->entity->contacts->whereNotNull('email')->first();
2019-10-02 12:22:10 +02:00
$contact_name = 'No Contact Set';
2023-03-19 10:30:28 +01:00
if ($contact && ((is_string($contact->first_name) && strlen($contact->first_name) >= 1) || (is_string($contact->last_name) && strlen($contact->last_name) >= 1))) {
$contact_name = $contact->first_name.' '.$contact->last_name;
} elseif ($contact && (strlen($contact->email))) {
2020-06-08 23:55:31 +02:00
$contact_name = $contact->email;
}
2019-10-02 12:22:10 +02:00
2020-06-08 23:55:31 +02:00
return $contact_name;
}
2019-08-21 06:29:07 +02:00
2021-10-03 05:50:01 +02:00
public function first_name()
{
2023-03-17 23:56:03 +01:00
return $this->entity->primary_contact()->first()?->first_name ?: ($this->entity->contacts()->first()->first_name ?: '');
2021-10-03 05:50:01 +02:00
}
public function last_name()
{
2023-03-17 23:56:03 +01:00
return $this->entity->primary_contact()->first()?->last_name ?: ($this->entity->contacts()->first()->last_name ?: '');
2021-10-03 05:50:01 +02:00
}
public function primary_contact_name()
{
2023-03-17 23:56:03 +01:00
return $this->entity?->primary_contact()?->first() ? $this->entity->primary_contact()->first()->first_name.' '.$this->entity->primary_contact()->first()->last_name : 'No primary contact set';
}
public function email()
{
2021-08-31 14:19:30 +02:00
$primary_contact = $this->entity->primary_contact->first();
if ($primary_contact && strlen($primary_contact->email) > 1) {
2021-08-31 14:19:30 +02:00
return $primary_contact->email;
}
2021-08-31 14:19:30 +02:00
$contact = $this->entity->contacts->whereNotNull('email')->first();
return $contact ? $contact->email : 'No Email Set';
}
2019-08-21 06:29:07 +02:00
public function address()
{
$str = '';
$client = $this->entity;
if ($address1 = $client->address1) {
$str .= e($address1).'<br/>';
2019-08-21 06:29:07 +02:00
}
if ($address2 = $client->address2) {
$str .= e($address2).'<br/>';
2019-08-21 06:29:07 +02:00
}
if ($cityState = $this->getCityState()) {
$str .= e($cityState).'<br/>';
2019-08-21 06:29:07 +02:00
}
if ($country = $client->country) {
$str .= e($country->name).'<br/>';
2019-08-21 06:29:07 +02:00
}
return $str;
}
2019-08-29 06:58:04 +02:00
public function shipping_address()
{
$str = '';
$client = $this->entity;
if ($address1 = $client->shipping_address1) {
$str .= e($address1).'<br/>';
2019-08-29 06:58:04 +02:00
}
if ($address2 = $client->shipping_address2) {
$str .= e($address2).'<br/>';
2019-08-29 06:58:04 +02:00
}
2023-06-02 07:53:33 +02:00
if ($cityState = $this->getShippingCityState()) {
$str .= e($cityState).'<br/>';
2019-08-29 06:58:04 +02:00
}
2023-11-22 05:11:08 +01:00
if ($country = $client->shipping_country ?? $client->country) {
$str .= e($country->name).'<br/>';
2019-08-29 06:58:04 +02:00
}
return $str;
}
2023-11-22 04:06:51 +01:00
public function shipping_country_code(): string
{
return $this->entity->shipping_country ? $this->entity->shipping_country->iso_3166_2 : $this->entity->country->iso_3166_2;
}
public function phone()
{
return $this->entity->phone ?: '';
}
2019-08-21 06:29:07 +02:00
public function website()
{
return $this->entity->website ?: '';
}
/**
* Calculated company data fields
* using settings.
*/
public function company_name()
{
$settings = $this->entity->getMergedSettings();
return $settings->name ?: ctrans('texts.untitled_account');
}
public function company_address()
{
$settings = $this->entity->getMergedSettings();
$str = '';
if ($settings->address1) {
$str .= e($settings->address1).'<br/>';
}
if ($settings->address2) {
$str .= e($settings->address2).'<br/>';
}
if ($cityState = $this->getCityState()) {
$str .= e($cityState).'<br/>';
}
if ($country = Country::find($settings->country_id)) {
$str .= e($country->name).'<br/>';
}
return $str;
}
// public function getCityState()
// {
// $settings = $this->entity->getMergedSettings();
// $country = false;
// if ($settings->country_id) {
// $country = Country::find($settings->country_id);
// }
// $swap = $country && $country->swap_postal_code;
// $city = e($settings->city ?: '');
// $state = e($settings->state ?: '');
// $postalCode = e($settings->postal_code ?: '');
// if ($city || $state || $postalCode) {
// return $this->cityStateZip($city, $state, $postalCode, $swap);
// } else {
// return false;
// }
// }
public function getCityState()
{
$client = $this->entity;
$country = $client->country ?: false;
$swap = $country && $country->swap_postal_code;
$city = e($client->city ?: '');
$state = e($client->state ?: '');
$postalCode = e($client->postal_code ?: '');
if ($city || $state || $postalCode) {
return $this->cityStateZip($city, $state, $postalCode, $swap);
} else {
return false;
}
}
2023-06-02 07:53:33 +02:00
}