smerp-quotation 0.2.1 → 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: c2cb34cdeb9ad8ef119a801316aa9d874cbd0565f8ad246e91a5311fcae2a8af
4
- data.tar.gz: 944120a5dca8e07e6b1b3f9dcf7724e32c5ca586ffe18fc84bb6a394bf59c4ed
3
+ metadata.gz: 3e42c00897ec17d8b63b419e3ed0ac415c067474de1dbf1e9c058a20ff2b79d5
4
+ data.tar.gz: eae98863ae3ca0604e35181283df1a38977f55b1d80428fd5a9ed61974c9b19a
5
5
  SHA512:
6
- metadata.gz: c9b447f31c94f7fe6ec928556f2e92a85af3569eebc459a0b5efee5041ecab2c70ba8a1c34bc8be9b832839698b0840e1d83bd3f5e0e39ec81ec1236b2cef9fe
7
- data.tar.gz: 1f08fe39a26bd4f74a6598ff4a2d699acd3eee5d431e9e4b9923f418a766cf57516e9378f9f062f1a88d319ca8e9f743695392092845d1417bbfa6286115c450
6
+ metadata.gz: 92f27ab04cb522e2aa3c61e76d1aa231245e40c54969de1e23a57b2d6ede741f5b330fabdd88bbf1ed926164d5ab60e16d95f14304ce94a75e049028d81583a2
7
+ data.tar.gz: ac6611999cf8a7c8ec1b0e22f192083ebdfa800fdbd999b49f5bdb18909f453ecc3610ec6a3cf2c7632c74e56cdcd899750dbd396523da2e09c984e42261a2cd
data/.release_history.yml CHANGED
@@ -4,3 +4,7 @@ smerp-quotation:
4
4
  :timestamp: 1664514502.3722005
5
5
  - :version: 0.2.0
6
6
  :timestamp: 1664783292.2046304
7
+ - :version: 0.2.1
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.1)
4
+ smerp-quotation (0.2.3)
5
5
  acts_as_list
6
6
  acts_as_tree
7
7
  ca-data_store
@@ -100,9 +100,10 @@ module Smerp
100
100
  end
101
101
 
102
102
  def recalculate
103
- # call by calculator after processing
103
+ # call by calculator after rounding
104
104
  end
105
105
 
106
+
106
107
  end # class Quotation
107
108
  end
108
109
 
@@ -33,13 +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
38
 
39
- before_destroy :update_total
40
- after_destroy :update_group_total, :update_quotation_total, :remove_total_from_destination_quotation_items
39
+ around_save :update_related_around_save
41
40
 
42
- def update_total
41
+ #before_destroy :update_total, :update_group_total, :update_quotation_total, :remove_total_from_destination_quotation_items
42
+ #after_destroy :update_group_total, :update_quotation_total, :remove_total_from_destination_quotation_items
43
+ #after_destroy :update_quotation_total, :remove_total_from_destination_quotation_items
44
+ around_destroy :destroy_hook
45
+
46
+ def update_related_around_save
43
47
 
44
48
  if self.children.length == 0
45
49
  self.line_total = self.quantity * self.unit_price if self.quantity_changed? or self.unit_price_changed?
@@ -142,9 +146,213 @@ module Smerp
142
146
  self.extended_calculators = YAML.dump(res)
143
147
  ## Calculator execution completed
144
148
 
145
- 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
+
146
247
  end
147
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
+
148
356
  def update_group_total
149
357
 
150
358
  grp = self.quotation_item_group
@@ -158,66 +366,203 @@ module Smerp
158
366
 
159
367
  end
160
368
 
161
- def update_quotation_total
369
+ # Only for destroy operation
370
+ def destroy_hook
371
+
372
+ parent = self.parent.clone
373
+ qi_dist = self.quotation_item_distributions
374
+ quotation = self.quotation
162
375
 
163
- if not self.parent.nil?
376
+ # record deleteion here
377
+ yield
164
378
 
165
- self.parent.reload
379
+ if not parent.nil?
166
380
 
167
- if self.parent.is_children_linked?
381
+ if parent.is_children_linked?
168
382
 
169
- teLogger.debug "Parent #{self.parent.inspect} IS children linked"
383
+ teLogger.debug "Parent #{parent.inspect} IS children linked"
170
384
  #
171
385
  # Children of another QuotationItem
172
386
  #
173
- if self.parent.quantity > 0
387
+ if parent.quantity > 0
174
388
  # for children item that has a parent
175
389
  # update parent line_total
176
- self.parent.line_total = self.parent.children.sum(:line_total)
177
- self.parent.consolidated_line_total = self.parent.children.sum(:consolidated_line_total)
390
+ parent.line_total = parent.children.sum(:line_total)
391
+ parent.consolidated_line_total = parent.children.sum(:consolidated_line_total)
178
392
 
179
393
  # update unit_price since line_total is sum of children's line_total
180
- self.parent.unit_price = self.parent.consolidated_line_total / self.parent.quantity
394
+ parent.unit_price = parent.consolidated_line_total / parent.quantity
181
395
 
182
- self.parent.discount = self.parent.children.sum(:discount)
183
- self.parent.line_total_after_discount = self.parent.children.sum(:line_total_after_discount)
184
- self.parent.tax = self.parent.children.sum(:tax)
185
- self.parent.line_total_with_tax = self.parent.children.sum(:line_total_with_tax)
396
+ parent.discount = parent.children.sum(:discount)
397
+ parent.line_total_after_discount = parent.children.sum(:line_total_after_discount)
398
+ parent.tax = parent.children.sum(:tax)
399
+ parent.line_total_with_tax = parent.children.sum(:line_total_with_tax)
186
400
 
187
401
  #teLogger.debug "After update from children : #{self.parent.inspect}"
188
402
 
189
403
  else
190
404
 
191
- self.parent.line_total = 0.0
192
- self.parent.consolidated_line_total = 0.0
193
- self.parent.unit_price = 0.0
194
- self.parent.discount = 0.0
195
- self.parent.line_total_after_discount = 0.0
196
- self.parent.tax = 0.0
197
- self.parent.line_total_with_tax = 0.0
405
+ parent.line_total = 0.0
406
+ parent.consolidated_line_total = 0.0
407
+ parent.unit_price = 0.0
408
+ parent.discount = 0.0
409
+ parent.line_total_after_discount = 0.0
410
+ parent.tax = 0.0
411
+ parent.line_total_with_tax = 0.0
198
412
  end
199
413
 
200
- self.parent.save
414
+ parent.save
201
415
 
202
416
  else
203
- teLogger.debug "Parent #{self.parent.inspect} is NOT children linked. Skipping children update to parent."
417
+ teLogger.debug "Parent #{parent.inspect} is NOT children linked. Skipping children update to parent."
204
418
  end
205
419
 
206
420
  end # if parent exist
207
421
 
208
- # calculate if I'm the source to be embedded into other items
209
- recalculate_total_to_destination_quotation_items
422
+ # remove the distributed figure from other record if this record is the source item
423
+ qi_dist.each do |qid|
424
+
425
+ citm = qid.distributed_quotation_item
210
426
 
211
- self.quotation.total = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total)
212
- self.quotation.total_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:discount)
213
- self.quotation.total_after_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_after_discount)
214
- self.quotation.total_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:tax)
215
- self.quotation.total_with_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_with_tax)
427
+ next if citm.nil?
216
428
 
217
- self.quotation.save
429
+ cal = YAML.load(qid.distribution_calculations)
430
+ extRes = cal.result.clone
431
+
432
+ citm.consolidated_line_total = (citm.consolidated_line_total - extRes[Smerp::Quotation::DistributionCalculator::DistributedAmount])
433
+
434
+ qid.destroy
435
+
436
+ citm.source_quotation_items_link.reload
437
+
438
+ citm.save
439
+
440
+ end
441
+
442
+ quotation.total = quotation.quotation_items.where(["parent_id is null"]).sum(:line_total)
443
+ quotation.total_discount = quotation.quotation_items.where(["parent_id is null"]).sum(:discount)
444
+ quotation.total_after_discount = quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_after_discount)
445
+ quotation.total_tax = quotation.quotation_items.where(["parent_id is null"]).sum(:tax)
446
+ quotation.total_with_tax = quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_with_tax)
447
+
448
+ quotation.save
218
449
 
219
450
  end
220
451
 
452
+ # trigger after save
453
+ #def update_quotation_total
454
+
455
+ # if not self.parent.nil?
456
+
457
+ # self.parent.reload
458
+
459
+ # if self.parent.is_children_linked?
460
+
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
470
+
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)
475
+
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
478
+
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)
483
+
484
+ # #teLogger.debug "After update from children : #{self.parent.inspect}"
485
+
486
+ # else
487
+
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
496
+
497
+ # self.parent.save
498
+
499
+ # else
500
+ # teLogger.debug "Parent #{self.parent.inspect} is NOT children linked. Skipping children update to parent."
501
+ # end
502
+
503
+ # end # if parent exist
504
+
505
+ # # calculate if I'm the source to be embedded into other items
506
+ # recalculate_total_to_destination_quotation_items
507
+
508
+ # #self.quotation.update_total_from_children
509
+
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)
515
+
516
+ # self.quotation.save
517
+
518
+ #end
519
+
520
+ # this called after child is destroy
521
+ #def update_self_as_parent
522
+
523
+ # self.reload
524
+
525
+ # if self.is_children_linked?
526
+
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)
536
+
537
+ # # update unit_price since line_total is sum of children's line_total
538
+ # self.unit_price = self.consolidated_line_total / self.quantity
539
+
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)
544
+
545
+ # #teLogger.debug "After update from children : #{self.parent.inspect}"
546
+
547
+ # else
548
+
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
557
+
558
+ # self.save
559
+
560
+ # else
561
+ # teLogger.debug "Parent #{self.inspect} is NOT children linked. Skipping children update to parent."
562
+ # end
563
+
564
+ #end
565
+
221
566
 
222
567
  def recalculate
223
568
 
@@ -320,36 +665,6 @@ module Smerp
320
665
 
321
666
  end
322
667
 
323
- # item that is being distributed to other
324
- # quotation items is being destroyed
325
- # Remove those portion from each and every items
326
- def remove_total_from_destination_quotation_items
327
-
328
- self.quotation_item_distributions.each do |qid|
329
-
330
- citm = qid.distributed_quotation_item
331
-
332
- next if citm.nil?
333
-
334
- cal = YAML.load(qid.distribution_calculations)
335
- extRes = cal.result.clone
336
-
337
- #teLogger.debug "before citm #{citm.inspect} / #{citm.consolidated_line_total}"
338
- citm.consolidated_line_total = (citm.consolidated_line_total - extRes[Smerp::Quotation::DistributionCalculator::DistributedAmount])
339
- #teLogger.debug "after deduction citm #{citm.inspect} / #{citm.consolidated_line_total}"
340
-
341
- #teLogger.debug "qid : #{qid.inspect}"
342
- qid.destroy
343
-
344
- citm.source_quotation_items_link.reload
345
-
346
- citm.save
347
- #teLogger.debug "after citm #{citm.inspect} / #{citm.consolidated_line_total}"
348
-
349
- end
350
-
351
- end
352
-
353
668
  def total_distributed
354
669
 
355
670
  ttl = 0.0
@@ -49,7 +49,7 @@ module Smerp
49
49
  end
50
50
 
51
51
  res[:output] = @output_field
52
- mdl.recalculate
52
+ #mdl.recalculate
53
53
  end
54
54
 
55
55
  @result = res
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Smerp
4
4
  module Quotation
5
- VERSION = "0.2.1"
5
+ VERSION = "0.2.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smerp-quotation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-03 00:00:00.000000000 Z
11
+ date: 2022-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: toolrack