smerp-quotation 0.2.2 → 0.2.3

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: 8c8ccbdb5928d32132abbd47b040135e7b119765e37357aca2a5decb984d53b9
4
- data.tar.gz: '0977180c69458fd15b22787c3a7f169fdf8b2002d0ad636571ba1257b99ec40c'
3
+ metadata.gz: 3e42c00897ec17d8b63b419e3ed0ac415c067474de1dbf1e9c058a20ff2b79d5
4
+ data.tar.gz: eae98863ae3ca0604e35181283df1a38977f55b1d80428fd5a9ed61974c9b19a
5
5
  SHA512:
6
- metadata.gz: cf286bbbcb7ef09a08acb48ae2f4749e4f2079a5f4866e14116a026e96126383ef96422e9bf14a124e4050def1ca9e6c05aa6c0c619b82a85028a7485e00d7a0
7
- data.tar.gz: cff193ff53153f5c892194668afda4bd4c4568a9dc5a187db801a6264d43bf01b4ca4adf690eef6b9a279f9d37962d28a5b65577ac3715a7e026f82512b32f35
6
+ metadata.gz: 92f27ab04cb522e2aa3c61e76d1aa231245e40c54969de1e23a57b2d6ede741f5b330fabdd88bbf1ed926164d5ab60e16d95f14304ce94a75e049028d81583a2
7
+ data.tar.gz: ac6611999cf8a7c8ec1b0e22f192083ebdfa800fdbd999b49f5bdb18909f453ecc3610ec6a3cf2c7632c74e56cdcd899750dbd396523da2e09c984e42261a2cd
data/.release_history.yml CHANGED
@@ -6,3 +6,5 @@ smerp-quotation:
6
6
  :timestamp: 1664783292.2046304
7
7
  - :version: 0.2.1
8
8
  :timestamp: 1664817326.603613
9
+ - :version: 0.2.2
10
+ :timestamp: 1664874428.3551033
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smerp-quotation (0.2.2)
4
+ smerp-quotation (0.2.3)
5
5
  acts_as_list
6
6
  acts_as_tree
7
7
  ca-data_store
@@ -33,15 +33,17 @@ module Smerp
33
33
 
34
34
  belongs_to :quotation_item_group, optional: true
35
35
 
36
- before_save :update_total
37
- after_save :update_group_total, :update_quotation_total
36
+ #before_save :update_total
37
+ #after_save :update_group_total, :update_quotation_total
38
+
39
+ around_save :update_related_around_save
38
40
 
39
41
  #before_destroy :update_total, :update_group_total, :update_quotation_total, :remove_total_from_destination_quotation_items
40
42
  #after_destroy :update_group_total, :update_quotation_total, :remove_total_from_destination_quotation_items
41
43
  #after_destroy :update_quotation_total, :remove_total_from_destination_quotation_items
42
44
  around_destroy :destroy_hook
43
45
 
44
- def update_total
46
+ def update_related_around_save
45
47
 
46
48
  if self.children.length == 0
47
49
  self.line_total = self.quantity * self.unit_price if self.quantity_changed? or self.unit_price_changed?
@@ -144,9 +146,213 @@ module Smerp
144
146
  self.extended_calculators = YAML.dump(res)
145
147
  ## Calculator execution completed
146
148
 
147
- teLogger.debug "before save done : #{self.inspect}"
149
+ # cater parent changed
150
+ oldParent = nil
151
+ if self.parent_id_changed? and not self.parent_id_was.nil?
152
+ oldParent = QuotationItem.find(self.parent_id_was)
153
+ end
154
+
155
+ # cater group changed
156
+ oldGroup = nil
157
+ if self.quotation_item_group_id_changed? and not self.quotation_item_group_id_was.nil?
158
+ oldGroup = QuotationItemGroup.find(self.quotation_item_group_id)
159
+ end
160
+
161
+ ###
162
+ # SAVE HAPPENED HERE
163
+ ###
164
+ yield
165
+
166
+
167
+ #
168
+ # AFTER SAVE
169
+ #
170
+ grp = self.quotation_item_group
171
+ [grp, oldGroup].each do |g|
172
+
173
+ if not g.nil?
174
+ # update total
175
+ g.group_total = g.children.sum(:group_total) + g.quotation_items.sum(:line_total)
176
+ g.group_discount = g.children.sum(:group_discount) + g.quotation_items.sum(:discount)
177
+ g.group_tax = g.children.sum(:group_tax) + g.quotation_items.sum(:tax)
178
+ g.save
179
+ end
180
+ end
181
+
182
+
183
+ [self.parent, oldParent].each do |pa|
184
+
185
+ if not pa.nil?
186
+
187
+ if pa.is_children_linked?
188
+
189
+ teLogger.debug "Parent #{pa.inspect} IS children linked"
190
+ #
191
+ # Children of another QuotationItem
192
+ #
193
+ if pa.quantity > 0
194
+
195
+ # this parent children has changed because self
196
+ # has changed its parent to other parent
197
+ pa.children.reload
198
+
199
+ # for children item that has a parent
200
+ # update parent line_total
201
+ pa.line_total = pa.children.sum(:line_total)
202
+ pa.consolidated_line_total = pa.children.sum(:consolidated_line_total)
203
+
204
+ # update unit_price since line_total is sum of children's line_total
205
+ pa.unit_price = pa.consolidated_line_total / pa.quantity
206
+
207
+ pa.discount = pa.children.sum(:discount)
208
+ pa.line_total_after_discount = pa.children.sum(:line_total_after_discount)
209
+ pa.tax = pa.children.sum(:tax)
210
+ pa.line_total_with_tax = pa.children.sum(:line_total_with_tax)
211
+
212
+ #teLogger.debug "After update from children : #{self.parent.inspect}"
213
+
214
+ else
215
+
216
+ pa.line_total = 0.0
217
+ pa.consolidated_line_total = 0.0
218
+ pa.unit_price = 0.0
219
+ pa.discount = 0.0
220
+ pa.line_total_after_discount = 0.0
221
+ pa.tax = 0.0
222
+ pa.line_total_with_tax = 0.0
223
+ end
224
+
225
+ pa.save
226
+
227
+ else
228
+ teLogger.debug "Parent #{pa.inspect} is NOT children linked. Skipping children update to parent."
229
+ end
230
+
231
+ end # if parent exist
232
+ end
233
+
234
+ # calculate if I'm the source to be embedded into other items
235
+ recalculate_total_to_destination_quotation_items
236
+
237
+ #self.quotation.update_total_from_children
238
+
239
+ self.quotation.total = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total)
240
+ self.quotation.total_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:discount)
241
+ self.quotation.total_after_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_after_discount)
242
+ self.quotation.total_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:tax)
243
+ self.quotation.total_with_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_with_tax)
244
+
245
+ self.quotation.save
246
+
148
247
  end
149
248
 
249
+ #def update_total
250
+
251
+ # if self.children.length == 0
252
+ # self.line_total = self.quantity * self.unit_price if self.quantity_changed? or self.unit_price_changed?
253
+ # self.line_total_after_discount = self.line_total
254
+ # self.line_total_with_tax = self.line_total
255
+
256
+ # #teLogger.debug "sync consolidated #{self.inspect} to line_total #{self.line_total.to_i}"
257
+ # self.consolidated_line_total = self.line_total if self.new_record?
258
+ # #teLogger.debug "sync consolidated #{self.inspect} to line_total #{self.line_total.to_i}"
259
+
260
+ # else
261
+ # #teLogger.debug "#{self.inspect} has children : #{self.children}"
262
+ # end
263
+
264
+ # if self.quantity_changed?
265
+ # teLogger.debug "Quantity changed"
266
+ # # Change unit_price for record that has children
267
+ # # since unit_price is 'derived' from children's line_total
268
+ # if self.quantity > 0
269
+ # self.unit_price = self.line_total / self.quantity
270
+ # else
271
+ # self.unit_price = 0.0
272
+ # self.line_total = 0.0
273
+ # end
274
+ # end
275
+
276
+ # #teLogger.debug "Before recalculate : #{self.inspect}"
277
+ # ## Calculate consolidated line total
278
+ # # Calculate line total of each item that push to this parent item
279
+ # if not self.new_record?
280
+ # recalculate_total_from_source_quotation_items
281
+ # end
282
+ # #teLogger.debug "After recalculate : #{self.inspect}"
283
+
284
+ # #
285
+ # # Start trigger defined calculators
286
+ # #
287
+ # res = { local: [], quotation: [] }
288
+ # extCals = { local: [], quotation: [] }
289
+ # if not_empty?(self.extended_calculators)
290
+ # extCals = YAML.load(self.extended_calculators)
291
+ # end
292
+
293
+ # extCals[:local].each do |ec|
294
+ # teLogger.debug "Executing item calculator : #{ec.inspect}"
295
+ # ec.calculate(self)
296
+ # ec.owner_id = self.id if is_empty?(ec.owner_id) and not self.new_record?
297
+ # res[:local] << ec.for_storage
298
+ # end
299
+
300
+ # if self.new_record?
301
+ #
302
+ # # apply global calculator from quotation
303
+ # gExtCals = []
304
+ # if not_empty?(self.quotation.extended_calculators)
305
+ # gExtCals = YAML.load(self.quotation.extended_calculators)
306
+ # end
307
+
308
+ # #cals = []
309
+ # gExtCals.each do |ec|
310
+
311
+ # teLogger.debug "Executing item-quotation calculator : #{ec.inspect}"
312
+ # ec.calculate(self)
313
+ # ec.owner_id = self.quotation.id if is_empty?(ec.owner_id)
314
+ # res[:quotation] << ec.for_storage
315
+
316
+ # end
317
+
318
+ # else
319
+
320
+ # extCals[:quotation].each do |ec|
321
+ # teLogger.debug "Executing item-quotation calculator : #{ec.inspect}"
322
+ # ec.calculate(self)
323
+ # ec.owner_id = self.quotation.id if is_empty?(ec.owner_id)
324
+ # res[:quotation] << ec.for_storage
325
+ # end
326
+ #
327
+ # end
328
+
329
+
330
+ # # after calculate, remove item with zero params
331
+ # res[:local].delete_if { |c|
332
+ # case c.params
333
+ # when Smerp::Common::FinUtils::Percent
334
+ # c.params.value == 0
335
+ # else
336
+ # c.params.to_i == 0
337
+ # end
338
+ # }
339
+
340
+ # res[:quotation].delete_if { |c|
341
+ # case c.params
342
+ # when Smerp::Common::FinUtils::Percent
343
+ # c.params.value == 0
344
+ # else
345
+ # c.params.to_i == 0
346
+ # end
347
+ # }
348
+
349
+ # self.extended_calculators = YAML.dump(res)
350
+ # ## Calculator execution completed
351
+
352
+ # teLogger.debug "before save done : #{self.inspect}"
353
+
354
+ #end
355
+
150
356
  def update_group_total
151
357
 
152
358
  grp = self.quotation_item_group
@@ -243,113 +449,119 @@ module Smerp
243
449
 
244
450
  end
245
451
 
246
- def update_quotation_total
452
+ # trigger after save
453
+ #def update_quotation_total
247
454
 
248
- if not self.parent.nil?
455
+ # if not self.parent.nil?
249
456
 
250
- self.parent.reload
457
+ # self.parent.reload
251
458
 
252
- if self.parent.is_children_linked?
459
+ # if self.parent.is_children_linked?
253
460
 
254
- teLogger.debug "Parent #{self.parent.inspect} IS children linked"
255
- #
256
- # Children of another QuotationItem
257
- #
258
- if self.parent.quantity > 0
259
- # for children item that has a parent
260
- # update parent line_total
261
- self.parent.line_total = self.parent.children.sum(:line_total)
262
- self.parent.consolidated_line_total = self.parent.children.sum(:consolidated_line_total)
461
+ # teLogger.debug "Parent #{self.parent.inspect} IS children linked"
462
+ # #
463
+ # # Children of another QuotationItem
464
+ # #
465
+ # if self.parent.quantity > 0
466
+ #
467
+ # # this parent children has changed because self
468
+ # # has changed its parent to other parent
469
+ # self.parent.children.reload
263
470
 
264
- # update unit_price since line_total is sum of children's line_total
265
- self.parent.unit_price = self.parent.consolidated_line_total / self.parent.quantity
471
+ # # for children item that has a parent
472
+ # # update parent line_total
473
+ # self.parent.line_total = self.parent.children.sum(:line_total)
474
+ # self.parent.consolidated_line_total = self.parent.children.sum(:consolidated_line_total)
266
475
 
267
- self.parent.discount = self.parent.children.sum(:discount)
268
- self.parent.line_total_after_discount = self.parent.children.sum(:line_total_after_discount)
269
- self.parent.tax = self.parent.children.sum(:tax)
270
- self.parent.line_total_with_tax = self.parent.children.sum(:line_total_with_tax)
476
+ # # update unit_price since line_total is sum of children's line_total
477
+ # self.parent.unit_price = self.parent.consolidated_line_total / self.parent.quantity
271
478
 
272
- #teLogger.debug "After update from children : #{self.parent.inspect}"
479
+ # self.parent.discount = self.parent.children.sum(:discount)
480
+ # self.parent.line_total_after_discount = self.parent.children.sum(:line_total_after_discount)
481
+ # self.parent.tax = self.parent.children.sum(:tax)
482
+ # self.parent.line_total_with_tax = self.parent.children.sum(:line_total_with_tax)
273
483
 
274
- else
484
+ # #teLogger.debug "After update from children : #{self.parent.inspect}"
275
485
 
276
- self.parent.line_total = 0.0
277
- self.parent.consolidated_line_total = 0.0
278
- self.parent.unit_price = 0.0
279
- self.parent.discount = 0.0
280
- self.parent.line_total_after_discount = 0.0
281
- self.parent.tax = 0.0
282
- self.parent.line_total_with_tax = 0.0
283
- end
486
+ # else
284
487
 
285
- self.parent.save
488
+ # self.parent.line_total = 0.0
489
+ # self.parent.consolidated_line_total = 0.0
490
+ # self.parent.unit_price = 0.0
491
+ # self.parent.discount = 0.0
492
+ # self.parent.line_total_after_discount = 0.0
493
+ # self.parent.tax = 0.0
494
+ # self.parent.line_total_with_tax = 0.0
495
+ # end
286
496
 
287
- else
288
- teLogger.debug "Parent #{self.parent.inspect} is NOT children linked. Skipping children update to parent."
289
- end
497
+ # self.parent.save
290
498
 
291
- end # if parent exist
499
+ # else
500
+ # teLogger.debug "Parent #{self.parent.inspect} is NOT children linked. Skipping children update to parent."
501
+ # end
292
502
 
293
- # calculate if I'm the source to be embedded into other items
294
- recalculate_total_to_destination_quotation_items
503
+ # end # if parent exist
295
504
 
296
- #self.quotation.update_total_from_children
505
+ # # calculate if I'm the source to be embedded into other items
506
+ # recalculate_total_to_destination_quotation_items
297
507
 
298
- self.quotation.total = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total)
299
- self.quotation.total_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:discount)
300
- self.quotation.total_after_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_after_discount)
301
- self.quotation.total_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:tax)
302
- self.quotation.total_with_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_with_tax)
508
+ # #self.quotation.update_total_from_children
303
509
 
304
- self.quotation.save
510
+ # self.quotation.total = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total)
511
+ # self.quotation.total_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:discount)
512
+ # self.quotation.total_after_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_after_discount)
513
+ # self.quotation.total_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:tax)
514
+ # self.quotation.total_with_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_with_tax)
305
515
 
306
- end
516
+ # self.quotation.save
517
+
518
+ #end
307
519
 
308
520
  # this called after child is destroy
309
- def update_self_as_parent
521
+ #def update_self_as_parent
310
522
 
311
- self.reload
523
+ # self.reload
312
524
 
313
- if self.is_children_linked?
525
+ # if self.is_children_linked?
314
526
 
315
- teLogger.debug "Parent #{self.inspect} IS children linked"
316
- #
317
- # Children of another QuotationItem
318
- #
319
- if self.quantity > 0
320
- # for children item that has a parent
321
- # update parent line_total
322
- self.line_total = self.children.sum(:line_total)
323
- self.consolidated_line_total = self.children.sum(:consolidated_line_total)
527
+ # teLogger.debug "Parent #{self.inspect} IS children linked"
528
+ # #
529
+ # # Children of another QuotationItem
530
+ # #
531
+ # if self.quantity > 0
532
+ # # for children item that has a parent
533
+ # # update parent line_total
534
+ # self.line_total = self.children.sum(:line_total)
535
+ # self.consolidated_line_total = self.children.sum(:consolidated_line_total)
324
536
 
325
- # update unit_price since line_total is sum of children's line_total
326
- self.unit_price = self.consolidated_line_total / self.quantity
537
+ # # update unit_price since line_total is sum of children's line_total
538
+ # self.unit_price = self.consolidated_line_total / self.quantity
327
539
 
328
- self.discount = self.children.sum(:discount)
329
- self.line_total_after_discount = self.children.sum(:line_total_after_discount)
330
- self.tax = self.children.sum(:tax)
331
- self.line_total_with_tax = self.children.sum(:line_total_with_tax)
540
+ # self.discount = self.children.sum(:discount)
541
+ # self.line_total_after_discount = self.children.sum(:line_total_after_discount)
542
+ # self.tax = self.children.sum(:tax)
543
+ # self.line_total_with_tax = self.children.sum(:line_total_with_tax)
332
544
 
333
- #teLogger.debug "After update from children : #{self.parent.inspect}"
545
+ # #teLogger.debug "After update from children : #{self.parent.inspect}"
334
546
 
335
- else
547
+ # else
336
548
 
337
- self.line_total = 0.0
338
- self.consolidated_line_total = 0.0
339
- self.unit_price = 0.0
340
- self.discount = 0.0
341
- self.line_total_after_discount = 0.0
342
- self.tax = 0.0
343
- self.line_total_with_tax = 0.0
344
- end
549
+ # self.line_total = 0.0
550
+ # self.consolidated_line_total = 0.0
551
+ # self.unit_price = 0.0
552
+ # self.discount = 0.0
553
+ # self.line_total_after_discount = 0.0
554
+ # self.tax = 0.0
555
+ # self.line_total_with_tax = 0.0
556
+ # end
345
557
 
346
- self.save
558
+ # self.save
347
559
 
348
- else
349
- teLogger.debug "Parent #{self.inspect} is NOT children linked. Skipping children update to parent."
350
- end
351
-
352
- end
560
+ # else
561
+ # teLogger.debug "Parent #{self.inspect} is NOT children linked. Skipping children update to parent."
562
+ # end
563
+
564
+ #end
353
565
 
354
566
 
355
567
  def recalculate
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Smerp
4
4
  module Quotation
5
- VERSION = "0.2.2"
5
+ VERSION = "0.2.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smerp-quotation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian