Last active
September 20, 2019 14:18
-
-
Save FredAzevedo/c94c2f8aa8148f354f6b9296f47e10f5 to your computer and use it in GitHub Desktop.
ReciboList.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ReciboContaReceberFormBaixa Registration | |
* @author Fred Azevedo | |
*/ | |
class ReciboContaReceberFormBaixa extends TPage | |
{ | |
protected $form; | |
use Adianti\Base\AdiantiStandardFormTrait; // Standard form methods | |
function __construct() | |
{ | |
parent::__construct(); | |
$this->setDatabase('sample'); | |
$this->setActiveRecord('ContaReceber'); | |
//parent::setTitle('Gerar Recibo'); | |
//parent::setSize(0.7, 0.7); | |
// creates the form | |
$this->form = new BootstrapFormBuilder('form_ReciboContaReceberFormBaixa'); | |
$this->form->setFormTitle(''); | |
$this->form->setFieldSizes('100%'); | |
// create the form fields | |
$id = new TEntry('id'); | |
$cliente_id = new TDBUniqueSearch('cliente_id', 'sample', 'Cliente', 'id', 'nome_fantasia'); | |
$cliente_id->setEditable(FALSE); | |
$id_unit_session_conta_bancaria = new TCriteria(); | |
$id_unit_session_conta_bancaria->add(new TFilter('unit_id','=',TSession::getValue('userunitid'))); | |
$conta_bancaria_id = new TDBCombo('conta_bancaria_id', 'sample', 'ContaBancaria', 'id', '{banco->nome_banco} - AG: {agencia} - CC: {conta}','',$id_unit_session_conta_bancaria); | |
$conta_bancaria_id->addValidation('Conta Bancária', new TRequiredValidator); | |
$data_vencimento = new TDate('data_vencimento'); | |
$data_vencimento->addValidation('Data de Vencimento', new TRequiredValidator); | |
$data_vencimento->setDatabaseMask('yyyy-mm-dd'); | |
$data_vencimento->setMask('dd/mm/yyyy'); | |
$data_vencimento->setEditable(FALSE); | |
$data_conta = new TDate('data_conta'); | |
$data_conta->setValue(date('d/m/Y')); | |
$data_conta->addValidation('Competência', new TRequiredValidator); | |
$data_conta->setDatabaseMask('yyyy-mm-dd'); | |
$data_conta->setMask('dd/mm/yyyy'); | |
$data_conta->setEditable(FALSE); | |
$documento = new TEntry('documento'); | |
$documento->setEditable(FALSE); | |
$data_baixa = new TDate('data_baixa'); | |
$data_baixa->setValue(date('d/m/Y')); | |
$data_baixa->addValidation('Data da Baixa', new TRequiredValidator); | |
$data_baixa->setDatabaseMask('yyyy-mm-dd'); | |
$data_baixa->setMask('dd/mm/yyyy'); | |
$multa = new TNumeric('multa',2,',','.',true); | |
$multa->setValue(date('0.00')); | |
$multa->setEditable(FALSE); | |
$juros = new TNumeric('juros',2,',','.',true); | |
$juros->setEditable(FALSE); | |
$juros->setValue(date('0.00')); | |
$valor = new TNumeric('valor',2,',','.',true); | |
$valor->addValidation('Valor', new TRequiredValidator); | |
$valor->setEditable(FALSE); | |
$valor_pago = new TNumeric('valor_pago',2,',','.',true); | |
$valor_pago->addValidation('Valor Pago', new TRequiredValidator); | |
$valor_pago->setEditable(FALSE); | |
$desconto = new TNumeric('desconto',2,',','.',true); | |
$desconto->setValue(date('0.00')); | |
$desconto->setEditable(FALSE); | |
$observacao = new TText('observacao'); | |
$row = $this->form->addFields( [ new TLabel('Cliente'), $cliente_id ]); | |
$row->layout = ['col-sm-12']; | |
$row = $this->form->addFields( [ new TLabel('ID'), $id ], | |
[ new TLabel('Conta Bancária'), $conta_bancaria_id ]); | |
$row->layout = ['col-sm-2','col-sm-10']; | |
$row = $this->form->addFields( [ new TLabel('Vencimento'), $data_vencimento ], | |
[ new TLabel('Data da Conta'), $data_conta ], | |
[ new TLabel('Data da Baixa'), $data_baixa ], | |
[ new TLabel('Documento'), $documento ]); | |
$row->layout = ['col-sm-2','col-sm-2', 'col-sm-2', 'col-sm-6']; | |
$row = $this->form->addFields( [ new TLabel('Multa'), $multa ], | |
[ new TLabel('Juros'), $juros ], | |
[ new TLabel('Desconto'), $desconto ], | |
[ new TLabel('Valor da Conta'), $valor ], | |
[ new TLabel('Valor Pago'), $valor_pago ] | |
); | |
$row->layout = ['col-sm-2','col-sm-2', 'col-sm-2', 'col-sm-2','col-sm-2']; | |
if (!empty($id)) | |
{ | |
$id->setEditable(FALSE); | |
} | |
// create the form actions | |
$btn = $this->form->addAction('Baixar Recibo', new TAction([$this, 'onBaixar']), 'fa:floppy-o'); | |
$btn->class = 'btn btn-sm btn-primary'; | |
$this->form->addAction('Atualizar Valor', new TAction([$this, 'onAtualizar']), 'fa:eraser red'); | |
$this->form->addAction('Gerar Recibo', new TAction([$this, 'onGerarRecibo']), 'fa:eraser red'); | |
// vertical box container | |
$container = new TVBox; | |
$container->style = 'width: 100%'; | |
$container->add($this->form); | |
parent::add($container); | |
$multa->onBlur = 'calc()'; | |
$juros->onBlur = 'calc()'; | |
$desconto->onBlur = 'calc()'; | |
TScript::create('calc = function() { | |
let multa = convertToFloatNumber(form_ReciboContaReceberFormBaixa.multa.value); | |
let juros = convertToFloatNumber(form_ReciboContaReceberFormBaixa.juros.value); | |
let desconto = convertToFloatNumber(form_ReciboContaReceberFormBaixa.desconto.value); | |
let valor = convertToFloatNumber(form_ReciboContaReceberFormBaixa.valor.value); | |
valorTitulo = parseFloat(multa) + parseFloat(juros) - parseFloat(desconto); | |
let total = valor + valorTitulo; | |
total = formatMoney(total); | |
form_ReciboContaReceberFormBaixa.valor_pago.value = total; | |
}; | |
function formatMoney (number, decimal, separatord, separatort) { | |
var n = number, | |
c = isNaN(decimal = Math.abs(decimal)) ? 2 : decimal, | |
d = separatord == undefined ? "," : separatord, | |
t = separatort == undefined ? "." : separatort, | |
s = n < 0 ? "-" : "", | |
i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", | |
j = (j = i.length) > 3 ? j % 3 : 0; | |
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); | |
}; | |
function convertToFloatNumber(value) { | |
value = value.toString(); | |
if (value.indexOf(\'.\') !== -1 || value.indexOf(\',\') !== -1) { | |
if (value.indexOf(\'.\') > value.indexOf(\',\')) { | |
return parseFloat(value.replace(/,/gi,\'\')); | |
} else { | |
return parseFloat(value.replace(/\./gi,\'\').replace(/,/gi,\'.\')); | |
} | |
} else { | |
return parseFloat(value); | |
} | |
}; | |
'); | |
} | |
public function onGerarRecibo( $param ) | |
{ | |
$key = $param['id']; // get the parameter $key | |
TTransaction::open('sample'); // open a transaction | |
$object = new ContaReceber($key); | |
//$cliente_id = $param['cliente_id']; | |
var_dump($param); | |
$this->form->setData($object); // fill the form | |
TTransaction::close(); | |
} | |
public function onAtualizar( $param ) | |
{ | |
try | |
{ | |
TTransaction::open('sample'); // open a transaction | |
$key = $param['id']; // get the parameter $key | |
TTransaction::open('sample'); // open a transaction | |
$object = new ContaReceber($key); | |
if($object->data_vencimento < date('Y-m-d')) | |
{ | |
$pegarBanco = BoletoRegra::where('conta_bancaria_id', '=', $param['conta_bancaria_id'])->first(); | |
$multa = $pegarBanco->valor_multa / 100; | |
$valorReplace = str_replace(',','.', $param['valor']); | |
$valorMulta = $multa * $valorReplace; | |
//var_dump($valorMulta); | |
$valorJuros = $pegarBanco->valor_juros; | |
$valorJurosDia = $valorJuros / 30; | |
//diferença de dias em atraso | |
$datavencimento = $object->data_vencimento; | |
$datahoje = date('Y-m-d'); | |
$data1 = new DateTime($datavencimento); | |
$data2 = new DateTime($datahoje); | |
$diasAtrasados = $data1->diff ($data2); | |
//var_dump($diasAtrasados); | |
$valorJurosAtual = $valorJurosDia * $diasAtrasados->days; | |
$totalgeral = $valorMulta + $valorJurosAtual + $valorReplace; | |
parent::add(new TAlert('danger', 'Esta atrasado '.$diasAtrasados->y.' ano(s) '.$diasAtrasados->m.' mês(s) e '.$diasAtrasados->d.' dia(s) totalizando hoje '.date('d/m/Y').', '.$diasAtrasados->days.' dia(s) de atraso e o valor de R$ '.number_format($totalgeral, 2, ',', '.'))); | |
$object->multa = $valorMulta; | |
$object->juros = $valorJurosAtual; | |
$object->valor_pago = $totalgeral; | |
$object->store(); | |
}else{ | |
new TMessage('info', 'A esse título não esta vencido!'); | |
} | |
$this->form->setData($object); // fill the form | |
TTransaction::close(); | |
} | |
catch (Exception $e) // in case of exception | |
{ | |
new TMessage('error', $e->getMessage()); // shows the exception error message | |
$this->form->setData( $this->form->getData() ); // keep form data | |
TTransaction::rollback(); // undo all pending operations | |
} | |
} | |
public function onBaixar($param) | |
{ | |
$key = $param['id']; | |
$data = $this->form->getData(); | |
try | |
{ | |
TTransaction::open('sample'); // open a transaction | |
$object = new ContaReceber($key); | |
$object->data_vencimento = $data->data_vencimento; | |
$object->data_conta = $data->data_conta; | |
$object->data_baixa = $data->data_baixa; | |
$object->documento = $data->documento; | |
$object->conta_bancaria_id = $data->conta_bancaria_id; | |
$object->multa = $data->multa; | |
$object->juros = $data->juros; | |
$object->desconto = $data->desconto; | |
$object->valor = $data->valor; | |
$object->valor_pago = $data->valor_pago; | |
$object->baixa = 'N'; | |
$object->recibo = 'S'; | |
$object->store(); | |
TTransaction::close(); // close the transaction | |
$pos_action = new TAction(['ReciboList', 'onReload']); | |
new TMessage('info', 'Atualização de valor realizada com Sucesso!', $pos_action); | |
//self::closeWindow(); | |
} | |
catch (Exception $e) // in case of exception | |
{ | |
new TMessage('error', $e->getMessage()); // shows the exception error message | |
$this->form->setData( $this->form->getData() ); // keep form data | |
TTransaction::rollback(); // undo all pending operations | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment