akaunting/app/Models/Purchase/BillTotal.php

90 lines
1.9 KiB
PHP
Raw Normal View History

<?php
2019-12-31 15:49:09 +03:00
namespace App\Models\Purchase;
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 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()
{
2019-12-31 15:49:09 +03:00
return $this->belongsTo('App\Models\Purchase\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;
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->bill->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') {
2019-12-04 21:21:15 +03:00
$title .= ($this->code == 'discount') ? false : (($tax->type == 'fixed') ? $percent : $percent . '%');
2018-04-17 16:40:52 +03:00
} else {
2019-12-04 21:21:15 +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
}
}