app/models/c80_estate/pstat.rb in c80_estate-0.1.0.24 vs app/models/c80_estate/pstat.rb in c80_estate-0.1.0.25

- old
+ new

@@ -101,12 +101,15 @@ # Rails.logger.debug "<Pstat.busy_coef> all_areas_count = #{ all_areas_count }" # Rails.logger.debug "<Pstat.busy_coef> result[:busy_coef] = #{ result[:busy_coef] }" # Занятость в метрах - free_areas_atnow_sq = pstats.where(:atype_id => nil).last.free_areas_sq - busy_areas_atnow_sq = pstats.where(:atype_id => nil).last.busy_areas_sq + # free_areas_atnow_sq = pstats.where(:atype_id => nil).last.free_areas_sq + # busy_areas_atnow_sq = pstats.where(:atype_id => nil).last.busy_areas_sq + tt = _calc_free_busy_areas_sq(pstats) + free_areas_atnow_sq = tt[:free_areas_atnow_sq] + busy_areas_atnow_sq = tt[:busy_areas_atnow_sq] # защищаемся от деления на ноль if busy_areas_atnow_sq + free_areas_atnow_sq == 0 bcoef_sq = 0.0 else @@ -765,12 +768,12 @@ } end def self._calc_free_busy_areas(pstats) - sum_free_areas = 0 - sum_busy_areas = 0 + sum_free_areas = 0.0 + sum_busy_areas = 0.0 all_props = Property.all all_props.each do |prop| ppstats = pstats.where(:property_id => prop.id).ordered_by_created_at.last if ppstats.present? sum_free_areas += ppstats.free_areas @@ -778,9 +781,33 @@ end end { sum_free_areas: sum_free_areas, sum_busy_areas: sum_busy_areas + } + end + + def self._calc_free_busy_areas_sq(pstats) + + sum_free_areas_sq = 0.0 + sum_busy_areas_sq = 0.0 + all_props = Property.all + + all_props.each do |prop| + ppstats = pstats.where(:property_id => prop.id).ordered_by_created_at.last + if ppstats.present? + Rails.logger.debug "\t\t ppstats.free_areas_sq = #{ppstats.free_areas_sq}" + Rails.logger.debug "\t\t ppstats.busy_areas_sq = #{ppstats.busy_areas_sq}" + sum_free_areas_sq += ppstats.free_areas_sq + sum_busy_areas_sq += ppstats.busy_areas_sq + end + end + + Rails.logger.debug "<_calc_free_busy_areas_sq> sum_free_areas_sq = #{sum_free_areas_sq}, sum_busy_areas_sq = #{sum_busy_areas_sq}" + + { + free_areas_atnow_sq: sum_free_areas_sq, + busy_areas_atnow_sq: sum_busy_areas_sq } end end end \ No newline at end of file