1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Models/PurchaseOrder.php

275 lines
7.3 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
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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-06-02 04:41:44 +02:00
use App\Jobs\Entity\CreateEntityPdf;
2022-06-06 14:27:17 +02:00
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
2022-05-29 04:05:12 +02:00
use App\Services\PurchaseOrder\PurchaseOrderService;
2022-06-02 04:41:44 +02:00
use App\Utils\Ninja;
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;
use Illuminate\Support\Facades\Storage;
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',
'company_id',
'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',
// 'custom_surcharge_tax1',
// 'custom_surcharge_tax2',
// 'custom_surcharge_tax3',
// 'custom_surcharge_tax4',
'design_id',
'invoice_id',
'assigned_user_id',
'exchange_rate',
'balance',
'partial',
'paid_to_date',
2022-06-20 23:33:55 +02:00
// 'subscription_id',
2022-05-29 04:05:12 +02:00
'vendor_id',
'last_viewed'
];
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-06-13 11:59:24 +02:00
const STATUS_CANCELLED = 4;
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');
break;
case self::STATUS_SENT:
return ctrans('texts.sent');
break;
2022-06-14 14:18:20 +02:00
case self::STATUS_ACCEPTED:
return ctrans('texts.accepted');
2022-06-13 11:59:24 +02:00
break;
case self::STATUS_CANCELLED:
return ctrans('texts.cancelled');
break;
// code...
break;
}
}
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>';
break;
case self::STATUS_SENT:
return '<h5><span class="badge badge-primary">'.ctrans('texts.sent').'</span></h5>';
break;
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
break;
case self::STATUS_CANCELLED:
return '<h5><span class="badge badge-secondary">'.ctrans('texts.cancelled').'</span></h5>';
break;
default:
// code...
break;
}
}
2022-05-29 04:05:12 +02:00
public function assigned_user()
{
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
}
public function vendor()
{
return $this->belongsTo(Vendor::class)->withTrashed();
2022-05-29 04:05:12 +02:00
}
public function history()
{
return $this->hasManyThrough(Backup::class, Activity::class);
}
public function activities()
{
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(50);
}
public function company()
{
return $this->belongsTo(Company::class);
}
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
}
public function client()
{
return $this->belongsTo(Client::class)->withTrashed();
}
2022-06-02 04:41:44 +02:00
public function markInvitationsSent()
{
$this->invitations->each(function ($invitation) {
if (! isset($invitation->sent_date)) {
$invitation->sent_date = Carbon::now();
$invitation->save();
}
});
}
public function pdf_file_path($invitation = null, string $type = 'path', bool $portal = false)
{
if (! $invitation) {
if($this->invitations()->exists())
$invitation = $this->invitations()->first();
else{
$this->service()->createInvitations();
$invitation = $this->invitations()->first();
}
}
2022-05-29 04:05:12 +02:00
2022-06-02 04:41:44 +02:00
if(!$invitation)
throw new \Exception('Hard fail, could not create an invitation - is there a valid contact?');
$file_path = $this->vendor->purchase_order_filepath($invitation).$this->numberFormatter().'.pdf';
2022-06-02 04:41:44 +02:00
if(Ninja::isHosted() && $portal && Storage::disk(config('filesystems.default'))->exists($file_path)){
return Storage::disk(config('filesystems.default'))->{$type}($file_path);
}
elseif(Ninja::isHosted() && $portal){
2022-06-06 14:27:17 +02:00
$file_path = CreatePurchaseOrderPdf::dispatchNow($invitation,config('filesystems.default'));
2022-06-02 04:41:44 +02:00
return Storage::disk(config('filesystems.default'))->{$type}($file_path);
}
if(Storage::disk('public')->exists($file_path))
return Storage::disk('public')->{$type}($file_path);
2022-06-06 14:27:17 +02:00
$file_path = CreatePurchaseOrderPdf::dispatchNow($invitation);
2022-06-02 04:41:44 +02:00
return Storage::disk('public')->{$type}($file_path);
}
2022-05-29 04:05:12 +02:00
public function invitations()
{
2022-06-02 04:41:44 +02:00
return $this->hasMany(PurchaseOrderInvitation::class);
2022-05-29 04:05:12 +02:00
}
public function project()
{
return $this->belongsTo(Project::class)->withTrashed();
}
public function invoice()
{
return $this->belongsTo(Invoice::class);
}
public function service()
{
2022-06-06 14:27:17 +02:00
return new PurchaseOrderService($this);
2022-05-29 04:05:12 +02:00
}
public function invoices()
{
return $this->belongsToMany(Invoice::class)->using(Paymentable::class);
}
public function payments()
{
return $this->morphToMany(Payment::class, 'paymentable');
}
public function documents()
{
return $this->morphMany(Document::class, 'documentable');
}
2022-06-06 14:27:17 +02:00
public function calc()
{
$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();
}
2022-05-29 04:05:12 +02:00
}