cats_core 1.0.47 → 1.1.0

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: efb65bb9da37fbd6070d5844cdcd48c9c7c7e6a5b264c41e2103593e309838bd
4
- data.tar.gz: 512001fdb9ddb2fa71fac19683b105632926a4cf75857b44a1a267c8710d02d9
3
+ metadata.gz: ebf6bb336bcf7b1fa89d734c18444e8f41b586c480a24b675fb62d5a8e3d5528
4
+ data.tar.gz: 6a5adfade3042c981cc482f336393cd51265a636e56b2ae91b5f4c1d8193840a
5
5
  SHA512:
6
- metadata.gz: 9f5188e5b7fbd88a26db6ee14473b199994b07fe42e475fea3b49fcb3dd63efe8beb07ad285733c9a9fcd14d1ef63740454905b4bd505c1217a02d7c3bb851b2
7
- data.tar.gz: '082ab5a2755af2ce386e8c8636d1ae022fa6c486cd83d7da7410d74b26653d03f1d21f8ff5c79b936546f92bfc65c013fe781f5613e3ac8ae0c2ba895fbb7da0'
6
+ metadata.gz: 5e3075ca71388f92b600ebfeb4d951e003b200987683a5de62d7138107d14efbd6c94a11425c1770e650ecf5facdcefd8ca90de71bb97d5b1685a9d787ef7ead
7
+ data.tar.gz: ecd7c5c357ea9e8a05c06523cd821a8c10fd21611e5cbc27390fefc8cec5aa54ea840175eb5c8419f484654bdb5a7918d2d551c36de242599e9b41dfa32377d8
@@ -0,0 +1,13 @@
1
+ module Cats
2
+ module Core
3
+ class DispatchTransactionsController < ApplicationController
4
+ include Common
5
+
6
+ private
7
+
8
+ def model_params
9
+ params.require(:payload).permit(:source_id, :destination_id, :transaction_date, :quantity)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ module Cats
2
+ module Core
3
+ class DispatchesController < ApplicationController
4
+ include Common
5
+
6
+ before_action :set_service, only: %i[create_receipt_authorization approve start]
7
+
8
+ def index
9
+ dispatches = Cats::Core::Dispatch.where(allocation_item_id: params[:id])
10
+ render json: { success: true, data: serialize(dispatches) }
11
+ end
12
+
13
+ def create
14
+ dispatch = Cats::Core::Dispatch.new(model_params)
15
+ dispatch.prepared_by = current_user
16
+ if dispatch.save
17
+ render json: { success: true, data: serialize(dispatch) }, status: :created
18
+ else
19
+ render json: { success: false, error: dispatch.errors.full_messages[0] }, status: :unprocessable_entity
20
+ end
21
+ end
22
+
23
+ def create_receipt_authorization
24
+ allocation_item = Cats::Core::AllocationItem.find(params[:id])
25
+ dispatch = @service.create_receipt_authorization(
26
+ params[:reference_no], allocation_item, params[:quantity], params[:remark], current_user
27
+ )
28
+ render json: { success: true, data: serialize(dispatch) }
29
+ end
30
+
31
+ def approve
32
+ dispatch = Cats::Core::Dispatch.find(params[:id])
33
+ dispatch = @service.approve(dispatch)
34
+ render json: { success: true, data: serialize(dispatch) }
35
+ rescue StandardError => e
36
+ render json: { success: false, error: e.message }
37
+ end
38
+
39
+ def start
40
+ dispatch = Cats::Core::Dispatch.find(params[:id])
41
+ dispatch = @service.start(dispatch)
42
+ render json: { success: true, data: serialize(dispatch) }
43
+ rescue StandardError => e
44
+ render json: { success: false, error: e.message }
45
+ end
46
+
47
+ private
48
+
49
+ def set_service
50
+ @service = DispatchService.new
51
+ end
52
+
53
+ def model_params
54
+ params.require(:payload).permit(:reference_no, :allocation_item_id, :transporter_id, :plate_no, :driver_name,
55
+ :driver_phone, :quantity, :remark, :dispatch_status)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,55 @@
1
+ module Cats
2
+ module Core
3
+ module Common
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_action :set_clazz
8
+ before_action :set_object, only: %i[show update]
9
+ end
10
+
11
+ def index
12
+ data = serialize(@clazz.all)
13
+ render json: { success: true, data: data }
14
+ end
15
+
16
+ def show
17
+ render json: { success: true, data: serialize(@obj) }
18
+ end
19
+
20
+ def create
21
+ obj = @clazz.new(model_params)
22
+ if obj.save
23
+ render json: { success: true, data: serialize(obj) }, status: :created
24
+ else
25
+ render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
26
+ end
27
+ end
28
+
29
+ def update
30
+ if @obj.update(model_params)
31
+ render json: { success: true, data: serialize(@obj) }
32
+ else
33
+ render json: { success: false, error: @obj.errors.full_messages[0] }, status: :unprocessable_entity
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def serialize(data)
40
+ ActiveModelSerializers::SerializableResource.new(data)
41
+ end
42
+
43
+ def set_clazz
44
+ @clazz = "Cats::Core::#{controller_name.classify}".constantize
45
+ end
46
+
47
+ def set_object
48
+ @obj = @clazz.find(params[:id])
49
+ end
50
+
51
+ # This class should be overridden by respective child controllers
52
+ def model_params; end
53
+ end
54
+ end
55
+ end
@@ -3,6 +3,9 @@ module Cats
3
3
  class DispatchTransaction < Transaction
4
4
  belongs_to :source, class_name: 'Cats::Core::Stack'
5
5
  belongs_to :destination, class_name: 'Cats::Core::Dispatch'
6
+
7
+ delegate(:code, to: :source, prefix: true)
8
+ delegate(:reference_no, to: :destination, prefix: true)
6
9
  end
7
10
  end
8
11
  end
@@ -2,6 +2,7 @@ module Cats
2
2
  module Core
3
3
  class Transaction < ApplicationRecord
4
4
  self.abstract_class = true
5
+ after_initialize :set_status
5
6
 
6
7
  # Transaction statuses
7
8
  DRAFT = 'Draft'.freeze
@@ -25,8 +26,16 @@ module Cats
25
26
  destination.quantity += quantity
26
27
  source.save
27
28
  destination.save
29
+ self.status = COMMITTED
30
+ save
28
31
  end
29
32
  end
33
+
34
+ def set_status
35
+ return unless new_record?
36
+
37
+ self.status = DRAFT
38
+ end
30
39
  end
31
40
  end
32
41
  end
@@ -0,0 +1,8 @@
1
+ class DispatchSerializer < ActiveModel::Serializer
2
+ attributes :id, :reference_no, :allocation_item_id, :transporter_id, :transporter_name, :plate_no, :driver_name,
3
+ :driver_phone, :quantity, :remark, :prepared_by_id, :prepared_by_email, :dispatch_status, :destination
4
+
5
+ def destination
6
+ object.allocation_item.destination.name
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ class DispatchTransactionSerializer < ActiveModel::Serializer
2
+ attributes :id, :source_id, :source_code, :destination_id, :destination_reference_no, :quantity, :transaction_date,
3
+ :status
4
+ end
@@ -0,0 +1,69 @@
1
+ module Cats
2
+ module Core
3
+ class DispatchService
4
+ def create_receipt_authorization(reference_no, allocation_item, quantity, remark, user)
5
+ status = allocation_item.allocation.allocation_status
6
+ raise(StandardError, 'Allocation not approved.') unless status == Cats::Core::Allocation::APPROVED
7
+
8
+ transporter = Cats::Core::Transporter.find_by(code: 'SUP-TRANS')
9
+ raise(StandardError, 'Supplier transporter does not exist.') unless transporter
10
+
11
+ store = Cats::Core::Store.find_by(code: 'SUP-STORE')
12
+ raise(StandardError, 'Supplier store does not exist.') unless store
13
+
14
+ commodity = allocation_item.allocation.commodity
15
+ stack = store.stacks.find_by(commodity: commodity)
16
+
17
+ raise(StandardError, 'Commodity not found in supplier store.') unless stack
18
+
19
+ dispatch = Cats::Core::Dispatch.create!(
20
+ reference_no: reference_no,
21
+ allocation_item: allocation_item,
22
+ transporter: transporter,
23
+ plate_no: 'Supplier plate no',
24
+ driver_name: 'Supplier driver',
25
+ driver_phone: 'Supplier driver phone',
26
+ quantity: quantity,
27
+ remark: remark,
28
+ prepared_by: user,
29
+ dispatch_status: Cats::Core::Dispatch::DRAFT
30
+ )
31
+
32
+ # Create a dispatch transaction
33
+ Cats::Core::DispatchTransaction.create!(
34
+ source: stack,
35
+ destination: dispatch,
36
+ transaction_date: Date.today,
37
+ quantity: quantity,
38
+ status: Cats::Core::Transaction::DRAFT
39
+ )
40
+ dispatch
41
+ end
42
+
43
+ def approve(dispatch)
44
+ unless dispatch.dispatch_status == Cats::Core::Dispatch::DRAFT
45
+ raise(StandardError, 'Dispatch has to be in draft state.')
46
+ end
47
+
48
+ transactions = Cats::Core::DispatchTransaction.where(destination: dispatch).count
49
+ raise(StandardError, 'Dispatch has no transactions.') unless transactions.positive?
50
+
51
+ dispatch.dispatch_status = Cats::Core::Dispatch::APPROVED
52
+ dispatch.save
53
+ dispatch
54
+ end
55
+
56
+ def start(dispatch)
57
+ unless dispatch.dispatch_status == Cats::Core::Dispatch::APPROVED
58
+ raise(StandardError, 'Dispatch has to be approved first.')
59
+ end
60
+
61
+ dispatch.dispatch_status = Cats::Core::Dispatch::STARTED
62
+ dispatch.save
63
+ transactions = Cats::Core::DispatchTransaction.where(destination: dispatch)
64
+ transactions.each(&:commit)
65
+ dispatch
66
+ end
67
+ end
68
+ end
69
+ end
@@ -8,7 +8,9 @@ module Cats
8
8
  def available_space(id, level)
9
9
  case level
10
10
  when HUB
11
- warehouse_ids = Cats::Core::Location.find(id).children.map(&:id)
11
+ hub = Cats::Core::Location.find(id)
12
+ warehouses = hub.children
13
+ warehouse_ids = warehouses.map(&:id)
12
14
  stores = Cats::Core::Store.joins(:warehouse)
13
15
  .where(
14
16
  cats_core_locations: {
@@ -19,19 +21,48 @@ module Cats
19
21
  wh_details = []
20
22
  stores.each do |key, value|
21
23
  total_space = value.inject(0) { |sum, val| sum + val.available_space }
22
- details = value.map { |val| { store_id: val.id, total_space: val.available_space } }
23
- wh_details << { warehouse_id: key, total_space: total_space, details: details }
24
+ details = value.map do |val|
25
+ { store_id: val.id, code: val.code, name: val.name, total_space: val.available_space }
26
+ end
27
+ warehouse = warehouses.find(key)
28
+ wh_details << {
29
+ warehouse_id: key,
30
+ code: warehouse.code,
31
+ name: warehouse.name,
32
+ total_space: total_space,
33
+ details: details
34
+ }
24
35
  end
25
36
  hub_space = wh_details.inject(0) { |sum, val| sum + val[:total_space] }
26
- space = { hub_id: id, total_space: hub_space, details: wh_details }
37
+ space = {
38
+ hub_id: id,
39
+ code: hub.code,
40
+ name: hub.name,
41
+ total_space: hub_space,
42
+ details: wh_details
43
+ }
27
44
  when WAREHOUSE
45
+ warehouse = Cats::Core::Location.find(id)
28
46
  stores = Cats::Core::Store.where(warehouse_id: id)
29
- details = stores.map { |store| { store_id: store.id, total_space: store.available_space } }
47
+ details = stores.map do |store|
48
+ { store_id: store.id, code: store.code, name: store.name, total_space: store.available_space }
49
+ end
30
50
  total_space = stores.inject(0) { |sum, val| sum + val.available_space }
31
- space = { warehouse_id: id, total_space: total_space, details: details }
51
+ space = {
52
+ warehouse_id: id,
53
+ code: warehouse.code,
54
+ name: warehouse.name,
55
+ total_space: total_space,
56
+ details: details
57
+ }
32
58
  when STORE
33
59
  store = Cats::Core::Store.find(id)
34
- space = { store_id: store.id, total_space: store.available_space }
60
+ space = {
61
+ store_id: store.id,
62
+ code: store.code,
63
+ name: store.name,
64
+ total_space: store.available_space
65
+ }
35
66
  end
36
67
  space
37
68
  end
data/config/routes.rb CHANGED
@@ -33,11 +33,11 @@ Cats::Core::Engine.routes.draw do
33
33
  to: 'spaces#available_space',
34
34
  as: :hub_space,
35
35
  defaults: { level: Cats::Core::SpaceService::HUB }
36
- get '/warehousess/:id/space',
36
+ get '/warehouses/:id/space',
37
37
  to: 'spaces#available_space',
38
38
  as: :warehouse_space,
39
39
  defaults: { level: Cats::Core::SpaceService::WAREHOUSE }
40
- get '/store/:id/space',
40
+ get '/stores/:id/space',
41
41
  to: 'spaces#available_space',
42
42
  as: :store_space,
43
43
  defaults: { level: Cats::Core::SpaceService::STORE }
@@ -49,4 +49,18 @@ Cats::Core::Engine.routes.draw do
49
49
  end
50
50
  resources :currencies, except: %i[destroy]
51
51
  resources :unit_of_measures, except: %i[destroy]
52
+
53
+ get '/allocation_items/:id/dispatches', controller: :dispatches, action: :index, as: :dispatches_allocation_item
54
+ post '/allocation_items/:id/receipt_authorizaton',
55
+ controller: :dispatches,
56
+ action: :create_receipt_authorization,
57
+ as: :receipt_authorization_allocation_item
58
+
59
+ resources :dispatches, except: %i[index destroy] do
60
+ member do
61
+ post 'approve'
62
+ post 'start'
63
+ end
64
+ end
65
+ resources :dispatch_transactions, except: %i[new edit destroy]
52
66
  end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.0.47'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
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.0.47
4
+ version: 1.1.0
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-21 00:00:00.000000000 Z
11
+ date: 2021-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -220,6 +220,8 @@ files:
220
220
  - app/controllers/cats/core/application_controller.rb
221
221
  - app/controllers/cats/core/commodity_categories_controller.rb
222
222
  - app/controllers/cats/core/currencies_controller.rb
223
+ - app/controllers/cats/core/dispatch_transactions_controller.rb
224
+ - app/controllers/cats/core/dispatches_controller.rb
223
225
  - app/controllers/cats/core/locations_controller.rb
224
226
  - app/controllers/cats/core/menus_controller.rb
225
227
  - app/controllers/cats/core/notifications_controller.rb
@@ -227,6 +229,7 @@ files:
227
229
  - app/controllers/cats/core/spaces_controller.rb
228
230
  - app/controllers/cats/core/unit_of_measures_controller.rb
229
231
  - app/controllers/cats/core/users_controller.rb
232
+ - app/controllers/concerns/cats/core/common.rb
230
233
  - app/jobs/cats/core/application_job.rb
231
234
  - app/models/cats/core/allocation.rb
232
235
  - app/models/cats/core/allocation_item.rb
@@ -269,9 +272,12 @@ files:
269
272
  - app/notifications/cats/core/simple_notification.rb
270
273
  - app/serializers/cats/core/commodity_category_serializer.rb
271
274
  - app/serializers/cats/core/currency_serializer.rb
275
+ - app/serializers/cats/core/dispatch_serializer.rb
276
+ - app/serializers/cats/core/dispatch_transaction_serializer.rb
272
277
  - app/serializers/cats/core/location_serializer.rb
273
278
  - app/serializers/cats/core/role_menu_serializer.rb
274
279
  - app/serializers/cats/core/unit_of_measure_serializer.rb
280
+ - app/services/cats/core/dispatch_service.rb
275
281
  - app/services/cats/core/menu_service.rb
276
282
  - app/services/cats/core/notification_service.rb
277
283
  - app/services/cats/core/space_service.rb