/* IMPRESION DE HORAS DE PARQUEO */

SET TERM ^ ;



CREATE OR ALTER procedure IMPR_FACTURA (

    ID integer,

    MONEDAEX char(1))

returns (

    PREF varchar(4),

    NUMERO varchar(8),

    FECHA date,

    VENCE date,

    DIASVENCE integer,

    CONTCRED char(17),

    NIT varchar(20),

    NOMTERCERO varchar(60),

    NOMCLIENTE varchar(60),

    VENDEDOR integer,

    NOMVENDEDOR varchar(60),

    PUNTOVTA integer,

    NOMPUNTOVTA varchar(60),

    CODBODEGA varchar(2),

    NOMBODEGA varchar(30),

    AUTORIZACION varchar(20),

    DESPACHO varchar(255),

    TRANSPORTADOR varchar(60),

    COTIZACION varchar(30),

    PEDIDO varchar(30),

    REMISION varchar(30),

    PACIENTE varchar(20),

    NOMPACIENTE varchar(60),

    COMIPORC numeric(9,2),

    COMIMONTO numeric(18,2),

    SUBTOTAL numeric(18,2),

    INGTERCEROS numeric(18,2),

    INGPROPIOS numeric(18,2),

    DTOPORC numeric(9,2),

    DTOMONTO numeric(18,2),

    ADICIONAL numeric(18,2),

    IVAMONTO numeric(18,2),

    DEC2799 numeric(18,2),

    RTEFTEPORC numeric(9,2),

    RTEFTEMONTO numeric(18,2),

    RTEIVAPORC numeric(9,2),

    RTEIVAMONTO numeric(18,2),

    RTEICAPORC numeric(9,2),

    RTEICAMONTO numeric(18,2),

    RTECREEP numeric(9,2),

    RTECREEM numeric(18,2),

    EXTRA numeric(18,2),

    DTOFPORC numeric(9,2),

    DTOFMONTO numeric(18,2),

    FECHADTO date,

    ANTICIPO numeric(18,2),

    RETGARANTIA numeric(18,2),

    TOTALFAC numeric(18,2),

    TOTALPAGAR numeric(18,2),

    TOTALNETO numeric(18,2),

    ENTREGA date,

    OBS blob sub_type 1 segment size 80,

    ITEM integer,

    ARTICULO varchar(15),

    CODBAR varchar(15),

    ARTIDES varchar(60),

    DESCORTA varchar(30),

    CANT numeric(18,4),

    LOTE varchar(15),

    VLOTE date,

    UNIDAD varchar(8),

    CAJAS numeric(18,4),

    FACTOR numeric(18,4),

    LISTAPR integer,

    REFITEM varchar(60),

    BODITEM varchar(2),

    STAND varchar(20),

    PRUNIT numeric(18,2),

    DTOITPORC numeric(9,2),

    DTOITMONTO numeric(18,2),

    PRNETO numeric(18,2),

    PRNETOSINIVA numeric(18,2),

    BASE numeric(18,2),

    PORCBASE numeric(9,2),

    DTOIT1 numeric(9,2),

    DTOIT2 numeric(9,2),

    DTOIT3 numeric(9,2),

    IVAPORC numeric(9,2),

    IVAITMONTO numeric(18,2),

    CONSUMO numeric(18,2),

    INALC numeric(18,2),

    SUBTOTIT numeric(18,2),

    TOTAL numeric(18,2),

    PESOTOT numeric(18,2),

    CODIGOCL varchar(20),

    ZONACOD varchar(2),

    ZONANOM varchar(60),

    DV char(1),

    DIRECCION varchar(60),

    CIUDAD varchar(40),

    TELEFONO varchar(40),

    CONTACTO varchar(60),

    FAX varchar(25),

    CEL varchar(25),

    GRUPO varchar(2),

    NOMGRUPO varchar(30),

    SUBGRUPO varchar(3),

    NOMSUBG varchar(30),

    MARCA varchar(3),

    NOMMARCA varchar(30),

    PESO numeric(18,4),

    ANCHO numeric(18,4),

    ALTO numeric(18,4),

    LARGO numeric(18,4),

    SUMA1 numeric(18,2),

    SUMA2 numeric(18,2),

    SUMA3 numeric(18,2),

    SUMA4 numeric(18,2),

    SUMA5 numeric(18,2),

    IVA1 numeric(18,2),

    IVA2 numeric(18,2),

    IVA3 numeric(18,2),

    IVA4 numeric(18,2),

    IVA5 numeric(18,2),

    SUMAEX numeric(18,2),

    SUMAGR numeric(18,2),

    TOTCONSUMO numeric(18,2),

    TIPOVH varchar(30),

    TOTINALC numeric(18,2),

    DTOIT1M numeric(18,2),

    DTOIT2M numeric(18,2),

    DTOIT3M numeric(18,2),

    TOTCAJAS numeric(18,2),

    TOTBASE numeric(18,2),

    VEHICULO varchar(8),

    RECIBIDO numeric(18,2),

    CAMBIO numeric(18,2),

    HORA time,

    FORMAPAGO1 varchar(30),

    MONTOPAGO1 numeric(18,2),

    FORMAPAGO2 varchar(30),

    MONTOPAGO2 numeric(18,2),

    FORMAPAGO3 varchar(30),

    MONTOPAGO3 numeric(18,2),

    PUNTOS integer,

    PUNTOSACUM integer,

    PUNTOSVENC integer,

    FECPTOS date,

    OBSITEM blob sub_type 1 segment size 80,

    SUCUR varchar(10),

    ALTERNATIVAS numeric(18,4),

    PRINCIPALES numeric(18,4),

    FECINI date,

    FECFIN date,

    CARTINI numeric(18,2),

    CARTFIN numeric(18,2),

    ABONOS numeric(18,2),

    CARGOS numeric(18,2),

    CODRECAUDO varchar(65),

    CODRECAUDOR varchar(65),

    HORAINI time,

    HORAFIN time)

AS

DECLARE VARIABLE IMPTOS CHAR(1);

DECLARE VARIABLE TARIFA INTEGER;

declare variable DEVUELTO NUMERIC(18,2);

declare variable ORDENCOD CHAR(8);

declare variable IMPRDEV CHAR(2);

declare variable IMPR CHAR(1);

declare variable ITEMFP INTEGER;

declare variable FORMAP VARCHAR(30);

declare variable MONTOP NUMERIC(18,2);

declare variable ICOINC CHAR(2);

declare variable DTOITM1 NUMERIC(18,2);

declare variable DTOITM2 NUMERIC(18,2);

declare variable DTOITM3 NUMERIC(18,2);

declare variable DTOITE3 CHAR(2);

declare variable SUMDTOIT NUMERIC(18,2);

declare variable AUTORET VARCHAR(10);

declare variable FAUTORET DATE;

declare variable AUTORCREE VARCHAR(10);

declare variable FAUTORCREE DATE;

declare variable FACTCANT NUMERIC(18,4);

declare variable ANULADO CHAR(1);

declare variable INGTER CHAR(2);

declare variable FACTENC NUMERIC(18,4);

declare variable CODGS1 VARCHAR(13);

declare variable STRVALOR VARCHAR(11);

declare variable AUXVALOR VARCHAR(15);

declare variable TRM NUMERIC(18,4);

BEGIN

execute procedure lee_configuracion('FACTURACION', 'ARTICULOS', 'IMPOCONSUMO INCLUIDO EN EL PRECIO BASE DE VENTA ANTES DE IVA') returning_values (icoinc);

execute procedure lee_configuracion('FACTURACION', 'FACTURAS', 'COLUMNAS DE DESCUENTO POR ITEM ADICIONALES') returning_values (DTOITE3);

execute procedure lee_configuracion('FACTURACION', 'FACTURAS', 'GRUPO DE MERCANCIA PARA INGRESOS DE TERCEROS') returning_values (INGTER);

SELECT F.PREF_PRE, FACT_NUMERO, FACT_FECHA, FACT_VENCE, F.BODE_COD, BODE_NOM, F.TERC_NIT, FACT_NOMCLIENTE, F.VEND_COD, VEND_NOMBRE, F.PTVT_ID, PTVT_NOM,

    AUTO_NUMERO, FACT_DESPACHO, FACT_TRANSP, FACT_COTIZACI, FACT_PEDIDO, FACT_REMISION, FACT_COMIPORC, FACT_COMIMONTO, FACT_IVAINC, 

    FACT_DTOPOR, FACT_DTOMONTO, FACT_ADICIONAL, FACT_IVAMONTO, FACT_RTFTEPOR, FACT_RTFTEMONTO, FACT_RTIVAPOR, FACT_RTIVAMONTO,

    FACT_RTICAPOR, FACT_RTICAMONTO, FACT_RTCREE, FACT_RTCREEM, FACT_EXTRA, FACT_DTOFPOR, FACT_DTOFMONTO, FACT_DTOFFECHA, FACT_TOTAL,

    TERC_DIR, TERC_CIU, TERC_TEL, TERC_CONTACTO, TERC_FAX, TERC_DV, TERC_CEL, FACT_DTOIT1, FACT_DTOIT2, FACT_DTOIT3, FACT_DEC2799, FACT_ENTREGA, FACT_TRM,

    FACT_OBS, VEHI_COD, /*FACT_NROCOPIA,*/ FACT_RECIBIDO, FACT_SUCURSAL, FACT_DETCLI, FACT_DETCLINOM, TERC_NOM, FACT_ANULADO, FACT_ANTICIPO, FACT_GARANTIA, FACT_FACTOR, FACT_FECINI, FACT_FECFIN

    FROM FACTURAS F, BODEGA B, VENDEDORES V, PUNTO_VENTA P, TERCEROS T

    WHERE F.BODE_COD = B.BODE_COD AND F.VEND_COD = V.VEND_COD AND F.PTVT_ID = P.PTVT_ID AND F.TERC_NIT = T.TERC_NIT AND F.FACT_ID = :ID

    into :PREF, :NUMERO, :FECHA, :VENCE, :CODBODEGA, :NOMBODEGA, :NIT, :NOMTERCERO, :VENDEDOR, :NOMVENDEDOR, :PUNTOVTA, :NOMPUNTOVTA,

    :AUTORIZACION, :DESPACHO, :TRANSPORTADOR, :COTIZACION, :PEDIDO, :REMISION, :COMIPORC, :COMIMONTO, :IMPTOS,

    :DTOPORC, :DTOMONTO, :ADICIONAL, :IVAMONTO, :RTEFTEPORC, :RTEFTEMONTO, :RTEIVAPORC, :RTEIVAMONTO,

    :RTEICAPORC, :RTEICAMONTO, :rtecreep, :rtecreem, :EXTRA, :DTOFPORC, :DTOFMONTO, :FECHADTO, :TOTALFAC,

    :DIRECCION, :CIUDAD, :TELEFONO, :CONTACTO, :FAX, :DV, :CEL, :dtoit1m, :dtoit2m, :dtoit3m, :dec2799, :ENTREGA, :TRM,

    :OBS, :VEHICULO,/* :NROCOPIA,*/ :RECIBIDO, :SUCUR, :paciente, :nompaciente, :nomcliente, :ANULADO, :anticipo, :retgarantia, :factenc, :FECINI, :FECFIN;



/* ACUMULE LOS PUNTOS, SE HACE ACA POR SER POSTERIOR AL DETALLE */

if (ANULADO = 'N') then

    EXECUTE PROCEDURE acumula_puntos_cliente (31, :id, :nit, :fecha) returning_values (:PUNTOS);

EXECUTE PROCEDURE consulta_puntos_cliente (:id, :nit, :fecha) returning_values (:puntos, :puntosacum, :puntosvenc, :fecptos);

if (DTOIT1M IS NULL) then

    DTOIT1M = 0;

if (DTOIT2M IS NULL) then

    DTOIT2M = 0;

if (DTOIT3M IS NULL) then

    DTOIT3M = 0;

SELECT PARQ_VEHICULO, PARQ_HORAINI, PARQ_HORAFIN, TICH_NOM FROM parqueadero P, tipo_vehiculo T

    WHERE T.tivh_cod = P.tivh_cod AND PARQ_FACTURA = :ID INTO :vehiculo, :horaini, :horafin, :TIPOVH;

SELECT CLIE_COD, C.ZONA_COD FROM CLIENTES C

    WHERE TERC_NIT = :NIT INTO :CODIGOCL, :ZONACOD;

SELECT CLSU_DIR, CLSU_CIUDAD, CLSU_TEL, ZONA_COD FROM CLIENTE_SUCURSALES WHERE TERC_NIT = :NIT AND CLCU_COD = :SUCUR

    INTO :DIRECCION, :CIUDAD, :TELEFONO, :ZONACOD;

ZONANOM = '';

SELECT ZONA_NOM FROM ZONAS Z WHERE ZONA_COD = :zonacod INTO :ZONANOM;

DIASVENCE = VENCE - FECHA;

if (DIASVENCE > 0) then

    CONTCRED = 'CREDITO ' || CAST(DIASVENCE AS VARCHAR(3)) || ' DIAS';

ELSE

    CONTCRED = 'CONTADO';

SUBTOTAL = TOTALFAC - EXTRA - IVAMONTO - ADICIONAL + DTOMONTO + dec2799;

SELECT SUM(FADE_DTOMONTO*FADE_CANT) FROM FACTURAS_DETALLE WHERE FACT_ID = :id INTO :sumdtoit;

if (DTOITE3 = 'SI') then

    BEGIN

    SUBTOTAL = SUBTOTAL + sumdtoit + dtoit1m + dtoit2m + dtoit3m;

    END

if (INGTER <> '') then

    BEGIN

    SELECT SUM(FADE_TOTAL) FROM FACTURAS_DETALLE D, ARTICULO A WHERE A.arti_cod = D.arti_cod AND FACT_ID = :id AND GRUP_COD = :ingter

        INTO :ingterceros;

    INGPROPIOS = SUBTOTAL - INGTERCEROS;

    END

EXECUTE PROCEDURE LEE_CONFIGURACION('CARTERA', 'SALDOS', 'FECHA AUTORETENEDOR NO DESCONTAR RETENCIONES DEL SALDO PERO SI CONTABILIZARLAS') returning_values (AUTORET);

if (AUTORET <> '') then

    FAUTORET = CAST(AUTORET AS DATE);

else

    FAUTORET = '9999/12/31';

EXECUTE PROCEDURE LEE_CONFIGURACION('CARTERA', 'SALDOS', 'FECHA AUTORETENEDOR DE RETENCION CREE') returning_values (AUTORCREE);

if (AUTORCREE <> '') then

    FAUTORCREE = CAST(AUTORCREE AS DATE);

else

    FAUTORCREE = '9999/12/31';

if (FECHA > fautoret) then

    if (FECHA >= fautorcree) then

        TOTALPAGAR = TOTALFAC - RTEIVAMONTO - RTEICAMONTO - ANTICIPO - RETGARANTIA;

    else

        TOTALPAGAR = TOTALFAC - RTEIVAMONTO - RTEICAMONTO - rtecreem - ANTICIPO - RETGARANTIA;

ELSE

    if (FECHA >= fautorcree) then

        TOTALPAGAR = TOTALFAC - RTEFTEMONTO - RTEIVAMONTO - RTEICAMONTO - ANTICIPO - RETGARANTIA;

    else

        TOTALPAGAR = TOTALFAC - RTEFTEMONTO - rtecreem - RTEIVAMONTO - RTEICAMONTO - ANTICIPO - RETGARANTIA;

TOTALNETO = TOTALFAC - ANTICIPO - RETGARANTIA;

if (RECIBIDO <> 0) then

    CAMBIO = RECIBIDO - totalpagar;

ELSE

    CAMBIO = 0;

/* si no hay cotizacion pero hay pedido llame la cotizacion del pedido */

if ((cotizacion is null) or (cotizacion = '')) then

    if ((pedido <> '') AND (STRLEN(:PEDIDO) > 6)) then

        select pedi_cotizaci from pedidos p where pedi_numero = right(:pedido, 6) and pref_pre = left(:pedido,  strlen(:pedido)-6) into :cotizacion;

/* CARTERA Y ABONOS */

SELECT SUM(SALDO) FROM cartera_cliente(:nit, :fecini-1, :sucur, 0, 'N', 0) INTO :cartini;

SELECT SUM(SALDO) FROM cartera_cliente(:nit, :fecfin, :sucur, 0, 'N', 0) INTO :cartfin;

SELECT SUM(M.mvcl_monto) FROM movimiento_clientes M WHERE TERC_NIT = :NIT AND M.mvcl_sucursal = :sucur AND

    M.mvcl_abono = 'S' AND M.mvcl_fecha >= :fecini AND M.mvcl_fecha <= :fecfin INTO :abonos;

SELECT SUM(M.mvcl_monto) FROM movimiento_clientes M WHERE TERC_NIT = :NIT AND M.mvcl_sucursal = :sucur AND

    M.mvcl_abono = 'N' AND M.mvcl_fecha >= :fecini AND M.mvcl_fecha <= :fecfin INTO :cargos;

if (cartini IS NULL) then

    CARTINI = 0;

if (cartfin IS NULL) then

    cartfin = 0;

if (abonos IS NULL) then

    abonos = 0;

if (cargos IS NULL) then

    cargos = 0;

/* CODIGO DE RECAUDO

STRVALOR = substring(cast(:totalfac as varchar(10)) from 1 for strlen(cast(:totalfac as varchar(10)))-3);

auxvalor = '000000' || strvalor;

strvalor = SUBSTR(auxvalor, (STRLEN(auxvalor) - 8), STRLEN(auxvalor));

EXECUTE PROCEDURE lee_configuracion('FACTURACION', 'FACTURAS', 'CODIGO DE IDENTIFICACION EAN-13 PARA EL GENERADOR DE RECAUDOS') returning_values (codgs1);

if (mod(strlen(:codigocl), 2) = 1) then

    codigocl = '0' || codigocl;

if (mod(strlen(strvalor), 2) = 1) then

    strvalor = '0' || strvalor;

CODRECAUDO = 'n415' || :CODGS1 || '8020' || :codigocl || 'n3900' || :strvalor || 'n96' || substring(:vence from 1 for 4) || substring(:vence from 6 for 2) || substring(:vence from 9 for 2);

CODRECAUDOR = '(415)' || :CODGS1 || '(8020)' || :codigocl || '(3900)' || :strvalor || '(96)' || substring(:vence from 1 for 4) || substring(:vence from 6 for 2) || substring(:vence from 9 for 2);

*/

SUMAEX = 0;

SUMA1 = 0;

SUMA2 = 0;

SUMA3 = 0;

SUMA4 = 0;

SUMA5 = 0;

IVA1 = 0;

IVA2 = 0;

IVA3 = 0;

IVA4 = 0;

IVA5 = 0;

TOTCONSUMO = 0;

TOTINALC = 0;

MONTOPAGO1 = 0;

PESOTOT = 0;

SELECT MAX(AUDI_HORA) FROM AUDITORIA WHERE TIDO_COD = 31 AND AUDI_IDDOC = :ID AND AUDI_OPER = 'I' INTO :HORA;

/* AHORA LAS FORMAS DE PAGO, BUSQUE EL ABONO DE HOY A LA FACTURA */

ITEMFP = 1;

MONTOPAGO1 = 0;

MONTOPAGO2 = 0;

MONTOPAGO3 = 0;

FOR SELECT FOPA_NOM, RCPA_MONTO FROM RECIBOS_CAJA_PAGO P, FORMAS_PAGO F, RECIBOS_CAJA R, RECIBOS_CAJA_DETALLE D

    WHERE R.RECA_ID = P.RECA_ID AND R.RECA_ID = D.RECA_ID AND P.FOPA_ID = F.FOPA_ID AND RECA_FECHA = :FECHA AND RCDE_TIPODOC = 31 AND RCDE_IDDOC = :ID

    INTO :formap, :MONTOP

    DO

    BEGIN

    if (ITEMFP = 1) then

        BEGIN

        FORMAPAGO1 = FORMAP;

        MONTOPAGO1 = MONTOP;

        END

    if (ITEMFP = 2) then

        BEGIN

        FORMAPAGO2 = FORMAP;

        MONTOPAGO2 = MONTOP;

        END

    if (ITEMFP = 3) then

        BEGIN

        FORMAPAGO3 = FORMAP;

        MONTOPAGO3 = MONTOP;

        END

    ITEMFP = ITEMFP + 1;

    END

if ((MONEDAEX = 'S') AND (TRM <> 1)) then

    BEGIN

    SUBTOTAL = SUBTOTAL / TRM;

    INGTERCEROS = INGTERCEROS / TRM;

    INGPROPIOS = INGPROPIOS / TRM;

    DTOMONTO = DTOMONTO / TRM;

    ADICIONAL = ADICIONAL / TRM;

    IVAMONTO = IVAMONTO / TRM;

    RTEFTEMONTO = RTEFTEMONTO / TRM;

    RTEIVAMONTO = RTEIVAMONTO / TRM;

    RTEICAMONTO = RTEICAMONTO / TRM;

    RTECREEM = RTECREEM / TRM;

    EXTRA = EXTRA / TRM;

    DTOFMONTO = DTOFMONTO / TRM;

    ANTICIPO = ANTICIPO / TRM;

    RETGARANTIA = RETGARANTIA / TRM;

    TOTALFAC = TOTALFAC / TRM;

    TOTALPAGAR = TOTALPAGAR / TRM;

    TOTALNETO = TOTALNETO / TRM;

    SUMA1 = SUMA1 / TRM;

    SUMA2 = SUMA2 / TRM;

    SUMA3 = SUMA3 / TRM;

    SUMA4 = SUMA4 / TRM;

    SUMA5 = SUMA5 / TRM;

    IVA1 = IVA1 / TRM;

    IVA2 = IVA2 / TRM;

    IVA3 = IVA3 / TRM;

    IVA4 = IVA4 / TRM;

    IVA5 = IVA5 / TRM;

    SUMAEX = SUMAEX / TRM;

    SUMAGR = SUMAGR / TRM;

    TOTCONSUMO = TOTCONSUMO / TRM;

    TOTINALC = TOTINALC / TRM;

    DTOIT1M = DTOIT1M / TRM;

    DTOIT2M = DTOIT2M / TRM;

    DTOIT3M = DTOIT3M / TRM;

    END

EXECUTE PROCEDURE lee_configuracion ('FACTURACION','DOCUMENTOS','ORDENAR ITEMS POR CODIGO DE ARTICULO') returning_values (:ORDENCOD);

EXECUTE PROCEDURE lee_configuracion ('FACTURACION','POS', 'IMPRIMIR DEVOLUCIONES DE ARTICULOS NEGATIVOS') returning_values (:imprdev);

totcajas = 0;

totbase = 0;

if (ORDENCOD = 'SI') then

    BEGIN

    FOR SELECT FADE_ITEM, D.ARTI_COD, FADE_CODBAR, SUBSTRING(FADE_DESC FROM 1 FOR 60), ARTI_DESCORTA, FADE_CANT, FADE_UNIDAD, FADE_FACTOR, BODE_COD, FADE_LOTE, LIPR_COD, FADE_PRUNIT, FADE_DTOPORC, FADE_DTOMONTO, FADE_IVAPORC, FADE_IVAMONTO, FADE_CONSUMO, FADE_TOTAL, FADE_REFERENCIA,

        GRUP_COD, SUBG_COD, MARC_COD, ARTI_PESO, ARTI_ANCHO, ARTI_ALTO, ARTI_LARGO, TAIV_COD, FADE_DEVUELTO, FADE_OBS, FADE_DTO1, FADE_DTO2, FADE_DTO3, FADE_DTOM1, FADE_DTOM2, FADE_DTOM3, FADE_CAJAS, FADE_BASE, FADE_PORCBASE, FADE_INALCM

        FROM FACTURAS_DETALLE D, ARTICULO A

        WHERE D.ARTI_COD = A.ARTI_COD AND D.FACT_ID = :ID

        ORDER BY D.ARTI_COD

        INTO :ITEM, :ARTICULO, :CODBAR, :ARTIDES, :DESCORTA, :CANT, :UNIDAD, :FACTOR, :BODITEM, :LOTE, :LISTAPR, :PRUNIT, :DTOITPORC, :DTOITMONTO, :IVAPORC, :IVAITMONTO, :CONSUMO, :TOTAL, :refitem,

        :GRUPO, :SUBGRUPO, :MARCA, :PESO, :ANCHO, :ALTO, :LARGO, :TARIFA, :devuelto, :obsitem, :dtoit1, :dtoit2, :dtoit3, :dtoitm1, :dtoitm2, :dtoitm3, :cajas, :base, :porcbase, :inalc

        DO

          BEGIN

          if (ARTICULO = CODBAR) then

            SELECT FIRST 1 COBA_COD FROM barras_articulo WHERE ARTI_COD = :articulo INTO :CODBAR;

          if (DTOIT1 IS NULL) then

            DTOIT1 = 0;

          if (DTOIT2 IS NULL) then

            DTOIT2 = 0;

          if (DTOIT3 IS NULL) then

            DTOIT3 = 0;

          if (IMPRDEV <> 'SI') then

            BEGIN

            if ((CANT >= 0) AND (DEVUELTO = 0)) then

                IMPR = 'S';

            ELSE

                begin

                if (EXISTS (SELECT FADE_ITEM FROM FACTURAS_DETALLE WHERE FADE_CANT < 0 AND FACT_ID = :ID AND ARTI_COD = :ARTICULO)) then

                    IMPR = 'N'; /* ES DEVOLUCION HECHA EN LA FACTURA */

                ELSE

                    IMPR = 'S'; /* ES DEVOLUCION HECHA FUERA DE LA FACTURA */

                end

            END

          ELSE

            IMPR = 'S';

          if (IMPR = 'S') then

            BEGIN

            if (CANT <> 0) then

                prneto = (PRUNIT - DTOITMONTO) * ((100 - dtoit1)/100) * ((100-dtoit2)/100) * ((100-dtoit3)/100);

            ELSE

                prneto = PRUNIT - DTOITMONTO;

            if (IMPTOS = 'S') then

                if (cant <> 0) then

                    begin

                    prnetosiniva = (PRUNIT - DTOITMONTO - ((IVAITMONTO + CONSUMO + INALC) / CANT));

                    prnetosiniva = prnetosiniva * ((100 - dtoit1)/100) * ((100-dtoit2)/100) * ((100-dtoit3)/100);

                    end

                else

                    prnetosiniva = PRUNIT - DTOITMONTO;

            if (ICOINC = 'SI') then

                SUBTOTIT = TOTAL - IVAITMONTO;

            else

                SUBTOTIT = TOTAL - IVAITMONTO - CONSUMO - INALC;

            TOTCONSUMO = TOTCONSUMO + CONSUMO;

            TOTINALC = TOTINALC + INALC;

            TOTBASE = TOTBASE + BASE;

            SELECT LOTE_VENCE FROM LOTES WHERE LOTE_NRO = :lote AND ARTI_COD = :ARTICULO AND BODE_COD = :boditem INTO :vlote;

            if ((REFITEM <> '.t') or (REFITEM IS NULL)) then

                BEGIN

                /* NO SUME LOS ENSAMBLES */

                if (IVAPORC = 0) then

                    SUMAEX = SUMAEX + (TOTAL - CONSUMO - INALC)*FACTENC;

                if (TARIFA = 1) then

                    BEGIN

                    SUMA1 = SUMA1 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA1 = IVA1 + IVAITMONTO*FACTENC;

                    END

                if (TARIFA = 2) then

                    BEGIN

                    SUMA2 = SUMA2 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA2 = IVA2 + IVAITMONTO*FACTENC;

                    END

                if (TARIFA = 3) then

                    BEGIN

                    SUMA3 = SUMA3 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA3 = IVA3 + IVAITMONTO*FACTENC;

                    END

                if (TARIFA = 4) then

                    BEGIN

                    SUMA4 = SUMA4 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA4 = IVA4 + IVAITMONTO*FACTENC;

                    END

                if (TARIFA = 5) then

                    BEGIN

                    SUMA5 = SUMA5 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA5 = IVA5 + IVAITMONTO*FACTENC;

                    END

                SUMAGR = 0;

                if (IVA1 <> 0) then

                    SUMAGR = SUMAGR + SUMA1;

                if (IVA2 <> 0) then

                    SUMAGR = SUMAGR + SUMA2;

                if (IVA3 <> 0) then

                    SUMAGR = SUMAGR + SUMA3;

                if (IVA4 <> 0) then

                    SUMAGR = SUMAGR + SUMA4;

                if (IVA5 <> 0) then

                    SUMAGR = SUMAGR + SUMA5;

                END

            if (ANCHO IS NULL) then

                ANCHO = 0;

            if (ALTO IS NULL) then

                ALTO = 0;

            if (LARGO IS NULL) then

                LARGO = 0;

            if (PESO IS NULL) then

                PESO = 0;

            PESO = (PESO / 1000) * CANT * FACTOR;

            PESOTOT = PESOTOT + PESO;

            totcajas = totcajas + cajas;

            if (substr(ARTICULO,1,2) = '.t') then

                BEGIN

                REFITEM = '';

                TOTAL = 0;

                SUBTOTIT = 0;

                END

            SELECT STAND_COD FROM STANDS WHERE ARTI_COD = :ARTICULO AND STAND_BODEGA = :boditem INTO :stand;

            factcant = NULL;

            SELECT FIRST 1 UNAR_FACCAN FROM unidad_articulo WHERE ARTI_COD = :articulo INTO :factcant;

            if (factcant IS NULL) then

                BEGIN

                ALTERNATIVAS = 0;

                PRINCIPALES = (CANT * FACTOR);

                END

            ELSE

                BEGIN

                ALTERNATIVAS = FLOOR(CANT * factor / factcant);

                PRINCIPALES = (CANT * FACTOR) - (alternativas * factcant);

                END

            if ((MONEDAEX = 'S') AND (TRM <> 1)) then

                BEGIN

                PRUNIT = PRUNIT / TRM;

                DTOITMONTO = DTOITMONTO / TRM;

                PRNETO = PRNETO / TRM;

                PRNETOSINIVA = PRNETOSINIVA / TRM;

                BASE = base / TRM;

                IVAITMONTO = IVAITMONTO / TRM;

                CONSUMO = consumo / TRM;

                INALC = INALC / TRM;

                SUBTOTIT = SUBTOTIT / TRM;

                TOTAL = TOTAL / TRM;

                END

            SUSPEND;

            END

          END

    END

ELSE

  if (ORDENCOD = 'UNIDAD') then

    BEGIN

    FOR SELECT FADE_ITEM, D.ARTI_COD, FADE_CODBAR, SUBSTRING(FADE_DESC FROM 1 FOR 60), ARTI_DESCORTA, FADE_CANT, FADE_UNIDAD, FADE_FACTOR, BODE_COD, FADE_LOTE, LIPR_COD, FADE_PRUNIT, FADE_DTOPORC, FADE_DTOMONTO, FADE_IVAPORC, FADE_IVAMONTO, FADE_CONSUMO, FADE_TOTAL, FADE_REFERENCIA,

        GRUP_COD, SUBG_COD, MARC_COD, ARTI_PESO, ARTI_ANCHO, ARTI_ALTO, ARTI_LARGO, TAIV_COD, FADE_DEVUELTO, FADE_OBS, FADE_DTO1, FADE_DTO2, FADE_DTO3, FADE_DTOM1, FADE_DTOM2, FADE_DTOM3, FADE_CAJAS, FADE_BASE, FADE_PORCBASE, FADE_INALCM

        FROM FACTURAS_DETALLE D, ARTICULO A

        WHERE D.ARTI_COD = A.ARTI_COD AND D.FACT_ID = :ID

        ORDER BY D.fade_unidad, D.fade_desc

        INTO :ITEM, :ARTICULO, :CODBAR, :ARTIDES, :DESCORTA, :CANT, :UNIDAD, :FACTOR, :BODITEM, :LOTE, :LISTAPR, :PRUNIT, :DTOITPORC, :DTOITMONTO, :IVAPORC, :IVAITMONTO, :CONSUMO, :TOTAL, :refitem,

        :GRUPO, :SUBGRUPO, :MARCA, :PESO, :ANCHO, :ALTO, :LARGO, :TARIFA, :devuelto, :obsitem, :dtoit1, :dtoit2, :dtoit3, :dtoitm1, :dtoitm2, :dtoitm3, :cajas, :base, :porcbase, :INALC

        DO

          BEGIN

          if (ARTICULO = CODBAR) then

            SELECT FIRST 1 COBA_COD FROM barras_articulo WHERE ARTI_COD = :articulo INTO :CODBAR;

          if (IMPRDEV <> 'SI') then

            BEGIN

            if ((CANT >= 0) AND (DEVUELTO = 0)) then

                IMPR = 'S';

            ELSE

                begin

                if (EXISTS (SELECT FADE_ITEM FROM FACTURAS_DETALLE WHERE FADE_CANT < 0 AND FACT_ID = :ID AND ARTI_COD = :ARTICULO)) then

                    IMPR = 'N'; /* ES DEVOLUCION HECHA EN LA FACTURA */

                ELSE

                    IMPR = 'S'; /* ES DEVOLUCION HECHA FUERA DE LA FACTURA */

                end

            END

          ELSE

            IMPR = 'S';

          if (IMPR = 'S') then

            BEGIN

            if (CANT <> 0) then

                prneto = (PRUNIT - DTOITMONTO) * ((100 - dtoit1)/100) * ((100-dtoit2)/100) * ((100-dtoit3)/100);

            ELSE

                prneto = PRUNIT - DTOITMONTO;

            if (IMPTOS = 'S') then

                if (cant <> 0) then

                    begin

                    prnetosiniva = (PRUNIT - DTOITMONTO - ((IVAITMONTO + CONSUMO + INALC) / CANT));

                    prnetosiniva = prnetosiniva * ((100 - dtoit1)/100) * ((100-dtoit2)/100) * ((100-dtoit3)/100);

                    end

                else

                    prnetosiniva = PRUNIT - DTOITMONTO;

            if (ICOINC = 'SI') then

                SUBTOTIT = TOTAL - IVAITMONTO;

            else

                SUBTOTIT = TOTAL - IVAITMONTO - CONSUMO - INALC;

            TOTCONSUMO = TOTCONSUMO + CONSUMO;

            TOTINALC = TOTINALC + INALC;

            TOTBASE = TOTBASE + BASE;

            SELECT LOTE_VENCE FROM LOTES WHERE LOTE_NRO = :lote AND ARTI_COD = :ARTICULO AND BODE_COD = :boditem INTO :vlote;

            if ((REFITEM <> '.t') or (REFITEM IS NULL)) then

                BEGIN

                /* NO SUME LOS ENSAMBLES */

                if (IVAPORC = 0) then

                    SUMAEX = SUMAEX + (TOTAL - CONSUMO - INALC)*FACTENC;

                if (TARIFA = 1) then

                    BEGIN

                    SUMA1 = SUMA1 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA1 = IVA1 + IVAITMONTO*FACTENC;

                    END

                if (TARIFA = 2) then

                    BEGIN

                    SUMA2 = SUMA2 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA2 = IVA2 + IVAITMONTO*FACTENC;

                    END

                if (TARIFA = 3) then

                    BEGIN

                    SUMA3 = SUMA3 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA3 = IVA3 + IVAITMONTO*FACTENC;

                    END

                if (TARIFA = 4) then

                    BEGIN

                    SUMA4 = SUMA4 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA4 = IVA4 + IVAITMONTO*FACTENC;

                    END

                if (TARIFA = 5) then

                    BEGIN

                    SUMA5 = SUMA5 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                    IVA5 = IVA5 + IVAITMONTO*FACTENC;

                    END

                SUMAGR = 0;

                if (IVA1 <> 0) then

                    SUMAGR = SUMAGR + SUMA1;

                if (IVA2 <> 0) then

                    SUMAGR = SUMAGR + SUMA2;

                if (IVA3 <> 0) then

                    SUMAGR = SUMAGR + SUMA3;

                if (IVA4 <> 0) then

                    SUMAGR = SUMAGR + SUMA4;

                if (IVA5 <> 0) then

                    SUMAGR = SUMAGR + SUMA5;

                END

            if (ANCHO IS NULL) then

                ANCHO = 0;

            if (ALTO IS NULL) then

                ALTO = 0;

            if (LARGO IS NULL) then

                LARGO = 0;

            if (PESO IS NULL) then

                PESO = 0;

            PESO = (PESO / 1000) * CANT * FACTOR;

            PESOTOT = PESOTOT + PESO;

            totcajas = totcajas + cajas;

            if (substr(ARTICULO,1,2) = '.t') then

                BEGIN

                REFITEM = '';

                TOTAL = 0;

                SUBTOTIT = 0;

                END

            SELECT STAND_COD FROM STANDS WHERE ARTI_COD = :ARTICULO AND STAND_BODEGA = :boditem INTO :stand;

            factcant = NULL;

            SELECT FIRST 1 UNAR_FACCAN FROM unidad_articulo WHERE ARTI_COD = :articulo INTO :factcant;

            if ((factcant IS NULL) or (factcant = 0)) then

                BEGIN

                ALTERNATIVAS = 0;

                PRINCIPALES = (CANT * FACTOR);

                END

            ELSE

                BEGIN

                ALTERNATIVAS = FLOOR(CANT * factor / factcant);

                PRINCIPALES = (CANT * FACTOR) - (alternativas * factcant);

                END

            if ((MONEDAEX = 'S') AND (TRM <> 1)) then

                BEGIN

                PRUNIT = PRUNIT / TRM;

                DTOITMONTO = DTOITMONTO / TRM;

                PRNETO = PRNETO / TRM;

                PRNETOSINIVA = PRNETOSINIVA / TRM;

                BASE = base / TRM;

                IVAITMONTO = IVAITMONTO / TRM;

                CONSUMO = consumo / TRM;

                INALC = INALC / TRM;

                SUBTOTIT = SUBTOTIT / TRM;

                TOTAL = TOTAL / TRM;

                END

            SUSPEND;

            END

          END

    END

  ELSE

    if (ORDENCOD = 'STAND') then

      BEGIN

      FOR SELECT FADE_ITEM, D.ARTI_COD, FADE_CODBAR, SUBSTRING(FADE_DESC FROM 1 FOR 60), ARTI_DESCORTA, FADE_CANT, FADE_UNIDAD, FADE_FACTOR, BODE_COD, FADE_LOTE, LIPR_COD, FADE_PRUNIT, FADE_DTOPORC, FADE_DTOMONTO, FADE_IVAPORC, FADE_IVAMONTO, FADE_CONSUMO, FADE_TOTAL, FADE_REFERENCIA,

            GRUP_COD, SUBG_COD, MARC_COD, ARTI_PESO, ARTI_ANCHO, ARTI_ALTO, ARTI_LARGO, TAIV_COD, FADE_DEVUELTO, FADE_OBS, FADE_DTO1, FADE_DTO2, FADE_DTO3, FADE_DTOM1, FADE_DTOM2, FADE_DTOM3, FADE_CAJAS, FADE_BASE, FADE_PORCBASE, S.stand_cod, FADE_INALCM

            FROM FACTURAS_DETALLE D, ARTICULO A, STANDS S

            WHERE D.ARTI_COD = A.ARTI_COD AND D.FACT_ID = :ID AND A.arti_cod = S.arti_cod AND S.stand_bodega = D.bode_cod

            ORDER BY S.stand_cod, D.fade_desc

            INTO :ITEM, :ARTICULO, :CODBAR, :ARTIDES, :DESCORTA, :CANT, :UNIDAD, :FACTOR, :BODITEM, :LOTE, :LISTAPR, :PRUNIT, :DTOITPORC, :DTOITMONTO, :IVAPORC, :IVAITMONTO, :CONSUMO, :TOTAL, :refitem,

            :GRUPO, :SUBGRUPO, :MARCA, :PESO, :ANCHO, :ALTO, :LARGO, :TARIFA, :devuelto, :obsitem, :dtoit1, :dtoit2, :dtoit3, :dtoitm1, :dtoitm2, :dtoitm3, :cajas, :base, :porcbase, :STAND, :INALC

            DO

              BEGIN

              if (ARTICULO = CODBAR) then

                SELECT FIRST 1 COBA_COD FROM barras_articulo WHERE ARTI_COD = :articulo INTO :CODBAR;

              if (IMPRDEV <> 'SI') then

                BEGIN

                if ((CANT >= 0) AND (DEVUELTO = 0)) then

                    IMPR = 'S';

                ELSE

                    begin

                    if (EXISTS (SELECT FADE_ITEM FROM FACTURAS_DETALLE WHERE FADE_CANT < 0 AND FACT_ID = :ID AND ARTI_COD = :ARTICULO)) then

                        IMPR = 'N'; /* ES DEVOLUCION HECHA EN LA FACTURA */

                    ELSE

                        IMPR = 'S'; /* ES DEVOLUCION HECHA FUERA DE LA FACTURA */

                    end

                END

              ELSE

                IMPR = 'S';

              if (IMPR = 'S') then

                BEGIN

                if (CANT <> 0) then

                    prneto = (PRUNIT - DTOITMONTO) * ((100 - dtoit1)/100) * ((100-dtoit2)/100) * ((100-dtoit3)/100);

                ELSE

                    prneto = PRUNIT - DTOITMONTO;

                if (IMPTOS = 'S') then

                    if (cant <> 0) then

                        begin

                        prnetosiniva = (PRUNIT - DTOITMONTO - ((IVAITMONTO + CONSUMO + INALC) / CANT));

                        prnetosiniva = prnetosiniva * ((100 - dtoit1)/100) * ((100-dtoit2)/100) * ((100-dtoit3)/100);

                        end

                    else

                        prnetosiniva = PRUNIT - DTOITMONTO;

                if (ICOINC = 'SI') then

                    SUBTOTIT = TOTAL - IVAITMONTO;

                else

                    SUBTOTIT = TOTAL - IVAITMONTO - CONSUMO - INALC;

                TOTCONSUMO = TOTCONSUMO + CONSUMO;

                TOTINALC = TOTINALC + INALC;

                TOTBASE = TOTBASE + BASE;

                SELECT LOTE_VENCE FROM LOTES WHERE LOTE_NRO = :lote AND ARTI_COD = :ARTICULO AND BODE_COD = :boditem INTO :vlote;

                if ((REFITEM <> '.t') or (REFITEM IS NULL)) then

                    BEGIN

                    /* NO SUME LOS ENSAMBLES */

                    if (IVAPORC = 0) then

                        SUMAEX = SUMAEX + (TOTAL - CONSUMO - INALC)*FACTENC;

                    if (TARIFA = 1) then

                        BEGIN

                        SUMA1 = SUMA1 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA1 = IVA1 + IVAITMONTO*FACTENC;

                        END

                    if (TARIFA = 2) then

                        BEGIN

                        SUMA2 = SUMA2 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA2 = IVA2 + IVAITMONTO*FACTENC;

                        END

                    if (TARIFA = 3) then

                        BEGIN

                        SUMA3 = SUMA3 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA3 = IVA3 + IVAITMONTO*FACTENC;

                        END

                    if (TARIFA = 4) then

                        BEGIN

                        SUMA4 = SUMA4 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA4 = IVA4 + IVAITMONTO*FACTENC;

                        END

                    if (TARIFA = 5) then

                        BEGIN

                        SUMA5 = SUMA5 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA5 = IVA5 + IVAITMONTO*FACTENC;

                        END

                    SUMAGR = 0;

                    if (IVA1 <> 0) then

                        SUMAGR = SUMAGR + SUMA1;

                    if (IVA2 <> 0) then

                        SUMAGR = SUMAGR + SUMA2;

                    if (IVA3 <> 0) then

                        SUMAGR = SUMAGR + SUMA3;

                    if (IVA4 <> 0) then

                        SUMAGR = SUMAGR + SUMA4;

                    if (IVA5 <> 0) then

                        SUMAGR = SUMAGR + SUMA5;

                    END

                if (ANCHO IS NULL) then

                    ANCHO = 0;

                if (ALTO IS NULL) then

                    ALTO = 0;

                if (LARGO IS NULL) then

                    LARGO = 0;

                if (PESO IS NULL) then

                    PESO = 0;

                PESO = (PESO / 1000) * CANT * FACTOR;

                PESOTOT = PESOTOT + PESO;

                totcajas = totcajas + cajas;

                if (substr(ARTICULO,1,2) = '.t') then

                    BEGIN

                    REFITEM = '';

                    TOTAL = 0;

                    SUBTOTIT = 0;

                    END

                SELECT STAND_COD FROM STANDS WHERE ARTI_COD = :ARTICULO AND STAND_BODEGA = :boditem INTO :stand;

                factcant = NULL;

                SELECT FIRST 1 UNAR_FACCAN FROM unidad_articulo WHERE ARTI_COD = :articulo INTO :factcant;

                if ((factcant IS NULL) or (factcant = 0)) then

                    BEGIN

                    ALTERNATIVAS = 0;

                    PRINCIPALES = (CANT * FACTOR);

                    END

                ELSE

                    BEGIN

                    ALTERNATIVAS = FLOOR(CANT * factor / factcant);

                    PRINCIPALES = (CANT * FACTOR) - (alternativas * factcant);

                    END

                if ((MONEDAEX = 'S') AND (TRM <> 1)) then

                    BEGIN

                    PRUNIT = PRUNIT / TRM;

                    DTOITMONTO = DTOITMONTO / TRM;

                    PRNETO = PRNETO / TRM;

                    PRNETOSINIVA = PRNETOSINIVA / TRM;

                    BASE = base / TRM;

                    IVAITMONTO = IVAITMONTO / TRM;

                    CONSUMO = consumo / TRM;

                    INALC = INALC / TRM;

                    SUBTOTIT = SUBTOTIT / TRM;

                    TOTAL = TOTAL / TRM;

                    END

                SUSPEND;

                END

              END

      END

    ELSE

        BEGIN

        FOR SELECT FADE_ITEM, D.ARTI_COD, FADE_CODBAR, SUBSTRING(FADE_DESC from 1 FOR 60), ARTI_DESCORTA, FADE_CANT, FADE_UNIDAD, FADE_FACTOR, BODE_COD, FADE_LOTE, LIPR_COD, FADE_PRUNIT, FADE_DTOPORC, FADE_DTOMONTO, FADE_IVAPORC, FADE_IVAMONTO, FADE_CONSUMO, FADE_TOTAL, FADE_REFERENCIA,

            GRUP_COD, SUBG_COD, MARC_COD, ARTI_PESO, ARTI_ANCHO, ARTI_ALTO, ARTI_LARGO, TAIV_COD, FADE_DEVUELTO, FADE_OBS, FADE_DTO1, FADE_DTO2, FADE_DTO3, FADE_DTOM1, FADE_DTOM2, FADE_DTOM3, FADE_CAJAS, FADE_BASE, FADE_PORCBASE, FADE_INALCM

            FROM FACTURAS_DETALLE D, ARTICULO A

            WHERE D.ARTI_COD = A.ARTI_COD AND D.FACT_ID = :ID

            ORDER BY FADE_ITEM

            INTO :ITEM, :ARTICULO, :CODBAR, :ARTIDES, :DESCORTA, :CANT, :UNIDAD, :FACTOR, :BODITEM, :LOTE, :LISTAPR, :PRUNIT, :DTOITPORC, :DTOITMONTO, :IVAPORC, :IVAITMONTO, :CONSUMO, :TOTAL, :refitem,

            :GRUPO, :SUBGRUPO, :MARCA, :PESO, :ANCHO, :ALTO, :LARGO, :TARIFA, :devuelto, :obsitem, :dtoit1, :dtoit2, :dtoit3, :dtoitm1, :dtoitm2, :dtoitm3, :cajas, :base, :porcbase, :INALC

            DO

              BEGIN

              if (IMPRDEV <> 'SI') then

                BEGIN

                if ((CANT >= 0) AND (DEVUELTO = 0)) then

                    IMPR = 'S';

                ELSE

                    begin

                    if (EXISTS (SELECT FADE_ITEM FROM FACTURAS_DETALLE WHERE FADE_CANT < 0 AND FACT_ID = :ID AND ARTI_COD = :ARTICULO)) then

                        IMPR = 'N'; /* ES DEVOLUCION HECHA EN LA FACTURA */

                    ELSE

                        IMPR = 'S'; /* ES DEVOLUCION HECHA FUERA DE LA FACTURA */

                    end

                END

              ELSE

                IMPR = 'S';

              if (IMPR = 'S') then

                BEGIN

                SELECT GRUP_NOM FROM GRUPO G WHERE GRUP_COD = :GRUPO INTO NOMGRUPO;

                SELECT MARC_NOM FROM MARCAS WHERE MARC_COD = :MARCA INTO NOMMARCA;

                SELECT SUBG_NOM FROM SUBGRUPO WHERE SUBG_COD = :SUBGRUPO AND GRUP_COD = :GRUPO INTO :NOMSUBG;

                if (CANT <> 0) then

                    prneto = (PRUNIT - DTOITMONTO) * ((100 - dtoit1)/100) * ((100-dtoit2)/100) * ((100-dtoit3)/100);

                ELSE

                    prneto = PRUNIT - DTOITMONTO;

                if (IMPTOS = 'S') then

                    if (cant <> 0) then

                        begin

                        prnetosiniva = (PRUNIT - DTOITMONTO - ((IVAITMONTO + CONSUMO + INALC) / CANT));

                        prnetosiniva = prnetosiniva * ((100 - dtoit1)/100) * ((100-dtoit2)/100) * ((100-dtoit3)/100);

                        end

                    else

                        prnetosiniva = PRUNIT - DTOITMONTO;

                if (ICOINC = 'SI') then

                    SUBTOTIT = TOTAL - IVAITMONTO;

                else

                    SUBTOTIT = TOTAL - IVAITMONTO - CONSUMO - INALC;

                TOTCONSUMO = TOTCONSUMO + CONSUMO;

                TOTINALC = TOTINALC + INALC;

                TOTBASE = TOTBASE + BASE;

                SELECT LOTE_VENCE FROM LOTES WHERE LOTE_NRO = :lote AND ARTI_COD = :ARTICULO AND BODE_COD = :boditem INTO :vlote;

                if ((REFITEM <> '.t') or (REFITEM IS NULL)) then

                    BEGIN

                    /* NO SUME LOS ENSAMBLES */

                    if (IVAPORC = 0) then

                        SUMAEX = SUMAEX + (TOTAL - CONSUMO - INALC)*FACTENC;

                    if (TARIFA = 1) then

                        BEGIN

                        SUMA1 = SUMA1 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA1 = IVA1 + IVAITMONTO*FACTENC;

                        END

                    if (TARIFA = 2) then

                        BEGIN

                        SUMA2 = SUMA2 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA2 = IVA2 + IVAITMONTO*FACTENC;

                        END

                    if (TARIFA = 3) then

                        BEGIN

                        SUMA3 = SUMA3 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA3 = IVA3 + IVAITMONTO*FACTENC;

                        END

                    if (TARIFA = 4) then

                        BEGIN

                        SUMA4 = SUMA4 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA4 = IVA4 + IVAITMONTO*FACTENC;

                        END

                    if (TARIFA = 5) then

                        BEGIN

                        SUMA5 = SUMA5 + (TOTAL - IVAITMONTO - CONSUMO - INALC)*FACTENC;

                        IVA5 = IVA5 + IVAITMONTO*FACTENC;

                        END

                    SUMAGR = 0;

                    if (IVA1 <> 0) then

                        SUMAGR = SUMAGR + SUMA1;

                    if (IVA2 <> 0) then

                        SUMAGR = SUMAGR + SUMA2;

                    if (IVA3 <> 0) then

                        SUMAGR = SUMAGR + SUMA3;

                    if (IVA4 <> 0) then

                        SUMAGR = SUMAGR + SUMA4;

                    if (IVA5 <> 0) then

                        SUMAGR = SUMAGR + SUMA5;

                    END

                if (ANCHO IS NULL) then

                    ANCHO = 0;

                if (ALTO IS NULL) then

                    ALTO = 0;

                if (LARGO IS NULL) then

                    LARGO = 0;

                if (PESO IS NULL) then

                    PESO = 0;

                PESO = (PESO / 1000)* CANT * FACTOR;

                PESOTOT = PESOTOT + PESO;

                totcajas = totcajas + cajas;

                if (substr(ARTICULO,1,2) = '.t') then

                    BEGIN

                    REFITEM = '';

                    TOTAL = 0;

                    SUBTOTIT = 0;

                    END

                SELECT STAND_COD FROM STANDS WHERE ARTI_COD = :ARTICULO AND STAND_BODEGA = :boditem INTO :stand;

                factcant = NULL;

                SELECT FIRST 1 UNAR_FACCAN FROM unidad_articulo WHERE ARTI_COD = :articulo INTO :factcant;

                if ((factcant IS NULL) or (factcant = 0)) then

                    BEGIN

                    ALTERNATIVAS = 0;

                    PRINCIPALES = (CANT * FACTOR);

                    END

                ELSE

                    BEGIN

                    ALTERNATIVAS = FLOOR(CANT * factor / factcant);

                    PRINCIPALES = (CANT * FACTOR) - (alternativas * factcant);

                    END

                if ((MONEDAEX = 'S') AND (TRM <> 1)) then

                    BEGIN

                    PRUNIT = PRUNIT / TRM;

                    DTOITMONTO = DTOITMONTO / TRM;

                    PRNETO = PRNETO / TRM;

                    PRNETOSINIVA = PRNETOSINIVA / TRM;

                    BASE = base / TRM;

                    IVAITMONTO = IVAITMONTO / TRM;

                    CONSUMO = consumo / TRM;

                    INALC = INALC / TRM;

                    SUBTOTIT = SUBTOTIT / TRM;

                    TOTAL = TOTAL / TRM;

                    END

                SUSPEND;

                END

              END

        END

END

^



SET TERM ; ^





