comee_core 0.3.38 → 0.3.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f24bf85150707bf055d33beb60ab6255121975d0287b6dcf0a0f2c5301ee24dc
4
- data.tar.gz: 59f736afd6f9c4919a9123672d40f3aa2abda42f9ccc4c0ec50279f5e645b72e
3
+ metadata.gz: 7c5bd7ecb372cda637b779b831f195910cb1d8e7a6a4a7a9687d58f6e5063f34
4
+ data.tar.gz: 2b66ca4edc892cfacdd239973330ea4ac1ea10ed9c0c749ffa8a67f0e2751621
5
5
  SHA512:
6
- metadata.gz: 14f20b4731201268d259c432ed1bf8c1f949f04ea3be5aab30161b3205ecf8c2d5ca934aa35ea2f25f8498304820209b7f4d2059869c0ccc50172ecca2cd4927
7
- data.tar.gz: f42dbab7b458ff1d8aaeb7f87eea054698e619b4098d459b61c857b6cc30dccc52f010ecc899cff53dadca9a38683da9a939dad5a4b205ae03e3aea2c8781370
6
+ metadata.gz: 3b23ad79739cf108028d62e244e2cee673f6bca41142c29956eeb903c565ace12997baad341c75c9936f49f44607d28d339a5890dbc976f05fd47203f64a0ca9
7
+ data.tar.gz: a4ab86afb792d939cf67d7eb880d1ad5ed1680d7edb9db68e6353de5531fa23fc70405ed22b1bbc2bb53680002805de0b8673cf5b1532747d3d38f610dcf8e31
@@ -0,0 +1,54 @@
1
+ module Comee
2
+ module Core
3
+ class CreditNote < ApplicationRecord
4
+ before_validation :generate_credit_note_no, if: proc { |note| note.credit_note_no.nil? }
5
+
6
+ belongs_to :invoice
7
+ has_many :credit_note_items
8
+ has_many :credit_note_additional_items
9
+
10
+ enum :status, {draft: 0, approved: 1, issued: 2}
11
+
12
+ validates :credit_note_no, presence: true, uniqueness: true
13
+ validates :status, presence: true
14
+ validates :total_price, numericality: {greater_than_or_equal_to: 0, allow_nil: true}
15
+ validate :validate_total_price
16
+
17
+ def calculate_total_price
18
+ self.total_price = calculate_total
19
+ end
20
+
21
+ def calculate_total
22
+ items = CreditNoteItem.where(credit_note_id: id)
23
+ additionals = CreditNoteAdditionalItem.where(credit_note_id: id)
24
+ (items.sum(:total_price) + additionals.sum(:total_price)).round(2)
25
+ end
26
+
27
+ def generate_credit_note_no
28
+ year = Date.current.year.to_s
29
+ self.credit_note_no = Util.generate_number("CreditNote", "credit_note_no", year, "-", 100001)
30
+ end
31
+
32
+ def validate_total_price
33
+ return unless total_price && invoice
34
+
35
+ balance = invoice.total_price - invoice.amount_paid
36
+ errors.add(:total_price, "exceeds invoice unpaid amount") if total_price > balance
37
+ end
38
+
39
+ def self.ransackable_attributes(_auth_object = nil)
40
+ %w[
41
+ id
42
+ invoice_id
43
+ credit_note_no
44
+ created_at
45
+ status
46
+ ]
47
+ end
48
+
49
+ def self.ransackable_associations(_auth_object = nil)
50
+ ["invoice"]
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,19 @@
1
+ module Comee
2
+ module Core
3
+ class CreditNoteAdditionalItem < ApplicationRecord
4
+ before_save { self.total_price = unit_price * quantity }
5
+ after_save :update_credit_note
6
+ after_destroy :update_credit_note
7
+
8
+ belongs_to :credit_note
9
+
10
+ validates :description, presence: true
11
+ validates :quantity, :unit_price, presence: true, numericality: {greater_than: 0}
12
+
13
+ def update_credit_note
14
+ credit_note.calculate_total_price
15
+ credit_note.save!
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module Comee
2
+ module Core
3
+ class CreditNoteItem < ApplicationRecord
4
+ before_save { self.total_price = unit_price * quantity }
5
+ after_save :update_credit_note
6
+ after_destroy :update_credit_note
7
+
8
+ belongs_to :credit_note
9
+ belongs_to :invoice_item
10
+
11
+ validates :quantity, :unit_price, presence: true, numericality: {greater_than: 0}
12
+ validates :total_price, numericality: {greater_than_or_equal_to: 0, allow_nil: true}
13
+
14
+ def update_credit_note
15
+ credit_note.calculate_total_price
16
+ credit_note.save!
17
+ end
18
+ end
19
+ end
20
+ end
@@ -46,6 +46,11 @@ module Comee
46
46
  (items.sum(:total_price) + additionals.sum(:total_price)).round(2)
47
47
  end
48
48
 
49
+ def calculate_amount_paid
50
+ payments = Payment.where(invoice_id: id)
51
+ payments.sum(:amount)
52
+ end
53
+
49
54
  def self.ransackable_attributes(_auth_object = nil)
50
55
  %w[
51
56
  id
@@ -1,6 +1,7 @@
1
1
  module Comee
2
2
  module Core
3
3
  class Payment < ApplicationRecord
4
+ after_save :calculate_amount_paid
4
5
  belongs_to :invoice
5
6
  belongs_to :payment_deposit
6
7
 
@@ -24,6 +25,11 @@ module Comee
24
25
  error = "Amount exceeded. The maximum amount you can set is #{remaining}"
25
26
  errors.add(:base, error) if amount > remaining
26
27
  end
28
+
29
+ def calculate_amount_paid
30
+ invoice.amount_paid = invoice.calculate_amount_paid
31
+ invoice.save!
32
+ end
27
33
  end
28
34
  end
29
35
  end
@@ -0,0 +1,19 @@
1
+ class CreateComeeCoreCreditNotes < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :comee_core_credit_notes do |t|
4
+ t.string :credit_note_no, null: false
5
+ t.references :invoice,
6
+ null: false,
7
+ index: {name: "invoice_on_cccn_indx"},
8
+ foreign_key: {to_table: :comee_core_invoices}
9
+ t.float :total_price
10
+ t.float :vat, default: 0
11
+ t.integer :status, null: false, default: 0
12
+ t.date :date_issued
13
+ t.string :remark
14
+
15
+ t.timestamps
16
+ end
17
+ add_index :comee_core_credit_notes, :credit_note_no, unique: true
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ class CreateComeeCoreCreditNoteItems < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :comee_core_credit_note_items do |t|
4
+ t.references :credit_note,
5
+ null: false,
6
+ index: {name: "cn_on_cccni_indx"},
7
+ foreign_key: {to_table: :comee_core_credit_notes}
8
+ t.references :invoice_item,
9
+ null: false,
10
+ index: {name: "ii_on_cccni_indx"},
11
+ foreign_key: {to_table: :comee_core_invoice_items}
12
+ t.float :quantity, null: false
13
+ t.float :unit_price, null: false
14
+ t.float :total_price
15
+
16
+ t.timestamps
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ class CreateComeeCoreCreditNoteAdditionalItems < ActiveRecord::Migration[7.1]
2
+ def change
3
+ create_table :comee_core_credit_note_additional_items do |t|
4
+ t.references :credit_note,
5
+ null: false,
6
+ index: {name: "cn_on_cccnai_indx"},
7
+ foreign_key: {to_table: :comee_core_credit_notes}
8
+ t.string :description, null: false
9
+ t.float :quantity, null: false
10
+ t.float :unit_price, null: false
11
+ t.float :total_price, null: false
12
+
13
+ t.timestamps
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  module Comee
2
2
  module Core
3
- VERSION = "0.3.38".freeze
3
+ VERSION = "0.3.39".freeze
4
4
  end
5
5
  end
@@ -0,0 +1,13 @@
1
+ FactoryBot.define do
2
+ factory :credit_note_additional_item, class: "Comee::Core::CreditNoteAdditionalItem" do
3
+ credit_note
4
+ description { Faker::Name.name }
5
+ quantity { 1.5 }
6
+ unit_price { 1.5 }
7
+ total_price { quantity * unit_price }
8
+
9
+ after(:build) do |item, _|
10
+ item.credit_note.invoice.total_price = 100
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ FactoryBot.define do
2
+ factory :credit_note_item, class: "Comee::Core::CreditNoteItem" do
3
+ transient do
4
+ invoice { create(:invoice) }
5
+ end
6
+ # association :credit_note, invoice: :invoice
7
+ # association :invoice_item, invoice: :invoice
8
+ quantity { 10 }
9
+ unit_price { 2 }
10
+ total_price { quantity * unit_price }
11
+
12
+ after(:build) do |item, evaluator|
13
+ item.credit_note ||= create(:credit_note, invoice: evaluator.invoice)
14
+ item.invoice_item ||= create(:invoice_item, invoice: evaluator.invoice)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ FactoryBot.define do
2
+ factory :credit_note, class: "Comee::Core::CreditNote" do
3
+ credit_note_no { nil }
4
+ invoice
5
+ total_price { 0 }
6
+ vat { 0 }
7
+ status { Comee::Core::CreditNote.statuses[:draft] }
8
+ date_issued { Date.current }
9
+ remark { Faker::Lorem.sentence }
10
+ end
11
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comee_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.38
4
+ version: 0.3.39
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-25 00:00:00.000000000 Z
11
+ date: 2024-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -337,6 +337,9 @@ files:
337
337
  - app/models/comee/core/client_price.rb
338
338
  - app/models/comee/core/client_warehouse.rb
339
339
  - app/models/comee/core/contact.rb
340
+ - app/models/comee/core/credit_note.rb
341
+ - app/models/comee/core/credit_note_additional_item.rb
342
+ - app/models/comee/core/credit_note_item.rb
340
343
  - app/models/comee/core/currency.rb
341
344
  - app/models/comee/core/customer_order.rb
342
345
  - app/models/comee/core/customer_order_item.rb
@@ -528,6 +531,9 @@ files:
528
531
  - db/migrate/20240718103456_make_delivery_note_no_and_goods_issue_date_optional.rb
529
532
  - db/migrate/20240720091730_add_use_alias_field_to_line_items.rb
530
533
  - db/migrate/20240721035647_add_invoice_address_to_invoice.rb
534
+ - db/migrate/20240731075328_create_comee_core_credit_notes.rb
535
+ - db/migrate/20240731080746_create_comee_core_credit_note_items.rb
536
+ - db/migrate/20240731081502_create_comee_core_credit_note_additional_items.rb
531
537
  - lib/comee/core.rb
532
538
  - lib/comee/core/engine.rb
533
539
  - lib/comee/core/version.rb
@@ -545,6 +551,9 @@ files:
545
551
  - spec/factories/comee/core/client_warehouses.rb
546
552
  - spec/factories/comee/core/clients.rb
547
553
  - spec/factories/comee/core/contacts.rb
554
+ - spec/factories/comee/core/credit_note_additional_items.rb
555
+ - spec/factories/comee/core/credit_note_items.rb
556
+ - spec/factories/comee/core/credit_notes.rb
548
557
  - spec/factories/comee/core/currencies.rb
549
558
  - spec/factories/comee/core/customer_order_items.rb
550
559
  - spec/factories/comee/core/customer_orders.rb