unidom-order 1.1 → 1.2

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
  SHA1:
3
- metadata.gz: 886e0f2fc020d095320a1625b35811e72134a62c
4
- data.tar.gz: 1caf01e51ec566eb451937b3c06bff9e42873424
3
+ metadata.gz: 9aba7cfe4b2be0605fcad15d0f3fe75611ebb70b
4
+ data.tar.gz: f1f23895e6e7588204f35a8a030e00f0360defa0
5
5
  SHA512:
6
- metadata.gz: ae16527ac881d663e0f6e2ddaf5cb2b2725abcc13e291642496a8d4570be800bb732b84c854f53142263dbbc3a613f6703d383cb70ca34e5263206ac84f50b5d
7
- data.tar.gz: 2ff4125a24922b3f7547474f7dbd4b492adcaa8a9b84e580563818d2642b1e1d84bec51ad3722f3fcc5c585b3fda3d3ec8f24085dfafecd36b716b4dc0c9fdbf
6
+ metadata.gz: f0bc8751bf0e1175210176029327cbe5a1c12d982de558aa137da9efa3d8f7e4bef037395714cabad7afa2424e3ada8a8205128b181316b5c39fc14959714033
7
+ data.tar.gz: aac0fc801a68b6b8322dbf06a6eeb898de5abfcc458bec7d40460821e803c4728d8b3b10f2232c88fb3759298fa2edd96f3f6a34895a11f9e00478cc74cc491c
data/README.md CHANGED
@@ -83,16 +83,22 @@ order.adjustments # Order Adjustments
83
83
  Unidom::Order::OrderItem.order! product, of: order, unit_price: 1080.00, quantity: 2
84
84
  # Add the given product into the given order
85
85
 
86
- Unidom::Order::OrderAdjustment.adjust! order, amount: 20, due_to: 'DSCT'
86
+ Unidom::Order::OrderAdjustment.adjust! order, amount: 20, due_to: 'DSCT', opened_at: Time.now
87
87
  # Add the given adjustment into the given order
88
88
  ```
89
89
 
90
90
  ## Include the Concerns
91
91
  ```ruby
92
+ include Unidom::Order::AsAdjusted
92
93
  include Unidom::Order::AsOrderPlacer
93
94
  include Unidom::Order::AsOrderTaker
94
95
  ```
95
96
 
97
+ ### As Adjusted concern
98
+ The As Adjusted concern do the following tasks for the includer automatically:
99
+ 1. Define the has_many :adjustments macro as: ``has_many :adjustments, class_name: 'Unidom::Order::OrderAdjustment', as: :adjusted``
100
+ 2. Define the #is_adjusted! method as: ``is_adjusted!(amount, due_to: 'FRGT', at: Time.now)``
101
+
96
102
  ### As Order Placer concern
97
103
  The As Order Placer concern do the following tasks for the includer automatically:
98
104
  1. Define the has_many :placed_orders macro as: ``has_many :placed_orders, class_name: 'Unidom::Order::Order', as: :placer``
@@ -6,6 +6,22 @@ module Unidom::Order::Concerns::AsAdjusted
6
6
 
7
7
  has_many :adjustments, class_name: 'Unidom::Order::OrderAdjustment', as: :adjusted
8
8
 
9
+ def is_adjusted!(amount, due_to: 'FRGT', at: Time.now)
10
+ query = adjustments.adjustment_factor_coded_as(due_to).valid_at(now: at).alive
11
+ adjustment = query.first
12
+ if adjustment.present?
13
+ if 0==amount
14
+ adjustment.soft_destroy
15
+ else
16
+ adjustment.amount = amount
17
+ adjustment.save!
18
+ end
19
+ else
20
+ adjustment = query.create! amount: amount, opened_at: at
21
+ end
22
+ adjustment
23
+ end
24
+
9
25
  end
10
26
 
11
27
  end
@@ -12,15 +12,20 @@ class Unidom::Order::OrderAdjustment < ActiveRecord::Base
12
12
 
13
13
  scope :adjusted_is, ->(adjusted) { where adjusted: adjusted }
14
14
 
15
- def self.adjust!(adjusted, amount: 0, due_to: 'FRGT')
16
- adjustment = self.adjusted_is(adjusted).adjustment_factor_coded_as(due_to).valid_at.alive.first
15
+ def self.adjust!(adjusted, amount: 0, due_to: 'FRGT', opened_at: Time.now)
16
+ query = adjusted_is(adjusted).adjustment_factor_coded_as(due_to).valid_at(now: opened_at).alive
17
+ adjustment = query.first
17
18
  if adjustment.present?
18
- adjustment.amount = amount
19
- adjustment.save!
20
- adjustment
19
+ if 0==amount
20
+ adjustment.soft_destroy
21
+ else
22
+ adjustment.amount = amount
23
+ adjustment.save!
24
+ end
21
25
  else
22
- create! adjusted: adjusted, amount: amount, adjustment_factor_code: due_to
26
+ adjustment = query.create! amount: amount, opened_at: opened_at
23
27
  end
28
+ adjustment
24
29
  end
25
30
 
26
31
  end
@@ -1,5 +1,5 @@
1
1
  module Unidom
2
2
  module Order
3
- VERSION = '1.1'.freeze
3
+ VERSION = '1.2'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unidom-order
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Topbit Du
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-30 00:00:00.000000000 Z
11
+ date: 2016-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unidom-common