mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-27 19:51:09 -04:00
Implement PHP CS Fixer and a coding standard to follow (#471)
* Create PHP CS Fixer config and add to CI workflow * Run php cs fixer on project * Add newline at end of file * Update to use PHP CS Fixer v3 * Run v3 config on project * Run seperate config in CI
This commit is contained in:
@ -3,44 +3,40 @@
|
||||
namespace Crater\Models;
|
||||
|
||||
use App;
|
||||
use Crater\Models\Company;
|
||||
use Crater\Models\CompanySetting;
|
||||
use Crater\Models\Currency;
|
||||
use Crater\Models\Tax;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Crater\Models\InvoiceTemplate;
|
||||
use Crater\Models\Payment;
|
||||
use Barryvdh\DomPDF\Facade as PDF;
|
||||
use Carbon\Carbon;
|
||||
use Crater\Traits\HasCustomFieldsTrait;
|
||||
use Crater\Mail\SendInvoiceMail;
|
||||
use Crater\Traits\GeneratesPdfTrait;
|
||||
use Crater\Traits\HasCustomFieldsTrait;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
use Crater\Traits\GeneratesPdfTrait;
|
||||
use Barryvdh\DomPDF\Facade as PDF;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class Invoice extends Model implements HasMedia
|
||||
{
|
||||
use HasFactory, InteractsWithMedia, GeneratesPdfTrait;
|
||||
use HasFactory;
|
||||
use InteractsWithMedia;
|
||||
use GeneratesPdfTrait;
|
||||
use HasCustomFieldsTrait;
|
||||
|
||||
const STATUS_DRAFT = 'DRAFT';
|
||||
const STATUS_SENT = 'SENT';
|
||||
const STATUS_VIEWED = 'VIEWED';
|
||||
const STATUS_OVERDUE = 'OVERDUE';
|
||||
const STATUS_COMPLETED = 'COMPLETED';
|
||||
public const STATUS_DRAFT = 'DRAFT';
|
||||
public const STATUS_SENT = 'SENT';
|
||||
public const STATUS_VIEWED = 'VIEWED';
|
||||
public const STATUS_OVERDUE = 'OVERDUE';
|
||||
public const STATUS_COMPLETED = 'COMPLETED';
|
||||
|
||||
const STATUS_DUE = 'DUE';
|
||||
const STATUS_UNPAID = 'UNPAID';
|
||||
const STATUS_PARTIALLY_PAID = 'PARTIALLY_PAID';
|
||||
const STATUS_PAID = 'PAID';
|
||||
public const STATUS_DUE = 'DUE';
|
||||
public const STATUS_UNPAID = 'UNPAID';
|
||||
public const STATUS_PARTIALLY_PAID = 'PARTIALLY_PAID';
|
||||
public const STATUS_PAID = 'PAID';
|
||||
|
||||
protected $dates = [
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at'
|
||||
'deleted_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@ -52,14 +48,14 @@ class Invoice extends Model implements HasMedia
|
||||
];
|
||||
|
||||
protected $guarded = [
|
||||
'id'
|
||||
'id',
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'formattedCreatedAt',
|
||||
'formattedInvoiceDate',
|
||||
'formattedDueDate',
|
||||
'invoicePdfUrl'
|
||||
'invoicePdfUrl',
|
||||
];
|
||||
|
||||
public function setInvoiceDateAttribute($value)
|
||||
@ -79,7 +75,7 @@ class Invoice extends Model implements HasMedia
|
||||
public static function getNextInvoiceNumber($value)
|
||||
{
|
||||
// Get the last created order
|
||||
$lastOrder = Invoice::where('invoice_number', 'LIKE', $value . '-%')
|
||||
$lastOrder = Invoice::where('invoice_number', 'LIKE', $value.'-%')
|
||||
->orderBy('invoice_number', 'desc')
|
||||
->first();
|
||||
|
||||
@ -87,7 +83,7 @@ class Invoice extends Model implements HasMedia
|
||||
$numberLength = CompanySetting::getSetting('invoice_number_length', request()->header('company'));
|
||||
$numberLengthText = "%0{$numberLength}d";
|
||||
|
||||
if (!$lastOrder) {
|
||||
if (! $lastOrder) {
|
||||
// We get here if there is no order at all
|
||||
// If there is no number set it to 0, which will be 1 at the end.
|
||||
$number = 0;
|
||||
@ -152,7 +148,7 @@ class Invoice extends Model implements HasMedia
|
||||
|
||||
public function getInvoicePdfUrlAttribute()
|
||||
{
|
||||
return url('/invoices/pdf/' . $this->unique_hash);
|
||||
return url('/invoices/pdf/'.$this->unique_hash);
|
||||
}
|
||||
|
||||
public function getPreviousStatus()
|
||||
@ -186,30 +182,35 @@ class Invoice extends Model implements HasMedia
|
||||
public function getInvoiceNumAttribute()
|
||||
{
|
||||
$position = $this->strposX($this->invoice_number, "-", 1) + 1;
|
||||
|
||||
return substr($this->invoice_number, $position);
|
||||
}
|
||||
|
||||
public function getInvoicePrefixAttribute()
|
||||
{
|
||||
$prefix = explode("-", $this->invoice_number)[0];
|
||||
|
||||
return $prefix;
|
||||
}
|
||||
|
||||
public function getFormattedCreatedAtAttribute($value)
|
||||
{
|
||||
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
|
||||
|
||||
return Carbon::parse($this->created_at)->format($dateFormat);
|
||||
}
|
||||
|
||||
public function getFormattedDueDateAttribute($value)
|
||||
{
|
||||
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
|
||||
|
||||
return Carbon::parse($this->due_date)->format($dateFormat);
|
||||
}
|
||||
|
||||
public function getFormattedInvoiceDateAttribute($value)
|
||||
{
|
||||
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
|
||||
|
||||
return Carbon::parse($this->invoice_date)->format($dateFormat);
|
||||
}
|
||||
|
||||
@ -227,13 +228,13 @@ class Invoice extends Model implements HasMedia
|
||||
{
|
||||
return $query->whereIn('invoices.paid_status', [
|
||||
self::STATUS_UNPAID,
|
||||
self::STATUS_PARTIALLY_PAID
|
||||
self::STATUS_PARTIALLY_PAID,
|
||||
]);
|
||||
}
|
||||
|
||||
public function scopeWhereInvoiceNumber($query, $invoiceNumber)
|
||||
{
|
||||
return $query->where('invoices.invoice_number', 'LIKE', '%' . $invoiceNumber . '%');
|
||||
return $query->where('invoices.invoice_number', 'LIKE', '%'.$invoiceNumber.'%');
|
||||
}
|
||||
|
||||
public function scopeInvoicesBetween($query, $start, $end)
|
||||
@ -248,9 +249,9 @@ class Invoice extends Model implements HasMedia
|
||||
{
|
||||
foreach (explode(' ', $search) as $term) {
|
||||
$query->whereHas('user', function ($query) use ($term) {
|
||||
$query->where('name', 'LIKE', '%' . $term . '%')
|
||||
->orWhere('contact_name', 'LIKE', '%' . $term . '%')
|
||||
->orWhere('company_name', 'LIKE', '%' . $term . '%');
|
||||
$query->where('name', 'LIKE', '%'.$term.'%')
|
||||
->orWhere('contact_name', 'LIKE', '%'.$term.'%')
|
||||
->orWhere('company_name', 'LIKE', '%'.$term.'%');
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -356,7 +357,7 @@ class Invoice extends Model implements HasMedia
|
||||
|
||||
self::createItems($invoice, $request);
|
||||
|
||||
if ($request->has('taxes') && (!empty($request->taxes))) {
|
||||
if ($request->has('taxes') && (! empty($request->taxes))) {
|
||||
self::createTaxes($invoice, $request);
|
||||
}
|
||||
|
||||
@ -368,7 +369,7 @@ class Invoice extends Model implements HasMedia
|
||||
'items',
|
||||
'user',
|
||||
'invoiceTemplate',
|
||||
'taxes'
|
||||
'taxes',
|
||||
])
|
||||
->find($invoice->id);
|
||||
|
||||
@ -393,7 +394,7 @@ class Invoice extends Model implements HasMedia
|
||||
$data['paid_status'] = Invoice::STATUS_PAID;
|
||||
} elseif ($this->due_amount < 0 && $this->paid_status != Invoice::STATUS_UNPAID) {
|
||||
return response()->json([
|
||||
'error' => 'invalid_due_amount'
|
||||
'error' => 'invalid_due_amount',
|
||||
]);
|
||||
} elseif ($data['due_amount'] != 0 && $this->paid_status == Invoice::STATUS_PAID) {
|
||||
$data['status'] = $this->getPreviousStatus();
|
||||
@ -408,7 +409,7 @@ class Invoice extends Model implements HasMedia
|
||||
|
||||
self::createItems($this, $request);
|
||||
|
||||
if ($request->has('taxes') && (!empty($request->taxes))) {
|
||||
if ($request->has('taxes') && (! empty($request->taxes))) {
|
||||
self::createTaxes($this, $request);
|
||||
}
|
||||
|
||||
@ -420,7 +421,7 @@ class Invoice extends Model implements HasMedia
|
||||
'items',
|
||||
'user',
|
||||
'invoiceTemplate',
|
||||
'taxes'
|
||||
'taxes',
|
||||
])
|
||||
->find($this->id);
|
||||
|
||||
@ -444,7 +445,7 @@ class Invoice extends Model implements HasMedia
|
||||
\Mail::to($data['to'])->send(new SendInvoiceMail($data));
|
||||
|
||||
return [
|
||||
'success' => true
|
||||
'success' => true,
|
||||
];
|
||||
}
|
||||
|
||||
@ -469,7 +470,7 @@ class Invoice extends Model implements HasMedia
|
||||
|
||||
public static function createTaxes($invoice, $request)
|
||||
{
|
||||
if ($request->has('taxes') && (!empty($request->taxes))) {
|
||||
if ($request->has('taxes') && (! empty($request->taxes))) {
|
||||
foreach ($request->taxes as $tax) {
|
||||
$tax['company_id'] = $request->header('company');
|
||||
|
||||
@ -489,9 +490,9 @@ class Invoice extends Model implements HasMedia
|
||||
if ($this->tax_per_item === 'YES') {
|
||||
foreach ($this->items as $item) {
|
||||
foreach ($item->taxes as $tax) {
|
||||
if (!in_array($tax->name, $taxTypes)) {
|
||||
if (! in_array($tax->name, $taxTypes)) {
|
||||
array_push($taxTypes, $tax->name);
|
||||
array_push($labels, $tax->name . ' (' . $tax->percent . '%)');
|
||||
array_push($labels, $tax->name.' ('.$tax->percent.'%)');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -528,10 +529,10 @@ class Invoice extends Model implements HasMedia
|
||||
'notes' => $this->getNotes(),
|
||||
'logo' => $logo ?? null,
|
||||
'labels' => $labels,
|
||||
'taxes' => $taxes
|
||||
'taxes' => $taxes,
|
||||
]);
|
||||
|
||||
return PDF::loadView('app.pdf.invoice.' . $invoiceTemplate->view);
|
||||
return PDF::loadView('app.pdf.invoice.'.$invoiceTemplate->view);
|
||||
}
|
||||
|
||||
public function getEmailAttachmentSetting()
|
||||
@ -587,7 +588,7 @@ class Invoice extends Model implements HasMedia
|
||||
'{INVOICE_DUE_DATE}' => $this->formattedDueDate,
|
||||
'{INVOICE_NUMBER}' => $this->invoice_number,
|
||||
'{INVOICE_REF_NUMBER}' => $this->reference_number,
|
||||
'{INVOICE_LINK}' => url('/customer/invoices/pdf/' . $this->unique_hash)
|
||||
'{INVOICE_LINK}' => url('/customer/invoices/pdf/'.$this->unique_hash),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user