comee_core 0.1.26 → 0.1.28

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 211383a0d0a72cf1a2f0c988bf6540171bbb80b2c4439da601c2ed5a7cf1e45e
4
- data.tar.gz: 5edd9daa10f399390c1861f590a549500414facb5983aa8bc16b9f60a35412c8
3
+ metadata.gz: 4584d0f6f728f6ab957e59500e8a8af4564e0922558df83cb7562e9d9b094603
4
+ data.tar.gz: 5404312320bbcf4d482e5261a6c960dc1418d1e9051c1cfa810419cd872fef7b
5
5
  SHA512:
6
- metadata.gz: 977501186b9496d53b6cbf15056245516e145894d5c75b78cce260c406ff5d6b37c1b004e6d55369f48abbf7bbef85cc17addf48c54afcd13f8ca4c6f1fff034
7
- data.tar.gz: d2caed2d5cac679e9e9c5b5d6969ce25b6a117bf2befbf107dd47146f6cf8f4b15149836a29f826dc0d01b74426a073ba10434dec16935318d09405abc43e527
6
+ metadata.gz: 86745d97fac73d6fc66de542291014f4c1b6b57fb61bac5c8c31f523b7be03728b3109fad94750100ad40c6cbdca0305e448baaa5d399c52208ddb14ff2921a0
7
+ data.tar.gz: d4013f260e88c21628dc3eb1434718e949f3132e77b77d1daa58554fe6f33339da882772c43e6f6efc45d5d07230b0255b08b4fffa96cf7a91a179b0f6f6ab9c
@@ -16,6 +16,51 @@ module Comee
16
16
  def self.ransackable_associations(_auth_object = nil)
17
17
  []
18
18
  end
19
+
20
+ def submit
21
+ self.status = WarehouseShipment.statuses[:submitted]
22
+ save!
23
+ self
24
+ end
25
+
26
+ def acknowledge
27
+ error = "Warehouse shipment should have a `submitted` status before acknowledgement."
28
+ raise(StandardError, error) unless WarehouseShipment.statuses[status] == WarehouseShipment.statuses[:submitted]
29
+
30
+ self.status = WarehouseShipment.statuses[:acknowledged]
31
+ save!
32
+ items = warehouse_shipment_items.includes(:sales_order_item)
33
+ return self unless items.count.positive?
34
+
35
+ updates = items.map do |item|
36
+ order_item = item.sales_order_item
37
+ {
38
+ id: order_item.id,
39
+ sales_order_id: order_item.sales_order_id,
40
+ product_id: order_item.product_id,
41
+ quantity: order_item.quantity,
42
+ quantity_en_route: order_item.quantity_en_route + item.quantity,
43
+ quantity_remaining: order_item.quantity_remaining - item.quantity,
44
+ price: order_item.price,
45
+ delivery_date: order_item.delivery_date
46
+ }
47
+ end
48
+ SalesOrderItem.upsert_all(updates)
49
+ self
50
+ end
51
+
52
+ def deliver
53
+ error = "Warehouse shipment should have an `acknowledged` status before delivery."
54
+ raise(StandardError, error) unless WarehouseShipment.statuses[status] == WarehouseShipment.statuses[:acknowledged]
55
+
56
+ self.status = WarehouseShipment.statuses[:delivered]
57
+ save!
58
+ order_items = warehouse_shipment_items.includes(:sales_order_item).map(&:sales_order_item)
59
+ return self unless order_items.count.positive?
60
+
61
+ SalesOrderItem.where(id: order_items.map(&:id)).update_all(quantity_en_route: 0)
62
+ self
63
+ end
19
64
  end
20
65
  end
21
66
  end
@@ -1,25 +1,36 @@
1
1
  module Comee
2
2
  module Core
3
3
  class WarehouseShipmentItem < ApplicationRecord
4
- after_create :update_inventory_and_sales_order_item
4
+ before_save :update_inventory
5
5
 
6
6
  belongs_to :warehouse_shipment
7
7
  belongs_to :sales_order_item
8
8
 
9
9
  validates :quantity, presence: true
10
+ validate :validate_inventory_quantity
10
11
 
11
- def update_inventory_and_sales_order_item
12
- update_inventory(sales_order_item.product_id, quantity)
12
+ def validate_inventory_quantity
13
+ return unless quantity && sales_order_item
13
14
 
14
- sales_order_item.update_quantity_en_route(quantity)
15
+ inventory = Inventory.find_by(product_id: sales_order_item.product_id)
16
+ unless inventory
17
+ errors.add(:base, "Inventory entry not found for product `#{sales_order_item.product.name}`.")
18
+ return
19
+ end
20
+
21
+ old_quantity = quantity_was || 0
22
+ diff = inventory.quantity - (quantity - old_quantity)
23
+ errors.add(:quantity, "exceeds available quantity in inventory by #{diff.abs}.") if diff.negative?
15
24
  end
16
25
 
17
- def update_inventory(product_id, quantity_change)
18
- inventory = Inventory.find_by(product_id: product_id)
19
- return unless inventory
26
+ def update_inventory
27
+ inventory = Inventory.find_by(product_id: sales_order_item.product_id)
28
+ return unless quantity_changed?
20
29
 
21
- inventory.quantity -= quantity_change
22
- inventory.save
30
+ old_quantity = quantity_was || 0
31
+ diff = quantity - old_quantity
32
+ inventory.quantity -= diff
33
+ inventory.save!
23
34
  end
24
35
  end
25
36
  end
@@ -1,5 +1,5 @@
1
1
  module Comee
2
2
  module Core
3
- VERSION = "0.1.26".freeze
3
+ VERSION = "0.1.28".freeze
4
4
  end
5
5
  end
@@ -1,6 +1,6 @@
1
1
  FactoryBot.define do
2
2
  factory :inventory, class: "Comee::Core::Inventory" do
3
- quantity { 10 }
3
+ quantity { 200 }
4
4
  product
5
5
  end
6
6
  end
@@ -4,8 +4,14 @@ FactoryBot.define do
4
4
  product
5
5
  quantity { 100 }
6
6
  quantity_en_route { 0 }
7
- quantity_remaining { 100 }
7
+ quantity_remaining { quantity }
8
8
  price { 50 }
9
9
  delivery_date { Date.current.advance(months: 1) }
10
+
11
+ trait :with_inventory do
12
+ after(:create) do |item|
13
+ create(:inventory, product: item.product, quantity: item.quantity + 100)
14
+ end
15
+ end
10
16
  end
11
17
  end
@@ -1,7 +1,7 @@
1
1
  FactoryBot.define do
2
2
  factory :warehouse_shipment_item, class: "Comee::Core::WarehouseShipmentItem" do
3
+ association :sales_order_item, :with_inventory
3
4
  warehouse_shipment
4
- sales_order_item
5
5
  quantity { 10 }
6
6
  end
7
7
  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.1.26
4
+ version: 0.1.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-25 00:00:00.000000000 Z
11
+ date: 2023-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: rails
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 7.0.6
75
+ version: 7.1.0
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 7.0.6
82
+ version: 7.1.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: ransack
85
85
  requirement: !ruby/object:Gem::Requirement