akaunting/app/Models/Income/InvoiceTotal.php

90 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Models\Income;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Model;
2018-04-17 16:40:52 +03:00
use App\Models\Setting\Tax;
use App\Traits\DateTime;
class InvoiceTotal extends Model
{
use DateTime;
protected $table = 'invoice_totals';
2018-05-02 22:33:28 +03:00
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['title'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'invoice_id', 'code', 'name', 'amount', 'sort_order'];
public function invoice()
{
return $this->belongsTo('App\Models\Income\Invoice');
}
2017-10-21 14:23:57 +03:00
/**
* Convert amount to double.
*
2019-11-16 10:21:14 +03:00
* @param string $value
2017-10-21 14:23:57 +03:00
* @return void
*/
public function setAmountAttribute($value)
{
2019-11-16 10:21:14 +03:00
$this->attributes['amount'] = (double)$value;
2017-10-21 14:23:57 +03:00
}
2018-04-17 16:40:52 +03:00
/**
* Get the formatted name.
*
* @return string
*/
2018-05-02 22:33:28 +03:00
public function getTitleAttribute()
2018-04-17 16:40:52 +03:00
{
2018-05-02 22:33:28 +03:00
$title = $this->name;
2018-04-17 16:40:52 +03:00
$percent = 0;
2019-11-16 10:21:14 +03:00
$tax = null;
2018-05-02 22:33:28 +03:00
switch ($this->code) {
case 'discount':
$title = trans($title);
$percent = $this->invoice->discount;
2018-04-17 16:40:52 +03:00
2018-05-02 22:33:28 +03:00
break;
case 'tax':
2019-11-16 10:21:14 +03:00
$tax = Tax::where('name', $title)->first();
2018-04-17 16:40:52 +03:00
2019-11-16 10:21:14 +03:00
if (!empty($tax->rate)) {
$percent = $tax->rate;
2018-05-02 22:33:28 +03:00
}
break;
2018-04-17 16:40:52 +03:00
}
if (!empty($percent)) {
2019-11-16 10:21:14 +03:00
2018-05-02 22:33:28 +03:00
$title .= ' (';
2018-04-17 16:40:52 +03:00
2019-11-16 10:21:14 +03:00
if (setting('localisation.percent_position', 'after') == 'after') {
$title .= $this->code == 'discount' ? false : $tax->type == 'fixed' ? $percent : $percent . '%';
2018-04-17 16:40:52 +03:00
} else {
2019-11-16 10:21:14 +03:00
$title .= $this->code == 'discount' ? false : $tax->type == 'fixed' ? $percent : '%' . $percent;
2018-04-17 16:40:52 +03:00
}
2018-05-02 22:33:28 +03:00
$title .= ')';
2018-04-17 16:40:52 +03:00
}
2018-05-02 22:33:28 +03:00
return $title;
2018-04-17 16:40:52 +03:00
}
}