ESTA ES MI ESTRUCTURA DE LAS TRES TABLAS Y AL FINAL PONGO MI CODIGO EN PHP
[/CODE]
FACTURA
CREATE TABLE factura
(
idfactura int4 NOT NULL,
fecha date,
total varchar(10),
pago varchar(10),
deuda varchar(10),
a_cuenta varchar(10),
idclientes int4,
CONSTRAINT pk_idfactura PRIMARY KEY (idfactura),
CONSTRAINT fk_idclientes FOREIGN KEY (idclientes)
REFERENCES clientes (idclientes) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
STOCK
CREATE TABLE stock
(
idstock int4 NOT NULL,
codigo varchar(15),
detalle varchar(80),
fecha_alta date,
t32 varchar(3),
t34 varchar(3),
t35 varchar(3),
t36 varchar(3),
t37 varchar(3),
t38 varchar(3),
t39 varchar(3),
t40 varchar(3),
t42 varchar(3),
t44 varchar(3),
t46 varchar(3),
t48 varchar(3),
t50 varchar(3),
t52 varchar(3),
t54 varchar(3),
t56 varchar(3),
p_vta varchar(5),
cant_pa varchar(3),
vta_cur varchar(10),
por_u varchar(3),
por_cur varchar(3),
cantidad varchar(10),
idprenda int4,
CONSTRAINT idstock PRIMARY KEY (idstock),
CONSTRAINT fk_idprenda FOREIGN KEY (idprenda)
REFERENCES prendas (idprenda) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
VENTAS
CREATE TABLE ventas
(
idventas int4 NOT NULL,
fecha date,
codigo varchar(15),
idusuario int4,
precio_unit varchar(10),
cantidad varchar(5),
total varchar(10),
"desc" varchar(3),
CONSTRAINT pk_ventas PRIMARY KEY (idventas),
CONSTRAINT fk_user FOREIGN KEY (idusuario)
REFERENCES usuario (idusuario) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
[CODE]
Código PHP:
<?php
require("librerias/conn.php");
$conexion= Conec_con_pass();
$total = $_POST['cant_rows'];
$n_fac = "select nextval('seq_factura')";
$Conn = ConectarBD();
$res_fac = EjecutarBD($Conn, $n_fac);
$can_fac = pg_fetch_result($res_fac,0);
$clie = $_POST['txtclie'];
$tot_ven = $_POST['pagar'];
$pago = $_POST['pagar'];
$seña = $_POST['pagar'];
$fec_fac = $_POST['txtfecha'];
$query2 = "insert into factura values ('$can_fac','$fec_fac', '$tot_ven', '$pago', '0', '$seña', '$clie' )";
$resul_fac = pg_query($query2);
for($i=0; $i < $total; $i++) {
// $var = $_POST['fec'][$i];
$var = cambiaf_a_bd($_POST['fec'][$i]);
$var1 = $_POST['cod'][$i];
$var2 = $_POST['use'][$i];
$var3 = $_POST['pre'][$i];
$var4 = $_POST['can'][$i];
$var5 = $_POST['pre_t'][$i];
$var6 = $_POST['des'][$i];
$var7 = $_POST['f_p'][$i];
$var8 = $_POST['tal'][$i];
$var9 = $_POST['sto'][$i];
BEGIN;
$query= "INSERT INTO ventas values ((select nextval('seq_ventas')),'$var','$var1','$var2','$var3','$var4','1','1','$can_fac')";
$result = pg_query($query);
$sql = "select $var8 from stock where codigo = $var1";
$Conn = ConectarBD();
$resultad = EjecutarBD($Conn, $sql);
//$fdev= pg_num_rows($result);
$total = pg_fetch_result($resultad,0);
$total = $total - $var4;
$tot_prenda = $var9 - $var4;
$query1 = "update stock set $var8 = $total, cant_pa =$tot_prenda where codigo = $var1";
$resulta = pg_query($query1);
}
COMMIT;
?>