|
| 1 | +module BrDanfe |
| 2 | + module DanfeLib |
| 3 | + module NfeLib |
| 4 | + class Entrega |
| 5 | + Y_POSITION = 12.92 |
| 6 | + MAXIMUM_SIZE_FOR_STREET = 319 |
| 7 | + |
| 8 | + def initialize(pdf, xml) |
| 9 | + @pdf = pdf |
| 10 | + @xml = xml |
| 11 | + |
| 12 | + @y_position = Y_POSITION |
| 13 | + @ltitle = @y_position - 0.42 |
| 14 | + @l1 = @y_position |
| 15 | + @l2 = @y_position + LINE_HEIGHT |
| 16 | + @l3 = @y_position + (LINE_HEIGHT * 2) |
| 17 | + end |
| 18 | + |
| 19 | + def render |
| 20 | + if Entrega.delivery_local?(@xml) |
| 21 | + @pdf.ititle 0.42, 10.00, 0.75, @ltitle, 'entrega.title' |
| 22 | + render_line1 |
| 23 | + render_line2 |
| 24 | + render_line3 |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + def self.delivery_local?(xml) |
| 29 | + doc = xml.is_a?(BrDanfe::XML) ? xml : Nokogiri::XML(xml) |
| 30 | + doc.css('entrega/xLgr').text.present? |
| 31 | + end |
| 32 | + |
| 33 | + private |
| 34 | + |
| 35 | + def render_line1 |
| 36 | + @pdf.lbox LINE_HEIGHT, 11.82, 0.75, @l1, @xml, 'entrega/xNome' |
| 37 | + render_cnpj_cpf |
| 38 | + @pdf.lie LINE_HEIGHT, 2.92, 17.40, @l1, @xml, 'entrega/UF', 'entrega/IE' |
| 39 | + end |
| 40 | + |
| 41 | + def render_cnpj_cpf |
| 42 | + if @xml['entrega/CNPJ'] == '' |
| 43 | + @pdf.i18n_lbox LINE_HEIGHT, 4.37, 12.57, @l1, 'entrega.CPF', cpf |
| 44 | + else |
| 45 | + @pdf.lcnpj LINE_HEIGHT, 4.37, 12.57, @l1, @xml, 'entrega/CNPJ' |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + def cpf |
| 50 | + cpf = BrDocuments::CnpjCpf::Cpf.new(@xml['entrega/CPF']) |
| 51 | + cpf.formatted |
| 52 | + end |
| 53 | + |
| 54 | + def render_line2 |
| 55 | + @pdf.i18n_lbox LINE_HEIGHT, 11.82, 0.75, @l2, 'entrega.xLgr', address |
| 56 | + @pdf.lbox LINE_HEIGHT, 4.37, 12.57, @l2, @xml, 'entrega/xBairro' |
| 57 | + @pdf.i18n_lbox LINE_HEIGHT, 2.92, 17.40, @l2, 'entrega.CEP', cep |
| 58 | + end |
| 59 | + |
| 60 | + def address |
| 61 | + address = Helper.generate_address @xml, 'entrega' |
| 62 | + |
| 63 | + if Helper.address_is_too_big(@pdf, address) |
| 64 | + address = address[0..address.length - 2] while Helper.mensure_text(@pdf, "#{address.strip}...") > MAXIMUM_SIZE_FOR_STREET && !address.empty? |
| 65 | + address = "#{address.strip}..." |
| 66 | + end |
| 67 | + address |
| 68 | + end |
| 69 | + |
| 70 | + def cep |
| 71 | + BrDanfe::Helper.format_cep(@xml['entrega/CEP']) |
| 72 | + end |
| 73 | + |
| 74 | + def render_line3 |
| 75 | + @pdf.lbox LINE_HEIGHT, 15.05, 0.75, @l3, @xml, 'entrega/xMun' |
| 76 | + @pdf.lbox LINE_HEIGHT, 1.14, 15.8, @l3, @xml, 'entrega/UF' |
| 77 | + @pdf.i18n_lbox LINE_HEIGHT, 2.92, 17.40, @l3, 'entrega.fone', phone |
| 78 | + end |
| 79 | + |
| 80 | + def phone |
| 81 | + Phone.format(@xml['entrega/fone']) |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments