Add discount per item for invoice
This commit is contained in:
@ -77,6 +77,8 @@ 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;
|
||||
@ -90,7 +92,22 @@ 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'));
|
||||
return view(
|
||||
'sales.invoices.show',
|
||||
compact(
|
||||
'invoice',
|
||||
'accounts',
|
||||
'currencies',
|
||||
'currency',
|
||||
'account_currency_code',
|
||||
'customers',
|
||||
'categories',
|
||||
'payment_methods',
|
||||
'signed_url',
|
||||
'date_format',
|
||||
'discount_location'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -27,12 +27,18 @@ class Defaults extends Controller
|
||||
|
||||
$payment_methods = Modules::getPaymentMethods();
|
||||
|
||||
$discount_locations = [
|
||||
'in_totals' => trans('settings.default.discount_in_totals'),
|
||||
'per_item' => trans('settings.default.discount_per_item'),
|
||||
];
|
||||
|
||||
return view('settings.default.edit', compact(
|
||||
'setting',
|
||||
'accounts',
|
||||
'currencies',
|
||||
'taxes',
|
||||
'payment_methods'
|
||||
'payment_methods',
|
||||
'discount_locations'
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ class CreateInvoiceItem 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 CreateInvoiceItem extends Job
|
||||
'quantity' => (double) $this->request['quantity'],
|
||||
'price' => (double) $this->request['price'],
|
||||
'tax' => $item_tax_total,
|
||||
'discount_rate' => $this->request['discount'],
|
||||
'total' => $item_amount,
|
||||
]);
|
||||
|
||||
|
@ -17,7 +17,18 @@ class InvoiceItem extends Model
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'invoice_id', 'item_id', 'name', 'quantity', 'price', 'total', 'tax'];
|
||||
protected $fillable = [
|
||||
'company_id',
|
||||
'invoice_id',
|
||||
'item_id',
|
||||
'name',
|
||||
'quantity',
|
||||
'price',
|
||||
'total',
|
||||
'tax',
|
||||
'discount_rate',
|
||||
'discount_type',
|
||||
];
|
||||
|
||||
/**
|
||||
* Clonable relationships.
|
||||
@ -83,6 +94,22 @@ class InvoiceItem 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.
|
||||
*
|
||||
|
42
app/Models/Sale/InvoiceItemDiscount.php
Normal file
42
app/Models/Sale/InvoiceItemDiscount.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user