From a079dc91576cff92756cb9d90f7026e1d0c97894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cihan=20=C5=9Eent=C3=BCrk?= Date: Tue, 31 Dec 2024 20:11:34 +0300 Subject: [PATCH] changed global discount calculate method --- app/Jobs/Document/CreateDocument.php | 5 +++ .../Document/CreateDocumentItemsAndTotals.php | 11 ++++++ app/Jobs/Document/UpdateDocument.php | 5 +++ app/Models/Document/Document.php | 22 ++++++++++++ app/Models/Document/DocumentTotal.php | 6 ++-- .../2024_12_29_000000_core_v3115.php | 34 +++++++++++++++++++ resources/assets/js/views/common/documents.js | 15 +++++--- .../documents/form/totals.blade.php | 5 +-- 8 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 database/migrations/2024_12_29_000000_core_v3115.php diff --git a/app/Jobs/Document/CreateDocument.php b/app/Jobs/Document/CreateDocument.php index c6add6eda..1e90560db 100644 --- a/app/Jobs/Document/CreateDocument.php +++ b/app/Jobs/Document/CreateDocument.php @@ -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 () { diff --git a/app/Jobs/Document/CreateDocumentItemsAndTotals.php b/app/Jobs/Document/CreateDocumentItemsAndTotals.php index eaa6411df..4b5771e9b 100644 --- a/app/Jobs/Document/CreateDocumentItemsAndTotals.php +++ b/app/Jobs/Document/CreateDocumentItemsAndTotals.php @@ -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]; } diff --git a/app/Jobs/Document/UpdateDocument.php b/app/Jobs/Document/UpdateDocument.php index ba000a3ca..2443d8dbb 100644 --- a/app/Jobs/Document/UpdateDocument.php +++ b/app/Jobs/Document/UpdateDocument.php @@ -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 () { diff --git a/app/Models/Document/Document.php b/app/Models/Document/Document.php index 264a0cdff..6a786f9ee 100644 --- a/app/Models/Document/Document.php +++ b/app/Models/Document/Document.php @@ -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. * diff --git a/app/Models/Document/DocumentTotal.php b/app/Models/Document/DocumentTotal.php index ae3a8590b..09592acdb 100644 --- a/app/Models/Document/DocumentTotal.php +++ b/app/Models/Document/DocumentTotal.php @@ -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') { diff --git a/database/migrations/2024_12_29_000000_core_v3115.php b/database/migrations/2024_12_29_000000_core_v3115.php new file mode 100644 index 000000000..02ec04d8c --- /dev/null +++ b/database/migrations/2024_12_29_000000_core_v3115.php @@ -0,0 +1,34 @@ +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'); + }); + } +}; diff --git a/resources/assets/js/views/common/documents.js b/resources/assets/js/views/common/documents.js index c9d27d890..ba7911480 100644 --- a/resources/assets/js/views/common/documents.js +++ b/resources/assets/js/views/common/documents.js @@ -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({ } }, }, -}); +}); \ No newline at end of file diff --git a/resources/views/components/documents/form/totals.blade.php b/resources/views/components/documents/form/totals.blade.php index 529d9eed3..35bb8c5ac 100644 --- a/resources/views/components/documents/form/totals.blade.php +++ b/resources/views/components/documents/form/totals.blade.php @@ -79,7 +79,7 @@ -
+
{{ trans('invoices.add_discount') }} @@ -125,7 +125,8 @@ />
- + + delete