unit fselectpedidos;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Grids, Vcl.DBGrids,
  Vcl.ExtCtrls,falbaranes, Data.DB, ZAbstractRODataset, ZAbstractDataset,
  ZDataset;

type
  Tseleccionarpedidos = class(TForm)
    panelpedidos: TPanel;
    Panel6: TPanel;
    DBGrid2: TDBGrid;
    Panel5: TPanel;
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    DBGrid3: TDBGrid;
    Panel4: TPanel;
    DBGrid4: TDBGrid;
    Panel9: TPanel;
    btALnuevo: TButton;
    Button1: TButton;
    Panel7: TPanel;
    qaux: TZQuery;
    qidspedidodetalle: TZQuery;
    insertarpedidodetalle: TZQuery;
    actualizarids: TZQuery;
    qidspedidodetalleidea: TIntegerField;
    qidspedidodetallebultos: TIntegerField;
    qidspedidodetallelote: TIntegerField;
    procedure btALnuevoClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure DBGrid4DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  seleccionarpedidos: Tseleccionarpedidos;
const
  rojo=$00AAAAFF;
  amarillo=$00B9FFFF;
  verde=$00C7FBAA;
  azul=$00FFFFBB;
implementation

{$R *.dfm}

procedure Tseleccionarpedidos.btALnuevoClick(Sender: TObject);
var iva,num_albaran,idalbaran,serie,empresa,id,idpedido,desclinalbaran,idiva:string;
    pesobruto,pesoneto,pesonetoteorico,numpalets,precio:string;
    cajas,idpesada:integer;
begin
    id:=albaranes.qpedidosdetalleid.asstring;
    idpedido:=albaranes.qpedidospedidocliente.asstring;
    serie:=albaranes.albaranesserie.AsString;
    idalbaran:=albaranes.albaranesid.asstring;
    num_albaran:=serie+'/'+idalbaran;
    empresa:=albaranes.albaranesidempresa.AsString;
    precio:=albaranes.qpedidosdetalleprecio.asstring;
    desclinalbaran:=albaranes.qpedidosdetalledesc_producto.AsString+' '+albaranes.qpedidosdetalledesc_calidad.AsString+' '+albaranes.qpedidosdetalledesc_envase.AsString+' '+albaranes.qpedidosdetalledesc_acabado.AsString;
    qaux.sql.clear;
    qaux.sql.add('select exento_iva from clientes where id='+albaranes.albaranesidcliente.AsString);
    qaux.Open;
    idiva:=qaux.Fields[0].AsString;
    qaux.sql.clear;
    qaux.sql.add('select tipo from tipos_iva where id='+idiva);
    qaux.Open;
    iva:=''''+qaux.fields[0].asstring+'''';
    albaranes.qpedidosdetalle.edit;
    albaranes.qpedidosdetalleidalbaran.value:=num_albaran;//albaranes.albaranesserie.asstring+'/'+albaranes.albaranesid.asstring;
    albaranes.qpedidosdetalle.post;
    albaranes.qpedidos.Refresh;
    albaranes.qpedidos.Locate('pedidocliente',vararrayof([idpedido]),[locaseinsensitive]);
    albaranes.qpedidos2.Refresh;
    albaranes.qpedidos2.Locate('pedidocliente',vararrayof([idpedido]),[locaseinsensitive]);
    albaranes.qpedidosdetalle.Refresh;
    albaranes.qpedidosdetalle2.Refresh;
    albaranes.albaranes.refresh;
    albaranes.albaranes.Locate('serie,id',vararrayof([serie,idalbaran]),[locaseinsensitive]);

    qidspedidodetalle.close;
    qidspedidodetalle.Prepare;
    qidspedidodetalle.ParamByName('empresa').Asstring:=empresa;
    qidspedidodetalle.ParamByName('serie').AsString:= serie;
    qidspedidodetalle.ParamByName('albaran').AsInteger:= strtoint(idalbaran);
    qidspedidodetalle.ParamByName('desclinalbaran').AsString:= desclinalbaran;
    qidspedidodetalle.ParamByName('idpedidodetalle').AsInteger:= strtoint(id);
    qidspedidodetalle.open;

{    qaux.sql.clear;
    qaux.sql.add(' (SELECT '+''''+empresa+''''+' as idempresa,'+''''+serie+''''+' as serie,'+''''+idalbaran+''''+' as idalbaran,lp.idea,'+''''+desclinalbaran+''''+' as descripcion,lp.desc_calibre as cal,lp.lote,cajas as bultos,pesoneto_teorico as cantidad,');
    qaux.sql.add(''''+'0'+''''+' as precio,'+''''+'0'+''''+' as descuento,'+''''+'0'+''''+' as totaldescuento,'+''''+'0'+''''+' as base,'+iva+' as tipoiva,'+''''+'0'+''''+' as iva,'+''''+'0'+''''+' as total');
    qaux.sql.add(' FROM lotes_pesadas lp,materias_primas mp where lp.mprima=mp.id and (lp.idea,lp.idea2) in  (select id,idea from ea_pesadas where (idea=0 or iddestino in (select id from almacenes where final=1)) and idpedido in (select id from pedidosdetalle');
    qaux.sql.add(' where idalbaran='+''''+num_albaran+''''+' and ide='+empresa+' and id='+id+')))');
    qaux.open;}
    if qidspedidodetalle.RecordCount>0 then
    begin
      idpesada:=0;
      while not qidspedidodetalle.Eof do
      begin
        if idpesada<>qidspedidodetalleidea.asinteger then
        begin
          idpesada:=qidspedidodetalleidea.asinteger;
          cajas:=qidspedidodetallebultos.asinteger;
        end else
        begin
          idpesada:=qidspedidodetalleidea.asinteger;
          cajas:=0;
        end;
        insertarpedidodetalle.Close;
        insertarpedidodetalle.Prepare;
        insertarpedidodetalle.ParamByName('empresa').AsInteger:=strtoint(empresa);
        insertarpedidodetalle.ParamByName('serie').AsString:=serie;
        insertarpedidodetalle.ParamByName('albaran').AsInteger:=strtoint(idalbaran);
        insertarpedidodetalle.ParamByName('desclinalbaran').AsString:=desclinalbaran;
        insertarpedidodetalle.ParamByName('cajas').AsInteger:=cajas;
        insertarpedidodetalle.ParamByName('precio').AsFloat:=strtofloat(precio);
        insertarpedidodetalle.ParamByName('idpesada').AsInteger:=idpesada;
        insertarpedidodetalle.ParamByName('lote').Asstring:=qidspedidodetallelote.asstring;
        insertarpedidodetalle.ParamByName('idpedido').AsInteger:=strtoint(id);
        insertarpedidodetalle.execsql;
        qidspedidodetalle.Next;
      end;

{        qaux.sql.clear;
        qaux.sql.add('insert into albaranes_detalles (idempresa,serie,idalbaran,idarticulo,descripcion,cal,lote,bultos,cantidad,precio,descuento,totaldescuento,base,tipoiva,iva,total)');
        qaux.sql.add(' (SELECT '+''''+empresa+''''+' as idempresa,'+''''+serie+''''+' as serie,'+''''+idalbaran+''''+' as idalbaran,lp.idea,'+''''+desclinalbaran+''''+' as descripcion,lp.desc_calibre as cal,lp.lote,cajas as bultos,pesoneto_teorico as cantidad,');
        qaux.sql.add(''''+precio+''''+' as precio,'+''''+'0'+''''+' as descuento,'+''''+'0'+''''+' as totaldescuento,'+''''+'0'+''''+' as base,'+iva+' as tipoiva,'+''''+'0'+''''+' as iva,'+''''+'0'+''''+' as total');
        qaux.sql.add(' FROM lotes_pesadas lp,materias_primas mp where lp.mprima=mp.id and (lp.idea,lp.idea2) in  (select id,idea from ea_pesadas where (idea=0 or iddestino in (select id from almacenes where final=1)) and idpedido in (select id from pedidosdetalle');
        qaux.sql.add(' where idalbaran='+''''+num_albaran+''''+' and ide='+empresa+' and id='+id+')))');
        qaux.execsql;  }

      albaranes.albaranesdetalle.Refresh;
      actualizarids.close;
      actualizarids.Prepare;
      actualizarids.ParamByName('serie').AsString:=serie;
      actualizarids.ParamByName('albaran').Asinteger:=strtoint(idalbaran);
      actualizarids.ParamByName('idpedidodetalle').Asinteger:=strtoint(id);
      actualizarids.ParamByName('empresa').Asinteger:=strtoint(empresa);
      actualizarids.ExecSQL;
      {
      qaux.close;
      qaux.SQL.Clear;
      qaux.SQL.Add('update ea_pesadas set id_palet2='+''''+'FACTURANDO'+''''+',idalbaran='+''''+num_albaran+''''+' where idpedido='+id+' and idempresa='+empresa);
      qaux.ExecSQL;}
      qaux.SQL.Clear;
      qaux.SQL.Add('select sum(peso_bruto-tara0),sum(peso_neto),sum(peso_neto_teorico),sum(num_palets) from  ea_pesadas where idalbaran='+''''+num_albaran+''''+' and idempresa='+albaranes.albaranesidempresa.asstring);
      qaux.open;
      pesobruto:=qaux.fields[0].asstring;
      pesobruto:=stringreplace(pesobruto,',','.',[rfreplaceall]);
      pesoneto:=qaux.fields[1].asstring;
      pesoneto:=stringreplace(pesoneto,',','.',[rfreplaceall]);
      pesonetoteorico:=qaux.fields[2].asstring;
      pesonetoteorico:=stringreplace(pesonetoteorico,',','.',[rfreplaceall]);
      numpalets:=qaux.fields[3].asstring;
      numpalets:=stringreplace(numpalets,',','.',[rfreplaceall]);
      qaux.SQL.Clear;
      qaux.SQL.Add('update albaranes set peso_bruto='+pesobruto+',kilos=+'+pesoneto+',kilosventa='+pesonetoteorico+',numpalets=+'+numpalets+' where serie='+''''+serie+''''+' and id='+idalbaran+' and idempresa='+albaranes.albaranesidempresa.AsString);
      qaux.execsql;

      albaranes.RecalcularTotalesAlbaran(albaranes.albaranesidempresa.AsInteger, serie, StrToInt(idalbaran));
      albaranes.albaranes.Refresh;
      albaranes.albaranes.Locate('serie,id', VarArrayOf([serie, idalbaran]), [loCaseInsensitive]);
      albaranes.qalbaranesiva.Refresh;

    end else showmessage('NO HAY ID ASOCIADO A LA LINEA DE PEDIDO');
end;

procedure Tseleccionarpedidos.Button1Click(Sender: TObject);
var serie,empresa,idalbaran,idalbaran2,id,num_albaran:string;
    pesobruto,pesoneto,pesonetoteorico,numpalets:string;
begin
    id:=albaranes.qpedidosdetalle2id.asstring;
    idalbaran:=albaranes.qpedidosdetalle2idalbaran.asstring;
    num_albaran:=idalbaran;
    idalbaran2:=albaranes.albaranesserie.AsString+'/'+albaranes.albaranesid.AsString;
    empresa:=albaranes.qpedidosdetalle2ide.Asstring;

    qaux.sql.clear;
    qaux.sql.add('delete from albaranes_detalles where idarticulo in ');
    qaux.sql.add(' (select id from ea_pesadas where idpedido in (select id from pedidosdetalle where idalbaran='+''''+idalbaran+''''+' and ide='+empresa+' and id='+id+'))');
//    qaux.sql.add(' (SELECT idea FROM lotes_pesadas lp,materias_primas mp where lp.mprima=mp.id and lp.idea in  (select id from ea_pesadas where idpedido in (select id from pedidosdetalle where idalbaran='+''''+idalbaran+''''+' and ide='+empresa+' and id='+id+')))');

    qaux.execsql;

    qaux.SQL.Clear;
    qaux.SQL.Add('update pedidosdetalle set idalbaran=null where  ide='+empresa+' and id='+id);
    qaux.ExecSQL;
    qaux.close;
    qaux.SQL.Clear;
    qaux.SQL.Add('update ea_pesadas set id_palet2='+''''+'TRIGGUER_OFF'+''''+',idalbaran=null where idpedido='+id+' and idempresa='+empresa);
    qaux.ExecSQL;
    albaranes.qpedidos2.Close;
    albaranes.qpedidos2.SQL.Clear;
    albaranes.qpedidos2.SQL.Add('select p.id,p.ide,p.idcliente,p.iddestino,p.fecha_pedido,p.fecha_entrega,c.nombre_corto as cliente,d.descripcion as destino,p.pedidocliente from pedidos p,clientes c,clientes_destinos d where');
    albaranes.qpedidos2.SQL.Add(' p.ide=c.idempresa and p.idcliente=c.id and p.ide=d.idempresa and p.idcliente=d.idcliente and p.iddestino=d.codigo and');
    albaranes.qpedidos2.SQL.Add(' p.id in (select pd.idpedido from pedidosdetalle pd where pd.idalbaran='+''''+albaranes.albaranesserie.AsString+'/'+albaranes.albaranesid.AsString+''''+')');
    albaranes.qpedidos2.Open;

    albaranes.qpedidosdetalle2.close;
    albaranes.qpedidosdetalle2.SQL.Clear;
    albaranes.qpedidosdetalle2.SQL.Add('select * from pedidosdetalle where idalbaran='+''''+albaranes.albaranesserie.asstring+'/'+albaranes.albaranesid.AsString+'''');
    albaranes.qpedidosdetalle2.Open;

    albaranes.qpedidosdetalle.Refresh;
    albaranes.qpedidos.Refresh;
    albaranes.albaranesdetalle.Refresh;
      qaux.SQL.Clear;
      qaux.SQL.Add('select sum(peso_bruto-tara0),sum(peso_neto),sum(peso_neto_teorico),sum(num_palets) from  ea_pesadas where idalbaran='+''''+idalbaran+''''+' and idempresa='+albaranes.albaranesidempresa.asstring);
      qaux.open;
      pesobruto:=qaux.fields[0].asstring;
      pesobruto:=stringreplace(pesobruto,',','.',[rfreplaceall]);
      if pesobruto='' then pesobruto:='0';
      pesoneto:=qaux.fields[1].asstring;
      pesoneto:=stringreplace(pesoneto,',','.',[rfreplaceall]);
      if pesoneto='' then pesoneto:='0';
      pesonetoteorico:=qaux.fields[2].asstring;
      pesonetoteorico:=stringreplace(pesonetoteorico,',','.',[rfreplaceall]);
      if pesonetoteorico='' then pesonetoteorico:='0';
      numpalets:=qaux.fields[3].asstring;
      numpalets:=stringreplace(numpalets,',','.',[rfreplaceall]);
      if numpalets='' then numpalets:='0';
      qaux.SQL.Clear;
      qaux.SQL.Add('update albaranes set peso_bruto='+pesobruto+',kilos=+'+pesoneto+',kilosventa='+pesonetoteorico+',numpalets=+'+numpalets+' where serie='+''''+albaranes.albaranesserie.asstring+''''+' and id='+albaranes.albaranesid.asstring+' and idempresa='+albaranes.albaranesidempresa.AsString);
      qaux.execsql;

    serie:=albaranes.albaranesserie.AsString;
    idalbaran:=albaranes.albaranesid.AsString;
    albaranes.RecalcularTotalesAlbaran(albaranes.albaranesidempresa.AsInteger, serie, StrToInt(idalbaran));
    albaranes.albaranes.refresh;
    albaranes.albaranes.Locate('serie,id',vararrayof([serie,idalbaran]),[locaseinsensitive]);
    albaranes.qalbaranesiva.Refresh;

end;

procedure Tseleccionarpedidos.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
    if albaranes.qpedidosdetallecantidadservida.Asinteger>albaranes.qpedidosdetallecantidad.Asinteger then
    begin
        dbgrid1.Canvas.brush.Color := rojo;
    end;
    if albaranes.qpedidosdetallecantidadservida.Asinteger=albaranes.qpedidosdetallecantidad.Asinteger then
    begin
        dbgrid1.Canvas.brush.Color := verde;
    end;

    dbgrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

procedure Tseleccionarpedidos.DBGrid4DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
    if albaranes.qpedidosdetalle2cantidadservida.Asinteger>albaranes.qpedidosdetalle2cantidad.Asinteger then
    begin
        dbgrid4.Canvas.brush.Color := rojo;
    end;
    if albaranes.qpedidosdetalle2cantidadservida.Asinteger=albaranes.qpedidosdetalle2cantidad.Asinteger then
    begin
        dbgrid4.Canvas.brush.Color := verde;
    end;
    dbgrid4.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

end.
