Guest User

Untitled

a guest
May 26th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. class ProductCountStruct < Struct.new(:product_id, :quantity, :product_code, :name, :unit_value, :unit_of_measure); end
  2.  
  3. class ProductCount < ActiveRecord::Base
  4. belongs_to :inventory_need
  5. belongs_to :product
  6.  
  7. def self.between_dates(start_date, end_date, area)
  8. all_archived_product_counts = Array.new
  9. area_condition = area_condition(area)
  10. #first get all the archived product counts as archived_delivery_order_items
  11. all_archived_product_counts = ArchivedDeliveryOrderItem.find(:all,
  12. :select => "adoi.product_id, sum(quantity) as quantity, adoi.product_code, adoi.name, adoi.unit_value, adoi.unit_of_measure",
  13. :joins => "as adoi inner join archived_delivery_orders as ado on ado.id = adoi.archived_delivery_order_id
  14. inner join customers as cu on cu.id = ado.customer_id
  15. inner join delivery_locations as dl on cu.id = dl.customer_id",
  16. :conditions => ["ado.delivery_date BETWEEN ? AND ? #{area_condition}", start_date, end_date],
  17. :group => "adoi.product_id, adoi.product_code, adoi.name, adoi.unit_value, adoi.unit_of_measure")
  18. archived_product_counts_without_ids = Array.new
  19. archived_product_counts_with_ids = Array.new
  20. # now separate out the archived_delivery_orders where there were no product_ids
  21. for product_count in all_archived_product_counts
  22. if product_count.product.nil? || product_count.product_id = 0
  23. archived_product_counts_without_ids << product_count
  24. else
  25. archived_product_counts_with_ids << product_count
  26. end
  27. end
  28. # now find the delivery_order product counts though only for customers who are not inactive.
  29. # first get nonsubstituted delivery order items.
  30. nonsub_unarchived_product_counts = DeliveryOrderItem.find( :all,
  31. :select => "doi.product_id, sum(quantity) as quantity, doi.product_code, doi.name, doi.unit_value, uom.name as unit_of_measure",
  32. :joins => "as doi inner join delivery_orders as deo on deo.id = doi.delivery_order_id
  33. inner join customers as cu on cu.id = deo.customer_id
  34. inner join products as pr on pr.id = doi.product_id
  35. inner join unit_of_measures as uom on pr.unit_of_measure_id = uom.id
  36. inner join delivery_locations as dl on cu.id = dl.customer_id",
  37. :conditions => ["deo.delivery_date BETWEEN ? AND ? AND cu.status <> ?
  38. AND pr.out_of_stock = false AND pr.out_of_season = false #{area_condition}",
  39. start_date, end_date, CustomerStatus::INACTIVE],
  40. :group => "doi.product_id, doi.product_code, doi.name, doi.unit_value, uom.name" )
  41. # add the delivery_order product_count to the archived product_counts with ids.
  42. # thankfully these are so similar that they can be grouped together easily
  43. archived_product_counts_with_ids += nonsub_unarchived_product_counts
  44. #now find the substituted delivery order items.
  45. sub_unarchived_product_counts = DeliveryOrderItem.find( :all,
  46. :select => "sub.id as product_id, sum(quantity) as quantity, sub.product_code, sub.name, sub.unit_value, uom.name as unit_of_measure",
  47. :joins => "as doi inner join delivery_orders as deo on deo.id = doi.delivery_order_id
  48. inner join customers as cu on cu.id = deo.customer_id
  49. inner join products as pr on pr.id = doi.product_id
  50. inner join products as sub on sub.id = pr.substitution_id
  51. inner join unit_of_measures as uom on sub.unit_of_measure_id = uom.id
  52. inner join delivery_locations as dl on cu.id = dl.customer_id",
  53. :conditions => ["deo.delivery_date BETWEEN ? AND ? AND cu.status <> ?
  54. AND (pr.out_of_stock = true OR pr.out_of_season = true) #{area_condition}",
  55. start_date, end_date, CustomerStatus::INACTIVE],
  56. :group => " sub.id, sub.product_code, sub.name, sub.unit_value, uom.name" )
  57. for substituted_item in sub_unarchived_product_counts
  58. substituted_item.name = substituted_item.name + " ** substitution"
  59. substituted_item.product_id = substituted_item.product_id + 10000
  60. end
  61. # now add in the substituted delivery order items.
  62. archived_product_counts_with_ids += sub_unarchived_product_counts
  63. #group the combined product_counts by product.id in order to get a sum of the quantities
  64. grouped_product_counts = archived_product_counts_with_ids.group_by {|product_count| product_count.product_id}
  65. product_counts = Array.new
  66. for key in grouped_product_counts.keys
  67. product_count = ProductCountStruct.new
  68. product_count.product_id = grouped_product_counts[key].last.product_id
  69. # this is the key line that gets the sum of the quantities to get a total count
  70. product_count.quantity = grouped_product_counts[key].sum {|pc| pc.quantity}
  71. product_count.product_code = grouped_product_counts[key].last.product_code
  72. product_count.name = grouped_product_counts[key].last.name
  73. product_count.unit_value = grouped_product_counts[key].last.unit_value
  74. product_count.unit_of_measure = grouped_product_counts[key].last.unit_of_measure
  75. product_counts << product_count
  76. end
  77. # now we add the product_counts without ids back in to the fold
  78. product_counts += archived_product_counts_without_ids
  79. #again we group though now by name in order to add the missing id product_count
  80. #to the quantity sums.
  81. all_grouped_product_counts = product_counts.group_by do |product_count|
  82. string = product_count.name
  83. string += product_count.unit_value.to_s unless product_count.unit_value.nil?
  84. string += product_count.unit_of_measure unless product_count.unit_of_measure.nil?
  85. string
  86. end
  87.  
  88. product_counts = Array.new
  89. for key in all_grouped_product_counts.keys
  90. product_count = ProductCountStruct.new
  91. product_count.product_id = all_grouped_product_counts[key].first.product_id
  92. product_count.quantity = all_grouped_product_counts[key].sum {|pc| pc.quantity}
  93. product_count.product_code = all_grouped_product_counts[key].first.product_code
  94. product_count.name = all_grouped_product_counts[key].first.name
  95. product_count.unit_value = all_grouped_product_counts[key].first.unit_value
  96. product_count.unit_of_measure = all_grouped_product_counts[key].first.unit_of_measure
  97. product_counts << product_count
  98. end
  99. #finally we do a sort by product_code and name.
  100. product_counts.sort_by {|product_count| [product_count.product_code, product_count.name]}
  101. end
  102.  
  103. private
  104.  
  105. def self.area_condition(area)
  106. case area
  107. when "All"
  108. return ""
  109. when "SanAntonio"
  110. return " AND dl.zip LIKE '782%'"
  111. when "Austin"
  112. return "AND dl.zip not LIKE '782%'"
  113. end
  114. end
  115. end
Add Comment
Please, Sign In to add comment