billing 0.0.7e → 0.0.7f

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 372a49ba72a93955ca4508c334463629b9e73683
4
- data.tar.gz: 2e7eff4b288280bb857c4e4bb1b52d2952d7d3bd
3
+ metadata.gz: 805e2665500fc7301222f87a0fccd5d44274c9da
4
+ data.tar.gz: 15a32f01fb6c33cc0d924b2e7b3a1e3aa777e1df
5
5
  SHA512:
6
- metadata.gz: 19e435c9c58a9b8e176de78ab838909c257412e4d2f3749e3f30b3dbc61dcf0f67649ca9d768736190b8dac37fcce1316af72e3afc66c053b12043dcba59cead
7
- data.tar.gz: 17bd694ea1398b1e10d2039190fea28fba72bd50fd34641f4f7a1e7314a145e8b8a0a24d109e1310292e97ae7c4d47a4cf04b8e5e7a3b2e1b1690964ca9d996b
6
+ metadata.gz: ce7748011a09d97a8fa6f82d75223c8f78e92dd88aa326500b243d90f82e719db092f18c46bb041816c912e67f83795de90e4b712d6b3c67af1fb20a752fd784
7
+ data.tar.gz: 1393357178169a6e7edac86475c9c710d593761ca81a84173815af3f4f716ebff816c2d9afe83b9c20a972040923ee370f8c2de0645045e1ff9695df04b9bfb9
@@ -0,0 +1,128 @@
1
+ module Resque
2
+ module Plugins
3
+ module ExtfaceLonelyDevice
4
+ LOCK_TIMEOUT = 60 * 60 * 24 * 5 # 5 days
5
+
6
+ def lock_timeout
7
+ Time.now.to_i + LOCK_TIMEOUT + 1
8
+ end
9
+
10
+ def requeue_interval
11
+ self.instance_variable_get(:@requeue_interval) || 1
12
+ end
13
+
14
+ # Overwrite this method to uniquely identify which mutex should be used
15
+ # for a resque worker.
16
+ def redis_key(*args)
17
+ "extface_"
18
+ end
19
+
20
+ def can_lock_queue?(*args)
21
+ now = Time.now.to_i
22
+ key = redis_key(*args)
23
+ timeout = lock_timeout
24
+
25
+ # Per http://redis.io/commands/setnx
26
+ return true if Resque.redis.setnx(key, timeout)
27
+ return false if Resque.redis.get(key).to_i > now
28
+ return true if Resque.redis.getset(key, timeout).to_i <= now
29
+ return false
30
+ end
31
+
32
+ def unlock_queue(*args)
33
+ Resque.redis.del(redis_key(*args))
34
+ end
35
+
36
+ def reenqueue(*args)
37
+ Resque.enqueue_to(redis_key(*args), self, *args)
38
+ end
39
+
40
+ def before_perform(*args)
41
+ unless can_lock_queue?(*args)
42
+ # Sleep so the CPU's rest
43
+ sleep(requeue_interval)
44
+
45
+ # can't get the lock, so re-enqueue the task
46
+ reenqueue(*args)
47
+
48
+ # and don't perform
49
+ raise Resque::Job::DontPerform
50
+ end
51
+ end
52
+
53
+ def around_perform(*args)
54
+ begin
55
+ yield
56
+ ensure
57
+ unlock_queue(*args)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ module Billing
65
+ class IssueFiscalDoc
66
+ extend Resque::Plugins::ExtfaceLonelyDevice
67
+
68
+ def self.redis_key(bill_id)
69
+ "extface_#{Bill.find(bill_id).extface_job.device_id}"
70
+ end
71
+
72
+ def self.perform(bill_id)
73
+ bill = Bill.find(bill_id)
74
+ qname = "extface_#{bill.extface_job.device_id}"
75
+ wjs = Resque::Worker.working.find_all{ |w| w.job && w.job['queue'] == qname }
76
+
77
+ p "############################"
78
+ p "d: #{bill.extface_job.device_id} Issue Fiscal Doc ##{bill_id}, job: #{bill.extface_job_id}, wjs: #{wjs.inspect}"
79
+ p "____________________________"
80
+ p "active jobs: #{bill.extface_job.device.jobs.active.count}"
81
+
82
+
83
+ bill.extface_job.runtime do |s|
84
+ return unless bill.fiscalizable?
85
+ operator_mapping = bill.find_operator_mapping_for(s)
86
+ s.notify "Fiscal Doc Start"
87
+ s.autofix_unclosed_doc
88
+ s.open_fiscal_doc(operator_mapping.try(:mapping), operator_mapping.try(:pwd))
89
+ s.notify "Register Sale"
90
+ bill.charges.each do |charge|
91
+ neto, percent_ratio = nil, nil, nil
92
+ if modifier = charge.modifier
93
+ neto = modifier.fixed_value
94
+ percent_ratio = modifier.percent_ratio unless neto.present?
95
+ end
96
+ if charge.price.zero? #printing comments with zero charges (TODO check zero charges allowed?)
97
+ s.add_comment charge.text
98
+ else
99
+ s.add_sale(
100
+ s.class::SaleItem.new(
101
+ price: charge.price.to_f,
102
+ text1: charge.name,
103
+ text2: charge.description,
104
+ tax_group: charge.find_tax_group_mapping_for(s), #find tax group mapping by ratio , not nice
105
+ qty: charge.qty,
106
+ neto: neto,
107
+ percent_ratio: percent_ratio #TODO check format
108
+ )
109
+ )
110
+ end
111
+ end
112
+ if global_modifier_value = bill.global_modifier_value
113
+ s.notify "Register Global Modifier"
114
+ s.add_total_modifier global_modifier_value.to_f
115
+ end
116
+ s.notify "Register Payment"
117
+ bill.payments.each do |payment|
118
+ s.add_payment payment.value.to_f, payment.find_payment_type_mapping_for(s)
119
+ end
120
+ s.notify "Close Fiscal Receipt"
121
+ s.close_fiscal_doc
122
+ s.notify "Fiscal Doc End"
123
+ end
124
+ rescue Resque::TermException
125
+ reenqueue(bill_id)
126
+ end
127
+ end
128
+ end
@@ -39,6 +39,7 @@ module Billing
39
39
  self.name = "B:#{number}" if name.nil?
40
40
  end
41
41
  before_save :perform_autofin, if: :becomes_paid?
42
+ after_save :create_fiscal_job, if: :fiscalizable?
42
43
 
43
44
  validates_numericality_of :total, greater_than_or_equal_to: 0
44
45
  validates_numericality_of :balance, less_than_or_equal_to: 0
@@ -89,8 +90,9 @@ module Billing
89
90
  end
90
91
 
91
92
  def fiscalize(detailed = false)
92
- self.extface_job = origin.fiscal_device.driver.fiscalize(self, detailed) if fiscalizable? && origin.try(:fiscal_device)
93
- self.extface_job if save
93
+ #TODO create resque job
94
+ #self.extface_job = origin.fiscal_device.driver.fiscalize(self, detailed) if fiscalizable? && origin.try(:fiscal_device)
95
+ #self.extface_job if save
94
96
  end
95
97
 
96
98
  def global_modifier_value
@@ -108,6 +110,10 @@ module Billing
108
110
  # get operator, who close/pay the bill?
109
111
  #operator.operator_fiscal_driver_mapping.find_by(extface_driver_id: fiscal_driver.id) if fiscal_driver.fiscal?
110
112
  end
113
+
114
+ def fiscalizable?
115
+ self.finalized_at.present? && payments.select(&:fiscal?).any? && origin.try(:fiscal_device) && balance.zero?
116
+ end
111
117
 
112
118
  private
113
119
  def calculate_modifiers
@@ -153,17 +159,26 @@ module Billing
153
159
  end
154
160
 
155
161
  def perform_autofin
162
+ p "!!!!!!!!!!perform_autofin"
156
163
  if autofin
157
164
  self.finalized_at = Time.now
158
- if defined? Extface
159
- self.extface_job = origin.fiscal_device.driver.fiscalize(self) if fiscalizable? && origin.try(:fiscal_device)
165
+ if defined?(Extface) && fiscalizable? && device = origin.try(:fiscal_device)
166
+ #self.extface_job = origin.fiscal_device.driver.fiscalize(self) if fiscalizable? && origin.try(:fiscal_device)
167
+ self.extface_job = device.jobs.new
160
168
  end
161
169
  end
162
170
  true
163
171
  end
164
172
 
165
- def fiscalizable?
166
- payments.select(&:fiscal?).any?
173
+ def create_fiscal_job
174
+ if self.extface_job_id_changed? && defined?(Resque) && defined?(Extface) && device = origin.try(:fiscal_device)
175
+ p "device: #{device.try(:id)}"
176
+ p "self: #{self.try(:id)}"
177
+ p "self.finalized_at changed: #{self.finalized_at_changed?}"
178
+
179
+ Resque.enqueue_to("extface_#{device.id}", Billing::IssueFiscalDoc, self.id)
180
+ end
181
+ true
167
182
  end
168
183
 
169
184
  end
@@ -6,5 +6,10 @@ require 'collection_proxy_wild'
6
6
  module Billing
7
7
  class Engine < ::Rails::Engine
8
8
  isolate_namespace Billing
9
+ config.to_prepare do
10
+ PaperTrail::Version.module_eval do
11
+ self.abstract_class = true
12
+ end
13
+ end
9
14
  end
10
15
  end
@@ -1,3 +1,3 @@
1
1
  module Billing
2
- VERSION = "0.0.7e"
2
+ VERSION = "0.0.7f"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: billing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7e
4
+ version: 0.0.7f
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Vangelov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-02 00:00:00.000000000 Z
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: resque
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: sqlite3
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -76,6 +90,7 @@ files:
76
90
  - MIT-LICENSE
77
91
  - README.rdoc
78
92
  - Rakefile
93
+ - app/jobs/billing/issue_fiscal_doc.rb
79
94
  - app/models/billing/bill.rb
80
95
  - app/models/billing/charge.rb
81
96
  - app/models/billing/department.rb
@@ -253,7 +268,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
253
268
  version: 1.3.1
254
269
  requirements: []
255
270
  rubyforge_project:
256
- rubygems_version: 2.4.6
271
+ rubygems_version: 2.5.0
257
272
  signing_key:
258
273
  specification_version: 4
259
274
  summary: Billing for Rails 4 app