Last active
September 9, 2018 21:56
-
-
Save FredAzevedo/26714c6397a86ade457d942948cc6b1c to your computer and use it in GitHub Desktop.
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 | |
/** | |
* OsForm Master/Detail | |
* @author <your name here> | |
*/ | |
class OsForm extends TPage | |
{ | |
protected $form; // form | |
protected $detail_list; | |
/** | |
* Page constructor | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
// creates the form | |
$this->form = new BootstrapFormBuilder('form_Os'); | |
$this->form->setFormTitle('Ordem de Serviços'); | |
$this->form->setFieldSizes('100%'); | |
// master fields | |
$id = new TEntry('id'); | |
$dataAbertura = new TDateTime('dataAbertura'); | |
$dataAbertura->setDatabaseMask('yyyy-mm-dd hh:ii'); | |
$dataAbertura->setMask('dd/mm/yyyy hh:ii'); | |
$dataPrevisao = new TDateTime('dataPrevisao'); | |
$dataPrevisao->setDatabaseMask('yyyy-mm-dd hh:ii'); | |
$dataPrevisao->setMask('dd/mm/yyyy hh:ii'); | |
$dataFechamento = new TDateTime('dataFechamento'); | |
$dataFechamento->setDatabaseMask('yyyy-mm-dd hh:ii'); | |
$dataFechamento->setMask('dd/mm/yyyy hh:ii'); | |
$oficina_id = new TDBUniqueSearch('oficina_id', 'sample', 'Oficina', 'id', 'nome'); | |
$veiculo_id = new TDBUniqueSearch('veiculo_id', 'sample', 'Veiculo', 'id', 'placa'); | |
$cliente_id = new TDBUniqueSearch('cliente_id', 'sample', 'User', 'id_user', 'nome'); | |
$consultor_id = new TDBUniqueSearch('consultor_id', 'sample', 'User', 'id_user', 'nome'); | |
$user_id = new TDBUniqueSearch('user_id', 'sample', 'User', 'id_user', 'nome'); | |
$situacao = new TEntry('situacao'); | |
$valor = new TEntry('valor'); | |
$observacao = new TEntry('observacao'); | |
// detail fields services | |
$detail_service_id = new THidden('detail_service_id'); | |
$detail_servico_id = new TDBUniqueSearch('detail_servico_id', 'sample', 'Servico', 'id', 'descricao'); | |
$detail_dataInicio = new TDateTime('detail_dataInicio'); | |
$detail_dataInicio->setDatabaseMask('yyyy-mm-dd hh:ii'); | |
$detail_dataInicio->setMask('dd/mm/yyyy hh:ii'); | |
$detail_dataTermino = new TDateTime('detail_dataTermino'); | |
$detail_dataTermino->setDatabaseMask('yyyy-mm-dd hh:ii'); | |
$detail_dataTermino->setMask('dd/mm/yyyy hh:ii'); | |
$detail_situacao = new TEntry('detail_situacao'); | |
// detail fields product | |
$detail_product_id = new THidden('detail_product_id'); | |
$detail_peca_id = new TDBUniqueSearch('detail_peca_id', 'sample', 'Peca', 'id', 'descricao'); | |
$detail_quantidade = new TEntry('detail_quantidade'); | |
$detail_precoUnitario = new TEntry('detail_precoUnitario'); | |
$detail_total = new TEntry('detail_total'); | |
if (!empty($id)) | |
{ | |
$id->setEditable(FALSE); | |
} | |
// master fields | |
$row = $this->form->addFields( [ new TLabel('Id'), $id ], | |
[ new TLabel('Data de Abertura'), $dataAbertura ], | |
[ new TLabel('Pevisão'), $dataPrevisao ], | |
[ new TLabel('Data de fechamento'), $dataFechamento ], | |
[ new TLabel('Oficina'), $oficina_id ] ); | |
$row->layout = ['col-sm-2', 'col-sm-2', 'col-sm-2', 'col-sm-2', 'col-sm-4' ]; | |
$row = $this->form->addFields( [ new TLabel('Veículo'), $veiculo_id ], | |
[ new TLabel('Cliente'), $cliente_id ], | |
[ new TLabel('Consultor'), $consultor_id ] ); | |
$row->layout = ['col-sm-4', 'col-sm-4', 'col-sm-4']; | |
$row = $this->form->addFields( [ new TLabel('User App'), $user_id ], | |
[ new TLabel('Situação'), $situacao ], | |
[ new TLabel('Valor'), $valor ] ); | |
$row->layout = ['col-sm-4', 'col-sm-4', 'col-sm-4']; | |
$row = $this->form->addFields( [ new TLabel('Observação'), $observacao ] ); | |
$row->layout = ['col-sm-4' ]; | |
//-------------------------------------------------------------------------------------------------------------------------- | |
// detail fields | |
$this->form->addContent( ['<h4>SERVIÇOS</h4><hr>'] ); | |
$this->form->addFields( [$detail_service_id] ); | |
$row = $this->form->addFields( [ new TLabel('Serviço'), $detail_servico_id ], | |
[ new TLabel('Data início'), $detail_dataInicio ], | |
[ new TLabel('Data termino'), $detail_dataTermino ], | |
[ new TLabel('Situação'), $detail_situacao ]); | |
$row->layout = ['col-sm-6', 'col-sm-2', 'col-sm-2', 'col-sm-2']; | |
$add = TButton::create('add', [$this, 'onSaveDetailServices'], 'Adicionar Serviço', 'fa:save'); | |
$this->form->addFields( [], [$add] )->style = 'background: whitesmoke; padding: 5px; margin: 1px;'; | |
$this->detail_list = new BootstrapDatagridWrapper(new TQuickGrid); | |
$this->detail_list->style = "min-width: 700px; width:100%;margin-bottom: 10px"; | |
$this->detail_list->setId('Os_list_service'); | |
// items | |
$this->detail_list->addQuickColumn('Serviço', 'descricao_servico', 'left', 100); | |
$this->detail_list->addQuickColumn('Data início', 'dataInicio', 'left', 50); | |
$this->detail_list->addQuickColumn('Data termino', 'dataTermino', 'left', 50); | |
$this->detail_list->addQuickColumn('Situação', 'situacao', 'left', 100); | |
// detail actions | |
$this->detail_list->addQuickAction( 'Edit', new TDataGridAction([$this, 'onEditDetailServices']), 'id', 'fa:edit blue'); | |
$this->detail_list->addQuickAction( 'Delete', new TDataGridAction([$this, 'onDeleteDetailServices']), 'id', 'fa:trash red'); | |
$this->detail_list->createModel(); | |
$panel = new TPanelGroup; | |
$panel->add($this->detail_list); | |
$panel->getBody()->style = 'overflow-x:auto'; | |
$this->form->addContent( [$panel] ); | |
//-------------------------------------------------------------------------------------------------------------------------- | |
// detail fields product | |
$this->form->addContent( ['<h4>PRODUTOS</h4><hr>'] ); | |
$this->form->addFields( [$detail_product_id] ); | |
$row = $this->form->addFields( [ new TLabel('Produto'), $detail_peca_id ], | |
[ new TLabel('Quantidade'), $detail_quantidade ], | |
[ new TLabel('Preço Unitário'), $detail_precoUnitario ], | |
[ new TLabel('Total'), $detail_total ]); | |
$row->layout = ['col-sm-6', 'col-sm-2', 'col-sm-2', 'col-sm-2']; | |
$add2 = TButton::create('add2', [$this, 'onSaveDetailProduct'], 'Adicionar Produto', 'fa:save'); | |
$this->form->addFields( [], [$add2] )->style = 'background: whitesmoke; padding: 5px; margin: 1px;'; | |
$this->detail_list_product = new BootstrapDatagridWrapper(new TQuickGrid); | |
$this->detail_list_product->style = "min-width: 700px; width:100%;margin-bottom: 10px"; | |
$this->detail_list_product->setId('Os_list_product'); | |
// items | |
$this->detail_list_product->addQuickColumn('Produto', 'descricao_produto', 'left', 100); | |
$this->detail_list_product->addQuickColumn('Quantidade', 'quantidade', 'left', 100); | |
$this->detail_list_product->addQuickColumn('Preço Unitário', 'precoUnitario', 'left', 100); | |
$this->detail_list_product->addQuickColumn('Total', 'total', 'left', 100); | |
// detail actions | |
$this->detail_list_product->addQuickAction( 'Edit', new TDataGridAction([$this, 'onEditDetailProduct']), 'id', 'fa:edit blue'); | |
$this->detail_list_product->addQuickAction( 'Delete', new TDataGridAction([$this, 'onDeleteDetailProduct']), 'id', 'fa:trash red'); | |
$this->detail_list_product->createModel(); | |
$panel_product = new TPanelGroup; | |
$panel_product->add($this->detail_list_product); | |
$panel_product->getBody()->style = 'overflow-x:auto'; | |
$this->form->addContent( [$panel_product] ); | |
//-------------------------------------------------------------------------------------------------------------------------- | |
$btn = $this->form->addAction( _t('Save'), new TAction([$this, 'onSave']), 'fa:save'); | |
$btn->class = 'btn btn-sm btn-primary'; | |
$this->form->addAction( _t('Clear'), new TAction([$this, 'onClear']), 'fa:eraser red'); | |
// create the page container | |
$container = new TVBox; | |
$container->style = 'width: 100%'; | |
// $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__)); | |
$container->add($this->form); | |
parent::add($container); | |
} | |
public function formatDate($detail_dataInicio, $object) | |
{ | |
$date = new DateTime($object->origin_date); | |
return $date->format($object->format); | |
} | |
public function onClear($param) | |
{ | |
$this->form->clear(TRUE); | |
TSession::setValue(__CLASS__.'_items_service', array()); | |
TSession::setValue(__CLASS__.'_items_product', array()); | |
$this->onReload( $param ); | |
} | |
public function onSaveDetailServices( $param ) | |
{ | |
try | |
{ | |
TTransaction::open('sample'); | |
TTransaction::setLogger(new TLoggerSTD()); | |
$data = $this->form->getData(); | |
$items = TSession::getValue(__CLASS__.'_items_service'); | |
$key = empty($data->detail_service_id) ? 'X'.mt_rand(1000000000, 1999999999) : $data->detail_service_id; | |
$nome_serv_obj = new Servico($data->detail_servico_id); | |
$items[ $key ] = array(); | |
$items[ $key ]['id'] = $key; | |
$items[ $key ]['servico_id'] = $data->detail_servico_id; | |
$items[ $key ]['descricao_servico'] = $nome_serv_obj->descricao; | |
/*$data->detail_dataInicio->setDatabaseMask('yyyy-mm-dd hh:ii'); | |
$data->detail_dataInicio->setMask('dd/mm/yyyy hh:ii');*/ | |
$items[ $key ]['dataInicio'] = $data->detail_dataInicio; | |
/*$data->detail_dataTermino->setDatabaseMask('yyyy-mm-dd hh:ii'); | |
$data->detail_dataTermino->setMask('dd/mm/yyyy hh:ii');*/ | |
$items[ $key ]['dataTermino'] = $data->detail_dataTermino; | |
$items[ $key ]['situacao'] = $data->detail_situacao; | |
TSession::setValue(__CLASS__.'_items_service', $items); | |
// clear detail form fields | |
$data->detail_service_id = ''; | |
$data->detail_servico_id = ''; | |
$data->detail_dataInicio = ''; | |
$data->detail_dataTermino = ''; | |
$data->detail_situacao = ''; | |
$nome_serv_obj->descricao = ''; | |
TTransaction::close(); | |
$this->form->setData($data); | |
$this->onReload( $param ); // reload the items | |
} | |
catch (Exception $e) | |
{ | |
$this->form->setData( $this->form->getData()); | |
new TMessage('error', $e->getMessage()); | |
} | |
} | |
public static function onEditDetailServices( $param ) | |
{ | |
// read session items | |
$items = TSession::getValue(__CLASS__.'_items_service'); | |
// get the session item | |
$item = $items[ $param['key'] ]; | |
$data = new stdClass; | |
$data->detail_service_id = $item['id']; | |
$data->detail_servico_id = $item['servico_id']; | |
$data->detail_dataInicio = $item['dataInicio']; | |
$data->detail_dataTermino = $item['dataTermino']; | |
$data->detail_situacao = $item['situacao']; | |
// fill detail fields | |
TForm::sendData( 'form_Os', $data ); | |
} | |
public static function onDeleteDetailServices( $param ) | |
{ | |
// reset items | |
$data = new stdClass; | |
$data->detail_service_id = ''; | |
$data->detail_servico_id = ''; | |
$data->detail_dataInicio = ''; | |
$data->detail_dataTermino = ''; | |
$data->detail_situacao = ''; | |
// clear form data | |
TForm::sendData('form_Os', $data ); | |
// read session items | |
$items = TSession::getValue(__CLASS__.'_items_service'); | |
// get detail id | |
$detail_id = $param['key']; | |
// delete the item from session | |
unset($items[ $detail_id ] ); | |
// rewrite session items | |
TSession::setValue(__CLASS__.'_items_service', $items); | |
// delete item from screen | |
TScript::create("ttable_remove_row_by_id('Os_list_service', '{$detail_id}')"); | |
} | |
//------------ | |
public function onSaveDetailProduct( $param ) | |
{ | |
try | |
{ | |
TTransaction::open('sample'); | |
TTransaction::setLogger(new TLoggerSTD()); | |
$data_product = $this->form->getData(); | |
$items_products = TSession::getValue(__CLASS__.'_items_product'); | |
$key = empty($data_product->detail_product_id) ? 'X'.mt_rand(1000000000, 1999999999) : $data_product->detail_product_id; | |
$nome_prod_obj = new Peca($data_product->detail_peca_id); | |
$items_products[ $key ] = array(); | |
$items_products[ $key ]['id'] = $key; | |
$items_products[ $key ]['peca_id'] = $data_product->detail_peca_id; | |
$items_products[ $key ]['descricao_produto'] = $nome_prod_obj->descricao; | |
$items_products[ $key ]['quantidade'] = $data_product->detail_quantidade; | |
$items_products[ $key ]['precoUnitario'] = $data_product->detail_precoUnitario; | |
$items_products[ $key ]['total'] = $data_product->detail_total; | |
TSession::setValue(__CLASS__.'_items_product', $items_products); | |
// clear detail form fields | |
$data_product->detail_product_id = ''; | |
$data_product->detail_peca_id = ''; | |
$data_product->detail_quantidade = ''; | |
$data_product->detail_precoUnitario = ''; | |
$data_product->detail_total = ''; | |
$nome_prod_obj->descricao = ''; | |
TTransaction::close(); | |
$this->form->setData($data_product); | |
$this->onReload( $param ); // reload the items | |
} | |
catch (Exception $e) | |
{ | |
$this->form->setData( $this->form->getData()); | |
new TMessage('error', $e->getMessage()); | |
} | |
} | |
public static function onEditDetailProduct( $param ) | |
{ | |
// read session items | |
$items_products = TSession::getValue(__CLASS__.'_items_product'); | |
// get the session item | |
$item_product = $items_products[ $param['key'] ]; | |
$data_product = new stdClass; | |
$data_product->detail_product_id = $item_product['id']; | |
$data_product->detail_peca_id = $item_product['peca_id']; | |
$data_product->detail_quantidade = $item_product['quantidade']; | |
$data_product->detail_precoUnitario = $item_product['precoUnitario']; | |
$data_product->detail_total = $item_product['total']; | |
// fill detail fields | |
TForm::sendData( 'form_Os', $data_product ); | |
} | |
public static function onDeleteDetailProduct( $param ) | |
{ | |
// reset items | |
$data_product = new stdClass; | |
$data_product->detail_product_id = ''; | |
$data_product->detail_peca_id = ''; | |
$data_product->detail_quantidade = ''; | |
$data_product->detail_precoUnitario = ''; | |
$data_product->detail_total = ''; | |
// clear form data | |
TForm::sendData('form_Os', $data_product ); | |
// read session items | |
$items_products = TSession::getValue(__CLASS__.'_items_product'); | |
// get detail id | |
$detail_id = $param['key']; | |
// delete the item from session | |
unset($items_products[ $detail_id ] ); | |
// rewrite session items | |
TSession::setValue(__CLASS__.'_items_product', $items_products); | |
// delete item from screen | |
TScript::create("ttable_remove_row_by_id('Os_list_product', '{$detail_id}')"); | |
} | |
//------------- | |
public function onReloadService($param) | |
{ | |
// read session items service | |
$items = TSession::getValue(__CLASS__.'_items_service'); | |
$this->detail_list->clear(); // clear detail list | |
if ($items) | |
{ | |
foreach ($items as $list_item) | |
{ | |
$item = (object) $list_item; | |
$row = $this->detail_list->addItem( $item ); | |
$row->id = $list_item['id']; | |
} | |
} | |
} | |
public function onReloadProduct($param) | |
{ | |
// read session items service | |
$items = TSession::getValue(__CLASS__.'_items_product'); | |
$this->detail_list_product->clear(); // clear detail list | |
if ($items) | |
{ | |
foreach ($items as $list_item) | |
{ | |
$item = (object) $list_item; | |
$row = $this->detail_list_product->addItem( $item ); | |
$row->id = $list_item['id']; | |
} | |
} | |
} | |
public function onReload($param) | |
{ | |
$this->onReloadService($param); | |
$this->onReloadProduct($param); | |
$this->loaded = TRUE; | |
} | |
public function onEdit($param) | |
{ | |
try | |
{ | |
TTransaction::open('sample'); | |
//TTransaction::setLogger(new TLoggerSTD()); | |
if (isset($param['key'])) | |
{ | |
$key = $param['key']; | |
$object = new Os($key); | |
$items_services = OsServicos::where('os_id', '=', $key)->load(); | |
$items_products = OsPecas::where('os_id', '=', $key)->load(); | |
$session_items_services = array(); | |
foreach( $items_services as $item_service ) | |
{ | |
$item_key = $item_service->id; | |
$session_items_services[$item_key] = $item_service->toArray(); | |
$session_items_services[$item_key]['id'] = $item_service->id; | |
$session_items_services[$item_key]['servico_id'] = $item_service->servico_id; | |
$nome_serv_obj = new Servico($item_service->servico_id); | |
$session_items_services[$item_key]['descricao_servico'] = $nome_serv_obj->descricao; | |
$session_items_services[$item_key]['dataInicio'] = $item_service->dataInicio; | |
$session_items_services[$item_key]['dataTermino'] = $item_service->dataTermino; | |
$session_items_services[$item_key]['situacao'] = $item_service->situacao; | |
} | |
TSession::setValue(__CLASS__.'_items_service', $session_items_services); | |
$session_items_products = array(); | |
foreach( $items_products as $item_product ) | |
{ | |
$item_key = $item_product->id; | |
$session_items_products[$item_key] = $item_product->toArray(); | |
$session_items_products[$item_key]['id'] = $item_product->id; | |
$nome_prod_obj = new Peca($item_product->peca_id); | |
$session_items_products[$item_key]['descricao_produto'] = $nome_prod_obj->descricao; | |
$session_items_products[$item_key]['peca_id'] = $item_product->peca_id; | |
$session_items_products[$item_key]['quantidade'] = $item_product->quantidade; | |
$session_items_products[$item_key]['precoUnitario'] = $item_product->precoUnitario; | |
$session_items_products[$item_key]['total'] = $item_product->total; | |
} | |
TSession::setValue(__CLASS__.'_items_product', $session_items_products); | |
$this->form->setData($object); // fill the form with the active record data | |
$this->onReload( $param ); // reload items list | |
TTransaction::close(); // close transaction | |
} | |
else | |
{ | |
$this->form->clear(TRUE); | |
TSession::setValue(__CLASS__.'_items_service', null); | |
TSession::setValue(__CLASS__.'_items_product', null); | |
$this->onReload( $param ); | |
} | |
} | |
catch (Exception $e) // in case of exception | |
{ | |
new TMessage('error', $e->getMessage()); | |
TTransaction::rollback(); | |
} | |
} | |
public function onSave() | |
{ | |
try | |
{ | |
// open a transaction with database | |
TTransaction::open('sample'); | |
$data = $this->form->getData(); | |
$master = new Os; | |
$master->fromArray( (array) $data); | |
$this->form->validate(); // form validation | |
$master->store(); // save master object | |
// delete details services | |
$old_items_services = OsServicos::where('os_id', '=', $master->id)->load(); | |
$keep_items_service = array(); | |
// get session items services | |
$items_services = TSession::getValue(__CLASS__.'_items_service'); | |
if( $items_services ) | |
{ | |
foreach( $items_services as $item_service ) | |
{ | |
if (substr($item_service['id'],0,1) == 'X' ) // new record | |
{ | |
$detail_service = new OsServicos; | |
} | |
else | |
{ | |
$detail_service = OsServicos::find($item_service['id']); | |
} | |
$detail_service->servico_id = $item_service['servico_id']; | |
$detail_service->dataInicio = $item_service['dataInicio']; | |
$detail_service->dataTermino = $item_service['dataTermino']; | |
$detail_service->situacao = $item_service['situacao']; | |
$detail_service->os_id = $master->id; | |
$detail_service->store(); | |
$keep_items_service[] = $detail_service->id; | |
} | |
} | |
if ($old_items_services) | |
{ | |
foreach ($old_items_services as $old_item__service) | |
{ | |
if (!in_array( $old_item__service->id, $keep_items_service)) | |
{ | |
$old_item__service->delete(); | |
} | |
} | |
} | |
// delete details products | |
$old_items_products = OsPecas::where('os_id', '=', $master->id)->load(); | |
$keep_items_products = array(); | |
// get session items | |
$items_products = TSession::getValue(__CLASS__.'_items_product'); | |
if( $items_products ) | |
{ | |
foreach( $items_products as $item_product ) | |
{ | |
if (substr($item_product['id'],0,1) == 'X' ) // new record | |
{ | |
$detail_product = new OsPecas; | |
} | |
else | |
{ | |
$detail_product = OsPecas::find($item_product['id']); | |
} | |
$detail_product->peca_id = $item_product['peca_id']; | |
$detail_product->quantidade = $item_product['quantidade']; | |
$detail_product->precoUnitario = $item_product['precoUnitario']; | |
$detail_product->total = $item_product['total']; | |
$detail_product->os_id = $master->id; | |
$detail_product->store(); | |
$keep_items_products[] = $detail_product->id; | |
} | |
} | |
if ($old_items_products) | |
{ | |
foreach ($old_items_products as $old_item_product) | |
{ | |
if (!in_array( $old_item_product->id, $keep_items_products)) | |
{ | |
$old_item_product->delete(); | |
} | |
} | |
} | |
TTransaction::close(); // close the transaction | |
// reload form and session items | |
$this->onEdit(array('key'=>$master->id)); | |
new TMessage('info', TAdiantiCoreTranslator::translate('Record saved')); | |
} | |
catch (Exception $e) // in case of exception | |
{ | |
new TMessage('error', $e->getMessage()); | |
$this->form->setData( $this->form->getData() ); // keep form data | |
TTransaction::rollback(); | |
} | |
} | |
public function show() | |
{ | |
// check if the datagrid is already loaded | |
if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') ) | |
{ | |
$this->onReload( func_get_arg(0) ); | |
} | |
parent::show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment