public static function updateFabricante() { $FabricanteTO = new FabricanteTO(); $FabricanteDao = new FabricanteDao(); $FabricanteTO->nome_fabricante = isset($_POST['nome_fabricante']) ? $_POST['nome_fabricante'] : ''; $FabricanteTO->id = isset($_POST['id']) ? $_POST['id'] : ''; $validator = new DataValidator(); $validator->set_msg('O nome do Fabricante é obrigatório')->set('nome_fabricante', $FabricanteTO->nome_fabricante)->is_required(); $validator->set_msg('O codigo do Fabricante é obrigatório')->set('id', $FabricanteTO->id)->is_required(); if (!$validator->validate()) { Flight::response()->status(406)->header('Content-Type', 'application/json')->write(json_encode($validator->get_errors()))->send(); return; } $empreendimentosAssociados = $_POST['empreendimentos']; if ($FabricanteDao->updateFabricante($FabricanteTO)) { if (!$FabricanteDao->removeEmpreendimentos($FabricanteTO->id)) { Flight::halt(500, 'Erro ao remover empreendimentos associados'); } else { foreach ($empreendimentosAssociados as $key => $empreendimento) { if (!$FabricanteDao->associarEmpreendimento($FabricanteTO->id, $empreendimento['id'])) { Flight::halt(500, 'Erro ao associar empreendimento'); } } } Flight::halt(200, ''); } else { Flight::halt(500, 'Erro ao atualizar dados'); } }
public static function requestSaveFabricante() { try { $fabricante = $_POST['fabricante']; $id_empreendimento = $_POST['id_empreendimento']; $FabricanteTO = new FabricanteTO(); $FabricanteDao = new FabricanteDao(); $ReferenciaIntegracaoDao = new ReferenciaIntegracaoDao(); $FabricanteTO->nome_fabricante = $fabricante['name']; $FabricanteTO->id_empreendimento = $id_empreendimento; $id = $FabricanteDao->saveFabricante($FabricanteTO); if (!$id) { throw new Exception('Erro ao criar fabricante', 1); } if (!$FabricanteDao->associarEmpreendimento($id, $id_empreendimento)) { throw new Exception('Erro ao associar empreendimento ao fabricante', 1); } $ReferenciaIntegracaoTO = new ReferenciaIntegracaoTO(); $ReferenciaIntegracaoTO->sistema_integrado = 'PrestaShop'; $ReferenciaIntegracaoTO->tabela = 'tbl_fabricante'; $ReferenciaIntegracaoTO->id_item = $id; $ReferenciaIntegracaoTO->id_item_referencia = $fabricante['id']; $ReferenciaIntegracaoTO->tipo = 'fabricante'; $ReferenciaIntegracaoTO->id_empreendimento = $id_empreendimento; if (!$ReferenciaIntegracaoDao->save($ReferenciaIntegracaoTO)) { throw new Exception('Erro ao criar referencia no sistema', 1); } } catch (Exception $e) { $log = new KLogger("logs/logErrorPrestaShop.txt", KLogger::DEBUG); $log->LogError($e->getMessage() . ' - line:' . $e->getLine()); $log->LogDebug(json_encode($_POST)); $log->LogJunp(); Flight::halt(500, $e->getMessage()); } }