cats_core 1.1.0 → 1.1.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: ebf6bb336bcf7b1fa89d734c18444e8f41b586c480a24b675fb62d5a8e3d5528
4
- data.tar.gz: 6a5adfade3042c981cc482f336393cd51265a636e56b2ae91b5f4c1d8193840a
3
+ metadata.gz: e1410fa75e3f9e4eb96e4c9df8cbd6bb358bb56e4cce6ef42b095aff3b6331b1
4
+ data.tar.gz: c1529759ffa5ba650bd31df10ae302943afc14e876fff0710a35144cf0841981
5
5
  SHA512:
6
- metadata.gz: 5e3075ca71388f92b600ebfeb4d951e003b200987683a5de62d7138107d14efbd6c94a11425c1770e650ecf5facdcefd8ca90de71bb97d5b1685a9d787ef7ead
7
- data.tar.gz: ecd7c5c357ea9e8a05c06523cd821a8c10fd21611e5cbc27390fefc8cec5aa54ea840175eb5c8419f484654bdb5a7918d2d551c36de242599e9b41dfa32377d8
6
+ metadata.gz: 9b31f50e3f08e9a65c1da38520f3afef988613654e5250607e48761274951d3be4975e6b47c704c65ad6211d03081ab1c60b3f6ae1eea6fae191d498bd021110
7
+ data.tar.gz: 4046501c4c360769d9bc23628f4e067b8c7b55d95f898d57fda4f202f75ddc73fa5d5a60e5f163cf37ab340024b12a28065bb80f8be9eb5f5fb64d81151e8863
@@ -3,7 +3,8 @@ module Cats
3
3
  class DispatchesController < ApplicationController
4
4
  include Common
5
5
 
6
- before_action :set_service, only: %i[create_receipt_authorization approve start]
6
+ before_action :set_service, only: %i[create_receipt_authorization approve start search confirm]
7
+ before_action :set_dispatch, only: %i[approve start confirm]
7
8
 
8
9
  def index
9
10
  dispatches = Cats::Core::Dispatch.where(allocation_item_id: params[:id])
@@ -12,6 +13,7 @@ module Cats
12
13
 
13
14
  def create
14
15
  dispatch = Cats::Core::Dispatch.new(model_params)
16
+ dispatch.dispatch_status = Cats::Core::Dispatch::DRAFT
15
17
  dispatch.prepared_by = current_user
16
18
  if dispatch.save
17
19
  render json: { success: true, data: serialize(dispatch) }, status: :created
@@ -29,30 +31,44 @@ module Cats
29
31
  end
30
32
 
31
33
  def approve
32
- dispatch = Cats::Core::Dispatch.find(params[:id])
33
- dispatch = @service.approve(dispatch)
34
+ dispatch = @service.approve(@dispatch)
34
35
  render json: { success: true, data: serialize(dispatch) }
35
36
  rescue StandardError => e
36
37
  render json: { success: false, error: e.message }
37
38
  end
38
39
 
39
40
  def start
40
- dispatch = Cats::Core::Dispatch.find(params[:id])
41
- dispatch = @service.start(dispatch)
41
+ dispatch = @service.start(@dispatch)
42
42
  render json: { success: true, data: serialize(dispatch) }
43
43
  rescue StandardError => e
44
44
  render json: { success: false, error: e.message }
45
45
  end
46
46
 
47
+ def confirm
48
+ dispatch = @service.confirm(@dispatch)
49
+ render json: { success: true, data: serialize(dispatch) }
50
+ rescue StandardError => e
51
+ render json: { success: false, error: e.message }
52
+ end
53
+
54
+ def search
55
+ data = @service.search(current_user)
56
+ render json: { success: true, data: serialize(data) }
57
+ end
58
+
47
59
  private
48
60
 
49
61
  def set_service
50
62
  @service = DispatchService.new
51
63
  end
52
64
 
65
+ def set_dispatch
66
+ @dispatch = Cats::Core::Dispatch.find(params[:id])
67
+ end
68
+
53
69
  def model_params
54
70
  params.require(:payload).permit(:reference_no, :allocation_item_id, :transporter_id, :plate_no, :driver_name,
55
- :driver_phone, :quantity, :remark, :dispatch_status)
71
+ :driver_phone, :quantity, :remark)
56
72
  end
57
73
  end
58
74
  end
@@ -0,0 +1,18 @@
1
+ module Cats
2
+ module Core
3
+ class ReceiptsController < ApplicationController
4
+ include Common
5
+
6
+ def index
7
+ receipts = Cats::Core::Receipt.where(dispatch_id: params[:id])
8
+ render json: { success: true, data: serialize(receipts) }
9
+ end
10
+
11
+ private
12
+
13
+ def model_params
14
+ params.require(:payload).permit(:dispatch_id, :quantity, :commodity_status, :status, :remark, :prepared_by_id)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -13,6 +13,7 @@ module Cats
13
13
  belongs_to :prepared_by, class_name: 'Cats::Core::User'
14
14
  belongs_to :transporter
15
15
  belongs_to :allocation_item
16
+ has_many :receipts
16
17
 
17
18
  validates :reference_no, :plate_no, :driver_name, :driver_phone, :quantity, :commodity_status, presence: true
18
19
  validates :dispatch_status, presence: true, inclusion: { in: DISPATCH_STATUSES }
@@ -3,9 +3,8 @@ module Cats
3
3
  class Receipt < ApplicationRecord
4
4
  # Receipt status
5
5
  DRAFT = 'Draft'.freeze
6
- ACCEPTED = 'Accepted'.freeze
7
- DECLINED = 'Declined'.freeze
8
- RECEIPT_STATUSES = [DRAFT, ACCEPTED, DECLINED].freeze
6
+ CONFIRMED = 'Confirmed'.freeze
7
+ RECEIPT_STATUSES = [DRAFT, CONFIRMED].freeze
9
8
 
10
9
  belongs_to :dispatch
11
10
  belongs_to :prepared_by, class_name: 'Cats::Core::User'
@@ -14,6 +13,28 @@ module Cats
14
13
  validates :quantity, numericality: { greater_than: 0 }
15
14
  validates :status, inclusion: { in: RECEIPT_STATUSES }
16
15
  validates :commodity_status, inclusion: { in: Cats::Core::Commodity::COMMODITY_STATUSES }
16
+ validate :validate_dispatch_status, :validate_total_quantity
17
+
18
+ delegate(:reference_no, to: :dispatch, prefix: true)
19
+ delegate(:email, to: :prepared_by, prefix: true)
20
+
21
+ def validate_dispatch_status
22
+ return unless dispatch
23
+
24
+ return if dispatch.dispatch_status == Cats::Core::Dispatch::STARTED
25
+
26
+ errors.add(:dispatch, 'Dispatch should be in "Started" state.')
27
+ end
28
+
29
+ def validate_total_quantity
30
+ return unless dispatch && quantity
31
+
32
+ received = Cats::Core::Receipt.where(dispatch: dispatch).sum(:quantity)
33
+ remaining = dispatch.quantity - received
34
+ return if quantity <= remaining
35
+
36
+ errors.add(:quantity, "Total receipt quantity is higher than dispatch quantity (Max = #{remaining}).")
37
+ end
17
38
  end
18
39
  end
19
40
  end
@@ -0,0 +1,4 @@
1
+ class ReceiptSerializer < ActiveModel::Serializer
2
+ attributes :id, :dispatch_id, :dispatch_reference_no, :commodity_status, :quantity, :status, :remark,
3
+ :prepared_by_id, :prepared_by_email
4
+ end
@@ -40,6 +40,53 @@ module Cats
40
40
  dispatch
41
41
  end
42
42
 
43
+ def search(user)
44
+ details = user.details
45
+
46
+ unless details['stores'] || details['warehouse'] || details['hub']
47
+ raise(StandardError, 'User does not have associated location.')
48
+ end
49
+
50
+ if details['stores']
51
+ warehouse = Cats::Core::Store.find(details['stores'][0]).warehouse
52
+ hub = warehouse.parent
53
+
54
+ dispatches = Cats::Core::Dispatch.joins(:allocation_item)
55
+ .where(
56
+ allocation_item: { destination: hub },
57
+ dispatch_status: Cats::Core::Dispatch::STARTED
58
+ ).or(
59
+ Cats::Core::Dispatch.joins(:allocation_item)
60
+ .where(
61
+ allocation_item: { destination: warehouse },
62
+ dispatch_status: Cats::Core::Dispatch::STARTED
63
+ )
64
+ )
65
+ elsif details['warehouse']
66
+ warehouse = Cats::Core::Location.find(details['warehouse'])
67
+ hub = warehouse.parent
68
+ dispatches = Cats::Core::Dispatch.joins(:allocation_item)
69
+ .where(
70
+ allocation_item: { destination: hub },
71
+ dispatch_status: Cats::Core::Dispatch::STARTED
72
+ ).or(
73
+ Cats::Core::Dispatch.joins(:allocation_item)
74
+ .where(
75
+ allocation_item: { destination: warehouse },
76
+ dispatch_status: Cats::Core::Dispatch::STARTED
77
+ )
78
+ )
79
+ elsif details['hub']
80
+ hub = Cats::Core::Location.find(details['hub'])
81
+ dispatches = Cats::Core::Dispatch.joins(:allocation_item)
82
+ .where(
83
+ allocation_item: { destination: hub },
84
+ dispatch_status: Cats::Core::Dispatch::STARTED
85
+ )
86
+ end
87
+ dispatches
88
+ end
89
+
43
90
  def approve(dispatch)
44
91
  unless dispatch.dispatch_status == Cats::Core::Dispatch::DRAFT
45
92
  raise(StandardError, 'Dispatch has to be in draft state.')
@@ -64,6 +111,26 @@ module Cats
64
111
  transactions.each(&:commit)
65
112
  dispatch
66
113
  end
114
+
115
+ def confirm(dispatch)
116
+ total = dispatch.receipts.sum(:quantity)
117
+
118
+ unless total == dispatch.quantity
119
+ raise(
120
+ StandardError,
121
+ "There is an amount of #{dispatch.quantity - total} in the dispatch which is unaccounted for."
122
+ )
123
+ end
124
+
125
+ Cats::Core::Dispatch.transaction do
126
+ dispatch.dispatch_status = Cats::Core::Dispatch::RECEIVED
127
+ dispatch.receipts.each { |r| r.status = Cats::Core::Receipt::CONFIRMED }
128
+ dispatch.save
129
+ dispatch.receipts.each(&:save)
130
+ end
131
+
132
+ dispatch
133
+ end
67
134
  end
68
135
  end
69
136
  end
data/config/routes.rb CHANGED
@@ -56,11 +56,15 @@ Cats::Core::Engine.routes.draw do
56
56
  action: :create_receipt_authorization,
57
57
  as: :receipt_authorization_allocation_item
58
58
 
59
+ get '/dispatches/search', controller: :dispatches, action: :search, as: :search_dispatches
59
60
  resources :dispatches, except: %i[index destroy] do
60
61
  member do
62
+ get 'receipts', controller: :receipts, action: :index
61
63
  post 'approve'
62
64
  post 'start'
65
+ post 'confirm'
63
66
  end
64
67
  end
65
68
  resources :dispatch_transactions, except: %i[new edit destroy]
69
+ resources :receipts, except: %i[index new edit destroy]
66
70
  end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.1.0'.freeze
3
+ VERSION = '1.1.1'.freeze
4
4
  end
5
5
  end
@@ -1,6 +1,6 @@
1
1
  FactoryBot.define do
2
2
  factory :receipt, class: 'Cats::Core::Receipt' do
3
- dispatch
3
+ dispatch factory: :dispatch, dispatch_status: Cats::Core::Dispatch::STARTED
4
4
  quantity { 1.5 }
5
5
  commodity_status { Cats::Core::Commodity::GOOD }
6
6
  status { Cats::Core::Receipt::DRAFT }
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.1.0
4
+ version: 1.1.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: 2021-09-23 00:00:00.000000000 Z
11
+ date: 2021-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -225,6 +225,7 @@ files:
225
225
  - app/controllers/cats/core/locations_controller.rb
226
226
  - app/controllers/cats/core/menus_controller.rb
227
227
  - app/controllers/cats/core/notifications_controller.rb
228
+ - app/controllers/cats/core/receipts_controller.rb
228
229
  - app/controllers/cats/core/roles_controller.rb
229
230
  - app/controllers/cats/core/spaces_controller.rb
230
231
  - app/controllers/cats/core/unit_of_measures_controller.rb
@@ -275,6 +276,7 @@ files:
275
276
  - app/serializers/cats/core/dispatch_serializer.rb
276
277
  - app/serializers/cats/core/dispatch_transaction_serializer.rb
277
278
  - app/serializers/cats/core/location_serializer.rb
279
+ - app/serializers/cats/core/receipt_serializer.rb
278
280
  - app/serializers/cats/core/role_menu_serializer.rb
279
281
  - app/serializers/cats/core/unit_of_measure_serializer.rb
280
282
  - app/services/cats/core/dispatch_service.rb