module Toolhound # A single charge for a rental item class RentalCharge < Base # self.table_name = :rental # self.primary_key = :int_rental_id def default_selects #{ }"SELECT tblEntity.intEntityID, tblEntity.varEntityID AS job_no, tblLocation.intLocationID, tblLocationText.varLocationName FROM tblEntity INNER JOIN tblLocation ON tblLocation.intEntityID = tblEntity.intEntityID INNER JOIN tblLocationText ON tblLocationText.intLocationID = tblLocation.intLocationID WHERE varEntityID LIKE '%10526.00%'" { rental_charge: [:int_rental_charge_id ] # rental_item_detail: [:var_status, :status_date, ], # rental_detail: [:int_rental_id, :int_inventory_id, :int_quantity, :dec_daily, :dec_weekly, :dec_monthly, :dec_selling_price], } end def default_joins arr = [] # arr << "INNER JOIN tblRentalItemDetail ON (tblRentalItemDetail.intRentalItemID = tblRentalItem.intRentalItemID)" # arr << "INNER JOIN tblRentalDetail ON (tblRentalDetail.intRentalDetailID = tblRentalItem.intRentalDetailID)" # arr << "INNER JOIN tblRental ON (tblRental.intRentalID = tblRentalDetail.intRentalID)" # arr << "INNER JOIN tblLocationText ON (tblLocationText.intLocationID = tblLocation.intLocationID)" arr end # def find_by_entity_id(id) # all(limit: 1, where: [{int_entity_id: id}]).first # end def find_first_and_last_charge_for_entity(entity_id) selects = { rental_charge: [ {dte_start_date: {as: :max_date, agg: :max} }, {dte_start_date: {as: :min_date, agg: :min} } ] } build_and_query(selects: selects, where: [{int_entity_id: entity_id}]).first end def find_by_rental_id(id) all(where: [{"rental_detail.int_rental_id" => id}]) end def for_entity_over_period(options) options = (options || {}).dup entity_id = options.delete :entity_id from = options.delete :from to = options.delete :to joins = [] joins << "LEFT OUTER JOIN tblRentalItem ON tblRentalItem.intRentalItemID = tblRentalCharge.intRentalItemID" joins << "LEFT OUTER JOIN tblRentalDetail ON tblRentalDetail.intRentalDetailID = tblRentalItem.intRentalDetailID" joins << "LEFT OUTER JOIN tblRental ON tblRental.intRentalID = tblRentalDetail.intRentalID" joins << "INNER JOIN tblInventoryID ON tblInventoryID.intInventoryIdID = tblRentalCharge.intInventoryIDID" joins << "INNER JOIN tblInventory ON tblInventoryID.intInventoryID = tblInventory.intInventoryID" joins << "INNER JOIN tblInventoryText ON tblInventory.intInventoryID = tblInventoryText.intInventoryID" joins << "INNER JOIN tblLocation ON tblLocation.intEntityID = tblRentalCharge.intEntityID" joins << "LEFT OUTER JOIN ( SELECT intLocationID, MAX(tblTax.dteCreatedDate) AS max_date FROM tblTax GROUP BY intLocationID ) as taxQuery ON taxQuery.intLocationID = tblLocation.intLocationID" joins << "LEFT OUTER JOIN tblTax ON tblTax.intLocationID = taxQuery.intLocationID AND tblTax.dteCreatedDate = taxQuery.max_date" joins << "LEFT OUTER JOIN tblTaxText ON tblTaxText.intTaxID = tblTax.intTaxID AND tblTaxText.varLocaleID = '#{locale}'" wheres = [ {"inventory.bol_deleted" => 0}, {"inventory.bol_is_active" => 1}, {"inventory_text.var_locale_id" => locale}, ] wheres << {"rental_charge.int_entity_id" => entity_id} if entity_id wheres << {dte_end_date: {value: [parse_time(from), parse_time(to)], op: :between} } if from && to selects = { rental_charge: [ :int_rental_charge_id, :int_entity_id, :int_qty, :dec_total, :var_type, :int_inventory_id_id, :dec_days, :dec_daily, :dec_weeks, :dec_weekly, :dec_months, :dec_monthly, :dte_start_date, :dte_end_date ], inventory_id: [{var_inventory_id: :inventory_id_no}, :int_inventory_id], inventory: [:int_category_id, :int_sub_category_id, :int_inventory_type_id], inventory_text: [ :var_part_no, :var_description, {var_user_field1: :gl_revenue}, {var_user_field2: :gl_cogs_code}, {var_user_field3: :phase_code} ], tax: [{dec_tax1_rate: :tax_rate}], tax_text: [{var_tax1_description: :tax_label}], rental: [:var_rental_number] } build_and_query(selects: selects, where: wheres, joins: joins, order: "tblInventoryText.varPartNo") end # rename_attributes intLocationID: 'location_id' end end