public function post_finalizar($venda) { $sqlVenda = "INSERT INTO vendas (idCliente,idVendedor,dataVenda) VALUES (:idCliente,:idVendedor,:dataVenda)"; $sqlItemVenda = "INSERT INTO itensVenda (idVenda,idProduto,quantidade,precoUnitario) VALUES (:idVenda,:idProduto,:quantidade,:precoUnitario)"; try { DB::beginTransaction(); $stmtVendas = DB::prepare($sqlVenda); $stmtVendas->bindParam("idCliente", $venda->idCliente); $stmtVendas->bindParam("idVendedor", $venda->idVendedor); $stmtVendas->bindParam("dataVenda", DB::dateToMySql($venda->data)); $stmtVendas->execute(); $venda->idVenda = DB::lastInsertId(); foreach ($venda->itens as $item) { $stmtItem = DB::prepare($sqlItemVenda); $stmtItem->bindParam("idVenda", $venda->idVenda); $stmtItem->bindParam("idProduto", $item->idProduto); $stmtItem->bindParam("quantidade", $item->quantidade); $stmtItem->bindParam("precoUnitario", $item->preco); $stmtItem->execute(); } DB::commit(); } catch (Exception $exc) { DB::rollBack(); throw new Exception($exc->getMessage()); } return $venda; }
function post_adicionarAluno($aluno) { $sqlInsertEndereco = "INSERT INTO endereco (rua,bairro,numero,complemento,cep,cidade,uf) \n\t\tVALUES (:rua,:bairro,:numero,:complemento,:cep,:cidade,:uf)"; $sqlInsertResponsavel = "INSERT INTO responsavel (nome,cpf,telefone,celular,operadora,email,idEndereco) \n\t\tVALUES (:nome,:cpf,:telefone,:celular,:operadora,:email,:idEndereco)"; $sqlInsertAluno = "INSERT INTO aluno (nome,matricula,turma,serie,dataNascimento,sexo,telefone,celular,operadora,\n\t\t\tidResponsavel) \n\t\tVALUES (:nome,:matricula,:turma,:serie,:dataNascimento,:sexo,:telefone,:celular,:operadora,:idResponsavel)"; try { DB::beginTransaction(); $stmt = DB::prepare($sqlInsertEndereco); $stmt->bindParam("rua", $aluno->enderecoRua); $stmt->bindParam("bairro", $aluno->enderecoBairro); $stmt->bindParam("numero", $aluno->enderecoNumero); $stmt->bindParam("complemento", $aluno->enderecoComplemento); $stmt->bindParam("cep", $aluno->enderecoCep); $stmt->bindParam("cidade", $aluno->enderecoCidade); $stmt->bindParam("uf", $aluno->enderecoUf); $stmt->execute(); $aluno->idEndereco = DB::lastInsertId(); $stmt = DB::prepare($sqlInsertResponsavel); $stmt->bindParam("nome", $aluno->responsavelNome); $stmt->bindParam("cpf", $aluno->responsavelCpf); $stmt->bindParam("telefone", $aluno->responsavelTelefone); $stmt->bindParam("celular", $aluno->responsavelCelular); $stmt->bindParam("operadora", $aluno->responsavelOperadora); $stmt->bindParam("email", $aluno->responsavelEmail); $stmt->bindParam("idEndereco", $aluno->idEndereco); $stmt->execute(); $aluno->idResponsavel = DB::lastInsertId(); $stmt = DB::prepare($sqlInsertAluno); $stmt->bindParam("nome", $aluno->alunoNome); $stmt->bindParam("matricula", $aluno->alunoMatricula); $stmt->bindParam("turma", $aluno->alunoTurma); $stmt->bindParam("serie", $aluno->alunoSerie); $stmt->bindParam("dataNascimento", DB::dateToMySql($aluno->alunoDataNasc)); $stmt->bindParam("sexo", $aluno->alunoSexo); $stmt->bindParam("telefone", $aluno->alunoTelefone); $stmt->bindParam("celular", $aluno->alunoCelular); $stmt->bindParam("operadora", $aluno->alunoOperadora); $stmt->bindParam("idResponsavel", $aluno->idResponsavel); $stmt->execute(); DB::commit(); } catch (Exception $ex) { DB::rollBack(); throw new Exception($ex->getMessage()); } return $aluno; }
$stmt = DB::prepare($sql); $stmt->execute(); formatJson($stmt->fetchAll()); }); $app->get("/employee/:id", function ($id) { //DATE_FORMAT( `date` , '%d/%c/%Y %H:%i:%s' ) AS `date` $sql = "SELECT EmployeeID,FirstName,LastName,HomePhone,DATE_FORMAT(BirthDate,'%d/%c/%Y') as BirthDate FROM employees WHERE EmployeeID=?"; $stmt = DB::prepare($sql); $stmt->execute(array($id)); formatJson($stmt->fetch()); }); $app->post("/employee/", function () { $data = json_decode(\Slim\Slim::getInstance()->request()->getBody()); if ($data->EmployeeID != 0) { $sql = "UPDATE employees SET FirstName=?,LastName=?,HomePhone=?,BirthDate=? WHERE EmployeeID=?"; $stmt = DB::prepare($sql); $stmt->execute(array($data->FirstName, $data->LastName, $data->HomePhone, DB::dateToMySql($data->BirthDate), $data->EmployeeID)); } else { $sql = "INSERT INTO employees (FirstName,LastName,HomePhone,BirthDate) VALUES (?,?,?,?)"; $stmt = DB::prepare($sql); $stmt->execute(array($data->FirstName, $data->LastName, $data->HomePhone, DB::dateToMySql($data->BirthDate))); $data->EmployeeID = DB::lastInsertId(); } formatJson($data); }); $app->delete("/employee/:id", function ($id) { $sql = "DELETE FROM customers WHERE CustomerID=?"; $stmt = DB::prepare($sql); $stmt->execute(array($id)); formatJson(true); });
public function post_save($vendedor) { if ($vendedor->idVendedor) { //verifica se o login foi alterado, e se foi, deve verificar // se o login já existe para outro usuário $sqlUsuario = "SELECT id,login FROM usuarios WHERE id=:id"; $stmtUsuario = DB::prepare($sqlUsuario); $stmtUsuario->bindParam("id", $vendedor->idUsuario); $stmtUsuario->execute(); $usuario = $stmtUsuario->fetch(); if ($usuario->login != $vendedor->login) { $sqlSelect = "SELECT id,nome FROM usuarios WHERE (login=:login)"; $stmtSelect = DB::prepare($sqlSelect); $stmtSelect->bindValue("login", $vendedor->login); $stmtSelect->execute(); $usuario = $stmtSelect->fetch(); if ($usuario) { throw new Exception("Login pertencente ao usuário '{$usuario->nome}'"); } } //update $sqlUpdateUsuario = "UPDATE usuarios SET nome=:nome,email=:email,login=:login,senha=:senha WHERE id=:idUsuario"; $sqlUpdateVendedor = "UPDATE vendedores SET cpf=:cpf,matricula=:matricula,dataContratacao=:dataContratacao WHERE id=:idVendedor"; try { DB::beginTransaction(); $vendedor->dataContratacao = DB::dateToMySql($vendedor->dataContratacao); $stmtUsuario = DB::prepare($sqlUpdateUsuario); $stmtUsuario->bindParam("nome", $vendedor->nome); $stmtUsuario->bindParam("email", $vendedor->email); $stmtUsuario->bindParam("login", $vendedor->login); $stmtUsuario->bindParam("senha", $vendedor->senha); $stmtUsuario->bindParam("idUsuario", $vendedor->idUsuario); $stmtUsuario->execute(); $stmtUsuario = DB::prepare($sqlUpdateVendedor); $stmtUsuario->bindParam("cpf", $vendedor->cpf); $stmtUsuario->bindParam("matricula", $vendedor->matricula); $stmtUsuario->bindParam("dataContratacao", $vendedor->dataContratacao); $stmtUsuario->bindParam("idVendedor", $vendedor->idVendedor); $stmtUsuario->execute(); DB::commit(); } catch (Exception $exc) { DB::rollBack(); throw new Exception($exc->getMessage()); } } else { //Verificar se login já existem $sqlSelect = "SELECT id,nome FROM usuarios where (login=:login)"; $stmtSelect = DB::prepare($sqlSelect); $stmtSelect->bindValue("login", $vendedor->login); $stmtSelect->execute(); $usuario = $stmtSelect->fetch(); if ($usuario) { throw new Exception("Login pertencente ao usuário '{$usuario->nome}'"); } //insert $sqlInsertUsuario = "INSERT INTO usuarios (nome,email,login,senha,tipo) VALUES (:nome,:email,:login,:senha,:tipo)"; $sqlInsertVendedor = "INSERT INTO vendedores (cpf,matricula,ativo,idUsuario,dataContratacao) VALUES (:cpf,:matricula,:ativo,:idUsuario,:dataContratacao)"; try { DB::beginTransaction(); $vendedor->ativo = "1"; $vendedor->tipo = "v"; $vendedor->dataContratacao = DB::dateToMySql($vendedor->dataContratacao); $stmtUsuario = DB::prepare($sqlInsertUsuario); $stmtUsuario->bindParam("nome", $vendedor->nome); $stmtUsuario->bindParam("email", $vendedor->email); $stmtUsuario->bindParam("login", $vendedor->login); $stmtUsuario->bindParam("senha", $vendedor->senha); $stmtUsuario->bindParam("tipo", $vendedor->tipo); $stmtUsuario->execute(); $vendedor->idUsuario = DB::lastInsertId(); $stmtUsuario = DB::prepare($sqlInsertVendedor); $stmtUsuario->bindParam("cpf", $vendedor->cpf); $stmtUsuario->bindParam("matricula", $vendedor->matricula); $stmtUsuario->bindParam("ativo", $vendedor->ativo); $stmtUsuario->bindParam("idUsuario", $vendedor->idUsuario); $stmtUsuario->bindParam("dataContratacao", $vendedor->dataContratacao); $stmtUsuario->execute(); $vendedor->id = DB::lastInsertId(); DB::commit(); } catch (Exception $exc) { DB::rollBack(); throw new Exception($exc->getMessage()); } return $vendedor; } }