vyapari 0.1.5dev9 → 0.1.5dev10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/vyapari/store_manager/base_controller.rb +3 -0
  3. data/app/controllers/vyapari/store_manager/dashboard_controller.rb +1 -0
  4. data/app/controllers/vyapari/store_manager/reports/invoices_controller.rb +206 -101
  5. data/app/controllers/vyapari/store_manager/reports/sales_controller.rb +299 -0
  6. data/app/controllers/vyapari/store_manager/reports/stock_controller.rb +1 -1
  7. data/app/controllers/vyapari/terminal_staff/dashboard_controller.rb +2 -1
  8. data/app/models/bank_account.rb +44 -0
  9. data/app/models/brand.rb +8 -13
  10. data/app/models/category.rb +3 -3
  11. data/app/models/contact.rb +72 -0
  12. data/app/models/country.rb +6 -4
  13. data/app/models/exchange_rate.rb +2 -2
  14. data/app/models/invoice.rb +12 -8
  15. data/app/models/line_item.rb +3 -3
  16. data/app/models/product.rb +3 -3
  17. data/app/models/region.rb +11 -17
  18. data/app/models/stock_bundle.rb +4 -4
  19. data/app/models/stock_entry.rb +8 -7
  20. data/app/models/store.rb +52 -21
  21. data/app/models/supplier.rb +19 -25
  22. data/app/models/terminal.rb +3 -3
  23. data/app/models/vyapari/application_record.rb +1 -2
  24. data/app/views/layouts/vyapari/_store_manager_menu.html.erb +24 -5
  25. data/app/views/layouts/vyapari/_terminal_staff_menu.html.erb +6 -3
  26. data/app/views/layouts/vyapari/store_manager.html.erb +4 -4
  27. data/app/views/vyapari/store_manager/dashboard/_sales_counts.html.erb +90 -0
  28. data/app/views/vyapari/store_manager/dashboard/_stock_counts.html.erb +63 -0
  29. data/app/views/vyapari/store_manager/dashboard/_terminals.html.erb +23 -0
  30. data/app/views/vyapari/store_manager/dashboard/index.html.erb +51 -79
  31. data/app/views/vyapari/store_manager/reports/invoices/_index.html.erb +65 -0
  32. data/app/views/vyapari/store_manager/reports/invoices/index.html.erb +105 -0
  33. data/app/views/vyapari/store_manager/reports/sales/_index.html.erb +64 -0
  34. data/app/views/vyapari/store_manager/reports/sales/index.html.erb +105 -0
  35. data/app/views/vyapari/terminal_staff/dashboard/_counts.html.erb +61 -18
  36. data/config/routes.rb +2 -0
  37. data/db/migrate/20170000000203_create_contacts.rb +1 -1
  38. data/db/migrate/20170000000206_create_stores.rb +1 -1
  39. data/lib/vyapari/version.rb +1 -1
  40. metadata +16 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 28ffe643842fcbd0f79cd728537aba00773433f3
4
- data.tar.gz: 638c38653a4c40346741351bf787ff4716b0d5d5
3
+ metadata.gz: 78c3cde2ff155082088ef1e7606db93fca5223b0
4
+ data.tar.gz: 73bca11944d2b9f8996af22561030bd94094f0c1
5
5
  SHA512:
6
- metadata.gz: 658da9c47f160b5f409388851aa626144f485f17f168f337f0b519d0e2be0d5f0c920f82d1305376f4880c4d36e52a7e7d8c1120811063365d2dfdd05bb799a0
7
- data.tar.gz: fc7d1306d50295df05cac0a76b429c55833408cd7bf42be385af5eb215aff2438ca2b840e29b9792cef7a32cf929f57813e46a6aae33b183f357491f030d6965
6
+ metadata.gz: 5e5af5f8a109d10ea7161c5a34611ccf8d999b8e2b1543772bcb135cb630bb12ceb4aad6bff8a2729b6d79007087ea2aac79663bd0c15bd2f01b59951dabdfb3
7
+ data.tar.gz: 1e07162b8f01363c58c93567608d67a93dae49ebeb595dec43347a7c170f3145163f05c2882b1610ea2abdd53f175061e84b9ca684e8a9a5b7fd211da9ea272d
@@ -23,6 +23,9 @@ module Vyapari
23
23
  @filter_param_mapping[:supplier] = :sp
24
24
  @filter_param_mapping[:brand] = :br
25
25
  @filter_param_mapping[:category] = :ct
26
+ @filter_param_mapping[:payment_method] = :pym
27
+ @filter_param_mapping[:terminal] = :trm
28
+ @filter_param_mapping[:user] = :usr
26
29
  end
27
30
 
28
31
  end
@@ -4,6 +4,7 @@ module Vyapari
4
4
 
5
5
  # GET /dashboard
6
6
  def index
7
+ @date = params[:date] ? Date.parse(params[:date]) : Date.today
7
8
  @terminals = @store.terminals
8
9
  end
9
10
 
@@ -1,144 +1,249 @@
1
1
  module Vyapari
2
2
  module StoreManager
3
- module Reports
4
- class InvoicesController < Vyapari::StoreManager::BaseController
3
+ module Reports
4
+ class InvoicesController < Vyapari::StoreManager::BaseController
5
5
 
6
6
  def index
7
- @relation = Product.select("MAX(products.id) as id,
8
- MAX(products.name) as name,
9
- MAX(products.ean_sku) as ean_sku,
10
- MAX(products.purchased_price) as purchased_price,
11
- MAX(products.landed_price) as landed_price,
12
- MAX(products.selling_price) as selling_price,
13
- MAX(products.retail_price) as retail_price,
14
- MAX(se.status) as se_status,
15
- SUM(se.quantity) as se_quantity").
16
- joins("LEFT JOIN stock_entries se ON se.product_id = products.id")
7
+ @relation = Invoice.select("
8
+ invoices.customer_name as customer_name,
9
+ invoices.customer_address as customer_address,
10
+ invoices.customer_phone as customer_phone,
11
+ invoices.customer_email as customer_email,
12
+
13
+ invoices.invoice_number as invoice_number,
14
+ invoices.invoice_date as invoice_date,
15
+
16
+ invoices.discount as discount,
17
+ TRUNCATE(invoices.net_total_amount, 2) as net_total_amount,
18
+
19
+ invoices.payment_method as payment_method,
20
+ invoices.credit_card_number as credit_card_number,
21
+ invoices.cheque_number as cheque_number,
22
+
23
+ u.name as user_name
24
+ ").
25
+ joins("LEFT JOIN users u ON invoices.user_id = u.id").
26
+ where("invoices.status = 'active'").
27
+ where("invoices.store_id = ?", @store.id)
17
28
 
18
29
  parse_filters
19
30
  apply_filters
31
+ set_columns
32
+
33
+ @results = @relation.all
20
34
 
21
- @results = @relation.group("products.id")
22
35
  @total = @relation.except(:select).
23
- select("SUM(products.id) as id,
24
- SUM(products.purchased_price) as purchased_price,
25
- SUM(products.landed_price) as landed_price,
26
- SUM(products.selling_price) as selling_price,
27
- SUM(products.retail_price) as retail_price")
36
+ select("SUM(invoices.id) as id,
37
+ SUM(invoices.net_total_amount) as net_total_amount")
28
38
 
29
39
  render_report
30
40
  end
31
41
 
32
42
  private
33
43
 
34
- def permitted_params
35
- params.require(:invoice).permit(:discount, :adjustment, :money_taken, :notes, :payment_method, :customer_name, :customer_address, :customer_phone, :customer_email, :credit_card_number)
36
- end
37
-
38
- def resolve_layout
39
- case action_name
40
- when "show"
41
- 'kuppayam/print_a4'
42
- else
43
- 'vyapari/terminal_staff'
44
+ def apply_filters
45
+ @relation = @relation.search(@query) if @query
46
+
47
+ if @terminal == "null"
48
+ @relation = @relation.where("invoices.terminal_id IS NULL")
49
+ elsif @terminal
50
+ @relation = @relation.where("invoices.terminal_id = ?",@terminal.id)
44
51
  end
45
- end
46
-
47
- def resource_controller_configuration
48
- {
49
- collection_name: :invoices,
50
- item_name: :invoice,
51
- class: Invoice,
52
- page_title: "#{@terminal.name} - Invoices",
53
- js_view_path: "/kuppayam/workflows/peacock",
54
- view_path: "/vyapari/terminal_staff/invoices"
55
- }
56
- end
57
52
 
58
- def breadcrumbs_configuration
59
- {
60
- heading: "Invoices - #{@terminal.name}",
61
- description: "Listing the invoices - #{@terminal.name}",
62
- links: [
63
- {name: "Change Terminal", link: store_manager_dashboard_path(@store), icon: 'fa-cog'},
64
- {name: @terminal.name, link: terminal_staff_dashboard_path(@terminal), icon: 'fa-desktop'},
65
- {name: "Invoices", link: nil, icon: 'fa-file', active: true}
66
- ]
67
- }
68
- end
69
-
70
- def get_collections
71
- @relation = @terminal.invoices.includes(:user).where("")
72
-
73
- parse_filters
74
- apply_filters
75
-
76
- @invoices = @r_objects = @relation.page(@current_page).per(@per_page)
77
-
78
- return true
79
- end
53
+ if @user == "null"
54
+ @relation = @relation.where("invoices.user_id IS NULL")
55
+ elsif @user
56
+ @relation = @relation.where("invoices.user_id = ?",@user.id)
57
+ end
80
58
 
81
- def apply_filters
82
- @relation = @relation.search(@query) if @query
83
- @relation = @relation.status(@status) if @status
84
- @relation = @relation.payment_method(@payment_method) if @payment_method
59
+ if @payment_method == "null"
60
+ @relation = @relation.where("invoices.payment_method IS NULL")
61
+ elsif @payment_method
62
+ @relation = @relation.where("invoices.payment_method = ?",@payment_method)
63
+ end
85
64
 
86
- # if @fterminal == "null"
87
- # @relation = @relation.where("invoices.terminal_id IS NULL")
88
- # elsif @fterminal
89
- # @relation = @relation.where("invoices.terminal_id = ?", @fterminal.id)
90
- # end
91
-
92
- # if @fstore == "null"
93
- # @relation = @relation.where("invoices.store_id IS NULL")
94
- # elsif @fstore
95
- # @relation = @relation.where("invoices.store_id = ?", @fstore.id)
96
- # end
97
-
98
- # if @user == "null"
99
- # @relation = @relation.where("invoices.user_id IS NULL")
100
- # elsif @user
101
- # @relation = @relation.where("invoices.user_id = ?", @terminal.id)
102
- # end
103
-
104
- @order_by = "created_at desc" unless @order_by
105
- @relation = @relation.order(@order_by)
65
+ @relation = @relation.order(@order_by_options[@order_by.to_sym])
106
66
  end
107
67
 
108
68
  def configure_filter_settings
69
+
70
+ @order_by_options = {
71
+ invoice_date_desc: "invoices.invoice_date DESC",
72
+ invoice_date_asc: "invoices.invoice_date ASC",
73
+ customer_name_desc: "invoices.customer_name DESC",
74
+ customer_name_asc: "invoices.customer_name ASC",
75
+ total_amount_asc: "invoices.net_total_amount DeSC",
76
+ total_amount_asc: "invoices.net_total_amount ASC"
77
+ }
78
+
109
79
  @filter_settings = {
110
80
  string_filters: [
111
81
  { filter_name: :query },
112
- { filter_name: :status },
82
+ { filter_name: :order_by, options: {default: :invoice_date_desc} },
113
83
  { filter_name: :payment_method }
114
84
  ],
115
- boolean_filters: [],
85
+
116
86
  reference_filters: [
117
87
  { filter_name: :user, filter_class: User },
118
- { filter_name: :fstore, filter_class: Store },
119
- { filter_name: :fterminal, filter_class: Terminal }
88
+ { filter_name: :terminal, filter_class: Terminal },
120
89
  ],
90
+
121
91
  variable_filters: [
122
- #{ variable_name: :store, filter_name: :store },
123
- #{ variable_name: :terminal, filter_name: :terminal }
92
+ { variable_name: :store, filter_name: :store }
124
93
  ]
125
94
  }
126
95
  end
127
96
 
128
97
  def configure_filter_ui_settings
129
- @filter_ui_settings = {}
98
+ @filter_ui_settings = {
99
+ terminal: {
100
+ object_filter: true,
101
+ select_label: 'Filter by Terminal',
102
+ current_value: @terminal,
103
+ values: @store.terminals.order(:name).all,
104
+ current_filters: @filters,
105
+ url_method_name: 'store_manager_invoices_report_url',
106
+ filters_to_remove: [:terminal, :store],
107
+ filters_to_add: {},
108
+ show_null_filter_on_top: false
109
+ },
110
+ user: {
111
+ object_filter: true,
112
+ select_label: 'Filter by User',
113
+ current_value: @user,
114
+ values: User.order(:name).all,
115
+ current_filters: @filters,
116
+ url_method_name: 'store_manager_invoices_report_url',
117
+ filters_to_remove: [:terminal, :store],
118
+ filters_to_add: {},
119
+ show_null_filter_on_top: true
120
+ },
121
+ payment_method: {
122
+ object_filter: false,
123
+ select_label: "Filter by Payment Method",
124
+ display_hash: Invoice::PAYMENT_METHOD_REVERSE,
125
+ current_value: @payment_method,
126
+ values: Invoice::PAYMENT_METHOD,
127
+ current_filters: @filters,
128
+ filters_to_remove: [],
129
+ filters_to_add: {},
130
+ url_method_name: 'store_manager_invoices_report_url',
131
+ show_all_filter_on_top: true
132
+ },
133
+ order_by: {
134
+ object_filter: false,
135
+ select_label: "Order by",
136
+ display_hash: Hash[@order_by_options.keys.map {|i| [i, i.to_s.titleize]}],
137
+ current_value: @order_by,
138
+ values: Hash[@order_by_options.keys.map {|i| [i.to_s.titleize, i]}],
139
+ current_filters: @filters,
140
+ filters_to_remove: [],
141
+ filters_to_add: {},
142
+ url_method_name: 'store_manager_invoices_report_url',
143
+ show_all_filter_on_top: true
144
+ },
145
+ }
146
+ end
147
+
148
+ def set_columns
149
+ @columns = [
150
+ { column_name: :id, display_name: "Sl.", align: "center", column_type: :integer },
151
+ { column_name: :customer_name, display_name: "Customer Name", align: "center", column_type: :string },
152
+ { column_name: :customer_address, display_name: "Customer Address", align: "center", column_type: :string },
153
+ { column_name: :customer_phone, display_name: "Customer Phone", align: "center", column_type: :string },
154
+ { column_name: :customer_email, display_name: "Customer Email", align: "center", column_type: :string },
155
+ { column_name: :invoice_number, display_name: "Invoice Number", align: "center", column_type: :string },
156
+ { column_name: :invoice_date, display_name: "Invoice Date", align: "center", column_type: :date },
157
+ { column_name: :discount, display_name: "Discount %", align: "center", column_type: :integer },
158
+ { column_name: :net_total_amount, display_name: "Total Amount", align: "right", column_type: :currency },
159
+ { column_name: :payment_method, display_name: "Payment Method", align: "center", column_type: :status },
160
+ { column_name: :credit_card_number, display_name: "Credit Card Number", align: "center", column_type: :string },
161
+ { column_name: :cheque_number, display_name: "Cheque Number", align: "center", column_type: :string },
162
+ { column_name: :user_name, display_name: "User", align: "center", column_type: :string }
163
+ ]
164
+
165
+ if params[:cols]
166
+ @selected_columns = params[:cols].map{|x| x.to_sym}
167
+ else
168
+ @selected_columns = [:id, :invoice_number, :invoice_date, :net_total_amount]
169
+ end
170
+ end
171
+
172
+ def breadcrumbs_configuration
173
+ {
174
+ heading: "Invoices Report",
175
+ description: "Invoices Report",
176
+ links: [{name: "Change Store", link: user_dashboard_path, icon: 'fa-desktop'},
177
+ {name: @store.name, link: store_manager_dashboard_path(@store), icon: 'fa-dashboard'},
178
+ {name: "Invoices Reports", link: nil, icon: 'fa-list', active: true}]
179
+ }
180
+ end
181
+
182
+ def set_navs
183
+ set_nav("store_manager/reports")
130
184
  end
131
185
 
132
- def resource_url(obj)
133
- url_for([:terminal_staff, @terminal, obj])
186
+ def render_report
187
+ respond_to do |format|
188
+ format.html {}
189
+ format.csv do
190
+ send_data generate_csv(@results),
191
+ :type => 'text/csv; charset=utf-8; header=present',
192
+ :disposition => "attachment; filename=invoice-report.csv",
193
+ :filename => "invoice-report.csv"
194
+ end
195
+ format.pdf do
196
+ render :pdf => "#{@heading} (As on #{Date.today})"
197
+ #:disposition => 'attachment'
198
+ #:show_as_html => true
199
+ end
200
+ end
134
201
  end
135
202
 
136
- def set_navs
137
- set_nav("terminal_staff/invoices")
138
- end
203
+ def generate_csv(results)
204
+
205
+ CSV.generate(headers: true) do |csv|
206
+ # Adding the headings
207
+ csv << @columns.select{|col_item| @selected_columns.include?(col_item[:column_name])}.collect{|x| x[:display_name]}
208
+
209
+ # Adding the results
210
+ @results.each_with_index do |result, i|
211
+
212
+ # Remove Id as we get it with i in each_with_index
213
+ cols_without_id = @columns.reject{|col_item| col_item[:column_name] == :id}
214
+
215
+ # Select columns which are selected by the user / default
216
+ selected_cols = cols_without_id.select{|col_item| @selected_columns.include?(col_item[:column_name]) }
217
+
218
+ # Initialize result row with just serial number
219
+ result_data = [i+1]
220
+
221
+ # Iterate selected columns and populate the result data
222
+ selected_cols.each do |col_item|
223
+
224
+ case col_item[:column_type]
225
+ when :string, :integer
226
+ result_data << result.send(col_item[:column_name])
227
+ when :status
228
+ result_data << result.send(col_item[:column_name]).titleize
229
+ when :currency
230
+ result_data << sprintf("%.2f", result.send(col_item[:column_name]))
231
+ when :date
232
+ result_data << result.send(col_item[:column_name]).strftime("%d-%m-%Y")
233
+ else
234
+ result_data << result.send(col_item[:column_name])
235
+ end
236
+
237
+ end
238
+
239
+ # Append to CSV
240
+ csv << result_data
241
+ end
242
+ end
243
+
244
+ end
139
245
 
140
- end
246
+ end
141
247
  end
142
- end
248
+ end
143
249
  end
144
-
@@ -0,0 +1,299 @@
1
+ module Vyapari
2
+ module StoreManager
3
+ module Reports
4
+ class SalesController < Vyapari::StoreManager::BaseController
5
+
6
+
7
+ # select p.name, p.ean_sku, p.retail_price,
8
+ # se.quantity, i.invoice_number, i.invoice_date,
9
+ # i.payment_method from stock_entries se
10
+ # left join products p on p.id = se.product_id
11
+ # left join invoices i on i.id = se.invoice_id
12
+ # where se.status = 'sold'
13
+ def index
14
+ @relation = StockEntry.select("p.name as product_name,
15
+ p.ean_sku as ean_sku,
16
+ p.retail_price as retail_price,
17
+ stock_entries.quantity as quantity,
18
+ u.name as user_name,
19
+ i.invoice_number as invoice_number,
20
+ i.invoice_date as invoice_date,
21
+ i.payment_method as payment_method").
22
+ joins("LEFT JOIN products p ON stock_entries.product_id = p.id").
23
+ joins("LEFT JOIN invoices i ON stock_entries.invoice_id = i.id").
24
+ joins("LEFT JOIN users u ON i.user_id = u.id").
25
+ where("stock_entries.status = 'sold'").
26
+ where("stock_entries.store_id = ?", @store.id)
27
+
28
+ parse_filters
29
+ apply_filters
30
+ set_columns
31
+
32
+ #.page(@current_page).per(@per_page)
33
+ @results = @relation.all
34
+
35
+ @total = @relation.except(:select).
36
+ select("SUM(stock_entries.id) as id,
37
+ SUM(p.retail_price) as retail_price")
38
+
39
+ render_report
40
+ end
41
+
42
+ # select MAX(p.id) as id, MAX(p.name) as name,
43
+ # MAX(p.ean_sku) as ean_sku,
44
+ # MAX(p.purchased_price) as purchased_price,
45
+ # MAX(p.landed_price) as landed_price,
46
+ # MAX(p.selling_price) as selling_price,
47
+ # MAX(p.retail_price) as retail_price,
48
+ # SUM(se.quantity) as se_quantity,
49
+ # MAX(se.status) as se_status
50
+ # from products p left join stock_entries se
51
+ # on se.product_id = p.id group by p.id;
52
+ def index_old
53
+ @relation = Product.select("MAX(products.id) as id,
54
+ MAX(products.name) as name,
55
+ MAX(products.ean_sku) as ean_sku,
56
+ MAX(products.purchased_price) as purchased_price,
57
+ MAX(products.landed_price) as landed_price,
58
+ MAX(products.selling_price) as selling_price,
59
+ MAX(products.retail_price) as retail_price,
60
+ MAX(se.status) as se_status,
61
+ SUM(se.quantity) as se_quantity").
62
+ joins("LEFT JOIN stock_entries se ON se.product_id = products.id")
63
+
64
+ parse_filters
65
+ apply_filters
66
+
67
+ @results = @relation.group("products.id").page(@current_page).per(@per_page)
68
+ @total = @relation.except(:select).
69
+ select("SUM(products.id) as id,
70
+ SUM(products.purchased_price) as purchased_price,
71
+ SUM(products.landed_price) as landed_price,
72
+ SUM(products.selling_price) as selling_price,
73
+ SUM(products.retail_price) as retail_price")
74
+
75
+ render_report
76
+ end
77
+
78
+ private
79
+
80
+ def apply_filters
81
+ @relation = @relation.search(@query) if @query
82
+ # @relation = @relation.where("se.se_status = ?", @status) if @status
83
+
84
+ if @terminal == "null"
85
+ @relation = @relation.where("i.terminal_id IS NULL")
86
+ elsif @terminal
87
+ @relation = @relation.where("i.terminal_id = ?",@terminal.id)
88
+ end
89
+
90
+ if @brand == "null"
91
+ @relation = @relation.where("p.brand_id IS NULL")
92
+ elsif @brand
93
+ @relation = @relation.where("p.brand_id = ?",@brand.id)
94
+ end
95
+
96
+ if @category == "null"
97
+ @relation = @relation.where("p.category_id IS NULL")
98
+ elsif @category
99
+ @relation = @relation.where("p.category_id = ?",@category.id)
100
+ end
101
+
102
+ if @payment_method == "null"
103
+ @relation = @relation.where("i.payment_method IS NULL")
104
+ elsif @payment_method
105
+ @relation = @relation.where("i.payment_method = ?",@payment_method)
106
+ end
107
+
108
+ @relation = @relation.order(@order_by_options[@order_by.to_sym])
109
+ end
110
+
111
+ def set_columns
112
+ @columns = [
113
+ { column_name: :id, display_name: "Sl.", align: "center", column_type: :integer },
114
+ { column_name: :product_name, display_name: "Product Name", align: "left", column_type: :string },
115
+ { column_name: :ean_sku, display_name: "EAN/SKU", align: "left", column_type: :string },
116
+ { column_name: :retail_price, display_name: "Retail Price", align: "right", column_type: :currency },
117
+ { column_name: :quantity, display_name: "Quantity", align: "center", column_type: :integer },
118
+ { column_name: :invoice_number, display_name: "Invoice Number", align: "center", column_type: :string },
119
+ { column_name: :invoice_date, display_name: "Invoice Date", align: "center", column_type: :date },
120
+ { column_name: :payment_method, display_name: "Payment Method", align: "center", column_type: :status },
121
+ { column_name: :user_name, display_name: "User", align: "center", column_type: :string }
122
+ ]
123
+
124
+ if params[:cols]
125
+ @selected_columns = params[:cols].map{|x| x.to_sym}
126
+ else
127
+ @selected_columns = [:id, :product_name, :ean_sku, :retail_price, :quantity, :invoice_number]
128
+ end
129
+ end
130
+
131
+ def configure_filter_settings
132
+
133
+ @order_by_options = {
134
+ invoice_date_desc: "i.invoice_date DESC",
135
+ invoice_date_asc: "i.invoice_date ASC",
136
+ quantity_desc: "stock_entries.quantity DESC",
137
+ quantity_asc: "stock_entries.quantity ASC",
138
+ product_name_asc: "p.name ASC, p.ean_sku ASC",
139
+ product_name_desc: "p.name DESC, p.ean_sku DESc"}
140
+
141
+ @filter_settings = {
142
+ string_filters: [
143
+ { filter_name: :query },
144
+ { filter_name: :order_by, options: {default: :invoice_date_desc} },
145
+ { filter_name: :payment_method }
146
+ ],
147
+
148
+ reference_filters: [
149
+ { filter_name: :brand, filter_class: Brand },
150
+ { filter_name: :category, filter_class: Category },
151
+ { filter_name: :terminal, filter_class: Terminal },
152
+ ],
153
+
154
+ variable_filters: [
155
+ { variable_name: :store, filter_name: :store }
156
+ ]
157
+ }
158
+ end
159
+
160
+ def configure_filter_ui_settings
161
+ @filter_ui_settings = {
162
+ terminal: {
163
+ object_filter: true,
164
+ select_label: 'Filter by Terminal',
165
+ current_value: @terminal,
166
+ values: @store.terminals.order(:name).all,
167
+ current_filters: @filters,
168
+ url_method_name: 'store_manager_sales_report_url',
169
+ filters_to_remove: [:terminal, :store],
170
+ filters_to_add: {},
171
+ show_null_filter_on_top: false
172
+ },
173
+ brand: {
174
+ object_filter: true,
175
+ select_label: 'Filter by Brand',
176
+ current_value: @brand,
177
+ values: Brand.order(:name).all,
178
+ current_filters: @filters,
179
+ url_method_name: 'store_manager_sales_report_url',
180
+ filters_to_remove: [:terminal, :store],
181
+ filters_to_add: {},
182
+ show_null_filter_on_top: true
183
+ },
184
+ category: {
185
+ object_filter: true,
186
+ select_label: 'Filter by Category',
187
+ current_value: @category,
188
+ values: Category.order(:name).all,
189
+ current_filters: @filters,
190
+ url_method_name: 'store_manager_sales_report_url',
191
+ filters_to_remove: [:terminal, :store],
192
+ filters_to_add: {},
193
+ show_null_filter_on_top: true
194
+ },
195
+ payment_method: {
196
+ object_filter: false,
197
+ select_label: "Filter by Payment Method",
198
+ display_hash: Invoice::PAYMENT_METHOD_REVERSE,
199
+ current_value: @payment_method,
200
+ values: Invoice::PAYMENT_METHOD,
201
+ current_filters: @filters,
202
+ filters_to_remove: [],
203
+ filters_to_add: {},
204
+ url_method_name: 'store_manager_sales_report_url',
205
+ show_all_filter_on_top: true
206
+ },
207
+ order_by: {
208
+ object_filter: false,
209
+ select_label: "Order by",
210
+ display_hash: Hash[@order_by_options.keys.map {|i| [i, i.to_s.titleize]}],
211
+ current_value: @order_by,
212
+ values: Hash[@order_by_options.keys.map {|i| [i.to_s.titleize, i]}],
213
+ current_filters: @filters,
214
+ filters_to_remove: [],
215
+ filters_to_add: {},
216
+ url_method_name: 'store_manager_sales_report_url',
217
+ show_all_filter_on_top: true
218
+ },
219
+ }
220
+ end
221
+
222
+ def breadcrumbs_configuration
223
+ {
224
+ heading: "Sales Report",
225
+ description: "Sales Report",
226
+ links: [{name: "Change Store", link: user_dashboard_path, icon: 'fa-desktop'},
227
+ {name: @store.name, link: store_manager_dashboard_path(@store), icon: 'fa-dashboard'},
228
+ {name: "Sales Reports", link: nil, icon: 'fa-list', active: true}]
229
+ }
230
+ end
231
+
232
+ def set_navs
233
+ set_nav("store_manager/reports")
234
+ end
235
+
236
+ def render_report
237
+ respond_to do |format|
238
+ format.html {}
239
+ format.csv do
240
+ send_data generate_csv(@results),
241
+ :type => 'text/csv; charset=utf-8; header=present',
242
+ :disposition => "attachment; filename=sales-report.csv",
243
+ :filename => "sales-report.csv"
244
+ end
245
+ format.pdf do
246
+ render :pdf => "#{@heading} (As on #{Date.today})"
247
+ #:disposition => 'attachment'
248
+ #:show_as_html => true
249
+ end
250
+ end
251
+ end
252
+
253
+ def generate_csv(results)
254
+
255
+ CSV.generate(headers: true) do |csv|
256
+ # Adding the headings
257
+ csv << @columns.select{|col_item| @selected_columns.include?(col_item[:column_name])}.collect{|x| x[:display_name]}
258
+
259
+ # Adding the results
260
+ @results.each_with_index do |result, i|
261
+
262
+ # Remove Id as we get it with i in each_with_index
263
+ cols_without_id = @columns.reject{|col_item| col_item[:column_name] == :id}
264
+
265
+ # Select columns which are selected by the user / default
266
+ selected_cols = cols_without_id.select{|col_item| @selected_columns.include?(col_item[:column_name]) }
267
+
268
+ # Initialize result row with just serial number
269
+ result_data = [i+1]
270
+
271
+ # Iterate selected columns and populate the result data
272
+ selected_cols.each do |col_item|
273
+
274
+ case col_item[:column_type]
275
+ when :string, :integer
276
+ result_data << result.send(col_item[:column_name])
277
+ when :status
278
+ result_data << result.send(col_item[:column_name]).titleize
279
+ when :currency
280
+ result_data << sprintf("%.2f", result.send(col_item[:column_name]))
281
+ when :date
282
+ result_data << result.send(col_item[:column_name]).strftime("%d-%m-%Y")
283
+ else
284
+ result_data << result.send(col_item[:column_name])
285
+ end
286
+
287
+ end
288
+
289
+ # Append to CSV
290
+ csv << result_data
291
+ end
292
+ end
293
+
294
+ end
295
+
296
+ end
297
+ end
298
+ end
299
+ end