1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
|
-- Todos los queries necesarios para crear las bases de datos iniciales de bulmafact
-- por separado en un solo script, cada uno con nombre y senyal en --#########
SET DATESTYLE TO European;
--Comentado en otra parte para ponerlo aqui arriba por si lo podemos quitar y dejarlo en el psql del admin
-- ahora lo de bulmafact **************************************************************************
-- bulmafact_schema.sql
-- ----------------------------------------------------------------------------------------
-- (C) Joan Miquel Torrer Rigo & Tomeu Borras Riera & Mateu Borras Marco, Agosto 2004
-- joanmi@bulma.net, tborras@conetxia.com mborras@conetxia.com
-- Este documento esta licenciado bajo licencia GPL, el cual no escribimos aqui por pereza.
-- ----------------------------------------------------------------------------------------
-- psql xxxx < bulmafact-0_0_1.sql
-- ---------------------------------------------------------------------------------------
SET SESSION AUTHORIZATION 'postgres';
SET search_path = public, pg_catalog;
SET DATESTYLE TO European;
CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler
AS '$libdir/plpgsql', 'plpgsql_call_handler'
LANGUAGE c;
--CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler
-- AS '/usr/lib/postgresql/8.0/lib/plpgsql.so', 'plpgsql_call_handler'
-- LANGUAGE c;
CREATE TRUSTED PROCEDURAL LANGUAGE plpgsql HANDLER plpgsql_call_handler;
-- NOTACION:
-- Considerar las siguientes opciones de codificaciᅵ:
-- Los nombres de tabla estan escritos SIEMPRE en singular.
-- Todos los campos de una tabla terminan siempre con el nombre de la tabla (salvo las claves foraneas).
-- Las claves foraneas tienen el mismo nombre que el campo con que se corresponden en la tabla relacionada.
-- En caso de que haya diversas claves foraneas referentes al mismo campo, el criterio es que una de ellas tenga el nombre del campo con el que se corresponde y la otra tenga un nombre significativo.
-- Los campos de clave automatica empiezan por id
-- Los enums se simulan (normalmente) con campos numericos, el significado de los valores debe estar
-- explicado en este archivo.
-- La tabla de configuraciᅵ.
-- En esta tabla se guardan parametros que el programa va a utilizar.
-- Como por ejemplo el numero de dᅵitos por defecto de las cuentas o el asiento inteligente que se enlaza con
-- facturacion.
-- Tiene tres campos
-- idconfiguracion: el identificador (No tiene ningn uso especial).
-- nombre: El nombre del parametro de configuracion.
-- valor: El valor que toma dicho parametro.
CREATE TABLE configuracion (
nombre character varying(25) PRIMARY KEY,
valor character varying(350)
);
-- Codi: Clau artificial.
-- Descripcio: Nom identificatiu o descripciᅵbreu.
-- Dies_1T: Dies abans del primer termini computant els blocs de 30 com a mesos naturals.
-- Descompte: Descompte automᅵic per l's d'aquesta forma de pagament.
CREATE TABLE forma_pago (
idforma_pago serial PRIMARY KEY,
descforma_pago character varying(500),
dias1tforma_pago integer,
descuentoforma_pago numeric(12,2)
);
-- Codigo: Clave artificial.
-- Nombre: Nombre identificativo del almacᅵ.
-- diralmacen: Direcciᅵ del almacᅵ.
-- poblalmacen: Poblaciᅵ del almacᅵ.
-- cpalmacenc: cᅵigo postal almacᅵ.
-- telfalmacen: Telᅵono del almacᅵ.
-- faxalmacen: Fax del almacᅵ.
-- emailalmacen: correo electrᅵico del almacᅵ.
-- presupuestoautoalmacen el numero de presupuesto es automatico? N=No,
-- albaranautoalmacen el numero de albaran es automatico? N=No,
-- facturaautoalmacen el numero de es automatico? N=No,
CREATE TABLE almacen (
idalmacen serial PRIMARY KEY,
codigoalmacen numeric(5, 0) NOT NULL,
nomalmacen character varying(50),
diralmacen character varying(150),
poblalmacen character varying(50),
cpalmacen character varying(20),
telalmacen character varying(20),
faxalmacen character varying(20),
emailalmacen character varying(100),
inactivoalmacen character(1),
UNIQUE(codigoalmacen)
);
--
-- TOC entry 47 (OID 3090354)
-- Name: staff; Type: TABLE; Schema: public; Owner: fewa
--
CREATE TABLE trabajador (
idtrabajador serial PRIMARY KEY,
nomtrabajador character varying NOT NULL,
apellidostrabajador character varying,
dirtrabajador character varying,
nsstrabajador character varying,
teltrabajador character varying,
moviltrabajador character varying,
emailtrabajador character varying,
fototrabajador character varying,
activotrabajador boolean DEFAULT TRUE NOT NULL
);
-- Tabla de paᅵes
-- cod2: cᅵigo de dos dᅵitos
-- cod3: cᅵigo de tres dᅵitos
-- desc: descripciᅵ del paᅵ
CREATE TABLE pais (
idpais serial PRIMARY KEY,
cod2pais character varying(2),
cod3pais character varying(3),
descpais character varying(50)
);
-- Tabla de monedas
-- cod2: cᅵigo de dos dᅵitos
-- cod3: cᅵigo de tres dᅵitos
-- desc: descripciᅵ de la moneda
CREATE TABLE moneda (
idmoneda serial PRIMARY KEY,
cod2moneda character varying(2),
cod3moneda character varying(3),
descmoneda character varying(50)
);
-- Esta tabla contiene las descripciones de los ivas que se pueden aplicar.
-- Descripcio: Text descriptiu del tipus d'IVA.
CREATE TABLE tipo_iva (
idtipo_iva serial PRIMARY KEY,
desctipo_iva character varying(2000)
);
-- Esta tabla contiene las tasas de cada tipo de iva a partir de una fecha dada.
-- porcentasa_iva contiene el porcentaje de la tasa de iva a aplicar.
-- fechatasa_iva es la fecha de entrada en vigor del % de IVA para el tipo descrito.
CREATE TABLE tasa_iva (
idtasa_iva serial PRIMARY KEY,
idtipo_iva integer REFERENCES tipo_iva(idtipo_iva) NOT NULL,
porcentasa_iva NUMERIC(5, 2) NOT NULL,
fechatasa_iva date NOT NULL,
UNIQUE (idtipo_iva, fechatasa_iva)
);
-- Tabla con series de Iva, cᅵigo i descripciᅵ
-- Bᅵicamente sirve para garantizar la integridad referencial en las series de facturaciᅵ
-- Deberᅵn existir en contabilidad tambien.
CREATE TABLE serie_factura (
-- idserie_factura serial PRIMARY KEY,
codigoserie_factura character varying (6) PRIMARY KEY,
descserie_factura character varying(50) NOT NULL,
UNIQUE (codigoserie_factura)
);
-- codigofamilia cᅵigo de la familia.
-- nombrefamilia nombre de la familia
-- descfamilia descripciᅵ extendida de la familia.
-- codcompfamilia cᅵigo compuesto de familia: Es la concatenaciᅵ del cᅵigo de familia con sus cᅵigos padres.
-- codigocompletofamilia Este campo es de sᅵo lectura, no se puede escribir sobre ᅵ.
CREATE TABLE familia (
idfamilia serial PRIMARY KEY,
codigofamilia character varying(12) NOT NULL,
nombrefamilia character varying(50) NOT NULL,
descfamilia character varying(300),
padrefamilia integer REFERENCES familia(idfamilia),
codigocompletofamilia character varying(50) UNIQUE
);
CREATE FUNCTION calculacodigocompletofamilia () RETURNS "trigger"
AS '
DECLARE
as RECORD;
codigocompleto character varying(50);
BEGIN
codigocompleto := NEW.codigofamilia;
SELECT INTO as codigocompletofamilia FROM familia WHERE idfamilia = NEW.padrefamilia;
IF FOUND THEN
codigocompleto := as.codigocompletofamilia || codigocompleto;
END IF;
NEW.codigocompletofamilia := codigocompleto;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER calculacodigocompletofamiliatrigger
BEFORE INSERT OR UPDATE ON familia
FOR EACH ROW
EXECUTE PROCEDURE calculacodigocompletofamilia();
CREATE FUNCTION propagacodigocompletofamilia () RETURNS "trigger"
AS '
DECLARE
BEGIN
UPDATE familia SET codigocompletofamilia=codigocompletofamilia WHERE padrefamilia = NEW.idfamilia;
UPDATE articulo SET codigocompletoarticulo = codigocompletoarticulo WHERE articulo.idfamilia = NEW.idfamilia;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER propagacodigocompletofamiliatrigger
AFTER UPDATE ON familia
FOR EACH ROW
EXECUTE PROCEDURE propagacodigocompletofamilia();
-- Esta funciᅵ nos da el identificador de familia dado un cᅵigo.
--CREATE OR REPLACE FUNCTION idfamilia (text) RETURNS "trigger"
-- AS '
--DECLARE
-- codigo ALIAS FROR $1;
-- mrecord RECORD;
--BEGIN
-- FOR mrecord IN SELECT SUM(baseiva) AS suma, SUM(ivaiva) AS sumaiva FROM iva WHERE iva.idregistroiva=NEW.idregistroiva LOOP
-- UPDATE registroiva SET baseimp=mrecord.suma, iva=mrecord.sumaiva WHERE idregistroiva=NEW.idregistroiva;
-- END LOOP;
-- RETURN NEW;
--END;
--' LANGUAGE plpgsql;
-- El tipo de artᅵulo es una tabla que permite crear una forma alternativa de agrupar los artᅵulos.
-- codigo: identificador del tipo.
-- desc:
CREATE TABLE tipo_articulo (
idtipo_articulo serial PRIMARY KEY,
codtipo_articulo character varying(10),
desctipo_articulo character varying(50)
);
-- Codigo: Clave artificial.
-- Nombre: Descripciᅵ corta del artᅵulo.
-- abrev: Nombre abreviado del articulo (para tpv o cartelitos estanterias...)
-- idtipo_articulo: identificador de tipo de artᅵulo que se utilizarᅵpara agrupar artᅵulos como clasificaciᅵ alternativa a el surtido (familias).
-- Observaciones: Campo de texto para a comentarios y observaciones.
-- El campo codigocompletoarticulo sᅵo puede ser de lectura.
CREATE TABLE articulo (
idarticulo serial PRIMARY KEY,
codarticulo character varying(12),
nomarticulo character varying(50),
abrevarticulo character varying(30),
obserarticulo character varying(2000),
presentablearticulo boolean NOT NULL DEFAULT TRUE,
controlstockarticulo boolean NOT NULL DEFAULT TRUE,
idtipo_articulo numeric(2, 0) REFERENCES tipo_articulo(idtipo_articulo),
idtipo_iva integer REFERENCES tipo_iva (idtipo_iva),
codigocompletoarticulo character varying(100) UNIQUE,
idfamilia integer REFERENCES familia(idfamilia) NOT NULL,
stockarticulo numeric(12,2) DEFAULT 0,
inactivoarticulo character(1),
-- ATENCION, este campo no da el pvp real del artᅵulo, solo es una de las multiples formas de acceder al precio del articulo.
-- Para obtener el precio de un artᅵulo se debe usar la funcion pvparticulo.
-- Para saber el iva correspondiente a un articulo se debe usar la funciᅵ ivaarticulo.
pvparticulo numeric(12,2) NOT NULL DEFAULT 0
);
create or replace function is_number(varchar) returns boolean as
'select $1 ~ ''^[-+]?[0-9]+$''' strict immutable language sql;
CREATE OR REPLACE FUNCTION to_number(character varying) RETURNS INT8 AS '
DECLARE
BEGIN
RAISE NOTICE ''to_number %'', $1;
RETURN CAST(text($1) AS INT8);
END
' LANGUAGE 'plpgsql';
CREATE OR REPLACE FUNCTION calculacodigocompletoarticulo () RETURNS "trigger"
AS '
DECLARE
as RECORD;
codigocompleto character varying(100);
codnumeric integer;
BEGIN
-- Lo primero comprobamos el el código del articulo no esté vacio y de ser asà lo llenamos.
IF NEW.codarticulo = '''' THEN
SELECT INTO as max(codarticulo) AS m FROM articulo WHERE idfamilia = NEW.idfamilia;
IF FOUND THEN
IF is_number(as.m) THEN
codnumeric := to_number(as.m);
codnumeric := codnumeric +1;
NEW.codarticulo := CAST (codnumeric AS varchar);
WHILE length(NEW.codarticulo) < 4 LOOP
NEW.codarticulo := ''0'' || NEW.codarticulo;
END LOOP;
ELSE
NEW.codarticulo := ''0000'';
END IF;
ELSE
NEW.codarticulo = ''0000'';
END IF;
END IF;
codigocompleto := NEW.codarticulo;
SELECT INTO as codigocompletofamilia FROM familia WHERE idfamilia = NEW.idfamilia;
IF FOUND THEN
codigocompleto := as.codigocompletofamilia || codigocompleto;
END IF;
NEW.codigocompletoarticulo := codigocompleto;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER calculacodigocompletoarticulotrigger
BEFORE INSERT OR UPDATE ON articulo
FOR EACH ROW
EXECUTE PROCEDURE calculacodigocompletoarticulo();
CREATE OR REPLACE FUNCTION modificadostock () RETURNS "trigger"
AS '
DECLARE
cant numeric;
as RECORD;
BEGIN
IF NEW.stockarticulo <> OLD.stockarticulo THEN
cant := NEW.stockarticulo - OLD.stockarticulo;
FOR as IN SELECT * FROM comparticulo WHERE idarticulo = NEW.idarticulo LOOP
UPDATE articulo SET stockarticulo = stockarticulo + cant * as.cantcomparticulo WHERE idarticulo = as.idcomponente;
END LOOP;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER modificastocktrigger
AFTER UPDATE ON articulo
FOR EACH ROW
EXECUTE PROCEDURE modificadostock();
-- Componentes de Artᅵulo
CREATE TABLE comparticulo (
idarticulo integer NOT NULL REFERENCES articulo(idarticulo),
cantcomparticulo integer NOT NULL DEFAULT 1,
idcomponente integer NOT NULL REFERENCES articulo(idarticulo),
PRIMARY KEY (idarticulo, idcomponente)
);
-- Los proveedores son los que nos suminstran articulos y/o servicios.
-- COMPROVACIONS D'INTEGRITAT>Genᅵiques:
-- 1 Article tᅵ1 sol proveᅵor principal.
-- 1 Article tᅵ1 sol proveᅵor referent.
-- CAMPOS
-- ======
-- Codi: Clau artificial.
-- Nom: Nom comercial o fiscal.
-- Nom_alternatiu: Nom comercial o fiscal.
-- CIF: Codi d'IdentificaciᅵFiscal.
-- CodiCli: Codi de client amb que ens facturen. ᅵil per a identificar-nos.
-- C_Banc
-- Comentaris
-- Adreᅵ: Adreᅵ.
-- Poblaciᅵ Poblaciᅵ
-- CProv: Codi de provincia (dos primers dᅵits del codi postal).
-- sCP: Tres darrers dᅵits del codi postal.
-- Telf: Telᅵon.
-- Fax: Fax.
-- Email: eMail.
-- Url: Url.
-- CompteWeb: Dades de login si disposen de tenda o tarifes en lᅵia
CREATE TABLE proveedor (
idproveedor serial PRIMARY KEY,
nomproveedor character varying(200),
nomaltproveedor character varying(200),
cifproveedor character varying(12) UNIQUE,
codicliproveedor character varying(30),
cbancproveedor character varying(20),
comentproveedor character varying(2000),
dirproveedor character varying(50),
poblproveedor character varying(50),
cpproveedor character varying(9) NOT NULL,
telproveedor character varying(12),
faxproveedor character varying(12),
emailproveedor character varying(100),
urlproveedor character varying(100),
clavewebproveedor character varying(100),
inactivoproveedor character(1)
);
--Numero: Nmero de divisiᅵ(clau artificial).
--Descripcio: Nom o descripciᅵde la divisiᅵ
--Contactes: Nom de persona o persones de contacte.
--Comentaris
--Telf: Telᅵon.
--Fax: Fax.
--Email
CREATE TABLE division (
iddivision serial PRIMARY KEY,
descdivision character varying(1000),
contactosdivisioon character varying(500),
comentdivision character varying(2000),
teldivision character varying(20),
faxdivision character varying(20),
maildivision character varying(100),
idproveedor integer NOT NULL REFERENCES proveedor(idproveedor),
inactivodivision character(1)
);
-- El cliente siempre tiene la razᅵ, bueno, o por lo menos eso cree.
--Codi: Clau artificial.
--Nom: Nom comercial o fiscal.
--Nom_alternatiu: Nom comercial o fiscal.
--CIF: Codi d'IdentificaciᅵFiscal.
--C_Banc: Compte Bancari.
--Adr: Adreᅵ.
--Pobl: Poblaciᅵ
--CProv: Codi de provincia (dos primers dᅵits del codi postal).
--sCP: Tres darrers dᅵits del codi postal.
--Telf: Telᅵon.
--Fax: Fax.
--Email: eMail.
--Url: Url.
--Data_alta
--Data_Baixa
---Comentaris
CREATE TABLE cliente (
idcliente serial PRIMARY KEY,
nomcliente character varying(100),
nomaltcliente character varying(300),
cifcliente character varying(200) UNIQUE,
bancocliente character varying(35),
dircliente character varying(100),
poblcliente character varying(40),
cpcliente character varying(10),
telcliente character varying(20),
faxcliente character varying(20),
mailcliente character varying(100),
urlcliente character varying(150),
faltacliente date DEFAULT NOW(),
fbajacliente date,
comentcliente character varying(2000),
inactivocliente character(1),
provcliente character varying
);
CREATE TABLE cobro (
idcobro serial PRIMARY KEY,
idcliente integer NOT NULL REFERENCES cliente(idcliente),
fechacobro date DEFAULT NOW(),
cantcobro numeric(12,2) DEFAULT 0,
refcobro character varying(12) NOT NULL,
previsioncobro boolean DEFAULT FALSE,
comentcobro character varying(500),
idtrabajador integer REFERENCES trabajador(idtrabajador)
);
CREATE TABLE pago (
idpago serial PRIMARY KEY,
idproveedor integer NOT NULL REFERENCES proveedor(idproveedor),
fechapago date DEFAULT NOW(),
cantpago numeric(12,2) DEFAULT 0,
refpago character varying(12) NOT NULL,
previsionpago boolean DEFAULT FALSE,
comentpago character varying(500),
idtrabajador integer REFERENCES trabajador(idtrabajador)
);
-- Any: Any en que s'efectua la comanda.
-- Numero: Nmero de comanda (comenᅵnt de 1 cada any).
-- Descripcio: Breu descripciᅵo comentari opcional.
-- Data: Data d'emisiᅵde la comanda.
CREATE TABLE pedido (
idpedido serial PRIMARY KEY,
numpedido character varying(60),
fechapedido date,
descpedido character varying(500),
iddivision integer NOT NULL REFERENCES division(iddivision),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
idtrabajador integer REFERENCES trabajador(idtrabajador)
);
CREATE TABLE usuario (
loginusuario character varying(15) PRIMARY KEY,
nombreusuario character varying(35),
apellido1usuario character varying(35),
apellido2usuario character varying(35),
claveusuario character varying(35),
permisosusuario text
);
-- Any: Any de facturaciᅵ
-- Numero: Nmero de factura.
-- Data Comentaris
-- Factura de proveᅵor.
CREATE TABLE fra_pro (
idfra_pro serial PRIMARY KEY,
numfra_pro character varying(60),
fcrefra_pro date,
comentfra_pro character varying(2000)
);
-- Albaran de proveedor
-- Any: Any en que s'efectua la comanda.
-- NumCompra: Numero de Compra (Clau artificial per poder registrar recepcions que ens arribin sense l'albarᅵpostposant la cumplimentaciᅵdel nmero d'albarᅵ.
-- NumAlbara: Nmero d'albarᅵ
-- Data: Data de l'albarᅵ-- Recepcio: Data de recepciᅵ
-- Comentaris
CREATE TABLE alb_pro (
idalb_pro serial PRIMARY KEY,
ncompraalb_pro integer,
nalbalb_pro character varying(60),
fcrealb_pro date,
frecepalb_pro date,
comentalb_pro character varying(2000),
idfra_pro integer REFERENCES fra_pro(idfra_pro),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen)
);
-- Linea de pedido
-- Numero: Nmero de lᅵia.
-- Descripcio: Descripcio de l'article.
-- Quantitat
-- PVD
-- Previsiᅵ Data prevista de recepciᅵ
CREATE TABLE lpedido (
numlpedido serial PRIMARY KEY,
desclpedido character varying(150),
cantlpedido numeric(12,2),
pvdlpedido numeric(12,2),
prevlpedido date,
ivalpedido numeric(12,2),
descuentolpedido numeric(12,2),
idpedido integer NOT NULL REFERENCES pedido(idpedido),
idalb_pro integer REFERENCES alb_pro(idalb_pro),
idarticulo integer REFERENCES articulo(idarticulo)
-- PRIMARY KEY(idpedido, numlpedido)
);
-- Entendemos que un presupuesto es una relaciᅵ de materiales y trabajos cuantificada que
-- hacemos a peticiᅵ de un cliente determinado
-- Numero
-- Data: Data d'emisiᅵdel presupost.
-- PersContacte: Nom de persona de contacte (si cal).
-- TelfContacte: Telᅵon.
-- Venciment: Data mᅵima de validesa del presupost.
-- Comentaris
-- Pressupost a clients.
CREATE TABLE presupuesto (
idpresupuesto serial PRIMARY KEY,
numpresupuesto integer NOT NULL UNIQUE,
refpresupuesto character varying(12) NOT NULL,
fpresupuesto date,
descpresupuesto character varying(150),
contactpresupuesto character varying(90),
telpresupuesto character varying(20),
vencpresupuesto date,
comentpresupuesto character varying(3000),
idusuari integer,
procesadopresupuesto boolean DEFAULT FALSE,
idcliente integer REFERENCES cliente(idcliente),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
idforma_pago integer NOT NULL REFERENCES forma_pago(idforma_pago),
idtrabajador integer REFERENCES trabajador(idtrabajador),
UNIQUE (idalmacen, numpresupuesto)
);
CREATE FUNCTION restriccionespresupuesto () RETURNS "trigger"
AS '
DECLARE
asd RECORD;
BEGIN
IF NEW.numpresupuesto IS NULL THEN
SELECT INTO asd max(numpresupuesto) AS m FROM presupuesto;
IF asd.m IS NOT NULL THEN
NEW.numpresupuesto := asd.m + 1;
ELSE
NEW.numpresupuesto := 1;
END IF;
END IF;
IF NEW.refpresupuesto IS NULL OR NEW.refpresupuesto = '''' THEN
SELECT INTO asd crearef() AS m;
IF FOUND THEN
NEW.refpresupuesto := asd.m;
END IF;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER restriccionespresupuestotrigger
BEFORE INSERT OR UPDATE ON presupuesto
FOR EACH ROW
EXECUTE PROCEDURE restriccionespresupuesto();
-- Descuento de presupuesto.
-- Numero
--Concepte: Descripciᅵdel motiu de descompte.
--Proporcio: Percentatge a descomptar.
-- Descompte de pressupost a clients.
CREATE TABLE dpresupuesto (
iddpresupuesto serial PRIMARY KEY,
conceptdpresupuesto character varying(2000),
proporciondpresupuesto numeric(12,2),
idpresupuesto integer REFERENCES presupuesto(idpresupuesto)
-- Falta poner el lugar donde se aplica el descuento, antes de la factura o despuᅵ de ᅵta.
);
-- Linea de presupuesto
-- Numero
-- Descripcio: Descripciᅵde l'article en el moment de ser presupostat.
-- Quantitat
-- PVP: Preu de l'article en el moment de ser pressupostat
-- Descompte: Percentatge de descompte en lᅵia.
-- Linia de pressupost a clients.
CREATE TABLE lpresupuesto (
idlpresupuesto serial PRIMARY KEY,
desclpresupuesto character varying(150),
cantlpresupuesto numeric(12,2),
pvplpresupuesto numeric(12,2),
ivalpresupuesto numeric(12,2),
descuentolpresupuesto numeric(12,2),
idpresupuesto integer NOT NULL REFERENCES presupuesto(idpresupuesto),
idarticulo integer REFERENCES articulo(idarticulo)
);
-- Falta poner por defecto el pvp y el iva
-- Any: Any en que s'efectua la comanda.
-- Numero: Nmero de comanda (comenᅵnt de 1 cada any).
-- Descripcio: Breu descripciᅵo comentari opcional.
-- Data: Data d'emisiᅵde la comanda.
CREATE TABLE pedidocliente (
idpedidocliente serial PRIMARY KEY,
numpedidocliente integer UNIQUE NOT NULL,
fechapedidocliente date,
refpedidocliente character varying(12) NOT NULL,
descpedidocliente character varying(500),
comentpedidocliente character varying(3000),
contactpedidocliente character varying(90),
telpedidocliente character varying(20),
idusuari integer,
idpresupuesto integer REFERENCES presupuesto(idpresupuesto),
procesadopedidocliente boolean DEFAULT FALSE,
idcliente integer NOT NULL REFERENCES cliente(idcliente),
idforma_pago integer REFERENCES forma_pago(idforma_pago),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
idtrabajador integer REFERENCES trabajador(idtrabajador)
);
CREATE FUNCTION restriccionespedidocliente () RETURNS "trigger"
AS '
DECLARE
asd RECORD;
BEGIN
IF NEW.numpedidocliente IS NULL THEN
SELECT INTO asd max(numpedidocliente) AS m FROM pedidocliente;
IF asd.m IS NOT NULL THEN
NEW.numpedidocliente := asd.m + 1;
ELSE
NEW.numpedidocliente := 1;
END IF;
END IF;
IF NEW.refpedidocliente IS NULL OR NEW.refpedidocliente = '''' THEN
SELECT INTO asd crearef() AS m;
IF FOUND THEN
NEW.refpedidocliente := asd.m;
END IF;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER restriccionespedidoclientetrigger
BEFORE INSERT OR UPDATE ON pedidocliente
FOR EACH ROW
EXECUTE PROCEDURE restriccionespedidocliente();
-- Descuento de pedidocliente.
-- Numero
--Concepte: Descripciᅵdel motiu de descompte.
--Proporcio: Percentatge a descomptar.
-- Descompte de pressupost a clients.
CREATE TABLE dpedidocliente (
iddpedidocliente serial PRIMARY KEY,
conceptdpedidocliente character varying(2000),
proporciondpedidocliente numeric(12,2),
idpedidocliente integer NOT NULL REFERENCES pedidocliente(idpedidocliente)
-- Falta poner el lugar donde se aplica el descuento, antes de la factura o despuᅵ de ᅵta.
);
-- Linea de pedido
-- Numero: Nmero de lᅵia.
-- Descripcio: Descripcio de l'article.
-- Quantitat
-- PVD
-- Previsiᅵ Data prevista de recepciᅵ
CREATE TABLE lpedidocliente (
numlpedidocliente serial PRIMARY KEY,
desclpedidocliente character varying(150),
cantlpedidocliente numeric(12,2),
pvplpedidocliente numeric(12,2),
prevlpedidocliente date,
ivalpedidocliente numeric(12,2),
descuentolpedidocliente numeric(12,2),
idpedidocliente integer NOT NULL REFERENCES pedidocliente(idpedidocliente),
puntlpedidocliente boolean DEFAULT FALSE,
idarticulo integer REFERENCES articulo(idarticulo)
);
-- FACTURACIO>Albarans:
-- Albarans pendents: S'entendran com albarans pendents tots aquells dels quals no existeixi ticket, factura ni nofactura.
-- Numero
-- Data
-- Factura a clients.
CREATE TABLE factura (
idfactura serial PRIMARY KEY,
codigoserie_factura character varying (6) NOT NULL REFERENCES serie_factura(codigoserie_factura),
numfactura integer NOT NULL,
reffactura character varying(15) NOT NULL,
ffactura date,
descfactura character varying(500),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
contactfactura character varying(90),
telfactura character varying(20),
comentfactura character varying(3000),
procesadafactura boolean DEFAULT FALSE,
idusuari integer,
idcliente integer REFERENCES cliente(idcliente),
idforma_pago integer REFERENCES forma_pago(idforma_pago),
UNIQUE (idalmacen, codigoserie_factura, numfactura),
idtrabajador integer REFERENCES trabajador(idtrabajador)
);
CREATE FUNCTION restriccionesfactura () RETURNS "trigger"
AS '
DECLARE
asd RECORD;
BEGIN
IF NEW.numfactura IS NULL THEN
SELECT INTO asd max(numfactura) AS m FROM factura WHERE idserie_factura=NEW.idserie_factura AND idalmacen = NEW.idalmacen;
IF asd.m IS NOT NULL THEN
NEW.numfactura := asd.m + 1;
ELSE
NEW.numfactura := 1;
END IF;
END IF;
IF NEW.reffactura IS NULL OR NEW.reffactura = '''' THEN
SELECT INTO asd crearef() AS m;
IF FOUND THEN
NEW.reffactura := asd.m;
END IF;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER restriccionesfacturatrigger
BEFORE INSERT OR UPDATE ON factura
FOR EACH ROW
EXECUTE PROCEDURE restriccionesfactura();
-- Descuento de pedidocliente.
-- Numero
--Concepte: Descripciᅵdel motiu de descompte.
--Proporcio: Percentatge a descomptar.
-- Descompte de pressupost a clients.
CREATE TABLE dfactura (
iddfactura serial PRIMARY KEY,
conceptdfactura character varying(2000),
proporciondfactura numeric(12,2),
idfactura integer NOT NULL REFERENCES factura(idfactura)
-- Falta poner el lugar donde se aplica el descuento, antes de la factura o despuᅵ de ᅵta.
);
-- Linea de presupuesto
-- Numero
-- Descripcio: Descripciᅵde l'article en el moment de ser presupostat.
-- Quantitat
-- PVP: Preu de l'article en el moment de ser pressupostat
-- Descompte: Percentatge de descompte en lᅵia.
-- Linia de pressupost a clients.
CREATE TABLE lfactura (
idlfactura serial PRIMARY KEY,
desclfactura character varying(150),
cantlfactura numeric(12,2),
pvplfactura numeric(12,2),
ivalfactura numeric(12,2),
descuentolfactura numeric(12,2),
idfactura integer NOT NULL REFERENCES factura(idfactura),
idarticulo integer REFERENCES articulo(idarticulo)
);
-- -------------------------------------------------------------------------------------------
-- FACTURACIO>Albarans:
-- Albarans pendents: S'entendran com albarans pendents tots aquells dels quals no existeixi ticket, factura ni nofactura.
-- Numero
-- Data
-- Factura a clients.
CREATE TABLE facturap (
idfacturap serial PRIMARY KEY,
numfacturap character varying (20) NOT NULL UNIQUE,
reffacturap character varying(15) NOT NULL,
ffacturap date,
descfacturap character varying(500),
contactfacturap character varying(90),
telfacturap character varying(20),
comentfacturap character varying(3000),
procesadafacturap boolean DEFAULT FALSE,
idusuari integer,
idproveedor integer REFERENCES proveedor(idproveedor),
idforma_pago integer REFERENCES forma_pago(idforma_pago),
idtrabajador integer REFERENCES trabajador(idtrabajador)
);
CREATE FUNCTION restriccionesfacturap () RETURNS "trigger"
AS '
DECLARE
asd RECORD;
BEGIN
IF NEW.reffacturap IS NULL OR NEW.reffacturap = '''' THEN
SELECT INTO asd crearef() AS m;
IF FOUND THEN
NEW.reffacturap := asd.m;
END IF;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER restriccionesfacturaptrigger
BEFORE INSERT OR UPDATE ON facturap
FOR EACH ROW
EXECUTE PROCEDURE restriccionesfacturap();
-- Linea de presupuesto
-- Numero
-- Descripcio: Descripciᅵde l'article en el moment de ser presupostat.
-- Quantitat
-- PVP: Preu de l'article en el moment de ser pressupostat
-- Descompte: Percentatge de descompte en lᅵia.
-- Linia de pressupost a clients.
CREATE TABLE lfacturap (
idlfacturap serial PRIMARY KEY,
desclfacturap character varying(150),
cantlfacturap numeric(12,2),
pvplfacturap numeric(12,2),
ivalfacturap numeric(12,2),
descuentolfacturap numeric(12,2),
idfacturap integer NOT NULL REFERENCES facturap(idfacturap),
idarticulo integer REFERENCES articulo(idarticulo)
);
-- Descuento de factura proveedor
-- Numero
--Concepte: Descripciᅵdel motiu de descompte.
--Proporcio: Percentatge a descomptar.
-- Descompte de pressupost a clients.
CREATE TABLE dfacturap (
iddfacturap serial PRIMARY KEY,
conceptdfacturap character varying(2000),
proporciondfacturap numeric(12,2),
idfacturap integer NOT NULL REFERENCES factura(idfactura)
-- Falta poner el lugar donde se aplica el descuento, antes de la factura o despuᅵ de ᅵta.
);
-- -------------------------------------------------------------------------------------------
-- FACTURACIO>Albarans:
-- Albarans pendents: S'entendran com albarans pendents tots aquells dels quals no existeixi ticket, factura ni nofactura.
-- Numero: Numero de nofactura
-- Data
-- Concepte: Descripcio del concepte pel qual no es poden facturar els albarans aqui agrupats (garantia, -- contracte de manteniment, regals a clients, etc...).
-- Observacions
-- Agrupacio d'albarans no facturables en funciᅵd'un determinat concepte.
CREATE TABLE nofactura (
idnofactura serial PRIMARY KEY,
numnofactura integer NOT NULL,
fechanofactura date,
conceptnofactura character varying(150),
observnofactura character varying(150),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
UNIQUE (idalmacen, numnofactura)
);
-- COMPROVACIONS D'INTEGRITAT>Genᅵiques:
-- Tots els albarans d'una factura corresponen al mateix client.
-- FACTURACIO>Albarans:
-- Albarans pendents: S'entendran com albarans pendents tots aquells dels quals no existeixi ticket, factura ni nofactura.
-- Numero
-- Data
-- Albarᅵa clients.
CREATE TABLE albaranp (
idalbaranp serial PRIMARY KEY,
numalbaranp integer NOT NULL UNIQUE,
descalbaranp character varying(150),
refalbaranp character varying(12) NOT NULL,
fechaalbaranp date DEFAULT now(),
-- loginusuario character varying(15) REFERENCES usuario(loginusuario),
comentalbaranp character varying(3000),
procesadoalbaranp boolean DEFAULT FALSE,
idproveedor integer REFERENCES proveedor(idproveedor),
idforma_pago integer REFERENCES forma_pago(idforma_pago),
idusuari integer,
-- idfactura integer REFERENCES factura(idfactura),
-- idnofactura integer REFERENCES nofactura(idnofactura),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
idtrabajador integer REFERENCES trabajador(idtrabajador),
UNIQUE (idalmacen, numalbaranp)
);
CREATE FUNCTION restriccionesalbaranp () RETURNS "trigger"
AS '
DECLARE
asd RECORD;
BEGIN
IF NEW.numalbaranp IS NULL THEN
SELECT INTO asd max(numalbaranp) AS m FROM albaranp;
IF asd.m IS NOT NULL THEN
NEW.numalbaranp := asd.m + 1;
ELSE
NEW.numalbaranp := 1;
END IF;
END IF;
IF NEW.refalbaranp IS NULL OR NEW.refalbaranp = '''' THEN
SELECT INTO asd crearef() AS m;
IF FOUND THEN
NEW.refalbaranp := asd.m;
END IF;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER restriccionesalbaranptrigger
BEFORE INSERT OR UPDATE ON albaranp
FOR EACH ROW
EXECUTE PROCEDURE restriccionesalbaranp();
-- Numero
-- Descripcio
-- Quantitat
-- PVP: Preu de l'article en el moment de la compra o de ser presupostat.
-- Descompte
-- Lᅵia d'albarᅵa clients.
CREATE TABLE lalbaranp (
numlalbaranp serial PRIMARY KEY,
desclalbaranp character varying(100),
cantlalbaranp numeric(12,2),
ivalalbaranp numeric(12,2),
pvplalbaranp numeric(12,2),
descontlalbaranp numeric(12,2),
idalbaranp integer NOT NULL REFERENCES albaranp(idalbaranp),
idarticulo integer NOT NULL REFERENCES articulo(idarticulo)
);
CREATE FUNCTION disminuyestockp () RETURNS "trigger"
AS '
DECLARE
BEGIN
UPDATE articulo SET stockarticulo = stockarticulo - OLD.cantlalbaranp WHERE idarticulo= OLD.idarticulo;
RETURN OLD;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER disminuyestockpt
AFTER DELETE OR UPDATE ON lalbaranp
FOR EACH ROW
EXECUTE PROCEDURE disminuyestockp();
CREATE FUNCTION aumentastockp () RETURNS "trigger"
AS '
DECLARE
BEGIN
UPDATE articulo SET stockarticulo = stockarticulo + NEW.cantlalbaranp WHERE idarticulo = NEW.idarticulo;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER aumentastockpt
AFTER INSERT OR UPDATE ON lalbaranp
FOR EACH ROW
EXECUTE PROCEDURE aumentastockp();
-- Descuento albaran proveedor
-- Numero
-- Concepte: Descripciᅵdel motiu de descompte.
-- Proporcio: Percentatge a descomptar.
-- Descompte d'albarᅵa clients.
CREATE TABLE dalbaranp (
iddalbaranp serial PRIMARY KEY,
conceptdalbaranp character varying(500),
proporciondalbaranp numeric(12,2),
idalbaranp integer NOT NULL REFERENCES albaranp(idalbaranp)
);
-- COMPROVACIONS D'INTEGRITAT>Genᅵiques:
-- Tots els albarans d'una factura corresponen al mateix client.
-- FACTURACIO>Albarans:
-- Albarans pendents: S'entendran com albarans pendents tots aquells dels quals no existeixi ticket, factura ni nofactura.
-- Numero
-- Data
-- Albarᅵa clients.
CREATE TABLE albaran (
idalbaran serial PRIMARY KEY,
numalbaran integer NOT NULL UNIQUE,
descalbaran character varying(150),
refalbaran character varying(12) NOT NULL,
fechaalbaran date,
-- loginusuario character varying(15) REFERENCES usuario(loginusuario),
comentalbaran character varying(3000),
comentprivalbaran character varying(3000),
procesadoalbaran boolean DEFAULT FALSE,
contactalbaran character varying,
telalbaran character varying,
idusuari integer,
idcliente integer REFERENCES cliente(idcliente),
idforma_pago integer REFERENCES forma_pago(idforma_pago),
idfactura integer REFERENCES factura(idfactura),
idnofactura integer REFERENCES nofactura(idnofactura),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
idtrabajador integer REFERENCES trabajador(idtrabajador),
UNIQUE (idalmacen, numalbaran)
);
-- **********************************************************************
-- APARTADO DE COMPROBACIONES DE INTEGRIDAD EXTRA Y DETECCIᅵ DE ERRORES.
-- **********************************************************************
-- **********************************************************************
CREATE FUNCTION restriccionesalbaran () RETURNS "trigger"
AS '
DECLARE
asd RECORD;
BEGIN
IF NEW.numalbaran IS NULL THEN
SELECT INTO asd max(numalbaran) AS m FROM albaran;
IF asd.m IS NOT NULL THEN
NEW.numalbaran := asd.m + 1;
ELSE
NEW.numalbaran := 1;
END IF;
END IF;
IF NEW.refalbaran IS NULL OR NEW.refalbaran = '''' THEN
SELECT INTO asd crearef() AS m;
IF FOUND THEN
NEW.refalbaran := asd.m;
END IF;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER restriccionesalbarantrigger
BEFORE INSERT OR UPDATE ON albaran
FOR EACH ROW
EXECUTE PROCEDURE restriccionesalbaran();
CREATE FUNCTION random_string(int4) RETURNS "varchar" AS '
DECLARE
iLoop int4;
result varchar;
BEGIN
result = '''';
IF ($1>0) AND ($1 < 255) THEN
FOR iLoop in 1 .. $1 LOOP
result = result || chr(int4(random()*26)+65);
END LOOP;
RETURN result;
ELSE
RETURN ''f'';
END IF;
END;
' LANGUAGE 'plpgsql';
CREATE FUNCTION crearef () RETURNS character varying (15)
AS '
DECLARE
asd RECORD;
result character varying(15);
efound boolean;
BEGIN
efound := FALSE;
WHILE efound = FALSE LOOP
result := random_string(6);
efound := TRUE;
SELECT INTO asd idpresupuesto FROM presupuesto WHERE refpresupuesto=result;
IF FOUND THEN
efound := FALSE;
END IF;
SELECT INTO asd idpedidocliente FROM pedidocliente WHERE refpedidocliente=result;
IF FOUND THEN
efound := FALSE;
END IF;
SELECT INTO asd idalbaran FROM albaran WHERE refalbaran=result;
IF FOUND THEN
efound := FALSE;
END IF;
SELECT INTO asd idfactura FROM factura WHERE reffactura=result;
IF FOUND THEN
efound := FALSE;
END IF;
END LOOP;
RETURN result;
END;
' LANGUAGE plpgsql;
-- Descuento albaran
-- Numero
-- Concepte: Descripciᅵdel motiu de descompte.
-- Proporcio: Percentatge a descomptar.
-- Descompte d'albarᅵa clients.
CREATE TABLE dalbaran (
iddalbaran serial PRIMARY KEY,
conceptdalbaran character varying(500),
proporciondalbaran numeric(12,2),
idalbaran integer NOT NULL REFERENCES albaran(idalbaran)
);
-- Numero
-- Descripcio
-- Quantitat
-- PVP: Preu de l'article en el moment de la compra o de ser presupostat.
-- Descompte
-- Lᅵia d'albarᅵa clients.
CREATE TABLE lalbaran (
numlalbaran serial PRIMARY KEY,
desclalbaran character varying(100),
cantlalbaran numeric(12,2),
pvplalbaran numeric(12,2),
descontlalbaran numeric(12,2),
ivalalbaran numeric(12,2),
idalbaran integer NOT NULL REFERENCES albaran(idalbaran),
idarticulo integer NOT NULL REFERENCES articulo(idarticulo)
);
CREATE FUNCTION disminuyestock () RETURNS "trigger"
AS '
DECLARE
BEGIN
UPDATE articulo SET stockarticulo = stockarticulo + OLD.cantlalbaran WHERE idarticulo= OLD.idarticulo;
RETURN OLD;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER disminuyestockt
AFTER DELETE OR UPDATE ON lalbaran
FOR EACH ROW
EXECUTE PROCEDURE disminuyestock();
CREATE FUNCTION aumentastock () RETURNS "trigger"
AS '
DECLARE
BEGIN
UPDATE articulo SET stockarticulo = stockarticulo - NEW.cantlalbaran WHERE idarticulo = NEW.idarticulo;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER aumentastockt
AFTER INSERT OR UPDATE ON lalbaran
FOR EACH ROW
EXECUTE PROCEDURE aumentastock();
-- FACTURACIO>Albarans:
-- Albarans pendents: S'entendran com albarans pendents tots aquells dels quals no existeixi ticket, factura ni nofactura.
CREATE TABLE ticket (
numticket integer PRIMARY KEY,
fechaticket date
);
-- COMPROVACIONS D'INTEGRITAT>Genᅵiques:
-- 1 Article tᅵ1 sol proveᅵor principal.
-- 1 Article tᅵ1 sol proveᅵor referent.
CREATE TABLE suministra (
idsuministra serial PRIMARY KEY,
refpro character varying(100),
principalsuministra numeric(12,2),
idproveedor integer REFERENCES proveedor(idproveedor),
idarticulo integer REFERENCES articulo(idarticulo)
);
-- Los tipos de tarifa permiten tener diferentes precios para un mismo artᅵulo en funciᅵ de alguna variable que queramos definir (para un cliente, para una zona, si es para un minorista, tienda propia franquiciada ...
-- codigo: es un identificador pnemotᅵnico de la tarifa.
-- desc: es un texto descriptivo del tipo de tarifa.
CREATE TABLE tipo_tarifa (
idtipo_tarifa serial PRIMARY KEY,
codtipo_tarifa character varying(10) NOT NULL UNIQUE,
desctipo_tarifa character varying(50)
);
-- La tabla tarifa contiene los precios de venta y oferta incluidas las ofertas MxN.
-- idalmacen: Almacᅵ o tienda a la que corresponden los precios.
-- idarticulo: idetificador del articulo al que corresponde el precio.
-- finicio: fecha de inicio vigencia del precio
-- ffin: fecha de finalizaciᅵ de vigencia del precio.
-- esoferta: indica si el precio es de oferta.
-- esmxn: indica si la oferta es mxn (p.e. 3x2).
-- cantidadm: cantidad de unidades para primer valor en oferta mxn (valor de unidades llevadas).
-- cantidadn: cantidad de unidades para segundo valor en oferta mxn (valor de unidades pagadas).
-- FALTA DEFINIR LAS REGLAS PARA EVITAR SOLAPAMIENTOS ENTRE OFERTAS.
CREATE TABLE tarifa (
idtarifa serial PRIMARY KEY,
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
idarticulo integer NOT NULL REFERENCES articulo(idarticulo),
idtipo_tarifa integer NOT NULL REFERENCES tipo_tarifa(idtipo_tarifa),
finiciotarifa date,
ffintarifa date,
preciotarifa numeric(13, 4),
esofertatarifa character(1) NOT NULL CHECK(esofertatarifa='S' OR esofertatarifa='N'),
esmxntarifa character(1),
cantidadmtarifa numeric(5, 0),
cantidadntarifa numeric(5, 0)
);
-- Restricciones para la tabla tarifa:
-- Para un mismo almacᅵ, artᅵulo y tarifa, no puede haber mᅵ de un precio a una misma fecha
-- Para un mismo almacᅵ, artᅵulo y tarifa, no puede haber mᅵ de una oferta precio a una misma fecha
-- Sᅵque se permite que haya solapamiento entre la tarifa normal y una oferta. Prevalece siempre la oferta.
--DROP TRIGGER restriccionestarifatrigger ON cuenta CASCADE;
--DROP FUNCTION restriccionestarifa();
CREATE FUNCTION restriccionestarifa () RETURNS "trigger"
AS '
DECLARE
cont INTEGER;
BEGIN
RAISE NOTICE '' IDTARIFA = % '',NEW.idtarifa;
SELECT count(*) INTO cont FROM tarifa
WHERE tarifa.idtipo_tarifa = NEW.idtipo_tarifa AND tarifa.idarticulo = NEW.idarticulo AND tarifa.idalmacen = NEW.idalmacen AND tarifa.esofertatarifa = NEW.esofertatarifa AND
tarifa.idtarifa != NEW.idtarifa AND tarifa.finiciotarifa <= NEW.finiciotarifa AND tarifa.ffintarifa >= NEW.finiciotarifa;
if (NOT(cont ISNULL) AND cont > 0) THEN
RAISE EXCEPTION '' Solapamiento de fechas en fecha inicio '';
END IF;
RAISE NOTICE '' CONTADOR = % '', cont;
SELECT count(*) INTO cont FROM tarifa
WHERE tarifa.idtipo_tarifa = NEW.idtipo_tarifa AND tarifa.idarticulo = NEW.idarticulo AND tarifa.idalmacen = NEW.idalmacen AND tarifa.esofertatarifa = NEW.esofertatarifa AND
tarifa.idtarifa != NEW.idtarifa AND tarifa.finiciotarifa <= NEW.ffintarifa AND tarifa.ffintarifa >= NEW.ffintarifa;
if (NOT(cont ISNULL) AND cont > 0) THEN
RAISE EXCEPTION '' Solapamiento de fechas en fecha fin '';
END IF;
RAISE NOTICE '' CONTADOR = % '', cont;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER restriccionestarifatrigger
BEFORE INSERT OR UPDATE ON tarifa
FOR EACH ROW
EXECUTE PROCEDURE restriccionestarifa();
CREATE TABLE precio_compra (
idprecio_compra serial PRIMARY KEY,
idarticulo integer NOT NULL REFERENCES articulo(idarticulo),
iddivision integer REFERENCES division(iddivision),
idalmacen integer REFERENCES almacen(idalmacen),
fechapreciocompra date,
valorpreciocompra numeric(13, 4) NOT NULL
);
CREATE TABLE codigobarras (
idcodigobarras serial PRIMARY KEY,
idarticulo integer NOT NULL REFERENCES articulo(idarticulo),
ean14codigobarras numeric(14, 0) NOT NULL UNIQUE,
unixcajacodigobarras numeric(4, 0),
cajxpaletcodigobarras numeric(4, 0),
unidadcodigobarras character(1)
);
-- FUNCIONES VARIAS DE SOPORTE.
CREATE OR REPLACE FUNCTION ivaarticulo(integer) RETURNS numeric(12,2)
AS'
DECLARE
idarticulo ALIAS FOR $1;
as RECORD;
BEGIN
SELECT INTO AS * FROM tipo_iva, tasa_iva, articulo WHERE tasa_iva.idtipo_iva = tipo_iva.idtipo_iva AND tipo_iva.idtipo_iva = articulo.idtipo_iva AND articulo.idarticulo = idarticulo ORDER BY fechatasa_iva;
IF FOUND THEN
RETURN as.porcentasa_iva;
END IF;
RETURN 0.0;
END;
' LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION pvparticulo(integer) RETURNS numeric(12,2)
AS'
DECLARE
idarticulo ALIAS FOR $1;
as RECORD;
BEGIN
SELECT INTO AS pvparticulo FROM articulo WHERE articulo.idarticulo = idarticulo;
IF FOUND THEN
RETURN as.pvparticulo;
END IF;
RETURN 0.0;
END;
' LANGUAGE plpgsql;
-- Any: Any en que s'efectua la comanda.
-- Numero: Nmero de comanda (comenᅵnt de 1 cada any).
-- Descripcio: Breu descripciᅵo comentari opcional.
-- Data: Data d'emisiᅵde la comanda.
CREATE TABLE pedidoproveedor (
idpedidoproveedor serial PRIMARY KEY,
numpedidoproveedor integer UNIQUE NOT NULL,
fechapedidoproveedor date,
refpedidoproveedor character varying(12) NOT NULL,
descpedidoproveedor character varying(500),
comentpedidoproveedor character varying(3000),
contactpedidoproveedor character varying(90),
telpedidoproveedor character varying(20),
procesadopedidoproveedor boolean DEFAULT FALSE,
idproveedor integer NOT NULL REFERENCES proveedor(idproveedor),
idforma_pago integer REFERENCES forma_pago(idforma_pago),
idalmacen integer NOT NULL REFERENCES almacen(idalmacen),
idtrabajador integer REFERENCES trabajador(idtrabajador)
);
CREATE FUNCTION restriccionespedidoproveedor () RETURNS "trigger"
AS '
DECLARE
asd RECORD;
BEGIN
IF NEW.numpedidoproveedor IS NULL THEN
SELECT INTO asd max(numpedidoproveedor) AS m FROM pedidoproveedor;
IF asd.m IS NOT NULL THEN
NEW.numpedidoproveedor := asd.m + 1;
ELSE
NEW.numpedidoproveedor := 1;
END IF;
END IF;
IF NEW.refpedidoproveedor IS NULL OR NEW.refpedidoproveedor = '''' THEN
SELECT INTO asd crearef() AS m;
IF FOUND THEN
NEW.refpedidoproveedor := asd.m;
END IF;
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER restriccionespedidoproveedortrigger
BEFORE INSERT OR UPDATE ON pedidoproveedor
FOR EACH ROW
EXECUTE PROCEDURE restriccionespedidoproveedor();
-- Descuento de pedidocliente.
-- Numero
--Concepte: Descripciᅵdel motiu de descompte.
--Proporcio: Percentatge a descomptar.
-- Descompte de pressupost a clients.
CREATE TABLE dpedidoproveedor (
iddpedidoproveedor serial PRIMARY KEY,
conceptdpedidoproveedor character varying(2000),
proporciondpedidoproveedor numeric(12,2),
idpedidoproveedor integer NOT NULL REFERENCES pedidoproveedor(idpedidoproveedor)
-- Falta poner el lugar donde se aplica el descuento, antes de la factura o despuᅵ de ᅵta.
);
-- Linea de pedido
-- Numero: Nmero de lᅵia.
-- Descripcio: Descripcio de l'article.
-- Quantitat
-- PVD
-- Previsiᅵ Data prevista de recepciᅵ
CREATE TABLE lpedidoproveedor (
numlpedidoproveedor serial PRIMARY KEY,
desclpedidoproveedor character varying(150),
cantlpedidoproveedor numeric(12,2),
pvplpedidoproveedor numeric(12,2),
prevlpedidoproveedor date,
ivalpedidoproveedor numeric(12,2),
descuentolpedidoproveedor numeric(12,2),
idpedidoproveedor integer NOT NULL REFERENCES pedidoproveedor(idpedidoproveedor),
puntlpedidoproveedor boolean DEFAULT FALSE,
idarticulo integer REFERENCES articulo(idarticulo)
);
-- Calculo de totales para presupuestos.
CREATE OR REPLACE FUNCTION calctotalpres(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpresupuesto * pvplpresupuesto * (1 - descuentolpresupuesto/100) *(1+ ivalpresupuesto/100) AS subtotal1 FROM lpresupuesto WHERE idpresupuesto = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpresupuesto FROM dpresupuesto WHERE idpresupuesto = idp LOOP
total := total * (1 - res.proporciondpresupuesto/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Calculo de totales para presupuestos.
CREATE OR REPLACE FUNCTION calcbimppres(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpresupuesto * pvplpresupuesto * (1 - descuentolpresupuesto/100) AS subtotal1 FROM lpresupuesto WHERE idpresupuesto = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpresupuesto FROM dpresupuesto WHERE idpresupuesto = idp LOOP
total := total * (1 - res.proporciondpresupuesto/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Calculo de totales para presupuestos.
CREATE OR REPLACE FUNCTION calcimpuestospres(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpresupuesto * pvplpresupuesto * (1 - descuentolpresupuesto/100) * (ivalpresupuesto/100) AS subtotal1 FROM lpresupuesto WHERE idpresupuesto = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpresupuesto FROM dpresupuesto WHERE idpresupuesto = idp LOOP
total := total * (1 - res.proporciondpresupuesto/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para pedido cliente.
CREATE OR REPLACE FUNCTION calctotalpedcli(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpedidocliente * pvplpedidocliente * (1 - descuentolpedidocliente/100) *(1+ ivalpedidocliente/100) AS subtotal1 FROM lpedidocliente WHERE idpedidocliente = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpedidocliente FROM dpedidocliente WHERE idpedidocliente = idp LOOP
total := total * (1 - res.proporciondpedidocliente/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para pedido cliente.
CREATE OR REPLACE FUNCTION calcbimppedcli(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpedidocliente * pvplpedidocliente * (1 - descuentolpedidocliente/100) AS subtotal1 FROM lpedidocliente WHERE idpedidocliente = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpedidocliente FROM dpedidocliente WHERE idpedidocliente = idp LOOP
total := total * (1 - res.proporciondpedidocliente/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para pedido cliente.
CREATE OR REPLACE FUNCTION calcimpuestospedcli(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpedidocliente * pvplpedidocliente * (1 - descuentolpedidocliente/100) *( ivalpedidocliente/100) AS subtotal1 FROM lpedidocliente WHERE idpedidocliente = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpedidocliente FROM dpedidocliente WHERE idpedidocliente = idp LOOP
total := total * (1 - res.proporciondpedidocliente/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para albaranes.
CREATE OR REPLACE FUNCTION calctotalalbaran(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlalbaran * pvplalbaran * (1 - descontlalbaran/100) *(1+ ivalalbaran/100) AS subtotal1 FROM lalbaran WHERE idalbaran = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondalbaran FROM dalbaran WHERE idalbaran = idp LOOP
total := total * (1 - res.proporciondalbaran/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para albaranes.
CREATE OR REPLACE FUNCTION calcbimpalbaran(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlalbaran * pvplalbaran * (1 - descontlalbaran/100) AS subtotal1 FROM lalbaran WHERE idalbaran = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondalbaran FROM dalbaran WHERE idalbaran = idp LOOP
total := total * (1 - res.proporciondalbaran/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para albaranes.
CREATE OR REPLACE FUNCTION calcimpuestosalbaran(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlalbaran * pvplalbaran * (1 - descontlalbaran/100)*(ivalalbaran/100) AS subtotal1 FROM lalbaran WHERE idalbaran = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondalbaran FROM dalbaran WHERE idalbaran = idp LOOP
total := total * (1 - res.proporciondalbaran/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para facturas
CREATE OR REPLACE FUNCTION calctotalfactura(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlfactura * pvplfactura * (1 - descuentolfactura/100) *(1+ ivalfactura/100) AS subtotal1 FROM lfactura WHERE idfactura = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondfactura FROM dfactura WHERE idfactura = idp LOOP
total := total * (1 - res.proporciondfactura/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para facturas
CREATE OR REPLACE FUNCTION calcbimpfactura(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlfactura * pvplfactura * (1 - descuentolfactura/100) AS subtotal1 FROM lfactura WHERE idfactura = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondfactura FROM dfactura WHERE idfactura = idp LOOP
total := total * (1 - res.proporciondfactura/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para facturas
CREATE OR REPLACE FUNCTION calcimpuestosfactura(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlfactura * pvplfactura * (1 - descuentolfactura/100) *(ivalfactura/100) AS subtotal1 FROM lfactura WHERE idfactura = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondfactura FROM dfactura WHERE idfactura = idp LOOP
total := total * (1 - res.proporciondfactura/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para pedido proveedor
CREATE OR REPLACE FUNCTION calctotalpedpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpedidoproveedor * pvplpedidoproveedor * (1 - descuentolpedidoproveedor/100) *(1+ ivalpedidoproveedor/100) AS subtotal1 FROM lpedidoproveedor WHERE idpedidoproveedor = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpedidoproveedor FROM dpedidoproveedor WHERE idpedidoproveedor = idp LOOP
total := total * (1 - res.proporciondpedidoproveedor/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
CREATE OR REPLACE FUNCTION calcbimppedpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpedidoproveedor * pvplpedidoproveedor * (1 - descuentolpedidoproveedor/100) AS subtotal1 FROM lpedidoproveedor WHERE idpedidoproveedor = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpedidoproveedor FROM dpedidoproveedor WHERE idpedidoproveedor = idp LOOP
total := total * (1 - res.proporciondpedidoproveedor/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
CREATE OR REPLACE FUNCTION calcimpuestospedpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlpedidoproveedor * pvplpedidoproveedor * (1 - descuentolpedidoproveedor/100) *( ivalpedidoproveedor/100) AS subtotal1 FROM lpedidoproveedor WHERE idpedidoproveedor = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondpedidoproveedor FROM dpedidoproveedor WHERE idpedidoproveedor = idp LOOP
total := total * (1 - res.proporciondpedidoproveedor/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para albaaran proveedor
CREATE OR REPLACE FUNCTION calctotalalbpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlalbaranp * pvplalbaranp * (1 - descontlalbaranp/100) *(1+ ivalalbaranp/100) AS subtotal1 FROM lalbaranp WHERE idalbaranp = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondalbaranp FROM dalbaranp WHERE idalbaranp = idp LOOP
total := total * (1 - res.proporciondalbaranp/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
CREATE OR REPLACE FUNCTION calcbimpalbpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlalbaranp * pvplalbaranp * (1 - descontlalbaranp/100) AS subtotal1 FROM lalbaranp WHERE idalbaranp = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondalbaranp FROM dalbaranp WHERE idalbaranp = idp LOOP
total := total * (1 - res.proporciondalbaranp/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
\echo "Cálculo de totales para albaranes de proveedor"
CREATE OR REPLACE FUNCTION calcimpuestosalbpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlalbaranp * pvplalbaranp * (1 - descontlalbaranp/100) *(ivalalbaranp/100) AS subtotal1 FROM lalbaranp WHERE idalbaranp = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondalbaranp FROM dalbaranp WHERE idalbaranp = idp LOOP
total := total * (1 - res.proporciondalbaranp/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para factura proveedor
CREATE OR REPLACE FUNCTION calctotalfacpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlfacturap * pvplfacturap * (1 - descuentolfacturap/100) *(1+ ivalfacturap/100) AS subtotal1 FROM lfacturap WHERE idfacturap = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondfacturap FROM dfacturap WHERE idfacturap = idp LOOP
total := total * (1 - res.proporciondfacturap/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para factura proveedor
CREATE OR REPLACE FUNCTION calcbimpfacpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlfacturap * pvplfacturap * (1 - descuentolfacturap/100) AS subtotal1 FROM lfacturap WHERE idfacturap = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondfacturap FROM dfacturap WHERE idfacturap = idp LOOP
total := total * (1 - res.proporciondfacturap/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- Cálculo de totales para factura proveedor
CREATE OR REPLACE FUNCTION calcimpuestosfacpro(integer) RETURNS numeric(12,2)
AS '
DECLARE
idp ALIAS FOR $1;
total numeric(12,2);
res RECORD;
BEGIN
total := 0;
FOR res IN SELECT cantlfacturap * pvplfacturap * (1 - descuentolfacturap/100) *(ivalfacturap/100) AS subtotal1 FROM lfacturap WHERE idfacturap = idp LOOP
total := total + res.subtotal1;
END LOOP;
FOR res IN SELECT proporciondfacturap FROM dfacturap WHERE idfacturap = idp LOOP
total := total * (1 - res.proporciondfacturap/100);
END LOOP;
RETURN total;
END;
' language plpgsql;
-- bulmafact_data.sql ###########################################
#SET DATESTYLE TO European;
INSERT INTO configuracion (nombre, valor) VALUES ('CodCuenta', 'xxxxyyy');
INSERT INTO configuracion (nombre, valor) VALUES ('CIF', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('TipoVia', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('NombreVia', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('NumeroVia', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('Escalera', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('Piso', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('Puerta', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('CodPostal', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('Municipio', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('Provincia', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('Pais', '--');
INSERT INTO configuracion (nombre, valor) VALUES ('Tipo', 'BulmaFact');
INSERT INTO configuracion (nombre, valor) VALUES ('ProgramaContabilidad', 'BulmaCont');
INSERT INTO configuracion (nombre, valor) VALUES ('NombreEmpresa', 'Sin Definir');
INSERT INTO configuracion (nombre, valor) VALUES ('AlmacenDefecto', '100');
INSERT INTO forma_pago (descforma_pago, dias1tforma_pago, descuentoforma_pago) VALUES ('Contado', 1, 5);
INSERT INTO forma_pago (descforma_pago, dias1tforma_pago, descuentoforma_pago) VALUES ('Pagare 30 dÃas', 30, 0);
INSERT INTO forma_pago (descforma_pago, dias1tforma_pago, descuentoforma_pago) VALUES ('Pagare 60 dÃas', 60, 0);
INSERT INTO forma_pago (descforma_pago, dias1tforma_pago, descuentoforma_pago) VALUES ('Talón 15 dias', 15, 1);
INSERT INTO tipo_tarifa (codtipo_tarifa, desctipo_tarifa) VALUES('GENERAL', 'Tarifa General');
INSERT INTO almacen (codigoalmacen, nomalmacen, diralmacen, poblalmacen, cpalmacen, telalmacen, faxalmacen) VALUES (100, 'La Botica del Abuelo', 'Camà Vell de Bunyola', 'Palma de Mallorca', '07009', '971434343', '971434344');
INSERT INTO almacen (codigoalmacen, nomalmacen, diralmacen, poblalmacen, cpalmacen, telalmacen, faxalmacen) VALUES (200, 'Colmado de Mateo', 'Joan Maura', 'Palma de Mallorca', '07005', '971467911', '971776776');
INSERT INTO almacen (codigoalmacen, nomalmacen, diralmacen, poblalmacen, cpalmacen, telalmacen, faxalmacen) VALUES (300, 'Almacén Norte', 'PolÃono homogeneo', 'Alcudia', '07012', '971167911', '971111776');
INSERT INTO proveedor (nomproveedor, cifproveedor, cpproveedor) VALUES ('Tomeu', '45678098D', '07008');
INSERT INTO proveedor (nomproveedor, cifproveedor, cpproveedor) VALUES ('Cristina', '45679998D', '07008');
INSERT INTO proveedor (nomproveedor, cifproveedor, cpproveedor) VALUES ('Marco Aurelio', '45668998D', '07008');
INSERT INTO proveedor (nomproveedor, cifproveedor, cpproveedor) VALUES ('Torrijas', '45678995D', '07008');
INSERT INTO division (idproveedor, descdivision) VALUES (1, 'division 1 de mateu');
INSERT INTO division (idproveedor, descdivision) VALUES (1, 'division 2 de mateu');
INSERT INTO division (idproveedor, descdivision) VALUES (2, 'division 1 de cristina');
INSERT INTO division (idproveedor, descdivision) VALUES (2, 'division 2 de cristina');
INSERT INTO division (idproveedor, descdivision) VALUES (2, 'division 3 de cristina');
INSERT INTO division (idproveedor, descdivision) VALUES (4, 'division 1 de Torrijas');
INSERT INTO cliente (nomcliente, cifcliente, telcliente, poblcliente) VALUES('Jorge Gutiérrez', '39881444W', '971998877', 'Palma de Mallorca');
INSERT INTO cliente (nomcliente, cifcliente, telcliente, poblcliente) VALUES('Carlos Santana', '43881555Z', '871998877', 'Campos');
INSERT INTO cliente (nomcliente, cifcliente, telcliente, poblcliente) VALUES('Dolores MartÃnez', '66881444E', '666998877', 'Muro');
INSERT INTO cliente (nomcliente, cifcliente, telcliente, poblcliente) VALUES('Pepe Infante', '55881444W', '9321998877', 'Barcelona');
INSERT INTO tipo_iva (desctipo_iva) VALUES('Exento');
INSERT INTO tipo_iva (desctipo_iva) VALUES('Normal');
INSERT INTO tasa_iva(idtipo_iva, porcentasa_iva, fechatasa_iva) VALUES(1, 0, '0001-01-01');
INSERT INTO tasa_iva(idtipo_iva, porcentasa_iva, fechatasa_iva) VALUES(2, 16.00, '0001-01-01');
INSERT INTO tasa_iva(idtipo_iva, porcentasa_iva, fechatasa_iva) VALUES(2, 17.00, '2005-01-01');
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('CO', 'Comestibles', NULL);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('DR', 'Drogueria', NULL);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('VE', 'Vehiculos', NULL);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('LA', 'Lacteos', 1);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('CA', 'Carnicos', 1);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('CH', 'Charcuteria', 1);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('PE', 'Pescaderia', 1);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('PA', 'Panaderia', 1);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('FU', 'Fruteria', 1);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('BE', 'Bebidas', 1);
INSERT INTO familia(codigofamilia, nombrefamilia, padrefamilia) VALUES ('IN', 'Infusiones', 1);
\echo "Hemos insertado las familias"
INSERT INTO articulo (codarticulo, nomarticulo, obserarticulo, idtipo_iva, idfamilia) VALUES('1', 'Leche Entera', 'Brick leche entera 1 litro', 1, 4);
INSERT INTO articulo (codarticulo, nomarticulo, obserarticulo, idtipo_iva, idfamilia) VALUES('2', 'Natillas', 'Pack dos envases de natillas de vainilla', 2, 4);
INSERT INTO articulo (codarticulo, nomarticulo, obserarticulo, idtipo_iva, idfamilia) VALUES('3', 'Iogurt', 'Pack 4 unidades yogures sabores', 1, 5);
INSERT INTO articulo (codarticulo, nomarticulo, obserarticulo, idtipo_iva, idfamilia) VALUES('4', 'Nestcafé', 'Bote 400 gramos café soluble natural', 1, 5);
\echo "Hemos insertado los articulos"
INSERT INTO pedido (numpedido, fechapedido, descpedido, iddivision, idalmacen) VALUES('P-1', '25/10/2004', 'Pedido semanal de carne y pescado', 1, 1);
INSERT INTO pedido (numpedido, fechapedido, descpedido, iddivision, idalmacen) VALUES('P-2', '30/10/2004', 'Pedido especial noche halloween', 2, 3);
INSERT INTO pedido (numpedido, fechapedido, descpedido, iddivision, idalmacen) VALUES('P-3', '02/11/2004', 'Frutos secos de temporada', 3, 2);
INSERT INTO pedido (numpedido, fechapedido, descpedido, iddivision, idalmacen) VALUES('P-4', '02/10/2004', 'Pedido semanal de lencerÃa', 6, 1);
\echo "Hemos insertado los pedidos"
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 10, 1.2, '30/10/2004', 1, 1);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 100, 2.23, '02/11/2004', 1, 2);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 15, 3.34, '30/10/2004', 1, 3);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 34, 11.20, '05/11/2004', 2, 1);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 45, 2.55, '05/11/2004', 2, 3);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 1, 4.28, '10/11/2004', 2, 4);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 34, 1.7, '31/12/2004', 3, 2);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 11, 1.3, '30/12/2004', 3, 3);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 28, 2.2, '30/12/2004', 3, 4);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 15, 3.2, '15/11/2004', 4, 1);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 10, 8.59, '10/11/2004', 4, 2);
INSERT INTO lpedido (desclpedido, cantlpedido, pvdlpedido, prevlpedido, idpedido, idarticulo) VALUES (' ', 6, 6.15, '25/11/2004', 4, 3);
INSERT INTO pedidocliente (numpedidocliente, fechapedidocliente, descpedidocliente, idcliente, idalmacen) VALUES(1, '25/10/2004', 'Pedido carne y pescado', 1, 1);
INSERT INTO pedidocliente (numpedidocliente, fechapedidocliente, descpedidocliente, idcliente, idalmacen) VALUES(2, '30/10/2004', 'Pedido especial noche ', 2, 3);
INSERT INTO pedidocliente (numpedidocliente, fechapedidocliente, descpedidocliente, idcliente, idalmacen) VALUES(3, '02/11/2004', 'Frutos de temporada', 1, 2);
INSERT INTO pedidocliente (numpedidocliente, fechapedidocliente, descpedidocliente, idcliente, idalmacen) VALUES(4, '02/10/2004', 'Pedido de lencerÃa', 2, 1);
\echo "Hemos insertado los pedidos de cliente"
INSERT INTO presupuesto (numpresupuesto, fpresupuesto, contactpresupuesto, telpresupuesto, vencpresupuesto, comentpresupuesto, idusuari, idcliente, idalmacen, idforma_pago) VALUES (1, '30/11/2004', 'Primer Contacto', '971 32 32 78', '12/12/2004', 'Sin comentarios', 1, 1, 1,1);
INSERT INTO presupuesto (numpresupuesto, fpresupuesto, contactpresupuesto, telpresupuesto, vencpresupuesto, comentpresupuesto, idusuari, idcliente, idalmacen, idforma_pago) VALUES (2, '29/11/2004', 'Segundo Contacto', '91 454 45 30', '13/12/2004', 'Sin comentarios', 1, 1, 1,1);
INSERT INTO presupuesto (numpresupuesto, fpresupuesto, contactpresupuesto, telpresupuesto, vencpresupuesto, comentpresupuesto, idusuari, idcliente, idalmacen, idforma_pago) VALUES (3, '10/11/2004', 'Tercer Contacto', '971 29 06 29', '14/12/2004', 'Sin comentarios', 1, 2, 1,1);
INSERT INTO serie_factura (codigoserie_factura, descserie_factura) VALUES ('DD', 'Serie de ejemplo');
INSERT INTO serie_factura (codigoserie_factura, descserie_factura) VALUES ('REC', 'Facturas Rectificativas');
INSERT INTO factura (codigoserie_factura, numfactura, ffactura, contactfactura, telfactura, comentfactura, idusuari, idcliente, idalmacen) VALUES ('DD', 1, '30/11/2004', 'Primer Contacto', '971 32 32 78', 'Sin comentarios', 1, 1, 1);
INSERT INTO factura (codigoserie_factura,numfactura, ffactura, contactfactura, telfactura, comentfactura, idusuari, idcliente, idalmacen) VALUES ('DD', 2, '29/11/2004', 'Segundo Contacto', '91 454 45 30', 'Sin comentarios', 1, 1, 1);
INSERT INTO factura (codigoserie_factura,numfactura, ffactura, contactfactura, telfactura, comentfactura, idusuari, idcliente, idalmacen) VALUES ('REC', 3, '10/11/2004', 'Tercer Contacto', '971 29 06 29', 'Sin comentarios', 1, 2, 1);
INSERT INTO facturap (numfacturap, ffacturap, contactfacturap, telfacturap, comentfacturap, idusuari, idproveedor) VALUES ( 1, '30/11/2004', 'Primer Contacto', '971 32 32 78', 'Sin comentarios', 1, 1);
INSERT INTO facturap (numfacturap, ffacturap, contactfacturap, telfacturap, comentfacturap, idusuari, idproveedor) VALUES ( 2, '29/11/2004', 'Segundo Contacto', '91 454 45 30', 'Sin comentarios', 1, 1);
INSERT INTO facturap (numfacturap, ffacturap, contactfacturap, telfacturap, comentfacturap, idusuari, idproveedor) VALUES ( 3, '10/11/2004', 'Tercer Contacto', '971 29 06 29', 'Sin comentarios', 1, 2);
INSERT INTO suministra (idarticulo, idproveedor) VALUES (1, 1);
INSERT INTO suministra (idarticulo, idproveedor) VALUES (2, 1);
INSERT INTO suministra (idarticulo, idproveedor) VALUES (3, 2);
INSERT INTO suministra (idarticulo, idproveedor) VALUES (4, 2);
INSERT INTO suministra (idarticulo, idproveedor) VALUES (2, 3);
INSERT INTO suministra (idarticulo, idproveedor) VALUES (3, 3);
INSERT INTO suministra (idarticulo, idproveedor) VALUES (4, 1);
INSERT INTO albaran (numalbaran, fechaalbaran, idtrabajador, idcliente, idforma_pago, idalmacen, refalbaran) VALUES (1, '01/01/2005', NULL, 1, 1, 1,'R1');
INSERT INTO albaran (numalbaran, fechaalbaran, idtrabajador, idcliente, idforma_pago, idalmacen, refalbaran) VALUES (2, '02/01/2005', NULL, 1, 2, 1,'R2');
INSERT INTO albaran (numalbaran, fechaalbaran, idtrabajador, idcliente, idforma_pago, idalmacen, refalbaran) VALUES (3, '03/01/2005', NULL, 2, 3, 1,'R3');
INSERT INTO albaran (numalbaran, fechaalbaran, idtrabajador, idcliente, idforma_pago, idalmacen, refalbaran) VALUES (4, '01/01/2005', NULL, 2, 4, 1,'R4');
INSERT INTO albaran (numalbaran, fechaalbaran, idtrabajador, idcliente, idforma_pago, idalmacen, refalbaran) VALUES (5, '02/01/2005', NULL, 3, 2, 1,'R5');
INSERT INTO albaran (numalbaran, fechaalbaran, idtrabajador, idcliente, idforma_pago, idalmacen, refalbaran) VALUES (6, '03/01/2005', NULL, 4, 1, 1,'R6');
INSERT INTO albaranp (numalbaranp, fechaalbaranp, idtrabajador, idproveedor, idforma_pago, idalmacen, refalbaranp) VALUES (1, '01/01/2005', NULL, 1, 1, 1,'RP1');
INSERT INTO albaranp (numalbaranp, fechaalbaranp, idtrabajador, idproveedor, idforma_pago, idalmacen, refalbaranp) VALUES (2, '02/01/2005', NULL, 1, 2, 1,'RP2');
INSERT INTO albaranp (numalbaranp, fechaalbaranp, idtrabajador, idproveedor, idforma_pago, idalmacen, refalbaranp) VALUES (3, '03/01/2005', NULL, 2, 3, 1,'RP3');
INSERT INTO albaranp (numalbaranp, fechaalbaranp, idtrabajador, idproveedor, idforma_pago, idalmacen, refalbaranp) VALUES (4, '01/01/2005', NULL, 2, 4, 1,'RP4');
INSERT INTO albaranp (numalbaranp, fechaalbaranp, idtrabajador, idproveedor, idforma_pago, idalmacen, refalbaranp) VALUES (5, '02/01/2005', NULL, 3, 2, 1,'RP5');
INSERT INTO albaranp (numalbaranp, fechaalbaranp, idtrabajador, idproveedor, idforma_pago, idalmacen, refalbaranp) VALUES (6, '03/01/2005', NULL, 4, 1, 1,'RP6');
|