cats_core 1.4.0 → 1.4.1

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: b1563c8d3250c1cda415de5081c46f445a82f593c727be676c3f9a7041f28fa7
4
- data.tar.gz: b614991a9f6a2a4046c7c4e54af584d59a33478c62b5a0c6dee77ddbbdad2e6b
3
+ metadata.gz: affaa1b4a210fc6234d49f105efd3a0e197b8d9e6c2ad7e00ae2dbefd23cfe31
4
+ data.tar.gz: 608f4585d6c34f68a62a7bcfc187d495ac21a0fa528f905bcda7044801ab4c1f
5
5
  SHA512:
6
- metadata.gz: ff00ffac606dfa2136aaaed05a6825238b5ae38f737f4a3790db2b94193f0108b6d394097c0828339af8aab226acf871d65ddefa68c54ad929a47e7069d9d49b
7
- data.tar.gz: fd1b163787f8fc21ba49f79929f027143f1a45d74425518536d9adc4369eb33ea6ab9b96a30fb34b7cba6e9f8ba11e2f9f191acd83200debf91589cc9be490e3
6
+ metadata.gz: 68b8a081e2ee0e3ad7edb1876b76a1cb2d2c0b8836de2af209acb01de699d004c0fd2f74c0357a27857fd287f249f3338950debe88c15ee2b742cfe72f4d62cd
7
+ data.tar.gz: 4b5c89c2de473ec47255835b1862ddaa42f44b4a99d5410bd4c63515b73bef77d1ef92328a75358a00dd29992afa2bcf2c582cbde84a5d63de2833d10944aada
@@ -19,6 +19,13 @@ module Cats
19
19
  render json: { success: true, data: serialize(commodities) }
20
20
  end
21
21
 
22
+ def dispatch_stacks
23
+ service = StackService.new
24
+ dispatch = Dispatch.find(params[:id])
25
+ stacks = service.dispatch_stacks(dispatch)
26
+ render json: { success: true, data: serialize(stacks) }
27
+ end
28
+
22
29
  def receipt_stacks
23
30
  service = StackService.new
24
31
  receipt = Receipt.find(params[:id])
@@ -14,6 +14,7 @@ module Cats
14
14
 
15
15
  validates :reference_no, presence: true, uniqueness: true
16
16
  validates :status, inclusion: { in: STATUSES }
17
+ validate :validate_dispatchable, on: :create
17
18
 
18
19
  delegate(:batch_no, :name, :quantity, to: :commodity, prefix: true)
19
20
  delegate(:request_reference, to: :dispatchable, allow_nil: true)
@@ -23,14 +24,40 @@ module Cats
23
24
  plan.upstream = !plan.dispatchable.nil? && plan.dispatchable_type == RhnRequest.name
24
25
  end
25
26
 
27
+ after_save do |plan|
28
+ plan.dispatchable&.reserve if status == DRAFT
29
+ end
30
+
31
+ def validate_dispatchable
32
+ # Check if dispatchable is already reserved and raise error if so.
33
+ return unless dispatchable
34
+
35
+ return if dispatchable.status == RhnRequest::APPROVED
36
+
37
+ errors.add(:dispatchable, 'is already reserved.') if id.nil?
38
+ end
39
+
26
40
  def approve
27
41
  raise(StandardError, 'Dispatch plan already approved.') if status == APPROVED
28
42
 
29
43
  raise(StandardError, 'Dispatch plan is empty.') if dispatch_plan_items.count.zero?
30
44
 
45
+ # Raise error if the total dispatch plan quantity is not equal to the
46
+ # dispatchable quantity
47
+ if dispatchable && dispatchable.quantity != quantity
48
+ raise(StandardError, 'Requested quantity and plan quantity do not match.')
49
+ end
50
+
31
51
  self.status = APPROVED
52
+ dispatchable&.allocate
32
53
  save!
33
54
  end
55
+
56
+ # A method which returns the total quantity in the dispatch plan
57
+ # based on dispatch plan item quantities.
58
+ def quantity
59
+ dispatch_plan_items.sum(:quantity)
60
+ end
34
61
  end
35
62
  end
36
63
  end
@@ -18,6 +18,7 @@ module Cats
18
18
  has_many :plan_item_details, through: :plan_items
19
19
 
20
20
  validates :reference_no, :year, :status, presence: true
21
+ validates :total_days, :rounds, presence: true, numericality: { greater_than: 0 }
21
22
  validates :season, presence: true, inclusion: { in: SEASONS }
22
23
  validates :reference_no, uniqueness: true
23
24
  validates :status, inclusion: { in: STATUSES }
@@ -5,8 +5,9 @@ module Cats
5
5
 
6
6
  DRAFT = 'Draft'.freeze
7
7
  APPROVED = 'Approved'.freeze
8
+ RESERVED = 'Reserved'.freeze
8
9
  ALLOCATED = 'Allocated'.freeze
9
- STATUSES = [DRAFT, APPROVED, ALLOCATED].freeze
10
+ STATUSES = [DRAFT, APPROVED, RESERVED, ALLOCATED].freeze
10
11
 
11
12
  belongs_to :commodity
12
13
 
@@ -43,11 +44,22 @@ module Cats
43
44
  end
44
45
 
45
46
  def approve
47
+ raise(StandardError, 'Request is not in draft state.') unless status == DRAFT
48
+
46
49
  self.status = APPROVED
47
50
  save!
48
51
  end
49
52
 
53
+ def reserve
54
+ raise(StandardError, 'Request is not approved.') unless status == APPROVED
55
+
56
+ self.status = RESERVED
57
+ save!
58
+ end
59
+
50
60
  def allocate
61
+ raise(StandardError, 'Request is not reserved.') unless status == RESERVED
62
+
51
63
  self.status = ALLOCATED
52
64
  save!
53
65
  end
@@ -23,7 +23,6 @@ module Cats
23
23
  end
24
24
  HubAuthorization.insert_all(authorizations)
25
25
  plan.dispatch_plan_items.update_all(status: DispatchPlanItem::SOURCE_AUTHORIZED)
26
- plan.dispatchable.allocate
27
26
  end
28
27
  send_notification(plan)
29
28
  plan
@@ -33,6 +32,7 @@ module Cats
33
32
  plan = Cats::Core::DispatchPlan.new(params)
34
33
  plan.prepared_by = user
35
34
  plan.save!
35
+
36
36
  plan
37
37
  rescue ActiveRecord::RecordInvalid
38
38
  raise(StandardError, plan.errors.full_messages[0])
@@ -1,7 +1,7 @@
1
1
  module Cats
2
2
  module Core
3
3
  class DispatchService
4
- def search(user, status)
4
+ def search(user, status, authorized: false)
5
5
  details = user.details
6
6
 
7
7
  unless details['stores'] || details['warehouse'] || details['hub']
@@ -10,18 +10,25 @@ module Cats
10
10
 
11
11
  # Get user's hub
12
12
  hub = if details['stores']
13
- Cats::Core::Store.find(details['stores'][0]).warehouse.parent
13
+ Store.find(details['stores'][0]).warehouse.parent
14
14
  elsif details['warehouse']
15
- Cats::Core::Location.find(details['warehouse']).parent
15
+ Location.find(details['warehouse']).parent
16
16
  else
17
- Cats::Core::Location.find(details['hub'])
17
+ Location.find(details['hub'])
18
18
  end
19
19
 
20
- Cats::Core::Dispatch.joins(:dispatch_plan_item)
21
- .where(
22
- dispatch_plan_item: { destination: hub },
23
- dispatch_status: status
24
- )
20
+ if authorized
21
+ return Dispatch.joins(:dispatch_plan_item)
22
+ .where(
23
+ dispatch_plan_item: { destination: hub, status: DispatchPlanItem::AUTHORIZED },
24
+ dispatch_status: status
25
+ )
26
+ end
27
+ Dispatch.joins(:dispatch_plan_item)
28
+ .where(
29
+ dispatch_plan_item: { destination: hub },
30
+ dispatch_status: status
31
+ )
25
32
  end
26
33
 
27
34
  def start(dispatch)
@@ -49,10 +49,19 @@ module Cats
49
49
  def receipt_stacks(receipt)
50
50
  location = receipt.dispatch.dispatch_plan_item.destination
51
51
  warehouses = location.children
52
- stores = Cats::Core::Store.where(warehouse: warehouses)
52
+ stores = Store.where(warehouse: warehouses)
53
53
 
54
54
  commodity = receipt.dispatch.dispatch_plan_item.dispatch_plan.commodity
55
- Cats::Core::Stack.where(commodity: commodity, store: stores)
55
+ Stack.where(commodity: commodity, store: stores)
56
+ end
57
+
58
+ def dispatch_stacks(dispatch)
59
+ location = dispatch.dispatch_plan_item.source
60
+ warehouses = location.children
61
+ stores = Store.where(warehouse: warehouses)
62
+
63
+ commodity = dispatch.dispatch_plan_item.dispatch_plan.commodity
64
+ Stack.where(commodity: commodity, store: stores)
56
65
  end
57
66
  end
58
67
  end
data/config/routes.rb CHANGED
@@ -137,5 +137,6 @@ Cats::Core::Engine.routes.draw do
137
137
  post '/stores/:id/stacks', controller: :stacks, action: :filter
138
138
  get '/stacks/commodity_for_location', controller: :stacks, action: :commodity_for_location, as: :commodity_for_location
139
139
  get '/receipts/:id/stacks', controller: :stacks, action: :receipt_stacks, as: :receipt_stacks
140
+ get '/dispatches/:id/stacks', controller: :stacks, action: :dispatch_stacks, as: :dispatch_stacks
140
141
  resources :stacks, only: %i[show index create update]
141
142
  end
@@ -5,6 +5,9 @@ class CreateCatsCorePlans < ActiveRecord::Migration[6.1]
5
5
  t.integer :year, null: false
6
6
  t.string :season, null: false
7
7
  t.string :status, null: false, default: 'Draft'
8
+ t.date :start_date
9
+ t.integer :total_days, null: false, default: 180
10
+ t.integer :rounds, null: false
8
11
 
9
12
  t.references :program,
10
13
  null: false,
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.4.0'.freeze
3
+ VERSION = '1.4.1'.freeze
4
4
  end
5
5
  end
@@ -3,6 +3,8 @@ FactoryBot.define do
3
3
  reference_no { FFaker::Name.name }
4
4
  year { 1 }
5
5
  season { Cats::Core::Plan::BELG }
6
+ total_days { 180 }
7
+ rounds { 6 }
6
8
  status { Cats::Core::Plan::DRAFT }
7
9
  program
8
10
  ration
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cats_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-24 00:00:00.000000000 Z
11
+ date: 2022-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers