Class: Toolhound::RentalCharge

Inherits:
Base
  • Object
show all
Defined in:
lib/toolhound-ruby/rental_charge.rb

Overview

A single charge for a rental item

Constant Summary

Constants inherited from Base

Base::DATE_TIME_FORMAT, Base::DB_TYPE_REGEX

Instance Attribute Summary

Attributes inherited from Base

#client, #connection

Instance Method Summary collapse

Methods inherited from Base

#_build_joins, #_build_selects, #_build_where, #all, #build_and_query, #build_group, #build_joins, #build_selects, #build_sql, #build_update_attributes, #build_update_sql, #build_where, #default_wheres, #find, #formatted_table_and_column, #formatted_table_name, #formmatted_column_name, #get_operator, #initialize, #insert, #locale, #merge_options, #parse_time, primary_key, #primary_key, primary_key=, #procedure, #query, rename_attributes, renamed_attributes, #table_name, table_name, table_name=, #transform_attribute_key, #transform_attributes, #transform_procedure_key, #transform_procedure_value, #transform_procedure_variables, #update, #update_query

Methods included from Util

#acronym_regex, #acronyms, #camelize, #demodulize, #underscore

Constructor Details

This class inherits a constructor from Toolhound::Base

Instance Method Details

#default_joinsObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/toolhound-ruby/rental_charge.rb', line 19

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

#default_selectsObject

self.table_name = :rental self.primary_key = :int_rental_id



10
11
12
13
14
15
16
17
# File 'lib/toolhound-ruby/rental_charge.rb', line 10

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

#find_by_rental_id(id) ⇒ Object



45
46
47
# File 'lib/toolhound-ruby/rental_charge.rb', line 45

def find_by_rental_id(id)
  all(where: [{"rental_detail.int_rental_id" => id}])
end

#find_first_and_last_charge_for_entity(entity_id) ⇒ Object

def find_by_entity_id(id)

all(limit: 1, where: [{int_entity_id: id}]).first

end



34
35
36
37
38
39
40
41
42
43
# File 'lib/toolhound-ruby/rental_charge.rb', line 34

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

#for_entity_over_period(options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/toolhound-ruby/rental_charge.rb', line 49

def for_entity_over_period(options)
  options       = (options || {}).dup

  entity_id     = options.delete :entity_id
  from          = options.delete :from
  to            = options.delete :to
  job_id        = options.delete :job_id

  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 << "LEFT OUTER JOIN tblTransaction ON tblTransaction.intRentalID = tblRental.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}'"
  joins << "LEFT OUTER JOIN tblJobText ON tblJobText.intJobID = tblRentalCharge.intJobID AND tblJobText.varLocaleID = '#{locale}'"
  wheres = [
    {"inventory.bol_deleted"        => 0},
    {"inventory.bol_is_active"      => 1},
    {"inventory_text.var_locale_id" => locale},
  ]

  wheres << {"rental_charge.int_job_id" => job_id} if job_id
  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,
      # {var_work_order: :var_work_order_no},
      :dte_start_date, :dte_end_date, :int_job_id
    ],
    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}
    ],
    job_text:       [:var_job, :var_job_number],
    transaction:    [{var_work_order: :var_work_order_no}, :var_transaction_no],
    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