Add discount per item feature to the bills

This commit is contained in:
Burak Çakırel
2020-03-24 00:16:11 +03:00
parent e2148ef9a7
commit 6903a44bce
17 changed files with 159 additions and 150 deletions

View File

@ -77,8 +77,6 @@ class Invoices extends Controller
$date_format = $this->getCompanyDateFormat();
$discount_location = $invoice->totals->contains($invoice->totals->where('code', 'discount')->first()) ? 'in_totals' : 'per_item';
// Get Invoice Totals
foreach ($invoice->totals as $invoice_total) {
$invoice->{$invoice_total->code} = $invoice_total->amount;
@ -92,22 +90,7 @@ class Invoices extends Controller
$invoice->grand_total = round($invoice->total - $invoice->paid, $currency->precision);
}
return view(
'sales.invoices.show',
compact(
'invoice',
'accounts',
'currencies',
'currency',
'account_currency_code',
'customers',
'categories',
'payment_methods',
'signed_url',
'date_format',
'discount_location'
)
);
return view('sales.invoices.show', compact('invoice', 'accounts', 'currencies', 'currency', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'signed_url', 'date_format'));
}
/**

View File

@ -40,7 +40,7 @@ class CreateBillItem extends Job
// Apply discount to amount
if (!empty($this->request['discount'])) {
$item_discounted_amount = $item_amount - ($item_amount * ($this->request['discount'] / 100));
$item_discounted_amount = $item_amount -= ($item_amount * ($this->request['discount'] / 100));
}
$tax_amount = 0;
@ -138,6 +138,7 @@ class CreateBillItem extends Job
'quantity' => (double) $this->request['quantity'],
'price' => (double) $this->request['price'],
'tax' => $item_tax_total,
'discount_rate' => $this->request['discount'],
'total' => $item_amount,
]);

View File

@ -18,7 +18,18 @@ class BillItem extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'bill_id', 'item_id', 'name', 'quantity', 'price', 'total', 'tax'];
protected $fillable = [
'company_id',
'bill_id',
'item_id',
'name',
'quantity',
'price',
'total',
'tax',
'discount_rate',
'discount_type',
];
/**
* Clonable relationships.
@ -84,6 +95,22 @@ class BillItem extends Model
$this->attributes['tax'] = (double) $value;
}
/**
* Get the formatted discount.
*
* @return string
*/
public function getDiscountRateAttribute($value)
{
if (setting('localisation.percent_position', 'after') === 'after') {
$text = ($this->discount_type === 'normal') ? $value . '%' : $value;
} else {
$text = ($this->discount_type === 'normal') ? '%' . $value : $value;
}
return $text;
}
/**
* Convert tax to Array.
*

View File

@ -1,42 +0,0 @@
<?php
namespace App\Models\Sale;
use App\Abstracts\Model;
use App\Traits\Currencies;
use Znck\Eloquent\Traits\BelongsToThrough;
class InvoiceItemDiscount extends Model
{
use Currencies, BelongsToThrough;
protected $table = 'invoice_item_discounts';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'invoice_id', 'invoice_item_id', 'rate', 'type', 'name', 'amount'];
public function invoice()
{
return $this->belongsTo('App\Models\Sale\Invoice');
}
public function item()
{
return $this->belongsToThrough('App\Models\Common\Item', 'App\Models\Sale\InvoiceItem', 'invoice_item_id')->withDefault(['name' => trans('general.na')]);
}
/**
* Convert rate to double.
*
* @param string $value
* @return void
*/
public function setRateAttribute($value)
{
$this->attributes['rate'] = (double) $value;
}
}