1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class MonitorController < ApplicationController

  class DataPoint
    attr_accessor :obj, :count, :quantity, :discount_total, :total
    def initialize(obj)
      self.obj = obj
      self.count = 0
      self.quantity = 0
      self.discount_total = 0
      self.total = 0
    end
    def append(invoice_item)
      self.count += 1
      self.quantity += invoice_item.quantity
      self.discount_total += invoice_item.discount_total
      self.total += invoice_item.total
    end
  end

  def zoo_activity_report
    @page_title = "Zoo Activity report"
    #TODO 
  end
  
  def drawer_report
    @page_title = "Drawer report"
    #TODO 
  end
  
  def farm_activity_report
    
    @page_title = "Farm activities report"

    @activitydatum = {}
    @productdatum = {}
    @miscdatum = DataPoint.new(nil)

    @timestring = "today"
    @start_date = Time.now.change({:hour=>0,:min=>0})
    @end_date = Time.now.change({:hour=>23,:min=>59})

    if params[:period]
      case 
        when params[:period] == "yesterday"
          @timestring = "yesterday"
          @start_date = @start_date.advance(:days => -1)
          @end_date = @end_date.advance(:days => -1)
        when params[:period] == "this_week"
          @timestring = "the current week"
          @start_date = DateTime.now.beginning_of_week.change({:hour=>0,:min=>0})
        when params[:period] == "this_month"
          @timestring = "the current month"
          @start_date = DateTime.now.beginning_of_month.change({:hour=>0,:min=>0})
          @end_date = DateTime.now.end_of_month.change({:hour=>23,:minute=>59})
        when params[:period] == "last_month"
          @timestring = "last month"
          @start_date = Time.now.change({:day => 1, :hour=>0,:minute=>0}).advance(:months=>-1)
          @end_date = @start_date.end_of_month.change({:hour=>23,:min=>59})
      end
    else    
      if params[:date_start] and params[:date_end]
        @timestring = "Today"
        @start_date = DateTime.strptime(params[:date_start], "%m/%d/%Y").to_time
        @end_date = DateTime.strptime(params[:date_end], "%m/%d/%Y").to_time
      end
    end

    if @start_date.year.to_i < 100
      @start_date = @start_date.change(:year=>@start_date.year+2000)
    end
    
    if @end_date.year.to_i < 100
      @end_date = @end_date.change(:year=>@end_date.year+2000) 
    end
    
    @status = 0
    @invoices = Invoice.where(["created_date >= ? AND created_date <= ? AND status = ?",@start_date,@end_date,@status]).includes(:invoice_item_groups, {:invoice_item_groups=>:invoice_items},{:invoice_item_groups=>{:invoice_items=>:activity}})

    @invoices.each do |i|
      i.invoice_item_groups.each do |ig|
        ig.invoice_items.each do |ii|
          if ii.activity
            logger.info "ACTIVITY: #{ii.id}"
            @activitydatum[ii.activity.id] = DataPoint.new(ii.activity) unless @activitydatum[ii.activity.id]
            @activitydatum[ii.activity.id].append(ii)
          elsif ii.product
            logger.info "PRODUCT: #{ii.id}"
            @productdatum[ii.product.id] = DataPoint.new(ii.product) unless @productdatum[ii.product.id]
            @productdatum[ii.product.id].append(ii)
          else
            @miscdatum.append(ii)
          end
        end
      end
    end

    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def        drawer_report
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def        list_messages
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def        list_my_messages
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def        list_orders
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def        list_payments
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def        recon_card_trans
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def        zoo_activity_report
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
  def        zoo_activity_report_2
    respond_to do |format|
      format.html # index.html.erb
    end
  end
  
end