Guest User

Untitled

a guest
Apr 24th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.22 KB | None | 0 0
  1. Index: /Users/seandick/coding/trunk/app/models/purchase_order.rb
  2. ===================================================================
  3. --- /Users/seandick/coding/trunk/app/models/purchase_order.rb (revision 6954)
  4. +++ /Users/seandick/coding/trunk/app/models/purchase_order.rb (working copy)
  5. @@ -1,5 +1,5 @@
  6. class PurchaseOrder < ActiveRecord::Base
  7. - include CreateWithItems
  8. + include CreateWithItems, SearchParams
  9.  
  10. attr_accessor :validation_level
  11.  
  12. @@ -14,16 +14,7 @@
  13. # Added back old and incorrect left outer join sql. This is the current behaviour that doesn't work
  14. # without the finder_sql in ReceivedShipment.
  15. #
  16. - has_many(:items, :class_name => 'PurchaseOrderItem', :dependent => :destroy,
  17. - :finder_sql => 'select i.* from purchase_order_items i ' +
  18. - 'left outer join items i2 on i.item_id = i2.id ' +
  19. - 'left outer join products p on i2.product_id = p.id ' +
  20. - 'left outer join colors c on i2.color_id = c.id ' +
  21. - 'left outer join sizes s on i2.size_id = s.id ' +
  22. - 'where i.purchase_order_id = #{id} ' +
  23. - 'order by p.name, c.name, s.position',
  24. - :counter_sql => 'select count(*) from purchase_order_items i ' +
  25. - 'where i.purchase_order_id = #{id}')
  26. + has_many(:items, :class_name => 'PurchaseOrderItem', :dependent => :destroy)
  27.  
  28. has_many(:received_shipments, :order => 'created_at')
  29.  
  30. @@ -78,118 +69,38 @@
  31. def po_number
  32. return "P" + self.number.to_s
  33. end
  34. -
  35. - def self.get_search_query_data(params,where_items=nil,query_params=nil)
  36. - query = "
  37. - SELECT DISTINCT po.*
  38. - FROM purchase_orders po
  39. - LEFT OUTER JOIN purchase_order_items poi ON poi.purchase_order_id = po.id
  40. - LEFT OUTER JOIN items i ON i.id = poi.item_id
  41. - LEFT OUTER JOIN products p ON p.id = i.product_id
  42. - LEFT OUTER JOIN brands b ON b.id = p.brand_id
  43. - LEFT OUTER JOIN received_shipments rs ON rs.purchase_order_id = po.id
  44. - "
  45. - count_query = "
  46. - SELECT COUNT(DISTINCT po.id)
  47. - FROM purchase_orders po
  48. - LEFT OUTER JOIN purchase_order_items poi ON poi.purchase_order_id = po.id
  49. - LEFT OUTER JOIN items i ON i.id = poi.item_id
  50. - LEFT OUTER JOIN products p ON p.id = i.product_id
  51. - LEFT OUTER JOIN brands b ON b.id = p.brand_id
  52. - LEFT OUTER JOIN received_shipments rs ON rs.purchase_order_id = po.id
  53. - "
  54. +
  55. + # _________________________________
  56. + # |:::::::::::::;;::::::::::::::::::|
  57. + # |:::::::::::'~||~~~``:::::::::::::|
  58. + # |::::::::' .': o`:::::::::::|
  59. + # |:::::::' oo | |o o ::::::::::|
  60. + # |::::::: 8 .'.' 8 o :::::::::|
  61. + # |::::::: 8 | | 8 :::::::::|
  62. + # |::::::: _._| |_,...8 :::::::::|
  63. + # |::::::'~--. .--. `. `::::::::|
  64. + # |:::::' =8 ~ \ o ::::::::|
  65. + # |::::' 8._ 88. \ o::::::::|
  66. + # |:::' __. ,.ooo~~. \ o`::::::|
  67. + # |::: . -. 88`78o/: \ `:::::|
  68. + # |::' /. o o \ :: \88`::::| "There is no escape. Don't make me destroy you."
  69. + # |:; o|| 8 8 |d. `8 `:::|
  70. + # |:. - ^ ^ -' `-`::|
  71. + # |::. .:::|
  72. + # |:::::..... ::' ``::|
  73. + # |::::::::-'`- 88 `|
  74. + # |:::::-'. - :: |
  75. + # |:-~. . . : |
  76. + # | .. . ..: o:8 88o |
  77. + # |. . ::: 8:P d888. . . |
  78. + # |. . :88 88 888' . . |
  79. + # | o8 d88P . 88 ' d88P .. |
  80. + # | 88P 888 d8P ' 888 |
  81. + # | 8 d88P.'d:8 .- dP~ o8 |
  82. + # | 888 888 d~ o888 |
  83. + # |_________________________________|
  84.  
  85. - where_items = [] unless where_items
  86. - query_params = {} unless query_params
  87. -
  88. - if params[:brand]
  89. - where_items += ["b.name ilike :brand"]
  90. - query_params[:brand] = "%#{params[:brand]}%"
  91. - end
  92. -
  93. - if params[:season]
  94. - where_items += ["po.season ILIKE :season"]
  95. - query_params[:season] = "%#{params[:season].strip}%"
  96. - end
  97. -
  98. - if params[:invoice_number]
  99. - where_items += ["rs.number = :invoice_number"]
  100. - query_params[:invoice_number] = params[:invoice_number]
  101. - end
  102. -
  103. - if params[:product_string]
  104. - where_items += ["(p.name ILIKE :product OR p.description ILIKE :product OR p.code ILIKE :product)"]
  105. - query_params[:product] = "%#{params[:product_string].strip}%"
  106. - end
  107. -
  108. - if params[:buying_category_id]
  109. - where_items += ["p.buying_category_id = :buying_category_id"]
  110. - query_params[:buying_category_id] = params[:buying_category_id]
  111. - end
  112. -
  113. - if params[:shipment_number]
  114. - where_items += ["(rs.number ILIKE :shipment_number OR 'S' || rs.id ILIKE :shipment_number)"]
  115. - query_params[:shipment_number] = "%#{params[:shipment_number].gsub(/\s+/, "").strip}%"
  116. - end
  117. -
  118. - if params[:po_number]
  119. - where_items += ["CAST(po.id AS VARCHAR) = :po_number"]
  120. - query_params[:po_number] = "#{params[:po_number].gsub(/^P(\d+)/i, '\1').strip}"
  121. - end
  122. -
  123. - if params[:status_code] && !query_params[:status_code]
  124. - where_items += ["po.status_code = :status_code"]
  125. - query_params[:status_code] = params[:status_code]
  126. - end
  127. -
  128. - if params[:order_type]
  129. - where_items += ["po.order_type = :order_type"]
  130. - query_params[:order_type] = params[:order_type]
  131. - end
  132. -
  133. - if params[:created_by_id]
  134. - where_items += ["po.created_by_id = :created_by"]
  135. - query_params[:created_by] = params[:created_by_id]
  136. - end
  137. -
  138. - if params[:receive_date_from]
  139. - where_items += ["rs.receive_date >= :receive_from"]
  140. - query_params[:receive_from] = Date.parse(params[:receive_date_from])
  141. - end
  142. - if params[:receive_date_to]
  143. - where_items += ["rs.receive_date <= :receive_to"]
  144. - query_params[:receive_to] = Date.parse(params[:receive_date_to])
  145. - end
  146. -
  147. - # If an end date is supplied, use it to limit the start date. Don't
  148. - # look for items that have a start date in the range, and an end date in the range.
  149. - if params[:ship_date_from]
  150. - if params[:ship_date_to]
  151. - where_items += ["po.receive_start BETWEEN :receive_start AND :receive_end"]
  152. - query_params[:receive_end] = Date.parse(params[:ship_date_to])
  153. - else
  154. - where_items += ["po.receive_start >= :receive_start"]
  155. - end
  156. - query_params[:receive_start] = Date.parse(params[:ship_date_from])
  157. - elsif params[:ship_date_to]
  158. - query_params[:receive_end] = Date.parse(params[:ship_date_to])
  159. - where_items += ["po.receive_end <= :receive_end"]
  160. - end
  161. -
  162. - if (where_items.length > 0)
  163. - query += " WHERE " + where_items.join(' AND ')
  164. - count_query += " WHERE " + where_items.join(' AND ')
  165. - end
  166. -
  167. - query += " ORDER BY po.id DESC"
  168. -
  169. - return {
  170. - :query => query,
  171. - :count_query => count_query,
  172. - :query_params => query_params
  173. - }
  174. - end
  175. -
  176. +
  177. def total(olde_buying_category = nil)
  178. conditions = [ 'purchase_order_id = :id' ]
  179. args = {:id => self.id}
  180. @@ -274,6 +185,14 @@
  181. return [(total_quantity - total_received).to_i,0].max
  182. end
  183.  
  184. + def total_retail
  185. + total = 0
  186. + self.items.each do |i|
  187. + total += i.item.retail_price * i.quantity if i.item.retail_price && i.quantity
  188. + end
  189. + return total
  190. + end
  191. +
  192. def total_received_amount
  193. total = 0
  194. self.received_shipments.each do |rs|
  195. @@ -314,41 +233,46 @@
  196. return years
  197. end
  198.  
  199. - #
  200. +
  201. # The total number of product/color combinations for the POs
  202. - #
  203. - def self.total_skus(pos)
  204. - query = <<-EOS
  205. - SELECT COUNT(DISTINCT(colors.id)) FROM
  206. - colors INNER JOIN items on items.color_id = colors.id
  207. - INNER JOIN purchase_order_items on purchase_order_items.item_id = items.id
  208. - INNER JOIN purchase_orders on purchase_orders.id = purchase_order_items.purchase_order_id
  209. - EOS
  210. - query << "WHERE purchase_orders.id IN (#{pos.collect(&:id).join(", ")})"
  211. - return PurchaseOrder.connection.select_one(query)['count'].to_f
  212. - end
  213.  
  214. - def self.total_quantity(pos)
  215. - query = "SELECT SUM(quantity) FROM purchase_order_items WHERE purchase_order_id IN (#{pos.collect(&:id).join(", ")})"
  216. - return PurchaseOrder.connection.select_one(query)['sum'].to_f
  217. - end
  218. -
  219. - def self.total_cost(pos)
  220. - query = "SELECT SUM(quantity * cost) FROM purchase_order_items WHERE purchase_order_id IN (#{pos.collect(&:id).join(", ")})"
  221. - return PurchaseOrder.connection.select_one(query)['sum'].to_f
  222. - end
  223. -
  224. - def self.total_received_amount(pos)
  225. - query = <<-EOS
  226. - SELECT SUM(received_shipment_items.quantity * cost) FROM
  227. - purchase_orders INNER JOIN received_shipments ON received_shipments.purchase_order_id = purchase_orders.id
  228. - INNER JOIN received_shipment_items ON received_shipment_items.received_shipment_id = received_shipments.id
  229. - INNER JOIN items ON items.id = received_shipment_items.item_id
  230. - EOS
  231. - query << "WHERE purchase_orders.id IN (#{pos.collect(&:id).join(", ")})"
  232. - return PurchaseOrder.connection.select_one(query)['sum'].to_f
  233. - end
  234. -
  235. + def self.total_skus(pos)
  236. + query = <<-EOS
  237. + SELECT COUNT(DISTINCT(colors.id)) FROM
  238. + colors INNER JOIN items on items.color_id = colors.id
  239. + INNER JOIN purchase_order_items on purchase_order_items.item_id = items.id
  240. + INNER JOIN purchase_orders on purchase_orders.id = purchase_order_items.purchase_order_id
  241. + EOS
  242. + query << "WHERE purchase_orders.id IN (#{pos.collect(&:id).join(", ")})"
  243. + return PurchaseOrder.connection.select_one(query)['count'].to_f
  244. + end
  245. +
  246. + def self.total_quantity(pos)
  247. +
  248. + query = "SELECT SUM(quantity) FROM purchase_order_items WHERE purchase_order_id IN (#{pos.collect(&:id).join(", ")})"
  249. + return PurchaseOrder.connection.select_one(query)['sum'].to_f
  250. + end
  251. +
  252. + def self.total_cost(pos)
  253. + query = "SELECT SUM(quantity * cost) FROM purchase_order_items WHERE purchase_order_id IN (#{pos.collect(&:id).join(", ")})"
  254. + return PurchaseOrder.connection.select_one(query)['sum'].to_f
  255. + end
  256. +
  257. + def self.total_retail(pos)
  258. + return self.quantity * self.items.retail_price
  259. + end
  260. +
  261. + def self.total_received_amount(pos)
  262. + query = <<-EOS
  263. + SELECT SUM(received_shipment_items.quantity * cost) FROM
  264. + purchase_orders INNER JOIN received_shipments ON received_shipments.purchase_order_id = purchase_orders.id
  265. + INNER JOIN received_shipment_items ON received_shipment_items.received_shipment_id = received_shipments.id
  266. + INNER JOIN items ON items.id = received_shipment_items.item_id
  267. + EOS
  268. + query << "WHERE purchase_orders.id IN (#{pos.collect(&:id).join(", ")})"
  269. + return PurchaseOrder.connection.select_one(query)['sum'].to_f
  270. + end
  271. +
  272. def brands_of_items
  273. brands = []
  274. self.items.each {|poi| brands << poi.item.product.brand_id if !brands.include?(poi.item.product.brand_id)}
Add Comment
Please, Sign In to add comment