Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: /Users/seandick/coding/trunk/app/models/purchase_order.rb
- ===================================================================
- --- /Users/seandick/coding/trunk/app/models/purchase_order.rb (revision 6954)
- +++ /Users/seandick/coding/trunk/app/models/purchase_order.rb (working copy)
- @@ -1,5 +1,5 @@
- class PurchaseOrder < ActiveRecord::Base
- - include CreateWithItems
- + include CreateWithItems, SearchParams
- attr_accessor :validation_level
- @@ -14,16 +14,7 @@
- # Added back old and incorrect left outer join sql. This is the current behaviour that doesn't work
- # without the finder_sql in ReceivedShipment.
- #
- - has_many(:items, :class_name => 'PurchaseOrderItem', :dependent => :destroy,
- - :finder_sql => 'select i.* from purchase_order_items i ' +
- - 'left outer join items i2 on i.item_id = i2.id ' +
- - 'left outer join products p on i2.product_id = p.id ' +
- - 'left outer join colors c on i2.color_id = c.id ' +
- - 'left outer join sizes s on i2.size_id = s.id ' +
- - 'where i.purchase_order_id = #{id} ' +
- - 'order by p.name, c.name, s.position',
- - :counter_sql => 'select count(*) from purchase_order_items i ' +
- - 'where i.purchase_order_id = #{id}')
- + has_many(:items, :class_name => 'PurchaseOrderItem', :dependent => :destroy)
- has_many(:received_shipments, :order => 'created_at')
- @@ -78,118 +69,38 @@
- def po_number
- return "P" + self.number.to_s
- end
- -
- - def self.get_search_query_data(params,where_items=nil,query_params=nil)
- - query = "
- - SELECT DISTINCT po.*
- - FROM purchase_orders po
- - LEFT OUTER JOIN purchase_order_items poi ON poi.purchase_order_id = po.id
- - LEFT OUTER JOIN items i ON i.id = poi.item_id
- - LEFT OUTER JOIN products p ON p.id = i.product_id
- - LEFT OUTER JOIN brands b ON b.id = p.brand_id
- - LEFT OUTER JOIN received_shipments rs ON rs.purchase_order_id = po.id
- - "
- - count_query = "
- - SELECT COUNT(DISTINCT po.id)
- - FROM purchase_orders po
- - LEFT OUTER JOIN purchase_order_items poi ON poi.purchase_order_id = po.id
- - LEFT OUTER JOIN items i ON i.id = poi.item_id
- - LEFT OUTER JOIN products p ON p.id = i.product_id
- - LEFT OUTER JOIN brands b ON b.id = p.brand_id
- - LEFT OUTER JOIN received_shipments rs ON rs.purchase_order_id = po.id
- - "
- +
- + # _________________________________
- + # |:::::::::::::;;::::::::::::::::::|
- + # |:::::::::::'~||~~~``:::::::::::::|
- + # |::::::::' .': o`:::::::::::|
- + # |:::::::' oo | |o o ::::::::::|
- + # |::::::: 8 .'.' 8 o :::::::::|
- + # |::::::: 8 | | 8 :::::::::|
- + # |::::::: _._| |_,...8 :::::::::|
- + # |::::::'~--. .--. `. `::::::::|
- + # |:::::' =8 ~ \ o ::::::::|
- + # |::::' 8._ 88. \ o::::::::|
- + # |:::' __. ,.ooo~~. \ o`::::::|
- + # |::: . -. 88`78o/: \ `:::::|
- + # |::' /. o o \ :: \88`::::| "There is no escape. Don't make me destroy you."
- + # |:; o|| 8 8 |d. `8 `:::|
- + # |:. - ^ ^ -' `-`::|
- + # |::. .:::|
- + # |:::::..... ::' ``::|
- + # |::::::::-'`- 88 `|
- + # |:::::-'. - :: |
- + # |:-~. . . : |
- + # | .. . ..: o:8 88o |
- + # |. . ::: 8:P d888. . . |
- + # |. . :88 88 888' . . |
- + # | o8 d88P . 88 ' d88P .. |
- + # | 88P 888 d8P ' 888 |
- + # | 8 d88P.'d:8 .- dP~ o8 |
- + # | 888 888 d~ o888 |
- + # |_________________________________|
- - where_items = [] unless where_items
- - query_params = {} unless query_params
- -
- - if params[:brand]
- - where_items += ["b.name ilike :brand"]
- - query_params[:brand] = "%#{params[:brand]}%"
- - end
- -
- - if params[:season]
- - where_items += ["po.season ILIKE :season"]
- - query_params[:season] = "%#{params[:season].strip}%"
- - end
- -
- - if params[:invoice_number]
- - where_items += ["rs.number = :invoice_number"]
- - query_params[:invoice_number] = params[:invoice_number]
- - end
- -
- - if params[:product_string]
- - where_items += ["(p.name ILIKE :product OR p.description ILIKE :product OR p.code ILIKE :product)"]
- - query_params[:product] = "%#{params[:product_string].strip}%"
- - end
- -
- - if params[:buying_category_id]
- - where_items += ["p.buying_category_id = :buying_category_id"]
- - query_params[:buying_category_id] = params[:buying_category_id]
- - end
- -
- - if params[:shipment_number]
- - where_items += ["(rs.number ILIKE :shipment_number OR 'S' || rs.id ILIKE :shipment_number)"]
- - query_params[:shipment_number] = "%#{params[:shipment_number].gsub(/\s+/, "").strip}%"
- - end
- -
- - if params[:po_number]
- - where_items += ["CAST(po.id AS VARCHAR) = :po_number"]
- - query_params[:po_number] = "#{params[:po_number].gsub(/^P(\d+)/i, '\1').strip}"
- - end
- -
- - if params[:status_code] && !query_params[:status_code]
- - where_items += ["po.status_code = :status_code"]
- - query_params[:status_code] = params[:status_code]
- - end
- -
- - if params[:order_type]
- - where_items += ["po.order_type = :order_type"]
- - query_params[:order_type] = params[:order_type]
- - end
- -
- - if params[:created_by_id]
- - where_items += ["po.created_by_id = :created_by"]
- - query_params[:created_by] = params[:created_by_id]
- - end
- -
- - if params[:receive_date_from]
- - where_items += ["rs.receive_date >= :receive_from"]
- - query_params[:receive_from] = Date.parse(params[:receive_date_from])
- - end
- - if params[:receive_date_to]
- - where_items += ["rs.receive_date <= :receive_to"]
- - query_params[:receive_to] = Date.parse(params[:receive_date_to])
- - end
- -
- - # If an end date is supplied, use it to limit the start date. Don't
- - # look for items that have a start date in the range, and an end date in the range.
- - if params[:ship_date_from]
- - if params[:ship_date_to]
- - where_items += ["po.receive_start BETWEEN :receive_start AND :receive_end"]
- - query_params[:receive_end] = Date.parse(params[:ship_date_to])
- - else
- - where_items += ["po.receive_start >= :receive_start"]
- - end
- - query_params[:receive_start] = Date.parse(params[:ship_date_from])
- - elsif params[:ship_date_to]
- - query_params[:receive_end] = Date.parse(params[:ship_date_to])
- - where_items += ["po.receive_end <= :receive_end"]
- - end
- -
- - if (where_items.length > 0)
- - query += " WHERE " + where_items.join(' AND ')
- - count_query += " WHERE " + where_items.join(' AND ')
- - end
- -
- - query += " ORDER BY po.id DESC"
- -
- - return {
- - :query => query,
- - :count_query => count_query,
- - :query_params => query_params
- - }
- - end
- -
- +
- def total(olde_buying_category = nil)
- conditions = [ 'purchase_order_id = :id' ]
- args = {:id => self.id}
- @@ -274,6 +185,14 @@
- return [(total_quantity - total_received).to_i,0].max
- end
- + def total_retail
- + total = 0
- + self.items.each do |i|
- + total += i.item.retail_price * i.quantity if i.item.retail_price && i.quantity
- + end
- + return total
- + end
- +
- def total_received_amount
- total = 0
- self.received_shipments.each do |rs|
- @@ -314,41 +233,46 @@
- return years
- end
- - #
- +
- # The total number of product/color combinations for the POs
- - #
- - def self.total_skus(pos)
- - query = <<-EOS
- - SELECT COUNT(DISTINCT(colors.id)) FROM
- - colors INNER JOIN items on items.color_id = colors.id
- - INNER JOIN purchase_order_items on purchase_order_items.item_id = items.id
- - INNER JOIN purchase_orders on purchase_orders.id = purchase_order_items.purchase_order_id
- - EOS
- - query << "WHERE purchase_orders.id IN (#{pos.collect(&:id).join(", ")})"
- - return PurchaseOrder.connection.select_one(query)['count'].to_f
- - end
- - def self.total_quantity(pos)
- - query = "SELECT SUM(quantity) FROM purchase_order_items WHERE purchase_order_id IN (#{pos.collect(&:id).join(", ")})"
- - return PurchaseOrder.connection.select_one(query)['sum'].to_f
- - end
- -
- - def self.total_cost(pos)
- - query = "SELECT SUM(quantity * cost) FROM purchase_order_items WHERE purchase_order_id IN (#{pos.collect(&:id).join(", ")})"
- - return PurchaseOrder.connection.select_one(query)['sum'].to_f
- - end
- -
- - def self.total_received_amount(pos)
- - query = <<-EOS
- - SELECT SUM(received_shipment_items.quantity * cost) FROM
- - purchase_orders INNER JOIN received_shipments ON received_shipments.purchase_order_id = purchase_orders.id
- - INNER JOIN received_shipment_items ON received_shipment_items.received_shipment_id = received_shipments.id
- - INNER JOIN items ON items.id = received_shipment_items.item_id
- - EOS
- - query << "WHERE purchase_orders.id IN (#{pos.collect(&:id).join(", ")})"
- - return PurchaseOrder.connection.select_one(query)['sum'].to_f
- - end
- -
- + def self.total_skus(pos)
- + query = <<-EOS
- + SELECT COUNT(DISTINCT(colors.id)) FROM
- + colors INNER JOIN items on items.color_id = colors.id
- + INNER JOIN purchase_order_items on purchase_order_items.item_id = items.id
- + INNER JOIN purchase_orders on purchase_orders.id = purchase_order_items.purchase_order_id
- + EOS
- + query << "WHERE purchase_orders.id IN (#{pos.collect(&:id).join(", ")})"
- + return PurchaseOrder.connection.select_one(query)['count'].to_f
- + end
- +
- + def self.total_quantity(pos)
- +
- + query = "SELECT SUM(quantity) FROM purchase_order_items WHERE purchase_order_id IN (#{pos.collect(&:id).join(", ")})"
- + return PurchaseOrder.connection.select_one(query)['sum'].to_f
- + end
- +
- + def self.total_cost(pos)
- + query = "SELECT SUM(quantity * cost) FROM purchase_order_items WHERE purchase_order_id IN (#{pos.collect(&:id).join(", ")})"
- + return PurchaseOrder.connection.select_one(query)['sum'].to_f
- + end
- +
- + def self.total_retail(pos)
- + return self.quantity * self.items.retail_price
- + end
- +
- + def self.total_received_amount(pos)
- + query = <<-EOS
- + SELECT SUM(received_shipment_items.quantity * cost) FROM
- + purchase_orders INNER JOIN received_shipments ON received_shipments.purchase_order_id = purchase_orders.id
- + INNER JOIN received_shipment_items ON received_shipment_items.received_shipment_id = received_shipments.id
- + INNER JOIN items ON items.id = received_shipment_items.item_id
- + EOS
- + query << "WHERE purchase_orders.id IN (#{pos.collect(&:id).join(", ")})"
- + return PurchaseOrder.connection.select_one(query)['sum'].to_f
- + end
- +
- def brands_of_items
- brands = []
- 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