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

384 lines
12 KiB
PHP
Raw Normal View History

2022-05-29 04:05:12 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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)
2022-05-29 04:05:12 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Models;
2022-06-06 14:27:17 +02:00
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
2022-05-29 04:05:12 +02:00
use App\Services\PurchaseOrder\PurchaseOrderService;
2022-06-13 11:59:24 +02:00
use App\Utils\Traits\MakesDates;
2022-05-29 04:05:12 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2022-06-02 04:41:44 +02:00
use Illuminate\Support\Carbon;
2022-05-29 04:05:12 +02:00
2023-03-08 08:33:42 +01:00
/**
* App\Models\PurchaseOrder
*
* @property int $id
* @property int|null $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 int|null $invoice_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
2023-10-13 23:31:17 +02:00
* @property bool $is_deleted
* @property object|array|string $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-08-08 11:44:52 +02:00
* @property float $tax_rate1
2023-03-08 08:33:42 +01:00
* @property string|null $tax_name2
2023-08-08 11:44:52 +02:00
* @property float $tax_rate2
2023-03-08 08:33:42 +01:00
* @property string|null $tax_name3
2023-08-08 11:44:52 +02:00
* @property float $tax_rate3
* @property float $total_taxes
2023-03-08 08:33:42 +01:00
* @property int $uses_inclusive_taxes
* @property string|null $reminder1_sent
* @property string|null $reminder2_sent
* @property string|null $reminder3_sent
* @property string|null $reminder_last_sent
* @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
* @property int $custom_surcharge_tax1
* @property int $custom_surcharge_tax2
* @property int $custom_surcharge_tax3
* @property int $custom_surcharge_tax4
2023-08-08 11:44:52 +02:00
* @property float $exchange_rate
* @property float $balance
2023-04-26 11:25:33 +02:00
* @property float|null $partial
2023-08-08 11:44:52 +02:00
* @property float $amount
* @property float $paid_to_date
2023-03-08 08:33:42 +01:00
* @property string|null $partial_due_date
* @property string|null $last_viewed
* @property int|null $deleted_at
* @property int|null $created_at
* @property int|null $updated_at
* @property int|null $expense_id
* @property int|null $currency_id
* @property-read int|null $activities_count
2023-08-11 06:18:58 +02:00
* @property \App\Models\User|null $assigned_user
* @property \App\Models\Client|null $client
* @property \App\Models\Company $company
2023-03-08 08:33:42 +01:00
* @property-read int|null $documents_count
2023-08-11 06:18:58 +02:00
* @property \App\Models\Expense|null $expense
* @property string $hashed_id
* @property \App\Models\Invoice|null $invoice
* @property \App\Models\Project|null $project
* @property \App\Models\User $user
* @property \App\Models\Vendor $vendor
* @property \App\Models\PurchaseOrderInvitation $invitation
2023-08-06 09:03:12 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|PurchaseOrder exclude($columns)
2023-03-08 08:33:42 +01:00
* @method static \Database\Factories\PurchaseOrderFactory factory($count = null, $state = [])
* @method static \Illuminate\Database\Eloquent\Builder|PurchaseOrder filter(\App\Filters\QueryFilters $filters)
* @method static \Illuminate\Database\Eloquent\Builder|PurchaseOrder newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|PurchaseOrder newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|PurchaseOrder onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|PurchaseOrder query()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel scope()
2023-03-16 05:20:38 +01:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
2023-04-13 07:39:12 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Document> $documents
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Backup> $history
2023-04-26 08:55:49 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\PurchaseOrderInvitation> $invitations
2023-08-11 06:18:58 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|PurchaseOrder withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|PurchaseOrder withoutTrashed()
2023-03-08 08:33:42 +01:00
* @mixin \Eloquent
*/
2022-05-29 04:05:12 +02:00
class PurchaseOrder extends BaseModel
{
use Filterable;
use SoftDeletes;
2022-06-13 11:59:24 +02:00
use MakesDates;
2022-05-29 04:05:12 +02:00
protected $fillable = [
'number',
'discount',
'status_id',
'last_sent_date',
'is_deleted',
'po_number',
'date',
'due_date',
'terms',
'public_notes',
'private_notes',
'tax_name1',
'tax_rate1',
'tax_name2',
'tax_rate2',
'tax_name3',
'tax_rate3',
'total_taxes',
'uses_inclusive_taxes',
'is_amount_discount',
'partial',
'recurring_id',
'next_send_date',
'reminder1_sent',
'reminder2_sent',
'reminder3_sent',
'reminder_last_sent',
'partial_due_date',
'project_id',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'backup',
'footer',
'line_items',
'client_id',
'custom_surcharge1',
'custom_surcharge2',
'custom_surcharge3',
'custom_surcharge4',
'design_id',
'invoice_id',
'assigned_user_id',
'exchange_rate',
'balance',
'partial',
'paid_to_date',
'vendor_id',
2022-11-24 07:23:36 +01:00
'last_viewed',
'currency_id',
2022-05-29 04:05:12 +02:00
];
protected $casts = [
'line_items' => 'object',
'backup' => 'object',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'is_amount_discount' => 'bool',
];
const STATUS_DRAFT = 1;
const STATUS_SENT = 2;
2022-06-14 14:18:20 +02:00
const STATUS_ACCEPTED = 3;
2022-07-07 03:20:43 +02:00
const STATUS_RECEIVED = 4;
const STATUS_CANCELLED = 5;
2022-05-29 04:05:12 +02:00
2022-06-13 11:59:24 +02:00
public static function stringStatus(int $status)
{
switch ($status) {
case self::STATUS_DRAFT:
return ctrans('texts.draft');
case self::STATUS_SENT:
return ctrans('texts.sent');
2022-06-14 14:18:20 +02:00
case self::STATUS_ACCEPTED:
return ctrans('texts.accepted');
2022-06-13 11:59:24 +02:00
case self::STATUS_CANCELLED:
return ctrans('texts.cancelled');
2023-04-26 11:25:33 +02:00
default:
return ctrans('texts.sent');
2022-06-13 11:59:24 +02:00
}
}
2022-06-13 12:34:40 +02:00
public static function badgeForStatus(int $status)
{
switch ($status) {
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>';
2022-06-14 14:18:20 +02:00
case self::STATUS_ACCEPTED:
return '<h5><span class="badge badge-primary">'.ctrans('texts.accepted').'</span></h5>';
2022-06-13 12:34:40 +02:00
case self::STATUS_CANCELLED:
return '<h5><span class="badge badge-secondary">'.ctrans('texts.cancelled').'</span></h5>';
default:
2023-04-26 12:57:30 +02:00
return '<h5><span class="badge badge-primary">'.ctrans('texts.sent').'</span></h5>';
2022-06-13 12:34:40 +02:00
}
}
2023-02-01 05:00:45 +01:00
public function getEntityType()
{
return self::class;
}
2022-06-13 12:34:40 +02:00
2023-08-01 15:53:10 +02:00
public function assigned_user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-29 04:05:12 +02:00
{
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
}
2023-08-11 06:18:58 +02:00
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2023-10-26 04:57:44 +02:00
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-29 04:05:12 +02:00
{
return $this->belongsTo(Vendor::class)->withTrashed();
2022-05-29 04:05:12 +02:00
}
2023-08-02 02:38:36 +02:00
public function history(): \Illuminate\Database\Eloquent\Relations\HasManyThrough
2022-05-29 04:05:12 +02:00
{
return $this->hasManyThrough(Backup::class, Activity::class);
}
2023-08-02 02:38:36 +02:00
public function activities(): \Illuminate\Database\Eloquent\Relations\HasMany
2022-05-29 04:05:12 +02:00
{
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(50);
}
2023-08-01 15:53:10 +02:00
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-29 04:05:12 +02:00
{
return $this->belongsTo(Company::class);
}
2023-08-01 15:53:10 +02:00
public function expense(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-07-06 11:25:22 +02:00
{
2022-07-07 08:13:37 +02:00
return $this->belongsTo(Expense::class);
2022-07-06 11:25:22 +02:00
}
2023-08-01 15:53:10 +02:00
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-29 04:05:12 +02:00
{
return $this->belongsTo(User::class)->withTrashed();
}
2023-08-01 15:53:10 +02:00
public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-29 04:05:12 +02:00
{
return $this->belongsTo(Client::class)->withTrashed();
}
2023-11-26 08:38:06 +01:00
public function currency(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Currency::class);
}
public function markInvitationsSent(): void
2022-06-02 04:41:44 +02:00
{
$this->invitations->each(function ($invitation) {
if (! isset($invitation->sent_date)) {
$invitation->sent_date = Carbon::now();
2023-02-01 05:00:45 +01:00
$invitation->saveQuietly();
2022-06-02 04:41:44 +02:00
}
});
}
2023-08-01 15:53:10 +02:00
public function invitations(): \Illuminate\Database\Eloquent\Relations\HasMany
2022-05-29 04:05:12 +02:00
{
2022-06-02 04:41:44 +02:00
return $this->hasMany(PurchaseOrderInvitation::class);
2022-05-29 04:05:12 +02:00
}
2023-08-02 02:38:36 +02:00
public function project(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-29 04:05:12 +02:00
{
return $this->belongsTo(Project::class)->withTrashed();
}
2023-08-02 02:38:36 +02:00
public function invoice(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-29 04:05:12 +02:00
{
return $this->belongsTo(Invoice::class);
}
/** @return PurchaseOrderService */
public function service() :PurchaseOrderService
2022-05-29 04:05:12 +02:00
{
2022-06-06 14:27:17 +02:00
return new PurchaseOrderService($this);
2022-05-29 04:05:12 +02:00
}
2023-08-02 02:38:36 +02:00
public function invoices(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
2022-05-29 04:05:12 +02:00
{
return $this->belongsToMany(Invoice::class)->using(Paymentable::class);
}
2023-08-02 02:38:36 +02:00
public function payments(): \Illuminate\Database\Eloquent\Relations\MorphToMany
2022-05-29 04:05:12 +02:00
{
return $this->morphToMany(Payment::class, 'paymentable');
}
2023-08-02 02:38:36 +02:00
public function documents(): \Illuminate\Database\Eloquent\Relations\MorphMany
2022-05-29 04:05:12 +02:00
{
return $this->morphMany(Document::class, 'documentable');
}
2023-04-26 12:57:30 +02:00
/**
* Access the invoice calculator object.
*
* @return InvoiceSumInclusive | InvoiceSum The invoice calculator object getters
*/
public function calc(): InvoiceSumInclusive | InvoiceSum
2022-06-06 14:27:17 +02:00
{
$purchase_order_calc = null;
if ($this->uses_inclusive_taxes) {
$purchase_order_calc = new InvoiceSumInclusive($this);
} else {
$purchase_order_calc = new InvoiceSum($this);
}
return $purchase_order_calc->build();
}
2023-08-02 02:38:36 +02:00
public function translate_entity(): string
2022-07-16 08:15:31 +02:00
{
return ctrans('texts.purchase_order');
}
2023-08-07 06:50:08 +02:00
public function typeIdString($id): string
{
$type = '';
match($id) {
'1' => $type = ctrans('texts.product'),
'2' => $type = ctrans('texts.service'),
'3' => $type = ctrans('texts.gateway_fees'),
'4' => $type = ctrans('texts.gateway_fees'),
'5' => $type = ctrans('texts.late_fees'),
'6' => $type = ctrans('texts.expense'),
default => $type = ctrans('texts.product'),
};
return $type;
}
public function taxTypeString($id): string
{
$tax_type = '';
2023-10-26 04:57:44 +02:00
match(intval($id)) {
2023-08-07 06:50:08 +02:00
Product::PRODUCT_TYPE_PHYSICAL => $tax_type = ctrans('texts.physical_goods'),
Product::PRODUCT_TYPE_SERVICE => $tax_type = ctrans('texts.services'),
Product::PRODUCT_TYPE_DIGITAL => $tax_type = ctrans('texts.digital_products'),
Product::PRODUCT_TYPE_SHIPPING => $tax_type = ctrans('texts.shipping'),
Product::PRODUCT_TYPE_EXEMPT => $tax_type = ctrans('texts.tax_exempt'),
Product::PRODUCT_TYPE_REDUCED_TAX => $tax_type = ctrans('texts.reduced_tax'),
Product::PRODUCT_TYPE_OVERRIDE_TAX => $tax_type = ctrans('texts.override_tax'),
Product::PRODUCT_TYPE_ZERO_RATED => $tax_type = ctrans('texts.zero_rated'),
Product::PRODUCT_TYPE_REVERSE_TAX => $tax_type = ctrans('texts.reverse_tax'),
default => $tax_type = ctrans('texts.physical_goods'),
};
return $tax_type;
}
2022-05-29 04:05:12 +02:00
}