CREATE OR REPLACE PACKAGE VariosPed IS

   PROCEDURE CalculaTotales(Precio in number, cantidad in number, porcdescuento in number,
                             PorcImpArt in number, PorcImpCli in number,
                             TotalLinea out number, Impuesto out number);

   FUNCTION CalculaBod(Modo in varchar2, DepCaja in varchar2,
                      DepArt in varchar2) RETURN varchar2;

   PROCEDURE DatosDep(Vcia in char, Vdepto in char,
                      Vbod out char, Vbloq out char);

   FUNCTION Reserva(cia IN char, tipinv IN char, articulo IN varchar2, 
                    bodega IN varchar2, lote IN varchar2, FactorMed in number,
                    CanPed in number, PresAnt in number) RETURN number;

   FUNCTION existencias(Cia Char, Tipoinv in char, Bodega Varchar2, Articulo Varchar2,
                     cons in char, Lote Varchar2 default null) Return number;
   FUNCTION ControlExistencias(Vtipo in char) RETURN boolean;
END;
/


CREATE OR REPLACE PACKAGE BODY VariosPed IS

   PROCEDURE CalculaTotales(Precio in number, cantidad in number, porcdescuento in number,
                            PorcImpArt in number, PorcImpCli in number,
                            TotalLinea out number, Impuesto out number) IS
      SubTotal  number;
      VImpLista number;
   BEGIN
      Subtotal:=precio*cantidad-Dsc.Redondeo(Precio*porcDescuento/100, cantidad);
      if pre_par.kprecioivi='S' then
         VImpLista:=Round(SubTotal*(1-1/(1+PorcImpArt/100)), 2);
         Impuesto:=Round(VImpLista*PorcImpCli/100, 2);
      else
         VImpLista:=0;
         Impuesto:=Round(Subtotal*PorcImpart*PorcImpCli/10000, 2);
      end if;
      Totallinea:=Round(Subtotal-VImpLista+Impuesto, 2);
   END CalculaTotales;

   FUNCTION CalculaBod(Modo in varchar2, DepCaja in varchar2,
                      DepArt in varchar2) RETURN varchar2 IS
   BEGIN
      if modo='CAJ' then
         return nvl(DepCaja, DepArt);
      elsif modo='ART' then
         return nvl(DepArt, DepCaja);
      elsif DepCaja=nvl(DepArt,DepCaja) then
         return nvl(DepArt,DepCaja);
      else
         return null;
      end if;
   END CalculaBod;

   PROCEDURE DatosDep(Vcia in char, Vdepto in char,
                      Vbod out char, Vbloq out char) IS
   BEGIN
      select nvl(bodega, Vdepto), blqprecios
         into Vbod, Vbloq
      from inv_deptos
      where cod_cia=Vcia
        and cod_depto=Vdepto;
   exception
      when no_data_found then
         Vbloq:='N';
   END DatosDep;

   FUNCTION Reserva(cia IN char, tipinv IN char, articulo IN varchar2, 
                    bodega IN varchar2, lote IN varchar2, FactorMed in number,
                    CanPed in number, PresAnt in number) RETURN number IS
      VCanExi    vendetpedido.reservado%type:=0;
   BEGIN
      VCanExi:=Greatest(Existencias(cia, tipinv, bodega, articulo, 'N', lote),0);
      return least(nvl(PresAnt,0)+VCanExi/FactorMed, nvl(CanPed,0));
   END Reserva;

   FUNCTION existencias(Cia Char, Tipoinv in char, Bodega Varchar2, Articulo Varchar2,
                     cons in char, Lote Varchar2 default null) Return number IS
   Vcant Inv_lote_Artic_bodega.can_existe%type:=0;
   BEGIN 
     if ControlExistencias(tipoInv) then
	    select Sum(decode(Cons,'N',Can_existe-Can_apartado, 
	                           'A',Can_Apartado,
	                           'E',Can_existe,
	                            Can_existe-Can_apartado))
         inTo Vcant
      from   Inv_Lote_Artic_Bodega
      where  Cod_Cia      = cia
      and    Cod_Bodega   = bodega
      and    Cod_Articulo = articulo
      and    (Cod_lote    = lote or lote is null);
     end if;
     return nvl(Vcant, 0);
  END;

  FUNCTION ControlExistencias(Vtipo in char) RETURN boolean IS
     vctl inv_tipinv.ctlexistencia%type;
  BEGIN
    select ctlexistencia 
    into Vctl
    from inv_tipinv
    where tipo_inv=Vtipo;
   return vctl='S';
  EXCEPTION
   when no_data_found then
     return null;
  END;
   
END;
/


