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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/rawbotz/routes/non_remote_orders.rb', line 7
def self.registered(app)
show_suppliers_orders = lambda do
@suppliers = Supplier.order(:name)
haml "orders/non_remotes".to_sym
end
create_supplier_order = lambda do
@supplier = Supplier.find(params[:supplier_id])
if @supplier.order_template.to_s == ""
add_flash :warning, "You need to set the mailer template to order from this supplier"
redirect "/supplier/#{@supplier.id}#tab_order_settings"
else
@order = Order.create(state: :new, supplier: @supplier, order_method: @supplier.order_method)
@products = LocalProduct.supplied_by(@supplier)
@stock = {}
begin
@monthly_sales = Rawbotz::SalesData.sales_since(Date.today - 31 * 4, @products)
RawgentoDB::Query.stock.each {|s| @stock[s.product_id] = s.qty}
rescue Exception => e
@monthly_sales = {}
add_flash :error, "Cannot connect to MySQL database (#{e.message})"
end
@products.find_each do |product|
@order.order_items.create(local_product: product,
current_stock: @stock[product.product_id],
min_stock: nil)
end
@order.save
add_flash :success, "New Order created"
redirect "/order/non_remote/#{@order.id}".to_sym
end
end
show_supplier_order = lambda do
@order = Order.find(params[:order_id])
@supplier = @order.supplier
if @supplier.order_template.to_s == ""
add_flash :warning, "You need to set the mailer template to order from this supplier"
redirect "/supplier/#{@supplier.id}#tab_order_settings"
else
@stock = {}
begin
@products = @order.order_items.map{|oi| oi.local_product}
@monthly_sales = Rawbotz::SalesData.sales_since(Date.today - 31 * 4, @products)
RawgentoDB::Query.stock.each {|s| @stock[s.product_id] = s.qty}
rescue Exception => e
@monthly_sales = {}
add_flash :error, "Cannot connect to MySQL database (#{e.message})"
end
@mail_preview_subject = Rawbotz::MailTemplate.(
@supplier.order_template, @order)
@mail_preview_text = Rawbotz::MailTemplate.consume(
@supplier.order_template, @order)
@mailto_url = Rawbotz::MailTemplate.create_mailto_url(
@supplier, @order)
haml "order/non_remote".to_sym
end
end
show_supplier_order_preview = lambda do
@supplier = Supplier.find(params[:supplier_id])
@products = @supplier.local_products
@order = Order.find(params[:order_id])
@stock = {}
if @order.state != 'new'
add_flash :error, "Orders in state #{@order.state} cannot be changed!"
redirect "order/non_remote/#{@order.id}"
end
begin
@monthly_sales = Rawbotz::SalesData.sales_since(Date.today - 31 * 4, @products)
RawgentoDB::Query.stock.each {|s| @stock[s.product_id] = s.qty}
rescue Exception => e
@monthly_sales = {}
add_flash :error, "Cannot connect to MySQL database (#{e.message})"
end
params.select{|p| p.start_with?("item_")}.each do |p, val|
if val && val.to_i > 0
qty = val.to_i
oi = @order.order_items.where(local_product_id: p[5..-1]).first_or_create
oi.update(num_wished: qty)
end
end
@order. = params[:public_comment]
@order. = params[:internal_comment]
@mail_preview_subject = Rawbotz::MailTemplate.(
@supplier.order_template, @order)
@mail_preview_text = Rawbotz::MailTemplate.consume(
@supplier.order_template, @order)
@mailto_url = Rawbotz::MailTemplate.create_mailto_url(
@supplier, @order)
@order.order_result = @mail_preview_text
@order.ordered_at = DateTime.now
@order.save
if params['action'] == 'fix'
@order.order_items.find_each do |oi|
oi.num_ordered = oi.num_wished
oi.current_stock = @stock[oi.local_product.product_id]
oi.save
end
@order.update(state: :mailed)
add_flash :success, "Order marked as mailed"
redirect '/orders'
elsif params['action'] == "delete"
@order.update(state: :deleted)
add_flash :success, 'Order marked deleted'
redirect '/orders'
else
add_flash :success, "Order saved"
end
haml "order/non_remote".to_sym
end
show_stock_explorer = lambda do
@supplier = Supplier.find(params[:supplier_id])
@stock = {}
RawgentoDB::Query.stock.each {|s| @stock[s.product_id] = s.qty}
days_ago_30 = Date.today - 30
days_ago_60 = Date.today - 60
days_ago_90 = Date.today - 90
days_ago_356 = Date.today - 356
product_ids = @supplier.local_products.map &:product_id
@sales_30 = RawgentoDB::Query.num_sales_since(days_ago_30, product_ids)
@sales_60 = RawgentoDB::Query.num_sales_since(days_ago_60, product_ids)
@sales_90 = RawgentoDB::Query.num_sales_since(days_ago_90, product_ids)
@sales_356 = RawgentoDB::Query.num_sales_since(days_ago_356, product_ids)
@out_of_stock_days_30 = @supplier.local_products.map{|l| [l.product_id, l.stock_items.where("qty <= ? AND date >= ?", 0, days_ago_30).count]}.to_h
@out_of_stock_days_60 = @supplier.local_products.map{|l| [l.product_id, l.stock_items.where("qty <= ? AND date >= ?", 0, days_ago_60).count]}.to_h
@out_of_stock_days_90 = @supplier.local_products.map{|l| [l.product_id, l.stock_items.where("qty <= ? AND date >= ?", 0, days_ago_90).count]}.to_h
@out_of_stock_days_356 = @supplier.local_products.map{|l| [l.product_id, l.stock_items.where("qty <= ? AND date >= ?", 0, days_ago_356).count]}.to_h
haml 'stockexplorer/stockexplorer'.to_sym, layout: !request.xhr?
end
app.get '/orders/non_remote', &show_suppliers_orders
app.get '/order/non_remote/:order_id', &show_supplier_order
app.post '/order/non_remote/:order_id', &show_supplier_order_preview
app.get '/order/non_remote/:supplier_id/new', &create_supplier_order
app.get '/stockexplorer/:supplier_id', &show_stock_explorer
end
|