changed global discount calculate method

This commit is contained in:
Cihan Şentürk 2024-12-31 20:11:34 +03:00
parent 9e2af48132
commit a079dc9157
8 changed files with 94 additions and 9 deletions

View File

@ -25,6 +25,11 @@ class CreateDocument extends Job implements HasOwner, HasSource, ShouldCreate
$this->request['amount'] = 0;
}
// Disable this lines for global discount issue fixed ( https://github.com/akaunting/akaunting/issues/2797 )
if (! empty($this->request['discount'])) {
$this->request['discount_rate'] = $this->request['discount'];
}
event(new DocumentCreating($this->request));
\DB::transaction(function () {

View File

@ -173,6 +173,7 @@ class CreateDocumentItemsAndTotals extends Job implements HasOwner, HasSource, S
foreach ((array) $this->request['items'] as $key => $item) {
$item['global_discount'] = 0;
/* // Disable this lines for global discount issue fixed ( https://github.com/akaunting/akaunting/issues/2797 )
if (! empty($this->request['discount'])) {
if (isset($for_fixed_discount)) {
$item['global_discount'] = ($for_fixed_discount[$key] / ($for_fixed_discount['total'] / 100)) * ($this->request['discount'] / 100);
@ -182,6 +183,7 @@ class CreateDocumentItemsAndTotals extends Job implements HasOwner, HasSource, S
$item['global_discount_type'] = $this->request['discount_type'];
}
}
*/
$item['created_from'] = $this->request['created_from'];
$item['created_by'] = $this->request['created_by'];
@ -244,6 +246,15 @@ class CreateDocumentItemsAndTotals extends Job implements HasOwner, HasSource, S
}
}
// Disable this lines for global discount issue fixed ( https://github.com/akaunting/akaunting/issues/2797 )
if (! empty($this->request['discount'])) {
if ($this->request['discount_type'] === 'percentage') {
$actual_total -= ($sub_total * ($this->request['discount'] / 100));
} else {
$actual_total -= $this->request['discount'];
}
}
return [$sub_total, $actual_total, $discount_amount_total, $taxes];
}

View File

@ -22,6 +22,11 @@ class UpdateDocument extends Job implements ShouldUpdate
$this->request['amount'] = 0;
}
// Disable this lines for global discount issue fixed ( https://github.com/akaunting/akaunting/issues/2797 )
if (! empty($this->request['discount'])) {
$this->request['discount_rate'] = $this->request['discount'];
}
event(new DocumentUpdating($this->model, $this->request));
\DB::transaction(function () {

View File

@ -43,6 +43,8 @@ class Document extends Model
'amount',
'currency_code',
'currency_rate',
'discount_type',
'discount_rate',
'category_id',
'contact_id',
'contact_name',
@ -311,6 +313,26 @@ class Document extends Model
}
}
/**
* Get the discount percentage.
*
* @return string
*/
public function getDiscountRateAttribute($value)
{
return $value ?? 0;
}
/**
* Get the discount percentage.
*
* @return string
*/
public function getDiscountTypeAttribute($value)
{
return $value ?? 'percentage';
}
/**
* Get the discount percentage.
*

View File

@ -70,20 +70,20 @@ class DocumentTotal extends Model
switch ($this->code) {
case 'discount':
$title = trans($title);
$percent = $this->document->discount;
$percent = ($this->document->discount_rate && $this->document->discount_type == 'percentage') ? $this->document->discount_rate : 0;
break;
case 'tax':
$tax = Tax::where('name', $title)->first();
if (!empty($tax->rate)) {
if (! empty($tax->rate)) {
$percent = $tax->rate;
}
break;
}
if (!empty($percent)) {
if (! empty($percent)) {
$title .= ' (';
if (setting('localisation.percent_position', 'after') === 'after') {

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('documents', function (Blueprint $table) {
$table->string('discount_type')->nullable()->after('currency_rate');
$table->double('discount_rate', 15, 4)->nullable()->after('discount_type');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('documents', function (Blueprint $table) {
$table->dropColumn('discount_type');
$table->dropColumn('discount_rate');
});
}
};

View File

@ -87,8 +87,6 @@ const app = new Vue({
},
mounted() {
this.form.discount_type = 'percentage';
if ((document.getElementById('items') != null) && (document.getElementById('items').rows)) {
this.colspan = document.getElementById("items").rows[0].cells.length - 1;
}
@ -751,7 +749,7 @@ const app = new Vue({
},
onAddDiscount() {
this.show_discount = !this.show_discount;
this.show_discount = ! this.show_discount;
if (this.show_discount) {
this.show_discount_text = false;
@ -764,6 +762,10 @@ const app = new Vue({
this.show_discount_text = true;
this.discount = false;
this.delete_discount = false;
this.form.discount = 0;
this.onCalculateTotal();
},
onDeleteTax(item_index, tax_index) {
@ -1017,6 +1019,11 @@ const app = new Vue({
this.onEmailViaTemplate(email_route, email_template);
}
// This line added edit document has discount show discount area
if (this.form.discount > 0) {
this.onAddDiscount();
}
this.page_loaded = true;
},
@ -1054,4 +1061,4 @@ const app = new Vue({
}
},
},
});
});

View File

@ -79,7 +79,7 @@
<td class="border-t-0 py-0"></td>
<td class="ltr:text-right rtl:text-left border-t-0 border-r-0 border-b-0 align-middle py-0 pr-0">
<div v-if="show_discount_text" v-if="!totals.discount_text" @click="onAddDiscount()">
<div v-if="show_discount_text" @click="onAddDiscount()">
<x-button.hover color="to-purple">
{{ trans('invoices.add_discount') }}
</x-button.hover>
@ -125,7 +125,8 @@
/>
</div>
<x-form.input.hidden name="discount" v-model="form.discount" />
<x-form.input.hidden name="discount_type" value="{{ $document->discount_type ?? 'percentage' }}" v-model="form.discount_type" />
<x-form.input.hidden name="discount" value="{{ $document->discount_rate ?? 0 }}" v-model="form.discount" />
<span v-if="delete_discount" @click="onRemoveDiscountArea()" class="material-icons-outlined absolute w-6 h-7 flex justify-center ltr:-right-10 rtl:-left-10 top-2 text-lg text-gray-300 rounded-lg cursor-pointer hover:bg-gray-100 hover:text-gray-500">delete</span>
</td>