| 1 | package WWW::NFSe::Guarapari; | ||
| 2 | |||
| 3 | #ABSTRACT: Another scraper perl | ||
| 4 | |||
| 5 | =encoding utf8 | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | WWW::NFSe::Guarapari- Module for issuance electronic invoice and customer management. | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | version 0.02 | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | our $VERSION = '0.02'; # VERSION | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use WWW::NFSe::Guarapari; | ||
| 22 | |||
| 23 | my $nfse = WWW::NFSe::Guarapari->new(); | ||
| 24 | #my $nfse= new WWW::NFSe::Guarapari(); | ||
| 25 | |||
| 26 | # Loga no sistema | ||
| 27 | $nfse->logar( "cpfcnpj", "senha" ); | ||
| 28 | |||
| 29 | # Novo Cliente(Tomador/Intermediário) | ||
| 30 | my $tipo_tomador = "J"; # F - J - E(Estrangeiro) | ||
| 31 | my $documento = "000.000.000-00"; # CPF ou CNPJ | ||
| 32 | my $nome = "Fulano de Tal"; | ||
| 33 | my $NomeFantasia = "Beltranos S.A"; #If tipo_tomador = "F" | ||
| 34 | my $InscricaoMunicipal = "000.000"; #If tipo_tomador = "F" | ||
| 35 | my $InscricaoEstadual = "000.000"; #If tipo_tomador = "F" | ||
| 36 | my $telefone = ""; | ||
| 37 | my $email = "fulano@de.tal"; | ||
| 38 | my $municipioEstrangeiro = "NeverLand"; #If tipo_tomador = "E" | ||
| 39 | my $PaisEstrangeiro = "NeverLand"; #If tipo_tomador = "E" | ||
| 40 | my $tipologradouro = ""; | ||
| 41 | my $tipoEndereco = "-1"; # -1/Selecione | 3/Cobrança | 2/Comercial | 4/Correspondência | 1/Residencial | ||
| 42 | my $logradouro = ""; | ||
| 43 | my $numero = ""; | ||
| 44 | my $complemento = ""; | ||
| 45 | my $bairro = ""; | ||
| 46 | my #cep = "000.000-000"; | ||
| 47 | my $uf = "DF"; | ||
| 48 | my $cidades = ""; | ||
| 49 | |||
| 50 | print $nfse->novo_cliente( $tipo_tomador, $documento, $nome, $telefone, $email, $tipologradouro, $tipoendereco, $logradouro, $numero, $complemento, $bairro, $cep, $uf, $cidades ); | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | use strict; | ||
| 55 | use warnings; | ||
| 56 | use vars qw($VERSION); | ||
| 57 | use utf8; | ||
| 58 | use WWW::Mechanize; | ||
| 59 | use HTML::TreeBuilder::XPath; | ||
| 60 | use HTML::Entities; | ||
| 61 | use JSON; | ||
| 62 | |||
| 63 | my $statusLogin; | ||
| 64 | my $statusLoginRegistro; | ||
| 65 | my $entitieschars; | ||
| 66 | my $mech; | ||
| 67 | |||
| 68 | sub new | ||
| 69 | { | ||
| 70 | my $class = shift; | ||
| 71 | my $self = { | ||
| 72 | |||
| 73 | }; | ||
| 74 | |||
| 75 | $statusLogin = 0; | ||
| 76 | $entitieschars = 'ÁÍÓÚÉÄÏÖÜËÀÌÒÙÈÃÕÂÎÔÛÊáíóúéäïöüëàìòùèãõâîôûêÇç'; | ||
| 77 | $mech = WWW::Mechanize->new; | ||
| 78 | $mech->agent_alias( 'Windows Mozilla' ); | ||
| 79 | $mech->cookie_jar(HTTP::Cookies->new()); | ||
| 80 | |||
| 81 | bless $self, $class; | ||
| 82 | return $self, $class; | ||
| 83 | } | ||
| 84 | |||
| 85 | =head1 METHODS | ||
| 86 | |||
| 87 | =head2 logar() | ||
| 88 | |||
| 89 | Loga no painel de controle. Este método deverá ser chamado antes de qualquer outro método. Ativa flag $statusLogin. | ||
| 90 | |||
| 91 | my $status_login = $painel->logar($email, $senha); | ||
| 92 | |||
| 93 | Return string | ||
| 94 | |||
| 95 | logged, invalid login, not found, unknow HTTP error, connection error | ||
| 96 | |||
| 97 | |||
| 98 | =cut | ||
| 99 | sub logar | ||
| 100 | { | ||
| 101 | my($self, $cpfcnpj, $senha) = @_; | ||
| 102 | my $html; | ||
| 103 | my $login_page = 'http://nfseteste.guarapari.es.gov.br/NFSETESTE/'; | ||
| 104 | |||
| 105 | $mech->get($login_page); | ||
| 106 | die $mech->response->status_line unless $mech->success; | ||
| 107 | |||
| 108 | if($mech->success()) | ||
| 109 | { | ||
| 110 | if($mech->status() == 200) | ||
| 111 | { | ||
| 112 | |||
| 113 | |||
| 114 | my $name = 'ctl00$ctl00$MasterConteudo$Conteudo$LoginSistema$UserName'; | ||
| 115 | my $pass = 'ctl00$ctl00$MasterConteudo$Conteudo$LoginSistema$Password'; | ||
| 116 | my $button = 'ctl00$ctl00$MasterConteudo$Conteudo$LoginSistema$LoginButton'; | ||
| 117 | my $viewstate = ($mech->find_all_inputs( type => 'hidden', name => '__VIEWSTATE' ))[0]->value; | ||
| 118 | my $validation = ($mech->find_all_inputs( type => 'hidden', name => '__EVENTVALIDATION' ))[0]->value; | ||
| 119 | |||
| 120 | #loga no painel | ||
| 121 | $mech->field($name => $cpfcnpj); | ||
| 122 | $mech->field($pass => $senha); | ||
| 123 | $mech->field(__VIEWSTATE => $viewstate); | ||
| 124 | $mech->field(__EVENTVALIDATION => $validation); | ||
| 125 | $mech->field(__VIEWSTATEENCRYPTED => ''); | ||
| 126 | $mech->click(); #Não funcionou nem com post() nem submit() apenas simulando click() | ||
| 127 | |||
| 128 | $html = $mech->content; | ||
| 129 | $mech->update_html( $html ); | ||
| 130 | my $tree = HTML::TreeBuilder::XPath->new; | ||
| 131 | $tree->parse( $html ); | ||
| 132 | # resposta da tentiva de login | ||
| 133 | my $respostaloga = $tree->findnodes( '//div[@id="_TituloArea"]' ); | ||
| 134 | |||
| 135 | if(index($respostaloga, "Gerar NFS-e") == -1) | ||
| 136 | { | ||
| 137 | $statusLogin = 1; | ||
| 138 | return "logged"; | ||
| 139 | } | ||
| 140 | else | ||
| 141 | { | ||
| 142 | return "invalid login"; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | elsif($mech->status() == 404) | ||
| 146 | { | ||
| 147 | return "not found"; | ||
| 148 | } | ||
| 149 | else | ||
| 150 | { | ||
| 151 | return "unknow HTTP error"; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | else | ||
| 155 | { | ||
| 156 | return "connection error"; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | =head2 novo_cliente() | ||
| 161 | |||
| 162 | Cria novo tomador/Intermediário | ||
| 163 | |||
| 164 | # Novo Cliente(Tomador/Intermediário) | ||
| 165 | my $tipo_tomador = "J"; # F - J - E(Estrangeiro) | ||
| 166 | my $documento = "000.000.000-00"; # CPF ou CNPJ | ||
| 167 | my $nome = "Fulano de Tal"; | ||
| 168 | my $NomeFantasia = "Beltranos S.A"; #If tipo_tomador = "F" | ||
| 169 | my $InscricaoMunicipal = "000.000"; #If tipo_tomador = "F" | ||
| 170 | my $InscricaoEstadual = "000.000"; #If tipo_tomador = "F" | ||
| 171 | my $telefone = ""; | ||
| 172 | my $email = "fulano@de.tal"; | ||
| 173 | my $municipioEstrangeiro = "NeverLand"; #If tipo_tomador = "E" | ||
| 174 | my $PaisEstrangeiro = "NeverLand"; #If tipo_tomador = "E" | ||
| 175 | my $tipologradouro = ""; | ||
| 176 | my $tipoEndereco = "-1"; # -1/Selecione | 3/Cobrança | 2/Comercial | 4/Correspondência | 1/Residencial | ||
| 177 | my $logradouro = ""; | ||
| 178 | my $numero = ""; | ||
| 179 | my $complemento = ""; | ||
| 180 | my $bairro = ""; | ||
| 181 | my #cep = "000.000-000"; | ||
| 182 | my $uf = "DF"; | ||
| 183 | my $cidades = ""; | ||
| 184 | |||
| 185 | print $nfse->novo_cliente( $tipo_tomador, $documento, $nome, $telefone, $email, $tipologradouro, $tipoendereco, $logradouro, $numero, $complemento, $bairro, $cep, $uf, $cidades ); | ||
| 186 | |||
| 187 | Return JSON | ||
| 188 | |||
| 189 | {"nome":"Fulano de Tal","resposta":"registrado","status":"sucesso","codigo":"107630"} | ||
| 190 | {"resposta":"E-mail em uso","status":"erro"} | ||
| 191 | {"resposta":"efetue login primeiro","status":"erro"} | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | |||
| 196 | 1; | ||
| 197 | |||
| 198 | __END__ |