diff --git a/src/resources/B422/remessa/cnab400/Registro0.php b/src/resources/B422/remessa/cnab400/Registro0.php index c94bf07..f157d95 100644 --- a/src/resources/B422/remessa/cnab400/Registro0.php +++ b/src/resources/B422/remessa/cnab400/Registro0.php @@ -36,95 +36,74 @@ class Registro0 extends Generico0 { protected $meta = array( - 'identificacao_registro' => array( - 'tamanho' => 1, - 'default' => '0', - 'tipo' => 'int', - 'required' => true - ), - 'operacao' => array( - 'tamanho' => 1, - 'default' => '1', - 'tipo' => 'int', - 'required' => true - ), - 'literal_remessa' => array( - 'tamanho' => 7, - 'default' => 'REMESSA', - 'tipo' => 'alfa', - 'required' => true - ), - 'tipo_servico' => array( - 'tamanho' => 2, - 'default' => '01', - 'tipo' => 'int', - 'required' => true - ), - 'literal_servico' => array( - 'tamanho' => 15, - 'default' => 'COBRANCA', - 'tipo' => 'alfa', - 'required' => true - ), - 'codigo_beneficiario' => array( - 'tamanho' => 20, - 'default' => '', - 'tipo' => 'alfa', - 'required' => true - ), - 'nome_empresa' => array( - 'tamanho' => 30, - 'default' => ' ', - 'tipo' => 'alfa', - 'required' => true - ), - 'codigo_banco' => array( - 'tamanho' => 3, - 'default' => '422', - 'tipo' => 'int', - 'required' => true - ), - 'nome_banco' => array( - 'tamanho' => 15, - 'default' => 'SAFRA', - 'tipo' => 'alfa', - 'required' => true - ), - 'data_gravacao' => array( - 'tamanho' => 6, - 'default' => '', // não informar a data na instanciação - gerada dinamicamente - 'tipo' => 'date', - 'required' => true - ), - 'filler1' => array( - 'tamanho' => 8, - 'default' => ' ', - 'tipo' => 'alfa', - 'required' => true - ), - 'identificacao_sistema' => array( - 'tamanho' => 2, - 'default' => 'MX', - 'tipo' => 'alfa', - 'required' => true - ), - 'numero_sequencial_arquivo' => array( - 'tamanho' => 7, - 'default' => '1', - 'tipo' => 'int', - 'required' => true - ), - 'filler2' => array( - 'tamanho' => 277, - 'default' => ' ', - 'tipo' => 'alfa', - 'required' => true - ), - 'numero_sequencial' => array( - 'tamanho' => 6, - 'default' => '1', - 'tipo' => 'int', - 'required' => true + 'tipo_registro' => array( // Pos: 001-001 + 'tamanho' => 1, 'default' => '0', 'tipo' => 'int', 'required' => true + ), + 'operacao' => array( // Pos: 002-002 + 'tamanho' => 1, 'default' => '1', 'tipo' => 'int', 'required' => true + ), + 'literal_remessa' => array( // Pos: 003-009 + 'tamanho' => 7, 'default' => 'REMESSA', 'tipo' => 'alfa', 'required' => true + ), + 'tipo_servico' => array( // Pos: 010-011 + 'tamanho' => 2, 'default' => '01', 'tipo' => 'int', 'required' => true + ), + 'literal_servico' => array( // Pos: 012-019 + 'tamanho' => 8, 'default' => 'COBRANCA', 'tipo' => 'alfa', 'required' => true + ), + 'brancos1' => array( // Pos: 020-026 (7 brancos) + 'tamanho' => 7, 'default' => ' ', 'tipo' => 'alfa', 'required' => false + ), + 'codigo_cedente' => array( // Pos: 027-040 (AGÊNCIA 5 + CONTA 9 = 14) + 'tamanho' => 14, 'default' => '', 'tipo' => 'int', 'required' => true + ), + 'brancos2' => array( // Pos: 041-046 (6 brancos) + 'tamanho' => 6, 'default' => ' ', 'tipo' => 'alfa', 'required' => false + ), + 'nome_empresa' => array( // Pos: 047-076 + 'tamanho' => 30, 'default' => '', 'tipo' => 'alfa', 'required' => true + ), + 'codigo_banco' => array( // Pos: 077-079 + 'tamanho' => 3, 'default' => '422', 'tipo' => 'int', 'required' => true + ), + 'nome_banco' => array( // Pos: 080-090 + 'tamanho' => 11, 'default' => 'BANCO SAFRA', 'tipo' => 'alfa', 'required' => true + ), + 'brancos3' => array( // Pos: 091-094 (4 brancos) + 'tamanho' => 4, 'default' => ' ', 'tipo' => 'alfa', 'required' => false + ), + 'data_gravacao' => array( // Pos: 095-100 (DDMMAA) + 'tamanho' => 6, 'default' => '', 'tipo' => 'date', 'required' => true + ), + 'brancos4' => array( // Pos: 101-391 (291 brancos) + 'tamanho' => 291, 'default' => ' ', 'tipo' => 'alfa', 'required' => false + ), + 'numero_sequencial_arquivo' => array( // Pos: 392-394 + 'tamanho' => 3, 'default' => '001', 'tipo' => 'int', 'required' => true + ), + 'sequencial_registro' => array( // Pos: 395-400 + 'tamanho' => 6, 'default' => '000001', 'tipo' => 'int', 'required' => true ), ); + + protected function set_codigo_banco($value) { + $this->data['codigo_banco'] = '422'; + } + + protected function set_nome_banco($value) { + $this->data['nome_banco'] = 'BANCO SAFRA'; + } + + protected function set_data_gravacao($value) { + $this->data['data_gravacao'] = date('dmy'); + } + + protected function set_numero_sequencial_arquivo($value) { + $valor = str_pad($value, 3, '0', STR_PAD_LEFT); + $this->data['numero_sequencial_arquivo'] = substr($valor, 0, 3); + } + + protected function set_sequencial_registro($value) { + $this->data['sequencial_registro'] = '000001'; + } } diff --git a/src/resources/B422/remessa/cnab400/Registro1.php b/src/resources/B422/remessa/cnab400/Registro1.php index 0e16a69..c84d2f3 100644 --- a/src/resources/B422/remessa/cnab400/Registro1.php +++ b/src/resources/B422/remessa/cnab400/Registro1.php @@ -38,297 +38,231 @@ class Registro1 extends Generico1 { protected $meta = array( - 'tipo_registro' => array( - 'tamanho' => 1, - 'default' => '1', - 'tipo' => 'int', - 'required' => true - ), - 'agencia_debito' => array( - 'tamanho' => 5, - 'default' => '0', - 'tipo' => 'int', - 'required' => true - ), - 'digito_agencia_debito' => array( - 'tamanho' => 1, - 'default' => '0', - 'tipo' => 'int', - 'required' => true - ), - 'razao_conta_corrente_pagador' => array( - 'tamanho' => 5, - 'default' => '0', - 'tipo' => 'int', - 'required' => true - ), - 'conta_corrente_pagador' => array( - 'tamanho' => 7, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'tipo_registro' => array( // 001-001 + 'tamanho' => 1, 'default' => '1', 'tipo' => 'int', 'required' => true ), - 'digito_conta_corrente_pagador' => array( - 'tamanho' => 1, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'tipo_inscricao' => array( // 002-003 + 'tamanho' => 2, 'default' => '', 'tipo' => 'int', 'required' => true ), - 'filler0' => array( - 'tamanho' => 1, - 'default' => '0', - 'tipo' => 'int', - 'required' => false + 'numero_inscricao' => array( // 004-017 + 'tamanho' => 14, 'default' => '', 'tipo' => 'int', 'required' => true ), - 'carteira' => array( - 'tamanho' => 3, - 'default' => '001', - 'tipo' => 'int', - 'required' => true - ), - 'agencia' => array( - 'tamanho' => 5, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'empresa_banco' => array( // 018-031 + 'tamanho' => 14, 'default' => '', 'tipo' => 'alfa', 'required' => true ), - 'conta' => array( - 'tamanho' => 7, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'filler1' => array( // 032-037 + 'tamanho' => 6, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'conta_dv' => array( - 'tamanho' => 1, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'uso_empresa' => array( // 038-062 + 'tamanho' => 25, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'seu_numero' => array( - 'tamanho' => 25, - 'default' => ' ', - 'tipo' => 'alfa', - 'required' => true + 'nosso_numero' => array( // 063-071 + 'tamanho' => 9, 'default' => '000000000', 'tipo' => 'int', 'required' => true ), - 'codigo_banco_debito' => array( - 'tamanho' => 3, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'brancos_pos072_101' => array( + 'tamanho' => 30, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'codigo_multa' => array( - 'tamanho' => 1, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'codigo_iof' => array( // 102-102 + 'tamanho' => 1, 'default' => '0', 'tipo' => 'int', 'required' => true ), - 'percentual_multa' => array( - 'tamanho' => 4, - 'default' => '0', - 'tipo' => 'decimal', - 'precision' => 2, - 'required' => true + 'identificacao_moeda' => array( // 103-104 + 'tamanho' => 2, 'default' => '00', 'tipo' => 'int', 'required' => true ), - 'nosso_numero' => array( - 'tamanho' => 9, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'brancos_pos105' => array( // 105-105 + 'tamanho' => 1, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'filler1' => array( - 'tamanho' => 10, - 'default' => ' ', - 'tipo' => 'alfa', - 'required' => true + 'terceira_instrucao' => array( // 106-107 + 'tamanho' => 2, 'default' => '00', 'tipo' => 'int', 'required' => true ), - 'emissao_boleto' => array( + 'identificacao_carteira' => array( // Posição 108 (1=Simples, 2=Vinculada) 'tamanho' => 1, - 'default' => '2', + 'default' => '1', 'tipo' => 'int', 'required' => true ), - 'debito_automatico' => array( - 'tamanho' => 1, - 'default' => 'N', - 'tipo' => 'alfa', - 'required' => true - ), - 'filler2' => array( - 'tamanho' => 13, - 'default' => ' ', - 'tipo' => 'alfa', - 'required' => true - ), - 'codigo_movimento' => array( + 'identificacao_ocorrencia' => array( // Posições 109-110 'tamanho' => 2, - 'default' => '01', + 'default' => '01', // 01 = entrada de títulos 'tipo' => 'int', 'required' => true ), - 'numero_documento' => array( + 'identificacao_titulo_empresa' => array( // 111-120 Seu número 'tamanho' => 10, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'data_vencimento' => array( + 'data_vencimento' => array( // 121-126 'tamanho' => 6, 'default' => '', - 'tipo' => 'date', + 'tipo' => 'alfa', 'required' => true ), - 'valor' => array( + 'valor' => array( // 127-139 13 dígitos (valor em centavos) 'tamanho' => 13, - 'default' => '', - 'tipo' => 'decimal', - 'precision' => 2, + 'default' => '0', + 'tipo' => 'int', // Usar int em vez de decimal para controlar tamanho 'required' => true ), - 'banco_cobrador' => array( + 'codigo_banco_cobrador' => array( //140-147 Posições 140-142 'tamanho' => 3, 'default' => '422', 'tipo' => 'int', 'required' => true ), - 'agencia_cobradora' => array( + 'agencia_cobradora' => array( // Posições 143-147 'tamanho' => 5, 'default' => '0', 'tipo' => 'int', 'required' => true ), - 'especie' => array( + 'especie_titulo' => array( // Posições 148-149 'tamanho' => 2, - 'default' => '01', + 'default' => '01', // 01=Duplicata Mercantil 'tipo' => 'int', 'required' => true ), - 'aceite' => array( + 'aceite' => array( // Posição 150 'tamanho' => 1, 'default' => 'N', 'tipo' => 'alfa', 'required' => true ), - 'data_emissao' => array( + 'data_emissao' => array( // Posições 151-156 'tamanho' => 6, 'default' => '', - 'tipo' => 'date', + 'tipo' => 'alfa', 'required' => true ), - 'instrucao1' => array( + 'primeira_instrucao' => array( // Posições 157-158 'tamanho' => 2, 'default' => '00', 'tipo' => 'int', 'required' => true ), - 'instrucao2' => array( + 'segunda_instrucao' => array( // Posições 159-160 'tamanho' => 2, 'default' => '00', 'tipo' => 'int', 'required' => true ), - 'vlr_juros' => array( + 'juros_mora_dia' => array( // 13 dígitos 'tamanho' => 13, 'default' => '0', - 'tipo' => 'decimal', - 'precision' => 2, + 'tipo' => 'int', 'required' => true ), - 'data_desconto' => array( + 'data_limite_desconto' => array( // Posições 174-179 'tamanho' => 6, - 'default' => '0', - 'tipo' => 'date', + 'default' => '000000', + 'tipo' => 'alfa', 'required' => true ), - 'vlr_desconto' => array( + 'valor_desconto' => array( // Posições 180-192 (13 dígitos) 'tamanho' => 13, 'default' => '0', - 'tipo' => 'decimal', - 'precision' => 2, + 'tipo' => 'int', 'required' => true ), - 'vlr_iof' => array( + 'valor_iof' => array( // Posições 193-205 (13 dígitos) 'tamanho' => 13, 'default' => '0', - 'tipo' => 'decimal', - 'precision' => 2, + 'tipo' => 'int', 'required' => true ), - 'vlr_abatimento' => array( + 'valor_abatimento_multas' => array( // Posições 206-218 (13 dígitos) 'tamanho' => 13, 'default' => '0', - 'tipo' => 'decimal', - 'precision' => 2, + 'tipo' => 'int', 'required' => true ), - 'tipo_inscricao_pagador' => array( + 'tipo_inscricao_pagador' => array( // Posições 219-220 'tamanho' => 2, 'default' => '', 'tipo' => 'int', 'required' => true ), - 'numero_inscricao_pagador' => array( + 'numero_inscricao_pagador' => array( // Posições 221-234 (14 dígitos) 'tamanho' => 14, 'default' => '', 'tipo' => 'int', 'required' => true ), - 'nome_pagador' => array( + 'nome_pagador' => array( // Posições 235-274 (40 chars) 'tamanho' => 40, 'default' => '', 'tipo' => 'alfa', 'required' => true ), - 'endereco_pagador' => array( + 'endereco_pagador' => array( // Posições 275-314 'tamanho' => 40, 'default' => '', 'tipo' => 'alfa', 'required' => true ), - 'bairro_pagador' => array( - 'tamanho' => 12, + 'bairro_pagador' => array( // Posições 315-324 + 'tamanho' => 10, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'cep_pagador' => array( + 'filler4' => array( // Posições 325-326 + 'tamanho' => 2, + 'default' => ' ', + 'tipo' => 'alfa', + 'required' => true + ), + 'cep_pagador' => array( // Posições 327-334 'tamanho' => 8, 'default' => '', 'tipo' => 'int', 'required' => true ), - 'cidade_pagador' => array( + 'cidade_pagador' => array( // Posições 335-349 'tamanho' => 15, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'uf_pagador' => array( + 'uf_pagador' => array( // Posições 350-351 'tamanho' => 2, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'mensagem' => array( - 'tamanho' => 40, + 'nome_sacador_avalista' => array( // Posições 352-381 + 'tamanho' => 30, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'dias_protesto' => array( - 'tamanho' => 2, - 'default' => '00', - 'tipo' => 'int', + 'filler5' => array( // Posições 382-386 + 'tamanho' => 6, // Ajustado para totalizar 400 chars + 'default' => ' ', + 'tipo' => 'alfa', 'required' => true ), - 'filler3' => array( + 'indicador_tipo_desconto' => array( // Posições 387-388 'tamanho' => 1, - 'default' => ' ', + 'default' => '0', 'tipo' => 'alfa', 'required' => true ), - 'numero_sequencial' => array( + 'banco_emitente' => array( // Posições 389-391 + 'tamanho' => 3, + 'default' => '422', + 'tipo' => 'int', + 'required' => true + ), + 'sequencial_arquivo' => array( // Posições 392-394 + 'tamanho' => 3, + 'default' => '001', + 'tipo' => 'int', + 'required' => true + ), + 'sequencial_registro' => array( // Posições 395-400 'tamanho' => 6, - 'default' => '0', + 'default' => '000002', 'tipo' => 'int', 'required' => true ), @@ -336,30 +270,176 @@ class Registro1 extends Generico1 public function __construct($data = null) { - if (empty($this->data)) { - parent::__construct($data); + parent::__construct($data); + $this->set_numero_sequencial(null); + } + + protected function set_codigo_banco_cobrador($value) { + $this->data['codigo_banco_cobrador'] = '422'; + } + + protected function set_banco_emitente($value) { + $this->data['banco_emitente'] = '422'; + } + + protected function set_identificacao_carteira($value) + { + $value = strval($value); + if (!in_array($value, ['1', '2'])) { + $value = '1'; } - $this->inserirMensagem($data); + + $this->data['identificacao_carteira'] = $value; } - public function inserirMensagem($data) + protected function set_indicador_tipo_desconto($value) { - if (!empty($data['mensagem'])) { - $class = 'CnabPHP\resources\\B' . RemessaAbstract::$banco . '\remessa\\' . RemessaAbstract::$layout . '\Registro2'; - if (class_exists($class)) { - $this->children[] = new $class($data); + $validos = ['0', '1', '2', '3', '5']; + $value = strval($value); + + if (!in_array($value, $validos)) { + $value = '0'; + } + + $this->data['indicador_tipo_desconto'] = $value; + } + + protected function set_data_emissao($value) { + if ($value instanceof \DateTime) { + $this->data['data_emissao'] = $value->format('dmy'); + + return; + } + + if (empty($value)) { + if (isset($this->data['data_vencimento'])) { + $this->data['data_emissao'] = $this->data['data_vencimento']; + return; } } + + if (is_string($value) && preg_match('/^\d{6}$/', $value)) { + $this->data['data_emissao'] = $value; + return; + } + + if (is_string($value)) { + try { + $date = new \DateTime($value); + $this->data['data_emissao'] = $date->format('dmy'); + return; + } catch (\Exception $e) {} + } + } + + protected function set_data_vencimento($value) { + if (is_string($value) && preg_match('/^\d{6}$/', $value)) { + $this->data['data_vencimento'] = $value; + return; + } + + if ($value instanceof \DateTime) { + $this->data['data_vencimento'] = $value->format('dmy'); + return; + } + + if (is_string($value) && preg_match('/^\d{2}\/\d{2}\/\d{4}/', $value)) { + $date = \DateTime::createFromFormat('d/m/Y', $value); + if ($date) { + $this->data['data_vencimento'] = $date->format('dmy'); + return; + } + } + + if (is_string($value) && preg_match('/^\d{4}-\d{2}-\d{2}/', $value)) { + $date = \DateTime::createFromFormat('Y-m-d', $value); + if ($date) { + $this->data['data_vencimento'] = $date->format('dmy'); + return; + } + } + + if (is_numeric($value)) { + $date = new \DateTime(); + $date->setTimestamp($value); + $this->data['data_vencimento'] = $date->format('dmy'); + return; + } + } + + protected function set_numero_sequencial($value) { + if (!isset($this->data['numero_sequencial']) || $this->data['numero_sequencial'] == 0) { + static $contadorDetalhe = 2; + $this->data['numero_sequencial'] = $contadorDetalhe++; + } + } + + protected function set_sequencial_arquivo($value) { + $lote = RemessaAbstract::getLote(0); + if ($lote && isset($lote->data['numero_sequencial_arquivo'])) { + $headerSequencial = $lote->data['numero_sequencial_arquivo']; + $this->data['sequencial_arquivo'] = str_pad($headerSequencial, 3, '0', STR_PAD_LEFT); + } else { + $this->data['sequencial_arquivo'] = str_pad($value, 3, '0', STR_PAD_LEFT); + } } - protected function set_endereco_pagador($value) + protected function set_empresa_banco($value) { - $this->data['endereco_pagador'] = $value . " " . ($this->entryData['bairro_pagador'] ?? ''); + $lote = RemessaAbstract::getLote(0); + if ($lote && isset($lote->data['codigo_cedente'])) { + $codigoCedenteHeader = $lote->data['codigo_cedente']; + $this->data['empresa_banco'] = str_pad($codigoCedenteHeader, 14, '0', STR_PAD_LEFT); + } else { + $this->data['empresa_banco'] = str_pad($value, 14, '0', STR_PAD_LEFT); + } + } + + protected function set_sequencial_registro($value) { + $lote = RemessaAbstract::getLote(0); + $sequencial = 2; + + if ($lote && isset($lote->children)) { + $count = 0; + foreach ($lote->children as $child) { + if ($child !== $this && isset($child->data['tipo_registro']) && $child->data['tipo_registro'] == '1') { + $count++; + } + } + $sequencial = 2 + $count; + } + + $this->data['sequencial_registro'] = str_pad($sequencial, 6, '0', STR_PAD_LEFT); } - protected function set_percentual_multa($value) + protected function set_data_limite_desconto($value) { - $this->data['percentual_multa'] = $value; - $this->data['codigo_multa'] = ($value > 0) ? 1 : 0; + if ($value === '000000' || $value === '0' || empty($value)) { + $this->data['data_limite_desconto'] = '000000'; + return; + } + + if (preg_match('/^\d{6}$/', $value)) { + $this->data['data_limite_desconto'] = $value; + return; + } + + if (preg_match('/^\d{2}\/\d{2}\/\d{4}/', $value)) { + $date = \DateTime::createFromFormat('d/m/Y', $value); + if ($date) { + $this->data['data_limite_desconto'] = $date->format('dmy'); + return; + } + } + + if (preg_match('/^\d{4}-\d{2}-\d{2}/', $value)) { + $date = \DateTime::createFromFormat('Y-m-d', $value); + if ($date) { + $this->data['data_limite_desconto'] = $date->format('dmy'); + return; + } + } + + $this->data['data_limite_desconto'] = '000000'; } } diff --git a/src/resources/B422/remessa/cnab400/Registro9.php b/src/resources/B422/remessa/cnab400/Registro9.php index 3bb587e..cdda4f0 100644 --- a/src/resources/B422/remessa/cnab400/Registro9.php +++ b/src/resources/B422/remessa/cnab400/Registro9.php @@ -26,31 +26,111 @@ namespace CnabPHP\resources\B422\remessa\cnab400; -use CnabPHP\resources\generico\remessa\cnab400\Generico1; +use CnabPHP\RegistroRemAbstract; +use CnabPHP\RemessaAbstract; +use CnabPHP\resources\generico\remessa\cnab400\Generico9; /** * Classe Registro9 - Trailer do arquivo CNAB 400 do Banco Safra (422) */ -class Registro9 extends Generico1 +class Registro9 extends Generico9 { protected $meta = array( 'tipo_registro' => array( - 'tamanho' => 1, - 'default' => '9', - 'tipo' => 'int', - 'required' => true + 'tamanho' => 1, 'default' => '9', 'tipo' => 'int', 'required' => true ), - 'filler1' => array( - 'tamanho' => 393, - 'default' => ' ', - 'tipo' => 'alfa', - 'required' => true + 'brancos1' => array( + 'tamanho' => 367, 'default' => ' ', 'tipo' => 'alfa', 'required' => true ), - 'numero_sequencial' => array( - 'tamanho' => 6, - 'default' => '0', - 'tipo' => 'int', - 'required' => true + 'qtde_titulos' => array( + 'tamanho' => 8, 'default' => '00000000', 'tipo' => 'int', 'required' => true + ), + 'valor_total' => array( + 'tamanho' => 15, 'default' => '000000000000000', 'tipo' => 'int', 'required' => true + ), + 'sequencial_arquivo' => array( + 'tamanho' => 3, 'default' => '001', 'tipo' => 'int', 'required' => true + ), + 'sequencial_registro' => array( + 'tamanho' => 6, 'default' => '000003', 'tipo' => 'int', 'required' => true ), ); + + public function __construct($data = null) + { + parent::__construct($data); + } + + /** + * Override getText para calcular totalizadores antes da geração + */ + public function getText() + { + $this->set_qtde_titulos(null); + $this->set_valor_total(null); + $this->set_sequencial_arquivo(null); + $this->set_sequencial_registro(null); + + return parent::getText(); + } + + protected function set_qtde_titulos($value) + { + $lote = RemessaAbstract::getLote(0); + $qtde = 0; + + if ($lote && isset($lote->children) && is_array($lote->children)) { + foreach ($lote->children as $registro) { + if (isset($registro->data['tipo_registro']) && $registro->data['tipo_registro'] == '1') { + $qtde++; + } + } + } + + $this->data['qtde_titulos'] = str_pad($qtde, 8, '0', STR_PAD_LEFT); + } + + protected function set_valor_total($value) + { + $lote = RemessaAbstract::getLote(0); + $total = 0; + + if ($lote && isset($lote->children) && is_array($lote->children)) { + foreach ($lote->children as $registro) { + if (isset($registro->data['tipo_registro']) && $registro->data['tipo_registro'] == '1') { + $valor = isset($registro->data['valor']) ? (int)$registro->data['valor'] : 0; + $total += $valor; + } + } + } + + $this->data['valor_total'] = str_pad($total, 15, '0', STR_PAD_LEFT); + } + + protected function set_sequencial_registro($value) + { + $lote = RemessaAbstract::getLote(0); + $sequencial = 1; + + if ($lote && isset($lote->children)) { + foreach ($lote->children as $child) { + if (isset($child->data['tipo_registro']) && $child->data['tipo_registro'] == '1') { + $sequencial++; + } + } + } + + $sequencial++; + $this->data['sequencial_registro'] = str_pad($sequencial, 6, '0', STR_PAD_LEFT); + } + + protected function set_sequencial_arquivo($value) + { + $lote = RemessaAbstract::getLote(0); + if ($lote && isset($lote->data['numero_sequencial_arquivo'])) { + $this->data['sequencial_arquivo'] = substr($lote->data['numero_sequencial_arquivo'], 0, 3); + } else { + $this->data['sequencial_arquivo'] = '001'; + } + } }