invoiceninja/app/Ninja/Presenters/AccountPresenter.php

273 lines
7.5 KiB
PHP
Raw Normal View History

2017-01-30 21:40:43 +02:00
<?php
namespace App\Ninja\Presenters;
2015-11-06 12:59:53 +02:00
use Carbon;
2017-01-12 15:12:02 +02:00
use Domain;
2017-02-10 09:43:46 +02:00
use App\Models\TaxRate;
2017-12-12 10:09:48 +02:00
use App\Models\Account;
2015-11-06 12:59:53 +02:00
use Laracasts\Presenter\Presenter;
2017-01-30 21:40:43 +02:00
use stdClass;
use Utils;
2015-11-06 12:59:53 +02:00
/**
2017-01-30 21:40:43 +02:00
* Class AccountPresenter.
*/
class AccountPresenter extends Presenter
{
/**
* @return mixed
*/
2015-11-06 12:59:53 +02:00
public function name()
{
return $this->entity->name ?: trans('texts.untitled_account');
}
2017-08-03 23:25:27 +03:00
/**
* @return string
*/
public function address()
{
$account = $this->entity;
$str = $account->address1 ?: '';
if ($account->address2 && $str) {
$str .= ', ';
}
$str .= $account->address2;
if ($account->getCityState() && $str) {
$str .= ' - ';
}
return $str . $account->getCityState();
}
/**
* @return string
*/
2015-12-14 23:05:17 +02:00
public function website()
{
return Utils::addHttp($this->entity->website);
}
2016-01-21 21:15:30 +02:00
/**
* @return string
*/
public function taskRate()
{
2017-12-25 12:24:39 +02:00
if (floatval($this->entity->task_rate)) {
return Utils::roundSignificant($this->entity->task_rate);
} else {
return '';
}
}
/**
* @return mixed
*/
2016-01-21 21:15:30 +02:00
public function currencyCode()
{
$currencyId = $this->entity->getCurrencyId();
$currency = Utils::getFromCache($currencyId, 'currencies');
2017-01-30 21:40:43 +02:00
2016-01-21 21:15:30 +02:00
return $currency->code;
}
2016-12-12 20:42:51 +02:00
public function clientPortalLink($subdomain = false)
2017-01-12 15:12:02 +02:00
{
$account = $this->entity;
$url = Domain::getLinkFromId($account->domain_id);
if ($subdomain && $account->subdomain) {
$url = Utils::replaceSubdomain($url, $account->subdomain);
}
return $url;
2017-01-12 15:12:02 +02:00
}
2016-12-12 20:42:51 +02:00
public function industry()
{
return $this->entity->industry ? $this->entity->industry->name : '';
}
public function size()
{
return $this->entity->size ? $this->entity->size->name : '';
}
2017-01-16 14:06:22 +02:00
public function paymentTerms()
{
$terms = $this->entity->payment_terms;
if ($terms == 0) {
return '';
} elseif ($terms == -1) {
$terms = 0;
}
return trans('texts.payment_terms_net') . ' ' . $terms;
}
2017-01-16 13:59:46 +02:00
public function dueDatePlaceholder()
{
if ($this->entity->payment_terms == 0) {
return ' ';
}
$date = $this->entity->defaultDueDate();
return $date ? Utils::fromSqlDate($date) : ' ';
}
2016-12-12 20:42:51 +02:00
private function createRBit($type, $source, $properties)
{
$data = new stdClass();
$data->receive_time = time();
$data->type = $type;
$data->source = $source;
$data->properties = new stdClass();
foreach ($properties as $key => $val) {
$data->properties->$key = $val;
}
return $data;
}
public function rBits()
{
$account = $this->entity;
$user = $account->users()->first();
$data = [];
$data[] = $this->createRBit('business_name', 'user', ['business_name' => $account->name]);
$data[] = $this->createRBit('industry_code', 'user', ['industry_detail' => $account->present()->industry]);
$data[] = $this->createRBit('comment', 'partner_database', ['comment_text' => 'Logo image not present']);
$data[] = $this->createRBit('business_description', 'user', ['business_description' => $account->present()->size]);
$data[] = $this->createRBit('person', 'user', ['name' => $user->getFullName()]);
$data[] = $this->createRBit('email', 'user', ['email' => $user->email]);
$data[] = $this->createRBit('phone', 'user', ['phone' => $user->phone]);
$data[] = $this->createRBit('website_uri', 'user', ['uri' => $account->website]);
$data[] = $this->createRBit('external_account', 'partner_database', ['is_partner_account' => 'yes', 'account_type' => 'Invoice Ninja', 'create_time' => time()]);
return $data;
}
public function dateRangeOptions()
{
$yearStart = Carbon::parse($this->entity->financialYearStart() ?: date('Y') . '-01-01');
$month = $yearStart->month - 1;
$year = $yearStart->year;
$lastYear = $year - 1;
$str = '{
"' . trans('texts.last_7_days') . '": [moment().subtract(6, "days"), moment()],
"' . trans('texts.last_30_days') . '": [moment().subtract(29, "days"), moment()],
"' . trans('texts.this_month') . '": [moment().startOf("month"), moment().endOf("month")],
"' . trans('texts.last_month') . '": [moment().subtract(1, "month").startOf("month"), moment().subtract(1, "month").endOf("month")],
"' . trans('texts.this_year') . '": [moment().date(1).month(' . $month . ').year(' . $year . '), moment()],
"' . trans('texts.last_year') . '": [moment().date(1).month(' . $month . ').year(' . $lastYear . '), moment().date(1).month(' . $month . ').year(' . $year . ').subtract(1, "day")],
}';
return $str;
}
2017-02-10 09:43:46 +02:00
public function taxRateOptions()
{
$rates = TaxRate::scope()->orderBy('name')->get();
$options = [];
foreach ($rates as $rate) {
$name = $rate->name . ' ' . ($rate->rate + 0) . '%';
if ($rate->is_inclusive) {
$name .= ' - ' . trans('texts.inclusive');
}
2017-08-04 16:39:11 +03:00
$options[($rate->is_inclusive ? '1 ' : '0 ') . $rate->rate . ' ' . $rate->name] = e($name);
2017-02-10 09:43:46 +02:00
}
return $options;
}
2017-03-30 12:40:04 +03:00
public function customTextFields()
{
$fields = [
2018-04-04 16:35:28 +03:00
'client1' => 'custom_client1',
'client1' => 'custom_client2',
'contact1' => 'custom_contact1',
'contact2' => 'custom_contact2',
'invoice_text1' => 'custom_invoice1',
'invoice_text2' => 'custom_invoice2',
'product1' => 'custom_product1',
'product2' => 'custom_product2',
2017-03-30 12:40:04 +03:00
];
$data = [];
foreach ($fields as $key => $val) {
2018-04-04 16:35:28 +03:00
if ($label = $this->customLabel($key)) {
$data[Utils::getCustomLabel($label)] = [
2017-03-30 12:40:04 +03:00
'value' => $val,
'name' => $val,
];
}
}
return $data;
}
2017-06-01 19:13:13 +03:00
public function customDesigns()
{
$account = $this->entity;
$data = [];
for ($i=1; $i<=3; $i++) {
$label = trans('texts.custom_design' . $i);
if (! $account->{'custom_design' . $i}) {
$label .= ' - ' . trans('texts.empty');
}
$data[] = [
'url' => url('/settings/customize_design?design_id=') . ($i + 10),
'label' => $label
];
}
return $data;
}
public function clientLoginUrl()
{
$account = $this->entity;
if (Utils::isNinjaProd()) {
$url = 'https://';
$url .= $account->subdomain ?: 'app';
$url .= '.' . Domain::getDomainFromId($account->domain_id);
} else {
2018-01-08 12:14:09 +02:00
$url = trim(SITE_URL, '/');
}
2017-12-25 12:24:39 +02:00
$url .= '/client/login';
if (Utils::isNinja()) {
if (! $account->subdomain) {
$url .= '?account_key=' . $account->account_key;
}
} else {
if (Account::count() > 1) {
$url .= '?account_key=' . $account->account_key;
}
}
return $url;
}
2018-04-04 16:24:59 +03:00
public function customLabel($field)
{
2018-04-04 16:24:59 +03:00
return Utils::getCustomLabel($this->entity->customLabel($field));
}
2016-12-12 20:42:51 +02:00
}