smerp-quotation 0.2.2 → 0.2.4
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a18dda7b56228271c1032e1a27ef4c0a1dabfa1ed8d44c219ba4b88a9ae4d5a
|
4
|
+
data.tar.gz: bd4083071f2cdf271a4eab01422282ad51fc5a55959e40f65514ba8af06e6c2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f52d9a81d3f337e75eb8abcb9087adc0faf6850b743ffe8378578dcaaa8550f118ae6085992dfb3631cb8c071971a7b9fefe97615787be4092ac7bd24fe6fb3d
|
7
|
+
data.tar.gz: cb3e06001da8068f511261852688f6c19aa2b3716b1db7ca6e99c1420f101e9134faf9014b060939127fbb5fd2dfde7975fdeb86070197a6e39950bb9b45d321
|
data/.release_history.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -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
|
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?
|
@@ -71,7 +73,7 @@ module Smerp
|
|
71
73
|
#teLogger.debug "Before recalculate : #{self.inspect}"
|
72
74
|
## Calculate consolidated line total
|
73
75
|
# Calculate line total of each item that push to this parent item
|
74
|
-
if not self.new_record?
|
76
|
+
if self.is_consolidation_destination_item? and not self.new_record?
|
75
77
|
recalculate_total_from_source_quotation_items
|
76
78
|
end
|
77
79
|
#teLogger.debug "After recalculate : #{self.inspect}"
|
@@ -144,9 +146,227 @@ module Smerp
|
|
144
146
|
self.extended_calculators = YAML.dump(res)
|
145
147
|
## Calculator execution completed
|
146
148
|
|
147
|
-
|
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
|
+
#pa.reload
|
188
|
+
|
189
|
+
if pa.is_children_linked?
|
190
|
+
|
191
|
+
teLogger.debug "Parent #{pa.inspect} IS children linked"
|
192
|
+
#
|
193
|
+
# Children of another QuotationItem
|
194
|
+
#
|
195
|
+
if pa.quantity > 0
|
196
|
+
|
197
|
+
# this parent children has changed because self
|
198
|
+
# has changed its parent to other parent
|
199
|
+
pa.children.reload
|
200
|
+
|
201
|
+
|
202
|
+
teLogger.debug "Before setting any price : #{pa.inspect}"
|
203
|
+
# for children item that has a parent
|
204
|
+
# update parent line_total
|
205
|
+
pa.line_total = pa.children.sum(:line_total)
|
206
|
+
pa.consolidated_line_total = pa.children.sum(:consolidated_line_total)
|
207
|
+
|
208
|
+
teLogger.debug "Before resetting unit price : #{pa.inspect}"
|
209
|
+
# update unit_price since line_total is sum of children's line_total
|
210
|
+
pa.unit_price = pa.consolidated_line_total / pa.quantity
|
211
|
+
teLogger.debug "After resetting unit price : #{pa.inspect}"
|
212
|
+
|
213
|
+
pa.discount = pa.children.sum(:discount)
|
214
|
+
pa.line_total_after_discount = pa.children.sum(:line_total_after_discount)
|
215
|
+
pa.tax = pa.children.sum(:tax)
|
216
|
+
pa.line_total_with_tax = pa.children.sum(:line_total_with_tax)
|
217
|
+
|
218
|
+
#teLogger.debug "After update from children : #{self.parent.inspect}"
|
219
|
+
|
220
|
+
else
|
221
|
+
|
222
|
+
pa.line_total = 0.0
|
223
|
+
pa.consolidated_line_total = 0.0
|
224
|
+
pa.unit_price = 0.0
|
225
|
+
pa.discount = 0.0
|
226
|
+
pa.line_total_after_discount = 0.0
|
227
|
+
pa.tax = 0.0
|
228
|
+
pa.line_total_with_tax = 0.0
|
229
|
+
end
|
230
|
+
|
231
|
+
teLogger.debug "Before save parent #{pa.changes}"
|
232
|
+
pa.save
|
233
|
+
|
234
|
+
#pobj = QuotationItem.find(pa.id)
|
235
|
+
#p pobj
|
236
|
+
#pa.changes.each do |k,v|
|
237
|
+
# pobj.send("#{k}=", v[1])
|
238
|
+
#end
|
239
|
+
#pobj.save
|
240
|
+
|
241
|
+
else
|
242
|
+
teLogger.debug "Parent #{pa.inspect} is NOT children linked. Skipping children update to parent."
|
243
|
+
end
|
244
|
+
|
245
|
+
end # if parent exist
|
246
|
+
end
|
247
|
+
|
248
|
+
# calculate if I'm the source to be embedded into other items
|
249
|
+
recalculate_total_to_destination_quotation_items if self.is_consolidation_source_item?
|
250
|
+
|
251
|
+
#self.quotation.update_total_from_children
|
252
|
+
|
253
|
+
self.quotation.total = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total)
|
254
|
+
self.quotation.total_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:discount)
|
255
|
+
self.quotation.total_after_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_after_discount)
|
256
|
+
self.quotation.total_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:tax)
|
257
|
+
self.quotation.total_with_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_with_tax)
|
258
|
+
|
259
|
+
self.quotation.save
|
260
|
+
|
148
261
|
end
|
149
262
|
|
263
|
+
#def update_total
|
264
|
+
|
265
|
+
# if self.children.length == 0
|
266
|
+
# self.line_total = self.quantity * self.unit_price if self.quantity_changed? or self.unit_price_changed?
|
267
|
+
# self.line_total_after_discount = self.line_total
|
268
|
+
# self.line_total_with_tax = self.line_total
|
269
|
+
|
270
|
+
# #teLogger.debug "sync consolidated #{self.inspect} to line_total #{self.line_total.to_i}"
|
271
|
+
# self.consolidated_line_total = self.line_total if self.new_record?
|
272
|
+
# #teLogger.debug "sync consolidated #{self.inspect} to line_total #{self.line_total.to_i}"
|
273
|
+
|
274
|
+
# else
|
275
|
+
# #teLogger.debug "#{self.inspect} has children : #{self.children}"
|
276
|
+
# end
|
277
|
+
|
278
|
+
# if self.quantity_changed?
|
279
|
+
# teLogger.debug "Quantity changed"
|
280
|
+
# # Change unit_price for record that has children
|
281
|
+
# # since unit_price is 'derived' from children's line_total
|
282
|
+
# if self.quantity > 0
|
283
|
+
# self.unit_price = self.line_total / self.quantity
|
284
|
+
# else
|
285
|
+
# self.unit_price = 0.0
|
286
|
+
# self.line_total = 0.0
|
287
|
+
# end
|
288
|
+
# end
|
289
|
+
|
290
|
+
# #teLogger.debug "Before recalculate : #{self.inspect}"
|
291
|
+
# ## Calculate consolidated line total
|
292
|
+
# # Calculate line total of each item that push to this parent item
|
293
|
+
# if not self.new_record?
|
294
|
+
# recalculate_total_from_source_quotation_items
|
295
|
+
# end
|
296
|
+
# #teLogger.debug "After recalculate : #{self.inspect}"
|
297
|
+
|
298
|
+
# #
|
299
|
+
# # Start trigger defined calculators
|
300
|
+
# #
|
301
|
+
# res = { local: [], quotation: [] }
|
302
|
+
# extCals = { local: [], quotation: [] }
|
303
|
+
# if not_empty?(self.extended_calculators)
|
304
|
+
# extCals = YAML.load(self.extended_calculators)
|
305
|
+
# end
|
306
|
+
|
307
|
+
# extCals[:local].each do |ec|
|
308
|
+
# teLogger.debug "Executing item calculator : #{ec.inspect}"
|
309
|
+
# ec.calculate(self)
|
310
|
+
# ec.owner_id = self.id if is_empty?(ec.owner_id) and not self.new_record?
|
311
|
+
# res[:local] << ec.for_storage
|
312
|
+
# end
|
313
|
+
|
314
|
+
# if self.new_record?
|
315
|
+
#
|
316
|
+
# # apply global calculator from quotation
|
317
|
+
# gExtCals = []
|
318
|
+
# if not_empty?(self.quotation.extended_calculators)
|
319
|
+
# gExtCals = YAML.load(self.quotation.extended_calculators)
|
320
|
+
# end
|
321
|
+
|
322
|
+
# #cals = []
|
323
|
+
# gExtCals.each do |ec|
|
324
|
+
|
325
|
+
# teLogger.debug "Executing item-quotation calculator : #{ec.inspect}"
|
326
|
+
# ec.calculate(self)
|
327
|
+
# ec.owner_id = self.quotation.id if is_empty?(ec.owner_id)
|
328
|
+
# res[:quotation] << ec.for_storage
|
329
|
+
|
330
|
+
# end
|
331
|
+
|
332
|
+
# else
|
333
|
+
|
334
|
+
# extCals[:quotation].each do |ec|
|
335
|
+
# teLogger.debug "Executing item-quotation calculator : #{ec.inspect}"
|
336
|
+
# ec.calculate(self)
|
337
|
+
# ec.owner_id = self.quotation.id if is_empty?(ec.owner_id)
|
338
|
+
# res[:quotation] << ec.for_storage
|
339
|
+
# end
|
340
|
+
#
|
341
|
+
# end
|
342
|
+
|
343
|
+
|
344
|
+
# # after calculate, remove item with zero params
|
345
|
+
# res[:local].delete_if { |c|
|
346
|
+
# case c.params
|
347
|
+
# when Smerp::Common::FinUtils::Percent
|
348
|
+
# c.params.value == 0
|
349
|
+
# else
|
350
|
+
# c.params.to_i == 0
|
351
|
+
# end
|
352
|
+
# }
|
353
|
+
|
354
|
+
# res[:quotation].delete_if { |c|
|
355
|
+
# case c.params
|
356
|
+
# when Smerp::Common::FinUtils::Percent
|
357
|
+
# c.params.value == 0
|
358
|
+
# else
|
359
|
+
# c.params.to_i == 0
|
360
|
+
# end
|
361
|
+
# }
|
362
|
+
|
363
|
+
# self.extended_calculators = YAML.dump(res)
|
364
|
+
# ## Calculator execution completed
|
365
|
+
|
366
|
+
# teLogger.debug "before save done : #{self.inspect}"
|
367
|
+
|
368
|
+
#end
|
369
|
+
|
150
370
|
def update_group_total
|
151
371
|
|
152
372
|
grp = self.quotation_item_group
|
@@ -243,113 +463,119 @@ module Smerp
|
|
243
463
|
|
244
464
|
end
|
245
465
|
|
246
|
-
|
466
|
+
# trigger after save
|
467
|
+
#def update_quotation_total
|
247
468
|
|
248
|
-
|
469
|
+
# if not self.parent.nil?
|
249
470
|
|
250
|
-
|
471
|
+
# self.parent.reload
|
251
472
|
|
252
|
-
|
473
|
+
# if self.parent.is_children_linked?
|
253
474
|
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
475
|
+
# teLogger.debug "Parent #{self.parent.inspect} IS children linked"
|
476
|
+
# #
|
477
|
+
# # Children of another QuotationItem
|
478
|
+
# #
|
479
|
+
# if self.parent.quantity > 0
|
480
|
+
#
|
481
|
+
# # this parent children has changed because self
|
482
|
+
# # has changed its parent to other parent
|
483
|
+
# self.parent.children.reload
|
263
484
|
|
264
|
-
|
265
|
-
|
485
|
+
# # for children item that has a parent
|
486
|
+
# # update parent line_total
|
487
|
+
# self.parent.line_total = self.parent.children.sum(:line_total)
|
488
|
+
# self.parent.consolidated_line_total = self.parent.children.sum(:consolidated_line_total)
|
266
489
|
|
267
|
-
|
268
|
-
|
269
|
-
self.parent.tax = self.parent.children.sum(:tax)
|
270
|
-
self.parent.line_total_with_tax = self.parent.children.sum(:line_total_with_tax)
|
490
|
+
# # update unit_price since line_total is sum of children's line_total
|
491
|
+
# self.parent.unit_price = self.parent.consolidated_line_total / self.parent.quantity
|
271
492
|
|
272
|
-
|
493
|
+
# self.parent.discount = self.parent.children.sum(:discount)
|
494
|
+
# self.parent.line_total_after_discount = self.parent.children.sum(:line_total_after_discount)
|
495
|
+
# self.parent.tax = self.parent.children.sum(:tax)
|
496
|
+
# self.parent.line_total_with_tax = self.parent.children.sum(:line_total_with_tax)
|
273
497
|
|
274
|
-
|
498
|
+
# #teLogger.debug "After update from children : #{self.parent.inspect}"
|
275
499
|
|
276
|
-
|
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
|
500
|
+
# else
|
284
501
|
|
285
|
-
|
502
|
+
# self.parent.line_total = 0.0
|
503
|
+
# self.parent.consolidated_line_total = 0.0
|
504
|
+
# self.parent.unit_price = 0.0
|
505
|
+
# self.parent.discount = 0.0
|
506
|
+
# self.parent.line_total_after_discount = 0.0
|
507
|
+
# self.parent.tax = 0.0
|
508
|
+
# self.parent.line_total_with_tax = 0.0
|
509
|
+
# end
|
286
510
|
|
287
|
-
|
288
|
-
teLogger.debug "Parent #{self.parent.inspect} is NOT children linked. Skipping children update to parent."
|
289
|
-
end
|
511
|
+
# self.parent.save
|
290
512
|
|
291
|
-
|
513
|
+
# else
|
514
|
+
# teLogger.debug "Parent #{self.parent.inspect} is NOT children linked. Skipping children update to parent."
|
515
|
+
# end
|
292
516
|
|
293
|
-
|
294
|
-
recalculate_total_to_destination_quotation_items
|
517
|
+
# end # if parent exist
|
295
518
|
|
296
|
-
|
519
|
+
# # calculate if I'm the source to be embedded into other items
|
520
|
+
# recalculate_total_to_destination_quotation_items
|
297
521
|
|
298
|
-
|
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)
|
522
|
+
# #self.quotation.update_total_from_children
|
303
523
|
|
304
|
-
|
524
|
+
# self.quotation.total = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total)
|
525
|
+
# self.quotation.total_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:discount)
|
526
|
+
# self.quotation.total_after_discount = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_after_discount)
|
527
|
+
# self.quotation.total_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:tax)
|
528
|
+
# self.quotation.total_with_tax = self.quotation.quotation_items.where(["parent_id is null"]).sum(:line_total_with_tax)
|
305
529
|
|
306
|
-
|
530
|
+
# self.quotation.save
|
531
|
+
|
532
|
+
#end
|
307
533
|
|
308
534
|
# this called after child is destroy
|
309
|
-
def update_self_as_parent
|
535
|
+
#def update_self_as_parent
|
310
536
|
|
311
|
-
|
537
|
+
# self.reload
|
312
538
|
|
313
|
-
|
539
|
+
# if self.is_children_linked?
|
314
540
|
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
541
|
+
# teLogger.debug "Parent #{self.inspect} IS children linked"
|
542
|
+
# #
|
543
|
+
# # Children of another QuotationItem
|
544
|
+
# #
|
545
|
+
# if self.quantity > 0
|
546
|
+
# # for children item that has a parent
|
547
|
+
# # update parent line_total
|
548
|
+
# self.line_total = self.children.sum(:line_total)
|
549
|
+
# self.consolidated_line_total = self.children.sum(:consolidated_line_total)
|
324
550
|
|
325
|
-
|
326
|
-
|
551
|
+
# # update unit_price since line_total is sum of children's line_total
|
552
|
+
# self.unit_price = self.consolidated_line_total / self.quantity
|
327
553
|
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
554
|
+
# self.discount = self.children.sum(:discount)
|
555
|
+
# self.line_total_after_discount = self.children.sum(:line_total_after_discount)
|
556
|
+
# self.tax = self.children.sum(:tax)
|
557
|
+
# self.line_total_with_tax = self.children.sum(:line_total_with_tax)
|
332
558
|
|
333
|
-
|
559
|
+
# #teLogger.debug "After update from children : #{self.parent.inspect}"
|
334
560
|
|
335
|
-
|
561
|
+
# else
|
336
562
|
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
563
|
+
# self.line_total = 0.0
|
564
|
+
# self.consolidated_line_total = 0.0
|
565
|
+
# self.unit_price = 0.0
|
566
|
+
# self.discount = 0.0
|
567
|
+
# self.line_total_after_discount = 0.0
|
568
|
+
# self.tax = 0.0
|
569
|
+
# self.line_total_with_tax = 0.0
|
570
|
+
# end
|
345
571
|
|
346
|
-
|
572
|
+
# self.save
|
347
573
|
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
end
|
574
|
+
# else
|
575
|
+
# teLogger.debug "Parent #{self.inspect} is NOT children linked. Skipping children update to parent."
|
576
|
+
# end
|
577
|
+
|
578
|
+
#end
|
353
579
|
|
354
580
|
|
355
581
|
def recalculate
|
@@ -7,10 +7,40 @@ module Smerp
|
|
7
7
|
class QuotationItemDistribution < Ca::DataStore::Ar::Model
|
8
8
|
include TR::CondUtils
|
9
9
|
|
10
|
+
include TeLogger::TeLogHelper
|
11
|
+
teLogger_tag :qiDist
|
12
|
+
|
10
13
|
belongs_to :quotation_item
|
11
14
|
belongs_to :distributed_quotation_item, class_name: 'QuotationItem'
|
12
15
|
|
13
16
|
|
17
|
+
around_destroy :update_link_total
|
18
|
+
|
19
|
+
def update_link_total
|
20
|
+
|
21
|
+
src = self.quotation_item
|
22
|
+
dest = self.distributed_quotation_item
|
23
|
+
cal = YAML.load(self.distribution_calculations)
|
24
|
+
|
25
|
+
##
|
26
|
+
# Deleted
|
27
|
+
##
|
28
|
+
yield
|
29
|
+
|
30
|
+
|
31
|
+
teLogger.debug "Result : #{cal.result}"
|
32
|
+
distVal = cal.result[Smerp::Quotation::DistributionCalculator::DistributedAmount]
|
33
|
+
|
34
|
+
#teLogger.debug "Source : #{src.inspect}"
|
35
|
+
#src.consolidated_line_total += distVal
|
36
|
+
#src.save
|
37
|
+
|
38
|
+
teLogger.debug "Dest : #{dest.inspect}"
|
39
|
+
dest.consolidated_line_total -= distVal
|
40
|
+
dest.save
|
41
|
+
|
42
|
+
end
|
43
|
+
|
14
44
|
#after_save :distribute
|
15
45
|
|
16
46
|
#def distribute
|