unit ffunciones;

interface

uses sysutils,math,controls,dialogs;


type

  Mensaje = record
    tipo:smallint;
    // 0 - El producto no tiene codigo de barras.Esto sera para los antiguos pq los nuevos
    //     van a tener todos código de barras.
    // 1 - El producto tiene codigo de barras pero no ha sido impreso.
    // 2 - El producto tiene codigo de barras pero ha sido impreso.
    borrar:boolean;
  end;

  tfunciones = class

  private

    palmacendetalle,palmacendetalle2:boolean;
    SEan,SCodigo_barras,SId_Articulo,SDescripcion: string;
    SIva, SMarca,SId_Serie,SId_Empresa,SAnyo :string;
    SArt_Proveedor,SId_Proveedor:string;
    DPrecio,DPrecioCoste,Dpvpprov,Ddescuento:double;
    SiEsta:boolean;
    Inumero,Inumero1:integer;

    BFactura,Balbaran,Bticket,BEntrada:boolean;

    FCodigo_barras,FEan:boolean;
    SIIvaDefecto:smallint;

//    FUNCTION OBTENERPRECIOVENTA(aEAN:BOOLEAN;PRECIO,PRECIO2:STRING;PVP2,PRECIO_VENTA:DOUBLE):DOUBLE;
    function PedirConfirmacion(mensaje,mensaje2:string; si,no,cancelar:boolean):integer;    

    { Private declarations }
  public
    constructor Create ;
    Destructor  Destroy; override;
    function Esta(aux:string;AEAN:BOOLEAN;PRECIO,PRECIO2:STRING;id_serie:string;id_empresa:string;anyo:string):boolean;
    procedure Borrar;
    function TieneCB(pID_ENTRADA,pID_EMPRESA,pID_ARTICULO,pANYO,pLINEA,pCANTIDAD:string): mensaje;
    function TieneCBE(pID_ENTRADA,pID_EMPRESA,pANYO:string): boolean;    
    procedure ObtenerCB1(pID_ENTRADA,pID_EMPRESA,pID_ARTICULO,pANYO,pLINEA,pCANTIDAD:string);
//    function ObtenerDatosArticulo(aux:string;id_serie:string;id_empresa:string;anyo:string):boolean;
    { Public declarations }

    property Ean : string read SEan write SEan;
    property Codigo_barras : string read SCodigo_Barras write SCodigo_Barras;
    property Id_Articulo : string read SId_Articulo write SId_Articulo;
    property Descripcion : string read SDescripcion write SDescripcion;
    property Precio: double read DPrecio write DPrecio;
    property PrecioCoste: double read DPrecioCoste write DPrecioCoste;
    property PvpProv: double read DPvpProv write DPvpProv;
    property descuento: double read Ddescuento write Ddescuento;
    property Iva: string read SIva write SIva;
    property Marca: string read SMarca write SMarca;
    property Id_Serie: string read SId_Serie write SId_Serie;
    property Id_Empresa: string read SId_Empresa write SId_Empresa;
    property Anyo: string read SAnyo write SAnyo;
    property Albaran: Boolean read BAlbaran write BAlbaran;
    property Ticket: Boolean read BTicket write Bticket;
    property Factura: Boolean read BFactura write BFactura;
    property Entrada: Boolean read BEntrada write BEntrada;    
    property Art_Proveedor: string read SArt_Proveedor write SArt_Proveedor;
    property Id_Proveedor: string read SId_Proveedor write SId_Proveedor;
    property Numero: integer read INumero write INumero;
    property Numero1: integer read INumero1 write INumero1;

    property EsCodigo : boolean read FCodigo_barras write FCodigo_barras;
    property EsEan : boolean read FEan write FEan;
  end;


implementation

uses FALUMNOS, principal;

{ tfunciones }

procedure tfunciones.Borrar;
begin
    palmacendetalle:=false;palmacendetalle2:=false;
    SEan:='';SCodigo_barras:='';SId_Articulo:='';SDescripcion:='';
    SIva:='';SMarca:='';SId_Serie:='';SId_Empresa:='';SAnyo:='';
    DPrecio:=0;
    Dpvpprov:=0;Ddescuento:=0;    
    DPrecioCoste :=0;
    SiEsta:=false;
    FCodigo_barras:=false;FEan:=false;
    BAlbaran:=false;Bfactura:=false;BTicket:=false;
//    SIIvaDefecto := STRTOINT(FMENU.IVA);

    SArt_Proveedor:='';SId_Proveedor:='';

    Numero:=0;Numero1:=0;

end;

constructor tfunciones.Create;
begin
  inherited Create;
  Borrar;

end;

destructor tfunciones.Destroy;
begin
  inherited destroy;
end;

function tfunciones.Esta(aux: string;AEAN:BOOLEAN;PRECIO,PRECIO2:STRING;id_serie:string;id_empresa:string;anyo:string):boolean;
var lnumero:integer;
begin
  SiEsta := false;

  self.Id_Serie:= id_serie;
  self.Id_Empresa:= id_empresa;
  self.Anyo:= anyo;

  aux := uppercase(aux);
  IF POS('''',aux) <> 0 THEN
  BEGIN
  // AL LEER CON EL CODIGO DE BARRAS LA / LO INTERPRETA COMO -
  // Y LA - LA LEE COMO ' 
      aux := StringReplace(aux , '-', '/', [rfreplaceall]);
      aux := StringReplace(aux , '''', '-', [rfreplaceall]);
  END;

  // el primer select saco el articulo de proveedor y despues en los siguientes
  // comparo con codigo de barras y Ean. Al primer select le doy menos valor.
  {dmgs.cdsq_maximo.Close;

  IF bentrada then
       DMGS.CDSQ_MAXIMO.CommandText := 'select count(*) as cuenta,id_articulo from articulos where (art_proveedor='''+aux+''' or id_articulo='''+aux+''') and (id_proveedor='+id_proveedor+' or almacenable <> 1 ) and borrado<>1 group by id_articulo'
  else
       dmgs.CDSQ_MAXIMO.CommandText := 'select count(*) as cuenta,id_articulo from articulos where (art_proveedor='''+aux+''' or id_articulo='''+aux+''') and borrado<>1 group by id_articulo';

  dmgs.cdsq_maximo.open;
  if dmgs.cdsq_maximo.Fields[0].asinteger  > 0 then
  begin
     SiEsta := true;
     SEan := aux;
     SCodigo_Barras := '';
     SId_Articulo := dmgs.cdsq_maximo.Fields[1].asstring;
     FEan := true;
     FCodigo_Barras:= false;
     palmacendetalle := false;
     palmacendetalle2 := false;
  end;
  dmgs.cdsq_maximo.Close;


  dmgs.cdsq_maximo.Close;
  dmgs.CDSQ_MAXIMO.CommandText := 'select count(*) as cuenta,numero1,codigo_barras,numeroserie from almacendetalle2 where codigo_barras='''+aux+''' or numeroserie='''+aux+''' group by numero1,codigo_barras,numeroserie ';
  dmgs.cdsq_maximo.open;
  if dmgs.cdsq_maximo.Fields[0].asinteger > 0 then
  begin
     SiEsta := true;
     SEan := '';
     lnumero := dmgs.cdsq_maximo.Fields[1].asinteger;
     SCodigo_Barras := dmgs.cdsq_maximo.Fields[2].asstring;
     dmgs.cdsq_maximo.Close;
     dmgs.CDSQ_MAXIMO.CommandText := 'select id_articulo from almacendetalle where numero='+inttostr(lnumero)+' ';
     dmgs.cdsq_maximo.open;
     SId_Articulo := dmgs.cdsq_maximo.Fields[0].asstring;
//     SCodigo_Barras := aux;
     FEan := false;
     FCodigo_Barras:= true;
     palmacendetalle2 := true;
     palmacendetalle := false;
  end;

  dmgs.cdsq_maximo.Close;
  dmgs.CDSQ_MAXIMO.CommandText := 'select count(*) as cuenta,id_articulo from almacendetalle where codigo_barras='''+aux+''' group by id_articulo';
  dmgs.cdsq_maximo.open;
  if dmgs.cdsq_maximo.Fields[0].asinteger > 0 then
  begin
     SiEsta := true;
     SEan := '';
     SId_Articulo := dmgs.cdsq_maximo.Fields[1].asstring;
     SCodigo_Barras := aux;
     FEan := false;
     FCodigo_Barras:= true;
     palmacendetalle := true;
     palmacendetalle2 := false;
  end;
  dmgs.cdsq_maximo.Close;

  dmgs.cdsq_maximo.Close;
  dmgs.CDSQ_MAXIMO.CommandText := 'select count(*) as cuenta,id_articulo from ean where ean='''+aux+''' group by id_articulo';
  dmgs.cdsq_maximo.open;
  if dmgs.cdsq_maximo.Fields[0].asinteger  > 0 then
  begin
     SiEsta := true;
     SEan := aux;
     SCodigo_Barras := '';
     SId_Articulo := dmgs.cdsq_maximo.Fields[1].asstring;
     FEan := true;
     FCodigo_Barras:= false;
     palmacendetalle := false;
     palmacendetalle2 := false;
  end;
  dmgs.cdsq_maximo.Close;

  if SiEsta then   // tengo el ID_ARTICULO
  begin
    WITH DMGS.CDSQ_MAXIMO DO
    BEGIN
       CLOSE;
       IF ENTRADA THEN
          COMMANDTEXT := 'select DESCRIPCION,IVA,MARCA,PVP2,PRECIO_VENTA,Precio_Coste,art_proveedor,id_proveedor,pvpprov,descuento from ARTICULOS where ID_ARTICULO='''+ID_ARTICULO+''' AND (ID_PROVEEDOR='+ID_PROVEEDOR+' OR ALMACENABLE<>1) and borrado<>1 '
       ELSE
          COMMANDTEXT := 'select DESCRIPCION,IVA,MARCA,PVP2,PRECIO_VENTA,Precio_Coste,art_proveedor,id_proveedor,pvpprov,descuento from ARTICULOS where ID_ARTICULO='''+ID_ARTICULO+''' and borrado<>1 ';
       OPEN;                                                                                         //7           //8   //9
       BEGIN
          self.DESCRIPCION := Fields[0].AsString;

          if (Fields[1].AsString='0') or (Fields[1].isnull) then
               self.iva := inttostr(SIIvaDefecto)
          else
               self.iva := Fields[1].AsString;

          if self.Balbaran then
             self.PRECIO := OBTENERPRECIOVENTA(aean,precio,precio2,Fields[3].ASFLOAT,Fields[4].ASFLOAT); //ROUNDTO(DMGS.CDSQ_ARTICULOEANPRECIO_VENTA.ASfloat,DMGS.DECIMALES);
          if self.BFactura  then
             self.PRECIO := ROUNDTO(Fields[4].asfloat/(1+strtoint(self.iva)/100),DMGS.DECIMALES);
          if self.BTicket  then
             self.PRECIO := ROUNDTO(Fields[4].asfloat,DMGS.DECIMALES);




          if self.BEntrada  then
             if (DMGS.CDSQ_ARTICULOSAUXPVPPROV.ASFLOAT > 0.0001 ) THEN // LO PONGO ASI PARA
             //EVITAR QUE NO TOME EL 0 JUSTO.
             BEGIN
                self.PRECIO  := ROUNDTO(Fields[8].asfloat,DMGS.DECIMALES);
                self.DESCUENTO  := ROUNdTO(Fields[9].asfloat,DMGS.DECIMALES);
             END
             ELSE
                self.PRECIO  := ROUNdTO(Fields[5].asfloat,DMGS.DECIMALES);



          self.MARCA := Fields[2].AsString;
          self.PrecioCoste := Fields[5].asfloat;
          Art_Proveedor := Fields[6].asstring;
          Id_Proveedor := Fields[7].asstring;
          SiEsta := true;
       END;
       CLOSE;
    END;
  end
  else   // if SiEsta then   NO TENGO EL ID_ARTICULO
  begin
    WITH DMGS.CDSQ_ARTICULOEAN DO
    BEGIN
       CLOSE;
       DMGS.AREBGRID  := aux;

       if entrada then
          DMGS.EAID := ID_PROVEEDOR
       ELSE
          DMGS.EAID := '';

       OPEN;
       IF ((DMGS.CDSQ_ARTICULOEAN.RECORDCOUNT > 0) and
       (dmgs.cdsq_articuloeanborrado.asstring <> '1')) THEN
       BEGIN
          self.ID_ARTICULO :=DMGS.CDSQ_ARTICULOEANID_ARTICULO.ASSTRING;
          self.DESCRIPCION := DMGS.CDSQ_ARTICULOEANDESCRIPCION.AsString;

          if (DMGS.CDSQ_ARTICULOEANIVA.AsString='0') or (DMGS.CDSQ_ARTICULOEANIVA.isnull) then
               self.iva := inttostr(SIIvaDefecto)
          else
               self.iva := DMGS.CDSQ_ARTICULOEANIVA.AsString;

          if self.Balbaran then
             self.PRECIO := OBTENERPRECIOVENTA(aean,precio,precio2,DMGS.CDSQ_ARTICULOEANPVP2.AsFloat,DMGS.CDSQ_ARTICULOEANPRECIO_VENTA.AsFloat); //ROUNDTO(DMGS.CDSQ_ARTICULOEANPRECIO_VENTA.ASfloat,DMGS.DECIMALES);
          if self.BFactura then
             self.PRECIO := ROUNDTO(DMGS.CDSQ_ARTICULOEANPRECIO_VENTA.ASFLOAT/(1+strtoint(self.iva)/100),DMGS.DECIMALES);
          if self.BTicket  then
             self.PRECIO := ROUNDTO(DMGS.CDSQ_ARTICULOEANPRECIO_VENTA.ASFLOAT,DMGS.DECIMALES);




           if self.BEntrada  then
             if (DMGS.CDSQ_ARTICULOSAUXPVPPROV.ASFLOAT > 0.0001 ) THEN // LO PONGO ASI PARA
             //EVITAR QUE NO TOME EL 0 JUSTO.
             BEGIN
                self.PRECIO  := ROUNdTO(DMGS.CDSQ_ARTICULOEANPVPPROV.ASFLOAT,DMGS.DECIMALES);
                self.DESCUENTO  := ROUNdTO(DMGS.CDSQ_ARTICULOEANDESCUENTO.ASFLOAT,DMGS.DECIMALES);
             END
             ELSE
                self.PRECIO  := ROUNdTO(DMGS.CDSQ_ARTICULOEANPRECIO_COSTE.ASFLOAT,DMGS.DECIMALES);


          self.MARCA := DMGS.CDSQ_ARTICULOEANMARCA.AsString;
          self.PrecioCoste := dmgs.cdsq_articuloeanprecio_coste.asfloat;
          Art_Proveedor := dmgs.cdsq_articuloeanart_proveedor.asstring;
          Id_Proveedor := dmgs.cdsq_articuloeanid_proveedor.asstring;
          SiEsta := true;
       END;
       CLOSE;
    END;
  end;

  result := SiEsta;  }


end;


procedure tfunciones.ObtenerCB1(pID_ENTRADA, pID_EMPRESA, pID_ARTICULO,
  pANYO, pLINEA, pCANTIDAD: string);
begin
{   dmgs.cdsq_maximo.Close;
   dmgs.CDSQ_MAXIMO.CommandText := 'select count(*) as cuenta,numero,codigo_barras from almacendetalle '+
        ' where ID_ENTRADA='+PID_ENTRADA+' AND ID_EMPRESA='+PID_EMPRESA+' AND ID_ARTICULO='''+
        PID_ARTICULO+''' AND ANYO='+PANYO+' AND LINEA='+PLINEA+' AND CANTIDAD='+PCANTIDAD+' group by numero,codigo_barras ';
   dmgs.cdsq_maximo.open;
   if dmgs.cdsq_maximo.Fields[0].asinteger > 0 then
   begin
     Codigo_barras := dmgs.cdsq_maximo.Fields[2].asstring;
     Numero:= dmgs.cdsq_maximo.Fields[1].ASinteger;
   end;
   dmgs.cdsq_maximo.Close;
end;

function tfunciones.OBTENERPRECIOVENTA(aEAN: BOOLEAN; PRECIO,
  PRECIO2: STRING;PVP2,PRECIO_VENTA:DOUBLE): DOUBLE;
VAR AUX:DOUBLE;
begin

   DMGS.CDSQ_FLOAT.Close;
   DMGS.CDSQ_FLOAT.CommandText := ' SELECT PRECIO FROM SERIES WHERE ID_SERIE='''+ID_SERIE+''' AND ID_EMPRESA='+ID_EMPRESA+' AND ANYO='+ANYO ;
   DMGS.CDSQ_FLOAT.Open;

   IF FMENU.ALBSINIVA='1' THEN
   BEGIN
     IF DMGS.CDSQ_FLOAT.Fields [0].AsString ='PVP2' THEN
       IF aEAN THEN
        AUX := ROUNDTO(PVP2/(1+strtofloat(IVA)/100),DMGS.DECIMALES)
       ELSE
        AUX := ROUNDTO(STRTOFLOAT(PRECIO2)/(1+strtofloat(IVA)/100),DMGS.DECIMALES)
     ELSE
       IF aEAN THEN
           AUX := ROUNDTO(PRECIO_VENTA/(1+strtofloat(IVA)/100),DMGS.DECIMALES)
       ELSE
           AUX := ROUNDTO(STRTOFLOAT(PRECIO)/(1+1+strtofloat(IVA)/100),DMGS.DECIMALES)
   END
   ELSE
   BEGIN
     IF DMGS.CDSQ_FLOAT.Fields [0].AsString ='PVP2' THEN
       IF aEAN THEN
         AUX := ROUNDTO(PVP2,DMGS.DECIMALES)
       ELSE
         AUX := STRTOFLOAT(PRECIO2)
     ELSE
       IF aEAN THEN
           AUX := ROUNDTO(PRECIO_VENTA,DMGS.DECIMALES)
       ELSE
           AUX := STRTOFLOAT(PRECIO);
   END;

   DMGS.CDSQ_FLOAT.Close;

   RESULT := AUX;}
end;

function tfunciones.PedirConfirmacion(mensaje,mensaje2: string; si, no,
  cancelar: boolean): integer;
begin
{    confirmara:=tconfirmara.create(nil);
    with confirmara do
    begin
        LMensaje.caption:=mensaje;
        LMensaje1.caption:=mensaje2;
        bsi.visible:=si;
        bno.visible:=no;
        bcancelar.visible:=cancelar;
    end;
    result:=Confirmara.Showmodal;
    confirmara.free}
end;

function tfunciones.TieneCB(pID_ENTRADA,pID_EMPRESA,pID_ARTICULO,pANYO,pLINEA,pCANTIDAD:string): mensaje;
var x:integer;
    aux:mensaje;
begin
{
   aux.tipo := 0;
   aux.borrar := false;
   dmgs.cdsq_maximo.Close;
   dmgs.CDSQ_MAXIMO.CommandText := 'select count(*) as cuenta,id_articulo,impreso from almacendetalle '+
        ' where ID_ENTRADA='+PID_ENTRADA+' AND ID_EMPRESA='+PID_EMPRESA+' AND ID_ARTICULO='''+
        PID_ARTICULO+''' AND ANYO='+PANYO+' AND LINEA='+PLINEA+' AND CANTIDAD='+PCANTIDAD+' group by id_articulo,impreso ';
   dmgs.cdsq_maximo.open;
   if dmgs.cdsq_maximo.Fields[0].asinteger > 0 then
   begin
    if dmgs.cdsq_maximo.Fields[2].asinteger > 0 then
    begin
      aux.tipo := 2;
      x:=PedirConfirmacion('De esta entrada ya tiene impreso código de barras.','¿Confirma que desea borrar todos los códigos de barras?',true,true,true);
      case x of
         mrno: aux.borrar := false;
         mrcancel: aux.borrar := false;
         mryes: aux.borrar := true;
     end;
    end
    else
    begin
       aux.tipo := 1;
       aux.borrar := true;
    end;
   end
   else
   begin
     aux.tipo := 0;
     aux.borrar := true;
   end;
   dmgs.cdsq_maximo.Close;
   result := aux;}
end;

function tfunciones.TieneCBE(pID_ENTRADA, pID_EMPRESA,
  pANYO: string): boolean;
  var aux:boolean;
begin
{
   aux:= false;
   dmgs.cdsq_maximo.Close;
   dmgs.CDSQ_MAXIMO.CommandText := 'select count(*) as cuenta,impreso,numero from almacendetalle '+
        ' where ID_ENTRADA='+PID_ENTRADA+' AND ID_EMPRESA='+PID_EMPRESA+' AND ANYO='+PANYO+' and impreso=1 group by impreso,numero ';
   dmgs.cdsq_maximo.open;
   if dmgs.cdsq_maximo.Fields[0].asinteger > 0 then
    if dmgs.cdsq_maximo.Fields[1].asinteger > 0 then
     begin
       showmessage ('Esta entrada hay lineas con codigos de barras impresos. Borre línea a línea.');
       aux:= true;
     end
     else
     begin
         dmgs.cdsq_float.Close;
         dmgs.cdsq_float.CommandText := 'select count(*) as cuenta,impreso from almacendetalle2 '+
          ' where numero1='+dmgs.cdsq_maximo.Fields[2].AsString +' AND impreso=1 group by impreso ';
         dmgs.cdsq_float.open;
         if dmgs.cdsq_maximo.Fields[1].asinteger > 0 then
         begin
            showmessage ('Esta entrada hay lineas con codigos de barras impresos. Borre línea a línea.');
            aux:= true;
         end
     end;
   dmgs.cdsq_maximo.Close;
   dmgs.cdsq_float.close;
   result := aux;}
end;

end.
