cats_core 1.0.50 → 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: b13a3549ac212c8e869ce5aff6b3508787e75af7afaeadba4b0e99f5476b101e
4
- data.tar.gz: d4993f0e36936fc4ed18ae9accca173189056c3cddae7fc2f16704f6509bac80
3
+ metadata.gz: ebf6bb336bcf7b1fa89d734c18444e8f41b586c480a24b675fb62d5a8e3d5528
4
+ data.tar.gz: 6a5adfade3042c981cc482f336393cd51265a636e56b2ae91b5f4c1d8193840a
5
5
  SHA512:
6
- metadata.gz: 91d4ba5e59ef7da90cf36d6073d20819496bae612eff73cdeccada481293a956311732201b049da61e84ba1c12b1a7464d636202b101f66a49bcf22146211440
7
- data.tar.gz: afdceef95b3321eaea7ebad43bd622b814ef268f97b20c5815af07376fc710b338b74a25bc3e81d6c4b366f00e30e6553c2f2dcc38e84685bc670201ce4e4e13
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
@@ -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
data/config/routes.rb CHANGED
@@ -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.50'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cats_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.50
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
@@ -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