Untitled
unknown
ruby
4 years ago
8.5 kB
4
Indexable
class Api::V1::InvoicesController < ApplicationController skip_before_action :verify_authenticity_token def index @invoices = Invoice.all.order(created_at: :desc) invoicesList = [] @invoices.each do |invoice| patientName = "" unless invoice.patient.nil? || invoice.patient == "" patientLastName = "AA," patientFirstName = Patient.find(invoice.patient.to_i).firstname patientName = patientLastName + patientFirstName end unless invoice.diagnostic.nil? || invoice.diagnostic == "" myDiagnosis = JSON.parse(invoice.diagnostic) myDiagnosis.each do |diagnosis| puts diagnosis end end invoicesList.push( :id => invoice.id, :name => patientName, :diagnosis => invoice.diagnostic, :date => (invoice.created_at).to_date, :type => "invoice", :total => invoice.total, :status => invoice.status ) end render json: invoicesList end def create citationProducts = JSON.parse(invoice_params["facuration"]) citationProducts.map { |element| element.unshift(Product.find(element[0]).title) } invoiceData = { :unique => invoice_params["unique"], :patient => invoice_params["patient"], :insurance => invoice_params["insurance"], :diagnostic => invoice_params["diagnostic"], :date => invoice_params["date"].to_date, :facuration => citationProducts, :total => invoice_params["total"], :status => invoice_params["status"] } puts invoiceData puts invoice_params invoice = Invoice.create!(invoiceData) if invoice puts 'invoice created!' render json: { message: 'Success!', id: (invoice.id).to_s }, status: :ok else render json: invoice.errors end end def show puts invoice currentPatient = Patient.find(invoice.patient) puts currentPatient.firstname puts currentPatient.lastname if invoice render json: invoice else render json: invoice.errors end end def patientsToInvoice invoice = Invoice.where(status: 'waiting') puts invoice render json: { message: 'hola' } end def buildPdf puts 'generating pdf...' @invoice = Invoice.find(params[:id].to_i) @patient = Patient.all @treatment = Treatment.all @citation = Citation.all puts '#### FIRST' invoicedItems = JSON.parse(@invoice.facuration) invoicedPatient = @patient.find(@invoice.patient) @invoicedPatientName = invoicedPatient.lastname + ', ' + invoicedPatient.firstname @invoicedPatientAddress = invoicedPatient.address @invoicedPatientEmail = invoicedPatient.email @invoicedPatientDiagnosis = @invoice.diagnostic @invoicedProducts = [] invoicedItems.each do |product| currentProductTitle = product[0] currentProductPrice = product[2].to_f currentProductQty = product[3].to_i currentSubtotal = currentProductPrice * currentProductQty @invoicedProducts.push( [currentProductTitle, currentProductPrice, currentProductQty, currentSubtotal]) end invoiceId = "<h4> ID: #000-" + (@invoice.id).to_s + "</h4>" patientName = "<h4> Name: " + @invoicedPatientName + "</h4>" patientAddress = "<h4> Address: " + @invoicedPatientAddress + "</h4>" patientEmail = "<h4> Email: " + @invoicedPatientEmail + "</h4>" patientInsurance = "<h4>Insurance: " + @invoice.insurance + "</h4>" invoicedAt = "<h4> Invoiced At: " + ((@invoice.created_at).to_date).to_s + "</h4>" invoiceDue = "<h4> Invoice Due: " + ((@invoice.created_at + 14.day).to_date).to_s + "</h4>" invoiceFacturation = "<h4>Products: </h4>" + (@invoicedProducts).to_s @invoiceDiagnosis = JSON.parse(@invoice.diagnostic) @invoiceTotal = (@invoice.total).to_s #pdf = WickedPdf.new.pdf_from_string(invoiceId + patientName + patientAddress + patientInsurance + patientEmail + invoicedAt + invoiceDue + invoiceFacturation + invoiceTotal) #send_data pdf, filename: @invoicedPatientName + '.pdf' #saludo() pdf_html = ActionController::Base.new.render_to_string( template: 'invoices/buildPdf.html.erb', layout: 'pdf.html', page_size: 'A4', orientation: "Landscape", zoom: 1, dpi: 75, :encoding => "utf-8", :margin => { :top => 0, :bottom => 0 , :left => 0 , :right => 0}, locals: { :@invoice => @invoice, :@invoicedPatientName => @invoicedPatientName, :@invoicedPatientAddress => @invoicedPatientAddress, :@invoicedPatientEmail => @invoicedPatientEmail, :@invoiceDiagnosis => @invoiceDiagnosis, :@invoicedProducts => @invoicedProducts, :@invoiceTotal => @invoiceTotal} ) pdf = WickedPdf.new.pdf_from_string(pdf_html, :encoding => 'utf-8' ) pdfFilename = 'RE_00' + @invoice.id.to_s + '_' + invoicedPatient.lastname + '_' + invoicedPatient.firstname + '.pdf' send_data pdf, filename: pdfFilename end def generatePdf puts 'generating pdf...' #@invoice = scope.find(params[:id]) @invoice = Invoice.find(params[:id].to_i) pricesForInsurances = [ [id = 0, value = "Beihilfe", name = "price"], [id = 1, value = "Postbeamtenkasse", name = "p_two"], [id = 2, value = "Kinderwunsch Zusatz", name = "p_three"], [id = 3, value = "Selbstzahler-Kinderwunsch", name = "p_four"], [id = 4, value = "Selbstzahler", name = "p_five"]] @patient = Patient.all @treatment = Treatment.all @citation = Citation.all puts '#### FIRST' #puts @invoice.inspect invoicedTreatments = JSON.parse(@invoice.facuration) invoicedPatient = @patient.find(@invoice.patient) @invoicedPatientName = invoicedPatient.firstname @invoicedPatientAddress = invoicedPatient.address @invoicedPatientEmail = invoicedPatient.email invoicedPatientInsurance = pricesForInsurances[(invoicedPatient.insurance).to_i][2] @invoicedCitations = [] @invoicedProducts = [] @invoicedPrices = [] @totalPrice = 0 @invoicedTreatmentsName = [] #puts invoicedTreatments invoicedTreatments.each do |treatment| thisTreatment = @treatment.find(treatment[3]) thisTreatmentName = thisTreatment.name thisCitationID = @citation.find(treatment[1]) #puts thisTreatmentName thisProducts = JSON.parse(thisTreatment.products) eachTreatmentArray = [] partialPrice = 0 thisProducts.each do |product| eachProductArray = [product['title'], product[invoicedPatientInsurance]] eachTreatmentArray.push(eachProductArray) partialPrice = partialPrice + product[invoicedPatientInsurance].to_f end @totalPrice = @totalPrice + partialPrice @invoicedPrices.push(partialPrice) @invoicedCitations.push([thisCitationID.id, thisCitationID.time, thisCitationID.diagnosis, thisCitationID.altdiagnosis]) @invoicedTreatmentsName.push(thisTreatmentName) @invoicedProducts.push(eachTreatmentArray) end invoiceId = "<h4> ID: #000-" + (@invoice.id).to_s + "</h4>" patientName = "<h4> Name: " + @invoicedPatientName + "</h4>" patientAddress = "<h4> Address: " + @invoicedPatientAddress + "</h4>" patientEmail = "<h4> Email: " + @invoicedPatientEmail + "</h4>" invoicedAt = "<h4> Invoiced At: " + ((@invoice.created_at).to_date).to_s + "</h4>" invoiceDue = "<h4> Invoice Due: " + ((@invoice.created_at + 14.day).to_date).to_s + "</h4>" invoiceFacturation = "<h4>Facturation: </h4>" + @invoice.facuration pdf = WickedPdf.new.pdf_from_string(invoiceId + patientName + patientAddress + patientEmail + invoicedAt + invoiceDue + invoiceFacturation) send_data pdf, filename: @invoicedPatientName + '.pdf' end def destroy invoice&.destroy render json: { message: 'invoice deleted!' } end private def invoice_params params.permit(:unique, :patient, :insurance, :diagnostic, :date, :facuration, :total, :status) end def invoice @invoice ||= Invoice.find(params[:id]) end end
Editor is loading...