mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-05-26 22:44:42 +00:00
invoice backend VAT & discount calculations
This commit is contained in:
@@ -182,7 +182,6 @@ class SetupOasisEnvironment extends Command
|
||||
'value' => 'system/oasis-og-image.jpg',
|
||||
],
|
||||
])->each(function ($option) {
|
||||
|
||||
Setting::updateOrCreate([
|
||||
'name' => $option['name'],
|
||||
], [
|
||||
|
||||
@@ -142,6 +142,8 @@ class InvoiceController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data for frontend invoice creator
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function editor()
|
||||
@@ -158,11 +160,11 @@ class InvoiceController extends Controller
|
||||
'isVatPayer' => $user->invoiceProfile->ic_dph
|
||||
? true
|
||||
: false,
|
||||
'latestInvoiceNumber' => $user->regularInvoices->last()
|
||||
? (int) $user->regularInvoices->last()->invoice_number
|
||||
'latestInvoiceNumber' => $user->regularInvoices->first()
|
||||
? (int) $user->regularInvoices->first()->invoice_number
|
||||
: null,
|
||||
'recommendedInvoiceNumber' => $user->regularInvoices->last()
|
||||
? (int) $user->regularInvoices->last()->invoice_number + 1
|
||||
'recommendedInvoiceNumber' => $user->regularInvoices->first()
|
||||
? (int) $user->regularInvoices->first()->invoice_number + 1
|
||||
: Carbon::now()->format('Y') . '0001',
|
||||
];
|
||||
}
|
||||
|
||||
+101
-46
@@ -43,81 +43,138 @@ function invoice_path($invoice)
|
||||
* Get only tax for single invoice item
|
||||
*
|
||||
* @param $item
|
||||
* @param false $format
|
||||
* @return float|int|string
|
||||
*/
|
||||
function invoice_item_only_tax_price($item, $format = false)
|
||||
function invoice_item_only_tax_price($item)
|
||||
{
|
||||
$tax = ($item['price'] * $item['amount']) * ($item['tax_rate'] / 100);
|
||||
|
||||
if ($format) {
|
||||
return Cashier::formatAmount($tax * 100, 'CZK', 'cs');
|
||||
}
|
||||
|
||||
return $tax;
|
||||
return ($item['price'] * $item['amount']) * ($item['tax_rate'] / 100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get item price with tax for single invoice item
|
||||
*
|
||||
* @param $item
|
||||
* @param false $format
|
||||
* @return float|int|string
|
||||
*/
|
||||
function invoice_item_with_tax_price($item, $format = false)
|
||||
function invoice_item_with_tax_price($item)
|
||||
{
|
||||
$tax = ($item['price'] * $item['amount']) * ($item['tax_rate'] / 100 + 1);
|
||||
|
||||
if ($format) {
|
||||
return Cashier::formatAmount($tax * 100, 'CZK', 'cs');
|
||||
}
|
||||
|
||||
return $tax;
|
||||
return ($item['price'] * $item['amount']) * ($item['tax_rate'] / 100 + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice
|
||||
* @param false $format
|
||||
* @return float|int|mixed|string
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
function invoice_total_discount($invoice, $format = false)
|
||||
function invoice_tax_base($invoice)
|
||||
{
|
||||
// Percent discount
|
||||
if ($invoice['discount_type'] === 'percent') {
|
||||
$discount = (int) (invoice_total_net($invoice) + invoice_total_tax($invoice)) * ($invoice['discount_rate'] / 100);
|
||||
$bag = collect([]);
|
||||
|
||||
if ($format) {
|
||||
return Cashier::formatAmount($discount * 100, $invoice['currency'], 'cs');
|
||||
// Count tax base
|
||||
foreach ($invoice['items'] as $item) {
|
||||
if ($bag->whereNotIn('rate', $item['tax_rate'])) {
|
||||
$bag->push([
|
||||
'rate' => $item['tax_rate'],
|
||||
'total' => $item['price'] * $item['amount'],
|
||||
]);
|
||||
} else {
|
||||
$bag->map(function ($bagItem) use ($item) {
|
||||
if ($bagItem['rate'] === $item['tax_rate']) {
|
||||
$bagItem['total'] += ($item['price'] * $item['amount']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return $discount;
|
||||
}
|
||||
|
||||
// Value discount
|
||||
if ($invoice['discount_type'] === 'value') {
|
||||
if ($format) {
|
||||
return Cashier::formatAmount($invoice['discount_rate'] * 100, $invoice['currency'], 'cs');
|
||||
}
|
||||
// Count discount
|
||||
if ($invoice['discount_type']) {
|
||||
return $bag->map(function ($bagItem) use ($invoice) {
|
||||
if ($invoice['discount_type'] === 'percent') {
|
||||
$bagItem['total'] -= $bagItem['total'] * ($invoice['discount_rate'] / 100);
|
||||
}
|
||||
|
||||
return $invoice['discount_rate'];
|
||||
if ($invoice['discount_type'] === 'value') {
|
||||
$percentage_of_discount = $invoice['discount_rate'] / (invoice_total($invoice) + $invoice['discount_rate']);
|
||||
|
||||
$bagItem['total'] -= $bagItem['total'] * $percentage_of_discount;
|
||||
}
|
||||
|
||||
return $bagItem;
|
||||
});
|
||||
}
|
||||
|
||||
return $bag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
function invoice_tax_summary($invoice)
|
||||
{
|
||||
$bag = collect([]);
|
||||
|
||||
// Count tax base
|
||||
foreach ($invoice['items'] as $item) {
|
||||
if ($bag->whereNotIn('rate', $item['tax_rate'])) {
|
||||
$bag->push([
|
||||
'rate' => $item['tax_rate'],
|
||||
'total' => ($item['price'] * $item['amount']) * ($item['tax_rate'] / 100),
|
||||
]);
|
||||
} else {
|
||||
$bag->map(function ($bagItem) use ($item) {
|
||||
if ($bagItem['rate'] === $item['tax_rate']) {
|
||||
$bagItem['total'] += ($item['price'] * $item['amount']) * ($item['tax_rate'] / 100);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Count discount
|
||||
if ($invoice['discount_type']) {
|
||||
return $bag->map(function ($bagItem) use ($invoice) {
|
||||
if ($invoice['discount_type'] === 'percent') {
|
||||
$bagItem['total'] -= $bagItem['total'] * ($invoice['discount_rate'] / 100);
|
||||
}
|
||||
|
||||
if ($invoice['discount_type'] === 'value') {
|
||||
$percentage_of_discount = $invoice['discount_rate'] / (invoice_total($invoice) + $invoice['discount_rate']);
|
||||
|
||||
$bagItem['total'] -= $bagItem['total'] * $percentage_of_discount;
|
||||
}
|
||||
|
||||
return $bagItem;
|
||||
});
|
||||
}
|
||||
|
||||
return $bag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $invoice
|
||||
* @param false $format
|
||||
* @return float|int|string
|
||||
*/
|
||||
function invoice_total_net($invoice, $format = false)
|
||||
function invoice_total($invoice)
|
||||
{
|
||||
$total = 0;
|
||||
|
||||
foreach ($invoice['items'] as $item) {
|
||||
$total += $item['amount'] * $item['price'];
|
||||
$total_without_tax = $item['amount'] * $item['price'];
|
||||
|
||||
if ($item['tax_rate']) {
|
||||
$total_without_tax += $total_without_tax * ($item['tax_rate'] / 100);
|
||||
}
|
||||
|
||||
$total += $total_without_tax;
|
||||
}
|
||||
|
||||
if ($format) {
|
||||
return Cashier::formatAmount(($total * 100), $invoice['currency'], 'cs');
|
||||
if ($invoice['discount_type']) {
|
||||
if ($invoice['discount_type'] === 'percent') {
|
||||
$total -= $total * ($invoice['discount_rate'] / 100);
|
||||
}
|
||||
|
||||
if ($invoice['discount_type'] === 'value') {
|
||||
$total -= $invoice['discount_rate'];
|
||||
}
|
||||
}
|
||||
|
||||
return $total;
|
||||
@@ -128,7 +185,7 @@ function invoice_total_net($invoice, $format = false)
|
||||
* @param false $format
|
||||
* @return float|int|string
|
||||
*/
|
||||
function invoice_total_tax($invoice, $format = false)
|
||||
function invoice_total_tax($invoice)
|
||||
{
|
||||
$total = 0;
|
||||
|
||||
@@ -136,20 +193,18 @@ function invoice_total_tax($invoice, $format = false)
|
||||
$total += ($item['amount'] * $item['price']) * ($item['tax_rate'] / 100);
|
||||
}
|
||||
|
||||
if ($format) {
|
||||
return Cashier::formatAmount(($total * 100), $invoice['currency'], 'cs');
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param $currency
|
||||
* @param string $currency
|
||||
* @param string $locale
|
||||
* @return string
|
||||
*/
|
||||
function format_to_currency($value, $currency = 'CZK', $locale = 'cs')
|
||||
{
|
||||
return Cashier::formatAmount(((int) $value * 100), $currency, $locale);
|
||||
$amount = round($value, 2) * 100;
|
||||
|
||||
return Cashier::formatAmount((int) $amount, $currency, $locale);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,8 @@ class Invoice extends Model
|
||||
'bag' => 'array',
|
||||
];
|
||||
|
||||
public function getMimetype() {
|
||||
public function getMimetype()
|
||||
{
|
||||
return 'pdf';
|
||||
}
|
||||
|
||||
|
||||
@@ -63,8 +63,7 @@ class Invoice extends Model
|
||||
$invoice->delivery_at = $invoice->created_at;
|
||||
$invoice->due_at = Carbon::parse($invoice->created_at)->addWeeks(2);
|
||||
|
||||
$invoice->total_discount = invoice_total_discount($invoice);
|
||||
$invoice->total_net = invoice_total_net($invoice);
|
||||
$invoice->total_net = invoice_total($invoice);
|
||||
$invoice->total_tax = invoice_total_tax($invoice);
|
||||
|
||||
$invoice->currency = 'CZK';
|
||||
|
||||
+1
-1
@@ -1,7 +1,6 @@
|
||||
<?php
|
||||
namespace App\Models;
|
||||
|
||||
use App\Notifications\ResetPassword;
|
||||
use App\Traits\Oasis;
|
||||
use ByteUnits\Metric;
|
||||
use Illuminate\Support\Str;
|
||||
@@ -10,6 +9,7 @@ use App\Services\HelperService;
|
||||
use App\Services\StripeService;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Kyslik\ColumnSortable\Sortable;
|
||||
use App\Notifications\ResetPassword;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
|
||||
Reference in New Issue
Block a user