akaunting/app/Models/Expense/BillTotal.php

87 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Models\Expense;
use App\Models\Model;
2018-04-17 16:40:52 +03:00
use App\Models\Setting\Tax;
use App\Traits\DateTime;
class BillTotal extends Model
{
use DateTime;
protected $table = 'bill_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', 'bill_id', 'code', 'name', 'amount', 'sort_order'];
public function bill()
{
return $this->belongsTo('App\Models\Expense\Bill');
}
2017-10-21 14:23:57 +03:00
/**
* Convert amount to double.
*
* @param string $value
* @return void
*/
public function setAmountAttribute($value)
{
$this->attributes['amount'] = (double) $value;
}
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;
2018-05-02 22:33:28 +03:00
switch ($this->code) {
case 'discount':
$title = trans($title);
$percent = $this->bill->discount;
2018-04-17 16:40:52 +03:00
2018-05-02 22:33:28 +03:00
break;
case 'tax':
$rate = Tax::where('name', $title)->value('rate');
2018-04-17 16:40:52 +03:00
2018-05-02 22:33:28 +03:00
if (!empty($rate)) {
$percent = $rate;
}
break;
2018-04-17 16:40:52 +03:00
}
if (!empty($percent)) {
2018-05-02 22:33:28 +03:00
$title .= ' (';
2018-04-17 16:40:52 +03:00
if (setting('general.percent_position', 'after') == 'after') {
2018-05-02 22:33:28 +03:00
$title .= $percent . '%';
2018-04-17 16:40:52 +03:00
} else {
2018-05-02 22:33:28 +03:00
$title .= '%' . $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
}
}