mirror of
https://github.com/internetee/registry.git
synced 2025-07-21 18:26:06 +02:00
Merge branch 'story/105852786-directo' into staging
# Conflicts: # Gemfile
This commit is contained in:
commit
30b057d535
8 changed files with 104 additions and 9 deletions
|
@ -2,6 +2,7 @@ class BankTransaction < ActiveRecord::Base
|
|||
include Versions
|
||||
belongs_to :bank_statement
|
||||
has_one :account_activity
|
||||
has_many :directo_records, as: :item, class_name: 'Directo'
|
||||
|
||||
scope :unbinded, lambda {
|
||||
where('id NOT IN (SELECT bank_transaction_id FROM account_activities where bank_transaction_id IS NOT NULL)')
|
||||
|
@ -16,21 +17,32 @@ class BankTransaction < ActiveRecord::Base
|
|||
account_activity.invoice
|
||||
end
|
||||
|
||||
|
||||
def invoice_num
|
||||
return @invoice_no if defined?(@invoice_no)
|
||||
|
||||
match = description.match(/^[^\d]*(\d+)/)
|
||||
return unless match
|
||||
|
||||
@invoice_no = match[1].try(:to_i)
|
||||
end
|
||||
|
||||
def invoice
|
||||
@invoice ||= registrar.invoices.find_by(number: invoice_num) if registrar
|
||||
end
|
||||
|
||||
def registrar
|
||||
@registrar ||= Registrar.find_by(reference_no: reference_no)
|
||||
end
|
||||
|
||||
|
||||
# For successful binding, reference number, invoice id and sum must match with the invoice
|
||||
# rubocop: disable Metrics/PerceivedComplexity
|
||||
# rubocop: disable Metrics/CyclomaticComplexity
|
||||
def autobind_invoice
|
||||
return if binded?
|
||||
registrar = Registrar.find_by(reference_no: reference_no)
|
||||
return unless registrar
|
||||
|
||||
match = description.match(/^[^\d]*(\d+)/)
|
||||
return unless match
|
||||
|
||||
invoice_no = match[1].to_i
|
||||
return unless invoice_no
|
||||
|
||||
invoice = registrar.invoices.find_by(number: invoice_no)
|
||||
return unless invoice_num
|
||||
return unless invoice
|
||||
|
||||
return if invoice.binded?
|
||||
|
|
47
app/models/directo.rb
Normal file
47
app/models/directo.rb
Normal file
|
@ -0,0 +1,47 @@
|
|||
class Directo < ActiveRecord::Base
|
||||
belongs_to :item, polymorphic: true
|
||||
|
||||
def self.send_receipts
|
||||
new_trans = BankTransaction.where(in_directo: false)
|
||||
new_trans.find_in_batches(batch_size: 10).each do |group|
|
||||
mappers = {} # need them as no direct connection between transaction and invoice
|
||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||
xml.invoices {
|
||||
group.each do |transaction|
|
||||
next unless transaction.invoice
|
||||
num = transaction.invoice_num
|
||||
mappers[num] = transaction
|
||||
|
||||
xml.invoice(
|
||||
"SalesAgent" => transaction.invoice.seller_name,
|
||||
"Number" => num,
|
||||
"InvoiceDate" => (transaction.paid_at||transaction.created_at).strftime("%Y-%m-%dT%H:%M:%S"),
|
||||
"PaymentTerm" => Setting.directo_receipt_payment_term,
|
||||
"Currency" => transaction.currency,
|
||||
"CustomerCode"=> transaction.invoice.try(:buyer).try(:directo_handle)
|
||||
){
|
||||
xml.line(
|
||||
"ProductID"=> Setting.directo_receipt_product_name,
|
||||
"Quantity" => 1,
|
||||
"UnitPriceWoVAT" =>ActionController::Base.helpers.number_with_precision(transaction.sum.to_f/1.2, precision: 2, separator: "."),
|
||||
"ProductName" => transaction.description
|
||||
)
|
||||
}
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
data = builder.to_xml.gsub("\n",'')
|
||||
response = RestClient::Request.execute(url: ENV['directo_invoice_url'], method: :post, payload: {put: "1", what: "invoice", xmldata: data}, verify_ssl: false).to_s
|
||||
dump_result_to_db(mappers, response)
|
||||
end
|
||||
end
|
||||
|
||||
def self.dump_result_to_db mappers, xml
|
||||
Nokogiri::XML(xml).css("Result").each do |res|
|
||||
obj = mappers[res.attributes["docid"].value.to_i]
|
||||
obj.directo_records.first_or_create!(response: res.as_json.to_h)
|
||||
obj.update_columns(in_directo: true)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue