cats_core 1.1.2 → 1.1.6

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: ae62c0b230969901c7405d3bdf56b72dc099e77e4e6d9a21ba89243f91a1bd07
4
- data.tar.gz: cc0d85257ffb25fc70a890011b1ae670dd18a0ea6938f79e73ec87a757787cd1
3
+ metadata.gz: dc868538b2dd7c7fc157b0fd2c78c79a38b868c60a12e0e1ce6662a7dac01e3a
4
+ data.tar.gz: d20518593e2a0bc565a47d2b426794c16790bf45f1650feb2c3a7550fb610637
5
5
  SHA512:
6
- metadata.gz: 582d0f4d63492a2ce3b3e12a7d6e84088c5c5badab3135cb108fc8876348f3e593f70d0498d6c23e79de143efb04436fd858325c6bac8a5bf29785d3a1d4d899
7
- data.tar.gz: 581e221e3dff760e51095cc7094736b0a2c7f7c261d4693c8f811f3b9e2aac1ee5093ba6823895bb910488947f8616cee32b799178b5d1da64905ca40cc7b20c
6
+ metadata.gz: 5447be874ddfaa36956f09550a313ce4078440e0b0be91ceb0cc5d8f035236c71f823ce6f4adf88efba0e4db44a047595e9a56fa84032fe3f3b7c14f9b602126
7
+ data.tar.gz: 44ab9de9141a4957e53f7739a761e1127940b0dfa813ea8503a579a4036b71a6e522a86b029facf406354d0d826e007d1260023ea4daa99e5531a15407288567
@@ -4,7 +4,7 @@ module Cats
4
4
  include Common
5
5
 
6
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
+ before_action :set_dispatch, only: %i[approve start confirm commodity]
8
8
 
9
9
  def index
10
10
  dispatches = Cats::Core::Dispatch.where(allocation_item_id: params[:id])
@@ -56,6 +56,11 @@ module Cats
56
56
  render json: { success: true, data: serialize(data) }
57
57
  end
58
58
 
59
+ def commodity
60
+ data = @dispatch.allocation_item.allocation.commodity
61
+ render json: { success: true, data: serialize(data) }
62
+ end
63
+
59
64
  private
60
65
 
61
66
  def set_service
@@ -1,50 +1,20 @@
1
1
  module Cats
2
2
  module Core
3
3
  class LocationsController < ApplicationController
4
- before_action :set_location, only: %i[show update]
4
+ include Common
5
5
 
6
6
  def index
7
7
  locations = Cats::Core::Location.where(location_type: params[:location_type])
8
- data = ActiveModelSerializers::SerializableResource.new(locations)
9
- render json: { success: true, data: data }
10
- end
11
-
12
- def show
13
- data = ActiveModelSerializers::SerializableResource.new(@location)
14
- render json: { success: true, data: data }
8
+ render json: { success: true, data: serialize(locations) }
15
9
  end
16
10
 
17
11
  def children
18
12
  parent = Cats::Core::Location.find(params[:id])
19
- data = ActiveModelSerializers::SerializableResource.new(parent.children)
20
- render json: { success: true, data: data }
21
- end
22
-
23
- def create
24
- obj = Cats::Core::Location.new(model_params)
25
- if obj.save
26
- data = ActiveModelSerializers::SerializableResource.new(obj)
27
- render json: { success: true, data: data }, status: :created
28
- else
29
- render json: { success: false, error: obj.errors.full_messages[0] }, status: :unprocessable_entity
30
- end
31
- end
32
-
33
- def update
34
- if @location.update(model_params)
35
- data = ActiveModelSerializers::SerializableResource.new(@location)
36
- render json: { success: true, data: data }
37
- else
38
- render json: { success: false, error: @location.errors.full_messages[0] }, status: :unprocessable_entity
39
- end
13
+ render json: { success: true, data: serialize(parent.children) }
40
14
  end
41
15
 
42
16
  private
43
17
 
44
- def set_location
45
- @location = Cats::Core::Location.find(params[:id])
46
- end
47
-
48
18
  def model_params
49
19
  params.require(:payload).permit(:code, :name, :location_type, :description, :parent_id)
50
20
  end
@@ -0,0 +1,13 @@
1
+ module Cats
2
+ module Core
3
+ class ReceiptTransactionsController < 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
@@ -8,6 +8,24 @@ module Cats
8
8
  render json: { success: true, data: serialize(receipts) }
9
9
  end
10
10
 
11
+ def start_stacking
12
+ receipt = Cats::Core::Receipt.find(params[:id])
13
+ service = ReceiptService.new
14
+ result = service.start_stacking(receipt)
15
+ render json: { success: true, data: serialize(result) }
16
+ rescue StandardError => e
17
+ render json: { success: false, error: e.message }
18
+ end
19
+
20
+ def stack
21
+ receipt = Cats::Core::Receipt.find(params[:id])
22
+ service = ReceiptService.new
23
+ result = service.stack(receipt)
24
+ render json: { success: true, data: serialize(result) }
25
+ rescue StandardError => e
26
+ render json: { success: false, error: e.message }
27
+ end
28
+
11
29
  private
12
30
 
13
31
  def model_params
@@ -5,6 +5,16 @@ module Cats
5
5
 
6
6
  validates :reference_no, presence: true, uniqueness: true
7
7
  validates :gift_date, presence: true
8
+
9
+ after_create :update_donation
10
+
11
+ def update_donation
12
+ donation = self.donation
13
+ return if donation.active
14
+
15
+ donation.active = true
16
+ donation.save!
17
+ end
8
18
  end
9
19
  end
10
20
  end
@@ -8,6 +8,16 @@ module Cats
8
8
  validates :reference_no, uniqueness: true
9
9
 
10
10
  delegate(:name, to: :commodity_category, prefix: true)
11
+
12
+ after_create :update_donation
13
+
14
+ def update_donation
15
+ donation = self.donation
16
+ return if donation.active
17
+
18
+ donation.active = true
19
+ donation.save!
20
+ end
11
21
  end
12
22
  end
13
23
  end
@@ -10,6 +10,7 @@ module Cats
10
10
 
11
11
  belongs_to :dispatch
12
12
  belongs_to :prepared_by, class_name: 'Cats::Core::User'
13
+ has_many :receipt_transactions, foreign_key: :source_id
13
14
 
14
15
  validates :quantity, :commodity_status, :status, presence: true
15
16
  validates :quantity, numericality: { greater_than: 0 }
@@ -23,19 +24,26 @@ module Cats
23
24
  def validate_dispatch_status
24
25
  return unless dispatch
25
26
 
26
- return if dispatch.dispatch_status == Cats::Core::Dispatch::STARTED
27
+ statuses = [Cats::Core::Dispatch::STARTED, Cats::Core::Dispatch::RECEIVED]
28
+ return if statuses.include?(dispatch.dispatch_status)
27
29
 
28
- errors.add(:dispatch, 'Dispatch should be in "Started" state.')
30
+ errors.add(:dispatch, 'should be in "Started" state.')
29
31
  end
30
32
 
31
33
  def validate_total_quantity
32
34
  return unless dispatch && quantity
33
35
 
34
- received = Cats::Core::Receipt.where(dispatch: dispatch).sum(:quantity)
35
- remaining = dispatch.quantity - received
36
- return if quantity <= remaining
36
+ received = dispatch.receipts.sum(:quantity)
37
+ diff = dispatch.quantity - received
38
+ if new_record?
39
+ return if quantity <= diff
37
40
 
38
- errors.add(:quantity, "Total receipt quantity is higher than dispatch quantity (Max = #{remaining}).")
41
+ errors.add(:quantity, "total is higher than dispatch quantity (Max = #{diff}).")
42
+ else
43
+ return unless diff.negative?
44
+
45
+ errors.add(:quantity, "total is higher than dispatch quantity (Max = #{diff.abs}).")
46
+ end
39
47
  end
40
48
  end
41
49
  end
@@ -3,6 +3,8 @@ module Cats
3
3
  class ReceiptTransaction < Transaction
4
4
  belongs_to :source, class_name: 'Cats::Core::Receipt'
5
5
  belongs_to :destination, class_name: 'Cats::Core::Stack'
6
+
7
+ delegate(:code, to: :destination, prefix: true)
6
8
  end
7
9
  end
8
10
  end
@@ -12,12 +12,14 @@ module Cats
12
12
  validates :transaction_date, :quantity, :status, presence: true
13
13
  validates :quantity, numericality: { greater_than: 0 }
14
14
  validates :status, inclusion: { in: STATUSES }
15
- validate :validate_quantity
15
+ validate :validate_quantity, unless: :skip_quantity_validation
16
16
 
17
17
  def validate_quantity
18
18
  return unless quantity.present? && source.present?
19
19
 
20
- errors.add(:quantity, 'cannot be more than source quantity') if quantity > source.quantity
20
+ total = self.class.where(source: source).sum(:quantity)
21
+ diff = source.quantity - total
22
+ errors.add(:quantity, "total is higher than source quantity (Max = #{diff}).") if quantity > diff
21
23
  end
22
24
 
23
25
  def commit
@@ -27,10 +29,23 @@ module Cats
27
29
  source.save
28
30
  destination.save
29
31
  self.status = COMMITTED
30
- save
32
+ save!
31
33
  end
32
34
  end
33
35
 
36
+ def skip_quantity_validation
37
+ # Quantity validation should be skipped if we are commiting transactions.
38
+ (
39
+ instance_of?(Cats::Core::DispatchTransaction) &&
40
+ destination &&
41
+ destination.dispatch_status == Cats::Core::Dispatch::STARTED
42
+ ) || (
43
+ instance_of?(Cats::Core::ReceiptTransaction) &&
44
+ source &&
45
+ source.status == Cats::Core::Receipt::STACKING
46
+ )
47
+ end
48
+
34
49
  def set_status
35
50
  return unless new_record?
36
51
 
@@ -0,0 +1,8 @@
1
+ class ReceiptTransactionSerializer < ActiveModel::Serializer
2
+ attributes :id, :source_id, :dispatch_reference, :destination_id, :destination_code, :quantity, :transaction_date,
3
+ :status
4
+
5
+ def dispatch_reference
6
+ object.source.dispatch.reference_no
7
+ end
8
+ end
@@ -126,7 +126,7 @@ module Cats
126
126
  dispatch.dispatch_status = Cats::Core::Dispatch::RECEIVED
127
127
  dispatch.receipts.each { |r| r.status = Cats::Core::Receipt::CONFIRMED }
128
128
  dispatch.save
129
- dispatch.receipts.each(&:save)
129
+ dispatch.receipts.each(&:save!)
130
130
  end
131
131
 
132
132
  dispatch
@@ -0,0 +1,32 @@
1
+ module Cats
2
+ module Core
3
+ class ReceiptService
4
+ def start_stacking(receipt)
5
+ raise(StandardError, 'Receipt should be confirmed.') unless receipt.status == Cats::Core::Receipt::CONFIRMED
6
+
7
+ raise(StandardError, 'There are no stack assignments in receipt.') if receipt.receipt_transactions.count.zero?
8
+
9
+ receipt.status = Cats::Core::Receipt::STACKING
10
+ receipt.save!
11
+ receipt
12
+ end
13
+
14
+ def stack(receipt)
15
+ unless receipt.status == Cats::Core::Receipt::STACKING
16
+ raise(StandardError, 'Receipt should be in stacking state.')
17
+ end
18
+
19
+ receipt.status = Cats::Core::Receipt::STACKED
20
+ stacks = receipt.receipt_transactions.map(&:destination)
21
+ stacks.each { |stack| stack.stack_status = Cats::Core::Stack::ALLOCATED }
22
+
23
+ Cats::Core::Receipt.transaction do
24
+ receipt.receipt_transactions.each(&:commit)
25
+ stacks.each(&:save!)
26
+ receipt.save!
27
+ end
28
+ receipt
29
+ end
30
+ end
31
+ end
32
+ end
data/config/routes.rb CHANGED
@@ -60,11 +60,18 @@ Cats::Core::Engine.routes.draw do
60
60
  resources :dispatches, except: %i[index destroy] do
61
61
  member do
62
62
  get 'receipts', controller: :receipts, action: :index
63
+ get 'commodity'
63
64
  post 'approve'
64
65
  post 'start'
65
66
  post 'confirm'
66
67
  end
67
68
  end
68
69
  resources :dispatch_transactions, except: %i[new edit destroy]
69
- resources :receipts, except: %i[index new edit destroy]
70
+ resources :receipts, except: %i[index new edit destroy] do
71
+ member do
72
+ post 'start_stacking'
73
+ post 'stack'
74
+ end
75
+ end
76
+ resources :receipt_transactions, except: %i[new edit destroy]
70
77
  end
@@ -25,6 +25,7 @@ class CreateCatsCoreDonations < ActiveRecord::Migration[6.1]
25
25
  null: true,
26
26
  index: { name: 'uom_on_donation_indx' },
27
27
  foreign_key: { to_table: :cats_core_unit_of_measures }
28
+ t.boolean :active, null: false, default: false
28
29
 
29
30
  t.timestamps
30
31
  end
@@ -1,5 +1,5 @@
1
1
  module Cats
2
2
  module Core
3
- VERSION = '1.1.2'.freeze
3
+ VERSION = '1.1.6'.freeze
4
4
  end
5
5
  end
@@ -9,5 +9,6 @@ FactoryBot.define do
9
9
  currency { nil }
10
10
  commodity_category
11
11
  unit_of_measure
12
+ active { false }
12
13
  end
13
14
  end
@@ -1,5 +1,5 @@
1
1
  FactoryBot.define do
2
- factory :receipt_transaction do
2
+ factory :receipt_transaction, class: 'Cats::Core::ReceiptTransaction' do
3
3
  source factory: :receipt
4
4
  destination factory: :stack
5
5
  transaction_date { Date.today }
@@ -1,7 +1,7 @@
1
1
  FactoryBot.define do
2
2
  factory :receipt, class: 'Cats::Core::Receipt' do
3
3
  dispatch factory: :dispatch, dispatch_status: Cats::Core::Dispatch::STARTED
4
- quantity { 1.5 }
4
+ quantity { 50 }
5
5
  commodity_status { Cats::Core::Commodity::GOOD }
6
6
  status { Cats::Core::Receipt::DRAFT }
7
7
  remark { FFaker::Name.name }
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.2
4
+ version: 1.1.6
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-25 00:00:00.000000000 Z
11
+ date: 2021-09-26 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/receipt_transactions_controller.rb
228
229
  - app/controllers/cats/core/receipts_controller.rb
229
230
  - app/controllers/cats/core/roles_controller.rb
230
231
  - app/controllers/cats/core/spaces_controller.rb
@@ -277,11 +278,13 @@ files:
277
278
  - app/serializers/cats/core/dispatch_transaction_serializer.rb
278
279
  - app/serializers/cats/core/location_serializer.rb
279
280
  - app/serializers/cats/core/receipt_serializer.rb
281
+ - app/serializers/cats/core/receipt_transaction_serializer.rb
280
282
  - app/serializers/cats/core/role_menu_serializer.rb
281
283
  - app/serializers/cats/core/unit_of_measure_serializer.rb
282
284
  - app/services/cats/core/dispatch_service.rb
283
285
  - app/services/cats/core/menu_service.rb
284
286
  - app/services/cats/core/notification_service.rb
287
+ - app/services/cats/core/receipt_service.rb
285
288
  - app/services/cats/core/space_service.rb
286
289
  - app/services/cats/core/token_auth_service.rb
287
290
  - config/routes.rb