comee_core 0.1.31 → 0.1.33

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: 5cd7848381270134d19787687f958edee66f77eaaeb6a04d72d422f0fcdf1ea9
4
- data.tar.gz: 32408a7fb537503ec05947ed99b4547bbb2494af12884a1e11c031e6c577d0a2
3
+ metadata.gz: 89be0ab79ac6789639448aaafa19ca5845e08e132d0d99c34eef619b97ece546
4
+ data.tar.gz: f62cdbb0af85b33c2322d17d9289cdd12c2021cd28b1fa06332cc63c39847a01
5
5
  SHA512:
6
- metadata.gz: fd8973d61ccb97551156b1d51707a07355ba32c8f6bd236052aac2e256af26c1477746f4bd95e71f291d4481d2f205e7de449af90afddc1dd81be336296215f1
7
- data.tar.gz: 21f6968a9c21f32473eb09de2c468684c4557ea82510fe36ebe07aa6fcfe5261b93476bd5ddb2c50d9b2553c79b21cd505d03fe03a6f51d26f09227f0dd296ef
6
+ metadata.gz: 5fca168ac18ac7ceef8614bdb8792f4395e21107cb62adfc9b9ac1d7b4301f8b418e3bdb5968f9b8fd4c4ebcebaf3517cb13fcda854ef1c36bb059943ac42b59
7
+ data.tar.gz: 6369177eb65524a12f58c1f2469b9fc3114a587edc0bdc7b6b64dc98e1655433910325fa4d3864fce8f861dbb866694f3079171e91f18d9a5d48c9d1c01a9e16
@@ -18,7 +18,8 @@ module Comee
18
18
  private
19
19
 
20
20
  def model_params
21
- params.require(:payload).permit(:valid_from, :valid_to, :price, :status, :product_id, :discount, :client_id, :previous_price)
21
+ params.require(:payload).permit(:valid_from, :valid_to, :price, :status, :product_id, :discount, :client_id, :previous_price,
22
+ :unit_id)
22
23
  end
23
24
  end
24
25
  end
@@ -3,6 +3,13 @@ module Comee
3
3
  class ProductsController < ApplicationController
4
4
  include Common
5
5
 
6
+ def index
7
+ super do
8
+ products = Comee::Core::Product.all
9
+ products.then(&paginate)
10
+ end
11
+ end
12
+
6
13
  private
7
14
 
8
15
  def model_params
@@ -1,30 +1,28 @@
1
1
  module Comee
2
2
  module Core
3
- class ClientPrice < ApplicationRecord
4
- enum :status, {old: 0, current: 1, future: 2}
5
- belongs_to :product
3
+ class ClientPrice < Price
6
4
  belongs_to :client
7
5
  belongs_to :previous_price, class_name: "Comee::Core::ClientPrice", optional: true
8
6
 
9
- validates :valid_from, :valid_to, :price, :status, presence: true
7
+ validates :price, presence: true
10
8
  validates :price, numericality: {greater_than: 0}
11
9
  validates :discount, presence: true, numericality: {greater_than_or_equal_to: 0, less_than_or_equal_to: 100}
12
10
  validates :product_id, uniqueness: {scope: %i[client_id previous_price_id]}
13
- validate :validate_price_validity_dates
14
-
15
- def validate_price_validity_dates
16
- return unless valid_from && valid_to
17
-
18
- period = Period.new(valid_from, valid_to)
19
- errors.add(:base, "Price validity date range is not correct.") unless period.valid?
20
- end
21
11
 
22
12
  def self.ransackable_attributes(_auth_object = nil)
23
- %w[valid_from valid_to price status product_id client_id previous_price_id]
13
+ %w[
14
+ valid_from
15
+ valid_to
16
+ price
17
+ status
18
+ product_id
19
+ client_id
20
+ previous_price_id
21
+ ]
24
22
  end
25
23
 
26
24
  def self.ransackable_associations(_auth_object = nil)
27
- []
25
+ %w[product client]
28
26
  end
29
27
  end
30
28
  end
@@ -1,38 +1,41 @@
1
1
  module Comee
2
2
  module Core
3
- class MasterPrice < ApplicationRecord
4
- enum :status, {old: 0, current: 1, future: 2}
3
+ class MasterPrice < Price
5
4
  belongs_to :supplier
6
- belongs_to :product
7
5
  belongs_to :previous_price, class_name: "Comee::Core::MasterPrice", optional: true
8
6
 
9
- validates :purchase_price, :status, :selling_price, :pp_valid_from, :pp_valid_to, :sp_valid_from, :sp_valid_to, presence: true
7
+ validates :purchase_price, :selling_price, presence: true
10
8
  validates :margin, presence: true, numericality: {greater_than_or_equal_to: 0, less_than_or_equal_to: 100}
11
9
  validates :product_id, uniqueness: {scope: %i[supplier_id previous_price_id]}
12
- validate :validate_primary_price, :validate_purchase_price_validity_dates, :validate_selling_price_validity_dates
10
+ validate :validate_primary_price
11
+
12
+ scope :current_primary, -> { where(primary: true).current }
13
+ scope :unapplied, -> { where(propagated_to_client: false).current_primary }
13
14
 
14
15
  def validate_primary_price
15
16
  return unless product && primary
16
17
 
17
- count = MasterPrice.where(product: product, primary: true, status: MasterPrice.statuses[:current]).count
18
+ count = MasterPrice.where(product: product, primary: true, status: Price.statuses[:current]).count
18
19
  return unless count.positive? && primary && MasterPrice.statuses[status] == MasterPrice.statuses[:current]
19
20
 
20
- errors.add(:base,
21
- "There is already a primary supplier for item '#{product.code}'")
21
+ errors.add(:base, "There is already a primary price entry for item '#{product.code}'")
22
22
  end
23
23
 
24
- def validate_purchase_price_validity_dates
25
- return unless pp_valid_from && pp_valid_to
26
-
27
- period = Period.new(pp_valid_from, pp_valid_to)
28
- errors.add(:base, "Purchase price validity date range is not correct.") unless period.valid?
24
+ def self.ransackable_attributes(_auth_object = nil)
25
+ %w[
26
+ valid_from
27
+ valid_to
28
+ purchase_price
29
+ selling_price
30
+ status
31
+ product_id
32
+ supplier_id
33
+ previous_price_id
34
+ ]
29
35
  end
30
36
 
31
- def validate_selling_price_validity_dates
32
- return unless sp_valid_from && sp_valid_to
33
-
34
- period = Period.new(sp_valid_from, sp_valid_to)
35
- errors.add(:base, "Selling price validity date range is not correct.") unless period.valid?
37
+ def self.ransackable_associations(_auth_object = nil)
38
+ %w[product supplier]
36
39
  end
37
40
  end
38
41
  end
@@ -0,0 +1,37 @@
1
+ module Comee
2
+ module Core
3
+ class Price < ApplicationRecord
4
+ self.abstract_class = true
5
+ before_save :compute_status
6
+
7
+ enum :status, {past: 0, current: 1, future: 2}
8
+
9
+ scope :current, -> { where(status: Price.statuses[:current]) }
10
+ scope :future, -> { where(status: Price.statuses[:future]) }
11
+
12
+ belongs_to :product
13
+ belongs_to :unit
14
+
15
+ validates :valid_from, :valid_to, presence: true
16
+ validate :validate_price_validity_dates
17
+
18
+ def validate_price_validity_dates
19
+ return unless valid_from && valid_to
20
+
21
+ period = Period.new(valid_from, valid_to)
22
+ errors.add(:base, "Price validity date range is not correct.") unless period.valid?
23
+ end
24
+
25
+ def compute_status
26
+ period = Period.new(valid_from, valid_to)
27
+ if period.past?
28
+ self.status = Price.statuses[:past]
29
+ elsif period.current?
30
+ self.status = Price.statuses[:current]
31
+ elsif period.future?
32
+ self.status = Price.statuses[:future]
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,7 +1,7 @@
1
1
  module Comee
2
2
  module Core
3
3
  class User < ApplicationRecord
4
- enum :user_type, {supplier: 0, admin: 1}
4
+ enum :user_type, {supplier: 0, admin: 1, client: 2, worker: 3}
5
5
 
6
6
  has_secure_password
7
7
  has_many :notifications, as: :recipient, dependent: :destroy
@@ -1,7 +1,10 @@
1
1
  module Comee
2
2
  module Core
3
3
  class ClientPriceSerializer < ActiveModel::Serializer
4
- attributes :id, :valid_from, :valid_to, :price, :status, :product, :client, :previous_price
4
+ attributes :id, :valid_from, :valid_to, :price, :status, :previous_price
5
+ belongs_to :product
6
+ belongs_to :client
7
+ belongs_to :unit
5
8
  end
6
9
  end
7
10
  end
@@ -1,7 +1,7 @@
1
1
  module Comee
2
2
  module Core
3
3
  class SupplierSerializer < ActiveModel::Serializer
4
- attributes :id, :code, :name, :address, :locale
4
+ attributes :id, :code, :name, :address, :locale, :user_id
5
5
  end
6
6
  end
7
7
  end
@@ -24,6 +24,24 @@ module Comee
24
24
  finish > start
25
25
  end
26
26
 
27
+ def past?
28
+ raise(StandardError, "Period must be valid.") unless valid?
29
+
30
+ finish < Date.current
31
+ end
32
+
33
+ def current?
34
+ raise(StandardError, "Period must be valid.") unless valid?
35
+
36
+ start <= Date.current && finish > Date.current
37
+ end
38
+
39
+ def future?
40
+ raise(StandardError, "Period must be valid.") unless valid?
41
+
42
+ start > Date.current
43
+ end
44
+
27
45
  def contains?(period)
28
46
  return true if start <= period.start && finish >= period.finish
29
47
 
@@ -3,29 +3,32 @@ class CreateComeeCoreMasterPrices < ActiveRecord::Migration[7.0]
3
3
  create_table :comee_core_master_prices do |t|
4
4
  t.float :purchase_price, null: false
5
5
  t.float :selling_price, null: false
6
- t.date :pp_valid_from, null: false
7
- t.date :pp_valid_to, null: false
8
- t.date :sp_valid_from, null: false
6
+ t.date :valid_from, null: false
7
+ t.date :valid_to, null: false
9
8
  t.float :status, null: false, default: 0
10
- t.date :sp_valid_to, null: false
11
- t.boolean :active, null: false
12
- t.boolean :primary, null: false
9
+ t.boolean :primary, null: false, default: false
13
10
  t.integer :margin, null: false
14
11
  t.references :product,
15
12
  null: false,
16
- index: {name: "product_on_ccsp_indx"},
13
+ index: {name: "product_on_ccmp_indx"},
17
14
  foreign_key: {to_table: :comee_core_products}
18
15
  t.references :supplier,
19
16
  null: false,
20
- index: {name: "supplier_on_ccsp_indx"},
17
+ index: {name: "supplier_on_ccmp_indx"},
21
18
  foreign_key: {to_table: :comee_core_suppliers}
19
+ t.references :unit,
20
+ null: false,
21
+ index: {name: "unit_on_ccmp_indx"},
22
+ foreign_key: {to_table: :comee_core_units}
22
23
  t.references :previous_price,
23
24
  null: true,
24
25
  index: {name: "previous_price_on_ccsp_indx"},
25
26
  foreign_key: {to_table: :comee_core_master_prices}
27
+ t.boolean :propagated_to_client, null: false, default: false
26
28
 
27
29
  t.timestamps
28
30
  end
31
+
29
32
  add_index :comee_core_master_prices,
30
33
  %i[product_id supplier_id previous_price_id],
31
34
  unique: true,
@@ -14,6 +14,10 @@ class CreateComeeCoreClientPrices < ActiveRecord::Migration[7.0]
14
14
  null: false,
15
15
  index: {name: "client_on_cccp_indx"},
16
16
  foreign_key: {to_table: :comee_core_clients}
17
+ t.references :unit,
18
+ null: false,
19
+ index: {name: "unit_on_cccp_indx"},
20
+ foreign_key: {to_table: :comee_core_units}
17
21
  t.references :previous_price,
18
22
  null: true,
19
23
  index: {name: "previous_price_on_cccp_indx"},
@@ -1,5 +1,5 @@
1
1
  module Comee
2
2
  module Core
3
- VERSION = "0.1.31".freeze
3
+ VERSION = "0.1.33".freeze
4
4
  end
5
5
  end
@@ -6,7 +6,18 @@ FactoryBot.define do
6
6
  price { 100 }
7
7
  product
8
8
  client
9
+ unit
9
10
  discount { 0 }
10
11
  previous_price { nil }
12
+
13
+ trait :past do
14
+ valid_from { Date.current.advance(months: -2) }
15
+ valid_to { Date.current.advance(months: -1) }
16
+ end
17
+
18
+ trait :future do
19
+ valid_from { Date.current.advance(months: 1) }
20
+ valid_to { Date.current.advance(months: 2) }
21
+ end
11
22
  end
12
23
  end
@@ -2,16 +2,24 @@ FactoryBot.define do
2
2
  factory :master_price, class: "Comee::Core::MasterPrice" do
3
3
  purchase_price { 100.0 }
4
4
  selling_price { 90.0 }
5
- pp_valid_from { Date.current.advance(months: -1) }
6
- pp_valid_to { Date.current.advance(months: 1) }
7
- sp_valid_from { Date.current.advance(months: -1) }
8
- sp_valid_to { Date.current.advance(months: 1) }
9
- active { true }
5
+ valid_from { Date.current.advance(months: -1) }
6
+ valid_to { Date.current.advance(months: 1) }
10
7
  primary { false }
11
8
  margin { 10.0 }
12
9
  product
13
10
  supplier
11
+ unit
14
12
  previous_price { nil }
15
- status { 0 }
13
+ propagated_to_client { false }
14
+
15
+ trait :past do
16
+ valid_from { Date.current.advance(months: -2) }
17
+ valid_to { Date.current.advance(months: -1) }
18
+ end
19
+
20
+ trait :future do
21
+ valid_from { Date.current.advance(months: 1) }
22
+ valid_to { Date.current.advance(months: 2) }
23
+ end
16
24
  end
17
25
  end
@@ -1,6 +1,6 @@
1
1
  FactoryBot.define do
2
2
  factory :product, class: "Comee::Core::Product" do
3
- code { Faker::Alphanumeric.alpha(number: 6) }
3
+ sequence(:code) { |n| "#{Faker::Alphanumeric.unique.alpha(number: 6)}-#{n}" }
4
4
  name { Faker::Name.name }
5
5
  description { Faker::Lorem.sentence }
6
6
  metadata { {} }
@@ -1,6 +1,6 @@
1
1
  FactoryBot.define do
2
2
  factory :supplier, class: "Comee::Core::Supplier" do
3
- code { Faker::Alphanumeric.alpha(number: 8) }
3
+ sequence(:code) { |n| "#{Faker::Alphanumeric.alpha(number: 8)}-#{n}" }
4
4
  name { Faker::Name.name }
5
5
  address { Faker::Address.full_address }
6
6
  locale { "en" }
@@ -1,6 +1,6 @@
1
1
  FactoryBot.define do
2
2
  factory :unit, class: "Comee::Core::Unit" do
3
- code { Faker::Alphanumeric.alpha(number: 2) }
3
+ sequence(:code) { |n| "#{Faker::Alphanumeric.alpha(number: 2)}-#{n}" }
4
4
  name { Faker::Name.name }
5
5
  unit_type { Comee::Core::Unit.unit_types[:weight] }
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comee_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.31
4
+ version: 0.1.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-03 00:00:00.000000000 Z
11
+ date: 2023-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_model_serializers
@@ -276,6 +276,7 @@ files:
276
276
  - app/models/comee/core/master_price.rb
277
277
  - app/models/comee/core/notification.rb
278
278
  - app/models/comee/core/order_source.rb
279
+ - app/models/comee/core/price.rb
279
280
  - app/models/comee/core/product.rb
280
281
  - app/models/comee/core/product_lookup.rb
281
282
  - app/models/comee/core/product_type.rb