smartkiosk-server 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (254) hide show
  1. data/.gitignore +32 -0
  2. data/.rspec +1 -0
  3. data/Capfile +4 -0
  4. data/Gemfile +71 -0
  5. data/Gemfile.lock +439 -0
  6. data/Guardfile +25 -0
  7. data/LICENSE +287 -0
  8. data/README.md +13 -0
  9. data/Rakefile +5 -0
  10. data/app/acquirers/cards_mkb_acquirer.rb +87 -0
  11. data/app/acquirers/cash_acquirer.rb +22 -0
  12. data/app/admin/agents.rb +108 -0
  13. data/app/admin/collections.rb +62 -0
  14. data/app/admin/commissions.rb +87 -0
  15. data/app/admin/gateways.rb +183 -0
  16. data/app/admin/limits.rb +82 -0
  17. data/app/admin/payments.rb +246 -0
  18. data/app/admin/provider_groups.rb +60 -0
  19. data/app/admin/provider_profiles.rb +11 -0
  20. data/app/admin/provider_receipt_templates.rb +82 -0
  21. data/app/admin/providers.rb +201 -0
  22. data/app/admin/rebates.rb +83 -0
  23. data/app/admin/report_results.rb +66 -0
  24. data/app/admin/report_templates.rb +209 -0
  25. data/app/admin/reports.rb +158 -0
  26. data/app/admin/revisions.rb +120 -0
  27. data/app/admin/system_receipt_templates.rb +76 -0
  28. data/app/admin/terminal_builds.rb +39 -0
  29. data/app/admin/terminal_profiles.rb +123 -0
  30. data/app/admin/terminals.rb +388 -0
  31. data/app/admin/users.rb +117 -0
  32. data/app/admin/versions.rb +131 -0
  33. data/app/admin/welcome.rb +64 -0
  34. data/app/assets/fonts/PT_Sans-Web-Bold.eot +0 -0
  35. data/app/assets/fonts/PT_Sans-Web-Bold.svg +6386 -0
  36. data/app/assets/fonts/PT_Sans-Web-Bold.ttf +0 -0
  37. data/app/assets/fonts/PT_Sans-Web-Bold.woff +0 -0
  38. data/app/assets/fonts/PT_Sans-Web-Regular.eot +0 -0
  39. data/app/assets/fonts/PT_Sans-Web-Regular.svg +5650 -0
  40. data/app/assets/fonts/PT_Sans-Web-Regular.ttf +0 -0
  41. data/app/assets/fonts/PT_Sans-Web-Regular.woff +0 -0
  42. data/app/assets/images/chosen-sprite.png +0 -0
  43. data/app/assets/javascripts/active_admin/chosen.js.erb.coffee +12 -0
  44. data/app/assets/javascripts/active_admin/qtip.js.coffee +22 -0
  45. data/app/assets/javascripts/active_admin/sortable_forms.js.coffee +14 -0
  46. data/app/assets/javascripts/active_admin.js.coffee +9 -0
  47. data/app/assets/javascripts/application.js +15 -0
  48. data/app/assets/javascripts/report_templates.js.coffee +10 -0
  49. data/app/assets/javascripts/terminals.js.coffee +12 -0
  50. data/app/assets/stylesheets/active_admin/PIE.css.scss +18 -0
  51. data/app/assets/stylesheets/active_admin/filter_numeric_range.css.scss +14 -0
  52. data/app/assets/stylesheets/active_admin/selectable_check_boxes.css.scss +12 -0
  53. data/app/assets/stylesheets/active_admin/sortable_forms.css.scss +11 -0
  54. data/app/assets/stylesheets/active_admin.css.scss +705 -0
  55. data/app/assets/stylesheets/fonts.css.scss +14 -0
  56. data/app/assets/stylesheets/provider_groups.css.scss +12 -0
  57. data/app/assets/stylesheets/provider_receipt_templates.css.scss +12 -0
  58. data/app/assets/stylesheets/terminals.css.scss +9 -0
  59. data/app/controllers/application_controller.rb +12 -0
  60. data/app/controllers/collections_controller.rb +10 -0
  61. data/app/controllers/payments_controller.rb +73 -0
  62. data/app/controllers/system_receipt_templates_controller.rb +10 -0
  63. data/app/controllers/terminal_builds_controller.rb +5 -0
  64. data/app/controllers/terminal_orders_controller.rb +25 -0
  65. data/app/controllers/terminal_pings_controller.rb +45 -0
  66. data/app/controllers/welcome_controller.rb +5 -0
  67. data/app/helpers/active_admin/report_templates_helper.rb +56 -0
  68. data/app/helpers/active_admin/views_helper.rb +20 -0
  69. data/app/helpers/application_helper.rb +2 -0
  70. data/app/mailers/.gitkeep +0 -0
  71. data/app/models/.gitkeep +0 -0
  72. data/app/models/ability.rb +30 -0
  73. data/app/models/agent.rb +24 -0
  74. data/app/models/collection.rb +65 -0
  75. data/app/models/commission.rb +77 -0
  76. data/app/models/commission_section.rb +75 -0
  77. data/app/models/gateway.rb +99 -0
  78. data/app/models/gateway_attachment.rb +9 -0
  79. data/app/models/gateway_setting.rb +7 -0
  80. data/app/models/gateway_switch.rb +7 -0
  81. data/app/models/limit.rb +77 -0
  82. data/app/models/limit_section.rb +58 -0
  83. data/app/models/payment.rb +322 -0
  84. data/app/models/provider.rb +59 -0
  85. data/app/models/provider_field.rb +7 -0
  86. data/app/models/provider_gateway.rb +47 -0
  87. data/app/models/provider_group.rb +45 -0
  88. data/app/models/provider_profile.rb +9 -0
  89. data/app/models/provider_rebate.rb +69 -0
  90. data/app/models/provider_receipt_template.rb +85 -0
  91. data/app/models/rebate.rb +84 -0
  92. data/app/models/report.rb +71 -0
  93. data/app/models/report_result.rb +11 -0
  94. data/app/models/report_template.rb +56 -0
  95. data/app/models/revision.rb +104 -0
  96. data/app/models/role.rb +48 -0
  97. data/app/models/system_receipt_template.rb +13 -0
  98. data/app/models/terminal.rb +126 -0
  99. data/app/models/terminal_build.rb +62 -0
  100. data/app/models/terminal_order.rb +42 -0
  101. data/app/models/terminal_ping.rb +120 -0
  102. data/app/models/terminal_profile.rb +51 -0
  103. data/app/models/terminal_profile_promotion.rb +18 -0
  104. data/app/models/terminal_profile_provider.rb +11 -0
  105. data/app/models/terminal_profile_provider_group.rb +19 -0
  106. data/app/models/user.rb +38 -0
  107. data/app/models/user_role.rb +45 -0
  108. data/app/reports/agents_report.rb +81 -0
  109. data/app/reports/collections_report.rb +151 -0
  110. data/app/reports/payments_report.rb +189 -0
  111. data/app/reports/terminals_report.rb +123 -0
  112. data/app/uploaders/file_uploader.rb +53 -0
  113. data/app/uploaders/icon_uploader.rb +15 -0
  114. data/app/uploaders/zip_uploader.rb +53 -0
  115. data/app/views/admin/gateways/providers.html.arb +10 -0
  116. data/app/views/admin/revisions/payments.html.arb +28 -0
  117. data/app/views/admin/terminal_profiles/_tree.html.haml +35 -0
  118. data/app/views/admin/terminal_profiles/sort.html.haml +21 -0
  119. data/app/views/admin/terminals/pings.html.arb +55 -0
  120. data/app/views/admin/terminals/upgrade_build.html.arb +30 -0
  121. data/app/views/layouts/application.html.erb +1 -0
  122. data/app/views/welcome/index.html.erb +25 -0
  123. data/app/workers/pay_worker.rb +11 -0
  124. data/app/workers/report_worker.rb +7 -0
  125. data/app/workers/revise_worker.rb +7 -0
  126. data/config/acquiring.yml +13 -0
  127. data/config/application.rb +75 -0
  128. data/config/boot.rb +6 -0
  129. data/config/database.yml +17 -0
  130. data/config/deploy/roundlake-passenger.rb +3 -0
  131. data/config/deploy/roundlake-trinidad.rb +3 -0
  132. data/config/deploy.rb +37 -0
  133. data/config/environment.rb +5 -0
  134. data/config/environments/development.rb +39 -0
  135. data/config/environments/production.rb +67 -0
  136. data/config/environments/test.rb +37 -0
  137. data/config/initializers/active_admin.rb +31 -0
  138. data/config/initializers/backtrace_silencers.rb +7 -0
  139. data/config/initializers/devise.rb +232 -0
  140. data/config/initializers/inflections.rb +15 -0
  141. data/config/initializers/mime_types.rb +5 -0
  142. data/config/initializers/money.rb +6 -0
  143. data/config/initializers/redis.rb +4 -0
  144. data/config/initializers/secret_token.rb +7 -0
  145. data/config/initializers/session_store.rb +8 -0
  146. data/config/initializers/setup_rack.rb +1 -0
  147. data/config/initializers/wrap_parameters.rb +14 -0
  148. data/config/locales/active_admin.ru.yml +71 -0
  149. data/config/locales/activerecord.ru.yml +645 -0
  150. data/config/locales/devise.ru.yml +57 -0
  151. data/config/locales/ru.yml +33 -0
  152. data/config/locales/smartkiosk.gateways.ru.yml +241 -0
  153. data/config/locales/smartkiosk.hardware.ru.yml +38 -0
  154. data/config/locales/smartkiosk.reports.ru.yml +56 -0
  155. data/config/locales/smartkiosk.ru.yml +163 -0
  156. data/config/nginx.conf +9 -0
  157. data/config/redis.yml +9 -0
  158. data/config/routes.rb +48 -0
  159. data/config/schedule.rb +13 -0
  160. data/config/sidekiq.yml +5 -0
  161. data/config/trinidad.yml +6 -0
  162. data/config.ru +4 -0
  163. data/db/migrate/20120825083035_create_terminals.rb +47 -0
  164. data/db/migrate/20120825083305_create_payments.rb +51 -0
  165. data/db/migrate/20120827062618_devise_create_users.rb +50 -0
  166. data/db/migrate/20120827132621_create_admin_notes.rb +17 -0
  167. data/db/migrate/20120827132622_move_admin_notes_to_comments.rb +25 -0
  168. data/db/migrate/20120903193346_create_providers.rb +24 -0
  169. data/db/migrate/20120922075345_create_collections.rb +26 -0
  170. data/db/migrate/20120922080440_create_agents.rb +29 -0
  171. data/db/migrate/20120922081104_create_commissions.rb +10 -0
  172. data/db/migrate/20120927035841_create_gateways.rb +13 -0
  173. data/db/migrate/20120927040626_create_provider_gateways.rb +13 -0
  174. data/db/migrate/20120927091849_create_limits.rb +10 -0
  175. data/db/migrate/20120927164647_create_roles.rb +14 -0
  176. data/db/migrate/20121003131522_create_report_templates.rb +18 -0
  177. data/db/migrate/20121007135652_create_reports.rb +13 -0
  178. data/db/migrate/20121008073905_create_report_results.rb +10 -0
  179. data/db/migrate/20121017114538_create_gateway_settings.rb +12 -0
  180. data/db/migrate/20121017114556_create_gateway_attachments.rb +10 -0
  181. data/db/migrate/20121019132606_create_revisions.rb +19 -0
  182. data/db/migrate/20121023073501_create_gateway_switches.rb +10 -0
  183. data/db/migrate/20121112142743_create_terminal_orders.rb +13 -0
  184. data/db/migrate/20121118191553_create_versions.rb +19 -0
  185. data/db/migrate/20121125093414_create_system_receipt_templates.rb +9 -0
  186. data/db/migrate/20121127192257_create_terminal_builds.rb +10 -0
  187. data/db/migrate/20121212053441_create_rebates.rb +15 -0
  188. data/db/migrate/20121216143855_create_provider_receipt_templates.rb +9 -0
  189. data/db/migrate/20130101091100_create_terminal_profiles.rb +13 -0
  190. data/db/migrate/20130101091734_create_provider_rebates.rb +21 -0
  191. data/db/migrate/20130102164447_create_limit_sections.rb +14 -0
  192. data/db/migrate/20130102164503_create_commission_sections.rb +17 -0
  193. data/db/migrate/20130102171743_create_provider_profiles.rb +10 -0
  194. data/db/migrate/20130103152507_create_provider_fields.rb +17 -0
  195. data/db/migrate/20130103154526_create_provider_groups.rb +10 -0
  196. data/db/migrate/20130104063628_create_terminal_profile_providers.rb +11 -0
  197. data/db/migrate/20130104090957_create_terminal_profile_provider_groups.rb +11 -0
  198. data/db/migrate/20130108091644_create_terminal_profile_promotions.rb +10 -0
  199. data/db/schema.rb +489 -0
  200. data/db/seeds/receipt_templates/payment.txt +19 -0
  201. data/db/seeds/receipt_templates/system/balance.txt +13 -0
  202. data/db/seeds/receipt_templates/system/collection.txt +17 -0
  203. data/db/seeds/receipt_templates/system/touchtest.txt +15 -0
  204. data/db/seeds.rb +18 -0
  205. data/doc/README_FOR_APP +2 -0
  206. data/init.rb +1 -0
  207. data/lib/active_admin/cancan_integration.rb +81 -0
  208. data/lib/active_admin/form_builder_fix.rb +76 -0
  209. data/lib/active_admin/inputs/filter_date_range_input_fix.rb +24 -0
  210. data/lib/active_admin/inputs/filter_multiple_select_input.rb +18 -0
  211. data/lib/active_admin/inputs/filter_numeric_range_input.rb +31 -0
  212. data/lib/active_admin/resource_controller_fix.rb +23 -0
  213. data/lib/active_admin/views/pages/base_fix.rb +13 -0
  214. data/lib/assets/.gitkeep +0 -0
  215. data/lib/blueprints.rb +79 -0
  216. data/lib/date_expander.rb +38 -0
  217. data/lib/dav4rack/build_resource.rb +59 -0
  218. data/lib/formtastic/inputs/selectable_check_boxes.rb +39 -0
  219. data/lib/paper_trail/version_fix.rb +3 -0
  220. data/lib/report_builder.rb +88 -0
  221. data/lib/seeder.rb +103 -0
  222. data/lib/smartkiosk/server/version.rb +5 -0
  223. data/lib/smartkiosk/server.rb +35 -0
  224. data/lib/string_file.rb +12 -0
  225. data/lib/tasks/.gitkeep +0 -0
  226. data/lib/tasks/db.rake +27 -0
  227. data/lib/tasks/dump.rake +53 -0
  228. data/lib/tasks/matrioshka.rake +16 -0
  229. data/log/.gitkeep +0 -0
  230. data/log/gateways/.gitkeep +0 -0
  231. data/public/404.html +26 -0
  232. data/public/422.html +26 -0
  233. data/public/500.html +25 -0
  234. data/public/robots.txt +5 -0
  235. data/script/rails +6 -0
  236. data/smartkiosk-server.gemspec +23 -0
  237. data/spec/controllers/collections_controller_spec.rb +32 -0
  238. data/spec/controllers/payments_controller_spec.rb +123 -0
  239. data/spec/controllers/system_receipt_templates_controller_spec.rb +5 -0
  240. data/spec/controllers/terminal_builds_controller_spec.rb +5 -0
  241. data/spec/controllers/terminal_orders_controller_spec.rb +32 -0
  242. data/spec/controllers/terminal_pings_controller_spec.rb +31 -0
  243. data/spec/spec_helper.rb +66 -0
  244. data/vendor/assets/javascripts/.gitkeep +0 -0
  245. data/vendor/assets/javascripts/chosen.jquery.js +1026 -0
  246. data/vendor/assets/javascripts/datepicker-ru.jquery.js +21 -0
  247. data/vendor/assets/javascripts/nestedSortable.jquery.js +429 -0
  248. data/vendor/assets/javascripts/qtip.jquery.js +3403 -0
  249. data/vendor/assets/stylesheets/.gitkeep +0 -0
  250. data/vendor/assets/stylesheets/chosen.jquery.css +397 -0
  251. data/vendor/assets/stylesheets/modules/PIE.htc +96 -0
  252. data/vendor/assets/stylesheets/qtip.jquery.css.scss +604 -0
  253. data/vendor/plugins/.gitkeep +0 -0
  254. metadata +333 -0
@@ -0,0 +1,45 @@
1
+ class ProviderGroup < ActiveRecord::Base
2
+ include Redis::Objects
3
+
4
+ mount_uploader :icon, IconUploader
5
+
6
+ value :timestamp, :global => true, :marshal => true
7
+
8
+ after_save do
9
+ self.class.timestamp = updated_at
10
+ end
11
+
12
+ after_destroy do
13
+ self.class.timestamp = DateTime.now
14
+ end
15
+
16
+ belongs_to :provider_group
17
+ has_many :provider_groups, :order => :title
18
+ has_many :providers, :order => :priority
19
+ has_many :terminal_profile_provider_groups, :dependent => :destroy
20
+
21
+ accepts_nested_attributes_for :providers
22
+
23
+ validates :title, :presence => true
24
+
25
+ def self.tree(exclude=nil)
26
+ result = {}
27
+
28
+ inject = lambda{|result, exclude, entry, level|
29
+ unless entry == exclude
30
+ result["#{'--'*level}#{' ' if level > 0}#{entry.title}"] = entry.id
31
+
32
+ entry.provider_groups.each do |x|
33
+ inject.call(result, exclude, x, level+1)
34
+ end
35
+ end
36
+ }
37
+
38
+ ProviderGroup.where(:provider_group_id => nil).order(:title).
39
+ select{|x| x.provider_group.blank?}.each do |x|
40
+ inject.call(result, exclude, x, 0)
41
+ end
42
+
43
+ result
44
+ end
45
+ end
@@ -0,0 +1,9 @@
1
+ class ProviderProfile < ActiveRecord::Base
2
+ include Redis::Objects::RMap
3
+
4
+ has_rmap({:id => lambda{|x| x.to_s}}, :title)
5
+
6
+ has_many :providers
7
+ has_many :limits
8
+ has_many :commissions
9
+ end
@@ -0,0 +1,69 @@
1
+ class ProviderRebate < ActiveRecord::Base
2
+ belongs_to :rebate, :inverse_of => :provider_rebates
3
+ belongs_to :provider
4
+
5
+ scope :by_provider, lambda { |x|
6
+ if x.blank?
7
+ where(:provider_id => nil)
8
+ else
9
+ where("provider_id IS NULL OR provider_id = ?", x.id)
10
+ end
11
+ }
12
+ scope :by_commission, lambda { |x|
13
+ where("requires_commission IS NULL OR requires_commission = ?", !x.blank?)
14
+ }
15
+ scope :by_payment_type, lambda { |x|
16
+ where("payment_type IS NULL OR payment_type = ?", x)
17
+ }
18
+
19
+ validates :rebate, :presence => true
20
+ validates :provider, :presence => true
21
+ validates :min, :presence => true
22
+ validates :max, :presence => true
23
+ validates :min_percent_amount, :presence => true
24
+
25
+ validate do
26
+ return if min.blank? || max.blank?
27
+
28
+ if min > max
29
+ errors.add(:min, :too_large)
30
+ return
31
+ end
32
+
33
+ # Conditions intersections
34
+ neighbors = rebate.provider_rebates.select{|x| x != self}
35
+
36
+ if neighbors.select{|x| x.requires_commission.nil? != requires_commission.nil?}.count > 0
37
+ errors[:base] << I18n.t('activerecord.errors.models.rebate.requires_commission_intersects')
38
+ end
39
+
40
+ if neighbors.select{|x| x.payment_type.nil? != payment_type.nil?}.count > 0
41
+ errors[:base] << I18n.t('activerecord.errors.models.rebate.payment_type_intersects')
42
+ end
43
+
44
+ # Ranges intersections
45
+ neighbors.select!{|x| x.provider_id == provider_id}
46
+
47
+ neighbors.select! do |x|
48
+ x.min.between?(min, max) ||
49
+ x.max.between?(min, max) ||
50
+ min.between?(x.min, x.max) ||
51
+ max.between?(x.min, x.max)
52
+ end
53
+
54
+ if neighbors.length > 0
55
+ errors[:base] << I18n.t('activerecord.errors.models.rebate.conditions_intersect')
56
+ end
57
+ end
58
+
59
+ def weight
60
+ return 2 unless provider_id.blank?
61
+ return 1
62
+ end
63
+
64
+ def fee(amount)
65
+ percent_amount = amount*(self.percent_fee||0)/100
66
+ percent_amount = [percent, min_percent_amount].min
67
+ (self.static_fee||0) + percent_amount
68
+ end
69
+ end
@@ -0,0 +1,85 @@
1
+ class ProviderReceiptTemplate < ActiveRecord::Base
2
+ has_many :providers, :conditions => "provider_receipt_template_id IS NOT NULL"
3
+ accepts_nested_attributes_for :providers
4
+
5
+ def self.base
6
+ ProviderReceiptTemplate.where(:system => true).first
7
+ end
8
+
9
+ def self.sample
10
+ ProviderReceiptTemplate.new :template => base.template
11
+ end
12
+
13
+ def self.for(payment)
14
+ payment.provider.provider_receipt_template || base
15
+ end
16
+
17
+ def self.entities
18
+ {
19
+ :terminal => terminal_fields,
20
+ :payment => payment_fields,
21
+ :provider => provider_fields
22
+ }
23
+ end
24
+
25
+ def self.payment_fields
26
+ [
27
+ :id,
28
+ :created_at,
29
+ :paid_at,
30
+ :foreign_id,
31
+ :account,
32
+ :fields,
33
+ :paid_amount,
34
+ :enrolled_amount,
35
+ :commission_amount
36
+ ]
37
+ end
38
+
39
+ def self.terminal_fields
40
+ [
41
+ :keyword,
42
+ :address,
43
+ :version
44
+ ]
45
+ end
46
+
47
+ def self.provider_fields
48
+ [
49
+ :title,
50
+ :keyword,
51
+ :juristic_name,
52
+ :inn,
53
+ :support_phone
54
+ ]
55
+ end
56
+
57
+ def self.masks
58
+ %w(
59
+ payment_paid_amount
60
+ payment_enrolled_amount
61
+ payment_commission_amount
62
+ payment_paid_at
63
+ )
64
+ end
65
+
66
+ def title
67
+ providers.blank? ? "--" : providers.map{|x| x.title}.join(', ')
68
+ end
69
+
70
+ def compile(payment)
71
+ data = {}
72
+ masks = self.class.masks
73
+
74
+ self.class.entities.each do |entity, fields|
75
+ fields.each do |field|
76
+ model = (entity == :payment) ? payment : payment.send(entity)
77
+ key = "#{entity}_#{field}"
78
+
79
+ data[key] = masks.include?(key) ? "{{ #{key} }}" : model.send(field)
80
+ end
81
+ end
82
+
83
+ Liquid::Template.parse(template).render data
84
+ end
85
+ end
@@ -0,0 +1,84 @@
1
+ class Rebate < ActiveRecord::Base
2
+
3
+ has_paper_trail
4
+
5
+ #
6
+ # RELATIONS
7
+ #
8
+ belongs_to :gateway
9
+ has_many :provider_rebates, :inverse_of => :rebate
10
+ has_many :providers, :through => :provider_rebates
11
+
12
+ accepts_nested_attributes_for :provider_rebates, :allow_destroy => true
13
+
14
+ default_scope includes(:provider_rebates)
15
+
16
+ scope :actual, lambda{
17
+ where(arel_table[:start].lteq Date.today).
18
+ where(arel_table[:finish].gteq Date.today)
19
+ }
20
+
21
+ scope :active, lambda{
22
+ where(arel_table[:finish].gteq Date.today)
23
+ }
24
+
25
+ scope :outdated, lambda{
26
+ where(arel_table[:finish].lteq Date.today)
27
+ }
28
+
29
+ #
30
+ # VALIDATIONS
31
+ #
32
+ validates :gateway, :presence => true
33
+ validates :start, :presence => true
34
+ validates :finish, :presence => true
35
+ validates :period_kind, :presence => true, :if => lambda{|x| !x.period_fee.blank?}
36
+
37
+ validate do
38
+ return if gateway.blank? || start.blank? || finish.blank?
39
+
40
+ if start > finish
41
+ errors.add(:start, :too_large)
42
+ return
43
+ end
44
+
45
+ # Conditions intersections
46
+ neighbors = self.class.where(:gateway_id => gateway_id)
47
+ neighbors = neighbors.where("id != ?", id) unless id.blank?
48
+
49
+ # Ranges intersections
50
+ neighbors = neighbors.to_a
51
+
52
+ neighbors.select! do |x|
53
+ x.start.between?(start, finish) ||
54
+ x.finish.between?(start, finish) ||
55
+ start.between?(x.start, x.finish) ||
56
+ finish.between?(x.start, x.finish)
57
+ end
58
+
59
+ if neighbors.count > 0
60
+ errors[:base] << I18n.t('activerecord.errors.models.rebate.conditions_intersect')
61
+ end
62
+ end
63
+
64
+ #
65
+ # METHODS
66
+ #
67
+ def self.for(payment)
68
+ rebate = payment.gateway.rebates.actual.first
69
+ return nil if rebate.blank?
70
+
71
+ rates = rebate.provider_rebates.
72
+ by_provider(payment.provider).
73
+ by_commission(payment.commission_amount).
74
+ by_payment_type(payment.payment_type)
75
+ rates.select{|x|
76
+ x.min <= payment.paid_amount &&
77
+ x.max >= payment.paid_amount
78
+ }.sort_by(&:weight).reverse.first
79
+ end
80
+
81
+ def title
82
+ "##{id}"
83
+ end
84
+ end
@@ -0,0 +1,71 @@
1
+ class Report < ActiveRecord::Base
2
+ include Stateflow
3
+
4
+ #
5
+ # STATEFLOW
6
+ #
7
+ stateflow do
8
+ initial :new
9
+
10
+ state :new, :queue, :done, :error
11
+
12
+ event :enqueue do
13
+ transitions :from => [:error, :new, :done], :to => :queue
14
+ end
15
+
16
+ event :build do
17
+ transitions :from => :queue, :to => [:done, :error], :decide => :build?
18
+ end
19
+ end
20
+
21
+ def enqueue!
22
+ enqueue
23
+ save!
24
+ ReportWorker.perform_async(id)
25
+ end
26
+
27
+ #
28
+ # RELATIONS
29
+ #
30
+ belongs_to :report_template
31
+ belongs_to :user
32
+ has_many :report_results, :order => 'id DESC'
33
+
34
+ #
35
+ # VALIDATIONS
36
+ #
37
+ validates :report_template, :presence => true
38
+ validates :user, :presence => true
39
+ validates :start, :presence => true, :if => lambda{|x| x.report_builder.try(:requires_dates?)}
40
+ validates :finish, :presence => true, :if => lambda{|x| x.report_builder.try(:requires_dates?)}
41
+
42
+ #
43
+ # MODIFICATIONS
44
+ #
45
+ after_create :enqueue!
46
+
47
+ #
48
+ # METHODS
49
+ #
50
+ def title
51
+ "##{id} (#{report_template.title})"
52
+ end
53
+
54
+ def report_builder
55
+ return nil if report_template.blank?
56
+ @report_builder ||= report_template.report_builder(self)
57
+ end
58
+
59
+ def build?
60
+ query = report_builder.query
61
+
62
+ begin
63
+ data = ActiveRecord::Base.connection.exec_query(query).to_hash
64
+ ReportResult.create! :report_id => id, :rows => data.length, :data => data
65
+ return :done
66
+ rescue Exception => e
67
+ update_attribute :error, e.to_s
68
+ return :error
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ class ReportResult < ActiveRecord::Base
2
+ #
3
+ # RELATIONS
4
+ #
5
+ belongs_to :report
6
+
7
+ #
8
+ # MODIFICATIONS
9
+ #
10
+ serialize :data
11
+ end
@@ -0,0 +1,56 @@
1
+ class ReportTemplate < ActiveRecord::Base
2
+
3
+ attr_accessor :report_builder_instance
4
+
5
+ has_paper_trail
6
+
7
+ #
8
+ # RELATIONS
9
+ #
10
+ belongs_to :user
11
+ has_many :reports, :dependent => :destroy
12
+
13
+ #
14
+ # VALIDATIONS
15
+ #
16
+ validates :title, :presence => true
17
+
18
+ #
19
+ # MODIFCIATIONS
20
+ #
21
+ serialize :groupping
22
+ serialize :fields
23
+ serialize :calculations
24
+ serialize :conditions
25
+
26
+ #
27
+ # METHODS
28
+ #
29
+ def report_builder(report=nil)
30
+ return report_builder_instance unless report_builder_instance.nil?
31
+
32
+ report_builder_instance = ReportBuilder.constantize(kind)
33
+ report_builder_instance = report_builder_instance.new(report) if report_builder_instance
34
+
35
+ return report_builder_instance
36
+ end
37
+
38
+ def respond_to?(key, include_private=false)
39
+ return true if key.to_s.starts_with?('condition_')
40
+ super(key, include_private)
41
+ end
42
+
43
+ def method_missing(name, *args, &block)
44
+ return super unless name.to_s.starts_with?('condition_')
45
+
46
+ self.conditions ||= {}
47
+ name = name.to_s.gsub('condition_', '')
48
+
49
+ if name[-1] == '='
50
+ name = name[0, name.length-1]
51
+ self.conditions[name] = (args[0].is_a?(Array) ? args[0].select{|x| !x.blank?} : args[0])
52
+ else
53
+ self.conditions[name]
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,104 @@
1
+ require 'string_file'
2
+
3
+ class Revision < ActiveRecord::Base
4
+ include Redis::Objects
5
+ include Stateflow
6
+
7
+ #
8
+ # STATEFLOW
9
+ #
10
+ stateflow do
11
+ initial :new
12
+
13
+ state :new, :queue, :done, :error
14
+
15
+ event :enqueue do
16
+ transitions :from => [:error, :new], :to => :queue, :if => :moderated?
17
+ transitions :from => :new, :to => :new
18
+ end
19
+
20
+ event :perform do
21
+ transitions :from => :queue, :to => [:done, :error], :decide => :perform?
22
+ end
23
+ end
24
+
25
+ def enqueue!
26
+ enqueue
27
+ save!
28
+ ReviseWorker.perform_async(id)
29
+ end
30
+
31
+ #
32
+ # RELATIONS
33
+ #
34
+ belongs_to :gateway
35
+ has_many :payments, :dependent => :nullify
36
+
37
+ mount_uploader :data, FileUploader
38
+
39
+ scope :unmoderated, where(:moderated => false)
40
+ scope :error, where(:state => 'error')
41
+
42
+ #
43
+ # VALIDATIONS
44
+ #
45
+ validates :gateway, :presence => true
46
+ validates :date, :uniqueness => {:scope => :gateway_id}
47
+
48
+ #
49
+ # MODIFICATIONS
50
+ #
51
+ after_create do
52
+ # This is `after_create` cause sometimes we have to have the ID of revision at Payzilla
53
+ # Also we require the id of revision to attach payments
54
+
55
+ start = date.to_datetime.change(:hour => 0)
56
+ finish = start + 1.day - 1.second
57
+
58
+ @payments = Payment.where(:gateway_id => gateway.id, :paid_at => start..finish)
59
+
60
+ @payments.update_all(:revision_id => self.id)
61
+
62
+ sum_query = @payments.arel; sum_query.projections = []
63
+ sum_query = sum_query.project(Payment.arel_table[:paid_amount].sum.as("paid"))
64
+ sum_query = sum_query.project(Payment.arel_table[:enrolled_amount].sum.as("enrolled"))
65
+ sum_query = connection.select_all(sum_query.to_sql).first
66
+
67
+ self.payments_count = @payments.count
68
+ self.paid_sum = sum_query['paid'] || 0
69
+ self.enrolled_sum = sum_query['enrolled'] || 0
70
+ self.commission_sum = self.paid_sum - self.enrolled_sum
71
+ self.moderated = !gateway.requires_revisions_moderation
72
+
73
+ data = gateway.librarize.generate_revision(self)
74
+ self.data = StringFile.new("data.#{data[0]}", data[1])
75
+
76
+ enqueue if moderated?
77
+
78
+ save!
79
+ end
80
+
81
+ #
82
+ # METHODS
83
+ #
84
+ def title
85
+ "#{gateway.title}: #{I18n.l date}"
86
+ end
87
+
88
+ def moderate!
89
+ return if moderated?
90
+ update_attribute(:moderated, true)
91
+ enqueue!
92
+ end
93
+
94
+ def perform?
95
+ result = gateway.librarize.send_revision(self, data.read)
96
+
97
+ if result[:success]
98
+ return :done
99
+ else
100
+ self.update_attribute(:error, result[:error])
101
+ return :error
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,48 @@
1
+ class Role < ActiveRecord::Base
2
+ def self.entries
3
+ @entries ||= %w(
4
+ users versions reports report_templates
5
+ payments providers commissions limits
6
+ provider_receipt_templates terminals
7
+ agents collections terminal_profiles system_receipt_templates
8
+ gateways rebates revisions
9
+ )
10
+ end
11
+
12
+ def self.actions
13
+ @actions ||= {
14
+ 'terminals' => %w(reload reboot disable enable upgrade)
15
+ }
16
+ end
17
+
18
+ def self.entries_actions
19
+ return @result unless @result.blank?
20
+
21
+ @result = {}
22
+ entries.each do |x|
23
+ @result[x] = {}
24
+
25
+ %w(read create edit destroy).each do |a|
26
+ @result[x][a] = I18n.t("smartkiosk.role_priveleges.basic.#{a}")
27
+ end
28
+
29
+ actions[x].each do |a|
30
+ @result[x][a] = I18n.t("smartkiosk.role_priveleges.#{x}.#{a}")
31
+ end unless actions[x].blank?
32
+ end
33
+
34
+ @result
35
+ end
36
+
37
+ def title
38
+ I18n.t "activerecord.models.#{keyword.singularize}.other"
39
+ end
40
+
41
+ def actions
42
+ self.class.entries_actions[keyword]
43
+ end
44
+
45
+ def modelize
46
+ keyword.singularize.camelize.constantize
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ class SystemReceiptTemplate < ActiveRecord::Base
2
+ belongs_to :provider
3
+
4
+ validates :template, :presence => true
5
+
6
+ scope :new_from, lambda{|x|
7
+ where(arel_table[:updated_at].gteq x)
8
+ }
9
+
10
+ def title
11
+ I18n.t "smartkiosk.system_receipt_templates.#{keyword}"
12
+ end
13
+ end