PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/page.rb

https://github.com/OksankaV/Stock_Viewer_2
Ruby | 1772 lines | 1630 code | 137 blank | 5 comment | 542 complexity | 6896a691008f3092c0a6d08bf1c9d15c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. # -*- encoding : utf-8 -*-
  2. require 'rubygems'
  3. require 'sinatra'
  4. require 'sqlite3'
  5. require 'json'
  6. require 'cgi'
  7. require 'net/http'
  8. require 'uri'
  9. require 'axlsx'
  10. require_relative 'public/lib/content'
  11. set :protection, :except => :ip_spoofing
  12. admin_data_hash = JSON.parse(IO.read('public/lib/admin.json'))
  13. set :username,admin_data_hash['username']
  14. set :token,admin_data_hash['token']
  15. set :password,admin_data_hash['password']
  16. helpers do
  17. def admin? ; request.cookies[settings.username] == settings.token ; end
  18. def protected! ; halt [ 401, 'Not Authorized' ] unless admin? ; end
  19. end
  20. Title = "VSIKOLESA"
  21. Price_table_headers = {'brand' =>'Виробник', 'family' => 'Марка', 'dimensiontype' => 'Типорозмір', 'sidewall' => 'Боковина', 'origin' => 'Країна', 'runflat' => 'Run Flat', 'productiondate' => 'DOT', 'season' => 'Сезон', 'remain' => 'Залишок', 'supplier' => 'Склад', 'suppliercomment' => 'Постачальник', 'rp' => 'Роздрібна ціна', 'bp' => 'Гуртова ціна', 'sp' => 'Вхідна ціна', 'bpvat' => 'Гуртова з ПДВ', 'actualdate' => 'Дата', 'sourcestring' => 'Вхідний рядок'}
  22. Price_table_columns = ['id', 'brand', 'family', 'origin', 'comment', 'remain', 'moreflag', 'supplier', 'sp', 'spc', 'sourcestring', 'minimalorder', 'deliverytyme', 'suppliercomment', 'actualdate', 'runflat', 'sidewall', 'productiondate', 'diameterc', 'application', 'season', 'traileraxle', 'steeringaxle', 'driveaxle', 'dimensiontype', 'sectionsize', 'bp', 'bpvat', 'bppe', 'rp', 'rpvat', 'rppe', 'unknown']
  23. Show_data_field = ['id','brand', 'family', 'dimensiontype', 'sidewall', 'origin', 'runflat', 'productiondate', 'season', 'bp', 'remain', 'supplier', 'rp', 'sp', 'suppliercomment', 'bpvat', 'actualdate', 'sourcestring']
  24. Header_data_field = {'id' => 'Вибрати', 'family' => 'Модель', 'season' => 'Сезон', 'dimensiontype' => 'Типорозмір', 'bp' => 'Гуртова ціна', 'rp' => 'Роздрібна ціна'}
  25. Seasons = ["-", "літо", "зима", "в/c"]
  26. Seasons_images = ["question", "summer", "winter", "all_season"]
  27. Remain = Array.new(10000){ |index| index.to_s}
  28. Orders_table_headers_cut = {'issued' =>'Дата','buyer' =>'Покупець', 'article' => 'Товар', 'amount' => 'К-ть', 'supplier' => 'Склад', 'status' => 'Статус', 'sent_date' => 'Від-ня', 'reserve_date' => 'Резерв', 'expected_receive_date' => 'План. от.', 'receive_date' => 'Факт. от.', 'post_name' => 'Трансп. комп.', 'track_id' => '№ декларації', 'cashless_flag' => 'Банківській переказ','taxes' => 'Податки' ,'notes' => 'Нотатки замовлення'}
  29. Orders_table_headers = {'issued' =>'Дата','buyer' =>'Покупець', 'article' => 'Товар', 'amount' => 'Кількість', 'supplier' => 'Склад', 'inprice' => 'Вхідна ціна (1 шт)', 'outprice' => 'Продажна ціна (1 шт)', 'transfered' => 'Оплачено клієнтом', 'transfered_date' => 'Дата', 'transfered_bank' => 'Банк', 'transferprice' => 'Оплачено нами', 'transferprice_date' => 'Дата', 'transferprice_bank' => 'Банк', 'status' => 'Статус', 'sent_date' => 'Відправлення', 'reserve_date' => 'Резерв', 'expected_receive_date' => 'Планове отримання', 'receive_date' => 'Фактичне отримання', 'post_name' => 'Транспортна компанія', 'track_id' => '№ декларації', 'cashless_flag' => 'Банківській переказ','taxes' => 'Податки' ,'notes' => 'Нотатки замовлення'}
  30. Orders_table_columns = ['id','issued','buyer', 'article', 'amount', 'supplier', 'inprice', 'outprice', 'transfered', 'transfered_date', 'transfered_bank', 'transferprice', 'transferprice_date', 'transferprice_bank', 'status', 'sent_date', 'reserve_date', 'expected_receive_date', 'receive_date', 'post_name', 'track_id', 'cashless_flag','taxes','notes']
  31. Status_values_array = ['нове', 'резерв', 'відправлено', 'отримано']
  32. Orders_table_excel_columns = ['buyer', 'fullname', 'telephone', 'city','article','amount', 'supplier', 'sent_date', 'expected_receive_date','post_name','track_id']
  33. Buyers_table_columns = ['name', 'fullname', 'telephone', 'city', 'contact_person', 'notes']
  34. Buyers_table_headers = {'buyer' =>'Скорочена назва', 'fullname' => 'Повна назва', 'telephone' => 'Телефон', 'city' => 'Місто', 'contact_person' => 'Контактна особа', 'notes' => 'Нотатки'}
  35. def select_data_from_db()
  36. if File.exists?("../data/tyre.db")
  37. $db = SQLite3::Database.new("../data/tyre.db")
  38. $price_date_check = File.new("../data/tyre.db").mtime
  39. $price_date = File.new("../data/tyre.db").mtime.localtime("+03:00").strftime("(оновлено %d/%m/%Y)")
  40. end
  41. $tyre_size = $db.execute("SELECT DISTINCT sectionsize FROM price ORDER BY sectionsize ASC").flatten
  42. $tyre_diameter = $db.execute("SELECT DISTINCT diameterc FROM price ORDER BY diameterc ASC").flatten
  43. $tyre_season = $db.execute("SELECT DISTINCT season FROM price ORDER BY season ASC").flatten
  44. $tyre_supplier = $db.execute("SELECT DISTINCT supplier FROM price ORDER BY supplier ASC").flatten
  45. tyre_family_brand_name = $db.execute("SELECT DISTINCT family, brand FROM price")
  46. tyre_family_brand = {}
  47. tyre_family_brand_name.each do |brand_family|
  48. if !tyre_family_brand.has_key?(brand_family.last)
  49. tyre_family_brand[brand_family.last] = []
  50. end
  51. tyre_family_brand[brand_family.last].push(brand_family.first)
  52. end
  53. tyre_family = tyre_family_brand.values.flatten.uniq.sort
  54. $tyre_brand_name = tyre_family_brand.keys.sort
  55. $tyre_family_brand_name = tyre_family_brand
  56. $tyre_family_name = tyre_family
  57. $tyre_family_brand_name.each_pair do |brand,families|
  58. $tyre_family_brand_name[brand] = []
  59. families.each do |one_family|
  60. $tyre_family_brand_name[brand].push(one_family)
  61. end
  62. end
  63. end
  64. def select_data_from_orders_db()
  65. if File.exists?("../data/orders.db")
  66. $db_orders = SQLite3::Database.new("../data/orders.db")
  67. $db_orders.execute("PRAGMA foreign_keys = ON;")
  68. end
  69. end
  70. def select_values(param_select_values,new_param_value,db_array)
  71. if param_select_values == nil
  72. select_array = []
  73. else
  74. if param_select_values != [""]
  75. select_array = param_select_values
  76. else
  77. select_array = []
  78. end
  79. end
  80. if new_param_value == nil
  81. select_array = []
  82. else
  83. if new_param_value != "" and db_array.include?(new_param_value) and select_array.include?(new_param_value) == false
  84. select_array.push(new_param_value)
  85. end
  86. end
  87. return (select_array)
  88. end
  89. def detect_one_select_value(input_value,select_value)
  90. return_value = ""
  91. if ((input_value == nil) || (input_value == ""))
  92. if select_value == nil
  93. return_value = ""
  94. else
  95. if select_value != ""
  96. return_value = select_value
  97. else
  98. return_value = ""
  99. end
  100. end
  101. else
  102. return_value = input_value
  103. end
  104. return return_value
  105. end
  106. def filter_select(select, value, check_value, text, hash_key)
  107. if check_value.include?(value)
  108. select = select + text
  109. @bind_hash[hash_key.to_sym] = value
  110. end
  111. return select
  112. end
  113. def make_href(select_value,param_name,param_array_name)
  114. if select_value.empty?
  115. @table_href += param_name + "="
  116. else
  117. select_value.each do |value|
  118. @table_href += param_array_name + "=" + CGI::escape(value)
  119. end
  120. end
  121. end
  122. select_data_from_db()
  123. get '/' do
  124. if (File.new("../data/tyre.db").mtime != $price_date_check)
  125. select_data_from_db()
  126. end
  127. @message = "Для пошуку даних обов'язково введіть параметр Ширина/Висота"
  128. @message_no_data = "Немає даних, що відповідають вибраним значенням"
  129. if params[:press_reset_button] == "true"
  130. @select_brands = []
  131. @select_families = []
  132. @select_sizes = []
  133. @select_diameters = []
  134. @select_seasons = []
  135. @select_suppliers = []
  136. @select_date = ""
  137. @select_remain = ""
  138. @select_start_price = ""
  139. @select_finish_price = ""
  140. else
  141. @select_brands = select_values(params[:tyre_brand_selected],params[:tyre_brand_typeahead],$tyre_brand_name)
  142. @select_families = select_values(params[:tyre_family_selected],params[:tyre_family_typeahead],$tyre_family_name)
  143. @select_sizes = select_values(params[:tyre_size_selected],params[:tyre_size_typeahead],$tyre_size)
  144. @select_diameters = select_values(params[:tyre_diameter_selected],params[:tyre_diameter_typeahead],$tyre_diameter)
  145. @select_seasons = select_values(params[:tyre_season_selected],"",$tyre_season)
  146. @select_suppliers = select_values(params[:tyre_supplier_selected],params[:tyre_supplier_typeahead],$tyre_supplier)
  147. @select_date = detect_one_select_value(params[:tyre_date_typeahead],params[:tyre_date_selected])
  148. @select_remain = detect_one_select_value(params[:tyre_remain_typeahead],params[:tyre_remain_selected])
  149. @select_start_price = detect_one_select_value(params[:tyre_start_price_input],params[:tyre_start_price_selected])
  150. @select_finish_price = detect_one_select_value(params[:tyre_finish_price_input],params[:tyre_finish_price_selected])
  151. end
  152. tyre_family_help_array = []
  153. if @select_brands.empty?
  154. tyre_family_help_array = $tyre_family_name
  155. else
  156. help_array = []
  157. $tyre_family_brand_name.each_pair do |brand,brand_families|
  158. if @select_brands.include?(brand)
  159. brand_families.each do |one_family|
  160. tyre_family_help_array.push(one_family)
  161. end
  162. end
  163. end
  164. end
  165. @tyre_family_help_array = tyre_family_help_array
  166. @table_href = ""
  167. if @select_date.empty?
  168. @table_href += "date="
  169. else
  170. @table_href += "date=" + @select_date
  171. end
  172. if @select_remain.empty?
  173. @table_href += "&remain="
  174. else
  175. @table_href += "&remain=" + @select_remain
  176. end
  177. if @select_start_price.empty?
  178. @table_href += "&start_price="
  179. else
  180. @table_href += "&start_price=" + @select_start_price
  181. end
  182. if @select_finish_price.empty?
  183. @table_href += "&finish_price="
  184. else
  185. @table_href += "&finish_price=" + @select_finish_price
  186. end
  187. make_href(@select_brands,"&brand","&brand[]")
  188. make_href(@select_families,"&family","&family[]")
  189. make_href(@select_sizes,"&size","&size[]")
  190. make_href(@select_diameters,"&diameter","&diameter[]")
  191. make_href(@select_seasons,"&season","&season[]")
  192. make_href(@select_suppliers,"&supplier","&supplier[]")
  193. @table_url = @table_href
  194. @show_table = false
  195. @bind_hash = {}
  196. if @select_sizes.empty? == false
  197. if params[:press_submit_button] == "true"
  198. @press_submit_button = true
  199. @show_table = true
  200. select_string = "select brand, min(rp), max(rp), group_concat(distinct(nullif(origin,''))), count(*) from price where"
  201. select_string = select_string + " ( "
  202. i=0
  203. @select_sizes.each do |tyre_sizes_check|
  204. if i == 0
  205. select_string = filter_select(select_string, tyre_sizes_check, $tyre_size, "sectionsize = :size" + i.to_s, "size" + i.to_s)
  206. else
  207. select_string = filter_select(select_string, tyre_sizes_check, $tyre_size, " or sectionsize = :size" + i.to_s, "size" + i.to_s)
  208. end
  209. i += 1
  210. end
  211. select_string = select_string + " ) "
  212. if @select_brands.empty? == false
  213. select_string = select_string + " and ( "
  214. i=0
  215. @select_brands.each do |tyre_brands_check|
  216. if i == 0
  217. select_string = filter_select(select_string, tyre_brands_check, $tyre_brand_name, "brand = :brand" + i.to_s, "brand" + i.to_s)
  218. else
  219. select_string = filter_select(select_string, tyre_brands_check, $tyre_brand_name, " or brand = :brand" + i.to_s, "brand" + i.to_s)
  220. end
  221. i += 1
  222. end
  223. select_string = select_string + " ) "
  224. end
  225. if @select_families.empty? == false
  226. select_string = select_string + " and ( "
  227. i=0
  228. @select_families.each do |tyre_families_check|
  229. if i == 0
  230. select_string = filter_select(select_string, tyre_families_check, $tyre_family_name, "family = :family" + i.to_s, "family" + i.to_s)
  231. else
  232. select_string = filter_select(select_string, tyre_families_check, $tyre_family_name, " or family = :family" + i.to_s, "family" + i.to_s)
  233. end
  234. i += 1
  235. end
  236. select_string = select_string + " ) "
  237. end
  238. if @select_diameters.empty? == false
  239. select_string = select_string + " and ( "
  240. i=0
  241. @select_diameters.each do |tyre_diameters_check|
  242. if i == 0
  243. select_string = filter_select(select_string, tyre_diameters_check, $tyre_diameter, "diameterc = :diameterc" + i.to_s, "diameterc" + i.to_s)
  244. else
  245. select_string = filter_select(select_string, tyre_diameters_check, $tyre_diameter, " or diameterc = :diameterc" + i.to_s, "diameterc" + i.to_s)
  246. end
  247. i += 1
  248. end
  249. select_string = select_string + " ) "
  250. end
  251. if @select_seasons.empty? == false
  252. select_string = select_string + " and ( "
  253. i=0
  254. @select_seasons.each do |tyre_seasons_check|
  255. if i == 0
  256. select_string = filter_select(select_string, tyre_seasons_check, $tyre_season, "season = :season" + i.to_s, "season" + i.to_s)
  257. else
  258. select_string = filter_select(select_string, tyre_seasons_check, $tyre_season, " or season = :season" + i.to_s, "season" + i.to_s)
  259. end
  260. i += 1
  261. end
  262. select_string = filter_select(select_string, "0", $tyre_season, " or season = :season" + i.to_s, "season" + i.to_s)
  263. select_string = select_string + " ) "
  264. end
  265. if @select_suppliers.empty? == false
  266. select_string = select_string + " and ( "
  267. i=0
  268. @select_suppliers.each do |tyre_suppliers_check|
  269. if i == 0
  270. select_string = filter_select(select_string, tyre_suppliers_check, $tyre_supplier, "supplier = :supplier" + i.to_s, "supplier" + i.to_s)
  271. else
  272. select_string = filter_select(select_string, tyre_suppliers_check, $tyre_supplier, " or supplier = :supplier" + i.to_s, "supplier" + i.to_s)
  273. end
  274. i += 1
  275. end
  276. select_string = select_string + " ) "
  277. end
  278. if @select_remain.empty? == false
  279. select_string = select_string + " and (remain >= :remain or remain = 0) "
  280. @bind_hash["remain".to_sym] = @select_remain
  281. end
  282. if @select_start_price.empty? == false || @select_finish_price.empty? == false
  283. select_string = select_string + " and ( "
  284. if @select_start_price != ""
  285. select_string = select_string + "rp >= :start_price"
  286. @bind_hash["start_price".to_sym] = @select_start_price
  287. end
  288. if (@select_start_price != "" && @select_finish_price != "")
  289. select_string = select_string + " and "
  290. end
  291. if @select_finish_price != ""
  292. select_string = select_string + "rp <= :finish_price"
  293. @bind_hash["finish_price".to_sym] = @select_finish_price
  294. end
  295. select_string = select_string + " ) "
  296. end
  297. if @select_date.empty? == false
  298. select_string = select_string + " and (actualdate >= :date) "
  299. date = @select_date.scan(/(\d+)\/(\d+)\/(\d+)/).flatten
  300. @bind_hash["date".to_sym] = Time.gm(date[2],date[1],date[0]).strftime("%Y-%m-%d %H:%M:%S")
  301. end
  302. select_string = select_string + "GROUP BY brand ORDER BY brand"
  303. select_brand_price_array = $db.execute(select_string, @bind_hash)
  304. @select_brand_price_hash = {}
  305. select_brand_price_array.each do |brand_price_array|
  306. @select_brand_price_hash[brand_price_array.first] = [brand_price_array[1],brand_price_array[2],brand_price_array[3],brand_price_array[4]]
  307. end
  308. if @select_brand_price_hash.empty?
  309. @show_table = false
  310. end
  311. @search_primary_btn = ""
  312. @show_primary_btn = "btn-primary"
  313. else
  314. @message = "Для відображення даних натисніть ПОШУК"
  315. @search_primary_btn = "btn-primary"
  316. @show_primary_btn = ""
  317. end
  318. else
  319. @message = "Для пошуку даних обов'язково введіть параметр Ширина/Висота"
  320. @search_primary_btn = "btn-primary"
  321. @show_primary_btn = ""
  322. end
  323. if admin?
  324. protected!
  325. #@show_all_columns = true
  326. @admin_filter_page = true
  327. erb :filter
  328. else
  329. @checked_array = []
  330. add_to_checked_array(@select_sizes,"size[]")
  331. add_to_checked_array(@select_brands,"brand[]")
  332. add_to_checked_array(@select_families,"family[]")
  333. add_to_checked_array(@select_diameters,"diameter[]")
  334. add_to_checked_array(@select_suppliers,"supplier[]")
  335. add_to_checked_array(@select_seasons,"season[]")
  336. if @select_date.empty? == false
  337. hash = {}
  338. hash['name'] = "date"
  339. hash['value'] = @select_date
  340. @checked_array.push(hash)
  341. end
  342. if @select_remain.empty? == false
  343. hash = {}
  344. hash['name'] = "remain"
  345. hash['value'] = @select_remain
  346. @checked_array.push(hash)
  347. end
  348. if @select_start_price.empty? == false
  349. hash = {}
  350. hash['name'] = "start_price"
  351. hash['value'] = @select_start_price
  352. @checked_array.push(hash)
  353. end
  354. if @select_finish_price.empty? == false
  355. hash = {}
  356. hash['name'] = "finish_price"
  357. hash['value'] = @select_finish_price
  358. @checked_array.push(hash)
  359. end
  360. @admin_filter_page = false
  361. erb :filter
  362. end
  363. end
  364. get '/table' do
  365. @bind_hash = {}
  366. select_brands = params[:brand]
  367. select_brands = "" if select_brands == nil
  368. select_families = params[:family]
  369. select_families = "" if select_families == nil
  370. select_sizes = params[:size]
  371. select_diameters = params[:diameter]
  372. select_diameters = "" if select_diameters == nil
  373. select_suppliers = params[:supplier]
  374. select_suppliers = "" if select_suppliers == nil
  375. select_seasons = params[:season]
  376. select_seasons = "" if select_seasons == nil
  377. select_date = params[:date]
  378. select_date = "" if select_date == nil
  379. select_remain = params[:remain]
  380. select_remain = "" if select_remain == nil
  381. select_start_price = params[:start_price]
  382. select_start_price = "" if select_start_price == nil
  383. select_finish_price = params[:finish_price]
  384. select_finish_price = "" if select_finish_price == nil
  385. select_string = "select * from price where"
  386. select_string = select_string + " ( "
  387. i=0
  388. select_sizes.each do |tyre_sizes_check|
  389. if i == 0
  390. select_string = filter_select(select_string, tyre_sizes_check, $tyre_size, "sectionsize = :size" + i.to_s, "size" + i.to_s)
  391. else
  392. select_string = filter_select(select_string, tyre_sizes_check, $tyre_size, " or sectionsize = :size" + i.to_s, "size" + i.to_s)
  393. end
  394. i += 1
  395. end
  396. select_string = select_string + " ) "
  397. if select_brands.empty? == false
  398. select_string = select_string + " and ( "
  399. i=0
  400. select_brands.each do |tyre_brands_check|
  401. if i == 0
  402. select_string = filter_select(select_string, tyre_brands_check, $tyre_brand_name, "brand = :brand" + i.to_s, "brand" + i.to_s)
  403. else
  404. select_string = filter_select(select_string, tyre_brands_check, $tyre_brand_name, " or brand = :brand" + i.to_s, "brand" + i.to_s)
  405. end
  406. i += 1
  407. end
  408. select_string = select_string + " ) "
  409. end
  410. if select_families.empty? == false
  411. select_string = select_string + " and ( "
  412. i=0
  413. select_families.each do |tyre_families_check|
  414. if i == 0
  415. select_string = filter_select(select_string, tyre_families_check, $tyre_family_name, "family = :family" + i.to_s, "family" + i.to_s)
  416. else
  417. select_string = filter_select(select_string, tyre_families_check, $tyre_family_name, " or family = :family" + i.to_s, "family" + i.to_s)
  418. end
  419. i += 1
  420. end
  421. select_string = select_string + " ) "
  422. end
  423. if select_diameters.empty? == false
  424. select_string = select_string + " and ( "
  425. i=0
  426. select_diameters.each do |tyre_diameters_check|
  427. if i == 0
  428. select_string = filter_select(select_string, tyre_diameters_check, $tyre_diameter, "diameterc = :diameterc" + i.to_s, "diameterc" + i.to_s)
  429. else
  430. select_string = filter_select(select_string, tyre_diameters_check, $tyre_diameter, " or diameterc = :diameterc" + i.to_s, "diameterc" + i.to_s)
  431. end
  432. i += 1
  433. end
  434. select_string = select_string + " ) "
  435. end
  436. if select_suppliers.empty? == false
  437. select_string = select_string + " and ( "
  438. i=0
  439. select_suppliers.each do |tyre_suppliers_check|
  440. if i == 0
  441. select_string = filter_select(select_string, tyre_suppliers_check, $tyre_supplier, "supplier = :supplier" + i.to_s, "supplier" + i.to_s)
  442. else
  443. select_string = filter_select(select_string, tyre_suppliers_check, $tyre_supplier, " or supplierc = :supplier" + i.to_s, "supplier" + i.to_s)
  444. end
  445. i += 1
  446. end
  447. select_string = select_string + " ) "
  448. end
  449. if select_seasons.empty? == false
  450. select_string = select_string + " and ( "
  451. i=0
  452. select_seasons.each do |tyre_seasons_check|
  453. if i == 0
  454. select_string = filter_select(select_string, tyre_seasons_check, $tyre_season, "season = :season" + i.to_s, "season" + i.to_s)
  455. else
  456. select_string = filter_select(select_string, tyre_seasons_check, $tyre_season, " or season = :season" + i.to_s, "season" + i.to_s)
  457. end
  458. i += 1
  459. end
  460. select_string = filter_select(select_string, "0", $tyre_season, " or season = :season" + i.to_s, "season" + i.to_s)
  461. select_string = select_string + " ) "
  462. end
  463. if select_remain.empty? == false
  464. select_string = select_string + " and (remain >= :remain or remain = 0) "
  465. @bind_hash["remain".to_sym] = select_remain
  466. end
  467. if select_start_price.empty? == false || select_finish_price.empty? == false
  468. select_string = select_string + " and ( "
  469. if select_start_price != ""
  470. select_string = select_string + "rp >= :start_price"
  471. @bind_hash["start_price".to_sym] = select_start_price
  472. end
  473. if (select_start_price != "" && select_finish_price != "")
  474. select_string = select_string + " and "
  475. end
  476. if select_finish_price != ""
  477. select_string = select_string + "rp <= :finish_price"
  478. @bind_hash["finish_price".to_sym] = select_finish_price
  479. end
  480. select_string = select_string + " ) "
  481. end
  482. if select_date.empty? == false
  483. select_string = select_string + " and (actualdate >= :date) "
  484. date = select_date.scan(/(\d+)\/(\d+)\/(\d+)/).flatten
  485. @bind_hash["date".to_sym] = Time.gm(date[2],date[1],date[0]).strftime("%Y-%m-%d %H:%M:%S")
  486. end
  487. all_data_array = []
  488. show_data_array = []
  489. select_all_data = $db.execute(select_string, @bind_hash)
  490. select_all_data.each do |one_row_data|
  491. data_hash = {}
  492. show_data_hash = {}
  493. one_row_data.each_index do |index|
  494. data_hash[Price_table_columns[index]] = one_row_data[index]
  495. if Header_data_field.has_key?(Price_table_columns[index])
  496. show_data_hash[Price_table_columns[index]] = one_row_data[index]
  497. end
  498. end
  499. all_data_array.push(data_hash)
  500. show_data_array.push(show_data_hash)
  501. end
  502. show_data_array.each_index do |show_data_array_index|
  503. data_hash = show_data_array.at(show_data_array_index)
  504. data_hash.each_pair do |data_hash_key, data_hash_value|
  505. if data_hash_value.class == Float
  506. show_data_array[show_data_array_index][data_hash_key] = data_hash_value.round(2)
  507. end
  508. if data_hash_key == 'season'
  509. show_data_array[show_data_array_index][data_hash_key] = Seasons[data_hash_value.to_i]
  510. end
  511. if (data_hash_key == 'sp') and (show_data_array[show_data_array_index]['sp'] != 0 or show_data_array[show_data_array_index]['sp'] != "невідомо")
  512. if data_hash['spc'] == "1"
  513. show_data_array[show_data_array_index][data_hash_key] = show_data_array[show_data_array_index][data_hash_key].ceil.to_s + " грн."
  514. elsif data_hash['spc'] == "2"
  515. show_data_array[show_data_array_index][data_hash_key] = (show_data_array[show_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " $"
  516. elsif data_hash['spc'] == "3"
  517. show_data_array[show_data_array_index][data_hash_key] = (show_data_array[show_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " &euro;"
  518. elsif data_hash['spc'] == "4"
  519. show_data_array[show_data_array_index][data_hash_key] = (show_data_array[show_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " PLN"
  520. end
  521. end
  522. if (data_hash_key == 'sp') and (show_data_array[show_data_array_index]['sp'] == 0 or show_data_array[show_data_array_index]['sp'] == "невідомо")
  523. show_data_array[show_data_array_index][data_hash_key] = "невідомо"
  524. end
  525. if (data_hash_key == 'bp' or data_hash_key == 'bpvat' or data_hash_key == 'rpvat' or data_hash_key == 'rppe' or data_hash_key == 'rp') and (show_data_array[show_data_array_index]['sp'] != 0 or show_data_array[show_data_array_index]['sp'] != "невідомо")
  526. show_data_array[show_data_array_index][data_hash_key ] = show_data_array[show_data_array_index][data_hash_key ].ceil.to_s + " грн."
  527. end
  528. if (data_hash_key == 'bp' or data_hash_key == 'bpvat' or data_hash_key == 'rpvat' or data_hash_key == 'rppe' or data_hash_key == 'rp') and (show_data_array[show_data_array_index]['sp'] == 0 or show_data_array[show_data_array_index]['sp'] == "невідомо")
  529. show_data_array[show_data_array_index][data_hash_key] = "невідомо"
  530. end
  531. end
  532. end
  533. return (JSON.pretty_generate(show_data_array))
  534. end
  535. def add_to_checked_array(param_array,param_name)
  536. param_array.each do |value|
  537. hash = {}
  538. hash['name'] = param_name
  539. hash['value'] = value
  540. @checked_array.push(hash)
  541. end
  542. end
  543. post '/selected_items' do
  544. select_brands = params[:brand]
  545. select_brands = [] if select_brands == nil
  546. select_families = params[:family]
  547. select_families = [] if select_families == nil
  548. select_sizes = params[:size]
  549. select_diameters = params[:diameter]
  550. select_diameters = [] if select_diameters == nil
  551. select_suppliers = params[:supplier]
  552. select_suppliers = [] if select_suppliers == nil
  553. select_seasons = params[:season]
  554. select_seasons = [] if select_seasons == nil
  555. select_date = params[:date]
  556. select_date = "" if select_date == nil
  557. select_remain = params[:remain]
  558. select_remain = "" if select_remain == nil
  559. select_start_price = params[:start_price]
  560. select_start_price = "" if select_start_price == nil
  561. select_finish_price = params[:finish_price]
  562. select_finish_price = "" if select_finish_price == nil
  563. @show_table = true
  564. checked_id_array = params[:checked_id]
  565. checked_id_array = [] if checked_id_array == nil
  566. checked_brand_array = params[:checked_brand]
  567. checked_brand_array = [] if checked_brand_array == nil
  568. if checked_id_array.empty? and checked_brand_array.empty?
  569. @show_table = false
  570. end
  571. @checked_array = []
  572. add_to_checked_array(checked_id_array,"checked_id[]")
  573. add_to_checked_array(checked_brand_array,"checked_brand[]")
  574. add_to_checked_array(select_sizes,"size[]")
  575. add_to_checked_array(select_brands,"brand[]")
  576. add_to_checked_array(select_families,"family[]")
  577. add_to_checked_array(select_diameters,"diameter[]")
  578. add_to_checked_array(select_suppliers,"supplier[]")
  579. add_to_checked_array(select_seasons,"season[]")
  580. if select_date.empty? == false
  581. hash = {}
  582. hash['name'] = "date"
  583. hash['value'] = select_date
  584. @checked_array.push(hash)
  585. end
  586. if select_remain.empty? == false
  587. hash = {}
  588. hash['name'] = "remain"
  589. hash['value'] = select_remain
  590. @checked_array.push(hash)
  591. end
  592. if select_start_price.empty? == false
  593. hash = {}
  594. hash['name'] = "start_price"
  595. hash['value'] = select_start_price
  596. @checked_array.push(hash)
  597. end
  598. if select_finish_price.empty? == false
  599. hash = {}
  600. hash['name'] = "finish_price"
  601. hash['value'] = select_finish_price
  602. @checked_array.push(hash)
  603. end
  604. @message = "Ви не вибрали жодного елементу"
  605. if admin?
  606. protected!
  607. @admin_select_items_page = true
  608. erb :selected_items
  609. else
  610. @admin_select_items_page = false
  611. erb :selected_items
  612. end
  613. end
  614. post '/table_selected_items' do
  615. select_brands = params[:brand]
  616. select_brands = [] if select_brands == nil
  617. select_families = params[:family]
  618. select_families = [] if select_families == nil
  619. select_sizes = params[:size]
  620. select_diameters = params[:diameter]
  621. select_diameters = [] if select_diameters == nil
  622. select_suppliers = params[:supplier]
  623. select_suppliers = [] if select_suppliers == nil
  624. select_seasons = params[:season]
  625. select_seasons = [] if select_seasons == nil
  626. select_date = params[:date]
  627. select_date = "" if select_date == nil
  628. select_remain = params[:remain]
  629. select_remain = "" if select_remain == nil
  630. select_start_price = params[:start_price]
  631. select_start_price = "" if select_start_price == nil
  632. select_finish_price = params[:finish_price]
  633. select_finish_price = "" if select_finish_price == nil
  634. @checked_id_array = params[:checked_id]
  635. @checked_id_array = [] if @checked_id_array == nil
  636. @checked_brand_array = params[:checked_brand]
  637. @checked_brand_array = [] if @checked_brand_array == nil
  638. @bind_hash = {}
  639. sortname_column = params[:sortname]
  640. rp_number = params[:rp]
  641. page_number = params[:page]
  642. sortorder_value = params[:sortorder]
  643. select_string = "select * from price where"
  644. if (@checked_brand_array.empty? == false && admin?) || (@checked_brand_array.empty? && admin? == false)
  645. if select_sizes.empty? == false
  646. select_string = select_string + " ( "
  647. i=0
  648. select_sizes.each do |tyre_sizes_check|
  649. if i == 0
  650. select_string = filter_select(select_string, tyre_sizes_check, $tyre_size, "sectionsize = :size" + i.to_s, "size" + i.to_s)
  651. else
  652. select_string = filter_select(select_string, tyre_sizes_check, $tyre_size, " or sectionsize = :size" + i.to_s, "size" + i.to_s)
  653. end
  654. i += 1
  655. end
  656. select_string = select_string + " ) "
  657. if select_families.empty? == false
  658. select_string = select_string + " and ( "
  659. i=0
  660. select_families.each do |tyre_families_check|
  661. if i == 0
  662. select_string = filter_select(select_string, tyre_families_check, $tyre_family_name, "family = :family" + i.to_s, "family" + i.to_s)
  663. else
  664. select_string = filter_select(select_string, tyre_families_check, $tyre_family_name, " or family = :family" + i.to_s, "family" + i.to_s)
  665. end
  666. i += 1
  667. end
  668. select_string = select_string + " ) "
  669. end
  670. if select_diameters.empty? == false
  671. select_string = select_string + " and ( "
  672. i=0
  673. select_diameters.each do |tyre_diameters_check|
  674. if i == 0
  675. select_string = filter_select(select_string, tyre_diameters_check, $tyre_diameter, "diameterc = :diameterc" + i.to_s, "diameterc" + i.to_s)
  676. else
  677. select_string = filter_select(select_string, tyre_diameters_check, $tyre_diameter, " or diameterc = :diameterc" + i.to_s, "diameterc" + i.to_s)
  678. end
  679. i += 1
  680. end
  681. select_string = select_string + " ) "
  682. end
  683. if select_suppliers.empty? == false
  684. select_string = select_string + " and ( "
  685. i=0
  686. select_suppliers.each do |tyre_suppliers_check|
  687. if i == 0
  688. select_string = filter_select(select_string, tyre_suppliers_check, $tyre_supplier, "supplier = :supplier" + i.to_s, "supplier" + i.to_s)
  689. else
  690. select_string = filter_select(select_string, tyre_suppliers_check, $tyre_supplier, " or supplier = :supplier" + i.to_s, "supplier" + i.to_s)
  691. end
  692. i += 1
  693. end
  694. select_string = select_string + " ) "
  695. end
  696. if select_seasons.empty? == false
  697. select_string = select_string + " and ( "
  698. i=0
  699. select_seasons.each do |tyre_seasons_check|
  700. if i == 0
  701. select_string = filter_select(select_string, tyre_seasons_check, $tyre_season, "season = :season" + i.to_s, "season" + i.to_s)
  702. else
  703. select_string = filter_select(select_string, tyre_seasons_check, $tyre_season, " or season = :season" + i.to_s, "season" + i.to_s)
  704. end
  705. i += 1
  706. end
  707. select_string = filter_select(select_string, "0", $tyre_season, " or season = :season" + i.to_s, "season" + i.to_s)
  708. select_string = select_string + " ) "
  709. end
  710. if select_remain.empty? == false
  711. select_string = select_string + " and (remain >= :remain or remain = 0) "
  712. @bind_hash["remain".to_sym] = select_remain
  713. end
  714. if select_start_price.empty? == false || select_finish_price.empty? == false
  715. select_string = select_string + " and ( "
  716. if select_start_price != ""
  717. select_string = select_string + "rp >= :start_price"
  718. @bind_hash["start_price".to_sym] = select_start_price
  719. end
  720. if (select_start_price != "" && select_finish_price != "")
  721. select_string = select_string + " and "
  722. end
  723. if select_finish_price != ""
  724. select_string = select_string + "rp <= :finish_price"
  725. @bind_hash["finish_price".to_sym] = select_finish_price
  726. end
  727. select_string = select_string + " ) "
  728. end
  729. if select_date.empty? == false
  730. select_string = select_string + " and (actualdate >= :date) "
  731. date = select_date.scan(/(\d+)\/(\d+)\/(\d+)/).flatten
  732. @bind_hash["date".to_sym] = Time.gm(date[2],date[1],date[0]).strftime("%Y-%m-%d %H:%M:%S")
  733. end
  734. if admin?
  735. select_string = select_string + " and ( "
  736. i = 0
  737. @checked_brand_array.each do |checked_brand|
  738. if i == 0
  739. select_string = filter_select(select_string, checked_brand, $tyre_brand_name, "brand = :brand" + i.to_s, "brand" + i.to_s)
  740. else
  741. select_string = filter_select(select_string, checked_brand, $tyre_brand_name, " or brand = :brand" + i.to_s, "brand" + i.to_s)
  742. end
  743. i += 1
  744. end
  745. select_string = select_string + " ) "
  746. else
  747. if select_brands.empty? == false
  748. select_string = select_string + " and ( "
  749. i = 0
  750. select_brands.each do |select_brand|
  751. if i == 0
  752. select_string = filter_select(select_string, select_brand, $tyre_brand_name, "brand = :brand" + i.to_s, "brand" + i.to_s)
  753. else
  754. select_string = filter_select(select_string, select_brand, $tyre_brand_name, " or brand = :brand" + i.to_s, "brand" + i.to_s)
  755. end
  756. i += 1
  757. end
  758. select_string = select_string + " ) "
  759. end
  760. end
  761. end
  762. end
  763. if admin?
  764. protected!
  765. if @checked_id_array.empty? == false
  766. if @checked_brand_array.empty? == false
  767. select_string = select_string + " or ( "
  768. else
  769. select_string = select_string + " ( "
  770. end
  771. i = 0
  772. @checked_id_array.each do |checked_id|
  773. if i == 0
  774. @bind_hash[("id" + i.to_s).to_sym] = checked_id
  775. select_string = select_string + "id = :id" + i.to_s
  776. else
  777. @bind_hash[("id" + i.to_s).to_sym] = checked_id
  778. select_string = select_string + " or id = :id" + i.to_s
  779. end
  780. i += 1
  781. end
  782. select_string = select_string + " ) "
  783. end
  784. end
  785. select_count = select_string.gsub(/\*/,"count(*)")
  786. rows_count = $db.execute(select_count, @bind_hash).flatten
  787. @select_string_to_excel = select_string
  788. offset_value = page_number.to_i * rp_number.to_i - rp_number.to_i
  789. select_string = select_string + " order by " + sortname_column + " " + sortorder_value + " limit " + rp_number + " offset " + offset_value.to_s
  790. all_data_array = []
  791. select_all_data = $db.execute(select_string, @bind_hash)
  792. select_all_data.each do |one_row_data|
  793. data_hash = {}
  794. one_row_data.each_index do |index|
  795. data_hash[Price_table_columns[index]] = one_row_data[index]
  796. end
  797. all_data_array.push(data_hash)
  798. end
  799. all_data_array.each_index do |all_data_array_index|
  800. data_hash = all_data_array.at(all_data_array_index)
  801. data_hash.each_pair do |data_hash_key, data_hash_value|
  802. if data_hash_value.class == Float
  803. all_data_array[all_data_array_index][data_hash_key] = data_hash_value.round(2)
  804. end
  805. if data_hash_key == 'moreflag' and data_hash_value == 1
  806. all_data_array[all_data_array_index]['remain'] = ">" + all_data_array[all_data_array_index]['remain'].to_s
  807. end
  808. if data_hash_key == 'runflat'
  809. if data_hash_value == 1
  810. all_data_array[all_data_array_index][data_hash_key] = "Так"
  811. else
  812. all_data_array[all_data_array_index][data_hash_key] = "Ні"
  813. end
  814. end
  815. if data_hash_key == 'season'
  816. all_data_array[all_data_array_index][data_hash_key] = Seasons[data_hash_value.to_i]
  817. end
  818. if (data_hash_key == 'sp') and (all_data_array[all_data_array_index]['sp'] != 0 or all_data_array[all_data_array_index]['sp'] != "невідомо")
  819. if data_hash['spc'] == "1"
  820. all_data_array[all_data_array_index][data_hash_key] = all_data_array[all_data_array_index][data_hash_key].ceil.to_s + " грн."
  821. elsif data_hash['spc'] == "2"
  822. all_data_array[all_data_array_index][data_hash_key] = (all_data_array[all_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " $"
  823. elsif data_hash['spc'] == "3"
  824. all_data_array[all_data_array_index][data_hash_key] = (all_data_array[all_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " &euro;"
  825. elsif data_hash['spc'] == "4"
  826. all_data_array[all_data_array_index][data_hash_key] = (all_data_array[all_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " PLN"
  827. end
  828. end
  829. if (data_hash_key == 'sp') and (all_data_array[all_data_array_index]['sp'] == 0 or all_data_array[all_data_array_index]['sp'] == "невідомо")
  830. all_data_array[all_data_array_index][data_hash_key] = "невідомо"
  831. end
  832. if data_hash_key == 'productiondate'
  833. if data_hash_value == nil
  834. all_data_array[all_data_array_index][data_hash_key] = ""
  835. else
  836. production_date = all_data_array[all_data_array_index][data_hash_key].scan(/\d{2}(\d{2})\s+(\d{2})/).flatten
  837. all_data_array[all_data_array_index][data_hash_key] = production_date[1].to_s + production_date[0].to_s
  838. end
  839. end
  840. if data_hash_key == 'actualdate'
  841. data_date = all_data_array[all_data_array_index][data_hash_key].scan(/(\d+)[-|\s+]/).flatten
  842. all_data_array[all_data_array_index][data_hash_key] = data_date[2].to_s + "/" + data_date[1].to_s + "/" + data_date[0].to_s
  843. end
  844. if (data_hash_key == 'bp' or data_hash_key == 'bpvat' or data_hash_key == 'rpvat' or data_hash_key == 'rppe' or data_hash_key == 'rp') and (all_data_array[all_data_array_index]['sp'] != 0 or all_data_array[all_data_array_index]['sp'] != "невідомо")
  845. all_data_array[all_data_array_index][data_hash_key ] = all_data_array[all_data_array_index][data_hash_key ].ceil.to_s + " грн."
  846. end
  847. if (data_hash_key == 'bp' or data_hash_key == 'bpvat' or data_hash_key == 'rpvat' or data_hash_key == 'rppe' or data_hash_key == 'rp') and (all_data_array[all_data_array_index]['sp'] == 0 or all_data_array[all_data_array_index]['sp'] == "невідомо")
  848. all_data_array[all_data_array_index][data_hash_key] = "невідомо"
  849. end
  850. end
  851. end
  852. select_data = {}
  853. rows_array = []
  854. all_data_array.each do |value_hash|
  855. rows_array.push({"id" => value_hash["id"], "cell" => value_hash})
  856. end
  857. select_data["page"] = page_number
  858. select_data["total"] = rows_count.first
  859. select_data["rows"] = rows_array
  860. select_data["post"] = []
  861. return (JSON.pretty_generate(select_data))
  862. end
  863. get '/models' do
  864. @families = {}
  865. if params[:tyre_brand] == nil
  866. tyre_brands = $tyre_brand_name
  867. elsif params[:tyre_brand].empty?
  868. tyre_brands = $tyre_brand_name
  869. else
  870. tyre_brands = params[:tyre_brand]
  871. end
  872. tyre_brands.each do |tyre_brand|
  873. @families[tyre_brand] = $db.execute("SELECT family,brand FROM price WHERE brand=?", tyre_brand).flatten
  874. end
  875. @bind_hash = {}
  876. select_family = "SELECT family FROM price WHERE "
  877. i=0
  878. tyre_brands.each do |tyre_brand|
  879. if i == 0
  880. select_family = filter_select(select_family, tyre_brand, Tyre_brand, "brand = :brand" + i.to_s, "brand" + i.to_s)
  881. else
  882. select_family = filter_select(select_family, tyre_brand, Tyre_brand, " or brand = :brand" + i.to_s, "brand" + i.to_s)
  883. end
  884. i += 1
  885. end
  886. select_family = select_string + " ) "
  887. families_brands = $db.execute(select_family, @bind_hash)
  888. erb :models
  889. end
  890. get '/login_form' do
  891. if params[:error] == "true"
  892. @message_incorect = true
  893. else
  894. @message_incorect = false
  895. end
  896. erb :login_form
  897. end
  898. post '/login' do
  899. if params['username'] == settings.username && params['password'] == settings.password
  900. response.set_cookie(settings.username,settings.token)
  901. redirect '/'
  902. else
  903. redirect '/login_form?error=true'
  904. end
  905. end
  906. get('/logout'){ response.set_cookie(settings.username, false) ; redirect '/' }
  907. post '/excel_file' do
  908. @bind_hash = {}
  909. select_params = JSON.parse(params[:excel_button_all])
  910. advanced_excel_print = params[:advanced_excel_print]
  911. select_row_id_array = params[:excel_button_selected].gsub(/row/,"").split(',')
  912. if select_row_id_array == []
  913. checked_brands = []
  914. checked_ids = []
  915. select_sizes = []
  916. select_brands = []
  917. select_families = []
  918. select_diameters = []
  919. select_suppliers = []
  920. select_seasons = []
  921. select_remain = ''
  922. select_date = ''
  923. select_params.each do |hash|
  924. if hash["name"] == "checked_brand[]"
  925. checked_brands.push(hash["value"])
  926. end
  927. if hash["name"] == "checked_id[]"
  928. checked_ids.push(hash["value"])
  929. end
  930. if hash["name"] == "brand[]"
  931. select_brands.push(hash["value"])
  932. end
  933. if hash["name"] == "size[]"
  934. select_sizes.push(hash["value"])
  935. end
  936. if hash["name"] == "family[]"
  937. select_families.push(hash["value"])
  938. end
  939. if hash["name"] == "diameter[]"
  940. select_diameters.push(hash["value"])
  941. end
  942. if hash["name"] == "supplier[]"
  943. select_suppliers.push(hash["value"])
  944. end
  945. if hash["name"] == "season[]"
  946. select_seasons.push(hash["value"])
  947. end
  948. if hash["name"] == "remain"
  949. select_remian = hash["value"]
  950. end
  951. if hash["name"] == "date"
  952. select_date = hash["value"]
  953. end
  954. end
  955. select_string = "select * from price where"
  956. if (checked_brands.empty? == false && admin?) || (checked_brands.empty? && admin? == false)
  957. if select_sizes.empty? == false
  958. select_string = select_string + " ( "
  959. i=0
  960. select_sizes.each do |tyre_sizes_check|
  961. if i == 0
  962. select_string = filter_select(select_string, tyre_sizes_check, $tyre_size, "sectionsize = :size" + i.to_s, "size" + i.to_s)
  963. else
  964. select_string = filter_select(select_string, tyre_sizes_check, $tyre_size, " or sectionsize = :size" + i.to_s, "size" + i.to_s)
  965. end
  966. i += 1
  967. end
  968. select_string = select_string + " ) "
  969. if select_families.empty? == false
  970. select_string = select_string + " and ( "
  971. i=0
  972. select_families.each do |tyre_families_check|
  973. if i == 0
  974. select_string = filter_select(select_string, tyre_families_check, $tyre_family_name, "family = :family" + i.to_s, "family" + i.to_s)
  975. else
  976. select_string = filter_select(select_string, tyre_families_check, $tyre_family_name, " or family = :family" + i.to_s, "family" + i.to_s)
  977. end
  978. i += 1
  979. end
  980. select_string = select_string + " ) "
  981. end
  982. if select_diameters.empty? == false
  983. select_string = select_string + " and ( "
  984. i=0
  985. select_diameters.each do |tyre_diameters_check|
  986. if i == 0
  987. select_string = filter_select(select_string, tyre_diameters_check, $tyre_diameter, "diameterc = :diameterc" + i.to_s, "diameterc" + i.to_s)
  988. else
  989. select_string = filter_select(select_string, tyre_diameters_check, $tyre_diameter, " or diameterc = :diameterc" + i.to_s, "diameterc" + i.to_s)
  990. end
  991. i += 1
  992. end
  993. select_string = select_string + " ) "
  994. end
  995. if select_suppliers.empty? == false
  996. select_string = select_string + " and ( "
  997. i=0
  998. select_suppliers.each do |tyre_suppliers_check|
  999. if i == 0
  1000. select_string = filter_select(select_string, tyre_suppliers_check, $tyre_supplier, "supplier = :supplier" + i.to_s, "supplier" + i.to_s)
  1001. else
  1002. select_string = filter_select(select_string, tyre_suppliers_check, $tyre_supplier, " or supplier = :supplier" + i.to_s, "supplier" + i.to_s)
  1003. end
  1004. i += 1
  1005. end
  1006. select_string = select_string + " ) "
  1007. end
  1008. if select_seasons.empty? == false
  1009. select_string = select_string + " and ( "
  1010. i=0
  1011. select_seasons.each do |tyre_seasons_check|
  1012. if i == 0
  1013. select_string = filter_select(select_string, tyre_seasons_check, $tyre_season, "season = :season" + i.to_s, "season" + i.to_s)
  1014. else
  1015. select_string = filter_select(select_string, tyre_seasons_check, $tyre_season, " or season = :season" + i.to_s, "season" + i.to_s)
  1016. end
  1017. i += 1
  1018. end
  1019. select_string = filter_select(select_string, "0", $tyre_season, " or season = :season" + i.to_s, "season" + i.to_s)
  1020. select_string = select_string + " ) "
  1021. end
  1022. if select_remain.empty? == false
  1023. select_string = select_string + " and (remain >= :remain or remain = 0) "
  1024. @bind_hash["remain".to_sym] = select_remain
  1025. end
  1026. if select_date.empty? == false
  1027. select_string = select_string + " and (actualdate >= :date) "
  1028. date = select_date.scan(/(\d+)\/(\d+)\/(\d+)/).flatten
  1029. @bind_hash["date".to_sym] = Time.gm(date[2],date[1],date[0]).strftime("%Y-%m-%d %H:%M:%S")
  1030. end
  1031. if admin?
  1032. select_string = select_string + " and ( "
  1033. i = 0
  1034. checked_brands.each do |checked_brand|
  1035. if i == 0
  1036. select_string = filter_select(select_string, checked_brand, $tyre_brand_name, "brand = :brand" + i.to_s, "brand" + i.to_s)
  1037. else
  1038. select_string = filter_select(select_string, checked_brand, $tyre_brand_name, " or brand = :brand" + i.to_s, "brand" + i.to_s)
  1039. end
  1040. i += 1
  1041. end
  1042. select_string = select_string + " ) "
  1043. else
  1044. if select_brands.empty? == false
  1045. select_string = select_string + " and ( "
  1046. i = 0
  1047. select_brands.each do |select_brand|
  1048. if i == 0
  1049. select_string = filter_select(select_string, select_brand, $tyre_brand_name, "brand = :brand" + i.to_s, "brand" + i.to_s)
  1050. else
  1051. select_string = filter_select(select_string, select_brand, $tyre_brand_name, " or brand = :brand" + i.to_s, "brand" + i.to_s)
  1052. end
  1053. i += 1
  1054. end
  1055. select_string = select_string + " ) "
  1056. end
  1057. end
  1058. end
  1059. if checked_ids.empty? == false
  1060. if checked_brands.empty? == false
  1061. select_string = select_string + " or ( "
  1062. else
  1063. select_string = select_string + " ( "
  1064. end
  1065. i = 0
  1066. checked_ids.each do |checked_id|
  1067. if i == 0
  1068. @bind_hash[("id" + i.to_s).to_sym] = checked_id
  1069. select_string = select_string + "id = :id" + i.to_s
  1070. else
  1071. @bind_hash[("id" + i.to_s).to_sym] = checked_id
  1072. select_string = select_string + " or id = :id" + i.to_s
  1073. end
  1074. i += 1
  1075. end
  1076. select_string = select_string + " ) "
  1077. end
  1078. end
  1079. else
  1080. select_string = "select * from price where ("
  1081. i = 0
  1082. select_row_id_array.each do |row_id|
  1083. if i == 0
  1084. @bind_hash[("id" + i.to_s).to_sym] = row_id.to_i
  1085. select_string = select_string + "id = :id" + i.to_s
  1086. else
  1087. @bind_hash[("id" + i.to_s).to_sym] = row_id.to_i
  1088. select_string = select_string + " or id = :id" + i.to_s
  1089. end
  1090. i += 1
  1091. end
  1092. select_string = select_string + " ) "
  1093. end
  1094. all_data_array = []
  1095. select_all_data = $db.execute(select_string, @bind_hash)
  1096. select_all_data.each do |one_row_data|
  1097. data_hash = {}
  1098. one_row_data.each_index do |index|
  1099. data_hash[Price_table_columns[index]] = one_row_data[index]
  1100. end
  1101. all_data_array.push(data_hash)
  1102. end
  1103. all_data_array.each_index do |all_data_array_index|
  1104. data_hash = all_data_array.at(all_data_array_index)
  1105. data_hash.each_pair do |data_hash_key, data_hash_value|
  1106. if data_hash_value.class == Float
  1107. all_data_array[all_data_array_index][data_hash_key] = data_hash_value.round(2)
  1108. end
  1109. if data_hash_key == 'moreflag' and data_hash_value == 1
  1110. all_data_array[all_data_array_index]['remain'] = ">" + all_data_array[all_data_array_index]['remain'].to_s
  1111. end
  1112. if data_hash_key == 'runflat'
  1113. if data_hash_value == 1
  1114. all_data_array[all_data_array_index][data_hash_key] = "Так"
  1115. else
  1116. all_data_array[all_data_array_index][data_hash_key] = "Ні"
  1117. end
  1118. end
  1119. if data_hash_key == 'season'
  1120. all_data_array[all_data_array_index][data_hash_key] = Seasons[data_hash_value.to_i]
  1121. end
  1122. if (data_hash_key == 'sp') and (all_data_array[all_data_array_index]['sp'] != 0 or all_data_array[all_data_array_index]['sp'] != "невідомо")
  1123. if data_hash['spc'] == "1"
  1124. all_data_array[all_data_array_index][data_hash_key] = all_data_array[all_data_array_index][data_hash_key].ceil.to_s + " грн."
  1125. elsif data_hash['spc'] == "2"
  1126. all_data_array[all_data_array_index][data_hash_key] = (all_data_array[all_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " $"
  1127. elsif data_hash['spc'] == "3"
  1128. all_data_array[all_data_array_index][data_hash_key] = (all_data_array[all_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " &euro;"
  1129. elsif data_hash['spc'] == "4"
  1130. all_data_array[all_data_array_index][data_hash_key] = (all_data_array[all_data_array_index][data_hash_key] + 0.0499999).round(1).to_s + " PLN"
  1131. end
  1132. end
  1133. if (data_hash_key == 'sp') and (all_data_array[all_data_array_index]['sp'] == 0 or all_data_array[all_data_array_index]['sp'] == "невідомо")
  1134. all_data_array[all_data_array_index][data_hash_key] = "невідомо"
  1135. end
  1136. if data_hash_key == 'productiondate'
  1137. if data_hash_value == nil
  1138. all_data_array[all_data_array_index][data_hash_key] = ""
  1139. else
  1140. production_date = all_data_array[all_data_array_index][data_hash_key].scan(/\d{2}(\d{2})\s+(\d{2})/).flatten
  1141. all_data_array[all_data_array_index][data_hash_key] = production_date[1].to_s + production_date[0].to_s
  1142. end
  1143. end
  1144. if data_hash_key == 'actualdate'
  1145. data_date = all_data_array[all_data_array_index][data_hash_key].scan(/(\d+)[-|\s+]/).flatten
  1146. all_data_array[all_data_array_index][data_hash_key] = data_date[2].to_s + "/" + data_date[1].to_s + "/" + data_date[0].to_s
  1147. end
  1148. if (data_hash_key == 'bp' or data_hash_key == 'bpvat' or data_hash_key == 'rpvat' or data_hash_key == 'rppe' or data_hash_key == 'rp') and (all_data_array[all_data_array_index]['sp'] != 0 or all_data_array[all_data_array_index]['sp'] != "невідомо")
  1149. all_data_array[all_data_array_index

Large files files are truncated, but you can click here to view the full file