Advertisement
LeoMonte

ichiba

Feb 13th, 2018
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 65.96 KB | None | 0 0
  1. CREATE SCHEMA IchibaSuperMarket;
  2. USE IchibaSuperMarket;
  3.  
  4. SET foreign_key_checks = 0;
  5.  
  6.  
  7. -- TABELA DE CURSOS
  8.  
  9. CREATE TABLE curso (
  10.  
  11.     id char(4),
  12.     cargahoraria integer(4) not null CHECK ( cargahoraria > 0 ),
  13.     descricao varchar(40),
  14.     primary key(id)
  15. );
  16.  
  17. -- inserindo valores para curso
  18.  
  19. INSERT INTO curso VALUES
  20.  
  21. ( '7070', 12 , 'entregas'),
  22. ( '1010' , 20, 'libras'),
  23. ( '2276' , 5 , 'marketing'),
  24. ( '9085' , 13 , 'informatica'),
  25. ( '7324' , 7 , 'financas'),
  26. ( '8012'  , 60 , 'ingles'),
  27. ( '3390', 30 , 'relacoes humanas'),
  28. ( '8344' , 30 , 'estatistica'),
  29. ( '5793' , 40 , 'circuitos'),
  30. ( '3333' , 20 , 'ginastica laboral'),
  31. ( '5449', 40 , 'mecanica'),
  32. ( '7573' , 20, 'administracao'),
  33. ( '9090' , 40 , 'programacao'),
  34. ( '2101' , 40 , 'espanhol');
  35.  
  36. -- TABELA DE JORNADA DE TRABALHO
  37.  
  38. CREATE TABLE jornadatrabalho (
  39.    
  40.     id char(4) ,
  41.     descricao varchar(60),
  42.     trabalha_sabado boolean not null default true,
  43.     primary key(id)
  44.  
  45. );
  46.  
  47. -- inserindo valores na tabela jornadatrabalho
  48.  
  49. INSERT INTO jornadatrabalho ( id , descricao , trabalha_sabado) VALUES
  50.  
  51. ('0001' , 'segunda a domingo' , true),
  52. ('0002' , NULL , false),
  53. ('0003' , 'trabalha feriados' , true),
  54. ('0004' , ' ' , true ),
  55. ('0005' , ' ' , false),
  56. ('0006' , ' ', false),
  57. ('0007' , 'trabalha feriados' , true),
  58. ('0008' , ' ' , true),
  59. ('0009' , ' ' , true),
  60. ('0010' , ' ', true ),
  61. ('0011' , ' ', true),
  62. ('0012', ' ', true),
  63. ('0013' , ' ' , true),
  64. ('0014', ' ' , true),
  65. ('0015' , ' ', true ),
  66. ('0016'  , ' ' , false);
  67.  
  68.  
  69. -- TABELA DE TURNO
  70.  
  71. CREATE TABLE turno (
  72.  
  73.     cod char(4),
  74.     descricao varchar(60),
  75.     hora_inicio time not null,
  76.     hora_fim time not null,
  77.     primary key (cod)
  78.  
  79. );
  80.  
  81. -- inserindo valores na tabela de turno
  82.  
  83. INSERT INTO turno ( cod, descricao , hora_inicio, hora_fim ) VALUES
  84.  
  85. ('0001' , ' ' , '07:30:00' , '16:30:00'),
  86. ('0002' , ' ', '08:40:00' , '17:40:00'),
  87. ('0003' , ' ' , '07:00:00' , '14:00:00'),
  88. ('0004' , ' ' , '11:00:00' , '17:00:00'),
  89. ('0005' , ' ' , '14:00:00' , '18:30:00'),
  90. ('0006' , ' ' , '13:35:00' , '19:45:00'),
  91. ('0007' , ' ' , '07:30:00' , '12:30:00'),
  92. ('0008' , ' ' , '07:10:00' , '13:30:00'),
  93. ('0009' , ' ' , '18:00:00' , '23:55:00'),
  94. ('0010' , ' ' , '00:00:01' , '08:00:01');
  95.  
  96. -- TABELA DE DIA
  97.  
  98. CREATE TABLE dia (
  99.  
  100.     sequencial char(4),
  101.     descricao varchar(60),
  102.     primary key (sequencial)
  103.    
  104. );
  105.  
  106. -- inserindo valores na tabela de dia
  107.  
  108. INSERT INTO dia ( sequencial , descricao ) VALUES
  109.  
  110. ('0001' , ' segunda-feira' ),
  111. ('0002' , ' terça-feira' ),
  112. ('0003' , ' quarta-feira' ),
  113. ('0004' , ' quinta-feira' ),
  114. ('0005' , ' sexta-feira' ),
  115. ('0006' , ' sabado' ),
  116. ('0007' , ' domingo' );
  117.  
  118. -- TABELA DA MATRIZ
  119.  
  120. CREATE TABLE matriz (
  121.    
  122.     CNPJ char(14),
  123.     nomefantasia varchar(10) not null,
  124.     primary key (CNPJ)
  125.  
  126. );
  127.  
  128. -- inserindo valores na tabela Matriz
  129.  
  130. INSERT INTO matriz ( CNPJ , nomefantasia ) VALUES
  131.  
  132. ( '23416393000114' , 'ICBSP'),
  133. ( '23416393000140' , 'ICBPE'),
  134. ( '23416393000169' , 'ICBRJ');
  135.  
  136.  
  137. -- TABELA DE TELEFONE DA MATRIZ
  138.  
  139.  
  140. CREATE TABLE telefone_matriz (
  141.      
  142.     CNPJ char(14),
  143.     telefone char(10),
  144.     CONSTRAINT telefone_matriz_pk  primary key (CNPJ, telefone),
  145.     CONSTRAINT fk_cnpjmatriz foreign key (CNPJ) references matriz (CNPJ) ON DELETE CASCADE ON UPDATE CASCADE
  146.  
  147. );
  148.  
  149. -- inserindo valores na tabela telefone_matriz
  150.  
  151. INSERT INTO telefone_matriz (CNPJ , telefone ) VALUES
  152.  
  153. ('23416393000114' , '8133002432'),
  154. ('23416393000140' , '8133002149'),
  155. ('23416393000140' , '1133002932'),
  156. ('23416393000140' , '1140443949'),
  157. ('23416393000169' , '1433004732'),
  158. ('23416393000169' , '1440002449');
  159.  
  160. -- TABELA DE FILIAL
  161.  
  162. CREATE TABLE filial (
  163.  
  164.     seq char(4),
  165.     CNPJ_Matriz char(14) not null,
  166.     CPF_gerente char(11) ,
  167.     endereco varchar(50),
  168.     qtd_func int(4) CHECK (qtd_func >= 0),
  169.     CONSTRAINT filial_pk primary key ( seq , CNPJ_Matriz ),
  170.     CONSTRAINT fk_cnpjmatrizfilial foreign key ( CNPJ_MATRIZ ) references matriz (CNPJ) ON DELETE CASCADE ON UPDATE CASCADE
  171.    
  172. );
  173.  
  174.  
  175. -- inserindo valores na tabela filial
  176.  
  177. INSERT INTO filial ( seq , CNPJ_Matriz , CPF_gerente , endereco , qtd_func ) VALUES
  178.  
  179. ( '0001' ,  '23416393000114' , NULL , 'Rua valtavares ' , 4 ),
  180. ( '0002' ,  '23416393000114' , NULL , 'Rua alivetania ' , 4 ),
  181. ( '0003' ,  '23416393000140' , NULL, 'Rua maranguape ' , 4 ),
  182. ( '0004' ,  '23416393000169' , NULL , 'Rua fernigan ' , 4 );
  183.  
  184. -- TABELA DE TELEFONE DA FILIAL
  185.  
  186. CREATE TABLE telefone_filial (
  187.  
  188.     seq_filial char(4),
  189.     CNPJ_Matriz char(14),
  190.     telefone char(10),
  191.     CONSTRAINT telefone_filial_pk primary key (seq_filial , CNPJ_Matriz , telefone ),
  192.     CONSTRAINT fk_cnpj foreign key (seq_filial, CNPJ_Matriz) references filial (seq , CNPJ_Matriz) ON DELETE CASCADE ON UPDATE CASCADE
  193.  
  194. );
  195.  
  196. -- inserindo valores na tabela telefone_filial
  197.  
  198. INSERT INTO telefone_filial( seq_filial , CNPJ_Matriz , telefone ) VALUES
  199.  
  200. ( '0001' ,  '23416393000114' , '1133115021' ),
  201. ( '0002' ,  '23416393000114' , '1133505231' ),
  202. ( '0003' ,  '23416393000140' , '8143022151' ),
  203. ( '0004' ,  '23416393000169' , '1431205412' );
  204.  
  205.  
  206. -- TABELA DE FUNCIONARIOS
  207.  
  208.  
  209. CREATE TABLE funcionario (
  210.  
  211.     CPF char(11) ,
  212.     id_jornada char(4) ,
  213.     seq_filial char(4),
  214.     cnpj_matriz char(14),
  215.     data_admissao date not null,
  216.     sex enum ('M', 'F'),
  217.     estado_civil varchar(10),
  218.     login varchar(60) default 'func' ,
  219.     senha varchar(15) default 'func',
  220.     RG char(7) not null UNIQUE,
  221.     nome varchar(45),
  222.     situacao varchar(10),
  223.     endereco varchar(45),
  224.     primary key(CPF),
  225.     foreign key (id_jornada) references jornadatrabalho (id) ,
  226.     foreign key ( seq_filial ) references filial (seq),
  227.     foreign key (cnpj_matriz) references matriz (CNPJ)
  228.  
  229. );
  230.  
  231.  
  232. -- inserindo valores para funcionario
  233.  
  234. INSERT INTO funcionario (CPF, id_jornada, seq_filial, cnpj_matriz, data_admissao, sex, estado_civil, login, senha, RG, nome, situacao, endereco) VALUES
  235.  
  236. ('77491222226' , '0001', '0001' ,  '23416393000114' , '2005-04-12' , 'F', 'solteiro' , 'helo.12' , '123' ,  '1259312' , 'Heloisa Macedo de Souza' , 'ativo' , 'Rua Eloy Monteiro Nunes'),
  237. ('98243208909' , '0002' , '0001' ,'23416393000114' , '2007-07-25' , 'M' , 'casado' , 'Garza2019' , 'E3221' ,'3654296' , 'Elias ChateauBriand Gomes' , 'inativo' , 'Avenida Paraíba'),
  238. ('57325297050' , '0003' , '0003' , '23416393000140', '2003-09-02', 'F', 'viuva' , 'Riggs642' , 'YM' ,       '4563573' , 'Maria Helena Rosendo' , 'ativo', 'Rua Pedro Viana Neto'),
  239. ('16565525749' , '0004' , '0002' , '23416393000114' , '2000-01-07', 'F', 'solteiro' , 'Eugene759' , 'XX133','2144770', 'Afrodite Bezerra das Flores' , 'ativo', 'Rua Amelia'),
  240. ('57859332507', '0005' , '0002' ,  '23416393000114' , '2006-02-02' , 'M' , 'solteiro' , 'TM1' , '98UJ' ,    '1555582' , 'Mauricio de Souza Carvalho' , 'ativo' , ' Rua da Concordia'),
  241. ('96202875763' , '0006' , '0003' , '23416393000140' , '2001-09-11' , 'F' , 'solteiro' ,'Juli.Alves', '8900','3494135' , 'Juliana Macedo Pinheiro' , 'inativo' , 'Rua Tucano'),
  242. ('02123011878' , '0007' , '0004' , '23416393000169' , '2000-08-10' , 'M' , 'solteiro' , 'ana.mari' , 'bb34','1783833' , 'Mariana Siqueira Jardim' , 'ativo' , 'Rua Lealberto Leal'),
  243. ('15141182894' , '0008' , '0001' , '23416393000114' , '2005-06-13' , 'F' , 'casado' , 'dudu.arda' , '1999' ,'2267700', 'Bernadete Maria da Silva' , 'ativo' , 'Rua Guajuvira'),
  244. ('33666472214' , '0009' , '0004' , '23416393000169' , '2011-03-01' , 'F' , 'solteiro', 'ana.belle' , 'AX6', '4137799','Anabelle Cristina Leal de Figueiredo' , 'ativo', 'Rua Manuel de Medeiros'),
  245. ('32568071001', '0010' , '0001' ,  '23416393000114' , '2010-09-26' , 'F' , 'casado' , 'bee.a' , '134N' ,    '4163131', 'Ana Beatriz Castanho Guedes' , 'ativo', 'Rua Jornalista Benedito Cunha'),
  246. ('45321186898' , '0011', '0004',   '23416393000169' , '2007-09-20', 'M' , 'solteiro' , 'alan.marq' , '0j7e','1557345','Allan Jose Malta de Souza' , 'inativo', ' Rua Projetada'),
  247. ('86147207504' , '0012' , '0004' , '23416393000169' , '2001-10-27' , 'F' , 'casado' , 'lelezinha' , '223d' ,'3217467', 'Leticia Santana Rodrigues' , 'ativo' , 'Rua Felipe Guerra'),
  248. ('85902755239' , '0013' , '0003' , '23416393000140' , '2005-11-14' , 'F' , 'solteiro' , 'mwd2', '1332',     '2437550', 'Gabriela Amado Batista', 'ativo', 'Rua da Palma'),
  249. ('14073416260' , '0014' , '0002' , '23416393000114', '2000-02-14', 'M' , 'casado', 'mumu321', '9901',       '1190089','Jadiane Matoso dos Santos', 'ativo', 'Rua Real da Torre'),
  250. ('32970753502', '0015' , '0002' ,  '23416393000114' , '2013-01-10', 'F' , 'solteiro' , 'JH10' , '1132' ,    '2422874', 'Viviane Mendonca do Nascimento', 'ativo', 'Rua da Harmonia'),
  251. ('88356795591' , '0016' , '0003' , '23416393000140' , '2012-06-10', 'M' , 'casado', '991jj', '1233',        '1316411', 'Ronaldo Fagundes da Silva' , 'ativo', 'Rua da Praia');
  252.  
  253.  
  254. -- TABELA DE ESTOQUISTA HERDA FUNCIONARIO
  255.  
  256.  
  257. CREATE TABLE estoquista (
  258.    
  259.     CPF char(11),
  260.     primary key (CPF),
  261.     CONSTRAINT fk_estoq foreign key (CPF) references funcionario (CPF) ON DELETE CASCADE ON UPDATE CASCADE
  262.  
  263. );
  264.  
  265.  -- inserindo valores para estoquista
  266.  
  267. INSERT INTO estoquista (CPF) VALUES
  268.  
  269. ('77491222226'),
  270. ('98243208909'),
  271. ('57859332507'),
  272. ('16565525749');
  273.  
  274. -- TABELA DE DBA HERDA FUNCIONARIO
  275.  
  276. CREATE TABLE DBA (
  277.    
  278.     CPF char(11),
  279.     primary key (CPF),
  280.     CONSTRAINT fk_dba foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  281.  
  282. );
  283.  -- inserindo valores para dba
  284.  
  285. INSERT INTO DBA (CPF) VALUES
  286.  
  287. ('45321186898');
  288.  
  289. -- TABELA DE GERENTE HERDA FUNCIONARIO
  290.  
  291. CREATE TABLE gerente(
  292.    
  293.     CPF char(11),
  294.     primary key (CPF),
  295.     CONSTRAINT fk_gerente foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  296.  
  297. );
  298.  
  299. -- inserindo valores para gerente
  300.  
  301. INSERT INTO gerente (CPF) VALUES
  302.  
  303. ('96202875763'),
  304. ('02123011878'),
  305. ('15141182894'),
  306. ('57325297050');
  307.  
  308. -- TABELA DE ENTREGADOR HERDA FUNCIONARIO
  309.  
  310. CREATE TABLE entregador(
  311.    
  312.     CPF char(11),
  313.     primary key (CPF),
  314.     CONSTRAINT fk_entregador foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  315.  
  316. );
  317.  
  318. -- inserindo valores na tabela entregador
  319.  
  320. INSERT INTO entregador (CPF) VALUES
  321.  
  322. ( '33666472214'),
  323. ('86147207504'),
  324. ('32970753502'),
  325. ('32568071001');
  326.  
  327. -- TABELA DE SUPERVISOR ESTOQUE HERDA FUNCIONARIO
  328.  
  329. CREATE TABLE supervisorestoque (
  330.    
  331.     CPF char(11),
  332.     primary key(CPF),
  333.     CONSTRAINT fk_super foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  334.  
  335. );
  336.  
  337. -- inserindo valotes na tabela supervisorestoque
  338.  
  339. INSERT INTO supervisorestoque (CPF) VALUES
  340.  
  341. ('14073416260'),
  342. ('85902755239' ),
  343. ('88356795591');
  344.  
  345.  
  346. -- TABELA MULTVALORADA DE FUNCIONARIO
  347.  
  348.  
  349. CREATE TABLE telefone_funcionario(
  350.    
  351.     CPF char(11),
  352.     telefone char(11),
  353.     primary key (CPF, telefone),
  354.     CONSTRAINT fk_telefonefuncinario foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  355.  
  356. );
  357.  
  358. -- inserindo valores na tabela de telefone_funcionario
  359.  
  360. INSERT INTO telefone_funcionario ( CPF, telefone ) VALUES
  361.  
  362. ('14073416260' , '08133552321'),
  363. ('85902755239' , '08132324567'),
  364. ('88356795591', '08131311111'),
  365. ('32568071001', '08134587831'),
  366. ('32970753502', '08132324502'),
  367. ('32970753502' , '08191912343'),
  368. ('33666472214' , '08199096532'),
  369. ('86147207504', '08132732100'),
  370. ('96202875763' , '08199690359');
  371.  
  372.  
  373. -- alterando a tabela filial
  374.  
  375. ALTER TABLE filial add constraint foreign key ( CPF_gerente ) references gerente (CPF);
  376.  
  377.  
  378. -- TABELA DE NOTIFICAÇÃO DADA A FUNCIONARIO
  379.  
  380. CREATE TABLE notificacao (
  381.    
  382.     id char(4),
  383.     cpf_fun char(11),
  384.     dia date not null,
  385.     descricao varchar(80),
  386.     primary key (id),
  387.     foreign key (Cpf_fun) references funcionario (CPF)
  388.    
  389. );
  390.  
  391. -- inserindo valores na tabela de notificacao
  392.  
  393. INSERT INTO notificacao (id, cpf_fun, dia, descricao) VALUES
  394.  
  395. ('2940' , '33666472214' , '2014-02-26' , NULL),
  396. ('2941' , '86147207504' , '2015-03-06' , NULL),
  397. ('2942' , '32970753502' , '2015-06-02' , NULL),
  398. ('2943' , '33666472214' , '2014-07-20' , NULL),  
  399. ('2944' , '33666472214' , '2014-07-20' , NULL),
  400. ('2945' , '16565525749' , '2014-02-12' , NULL),
  401. ('2946' , '33666472214' , '2015-03-06' , NULL),
  402. ('2947' , '33666472214' , '2015-06-02' , NULL),
  403. ('2948' , '77491222226' , '2014-07-20' , NULL),
  404. ('2949' , '88356795591' , '2014-07-20' , NULL),
  405. ('2950' , '45321186898' , '2014-07-20' , NULL),
  406. ('2951' , '86147207504' , '2014-02-10' , NULL);
  407.  
  408. -- TABELA DE NOTIFICAÇÃO DE MULTA DADA A FUNCIONARIO HERDA DE NOTIFICAÇÃO
  409.  
  410. CREATE TABLE notif_multa (
  411.  
  412.     id char(4),
  413.     pontos_cnh int,
  414.     valor float not null,
  415.     cep char(8),
  416.     complemento varchar(30),
  417.     primary key(id),
  418.     CONSTRAINT fk_notmulta foreign key (id) references notificacao(id) ON DELETE CASCADE ON UPDATE CASCADE
  419.    
  420. );
  421.  
  422. -- inserindo dados para notif_multa
  423.  
  424. INSERT INTO notif_multa (id , pontos_cnh, valor, cep, complemento) VALUES
  425.  
  426. ('2940',  3 , 58.50 , 50789123 , 'Rua da Aurora'),
  427. ('2941' ,4 , 198.50 , 54330315 , 'Rua Itacuruba'),
  428. ('2942' , 5 , 398.50 , 54315330 , 'Rua verdejantes');
  429.  
  430. -- TABELA DE NOTIFICAÇÃO ADVERTENCIA DADA A FUNCIONARIO HERDA DE NOTIFICAÇÃO
  431.  
  432. CREATE TABLE notif_advertencia (
  433.  
  434.     id char(4),
  435.     descricao varchar(20),
  436.     primary key(id),
  437.     CONSTRAINT fk_notadvert foreign key (id) references notificacao (id) ON DELETE CASCADE ON UPDATE CASCADE
  438. );
  439.  
  440. -- inserindo dados para notif_advertencia
  441.  
  442. INSERT INTO notif_advertencia (id, descricao ) VALUES
  443.  
  444. ('2943' , NULL),
  445. ('2944' , NULL),
  446. ('2945' , NULL);
  447.  
  448. -- TABELA DE NOTIFICAÇÃO DE SUSPENSAO DADA A FUNCIONARIO HERDA DE NOTIFICAÇÃO
  449.  
  450. CREATE TABLE notif_suspensao (
  451.    
  452.     id char(4),
  453.     data_inicio date not null,
  454.     data_termino date not null,
  455.     primary key(id),
  456.     CONSTRAINT not_suspen foreign key (id) references notificacao(id) ON DELETE CASCADE ON UPDATE CASCADE
  457. );
  458.  
  459. -- inseindo dados para notif_suspensao
  460.  
  461. INSERT INTO notif_suspensao ( id , data_inicio , data_termino ) VALUES
  462.  
  463. ('2946' , '2015-06-02' , '2015-06-04' ),
  464. ('2947' , '2015-08-20' , '2015-08-23'  ),
  465. ('2948' , '2015-09-10' , '2015-09-13'  );
  466.  
  467.  
  468. -- TABELA DE NOTIFICAÇÃO DE FALTA DADA A FUNCIONARIO HERDA DE NOTIFICAÇÃO
  469.  
  470.  
  471. CREATE TABLE notif_falta (
  472.    
  473.     id char(4),
  474.     dia date,
  475.     duracao int CHECK (duracao > 0 ),
  476.     primary key (id),
  477.     CONSTRAINT fk_notfalta foreign key (id) references notificacao (id) ON DELETE CASCADE ON UPDATE CASCADE
  478. );
  479.  
  480.  -- inserindo valores para notif_falta
  481.  
  482. INSERT INTO notif_falta ( id , dia , duracao ) VALUES
  483.  
  484. ('2949' , '2015-06-01' , 3),
  485. ('2950' , '2015-08-19' , 2 ),
  486. ('2951' , '2015-09-09' , 2);
  487.  
  488. -- TABELA DE FERIAS DE FUNCIONARIO
  489.  
  490. CREATE TABLE ferias (
  491.    
  492.     id char(4),
  493.     cpf_fun char(11),
  494.     numero_dias int CHECK ( numero_dias > 0 ),
  495.     seq char(4) not null,
  496.     data_inicio date not null,
  497.     data_fim date not null,
  498.     primary key (id),
  499.     foreign key (cpf_fun) references funcionario (CPF)
  500.    
  501. );
  502.  
  503. -- insenrindo valores na tabela ferias
  504.  
  505. INSERT INTO ferias (id , cpf_fun , numero_dias , seq , data_inicio , data_fim) VALUES
  506.  
  507. ( '2340' , '96202875763' , 20 , '0001' , '2016-06-02' , '2016-06-22' ),
  508. ( '2341' , '32970753502' , 20 , '0002' , '2016-06-02' , '2016-06-22' ),
  509. ( '2342' , '45321186898' , 20 , '0003' , '2016-01-02' , '2016-01-22' ),
  510. ( '2343' , '57859332507' , 20 , '0004' , '2016-01-02' , '2016-01-22' ),
  511. ( '2344' , '14073416260' , 20 , '0005' , '2016-04-02' , '2016-04-22' );
  512.  
  513.  
  514. -- TABELA DE CONTRA CHEQUE
  515.  
  516. CREATE TABLE contracheque (
  517.    
  518.     mes_referencia char(2) CHECK( mes_referencia > 0 AND mes_referencia < 13 ),
  519.     CPF char(11),
  520.     data_pagamento date,
  521.     valor_bruto float(5) not null CHECK( valor_bruto >= 0 ),
  522.     valor_liqd float(5) not null CHECK( valor_liqd >= 0),
  523.     valor_desconto float(5) CHECK ( valor_desconto >= 0 ),
  524.     primary key (mes_referencia , CPF),
  525.     foreign key(CPF) references funcionario(CPF) ON DELETE CASCADE ON UPDATE CASCADE
  526.  
  527. );
  528.  
  529.  
  530. -- inserindo valores na tabela contracheque
  531.  
  532. INSERT INTO contracheque (mes_referencia , CPF, data_pagamento , valor_bruto, valor_liqd, valor_desconto) VALUES
  533.  
  534. ('02', '96202875763' , '2017-02-15', 3250.30, 3237.68 , 12.32),
  535. ('03' , '96202875763', '2017-03-15' , 3250.30,  3237.68, 12.32),
  536. ('05' , '45321186898' , '2017-05-15' , 8720.55,8708.8, 11.75),
  537. ('01' , '57325297050' , '2017-01-15' , 5956.00, 5942.00 , 14.00),
  538. ('03' , '85902755239' ,'2017-03-15', 4567.32 , 4553.32 , 14.00),
  539. ('08' , '85902755239' , '2017-08-15',4567.32 , 4553.32 , 14.00),
  540. ('01', '96202875763' , '2017-01-15', 3250.30, 3237.68 , 12.32),
  541. ('11' , '45321186898' , '2017-11-15' , 8720.55,8708.8, 11.75),
  542. ('07' , '45321186898' , '2017-07-15' , 8720.55,8708.8, 11.75),
  543. ('04' , '45321186898' , '2017-04-15' , 8720.55,8708.8, 11.75),
  544. ('04' , '02123011878' , '2017-04-15' ,4567.32 , 4553.32, 14.00),
  545. ('05' ,'86147207504', '2016-05-16' , 3250.30, 3237.68 , 12.32);
  546.  
  547.  
  548. -- TABELA DE ENTREGA
  549.  
  550. CREATE TABLE entrega (
  551.    
  552.     seq char(4) ,
  553.     CPF_entregador char(11),
  554.     data_entrega date ,
  555.     hora_estimada time,
  556.     primary key (seq),
  557.     foreign key(CPF_entregador) references entregador(CPF)
  558.  
  559. );
  560.  
  561.  -- inserindo valores na tabela de entrega
  562.  
  563. INSERT INTO entrega ( seq , CPF_entregador , data_entrega , hora_estimada ) VALUES
  564.  
  565. ('0001' , '33666472214' , '2016-04-09 ' , '07:30:00'),
  566. ('0002' , '33666472214' , '2016-04-09 ' , '19:45:00'),
  567. ('0003' , '33666472214' , '2016-04-09 ' , '18:00:00'),
  568. ('0004' , '33666472214' , '2016-04-09 ' , '09:30:00'),
  569. ('0005' , '33666472214' , '2016-04-09 ' , '13:30:00'),
  570. ('0006' , '86147207504' , '2016-04-02 ' , '06:30:00'),
  571. ('0007' , '86147207504' , '2016-04-02 ' , '11:45:00'),
  572. ('0008' , '86147207504' , '2016-04-02 ' , '14:00:00'),
  573. ('0009' , '86147207504' , '2016-04-02 ' , '015:30:00'),
  574. ('0010' , '86147207504' , '2016-04-02 ' , '14:30:00');
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581. -- TABELA DE COMPRA
  582.  
  583. CREATE TABLE compra (
  584.  
  585.     codd char(4),
  586.     seq_entrega char(4),
  587.     cpfcliente char(11),
  588.     valor_total float not null CHECK( valor_total > 0 ),
  589.     dia date not null,
  590.     valor_total_desconto float CHECK (valor_total_desconto >= 0 ),
  591.     statos boolean default true,
  592.     primary key (codd),
  593.     foreign key (cpfcliente) references cliente (CPF),
  594.     foreign key (seq_entrega) references entrega ( seq )
  595. );
  596.  
  597. -- inserindo valores na tabela de compra
  598.  
  599. INSERT INTO compra ( codd , seq_entrega , cpfcliente , valor_total , dia , valor_total_desconto , statos ) VALUES
  600.  
  601. ('0001' , '0001 ' , '54501233290' , 693 , '2016-04-09 ' , 0 , true ),
  602. ('0002' , '0002 ' , '25251145314' ,789 , '2016-04-09 ' , 23.4 , true ),
  603. ('0003' , '0003 ' , '54501212341' ,403 , '2016-04-09 ' , 4.2 , true ),
  604. ('0004' , '0004 ' , '25251444444' ,233 , '2016-04-09 ' , 0 , true ),
  605. ('0005' , '0005 ' , '52125199997' ,70  , '2016-04-09 ' , 0 , true ),
  606. ('0006' , '0006 ' , '96468754345' ,2020 , '2016-04-02  ' , 20.2 , true ),
  607. ('0007' , '0007 ' , '13345509287' ,400 , '2016-04-02  ' , 0 , true ),
  608. ('0008' , '0008 ' , '52125191105' ,121 , '2016-04-02 ' , 0 , true ),
  609. ('0009' , '0009 ' , '25251444444' ,133 , '2016-04-02 ' , 3.2 , true ),
  610. ('0010' , '0010 ' , '54501233290' ,154 , '2016-04-02 ' , 0 , true );
  611.  
  612.  
  613.  
  614.  
  615. -- TABELA DE COMPRA COMUM
  616.  
  617. CREATE TABLE compra_comum (
  618.  
  619.     cod char(4),
  620.     primary key(cod),
  621.     CONSTRAINT fk_compracomum foreign key (cod) references compra (codd) ON DELETE CASCADE ON UPDATE CASCADE
  622. );
  623.  
  624.  
  625. -- inserindo valores na tabela de compra comum
  626.  
  627. INSERT INTO compra_comum (cod ) VALUES
  628.  
  629. ('0001'),
  630. ('0003'),
  631. ('0005'),
  632. ('0006'),
  633. ('0008');
  634.  
  635. -- TABELA DE COMPRA PROGRAMADA
  636.  
  637. CREATE TABLE compra_programada (
  638.  
  639.     cod char(4),
  640.     data_1 date not null,
  641.     data2 date,
  642.     esta_ativa boolean default true,
  643.     CONSTRAINT primary key (cod),
  644.     CONSTRAINT fk_compraprogramada foreign key (cod) references compra (codd) ON DELETE CASCADE ON UPDATE CASCADE
  645. );
  646.  
  647. -- inserindo valores na tabela de compra programada
  648.  
  649. INSERT INTO compra_programada (cod , data_1 , data2 , esta_ativa ) VALUES
  650.  
  651. ('0002' , '2016-04-09' , '2017-04-09' , true ),
  652. ('0004' , '2016-04-09' , '2017-01-09' , true ),
  653. ('0007' , '2016-04-02' , '2016-11-02' , true ),
  654. ('0009' , '2016-04-02' , '2017-02-02' , true ),
  655. ('0010' , '2016-04-02' , '2017-04-02' , true );
  656.    
  657.  
  658. -- TABELA DE GARAGEM
  659.  
  660. CREATE TABLE garagem (
  661.  
  662.     cod char(4),
  663.     seq_filial char(4),
  664.     CNPJ_matriz char(14),
  665.     descricao varchar(15),
  666.     capacidade int(3) not null,
  667.     num_veiculos_atual int(3),
  668.     primary key(cod),
  669.     foreign key(seq_filial , CNPJ_matriz ) references filial (seq , CNPJ_Matriz)
  670.    
  671.  
  672. );
  673.  
  674. -- inserindo valores na tabela garagem
  675.  
  676. INSERT INTO garagem ( cod , seq_filial , CNPJ_matriz , descricao , capacidade , num_veiculos_atual ) VALUES
  677.  
  678. ( '0001' , '0001' ,  '23416393000114' , null , 4 , 2 ),
  679. ( '0002' , '0002' ,  '23416393000114' , null , 4 , 2 ),
  680. ( '0003' , '0003' ,  '23416393000140' , null , 4 , 2 ),
  681. ( '0004' , '0004' ,  '23416393000169' , null , 4 , 2 );
  682.    
  683.  
  684. -- TABELA DE VEICULO
  685.  
  686. CREATE TABLE veiculo (
  687.  
  688.     placa char(7),
  689.     seq_filial char(4),
  690.     CNPJ_matriz char(14),
  691.     cod_garagem char(4) ,
  692.     modelo varchar(15),
  693.     descricao varchar(15),
  694.     cor varchar(10),
  695.     ano year,
  696.     statuss boolean default true,
  697.     primary key (placa),
  698.     foreign key(seq_filial , CNPJ_matriz ) references filial(seq , CNPJ_Matriz),
  699.     foreign key(cod_garagem) references garagem (cod)
  700. );
  701.  
  702. -- inserindo valores na tabela veiculo
  703.  
  704. INSERT INTO veiculo ( placa , seq_filial , CNPJ_matriz , cod_garagem , modelo , descricao , cor , ano , statuss ) VALUES
  705.  
  706. ( 'PEX0220 ' ,  '0001' ,  '23416393000114' , '0001' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  707. ( 'PEX2030 ' ,  '0001' ,  '23416393000114' , '0001' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  708. ( 'VET4320 ' ,  '0002' ,  '23416393000114' , '0002' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  709. ( 'VET3240 ' ,  '0002' ,  '23416393000114' , '0002' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  710. ( 'WCV0943'  ,  '0003' ,  '23416393000140' , '0003' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  711. ( 'WCV3344 ' ,  '0003' ,  '23416393000140' , '0003' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  712. ( 'HLT0032 ' ,  '0004' ,  '23416393000169' , '0004' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true),
  713. ( 'HLT3994 ' ,  '0004' ,  '23416393000169' , '0004' , 'Caminhão' , 'Mercedes-benz' , 'preto' , '2014' , true);
  714.  
  715.  
  716.  
  717.  
  718. -- TABELA DE ESTOQUE
  719.  
  720. CREATE TABLE estoque (
  721.  
  722.     id char(4),
  723.     seq_filial char(7),
  724.     cnpj_matriz char(14),
  725.     descricao varchar(80),
  726.     dt_ultima_entrada date,
  727.     primary key (id),
  728.     foreign key (seq_filial , cnpj_matriz) references filial (seq , CNPJ_Matriz)
  729.    
  730. );
  731.  
  732. -- inserindo valores na tabela estoque
  733.  
  734. INSERT INTO estoque ( id , seq_filial , cnpj_matriz , descricao , dt_ultima_entrada ) VALUES
  735.  
  736. ( '0001' , '0001' ,  '23416393000114' , null , '2018-01-04'),
  737. ( '0002' , '0002' ,  '23416393000114' , null , '2018-01-04'),
  738. ( '0003' , '0003' ,  '23416393000140' , null , '2018-01-04'),
  739. ( '0004' , '0004' ,  '23416393000169' , null , '2018-01-04');
  740.  
  741.  
  742. -- TABELA MAQUINA
  743.  
  744. CREATE TABLE maquina (
  745.  
  746.     id char(4),
  747.     id_estoque char(4),
  748.     id_operador char(11),
  749.     ano year ,
  750.     combustivel varchar(30),
  751.     modelo varchar(30),
  752.     capacidade float CHECK( capacidade > 0 ) ,
  753.     elevavao_max float  CHECK ( elevavao_max > 0 ),
  754.     garantia date,
  755.     tipo varchar(30),
  756.     comprimento float ,
  757.     primary key (id),
  758.     foreign key (id_estoque) references estoque (id),
  759.     foreign key (id_operador) references estoquista (cpf)
  760. );
  761.  
  762. -- inserindo valores na tabela maquina
  763.  
  764. INSERT INTO maquina ( id , id_estoque , id_operador , ano, combustivel , modelo , capacidade , elevavao_max ,
  765. garantia , tipo , comprimento ) VALUES
  766.  
  767. ( '0001' , '0001' , '77491222226' , '2014' , 'diesel', null , 600 , 5 , '2019-02-02' , 'empilhadeira' , 2.20 ),
  768. ( '0002' , '0002' , '57859332507' , '2014' , 'diesel', null , 600 , 5 , '2019-02-02' , 'empilhadeira' , 2.20 ),
  769. ( '0003' , '0003' , '16565525749' , '2014' , 'diesel', null , 600 , 5 , '2019-02-02' , 'empilhadeira' , 2.20 ),
  770. ( '0004' , '0004' , '98243208909' , '2014' , 'diesel', null , 600 , 5 , '2019-02-02' , 'empilhadeira' , 2.20 );
  771.  
  772. -- TABELA DE AVARIA
  773.  
  774. CREATE TABLE avaria (
  775.    
  776.     id char(4),
  777.     causa varchar(20) not null,
  778.     preco float(3) CHECK ( preco > 0 ),
  779.     obs varchar(40),
  780.     primary key(id)
  781.    
  782. );
  783.  
  784. -- inserindo valores na tabela avaria
  785.  
  786. INSERT INTO avaria (id , causa , preco , obs ) VALUES
  787.  
  788. ('0001' , 'Queda' , 30 , null ),
  789. ('0002' , 'Queda' , 10 , null ),
  790. ('0003' , 'Queda' , 200 , null ),
  791. ('0004' , 'Queda' , 32.2 , null ),
  792. ('0005' , 'Queda' , 230 , null ),
  793. ('0006' , 'Queda' , 92.2 , null );
  794.  
  795. -- TABELA DE PRATELEIRA
  796.  
  797. CREATE TABLE prateleira (
  798.  
  799.     codigo char(4),
  800.     altura float(2) not null,
  801.     comprimento float(2) not null,
  802.     posicao_nivel char(2),
  803.     primary key(codigo)
  804.  
  805. );
  806.  
  807. -- inserindo valores na tabela prateleira  
  808.  
  809. INSERT INTO prateleira ( codigo , altura , comprimento , posicao_nivel ) VALUES
  810.  
  811. ('0001' , 5 , 10 , 'A1' ),
  812. ('0002' , 5 , 10 , 'A2' ),
  813. ('0003' , 5 , 10 , 'A3' ),
  814. ('0004' , 5 , 10 , 'A4' ),
  815. ('0005' , 5 , 10 , 'A5' ),
  816. ('0006' , 5 , 10 , 'B1' ),
  817. ('0007' , 5 , 10 , 'B2' ),
  818. ('0008' , 5 , 10 , 'B3' ),
  819. ('0009' , 5 , 10 , 'B4' ),
  820. ('0010' , 5 , 10 , 'B5' ),
  821. ('0011' , 5 , 10 , 'C1' ),
  822. ('0012' , 5 , 10 , 'C2' ),
  823. ('0013' , 5 , 10 , 'C3' ),
  824. ('0014' , 5 , 10 , 'C4' ),
  825. ('0015' , 5 , 10 , 'C5' );
  826.  
  827.  
  828.  
  829. -- TABELA DE NCM DE PRODUTO
  830.  
  831. CREATE TABLE NCM (
  832.    
  833.     id char(4),
  834.     descricao varchar(40),
  835.     cod_mercosul char(9) not null,
  836.     primary key(id)
  837.  
  838. );
  839.  
  840. -- inserindo valores na tabela NCM
  841.  
  842. INSERT INTO NCM ( id , descricao , cod_mercosul) VALUES
  843.  
  844. ('0001', ' ' , '000000001'),
  845. ('0002' , ' ' , '000000002'),
  846. ('0003' , ' ', '000000003'),
  847. ('0004' , ' ', '000000004'),
  848. ('0005', ' ' , '000000005');
  849.  
  850. -- TABELA DE UNIDADE DE PRODUTO  
  851.  
  852. CREATE TABLE unidade (
  853.  
  854.     cod char(4),
  855.     descricao varchar(30),
  856.     sigla char(2) not null,
  857.     primary key(cod)
  858.    
  859. );
  860.  
  861. -- inserindo valores na tabela unidade  
  862.  
  863. INSERT INTO unidade (cod, descricao , sigla) VALUES
  864.  
  865. ('0001', 'quilogramas', 'kg'),
  866. ('0002' , 'mililitros', 'mL'),
  867. ('0003', 'gramas' , 'g'),
  868. ('0004', 'litros' , 'L'),
  869. ('0005', 'miligramas' , 'mg');
  870.  
  871. -- TABELA DE CATEGORIA DE PRODUTO
  872.  
  873. CREATE TABLE categoria (
  874.    
  875.     codcategoria char(4),
  876.     descricaocategoria varchar(30) not null,
  877.     primary key (codcategoria)
  878.  
  879. );
  880.  
  881. -- inserindo valores na tabela categoria
  882.  
  883. INSERT INTO categoria ( codcategoria , descricaocategoria ) VALUES
  884.  
  885. ('0001', 'Condimentos' ),
  886. ('0002' , 'Laticinios'),
  887. ('0003', 'HortiFruti'),
  888. ('0004' , 'Conservas'),
  889. ('0005', 'Limpeza'),
  890. ('0006' , 'Bebidas Alcoolicas'),
  891. ('0007' , 'Bebidas nao Alcoolicas'),
  892. ('0008' , 'Graos e Cereais');
  893.  
  894. -- TABELA DE SUBCATEGORIA DE PRODUTO
  895.  
  896. CREATE TABLE subcategoria (
  897.  
  898.     codsubcategoria char(4),
  899.     cod_categoria char(4),
  900.     descricaosubcategoria varchar(30) not null,
  901.     primary key(codsubcategoria),
  902.     foreign key (cod_categoria) references categoria(codcategoria)
  903.  
  904. );
  905.  
  906. -- inserindo valores na tabela subcategoria
  907.  
  908. INSERT INTO subcategoria ( codsubcategoria , cod_categoria , descricaosubcategoria ) VALUES
  909.  
  910. ('0001' , '0001' , 'Vinagres'),
  911. ('0002' , '0001' , 'Temperos'),
  912. ('0003' , '0001' , 'Sal' ),
  913. ('0004' , '0001', 'Azeites'),
  914. ('0005' , '0001', 'Oleos'),
  915. ('0006' , '0001', 'Especiarias'),
  916. ('0007' , '0002', 'Leites'),
  917. ('0008' , '0002', 'Iogurtes'),
  918. ('0009' , '0002', 'Fermentados'),
  919. ('0010' , '0002', 'Queijos'),
  920. ('0011' , '0003', 'Ovos'),
  921. ('0012' , '0003', 'Frutas secas'),
  922. ('0013' , '0003', 'Legumes'),
  923. ('0014' , '0004' , 'Frutas'),
  924. ('0015' , '0004', 'Peixes'),
  925. ('0016' , '0004', 'Vegetais'),
  926. ('0017' , '0004', 'Cogumelos'),
  927. ('0018' , '0005', 'Detergente'),
  928. ('0019' , '0005', 'Desinfetante'),
  929. ('0020' , '0005', 'Sabao em po'),
  930. ('0021' , '0005', 'Sabao em barra'),
  931. ('0022' , '0005', 'Amaciante');
  932.  
  933.  -- TABELA DE MARCA DE PRODUTO
  934.  
  935. CREATE TABLE marca (
  936.  
  937.     codmarca char(4),
  938.     descricaomarca varchar(40),
  939.     primary key(codmarca)
  940.  
  941. );
  942.  
  943. -- inserindo valores na tabela marca  
  944.  
  945. INSERT INTO marca ( codmarca , descricaomarca ) VALUES
  946.  
  947. ('0001', 'Sadia'),
  948. ('0002' , 'Knorr'),
  949. ('0003' , 'Camponesa'),
  950. ('0004' , 'Kicaldo'),
  951. ('0005' , 'Vitarela'),
  952. ('0006' , 'Bauduco'),
  953. ('0007', 'OMO'),
  954. ('0008' , 'Dona Benta'),
  955. ('0009', 'Nestle');
  956.  
  957.  
  958. -- TABELA DE FORNECEDOR
  959.  
  960. CREATE TABLE fornecedor (
  961.  
  962.     cod char(4),
  963.     nome varchar(20) not null,
  964.     CNPJ char(14) not null,
  965.     rua varchar(20),
  966.     bairro varchar(15),
  967.     CEP char(8),
  968.     estado varchar(15) not null,
  969.     ativo boolean default true,
  970.     primary key(cod )
  971. );
  972.  
  973. -- inserindo valores na tabela fornecedor
  974.  
  975. INSERT INTO fornecedor ( cod , nome , CNPJ , rua , bairro , CEP , estado , ativo ) VALUES
  976.  
  977. ('0001' , 'Sadia'     , '55274471000180' , 'Rua valadares' , 'ipsep', '54330315' , 'Pernambuco' , true ) ,
  978. ('0002' , 'Pampers'   , '26724671000180' , 'Rua cartomante' , 'ibura', '54202010' , 'Pernambuco' , true ) ,
  979. ('0003' , 'Vitarela'  , '21712241000162' , 'Rua maniac' , 'algodão', '54215322' , 'São paulo' , true ) ,
  980. ('0004' , 'Coca-cola' , '26804531000180' , 'Rua argola' , 'vale tinhaem ', '51215020' , 'Rio de janeiro' , true ) ,
  981. ('0005' , 'Helmans'   , '77411981000180' , 'Rua sartre' , 'gitacity ', '44650201' , 'São paulo' , true ),
  982. ('0006' , 'Bombril'   , '53686527000188' , 'Rua 3' , 'Varzea' , '50980320' , 'Recife' , true );
  983.  
  984.  -- TABELA DE TELEFONE DE FORNECEDOR
  985.  
  986. CREATE TABLE telefone_fornecedor(
  987.    
  988.     cod_fornecedor char(4),
  989.     telefone char(11),
  990.     primary key( cod_fornecedor, telefone),
  991.     CONSTRAINT fk_forn foreign key(cod_fornecedor) references fornecedor (cod) ON DELETE CASCADE ON UPDATE CASCADE
  992.  
  993. );
  994.  
  995. -- inserindo valores na tabela telefone_fornecedor
  996.  
  997. INSERT INTO telefone_fornecedor (cod_fornecedor, telefone) VALUES
  998.  
  999. ('0001' , '8133115544'),
  1000. ('0002' ,  '8133468952'),
  1001. ('0003' , '1140025632'),
  1002. ('0004', '1432456895'),
  1003. ('0005', '1121452062');
  1004.  
  1005.  
  1006. -- TABELA  DE PRODUTO REF  
  1007.  
  1008. CREATE TABLE produto_ref (
  1009.  
  1010.     cod char(4),
  1011.     id_unidade char(4),
  1012.     id_marca char(4),
  1013.     id_ncm char(4),
  1014.     id_categoria char(4),
  1015.     id_subcategoria char(4),
  1016.     id_fornecedor char(4),
  1017.     qtd_estoque int ,
  1018.     ICMS float,
  1019.     CST varchar(3),
  1020.     preco_por_tabela float,
  1021.     cod_barra char(13),
  1022.     freq_pedido float NULL,
  1023.     descricao varchar(30),
  1024.     qtd_min int,
  1025.     qtd_total_estoque int,
  1026.     preco_ult_compra float,
  1027.     primary key (cod),
  1028.     foreign key (id_unidade) references unidade (cod),
  1029.     foreign key (id_marca) references marca (codmarca),
  1030.     foreign key (id_ncm) references ncm (id),
  1031.     foreign key (id_categoria) references categoria (codcategoria),
  1032.     foreign key (id_subcategoria) references subcategoria (codsubcategoria),
  1033.     foreign key (id_fornecedor) references fornecedor (cod)
  1034.  
  1035. );
  1036.  
  1037. INSERT INTO produto_ref (cod, id_unidade , id_marca , id_ncm , id_categoria, id_subcategoria, id_fornecedor , qtd_estoque ,
  1038.  ICMS , CST , preco_por_tabela , cod_barra , freq_pedido , descricao , qtd_min, qtd_total_estoque , preco_ult_compra) VALUES
  1039.  
  1040. ('0001' , '0001' , '0001', '0001' , '0001' , '0001' , '0001' , 30 , 4.5 , '00' , 8.69  , '0201254123151' ,  0.2 , 'something' , 3 , 80 , 8.20 ),
  1041. ('0002' , '0001' , '0001', '0001' , '0001' , '0002' , '0001' , 31 , 1.1 , '00' , 3.69  , '0201223412341' ,  0.3 , 'something' , 3 , 81 , 8 ),
  1042. ('0003' , '0001' , '0001', '0001' , '0001' , '0001' , '0001' , 32 , 1.4 , '00' , 4.69  , '0201254353434' ,  0.3 , 'something' , 3 , 82 , 7 ),
  1043. ('0004' , '0001' , '0002', '0001' , '0001' , '0002' , '0002' , 33 , 2.4 , '00' , 5.69  , '0243453453453' ,  0.2 , 'something' , 3 , 83 , 6.20 ),
  1044. ('0005' , '0005' , '0002', '0002' , '0001' , '0003' , '0002' , 34 , 3.7 , '00' , 6.69  , '0256523435134' ,  0.2 , 'something' , 3 , 84 , 5.20 ),
  1045. ('0006' , '0002' , '0002', '0002' , '0001' , '0003' , '0002' , 35 , 1.7 , '00' , 7.69  , '3424343423434' ,  0.4 , 'something' , 3 , 85 , 8.20 ),
  1046. ('0007' , '0002' , '0003', '0002' , '0001' , '0004' , '0003' , 36 , 4.8 , '00' , 8.69  , '0201545213412' ,  0.5 , 'something' , 3 , 86 , 9.20 ),
  1047. ('0008' , '0002' , '0003', '0002' , '0001' , '0005' , '0003' , 37 , 4.7 , '00' , 21.69 , '0201265243565' ,  0.1 , 'something' , 3 , 87 , 7.20 ),
  1048. ('0009' , '0002' , '0003', '0003' , '0001' , '0006' , '0003' , 38 , 5.4 , '00' , 10.69 , '0201256435453' ,  0.2 , 'something' , 3 , 88 , 1.20 ),
  1049. ('0010' , '0005' , '0004', '0003' , '0001' , '0001' , '0004' , 39 , 9.3 , '00' , 69    , '0201250454544' ,  0.3 ,'something' , 3 , 89 , 82.20 ),
  1050. ('0011' , '0003' , '0004', '0003' , '0001' , '0002' , '0004' , 20 , 7.1 , '00' , 23.69 , '0201254177575' ,  0.3 , 'something' , 3 , 90 , 2.20 ),
  1051. ('0012' , '0003' , '0005', '0003' , '0002' , '0007' , '0004' , 30 , 6.4 , '00' , 8     , '0201254199997' ,  0.5 , 'something' , 3 , 99 , 3.20 ),
  1052. ('0013' , '0003' , '0005', '0004' , '0002' , '0007' , '0005' , 40 , 1.5 , '00' , 0.69  , '0201255455555' ,  0.9 , 'something' , 3 , 98 , 8.20 ),
  1053. ('0014' , '0003' , '0006', '0004' , '0002' , '0007' , '0005' , 50 , 3.4 , '00' , 2.69  , '0201254176777' ,  0.5 , 'something' , 3 , 97 , 99.20 ),
  1054. ('0015' , '0005' , '0006', '0004' , '0002' , '0008' , '0005' , 70 , 7.9 , '00' , 3.69  , '0201254543543' ,  0.3 , 'something' , 3 , 96 , 20 ),
  1055. ('0016' , '0004' , '0007', '0004' , '0002' , '0007' , '0006' , 50 , 4.2 , '00' , 4.69  , '0201256555555' ,  0.1 , 'something' , 3 , 95 , 12.20 ),
  1056. ('0017' , '0004' , '0007', '0005' , '0002' , '0008' , '0006' , 30 , 6.3 , '00' , 8.69  , '0201255523151' ,  0.7 , 'something' , 3 , 94 , 20 ),
  1057. ('0018' , '0004' , '0008', '0005' , '0002' , '0007' , '0006' , 60 , 5.2 , '00' , 89.69 , '0205465793151' ,  0.7 , 'something' , 3 , 93 , 21.20 ),
  1058. ('0019' , '0004' , '0009', '0005' , '0002' , '0007' , '0006' , 40 , 7.1 , '00' , 87.69 , '0201888863151' ,  0.6 , 'something' , 3 , 92 , 23 ),
  1059. ('0020' , '0005' , '0008', '0005' , '0002' , '0008' , '0006' , 57 , 4.1 , '00' , 46    , '0201999923151' ,  0.6 , 'something' , 3 , 91 , 42 );
  1060.  
  1061.  
  1062.  
  1063. -- TABELA ITEM DE COMPRA
  1064.  
  1065. CREATE TABLE item_compra (
  1066.  
  1067.     cod_compra char(4),
  1068.     cod_produto char(4),
  1069.     quantidade int CHECK (quantidade > 0) ,
  1070.     valor_desconto float default 0 CHECK (valor_desconto >= 0),
  1071.     valor_unitario float CHECK (valor_unitario > 0),
  1072.     primary key (cod_compra , cod_produto),
  1073.     foreign key (cod_compra) references compra (codd),
  1074.     foreign key (cod_produto) references produto_ref (cod)
  1075. );
  1076.  
  1077.  -- ITEM COMPRA
  1078.  
  1079. INSERT INTO item_compra (cod_compra , cod_produto , quantidade , valor_desconto , valor_unitario ) VALUES
  1080. ('0001' , '0001' , 3 , null , 8.69 ),
  1081. ('0002' , '0001' , 5 , null , 8.69 ),
  1082. ('0003' , '0001' , 8 , null , 8.69 ),
  1083. ('0004' , '0003' , 5 , null , 4.69 ),
  1084. ('0005' , '0004' , 2 , null , 5.69 ),
  1085. ('0006' , '0005' , 4 , null , 6.69 ),
  1086. ('0007' , '0006' , 5 , null , 7.69 ),
  1087. ('0008' , '0007' , 4 , null , 8.69 );
  1088.  
  1089.  
  1090.  
  1091. -- TABELA DE INCIDENTE  
  1092.  
  1093. CREATE TABLE incidente (
  1094.  
  1095.     cod char(4),
  1096.     seq_entrega char(4),
  1097.     dataa date not null,
  1098.     relatorio varchar(200),
  1099.     hora time ,
  1100.     primary key(cod),
  1101.     foreign key (seq_entrega) references entrega (seq)
  1102.  
  1103. );
  1104.  
  1105. -- inserindo valores na tabela incidente
  1106.  
  1107. INSERT INTO incidente (cod , seq_entrega , dataa, relatorio , hora ) VALUES
  1108.  
  1109. ('0001' , '0003' , '2016-04-09 ' , ' Tentativa de assalto ' , '18:06:00' ),
  1110. ('0002' , '0005' , '2016-04-09 ' , ' Tentativa de assalto ' , '13:34:00' ),
  1111. ('0003' , '0006' , '2016-04-02 ' , ' Tentativa de assalto ' , '06:32:00' );
  1112.  
  1113. -- TABELA DOCS
  1114.  
  1115.  
  1116. CREATE TABLE docs (
  1117.    
  1118.     cod_incidente char(4),
  1119.     docs varchar(20),
  1120.     primary key (cod_incidente , docs),
  1121.     CONSTRAINT fk_incidente foreign key (cod_incidente) references incidente (cod ) ON DELETE CASCADE ON UPDATE CASCADE
  1122. );
  1123.  
  1124.  -- inserindo valores na tabela docs
  1125.  
  1126. INSERT INTO docs ( cod_incidente , docs ) VALUES
  1127.  
  1128. ( '0001', 'something' ) ,
  1129. ('0002' , 'something' ) ,
  1130. ('0003' , 'something');
  1131.  
  1132. -- TABELA DE TIPO DE PAGAMENTO
  1133.  
  1134. CREATE TABLE tipo_pagamento (
  1135.    
  1136.     codtipo_pagamento mediumint AUTO_INCREMENT,
  1137.     descricao varchar(30),
  1138.     primary key(codtipo_pagamento)
  1139.  
  1140.  );
  1141.  
  1142.  -- inserindo valores na tabela tipo_pagamento  
  1143.  
  1144. INSERT INTO tipo_pagamento ( descricao ) VALUES
  1145.  
  1146. ( 'Boleto Bancario'),
  1147. ( 'Cartao de Credito'),
  1148. ('Cartao de Debito');
  1149.  
  1150. -- TABELA DE PEDIDO FORNECEDOR
  1151.  
  1152. CREATE TABLE pedido_fornecedor (
  1153.    
  1154.     codpdfornecedor char(4),
  1155.     CPF_gerente char(11),
  1156.     total_desconto float CHECK ( total_desconto >= 0 ),
  1157.     valor_total_IPI float CHECK ( valor_total_IPI >= 0 ),
  1158.     CFOPpf char(4) not null,
  1159.     valor_total float CHECK ( valor_total >= 0),
  1160.     dia date not null,
  1161.     statospf boolean default true,
  1162.     valor_frete float CHECK (valor_frete >= 0),
  1163.     primary key (codpdfornecedor),
  1164.     foreign key (CPF_gerente) references gerente (CPF)
  1165. );
  1166.  
  1167.  
  1168.  
  1169.  
  1170. -- inserindo valores na tabela pedido_fornecedor
  1171.  
  1172. INSERT INTO pedido_fornecedor ( codpdfornecedor, CPF_gerente , total_desconto , valor_total_IPI, CFOPpf, dia , statospf, valor_frete ) VALUES
  1173.  
  1174. ('0001' , '96202875763' , 12380.00, 354.20 , '0001', '2017-12-09' , true , 12.90),
  1175. ('0002', '96202875763' , 3456.40 , 123.20 , '0002', '2017-11-09' , true , 10.30),
  1176. ('0003' , '96202875763' , 2280.25 , 220.00,'0003',  '2017-06-13', true , 28.50),
  1177. ('0004' , '96202875763' , 12200.00, 123.99,'0004',  '2017-07-04', true , 9.89),
  1178. ('0005' , '96202875763' , 990.00 , 19.89,  '0005',  '2016-12-09', true , 12.43),
  1179. ('0006' , '96202875763' , 5660.00, 56.70 , '0006', '2015-08-27' , true , 6.98),
  1180. ('0007' , '96202875763' , 2890.00 , 35.47 , '0007', '2016-04-12' , true , 10.20),
  1181. ('0008' , '96202875763' , 1873.20 , 190.00 ,'0008' , '2017-01-04' , true , 8.33),
  1182. ('0009' , '96202875763' , 2348.80, 111.10 , '0009' ,'2017-06-09' , true , 10.22),
  1183. ('0010' , '02123011878' , 1300.00, 132.30 , '0010','2017-08-27' , true , 11.30);
  1184.  
  1185.  
  1186.  
  1187.  
  1188. -- TABELA DE FATURA
  1189.  
  1190. CREATE TABLE fatura (
  1191.  
  1192.     idfatura mediumint auto_increment,
  1193.     cod_pedido_fornecedor char(4),
  1194.     data_vencimento date,
  1195.     valor_pago_atual float CHECK (valor_pago_atual >= 0 ),
  1196.     valor_total_final float CHECK (valor_total_final >= 0),
  1197.     data_emissao date,
  1198.     statos boolean default true,
  1199.     data_paga date,
  1200.     multa float,
  1201.     primary key (idfatura),
  1202.     foreign key (cod_pedido_fornecedor) references pedido_fornecedor (codpdfornecedor)
  1203.  
  1204. );
  1205. -- inserindo valores na tabela fatura
  1206.  
  1207. INSERT INTO fatura ( cod_pedido_fornecedor , data_vencimento , valor_pago_atual, valor_total_final ,
  1208.  data_emissao , statos , data_paga , multa ) VALUES
  1209.  
  1210.  ( '0001', '2018-03-03' , 200.30 , 600 , '2017-11-13' , true , null , 0),
  1211.  ( '0002', '2018-02-03' , 343 , 800 , '2017-11-17' , true , null , 0),
  1212.  ( '0003', '2018-05-03' , 20.10 , 200 , '2017-11-16' , true , null , 0),
  1213.  ( '0004', '2018-07-03' , 40 , 300 , '2017-12-20' , true , null , 0),
  1214.  ( '0005', '2018-08-03' , 234 , 400 , '2018-01-13' , true , null , 0),
  1215.  ( '0006', '2018-09-03' , 120 , 500 , '2017-11-22' , true , null , 0);
  1216.  
  1217.  
  1218.  
  1219.  
  1220. -- TABELA DE NOTA FISCAL
  1221.  
  1222. CREATE TABLE nota_fiscal (
  1223.  
  1224.     NFE char(9),
  1225.     ICMS float not null,
  1226.     valor_total float not null CHECK ( valor_total > 0 ),
  1227.     valor_total_desconto float CHECK ( valor_total_desconto >= 0 ),
  1228.     dia date not null,
  1229.     valor_frete float,
  1230.     primary key (NFE)
  1231. );
  1232.  
  1233. --  inserindo valores na tabela nota_fiscal
  1234.  
  1235. INSERT INTO nota_fiscal (NFE , ICMS , valor_total, valor_total_desconto, dia , valor_frete) VALUES
  1236.  
  1237. ('111111109' ,  0.04 , 143.98 , 139.90 , '2016-11-12' , 3.78),
  1238. ('111111104' ,  0.06 , 35.80 , 35.80, '2017-10-21' , 7.90),
  1239. ('111111113' ,  0.04 , 50.21 , 49.95 , '2012-03-27' , 10.32),
  1240. ('111111105' ,  0.06 , 173.98, 173.98 , '2010-06-13' , 8.50),
  1241. ('111111107' ,  0.02 , 50.43 , 50.43 , '2002-04-01' , 2.21),
  1242. ('111111108' ,  0.03 , 283.10 , 279.50 , '2011-11-11' , 4.30),
  1243. ('111111139' ,  0.02 , 123.54 , 111.20 , '2016-11-12' , 6.80),
  1244. ('111111199' ,  0.02 , 12.99 , 12.99 , '2016-11-13' , 8.30),
  1245. ('111111129' ,  0.03 , 346.10 , 336.10 , '2016-11-10' , 2.12);
  1246.  
  1247. -- TABELA DE PAGAMENTO  
  1248.  
  1249. CREATE TABLE pagamento (
  1250.    
  1251.     cod mediumint not null auto_increment,
  1252.     id_fatura mediumint,
  1253.     cod_compra char(4),
  1254.     cod_tipo_pagamento mediumint,
  1255.     valor_pago float CHECK ( valor_pago >= 0 ),
  1256.     dia date,
  1257.     statos boolean default true ,
  1258.     tipo_pagamento enum ('Cartao credito', 'Cartao Debito', 'Boleto'),
  1259.     primary key (cod),
  1260.     foreign key (id_fatura) references fatura (idfatura),
  1261.     foreign key (cod_compra) references compra (codd),
  1262.     foreign key (cod_tipo_pagamento) references tipo_pagamento (codtipo_pagamento)
  1263.    
  1264. );
  1265.  
  1266.  
  1267. INSERT INTO pagamento ( id_fatura ,cod_compra , cod_tipo_pagamento  , valor_pago , dia ,  statos, tipo_pagamento ) VALUES
  1268.  
  1269. ( 1, '0001' , 1 , 100 , '2017-11-03' , true , null ),  
  1270. ( 2 , '0002' , 2 , 80 , '2017-11-13' , true , null ),
  1271. ( 3 , '0003' , 3 , 200 , '2017-11-14' , true , null ),
  1272. ( 4 , '0004' , 2 , 49 , '2017-11-20' , true , null ),
  1273. ( 5 , '0005' , 3 , 90.20 , '2017-12-03' , true , null );
  1274.  
  1275.  
  1276. -- TABELA DA NOTA FISCAL FORNECEDOR
  1277.  
  1278. CREATE TABLE nota_fiscal_fornecedor (
  1279.  
  1280.     NFE char(9),
  1281.     cod_pagamento mediumint,
  1282.     CFOP char(4) not null,
  1283.     IPI float,
  1284.     primary key(NFE),
  1285.     CONSTRAINT fk_ntfiscalfornecedor foreign key (NFE) references nota_fiscal (NFE) ON DELETE CASCADE ON UPDATE CASCADE,
  1286.     foreign key (cod_pagamento) references pagamento (cod)
  1287.  );
  1288.  
  1289.  
  1290. INSERT INTO nota_fiscal_fornecedor (NFE, cod_pagamento , CFOP , IPI ) VALUES
  1291. ('111111109' , 1, '1100' , 5.4),
  1292. ( '111111104' , 2, '1100' , 6.4),
  1293. ('111111113' ,  3, '1100' , 7.4),
  1294. ( '111111105' , 4 , '1100' , 3.2);
  1295.  
  1296. -- TABELA NOTA FISCAL COMPRA
  1297.  
  1298. CREATE TABLE nota_fiscal_compra (
  1299.  
  1300.     NFE char(9),
  1301.     cod_pagamento mediumint,
  1302.     primary key (NFE),
  1303.     CONSTRAINT fk_ntfiscalcompra foreign key (NFE) references nota_fiscal (NFE) ON DELETE CASCADE ON UPDATE CASCADE,
  1304.     foreign key (cod_pagamento) references pagamento (cod)
  1305.  
  1306. );
  1307.  
  1308. -- inserindo valores na tabela nota_fical_compra
  1309.  
  1310. INSERT INTO nota_fiscal_compra ( NFE , cod_pagamento ) VALUES
  1311. ('111111129' , 1),
  1312. ('111111107' , 2 ),
  1313. ('111111108' , 3 ),
  1314. ('111111139' , 4 ),
  1315. ('111111199' , 5 );
  1316.  
  1317.  
  1318.  
  1319. -- TABELA DE CLIENTE  
  1320.  
  1321. CREATE TABLE cliente (
  1322.  
  1323.     CPF char(11),
  1324.     seq_filial char(4),
  1325.     cnpj_matriz char(14),
  1326.     cep char(8) not null,
  1327.     cidade varchar(30),
  1328.     numero char(3),
  1329.     descricao varchar(30),
  1330.     valor_credito float,
  1331.     p_nome varchar(20) not null,
  1332.     m_nome varchar(20) ,
  1333.     u_nome varchar(30) not null,
  1334.     rg char(7) not null UNIQUE,
  1335.     senha varchar(12) not null default '123456',
  1336.     tem_clube_desconto boolean default false,
  1337.     data_cadastro date,
  1338.     email varchar(40) not null,
  1339.     data_nascimento date not null,
  1340.     primary key (CPF),
  1341.     foreign key (seq_filial , cnpj_matriz) references filial (seq , CNPJ_Matriz)
  1342.    
  1343. );
  1344.  
  1345.  
  1346. INSERT INTO cliente ( CPF , seq_filial , cnpj_matriz ,cep, cidade, numero, descricao , valor_credito, p_nome , m_nome , u_nome, rg , senha , tem_clube_desconto , data_cadastro , email , data_nascimento) VALUES
  1347.  
  1348. ('54501233290' , '0001' , '23416393000114', '52291045' , 'Santo andre'  , '111' ,  'Rua Jaguaribara'         , 1200.00 , 'Melissa'  , 'Andreia'    , 'Nascimento', '1908234' , '3214'    , false , '2016-02-12' , 'manasc@gmail.com'            , '1999-06-13'),
  1349. ('25251145314' , '0002' , '23416393000114', '50999321' , 'Sao bernardo' , '123' , 'Rua de Saão Bento'       , 0       , 'Marcos'   , 'Andre'      , 'Marques'   , '9123786' , 'xxx9'    , false , '2015-03-11' , 'marcossantos@hotmail.com'    , '1980-03-25' ),
  1350. ('13345509287' , '0001' , '23416393000114', '50348567' , 'Sao caetano'  , '453' , 'Rua Real da Torre '       , 0       , 'Fatima'   , 'Caixias'    , 'Laffaiete' , '1866630' , '1233'    , false , '2017-02-11' , 'falcaix@gmail.com'           , '1972-09-09'),
  1351. ('77658476358' , '0001' , '23416393000114', '63900435' , 'Sao bernardo' , '434' , 'Rua Chile'                , 0       , 'Renato'   , 'Murilo'     , 'Dias'      , '1266839' , '34g4'    , false , '2017-07-04' , 'murilodias@gmail.com'        , '1989-12-29'),
  1352. ('50519774647' , '0002' , '23416393000114', '56332078' , 'Sao bernardo' ,  null , 'Rua Quarenta e Seis'      , 22.30   , 'Joaquim'  , 'Luiz'       , 'Campos'    , '2871019' , '88uh'    , true  , '2016-04-04' , 'uizjoaquim@hotmail.com'      , '1990-04-30'),
  1353. ('79317880797' , '0001' , '23416393000114', '82600130' , 'Sao bernardo' , null  , 'Rua Bernardo Rosenmann'   , 0       , 'Alana'    , 'Rodrigues'  , 'Mello'     , '2368088' ,  '34f4'   , true  , '2016-09-11' , 'alana_rodriques@gmail.com'   , '1993-06-06'),
  1354. ('52125191105' , '0002' , '23416393000114', '77820026' , 'Sao bernardo' , null  , 'Rua 16'                   , 0       , 'Luana'    , 'Barbosa'    , 'Pinto'     , '1994566' , '9jf4'    , true  , '2016-09-11' , 'luaninha_babrbosa@gmail.com' , '1996-06-06'),
  1355. ('50130428361' , '0001' , '23416393000114', '77016638' , 'Sao bernardo' , null  , 'Quadra 509 Sul Alameda 7' , 0       , 'Agatha'   , 'Vasconcelos', 'Belarmino' , '4526174' , 'f56g'    , true  , '2016-03-04' , 'agatinha_bb@gmail.com'       , '1999-09-09'),
  1356. ('96468793068' , '0001' , '23416393000114', '49069186' , 'Santo Andre'  , null  , 'Rua do Sol'               , 0       , 'Camila'   , 'Castro'     , 'Raimundo'  , '3199788' , 'kj004'   , true  , '2016-09-18' , 'camila_racastro@gmail.com'   , '1992-02-20'),
  1357. ('16512268903' , '0002',  '23416393000114', '72876134' , 'Sao bernardo' , null  , 'Quadra 37'                , 23.33   , 'Valkiria' , 'Queiroz'    , 'Calado'    , '4241822' , 'kd3io4'  , true  , '2016-02-12' , 'valqueiros@outlook.com'      , '1960-03-07'),
  1358. ('07617589689' , '0002' , '23416393000114', '89280577' , 'Sao bernardo' , null  , 'Rua Zanzibar'             , 0       , 'Angelica' , 'Meneses'    , 'da Silva'  , '4764955' , 'i4hfi4h' , false , '2017-02-05' , 'angelica_mene@outlook.com'   , '1992-12-11'),
  1359.  
  1360. ('54501212341' , '0004' , '23416393000169', '52291032' , 'Ipanema'      , '131' , 'Rua dreamer'              , 200.00  , 'Felissia' , 'Andreia'    , 'Nasciso'   , '1908312' , '3214'    , true  , '2016-02-12' , 'xxxxx@gmail.com'             , '1978-06-13'),
  1361. ('25251444444' , '0004' , '23416393000169', '50999399' , 'Ipanema'      , '163' , 'Rua gastino'              , 123     , 'Maycomu'  , 'Andre'      , 'feliciano' , '9123321' , 'xxx9'    , true  , '2015-03-11' , 'yyyyy@hotmail.com'           , '1987-03-25' ),
  1362. ('13345555555' , '0004' , '23416393000169', '50348577' , 'Ipanema'      , '543' , 'Rua malaneti'             , 214     , 'Fatima'   , 'Bernardes'  , 'Laffaiete' , '1864568' , '1233'    , false , '2017-02-11' , 'zzzzz@gmail.com'             , '1967-09-09'),
  1363. ('77656666666' , '0004' , '23416393000169', '63900442' , 'Ipanema'      , '544' , 'Rua valadares'            , 54      , 'Renan'    , 'Murilo'     , 'Anos'      , '1264444' , '34g4'    , false , '2017-07-04' , 'hhhhh@gmail.com'             , '1989-12-29'),
  1364. ('50519434234' , '0004' , '23416393000169', '56332011' , 'Ipanema'      , '003' , 'Rua noventa e sete'       , 22.30   , 'Joaquim'  , 'Nabuco'     , 'Oliveira'  , '2875412' , '88uh'    , true  , '2016-04-04' , 'jjjjj@hotmail.com'           , '1998-04-30'),
  1365. ('79317887675' , '0004' , '23416393000169', '82600131' , 'Ipanema'      , null  , 'Rua Bernardo manoel'      , 0       , 'Alana'    , 'Guedes'     , 'Mello'     , '2364811' ,  '34f4'   , true  , '2016-09-11' , 'iiiii@gmail.com'             , '1976-06-06'),
  1366. ('52125199997' , '0003' , '23416393000140', '77820021' , 'Recife'       , null  , 'Rua 16'                   , 21      , 'Valdemir' , 'Barbosa'    , 'Pinto'     , '1993333' , '9jf4'    , true  , '2016-09-11' , 'ppppp@gmail.com'             , '1956-06-06'),
  1367. ('50130400000' , '0003' , '23416393000140', '77016614' , 'Recife'       , null  , 'Rua vital dos sonhos'     , 100     , 'Agatha'   , 'Cristh'     , 'Belarmino' , '4527777' , 'f56g'    , true  , '2016-03-04' , 'bbbbb@gmail.com'             , '1978-09-09'),
  1368. ('96468754345' , '0003' , '23416393000140', '49069112' , 'Recife'       , null  , 'Rua nevermore'            , 0       , 'Camila'   , 'Fagundes'   , 'Nonato'    , '3194514' , 'kj004'   , true  , '2016-09-18' , 'ccccc@gmail.com'             , '1998-02-20'),
  1369. ('16512211111' , '0003',  '23416393000140', '72876146' , 'Olinda'       , null  , 'Quadra 98'                , 20.33   , 'Marilia'  , 'Queiroz'    , 'Falado'    , '4241847' , 'kd3io4'  , true  , '2016-02-12' , 'aaaaa@outlook.com'           , '1978-03-07'),
  1370. ('07617555555' , '0003' , '23416393000140', '89284561' , 'Olinda'       , null  , 'Rua gandalf'              , 0       , 'Diabolica', 'Meneses'    , 'da Cunha'  , '4764454' , 'i4hfi4h' , false , '2017-02-05' , 'mmmmm@outlook.com'           , '1997-12-11');
  1371.  
  1372.  
  1373. -- TABELA DE TELEFONE DE CLIENTE
  1374.  
  1375. CREATE TABLE telefone_cliente (
  1376.    
  1377.     CPF char(11) not null,
  1378.     telefone char(10),
  1379.     CONSTRAINT telefone_cliente_pk primary key (CPF, telefone),
  1380.     CONSTRAINT fk_telefonecliente foreign key (CPF) references cliente (CPF) ON DELETE CASCADE ON UPDATE CASCADE
  1381. );
  1382.  
  1383. -- inserindo valores na tabela telefone_cliente
  1384.  
  1385. INSERT INTO telefone_cliente ( CPF, telefone) VALUES
  1386.  
  1387.  ('54501233290' , '8134564432'),
  1388.  ('25251145314' , '8199890765'),
  1389.  ('77658476358' , '8599690359'),
  1390.  ('07617589689' , '4899097820'),
  1391.  ('07617589689' , '8191913012'),
  1392.  ('96468793068' , '7934876650'),
  1393.  ('96468793068' , '7988786534'),
  1394.  ('50519774647' , '8133234567');
  1395.  
  1396. -- TABELA DE SUGESTAO
  1397.  
  1398. CREATE TABLE sugestao (
  1399.    
  1400.     CPF_cliente char(11),
  1401.     id char(4),
  1402.     dia date,
  1403.     descricao varchar(30) not null,
  1404.     primary key (id , CPF_cliente),
  1405.     CONSTRAINT fk_sugestao foreign key (CPF_cliente) references cliente (CPF) ON DELETE CASCADE ON UPDATE CASCADE
  1406. );
  1407.  
  1408. -- inserindo valores na tabela sugestao
  1409.  
  1410. INSERT INTO  sugestao (CPF_cliente, id, dia , descricao) VALUES
  1411.  
  1412. ('54501233290' , '0001' , '2016-02-12' , 'mais entregadores'),
  1413. ('54501233290' , '0002' , '2016-03-09' , 'mais entregadores'),
  1414. ('54501233290' , '0003' , '2016-03-09' , 'mais entregadores'),
  1415. ('54501233290' , '0004' , '2016-03-09' , 'mais entregadores'),
  1416. ('54501233290' , '0005' , '2016-03-09' , 'mais entregadores'),
  1417. ('54501233290' , '0006' , '2016-03-09' , 'mais entregadores'),
  1418. ('54501233290' , '0007' , '2016-03-09' , 'mais entregadores'),
  1419. ('54501233290' , '0008' , '2016-03-09' , 'mais entregadores'),
  1420. ('54501233290' , '0009' , '2016-03-09' , 'mais entregadores');
  1421.  
  1422.  
  1423. -- TABELA DE RECLAMACAO
  1424.  
  1425. CREATE TABLE reclamacao (
  1426.  
  1427.     CPF_cliente char(11),
  1428.     id char(4),
  1429.     descricao varchar(30),
  1430.     motivo varchar(70),
  1431.     data_ocorrido date,
  1432.     data_reclamacao date,
  1433.     primary key (id, CPF_cliente),
  1434.     CONSTRAINT fk_reclamacao foreign key (CPF_cliente) references cliente (CPF) ON DELETE CASCADE ON UPDATE CASCADE
  1435. );
  1436.  
  1437. -- inserindo valores na tabela reclamacao
  1438.  
  1439. INSERT INTO reclamacao ( CPF_cliente , id , motivo, data_ocorrido, data_reclamacao) VALUES
  1440.  
  1441. ('54501233290' , '0001' , 'atendimento demorado', '2016-03-09' , '2016-03-13'),
  1442. ('54501233290' , '0002' , 'perto de vencer', '2016-03-09' , '2016-03-13'),
  1443. ('54501233290' , '0003' , 'o entregador nao chegou', '2016-03-01' , '2016-03-20'),
  1444. ('54501233290' , '0004' , 'erraram o endereco de entrega', '2016-03-09' , '2016-03-13'),
  1445. ('54501233290' , '0005' , 'atendimento muito demorado', '2016-03-09' , '2016-03-10'),
  1446. ('54501233290' , '0006' , 'site fora do ar', '2016-04-01' , '2016-04-01'),
  1447. ('54501233290' , '0007' , 'atendimento muito demorado', '2016-03-09' , '2016-03-13');
  1448.  
  1449.  
  1450. -- TABELA DE PROMOCAO
  1451.  
  1452. CREATE TABLE promocao (
  1453.  
  1454.     cod char(4),
  1455.     seq_filial char(4),
  1456.     cnpj_matriz char(14),
  1457.     nome varchar(30),
  1458.     data_inicio date not null,
  1459.     percentual_reducao float not null CHECK ( percentual_reducao > 0 ),
  1460.     data_fim date,
  1461.     obs varchar(30),
  1462.     descricao varchar(50),
  1463.     primary key (cod),
  1464.     foreign key (seq_filial, cnpj_matriz ) references filial (seq , CNPJ_Matriz)
  1465.  
  1466. );
  1467.  
  1468. -- inserindo valores na tabela promocao
  1469.  
  1470. INSERT INTO promocao (cod, seq_filial , cnpj_matriz , nome , data_inicio, percentual_reducao , data_fim, obs, descricao) VALUES
  1471.  
  1472. ('0001' , '0001 ',  '23416393000114', 'Um barato no pedaco' , '2017-02-05', 0.13 , '2017-03-05' , 'n inclui importados' , 'promocao para laticinios'),
  1473. -- ('0002' , '0001' ,  '23416393000114', 'Queima de estoque' , '2016-12-22' , 0.33 , '2016-12-31' , NULL , NULL),
  1474. ('0003' , '0002',  '23416393000114','Mes da verdura' , '2017-08-12' , 0.26 , '2017-10-12' , 'n inclui conservas' , 'promocao para hortifruti' ),
  1475. ('0004' , '0002' ,  '23416393000114', 'Mes da conserva' , '2017-11-20' , 0.21 , '2017-12-20', NULL , 'apenas conservas' ),
  1476. -- ('0005 ', '0001' , '23416393000114', 'Desinfetantes em queima' , '2018-01-02' , 0.24 , '2018-02-02' , NULL , 'apenas desinfetantes'),
  1477. ('0006' , '0002' , '23416393000114', 'Promocao de amaciante' , '2017-09-15' , 0.12, '2017-11-15' , NULL , 'amaciantes nacionais'),
  1478. -- ('0007' , '0001' , '23416393000114', 'Laticinios vao a loucura' , '2018-01-02', 0.29, '2018-03-02' , NULL , 'reducao de preco laticinios'),
  1479. ('0008' , '0002' ,  '23416393000114','Promocao de Sabao em po' , '2017-06-15' , 0.19 , '2017-07-15' , NULL , 'promocao sabao em po');
  1480.  
  1481.  
  1482. -- TABELA DE ITEM PEDIDO
  1483.  
  1484. CREATE TABLE item_pedido (
  1485.  
  1486.     seq char(4),
  1487.     cod_produto_ref char(4),
  1488.     cod_pedido_fornecedor char(4)   ,
  1489.     quantidade int not null CHECK (quantidade > 0) ,
  1490.     preco_unitario float  not null CHECK (preco_unitario  > 0),
  1491.     primary key (cod_produto_ref , cod_pedido_fornecedor),
  1492.     key(seq),
  1493.     foreign key (cod_produto_ref) references produto_ref (cod),
  1494.     foreign key (cod_pedido_fornecedor) references pedido_fornecedor(codpdfornecedor)
  1495. );
  1496.  
  1497.  
  1498.  
  1499. INSERT INTO item_pedido (seq , cod_produto_ref , cod_pedido_fornecedor, quantidade , preco_unitario) VALUES
  1500.  
  1501. ('0001' , '0001' ,'0001' , 10 , 10.4),
  1502. ('0002' , '0002' ,'0002' , 5 , 4.60),
  1503. ('0003' , '0003' ,'0003' , 40 , 5.44),
  1504. ('0004' , '0004' ,'0004' , 12 , 4.41),
  1505. ('0005' , '0005' ,'0005' , 7 , 6.43),
  1506. ('0006' , '0006' ,'0006' , 9 , 7.2),
  1507. ('0007' , '0007' ,'0007' , 3 , 8.3),
  1508. ('0008' , '0008' ,'0008' , 5 , 9);
  1509.  
  1510.  
  1511.  
  1512.  
  1513. -- TABELA DE LOTE
  1514. CREATE TABLE lote (
  1515.    
  1516.     cod char(4),
  1517.     cod_pedido_fornecedor char(4),
  1518.     descricao varchar(30),
  1519.     data_chegada date not null,
  1520.     primary key( cod) ,
  1521.     foreign key (cod_pedido_fornecedor) references pedido_fornecedor (codpdfornecedor)
  1522.  
  1523. );
  1524.  
  1525.  
  1526. INSERT INTO lote ( cod, cod_pedido_fornecedor, descricao , data_chegada) VALUES
  1527.  
  1528. ( '0001' , '0001', 'Lote de coca-cola ' , '2017-03-03' ),
  1529. ( '0002' , '0002', 'Lote de pepsi ' , '2017-03-20' ),
  1530. ( '0003' , '0003', 'Lote de margarina deline ' , '2017-05-03' ),
  1531. ( '0004' , '0004', 'Lote de açucar pretinho ' , '2017-02-03' ),
  1532. ( '0005' , '0005', null , '2017-04-03' );
  1533.  
  1534.  
  1535. -- TABELA DE ITEM DE ESTOQUE
  1536.  
  1537. CREATE TABLE item_estoque (
  1538.    
  1539.     cod_lote char(4),
  1540.     id_estoque char(4),
  1541.     cod_produto char(4),
  1542.     id_avaria char(4),
  1543.     id_prateleira char(4),
  1544.     data_validade date ,
  1545.     data_fabricacao date,
  1546.     data_entrada date not null,
  1547.     valor_compra float CHECK (valor_compra > 0),
  1548.     quantidade int CHECK (quantidade > 0),
  1549.     primary key (cod_lote, id_estoque , cod_produto),
  1550.     foreign key (cod_lote) references lote (cod),
  1551.     foreign key (id_estoque) references estoque (id),
  1552.     CONSTRAINT fk_produtoestoque foreign key ( cod_produto ) references produto_ref( cod) ON DELETE CASCADE ON UPDATE CASCADE,
  1553.     foreign key (id_avaria ) references avaria ( id),
  1554.     foreign key (id_prateleira ) references prateleira ( codigo)
  1555.    
  1556. );
  1557.  
  1558.  
  1559. INSERT INTO item_estoque (cod_lote, id_estoque , cod_produto , id_avaria , id_prateleira , data_validade ,
  1560.  data_fabricacao , data_entrada , valor_compra, quantidade)  VALUES
  1561.  ('0001' , '0001' , '0001' , null , '0001', '2018-04-04' , '2018-01-10' , '2018-01-14' , 60.30 , 8 ),
  1562.  ('0002' , '0001' , '0002' , null , '0010', '2018-03-04' , '2018-01-04' , '2018-01-14' , 200 , 20 ),
  1563.  ('0003' , '0002' , '0003' , null , '0006', '2018-04-05' , '2018-01-02' , '2018-01-14' , 43.70 , 10 ),
  1564.  ('0004' , '0003' , '0004' , null , '0011', '2018-02-07' , '2018-01-10' , '2018-01-14' , 24.20 , 4 ),
  1565.  ('0005' , '0004' , '0005' , null , '0013', '2018-01-01' , '2017-11-10' , '2018-01-14' , 90.32 , 12 );
  1566.  
  1567.  
  1568.  
  1569.  
  1570. -- TABELA DE PERDA DE PRODUTO
  1571.  
  1572. CREATE TABLE perda (
  1573.  
  1574.     seq char(4),
  1575.     cod_lote char(4),
  1576.     cod_produto char(4),
  1577.     id_estoque char(4),
  1578.     cpf_gerente char(11),
  1579.     dia date not null ,
  1580.     quantidade_perdida int CHECK (quantidade_perdida > 0) ,
  1581.     motivo varchar(80),
  1582.     primary key (seq , cod_lote , cod_produto , id_estoque),
  1583.     foreign key (cpf_gerente) references gerente (cpf),
  1584.     foreign key ( id_estoque , cod_lote , cod_produto ) references item_estoque(id_estoque , cod_lote, cod_produto ) ON DELETE CASCADE ON UPDATE CASCADE
  1585.  
  1586. );
  1587.  
  1588. INSERT INTO perda ( seq , cod_lote, cod_produto, id_estoque, cpf_gerente , dia , quantidade_perdida , motivo) VALUES
  1589. ( '0001' , '0001' , '0002', '0001' , null , '2018-03-04' , 2 , ' nao descoberto ' ),
  1590. ( '0002' , '0003' , '0004', '0002' , null , '2018-03-06' , 6 , ' nao descoberto ' );
  1591.  
  1592. INSERT INTO perda ( seq , cod_lote, cod_produto, id_estoque, cpf_gerente , dia , quantidade_perdida , motivo) VALUES
  1593. ( '0001' , '0001' , '0001', '0001' , null , '2018-03-04' , 2 , ' burrice ' );
  1594.  
  1595.  
  1596.  
  1597. -- TABELA DE RELACIONAMENTO TEM DE FUNCIONARIO
  1598.  
  1599. CREATE TABLE tem (
  1600.      
  1601.     id_jornada char(4),
  1602.     id_turno char(4),
  1603.     id_dia char(4),
  1604.     primary key (id_jornada , id_turno , id_dia ),
  1605.     foreign key (id_jornada) references jornadatrabalho(id),
  1606.     foreign key (id_turno) references turno(cod),
  1607.     foreign key (id_dia) references dia (sequencial)
  1608.    
  1609. );
  1610.  
  1611. -- inserindo valores na tabela tem
  1612.  
  1613. INSERT INTO tem (id_jornada , id_turno, id_dia ) VALUES
  1614.  
  1615. ('0001' , '0001' , '0001' ),
  1616. ('0001' , '0002' , '0002' ),
  1617. ('0001' , '0003' , '0003' ),
  1618. ('0001' , '0002' , '0004' ),
  1619. ('0001' , '0002' , '0005' ),
  1620. ('0001' , '0002' , '0006' ),
  1621. ('0001' , '0001' , '0007' ),
  1622. ('0002' , '0010' , '0001' ),
  1623. ('0002' , '0006' , '0005' ),
  1624. ('0003' , '0009' , '0006' ),
  1625. ('0004' , '0001' , '0001' );
  1626.  
  1627.  
  1628. -- TABELA RELACIONAMENTO REALIZA CURSO
  1629.  
  1630. CREATE TABLE realizacurso (
  1631.    
  1632.     cpf_fun char(11),
  1633.     id_curso char(4),
  1634.     dt_inicio date,
  1635.     dt_fim date,
  1636.     primary key (cpf_fun , id_curso),
  1637.     foreign key (cpf_fun) references funcionario(CPF) on delete cascade on update cascade,
  1638.     foreign key (id_curso) references curso (id)
  1639. );
  1640.  
  1641. -- inserindo valores na tabela realiza_curso
  1642.  
  1643. INSERT INTO realizacurso ( cpf_fun , id_curso ,  dt_inicio , dt_fim) VALUES
  1644.  
  1645. ( '77491222226', '2101' , '2016-02-13' , '2016-03-15' ),
  1646. ('77491222226' , '8012' , '2016-04-09' , '2016-06-09' ),
  1647. ( '32568071001' , '2101' ,'2016-02-13' , '2016-03-15' ),
  1648. ( '45321186898' , '8012' , '2016-04-09' , '2016-06-09' ),
  1649. ('14073416260' , '8012' , '2016-04-09' , '2016-06-09' ),
  1650. ( '88356795591' , '8012' , '2016-04-09' , '2016-06-09' );
  1651.  
  1652. -- TABELA DE GERENCIA DE ESTOQUE/* createdyes 13 show
  1653.  
  1654. CREATE TABLE gerencia_estoque (
  1655.    
  1656.     cpf_super_estoque char(11),
  1657.     id_estoque char(4),
  1658.     dt_fim date,
  1659.     dt_inicio date,
  1660.     primary key (cpf_super_estoque , id_estoque),
  1661.     foreign key (cpf_super_estoque) references SupervisorEstoque (CPF) on delete cascade on update cascade,
  1662.     foreign key (id_estoque) references estoque(id)
  1663.    
  1664. );
  1665.  
  1666.  
  1667.  
  1668. -- inserindo valores na tabela gerencia_estoque
  1669.  
  1670. INSERT INTO gerencia_estoque (cpf_super_estoque, id_estoque , dt_fim, dt_inicio) VALUES
  1671.  
  1672. ('14073416260' , '0001' ,'2017-03-02' ,'2016-03-02'),
  1673. ( '85902755239' , '0002' , '2017-04-15' , '2016-02-15'),
  1674. ( '88356795591' , '0003' , '2016-01-01' , '2014-02-13'),
  1675. ( '14073416260' , '0002 ' , '2013-06-22' , '2012-06-10'),
  1676. ( '85902755239' , '0003'  ,'2017-09-02', '2016-01-02' );
  1677.  
  1678.  
  1679.  
  1680.  
  1681. -- VIEWS
  1682.  
  1683. create view produtodate as select * from produto_ref inner join item_estoque on produto_ref.cod = item_estoque.cod_produto
  1684. left join avaria on item_estoque.id_avaria = avaria.id;
  1685.  
  1686. create view comprabetweendates as select * from compra inner join item_compra on compra.codd = item_compra.cod_compra inner join produto_ref
  1687. on produto_ref.cod = item_compra.cod_produto inner join marca on marca.codmarca = produto_ref.id_marca
  1688. inner join categoria on categoria.codcategoria = produto_ref.id_categoria
  1689. inner join subcategoria on subcategoria.cod_categoria = categoria.codcategoria
  1690. and subcategoria.codsubcategoria = produto_ref.id_subcategoria;
  1691.  
  1692.  
  1693. create view relatoriopf as select codpdfornecedor , total_desconto , valor_total_IPI , CFOPpf , valor_total , pedido_fornecedor.dia , pedido_fornecedor.statospf , valor_frete, -- pedidofornecedor
  1694. idfatura , data_vencimento, valor_pago_atual , valor_total_final , data_emissao , fatura.statos , data_paga , multa, -- fatura
  1695. codtipo_pagamento, descricao , -- tipo_pagamento
  1696. NFE , cod_pagamento, CFOP , IPI, -- nf fornecedor
  1697. CPF -- gerente
  1698. from pedido_fornecedor left join fatura on pedido_fornecedor.codpdfornecedor = fatura.cod_pedido_fornecedor
  1699. left join pagamento on pagamento.id_fatura = fatura.idfatura left join tipo_pagamento on tipo_pagamento.codtipo_pagamento =
  1700. pagamento.cod_tipo_pagamento left join nota_fiscal_fornecedor on nota_fiscal_fornecedor.cod_pagamento = pagamento.cod
  1701. left join gerente on gerente.CPF = pedido_fornecedor.CPF_gerente;
  1702.  
  1703.  
  1704.  
  1705.  
  1706. -- FUNCOES, TRIGGERS E PROCEDURES
  1707.  
  1708.  
  1709.     -- Função que retorna o total a ser pago pelo Pedido do Fornecedor
  1710.     -- recebe como argumentos
  1711. delimiter ;;
  1712.  
  1713. CREATE function totalPedidoFornecedor ( cod char(4) )
  1714. returns float
  1715. deterministic
  1716. begin
  1717.        
  1718.         declare valor float;
  1719.         declare quatd int;
  1720.                     select SUM( preco_unitario ) into valor
  1721.                     FROM item_pedido
  1722.                     WHERE cod_pedido_fornecedor = cod;
  1723.                    
  1724.                     select SUM( quantidade ) into quatd
  1725.                     FROM item_pedido
  1726.                     WHERE cod_pedido_fornecedor = cod;
  1727.                    
  1728.         return valor*quatd;
  1729.        
  1730.        
  1731.        
  1732. end ;;
  1733.  
  1734.  
  1735.  
  1736.  
  1737.  
  1738. /*  Função que retorna se o produto está fora da validade
  1739.     Caso o produto esteja vencido , ele retorna TRUE ,
  1740.     caso contrário retorna FALSE
  1741. */
  1742.  
  1743. delimiter ;;
  1744.  
  1745. create function estaVencido ( cod char(4), numeroDias int )
  1746. returns boolean
  1747.  
  1748. begin
  1749.  
  1750.         declare result boolean;
  1751.         declare validade date;
  1752.         declare hoje date;
  1753.  
  1754.         select data_validade into validade
  1755.         from item_estoque
  1756.         where cod_produto = cod;
  1757.        
  1758.        
  1759.         select date_add(CURDATE(), INTERVAL numeroDias DAY) into hoje;
  1760.  
  1761.        
  1762.         if datediff(validade , hoje ) < 0 then
  1763.             set result = true;
  1764.        
  1765.         else
  1766.             set result = false;
  1767.         end if;
  1768.        
  1769.         return result;
  1770. end ;;
  1771.  
  1772.  
  1773.  
  1774.  
  1775. delimiter ;;
  1776.  
  1777. create function produtosuficiente ( cod char(4) , quantidade2 int )
  1778. returns boolean
  1779.  
  1780. begin
  1781.    
  1782.     declare resultado boolean;
  1783.     declare qtdcomp int;
  1784.    
  1785.    
  1786.     select SUM(quantidade) into qtdcomp
  1787.     from item_estoque
  1788.     where cod_produto = cod;
  1789.    
  1790.     if quantidade2 > qtdcomp then
  1791.         set resultado = false;
  1792.    
  1793.     else
  1794.         set resultado = true;
  1795.        
  1796.     end if;
  1797.    
  1798.     return resultado;
  1799.    
  1800. end ;;
  1801.  
  1802.  
  1803.  
  1804. delimiter ;;
  1805.  
  1806. create trigger funcionarioadicionado
  1807. after insert on funcionario
  1808. for each row
  1809. begin
  1810.    
  1811.     if new.seq_filial is not null then
  1812.         update filial
  1813.         set qtd_func = qtd_func + 1
  1814.         where seq = new.seq_filial;
  1815.    
  1816.     end if;
  1817.    
  1818. end ;;
  1819.  
  1820. delimiter ;;
  1821.  
  1822. create trigger funcionarioremovido
  1823. after delete on funcionario
  1824. for each row
  1825. begin
  1826.  
  1827.     if old.seq_filial is not null then
  1828.         update filial
  1829.         set qtd_func = qtd_func - 1
  1830.         where seq = old.seq_filial;
  1831.        
  1832.     end if;
  1833.  
  1834. end ;;
  1835.  
  1836.  
  1837. delimiter ;;
  1838.  
  1839. create trigger adicionaveiculogaragem
  1840. after insert on veiculo
  1841. for each row
  1842. begin
  1843.        
  1844.         if new.cod_garagem is not null then
  1845.             update garagem
  1846.             set num_veiculos_atual = num_veiculos_atual + 1
  1847.             where cod = new.cod_garagem;
  1848.        
  1849.         end if;
  1850.  
  1851.  
  1852. end ;;
  1853.  
  1854. delimiter ;;
  1855.  
  1856. create trigger removeveiculogaragem
  1857. after delete on veiculo
  1858. for each row
  1859. begin
  1860.  
  1861.     if old.cod_garagem is not null then
  1862.         update garagem
  1863.         set num_veiculos_atual = num_veiculos_atual - 1
  1864.         where cod = old.cod_garagem;
  1865.        
  1866.     end if;
  1867.  
  1868. end ;;
  1869.  
  1870.  
  1871.  
  1872.  
  1873. delimiter ;;
  1874.  
  1875. create trigger descontocompraprogamada
  1876. after insert on compra_programada
  1877. for each row
  1878. begin
  1879.    
  1880.     declare percentualdesconto float;
  1881.    
  1882.     if new.cod is not null then
  1883.    
  1884.     select percentual_reducao into percentualdesconto
  1885.     from promocao
  1886.     join filial
  1887.     on promocao.seq_filial = filial.seq
  1888.     join cliente
  1889.     on cliente.seq_filial = filial.seq
  1890.     join compra
  1891.     on cliente.CPF = compra.cpfcliente
  1892.     where new.cod = compra.codd;
  1893.    
  1894.     update compra
  1895.     set valor_total_desconto = valor_total * percentualdesconto
  1896.     where new.cod = compra.codd;
  1897.    
  1898.    
  1899.     end if;
  1900.  
  1901. end ;;
  1902.  
  1903.  
  1904.  
  1905. delimiter ;;
  1906.  
  1907. create procedure updateaftercompra( in codcompra char(4) )
  1908. begin
  1909.    
  1910.         declare done int default 0;
  1911.         declare qtd int;
  1912.         declare codigoproduto char(4);
  1913.         declare itemcompraqtd cursor for select quantidade , cod_produto
  1914.                                         from item_compra
  1915.                                         where item_compra.cod_compra = codcompra;
  1916.                                        
  1917.         declare continue handler for not found set done = 1;
  1918.        
  1919.         open itemcompraqtd;
  1920.         stoop : LOOP
  1921.            
  1922.             fetch itemcompraqtd into qtd, codigoproduto;
  1923.            
  1924.             if done = 1 then
  1925.             leave stoop;
  1926.             end if;
  1927.        
  1928.            
  1929.             update item_estoque
  1930.             set quantidade =  (item_estoque.quantidade-qtd)
  1931.             where item_estoque.cod_produto = codigoproduto and item_estoque.quantidade >= qtd;
  1932.            
  1933.            
  1934.         END LOOP stoop;
  1935.            
  1936.            
  1937.         close itemcompraqtd;
  1938.  
  1939. end ;;
  1940.  
  1941.  
  1942.  
  1943. delimiter ;;
  1944.  
  1945. create procedure gerarfaturaepagamento( in cod char(4) , in dataemissao date  )
  1946. begin
  1947.        
  1948.         declare datavencimento date;
  1949.         declare codfatura mediumint;
  1950.        
  1951.         select date_add(dataemissao, INTERVAL 15 DAY) into datavencimento;
  1952.        
  1953.        
  1954.         insert into fatura  ( cod_pedido_fornecedor , data_vencimento , valor_pago_atual, valor_total_final ,
  1955.         data_emissao , statos , data_paga , multa ) values
  1956.         (cod , datavencimento , null , null , dataemissao , false , null , null );
  1957.        
  1958.         select idfatura into codfatura
  1959.         from fatura
  1960.         where fatura.cod_pedido_fornecedor = cod;
  1961.  
  1962.        
  1963.         insert into pagamento ( id_fatura ,cod_compra , cod_tipo_pagamento  , valor_pago , dia ,  statos, tipo_pagamento ) values
  1964.         (codfatura , null , null , null , null , false , null);
  1965.    
  1966.  
  1967.  
  1968. end ;;
  1969.  
  1970.  
  1971.  
  1972.  
  1973.  
  1974. SET foreign_key_checks = 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement