2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2015-11-08 09:43:49 +01:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Ninja\Presenters;
|
|
|
|
|
|
|
|
use App\Libraries\Skype\InvoiceCard;
|
2017-02-05 08:53:49 +01:00
|
|
|
use App\Models\Activity;
|
2017-01-22 11:09:29 +01:00
|
|
|
use Carbon;
|
2017-01-30 20:40:43 +01:00
|
|
|
use DropdownButton;
|
2016-12-12 19:42:51 +01:00
|
|
|
use stdClass;
|
2015-11-08 09:43:49 +01:00
|
|
|
use Utils;
|
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
class InvoicePresenter extends EntityPresenter
|
|
|
|
{
|
2015-11-12 21:36:28 +01:00
|
|
|
public function client()
|
|
|
|
{
|
|
|
|
return $this->entity->client ? $this->entity->client->getDisplayName() : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->entity->user->getDisplayName();
|
|
|
|
}
|
|
|
|
|
2016-08-04 19:01:30 +02:00
|
|
|
public function amount()
|
|
|
|
{
|
|
|
|
$invoice = $this->entity;
|
|
|
|
$account = $invoice->account;
|
|
|
|
|
|
|
|
return $account->formatMoney($invoice->amount, $invoice->client);
|
|
|
|
}
|
|
|
|
|
2016-08-10 14:57:34 +02:00
|
|
|
public function requestedAmount()
|
|
|
|
{
|
|
|
|
$invoice = $this->entity;
|
|
|
|
$account = $invoice->account;
|
|
|
|
|
|
|
|
return $account->formatMoney($invoice->getRequestedAmount(), $invoice->client);
|
|
|
|
}
|
|
|
|
|
2015-12-02 14:26:06 +01:00
|
|
|
public function balanceDueLabel()
|
|
|
|
{
|
2016-05-05 18:43:44 +02:00
|
|
|
if ($this->entity->partial > 0) {
|
2016-03-18 13:34:46 +01:00
|
|
|
return 'partial_due';
|
2016-05-26 16:56:54 +02:00
|
|
|
} elseif ($this->entity->isType(INVOICE_TYPE_QUOTE)) {
|
2015-12-02 14:26:06 +01:00
|
|
|
return 'total';
|
|
|
|
} else {
|
|
|
|
return 'balance_due';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 11:09:29 +01:00
|
|
|
public function age()
|
|
|
|
{
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $this->entity->due_date || $this->entity->date_date == '0000-00-00') {
|
2017-01-22 11:09:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$date = Carbon::parse($this->entity->due_date);
|
|
|
|
|
|
|
|
if ($date->isFuture()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $date->diffInDays();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function ageGroup()
|
|
|
|
{
|
|
|
|
$age = $this->age();
|
|
|
|
|
|
|
|
if ($age > 120) {
|
|
|
|
return 'age_group_120';
|
|
|
|
} elseif ($age > 90) {
|
|
|
|
return 'age_group_90';
|
|
|
|
} elseif ($age > 60) {
|
|
|
|
return 'age_group_60';
|
|
|
|
} elseif ($age > 30) {
|
|
|
|
return 'age_group_30';
|
|
|
|
} else {
|
|
|
|
return 'age_group_0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 14:57:34 +02:00
|
|
|
public function dueDateLabel()
|
|
|
|
{
|
|
|
|
if ($this->entity->isType(INVOICE_TYPE_STANDARD)) {
|
|
|
|
return trans('texts.due_date');
|
|
|
|
} else {
|
|
|
|
return trans('texts.valid_until');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function discount()
|
|
|
|
{
|
|
|
|
$invoice = $this->entity;
|
|
|
|
|
|
|
|
if ($invoice->is_amount_discount) {
|
|
|
|
return $invoice->account->formatMoney($invoice->discount);
|
|
|
|
} else {
|
|
|
|
return $invoice->discount . '%';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 12:49:26 +01:00
|
|
|
// https://schema.org/PaymentStatusType
|
|
|
|
public function paymentStatus()
|
|
|
|
{
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $this->entity->balance) {
|
2015-12-16 12:49:26 +01:00
|
|
|
return 'PaymentComplete';
|
|
|
|
} elseif ($this->entity->isOverdue()) {
|
|
|
|
return 'PaymentPastDue';
|
|
|
|
} else {
|
|
|
|
return 'PaymentDue';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 21:36:28 +01:00
|
|
|
public function status()
|
|
|
|
{
|
2016-02-25 10:25:07 +01:00
|
|
|
if ($this->entity->is_deleted) {
|
|
|
|
return trans('texts.deleted');
|
|
|
|
} elseif ($this->entity->trashed()) {
|
|
|
|
return trans('texts.archived');
|
2016-05-22 19:01:37 +02:00
|
|
|
} elseif ($this->entity->is_recurring) {
|
|
|
|
return trans('texts.active');
|
2016-02-25 10:25:07 +01:00
|
|
|
} else {
|
|
|
|
$status = $this->entity->invoice_status ? $this->entity->invoice_status->name : 'draft';
|
|
|
|
$status = strtolower($status);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-02-25 10:25:07 +01:00
|
|
|
return trans("texts.status_{$status}");
|
|
|
|
}
|
2015-11-12 21:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function invoice_date()
|
|
|
|
{
|
|
|
|
return Utils::fromSqlDate($this->entity->invoice_date);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function due_date()
|
|
|
|
{
|
|
|
|
return Utils::fromSqlDate($this->entity->due_date);
|
|
|
|
}
|
|
|
|
|
2016-02-25 10:25:07 +01:00
|
|
|
public function frequency()
|
|
|
|
{
|
2016-07-01 23:19:09 +02:00
|
|
|
$frequency = $this->entity->frequency ? $this->entity->frequency->name : '';
|
|
|
|
$frequency = strtolower($frequency);
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-07-02 23:18:13 +02:00
|
|
|
return trans('texts.freq_'.$frequency);
|
2016-02-25 10:25:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function email()
|
|
|
|
{
|
|
|
|
$client = $this->entity->client;
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-02-25 10:25:07 +01:00
|
|
|
return count($client->contacts) ? $client->contacts[0]->email : '';
|
|
|
|
}
|
2016-06-26 11:59:42 +02:00
|
|
|
|
|
|
|
public function autoBillEmailMessage()
|
|
|
|
{
|
|
|
|
$client = $this->entity->client;
|
|
|
|
$paymentMethod = $client->defaultPaymentMethod();
|
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! $paymentMethod) {
|
2016-06-26 11:59:42 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($paymentMethod->payment_type_id == PAYMENT_TYPE_ACH) {
|
|
|
|
$paymentMethodString = trans('texts.auto_bill_payment_method_bank_transfer');
|
|
|
|
} elseif ($paymentMethod->payment_type_id == PAYMENT_TYPE_PAYPAL) {
|
|
|
|
$paymentMethodString = trans('texts.auto_bill_payment_method_paypal');
|
|
|
|
} else {
|
|
|
|
$paymentMethodString = trans('texts.auto_bill_payment_method_credit_card');
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'payment_method' => $paymentMethodString,
|
|
|
|
'due_date' => $this->due_date(),
|
|
|
|
];
|
|
|
|
|
|
|
|
return trans('texts.auto_bill_notification', $data);
|
|
|
|
}
|
2016-08-10 14:57:34 +02:00
|
|
|
|
|
|
|
public function skypeBot()
|
|
|
|
{
|
|
|
|
return new InvoiceCard($this->entity);
|
|
|
|
}
|
2016-12-12 19:42:51 +01:00
|
|
|
|
|
|
|
public function rBits()
|
|
|
|
{
|
|
|
|
$properties = new stdClass();
|
|
|
|
$properties->terms_text = $this->entity->terms;
|
|
|
|
$properties->note = $this->entity->public_notes;
|
|
|
|
$properties->itemized_receipt = [];
|
|
|
|
|
|
|
|
foreach ($this->entity->invoice_items as $item) {
|
|
|
|
$properties->itemized_receipt[] = $item->present()->rBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = new stdClass();
|
|
|
|
$data->receive_time = time();
|
|
|
|
$data->type = 'transaction_details';
|
|
|
|
$data->source = 'user';
|
|
|
|
$data->properties = $properties;
|
|
|
|
|
|
|
|
return [$data];
|
|
|
|
}
|
2017-01-13 08:02:22 +01:00
|
|
|
|
|
|
|
public function moreActions()
|
|
|
|
{
|
|
|
|
$invoice = $this->entity;
|
|
|
|
$entityType = $invoice->getEntityType();
|
|
|
|
|
|
|
|
$actions = [
|
|
|
|
['url' => 'javascript:onCloneClick()', 'label' => trans("texts.clone_{$entityType}")],
|
|
|
|
['url' => url("{$entityType}s/{$entityType}_history/{$invoice->public_id}"), 'label' => trans('texts.view_history')],
|
2017-01-30 20:40:43 +01:00
|
|
|
DropdownButton::DIVIDER,
|
2017-01-13 08:02:22 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($entityType == ENTITY_QUOTE) {
|
|
|
|
if ($invoice->quote_invoice_id) {
|
|
|
|
$actions[] = ['url' => url("invoices/{$invoice->quote_invoice_id}/edit"), 'label' => trans('texts.view_invoice')];
|
|
|
|
} else {
|
|
|
|
$actions[] = ['url' => 'javascript:onConvertClick()', 'label' => trans('texts.convert_to_invoice')];
|
|
|
|
}
|
|
|
|
} elseif ($entityType == ENTITY_INVOICE) {
|
|
|
|
if ($invoice->quote_id) {
|
|
|
|
$actions[] = ['url' => url("quotes/{$invoice->quote_id}/edit"), 'label' => trans('texts.view_quote')];
|
|
|
|
}
|
|
|
|
|
2017-03-26 20:31:13 +02:00
|
|
|
if (!$invoice->deleted_at && ! $invoice->is_recurring && $invoice->balance != 0) {
|
2017-01-13 08:02:22 +01:00
|
|
|
$actions[] = ['url' => 'javascript:submitBulkAction("markPaid")', 'label' => trans('texts.mark_paid')];
|
|
|
|
$actions[] = ['url' => 'javascript:onPaymentClick()', 'label' => trans('texts.enter_payment')];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($invoice->payments as $payment) {
|
|
|
|
$label = trans('texts.view_payment');
|
|
|
|
if (count($invoice->payments) > 1) {
|
|
|
|
$label .= ' - ' . $invoice->account->formatMoney($payment->amount, $invoice->client);
|
|
|
|
}
|
|
|
|
$actions[] = ['url' => $payment->present()->url, 'label' => $label];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($actions) > 3) {
|
|
|
|
$actions[] = DropdownButton::DIVIDER;
|
|
|
|
}
|
|
|
|
|
2017-02-20 12:47:25 +01:00
|
|
|
if (! $invoice->trashed()) {
|
|
|
|
$actions[] = ['url' => 'javascript:onArchiveClick()', 'label' => trans("texts.archive_{$entityType}")];
|
|
|
|
}
|
|
|
|
if (! $invoice->is_deleted) {
|
|
|
|
$actions[] = ['url' => 'javascript:onDeleteClick()', 'label' => trans("texts.delete_{$entityType}")];
|
|
|
|
}
|
2017-01-13 08:02:22 +01:00
|
|
|
|
|
|
|
return $actions;
|
|
|
|
}
|
2017-03-15 16:09:23 +01:00
|
|
|
|
|
|
|
public function gatewayFee($gatewayTypeId = false)
|
|
|
|
{
|
|
|
|
$invoice = $this->entity;
|
|
|
|
$account = $invoice->account;
|
2017-03-16 16:32:46 +01:00
|
|
|
|
2017-03-26 09:37:32 +02:00
|
|
|
if (! $account->gateway_fee_enabled) {
|
2017-03-16 16:32:46 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-03-16 15:03:17 +01:00
|
|
|
$settings = $account->getGatewaySettings($gatewayTypeId);
|
2017-03-15 16:09:23 +01:00
|
|
|
|
2017-03-16 15:03:17 +01:00
|
|
|
if (! $settings || ! $settings->areFeesEnabled()) {
|
|
|
|
return '';
|
2017-03-15 16:09:23 +01:00
|
|
|
}
|
|
|
|
|
2017-03-16 22:12:56 +01:00
|
|
|
$fee = $invoice->calcGatewayFee($gatewayTypeId, true);
|
2017-03-16 21:34:45 +01:00
|
|
|
$fee = $account->formatMoney($fee, $invoice->client);
|
2017-03-16 16:32:46 +01:00
|
|
|
|
|
|
|
if (floatval($settings->fee_amount) < 0 || floatval($settings->fee_percent) < 0) {
|
|
|
|
$label = trans('texts.discount');
|
|
|
|
} else {
|
|
|
|
$label = trans('texts.fee');
|
|
|
|
}
|
|
|
|
|
2017-03-16 21:58:31 +01:00
|
|
|
return ' - ' . $fee . ' ' . $label;
|
2017-03-15 16:09:23 +01:00
|
|
|
}
|
2017-03-23 14:55:46 +01:00
|
|
|
|
|
|
|
public function multiAccountLink()
|
|
|
|
{
|
|
|
|
$invoice = $this->entity;
|
|
|
|
$account = $invoice->account;
|
|
|
|
|
|
|
|
if ($account->hasMultipleAccounts()) {
|
|
|
|
$link = url(sprintf('/account/%s?redirect_to=%s', $account->account_key, $invoice->present()->path));
|
|
|
|
} else {
|
|
|
|
$link = $invoice->present()->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
2016-05-22 19:01:37 +02:00
|
|
|
}
|