2018-10-15 14:40:34 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +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
|
|
|
*/
|
2018-10-15 14:40:34 +02:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2023-04-29 08:04:42 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2019-05-02 07:47:16 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2023-06-30 02:26:26 +02:00
|
|
|
use App\Jobs\Entity\CreateRawPdf;
|
2023-04-29 08:04:42 +02:00
|
|
|
use App\Helpers\Invoice\InvoiceSum;
|
|
|
|
use App\Jobs\Entity\CreateEntityPdf;
|
2019-12-18 03:45:18 +01:00
|
|
|
use App\Utils\Traits\MakesReminders;
|
2019-05-02 07:47:16 +02:00
|
|
|
use App\Utils\Traits\NumberFormatter;
|
2023-04-29 08:04:42 +02:00
|
|
|
use App\Services\Ledger\LedgerService;
|
2019-09-04 03:45:53 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2023-04-29 08:04:42 +02:00
|
|
|
use App\Services\Invoice\InvoiceService;
|
|
|
|
use App\Utils\Traits\MakesInvoiceValues;
|
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
2019-08-29 06:07:04 +02:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2023-04-29 08:04:42 +02:00
|
|
|
use App\Models\Presenters\InvoicePresenter;
|
|
|
|
use App\Helpers\Invoice\InvoiceSumInclusive;
|
|
|
|
use App\Utils\Traits\Invoice\ActionsInvoice;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use App\Events\Invoice\InvoiceReminderWasEmailed;
|
2018-10-15 14:40:34 +02:00
|
|
|
|
2023-03-08 08:33:42 +01:00
|
|
|
/**
|
|
|
|
* App\Models\Invoice
|
|
|
|
*
|
|
|
|
* @property int $id
|
|
|
|
* @property int $client_id
|
|
|
|
* @property int $user_id
|
|
|
|
* @property int|null $assigned_user_id
|
|
|
|
* @property int $company_id
|
|
|
|
* @property int $status_id
|
|
|
|
* @property int|null $project_id
|
|
|
|
* @property int|null $vendor_id
|
|
|
|
* @property int|null $recurring_id
|
|
|
|
* @property int|null $design_id
|
|
|
|
* @property string|null $number
|
|
|
|
* @property float $discount
|
|
|
|
* @property bool $is_amount_discount
|
|
|
|
* @property string|null $po_number
|
|
|
|
* @property string|null $date
|
|
|
|
* @property string|null $last_sent_date
|
|
|
|
* @property string|null $due_date
|
|
|
|
* @property bool $is_deleted
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property object|array $line_items
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property object|null $backup
|
|
|
|
* @property string|null $footer
|
|
|
|
* @property string|null $public_notes
|
|
|
|
* @property string|null $private_notes
|
|
|
|
* @property string|null $terms
|
|
|
|
* @property string|null $tax_name1
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property float $tax_rate1
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $tax_name2
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property float $tax_rate2
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $tax_name3
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property float $tax_rate3
|
|
|
|
* @property float $total_taxes
|
|
|
|
* @property bool $uses_inclusive_taxes
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $custom_value1
|
|
|
|
* @property string|null $custom_value2
|
|
|
|
* @property string|null $custom_value3
|
|
|
|
* @property string|null $custom_value4
|
|
|
|
* @property string|null $next_send_date
|
|
|
|
* @property string|null $custom_surcharge1
|
|
|
|
* @property string|null $custom_surcharge2
|
|
|
|
* @property string|null $custom_surcharge3
|
|
|
|
* @property string|null $custom_surcharge4
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property bool $custom_surcharge_tax1
|
|
|
|
* @property bool $custom_surcharge_tax2
|
|
|
|
* @property bool $custom_surcharge_tax3
|
|
|
|
* @property bool $custom_surcharge_tax4
|
2023-04-26 11:25:33 +02:00
|
|
|
* @property float $exchange_rate
|
|
|
|
* @property float $amount
|
|
|
|
* @property float $balance
|
|
|
|
* @property float|null $partial
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property string|null $partial_due_date
|
|
|
|
* @property string|null $last_viewed
|
|
|
|
* @property int|null $created_at
|
|
|
|
* @property int|null $updated_at
|
|
|
|
* @property int|null $deleted_at
|
|
|
|
* @property string|null $reminder1_sent
|
|
|
|
* @property string|null $reminder2_sent
|
|
|
|
* @property string|null $reminder3_sent
|
|
|
|
* @property string|null $reminder_last_sent
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property bool $auto_bill_enabled
|
|
|
|
* @property float $paid_to_date
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property int|null $subscription_id
|
|
|
|
* @property int $auto_bill_tries
|
2023-04-26 14:17:40 +02:00
|
|
|
* @property bool $is_proforma
|
2023-03-08 08:33:42 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read int|null $activities_count
|
|
|
|
* @property-read \App\Models\User|null $assigned_user
|
|
|
|
* @property-read \App\Models\Client $client
|
|
|
|
* @property-read \App\Models\Company $company
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read int|null $company_ledger_count
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read int|null $credits_count
|
|
|
|
* @property-read \App\Models\Design|null $design
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read int|null $documents_count
|
|
|
|
* @property-read \App\Models\Expense|null $expense
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read int|null $expenses_count
|
|
|
|
* @property-read mixed $balance_due
|
|
|
|
* @property-read mixed $hashed_id
|
|
|
|
* @property-read mixed $status
|
|
|
|
* @property-read mixed $total
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read int|null $history_count
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read int|null $invitations_count
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read int|null $payments_count
|
|
|
|
* @property-read \App\Models\Project|null $project
|
|
|
|
* @property-read \App\Models\RecurringInvoice|null $recurring_invoice
|
|
|
|
* @property-read \App\Models\Subscription|null $subscription
|
|
|
|
* @property-read \App\Models\Task|null $task
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @property-read int|null $tasks_count
|
|
|
|
* @property-read \App\Models\User $user
|
|
|
|
* @property-read \App\Models\Vendor|null $vendor
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel company()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
|
|
|
|
* @method static \Database\Factories\InvoiceFactory factory($count = null, $state = [])
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice filter(\App\Filters\QueryFilters $filters)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice newModelQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice newQuery()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice onlyTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice query()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereAmount($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereAssignedUserId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereAutoBillEnabled($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereAutoBillTries($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereBackup($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereBalance($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereClientId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCompanyId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCreatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomSurcharge1($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomSurcharge2($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomSurcharge3($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomSurcharge4($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomSurchargeTax1($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomSurchargeTax2($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomSurchargeTax3($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomSurchargeTax4($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomValue1($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomValue2($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomValue3($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereCustomValue4($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereDate($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereDeletedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereDesignId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereDiscount($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereDueDate($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereExchangeRate($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereFooter($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereIsAmountDiscount($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereIsDeleted($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereIsProforma($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereLastSentDate($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereLastViewed($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereLineItems($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereNextSendDate($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereNumber($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice wherePaidToDate($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice wherePartial($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice wherePartialDueDate($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice wherePoNumber($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice wherePrivateNotes($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereProjectId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice wherePublicNotes($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereRecurringId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereReminder1Sent($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereReminder2Sent($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereReminder3Sent($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereReminderLastSent($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereStatusId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereSubscriptionId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTaxName1($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTaxName2($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTaxName3($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTaxRate1($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTaxRate2($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTaxRate3($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTerms($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTotalTaxes($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereUpdatedAt($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereUserId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereUsesInclusiveTaxes($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereVendorId($value)
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice withTrashed()
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice withoutTrashed()
|
2023-03-16 05:20:38 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
2023-03-21 12:37:06 +01:00
|
|
|
* @property object|null $tax_data
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|Invoice whereTaxData($value)
|
2023-03-24 02:12:12 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
2023-03-27 22:47:07 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
2023-04-26 08:55:49 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
2023-03-27 22:47:07 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
2023-04-26 00:43:54 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
2023-03-27 22:47:07 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
2023-04-13 07:39:12 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
2023-03-27 22:47:07 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
2023-04-10 09:37:40 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
2023-03-27 22:47:07 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
2023-03-29 05:23:06 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
2023-03-27 22:47:07 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
2023-03-29 04:13:50 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
2023-03-24 02:12:12 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
2023-03-27 05:47:01 +02:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
2023-03-24 02:12:12 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
2023-03-24 08:02:34 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\CompanyLedger> $company_ledger
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Credit> $credits
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
|
2023-03-24 02:12:12 +01:00
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Expense> $expenses
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation> $invitations
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Payment> $payments
|
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Task> $tasks
|
2023-03-08 08:33:42 +01:00
|
|
|
* @mixin \Eloquent
|
|
|
|
*/
|
2018-11-02 11:54:46 +01:00
|
|
|
class Invoice extends BaseModel
|
2018-10-15 14:40:34 +02:00
|
|
|
{
|
2019-04-04 03:38:17 +02:00
|
|
|
use SoftDeletes;
|
|
|
|
use Filterable;
|
2019-05-02 07:47:16 +02:00
|
|
|
use NumberFormatter;
|
|
|
|
use MakesDates;
|
2019-08-29 06:07:04 +02:00
|
|
|
use PresentableTrait;
|
2019-09-02 07:08:26 +02:00
|
|
|
use MakesInvoiceValues;
|
2019-12-18 03:45:18 +01:00
|
|
|
use MakesReminders;
|
2020-04-08 12:48:31 +02:00
|
|
|
use ActionsInvoice;
|
2019-12-18 03:45:18 +01:00
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
protected $presenter = InvoicePresenter::class;
|
2019-04-04 03:38:17 +02:00
|
|
|
|
2020-07-23 05:55:11 +02:00
|
|
|
protected $touches = [];
|
2020-07-16 13:01:39 +02:00
|
|
|
|
2019-07-29 05:59:28 +02:00
|
|
|
protected $hidden = [
|
|
|
|
'id',
|
2019-07-30 00:28:38 +02:00
|
|
|
'private_notes',
|
|
|
|
'user_id',
|
|
|
|
'client_id',
|
|
|
|
'company_id',
|
2019-07-29 05:59:28 +02:00
|
|
|
];
|
|
|
|
|
2019-05-16 00:26:21 +02:00
|
|
|
protected $fillable = [
|
2019-11-27 11:27:24 +01:00
|
|
|
'number',
|
2019-05-16 00:26:21 +02:00
|
|
|
'discount',
|
|
|
|
'po_number',
|
2019-11-27 11:27:24 +01:00
|
|
|
'date',
|
2019-05-16 00:26:21 +02:00
|
|
|
'due_date',
|
|
|
|
'terms',
|
|
|
|
'public_notes',
|
|
|
|
'private_notes',
|
|
|
|
'tax_name1',
|
|
|
|
'tax_rate1',
|
|
|
|
'tax_name2',
|
|
|
|
'tax_rate2',
|
2019-10-05 04:28:23 +02:00
|
|
|
'tax_name3',
|
|
|
|
'tax_rate3',
|
2019-05-16 00:26:21 +02:00
|
|
|
'is_amount_discount',
|
|
|
|
'partial',
|
|
|
|
'partial_due_date',
|
2020-10-14 22:58:20 +02:00
|
|
|
'project_id',
|
2019-05-16 00:26:21 +02:00
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
2019-06-26 06:04:10 +02:00
|
|
|
'custom_value3',
|
|
|
|
'custom_value4',
|
2019-05-16 00:26:21 +02:00
|
|
|
'line_items',
|
|
|
|
'client_id',
|
2019-06-26 06:04:10 +02:00
|
|
|
'footer',
|
2020-03-05 08:14:57 +01:00
|
|
|
'custom_surcharge1',
|
|
|
|
'custom_surcharge2',
|
|
|
|
'custom_surcharge3',
|
|
|
|
'custom_surcharge4',
|
2020-03-07 23:15:11 +01:00
|
|
|
'design_id',
|
2020-06-16 02:21:40 +02:00
|
|
|
'assigned_user_id',
|
2020-09-06 11:38:10 +02:00
|
|
|
'exchange_rate',
|
2021-03-10 01:08:58 +01:00
|
|
|
'subscription_id',
|
2021-05-21 14:08:56 +02:00
|
|
|
'auto_bill_enabled',
|
2021-07-17 12:36:51 +02:00
|
|
|
'uses_inclusive_taxes',
|
2022-06-21 11:57:17 +02:00
|
|
|
'vendor_id',
|
2019-05-16 00:26:21 +02:00
|
|
|
];
|
2018-11-20 05:36:56 +01:00
|
|
|
|
2019-04-17 08:20:32 +02:00
|
|
|
protected $casts = [
|
2019-09-26 00:27:26 +02:00
|
|
|
'line_items' => 'object',
|
2020-06-15 01:34:18 +02:00
|
|
|
'backup' => 'object',
|
2019-09-26 00:27:26 +02:00
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
2022-01-05 05:32:07 +01:00
|
|
|
'is_deleted' => 'bool',
|
2022-02-27 07:49:49 +01:00
|
|
|
'is_amount_discount' => 'bool',
|
2023-03-21 12:31:19 +01:00
|
|
|
'tax_data' => 'object',
|
2019-04-17 08:20:32 +02:00
|
|
|
];
|
|
|
|
|
2020-08-14 14:21:46 +02:00
|
|
|
protected $with = [];
|
2019-04-25 09:16:41 +02:00
|
|
|
|
2019-08-14 04:16:09 +02:00
|
|
|
protected $appends = [
|
|
|
|
'hashed_id',
|
2020-09-06 11:38:10 +02:00
|
|
|
'status',
|
2019-08-14 04:16:09 +02:00
|
|
|
];
|
|
|
|
|
2019-04-04 01:30:49 +02:00
|
|
|
const STATUS_DRAFT = 1;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-04-28 12:25:18 +02:00
|
|
|
const STATUS_SENT = 2;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-02 07:47:16 +02:00
|
|
|
const STATUS_PARTIAL = 3;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-02 07:47:16 +02:00
|
|
|
const STATUS_PAID = 4;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-05-02 07:47:16 +02:00
|
|
|
const STATUS_CANCELLED = 5;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-04-08 12:48:31 +02:00
|
|
|
const STATUS_REVERSED = 6;
|
2019-04-23 06:16:41 +02:00
|
|
|
|
2020-04-08 12:48:31 +02:00
|
|
|
const STATUS_OVERDUE = -1; //status < 4 || < 3 && !is_deleted && !trashed() && due_date < now()
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2020-04-08 12:48:31 +02:00
|
|
|
const STATUS_UNPAID = -2; //status < 4 || < 3 && !is_deleted && !trashed()
|
2019-04-04 01:30:49 +02:00
|
|
|
|
2020-05-06 13:49:42 +02:00
|
|
|
public function getEntityType()
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::class;
|
2020-05-06 13:49:42 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getDateAttribute($value)
|
|
|
|
{
|
2020-08-15 00:03:29 +02:00
|
|
|
return $this->dateMutator($value);
|
2020-03-29 14:22:14 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getDueDateAttribute($value)
|
|
|
|
{
|
2020-08-15 00:03:29 +02:00
|
|
|
return $this->dateMutator($value);
|
2020-03-29 14:22:14 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
public function getPartialDueDateAttribute($value)
|
|
|
|
{
|
2020-08-15 00:03:29 +02:00
|
|
|
return $this->dateMutator($value);
|
2020-03-29 14:22:14 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
public function company()
|
2018-11-20 05:36:56 +01:00
|
|
|
{
|
2019-04-04 01:17:15 +02:00
|
|
|
return $this->belongsTo(Company::class);
|
2018-11-20 05:36:56 +01:00
|
|
|
}
|
|
|
|
|
2021-02-22 23:47:54 +01:00
|
|
|
public function project()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Project::class);
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:52:11 +01:00
|
|
|
public function vendor()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Vendor::class);
|
|
|
|
}
|
|
|
|
|
2021-02-22 23:47:54 +01:00
|
|
|
public function design()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Design::class);
|
|
|
|
}
|
|
|
|
|
2019-04-04 01:17:15 +02:00
|
|
|
public function user()
|
2018-11-20 05:36:56 +01:00
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(User::class)->withTrashed();
|
2018-11-20 05:36:56 +01:00
|
|
|
}
|
2019-04-04 01:30:49 +02:00
|
|
|
|
2021-04-07 10:06:50 +02:00
|
|
|
public function recurring_invoice()
|
|
|
|
{
|
2021-07-14 13:20:41 +02:00
|
|
|
return $this->belongsTo(RecurringInvoice::class, 'recurring_id', 'id')->withTrashed();
|
2021-04-07 10:06:50 +02:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:16:38 +01:00
|
|
|
public function assigned_user()
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
|
2019-11-05 11:16:38 +01:00
|
|
|
}
|
2019-12-07 12:33:49 +01:00
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
public function invitations()
|
|
|
|
{
|
2019-04-24 02:22:02 +02:00
|
|
|
return $this->hasMany(InvoiceInvitation::class);
|
2018-10-22 14:04:37 +02:00
|
|
|
}
|
2019-04-24 07:18:48 +02:00
|
|
|
|
|
|
|
public function client()
|
|
|
|
{
|
2019-11-25 10:38:55 +01:00
|
|
|
return $this->belongsTo(Client::class)->withTrashed();
|
2019-04-24 07:18:48 +02:00
|
|
|
}
|
2019-04-28 07:31:32 +02:00
|
|
|
|
2021-04-07 05:35:16 +02:00
|
|
|
public function subscription()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Subscription::class)->withTrashed();
|
|
|
|
}
|
|
|
|
|
2019-04-28 07:31:32 +02:00
|
|
|
public function documents()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Document::class, 'documentable');
|
|
|
|
}
|
2019-05-02 07:47:16 +02:00
|
|
|
|
2019-05-13 08:18:46 +02:00
|
|
|
public function payments()
|
|
|
|
{
|
2020-11-25 10:21:26 +01:00
|
|
|
return $this->morphToMany(Payment::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps();
|
2019-05-13 08:18:46 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 06:47:07 +02:00
|
|
|
public function company_ledger()
|
|
|
|
{
|
|
|
|
return $this->morphMany(CompanyLedger::class, 'company_ledgerable');
|
|
|
|
}
|
|
|
|
|
2020-04-16 10:41:25 +02:00
|
|
|
public function activities()
|
|
|
|
{
|
2021-08-22 08:23:00 +02:00
|
|
|
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(50);
|
2020-04-16 10:41:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function history()
|
|
|
|
{
|
2020-06-01 13:49:11 +02:00
|
|
|
return $this->hasManyThrough(Backup::class, Activity::class);
|
2020-04-16 10:41:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 05:54:26 +02:00
|
|
|
public function credits()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Credit::class);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-11-01 08:53:43 +01:00
|
|
|
public function tasks()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Task::class);
|
|
|
|
}
|
|
|
|
|
2022-03-14 07:17:26 +01:00
|
|
|
public function task()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Task::class);
|
|
|
|
}
|
|
|
|
|
2022-03-12 00:30:55 +01:00
|
|
|
public function expenses()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Expense::class);
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:49:51 +01:00
|
|
|
public function expense()
|
2020-11-01 08:53:43 +01:00
|
|
|
{
|
2022-03-11 09:49:51 +01:00
|
|
|
return $this->hasOne(Expense::class);
|
2020-11-01 08:53:43 +01:00
|
|
|
}
|
2023-05-14 01:25:08 +02:00
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Service entry points.
|
2023-05-14 01:25:08 +02:00
|
|
|
*
|
|
|
|
* @return InvoiceService
|
2020-02-03 11:33:07 +01:00
|
|
|
*/
|
2020-02-20 22:05:01 +01:00
|
|
|
public function service() :InvoiceService
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
return new InvoiceService($this);
|
|
|
|
}
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
public function ledger()
|
2020-02-20 22:05:01 +01:00
|
|
|
{
|
|
|
|
return new LedgerService($this);
|
|
|
|
}
|
|
|
|
|
2019-05-02 07:47:16 +02:00
|
|
|
/* ---------------- */
|
|
|
|
/* Settings getters */
|
|
|
|
/* ---------------- */
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
public function getStatusAttribute()
|
|
|
|
{
|
2021-04-21 14:27:08 +02:00
|
|
|
$due_date = $this->due_date ? Carbon::parse($this->due_date) : false;
|
|
|
|
$partial_due_date = $this->partial_due_Date ? Carbon::parse($this->partial_due_date) : false;
|
|
|
|
|
|
|
|
if ($this->status_id == self::STATUS_SENT && $due_date && $due_date->gt(now())) {
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::STATUS_UNPAID;
|
2021-04-21 14:27:08 +02:00
|
|
|
} elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->gt(now())) {
|
|
|
|
return self::STATUS_PARTIAL;
|
|
|
|
} elseif ($this->status_id == self::STATUS_SENT && $due_date && $due_date->lt(now())) {
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::STATUS_OVERDUE;
|
2021-04-21 14:27:08 +02:00
|
|
|
} elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->lt(now())) {
|
2020-09-06 11:38:10 +02:00
|
|
|
return self::STATUS_OVERDUE;
|
2020-02-03 11:33:07 +01:00
|
|
|
} else {
|
|
|
|
return $this->status_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public function isPayable(): bool
|
2019-08-14 07:40:22 +02:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if ($this->status_id == self::STATUS_DRAFT && $this->is_deleted == false) {
|
2020-07-07 14:33:11 +02:00
|
|
|
return true;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($this->status_id == self::STATUS_SENT && $this->is_deleted == false) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return true;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($this->status_id == self::STATUS_PARTIAL && $this->is_deleted == false) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return true;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($this->status_id == self::STATUS_SENT && $this->is_deleted == false) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return true;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif ($this->status_id == self::STATUS_DRAFT && $this->is_deleted == false) {
|
2019-10-08 04:03:40 +02:00
|
|
|
return true;
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-10-08 04:03:40 +02:00
|
|
|
return false;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-08-14 07:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public function isRefundable(): bool
|
2020-01-20 11:10:33 +01:00
|
|
|
{
|
2020-02-15 10:01:15 +01:00
|
|
|
if ($this->is_deleted) {
|
2020-01-29 05:25:08 +01:00
|
|
|
return false;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2020-01-20 11:10:33 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
if (($this->amount - $this->balance) == 0) {
|
2020-01-30 05:50:45 +01:00
|
|
|
return false;
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-01-20 11:10:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
public function isPartial(): bool
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
return $this->status_id >= self::STATUS_PARTIAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-02-15 10:01:15 +01:00
|
|
|
public function hasPartial(): bool
|
2020-02-03 11:33:07 +01:00
|
|
|
{
|
|
|
|
return ($this->partial && $this->partial > 0) === true;
|
|
|
|
}
|
|
|
|
|
2023-04-26 09:41:30 +02:00
|
|
|
public static function badgeForStatus(int $status): string
|
2019-08-14 04:16:09 +02:00
|
|
|
{
|
|
|
|
switch ($status) {
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_DRAFT:
|
|
|
|
return '<h5><span class="badge badge-light">'.ctrans('texts.draft').'</span></h5>';
|
|
|
|
case self::STATUS_SENT:
|
|
|
|
return '<h5><span class="badge badge-primary">'.ctrans('texts.sent').'</span></h5>';
|
|
|
|
case self::STATUS_PARTIAL:
|
|
|
|
return '<h5><span class="badge badge-primary">'.ctrans('texts.partial').'</span></h5>';
|
|
|
|
case self::STATUS_PAID:
|
|
|
|
return '<h5><span class="badge badge-success">'.ctrans('texts.paid').'</span></h5>';
|
|
|
|
case self::STATUS_CANCELLED:
|
|
|
|
return '<h5><span class="badge badge-secondary">'.ctrans('texts.cancelled').'</span></h5>';
|
|
|
|
case self::STATUS_OVERDUE:
|
|
|
|
return '<h5><span class="badge badge-danger">'.ctrans('texts.overdue').'</span></h5>';
|
|
|
|
case self::STATUS_UNPAID:
|
2022-06-12 09:15:51 +02:00
|
|
|
return '<h5><span class="badge badge-warning text-white">'.ctrans('texts.unpaid').'</span></h5>';
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_REVERSED:
|
|
|
|
return '<h5><span class="badge badge-info">'.ctrans('texts.reversed').'</span></h5>';
|
2019-08-14 04:16:09 +02:00
|
|
|
default:
|
2023-04-26 09:41:30 +02:00
|
|
|
return '<h5><span class="badge badge-primary">'.ctrans('texts.sent').'</span></h5>';
|
|
|
|
|
2019-08-14 04:16:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-29 14:47:45 +02:00
|
|
|
|
2023-04-26 09:41:30 +02:00
|
|
|
public static function stringStatus(int $status): string
|
2019-12-14 06:49:48 +01:00
|
|
|
{
|
|
|
|
switch ($status) {
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_DRAFT:
|
2019-12-14 06:49:48 +01:00
|
|
|
return ctrans('texts.draft');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_SENT:
|
2019-12-14 06:49:48 +01:00
|
|
|
return ctrans('texts.sent');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_PARTIAL:
|
2019-12-14 06:49:48 +01:00
|
|
|
return ctrans('texts.partial');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_PAID:
|
2019-12-14 06:49:48 +01:00
|
|
|
return ctrans('texts.paid');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_CANCELLED:
|
2019-12-14 06:49:48 +01:00
|
|
|
return ctrans('texts.cancelled');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_OVERDUE:
|
2019-12-14 06:49:48 +01:00
|
|
|
return ctrans('texts.overdue');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_UNPAID:
|
2019-12-14 06:49:48 +01:00
|
|
|
return ctrans('texts.unpaid');
|
2020-09-06 11:38:10 +02:00
|
|
|
case self::STATUS_REVERSED:
|
2019-12-14 06:49:48 +01:00
|
|
|
return ctrans('texts.reversed');
|
|
|
|
default:
|
2023-04-26 09:41:30 +02:00
|
|
|
return ctrans('texts.sent');
|
2019-12-14 06:49:48 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2019-09-04 07:10:10 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Access the invoice calculator object.
|
2019-12-07 12:33:49 +01:00
|
|
|
*
|
2023-04-26 09:41:30 +02:00
|
|
|
* @return InvoiceSumInclusive | InvoiceSum The invoice calculator object getters
|
2019-09-04 07:10:10 +02:00
|
|
|
*/
|
2023-04-26 12:57:30 +02:00
|
|
|
public function calc(): InvoiceSumInclusive | InvoiceSum
|
2019-09-04 07:10:10 +02:00
|
|
|
{
|
2019-10-22 04:07:00 +02:00
|
|
|
$invoice_calc = null;
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if ($this->uses_inclusive_taxes) {
|
2019-11-10 22:12:21 +01:00
|
|
|
$invoice_calc = new InvoiceSumInclusive($this);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-12-04 02:06:14 +01:00
|
|
|
$invoice_calc = new InvoiceSum($this);
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
2019-12-07 12:33:49 +01:00
|
|
|
|
2019-09-04 07:10:10 +02:00
|
|
|
return $invoice_calc->build();
|
|
|
|
}
|
2019-09-05 01:52:49 +02:00
|
|
|
|
2021-05-16 01:41:12 +02:00
|
|
|
public function pdf_file_path($invitation = null, string $type = 'path', bool $portal = false)
|
2023-06-30 02:26:26 +02:00
|
|
|
{return "data:application/pdf;base64,".base64_encode((new CreateRawPdf($invitation, $invitation->company->db))->handle());
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $invitation) {
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->invitations()->exists()) {
|
2021-04-30 01:01:56 +02:00
|
|
|
$invitation = $this->invitations()->first();
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-04-30 01:01:56 +02:00
|
|
|
$this->service()->createInvitations();
|
|
|
|
$invitation = $this->invitations()->first();
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-02-12 01:41:17 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $invitation) {
|
2021-04-30 01:01:56 +02:00
|
|
|
throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?');
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-04-30 01:01:56 +02:00
|
|
|
|
2021-06-12 13:50:01 +02:00
|
|
|
$file_path = $this->client->invoice_filepath($invitation).$this->numberFormatter().'.pdf';
|
2021-05-16 08:26:55 +02:00
|
|
|
|
2022-01-06 05:58:55 +01:00
|
|
|
$file_exists = false;
|
|
|
|
|
|
|
|
/* Flysystem throws an exception if the path is "corrupted" so lets wrap it in a try catch and return a bool 06/01/2022*/
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2022-01-06 05:58:55 +01:00
|
|
|
$file_exists = Storage::disk(config('filesystems.default'))->exists($file_path);
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
2022-01-06 05:58:55 +01:00
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted() && $portal && $file_exists) {
|
2021-05-16 08:26:55 +02:00
|
|
|
return Storage::disk(config('filesystems.default'))->{$type}($file_path);
|
2022-06-21 11:57:17 +02:00
|
|
|
} elseif (Ninja::isHosted()) {
|
2022-06-24 13:15:14 +02:00
|
|
|
$file_path = (new CreateEntityPdf($invitation, config('filesystems.default')))->handle();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-05-16 08:26:55 +02:00
|
|
|
return Storage::disk(config('filesystems.default'))->{$type}($file_path);
|
|
|
|
}
|
2022-05-17 01:22:03 +02:00
|
|
|
|
2022-10-12 02:27:14 +02:00
|
|
|
try {
|
|
|
|
$file_exists = Storage::disk(config('filesystems.default'))->exists($file_path);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file_exists) {
|
|
|
|
return Storage::disk(config('filesystems.default'))->{$type}($file_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2022-01-06 05:58:55 +01:00
|
|
|
$file_exists = Storage::disk('public')->exists($file_path);
|
2022-06-21 11:57:17 +02:00
|
|
|
} catch (\Exception $e) {
|
2022-01-06 05:58:55 +01:00
|
|
|
nlog($e->getMessage());
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($file_exists) {
|
2021-05-16 01:41:12 +02:00
|
|
|
return Storage::disk('public')->{$type}($file_path);
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-05-16 01:41:12 +02:00
|
|
|
|
2022-06-24 13:15:14 +02:00
|
|
|
$file_path = (new CreateEntityPdf($invitation))->handle();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
return Storage::disk('public')->{$type}($file_path);
|
2019-11-05 23:52:57 +01:00
|
|
|
}
|
|
|
|
|
2020-02-03 11:33:07 +01:00
|
|
|
public function markInvitationsSent()
|
2019-11-19 22:06:48 +01:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
$this->invitations->each(function ($invitation) {
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! isset($invitation->sent_date)) {
|
2022-04-20 03:55:33 +02:00
|
|
|
$invitation->load('invoice');
|
2019-11-19 22:06:48 +01:00
|
|
|
$invitation->sent_date = Carbon::now();
|
|
|
|
$invitation->save();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-02-03 11:33:07 +01:00
|
|
|
|
2020-07-28 06:29:56 +02:00
|
|
|
/**
|
|
|
|
* Filtering logic to determine
|
2020-09-06 11:38:10 +02:00
|
|
|
* whether an invoice is locked
|
|
|
|
* based on the current status of the invoice.
|
|
|
|
* @return bool [description]
|
2020-07-28 06:29:56 +02:00
|
|
|
*/
|
|
|
|
public function isLocked() :bool
|
|
|
|
{
|
|
|
|
$locked_status = $this->client->getSetting('lock_invoices');
|
|
|
|
|
|
|
|
switch ($locked_status) {
|
|
|
|
case 'off':
|
|
|
|
return false;
|
|
|
|
case 'when_sent':
|
2021-07-28 04:29:51 +02:00
|
|
|
return $this->status_id == self::STATUS_SENT;
|
2020-07-28 06:29:56 +02:00
|
|
|
case 'when_paid':
|
|
|
|
return $this->status_id == self::STATUS_PAID || $this->status_id == self::STATUS_PARTIAL;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 14:53:36 +02:00
|
|
|
public function getBalanceDueAttribute()
|
|
|
|
{
|
|
|
|
return $this->balance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTotalAttribute()
|
|
|
|
{
|
|
|
|
return $this->calc()->getTotal();
|
|
|
|
}
|
2020-12-09 10:52:08 +01:00
|
|
|
|
2022-02-28 00:15:36 +01:00
|
|
|
public function getPayableAmount()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->partial > 0) {
|
2022-02-28 00:15:36 +01:00
|
|
|
return $this->partial;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-28 00:15:36 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->balance > 0) {
|
2022-02-28 00:15:36 +01:00
|
|
|
return $this->balance;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-28 00:15:36 +01:00
|
|
|
|
2023-04-26 09:41:30 +02:00
|
|
|
if ($this->status_id == 1) {
|
2022-02-28 00:15:36 +01:00
|
|
|
return $this->amount;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-02-28 00:15:36 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-17 07:23:48 +01:00
|
|
|
public function entityEmailEvent($invitation, $reminder_template, $template = '')
|
2020-12-09 10:52:08 +01:00
|
|
|
{
|
2023-04-27 05:15:49 +02:00
|
|
|
|
2020-12-09 10:52:08 +01:00
|
|
|
switch ($reminder_template) {
|
|
|
|
case 'invoice':
|
2021-07-26 11:55:54 +02:00
|
|
|
event(new InvoiceWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template));
|
2020-12-09 10:52:08 +01:00
|
|
|
break;
|
|
|
|
case 'reminder1':
|
2023-04-27 05:15:49 +02:00
|
|
|
event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template));
|
2020-12-09 10:52:08 +01:00
|
|
|
break;
|
|
|
|
case 'reminder2':
|
2023-04-27 05:15:49 +02:00
|
|
|
event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template));
|
2020-12-09 10:52:08 +01:00
|
|
|
break;
|
|
|
|
case 'reminder3':
|
2023-04-27 05:15:49 +02:00
|
|
|
event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template));
|
2020-12-09 10:52:08 +01:00
|
|
|
break;
|
|
|
|
case 'reminder_endless':
|
2023-03-17 07:23:48 +01:00
|
|
|
case 'endless_reminder':
|
2023-04-27 05:15:49 +02:00
|
|
|
event(new InvoiceReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template));
|
|
|
|
break;
|
|
|
|
case 'custom1':
|
|
|
|
case 'custom2':
|
|
|
|
case 'custom3':
|
2023-05-17 01:32:23 +02:00
|
|
|
event(new InvoiceWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $template));
|
2020-12-09 10:52:08 +01:00
|
|
|
break;
|
|
|
|
default:
|
2022-06-21 11:57:17 +02:00
|
|
|
// code...
|
2020-12-09 10:52:08 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 22:52:33 +01:00
|
|
|
|
|
|
|
public function transaction_event()
|
|
|
|
{
|
2022-03-10 02:17:05 +01:00
|
|
|
$invoice = $this->fresh();
|
2022-03-10 01:32:04 +01:00
|
|
|
|
2022-03-09 22:52:33 +01:00
|
|
|
return [
|
2022-05-17 01:22:03 +02:00
|
|
|
'invoice_id' => $invoice->id,
|
|
|
|
'invoice_amount' => $invoice->amount ?: 0,
|
|
|
|
'invoice_partial' => $invoice->partial ?: 0,
|
|
|
|
'invoice_balance' => $invoice->balance ?: 0,
|
2022-03-10 02:17:05 +01:00
|
|
|
'invoice_paid_to_date' => $invoice->paid_to_date ?: 0,
|
|
|
|
'invoice_status' => $invoice->status_id ?: 1,
|
2022-03-09 22:52:33 +01:00
|
|
|
];
|
|
|
|
}
|
2022-04-06 02:38:01 +02:00
|
|
|
|
2022-07-20 01:24:38 +02:00
|
|
|
public function expense_documents()
|
|
|
|
{
|
|
|
|
$line_items = $this->line_items;
|
|
|
|
|
|
|
|
$expense_ids = [];
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($line_items as $item) {
|
|
|
|
if (property_exists($item, 'expense_id')) {
|
2022-07-20 01:24:38 +02:00
|
|
|
$expense_ids[] = $item->expense_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Expense::whereIn('id', $this->transformKeys($expense_ids))
|
|
|
|
->where('invoice_documents', 1)
|
|
|
|
->where('company_id', $this->company_id)
|
|
|
|
->cursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function task_documents()
|
|
|
|
{
|
|
|
|
$line_items = $this->line_items;
|
|
|
|
|
|
|
|
$task_ids = [];
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
foreach ($line_items as $item) {
|
|
|
|
if (property_exists($item, 'task_id')) {
|
2022-07-20 01:24:38 +02:00
|
|
|
$task_ids[] = $item->task_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task::whereIn('id', $this->transformKeys($task_ids))
|
2023-02-16 02:36:09 +01:00
|
|
|
->whereHas('company', function ($query) {
|
|
|
|
$query->where('invoice_task_documents', 1);
|
2022-07-20 01:24:38 +02:00
|
|
|
})
|
|
|
|
->where('company_id', $this->company_id)
|
|
|
|
->cursor();
|
|
|
|
}
|
|
|
|
|
2022-04-06 02:38:01 +02:00
|
|
|
public function translate_entity()
|
|
|
|
{
|
|
|
|
return ctrans('texts.invoice');
|
|
|
|
}
|
2019-12-07 12:33:49 +01:00
|
|
|
}
|