unit fentradas; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.DBCtrls, Vcl.Mask, Vcl.ComCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls, datos, ZAbstractDataset, ZDataset, Data.DB, ZAbstractRODataset, ZAbstractTable; type Tentradas = class(TForm) Panel1: TPanel; Panel2: TPanel; Panel5: TPanel; Label1: TLabel; tbuscar: TEdit; DBGrid1: TDBGrid; Panel3: TPanel; Panel4: TPanel; DatosProveedores: TPageControl; TDatosClientes: TTabSheet; Panel14: TPanel; Label2: TLabel; Label4: TLabel; DBEdit3: TDBEdit; Panel6: TPanel; Label37: TLabel; BtEcancelar: TButton; btEmodificar: TButton; BtEborrar: TButton; BtENuevo: TButton; BtEguardar: TButton; qentradas: TZReadOnlyQuery; entradas: TZQuery; entradas_detalle: TZQuery; entradas_detalleid: TIntegerField; entradas_detalleidempresa: TIntegerField; entradas_detalleidentrada: TIntegerField; entradas_detallecodigo: TWideStringField; entradas_detalleid_propio: TWideStringField; entradas_detalledescripcion: TWideStringField; entradas_detallecantidad: TFloatField; entradas_detallestock: TFloatField; entradas_detalleprecio: TFloatField; DBLookupComboBox1: TDBLookupComboBox; dsentradas: TDataSource; dsqentradas: TDataSource; qaux: TZReadOnlyQuery; DBEdit2: TDBEdit; DBText3: TDBText; DBText2: TDBText; DBText1: TDBText; qproveedores: TZReadOnlyQuery; qproveedoresIdempresa: TIntegerField; qproveedoresnombre: TWideStringField; qproveedoresid: TIntegerField; dsqproveedores: TDataSource; dsentradasdetalle: TDataSource; Label3: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; DBGrid2: TDBGrid; entradas_detallelote: TWideStringField; entradas_detalletipo_iva: TIntegerField; entradas_detallebase: TFloatField; entradas_detalletotal: TFloatField; entradasid: TIntegerField; entradasidempresa: TIntegerField; entradasfecha: TDateField; entradasidproveedor: TIntegerField; entradasdocumento: TWideStringField; entradasbase: TFloatField; entradastotaliva: TFloatField; entradastotalentrada: TFloatField; qentradasid: TIntegerField; qentradasidempresa: TIntegerField; qentradasfecha: TDateField; qentradasidproveedor: TIntegerField; qentradasdocumento: TWideStringField; qentradasbase: TFloatField; qentradastotaliva: TFloatField; qentradastotalentrada: TFloatField; qentradasnomproveedor: TStringField; DBLookupComboBox2: TDBLookupComboBox; qarticulospropios: TZReadOnlyQuery; qarticulospropioscodigo: TWideStringField; qarticulospropiosdescripcion: TWideStringField; dsarticulospropios: TDataSource; qarticulospropiosidempresa: TIntegerField; DBEdit1: TDBEdit; Panel7: TPanel; Button1: TButton; entradas_detalleidproveedor: TIntegerField; Button2: TButton; procedure qentradasCalcFields(DataSet: TDataSet); procedure FormShow(Sender: TObject); procedure BtENuevoClick(Sender: TObject); procedure BtEborrarClick(Sender: TObject); procedure btEmodificarClick(Sender: TObject); procedure BtEguardarClick(Sender: TObject); procedure BtEcancelarClick(Sender: TObject); procedure dsentradasStateChange(Sender: TObject); procedure FormCreate(Sender: TObject); procedure DBGrid2KeyPress(Sender: TObject; var Key: Char); procedure DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); procedure DBGrid2ColExit(Sender: TObject); procedure entradas_detalleBeforePost(DataSet: TDataSet); procedure Button1Click(Sender: TObject); procedure FormKeyPress(Sender: TObject; var Key: Char); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure tbuscarChange(Sender: TObject); procedure qentradasBeforeOpen(DataSet: TDataSet); procedure DBGrid2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure DBGrid1TitleClick(Column: TColumn); procedure Button2Click(Sender: TObject); procedure entradas_detalleAfterPost(DataSet: TDataSet); private { Private declarations } public { Public declarations } var filtro:string; idpropio:string; vertodos:integer; procedure AppMessage(var Msg: Tmsg; var Handled:Boolean); end; var entradas: Tentradas; implementation {$R *.dfm} uses fbuscararticulos, fbuscararticulosprov, ftraza_materiales; procedure Tentradas.AppMessage(var Msg: Tmsg; var Handled: Boolean); var actual : TWincontrol; begin if Msg.message = WM_KEYDOWN then begin // esto es para controlar que con la flecha abajo se desplegen las // listas if Msg.wParam = VK_DOWN then begin Actual := Screen.ActiveControl; if Actual is TDBLookupComboBox then if not TDBLookupComboBox(Screen.ActiveForm.ActiveControl).listVisible then TDBLookupComboBox(Screen.ActiveForm.ActiveControl).DropDown; // si utilizas las RX { if Actual is TRxDBLookupCombo then if not TRxDBLookupCombo(Screen.ActiveForm.ActiveControl).listVisible then TRxDBLookupCombo(Screen.ActiveForm.ActiveControl).DropDown; } if Actual is TComboBox then SendMessage(TComboBox(Screen.ActiveForm.ActiveControl).Handle,CB_SHOWDROPDOWN,-1,0); if (Actual is TCustomEdit) then Msg.wParam := VK_TAB; // se pueden añadir mas controles.... end; // y aqui es el control del intro. if Msg.wParam = VK_RETURN THEN begin Actual := Screen.ActiveControl; // para los edit. if (Actual is TCustomEdit) and not (Actual is TCustomMemo) then Msg.wParam := VK_TAB; // para los memo. Con ctrl + enter se salta de linea dentro del // memo, si solo es intro salta al siguiente control if (Actual is TCustomMemo) and (HiWord(GetKeyState(VK_CONTROL)) = 0)then Msg.wParam := VK_TAB; // para los lookup if Actual is TDBLookupComboBox then if not TDBLookupComboBox(Actual).listVisible then Msg.wParam := VK_TAB; // Si utilizas las RX { if Actual is TRxDBLookupCombo then if not TRxDBLookupCombo(Actual).listVisible then Msg.wParam := VK_TAB; } // Los combobox if Actual is TCustomComboBox then if not TCustomComboBox(Actual).DroppedDown then Msg.wParam := VK_TAB; // el radiobutton if Actual is TRadioButton then Msg.wParam := VK_TAB; // en el grid if Actual is TDBGrid then if (TDBGrid(Actual).ReadOnly) or (HiWord(GetKeyState(VK_CONTROL)) = 1)then Msg.wParam := VK_TAB; // o esto otro Con Ctrl + intro siguiente celda con // ctrl + shift + intro celda anterior // es complicado pero es por no liar con el intro solo // que siempre pasa de un control a otro. if Actual is TDBGrid then begin if (TDBGrid(Actual).ReadOnly) or (HiWord(GetKeyState(VK_CONTROL)) = 0) then Msg.wParam := VK_TAB else if not(HiWord(GetKeyState(VK_SHIFT)) = 0) then begin if TDBGrid(Actual).selectedindex > 0 then { increment the field } TDBGrid(Actual).selectedindex := TDBGrid(Actual).selectedindex -1 else TDBGrid(Actual).selectedindex := TDBGrid(Actual).fieldcount -1; end else begin if TDBGrid(Actual).selectedindex < (TDBGrid(Actual).fieldcount -1) then { increment the field } TDBGrid(Actual).selectedindex := TDBGrid(Actual).selectedindex +1 else TDBGrid(Actual).selectedindex := 0; end; end; // ListBox if Actual is TCustomListBox then Msg.wParam := VK_TAB; // TabControl if Actual is TCustomTabControl then Msg.wParam := VK_TAB; // Aqui se pondrían todos los controles que usamos en la // aplicación y necesitamos cambiar el tab por intro. // de esta forma te despreocupas de cuantos añades y es más // limpio el código end; end; end; procedure Tentradas.BtEborrarClick(Sender: TObject); begin entradas.Delete; qentradas.Refresh; end; procedure Tentradas.BtEcancelarClick(Sender: TObject); begin entradas.Cancel; entradas.MasterSource:=dsqentradas; end; procedure Tentradas.BtEguardarClick(Sender: TObject); begin entradas.Post; qentradas.Refresh; qentradas.Locate('id',entradasid.AsInteger,[locaseinsensitive,lopartialkey]); entradas.MasterSource:=dsqentradas; entradas.Refresh; end; procedure Tentradas.btEmodificarClick(Sender: TObject); begin entradas.edit; dbedit3.SetFocus; end; procedure Tentradas.BtENuevoClick(Sender: TObject); begin entradas.MasterSource:=nil; entradas.append; entradasidempresa.Value:=dm.tempresaid.AsInteger; dbedit3.SetFocus; end; procedure Tentradas.Button1Click(Sender: TObject); begin entradas_detalle.Delete; entradas_detalle.Refresh; entradas.Refresh; end; procedure Tentradas.Button2Click(Sender: TObject); //var tlote,tcodigo,proveedor,documento:string; begin // tlote:=''''+entradas_detallelote.asstring+''''; // tcodigo:=''''+entradas_detalleid_propio.AsString+''''; // proveedor:=''''+qentradasnomproveedor.AsString+''''; // documento:=''''+qentradasdocumento.AsString+''''; traza_materiales := Ttraza_materiales.Create(nil); try traza_materiales.qpesadas.close; traza_materiales.qpesadas.Prepare; traza_materiales.qpesadas.ParamByName('IDENTRADA').AsInteger:=entradas_detalleid.AsInteger; traza_materiales.qpesadas.ParamByName('LOTE').Asstring:=entradas_detalleid.AsString; traza_materiales.qpesadas.open; { traza_materiales.qpesadas2.close; traza_materiales.qpesadas2.SQL.Clear; traza_materiales.qpesadas2.SQL.Add('select '+proveedor+' as proveedor,'+documento+' as documento,'+tcodigo+' as codigo,'+tlote+' as lote,ea.id,idea,fecha,p.iddestino,origen,idorigen,parcela,lotes_detalle,ea.idalbaran,ea.idfactura as factura,c.nombre,p.pedidocliente,cd.descripcion as destino from ea_pesadas ea,pedidos p,clientes c,clientes_destinos cd'); traza_materiales.qpesadas2.SQL.Add(' where c.idempresa=cd.idempresa and c.id=cd.idcliente and cd.codigo=p.iddestino and ea.idempresa=p.ide and ea.pedidocliente=p.id and p.idcliente=c.id and c.idempresa=ea.idempresa and ea.id in (select DISTINCT(idea) from ea_materiales where lote='+tlote+' and idaf=(select id from articulosf where codigo='+tcodigo+')) order by ea.id'); traza_materiales.qpesadas2.open;} traza_materiales.showmodal(); finally traza_materiales.Free; end; end; procedure Tentradas.DBGrid1TitleClick(Column: TColumn); var st:ZAbstractRODataset.TSortType; begin st:=qentradas.SortType; qentradas.SortedFields:=Column.FieldName; If st = stAscending then qentradas.SortType:=stDescending else qentradas.SortType:=stAscending; dsqentradas.DataSet.First; end; procedure Tentradas.DBGrid2ColExit(Sender: TObject); begin if DBGrid2.SelectedField.FieldName = DBLookupComboBox2.DataField then DBLookupComboBox2.Visible := False; if (dbgrid2.SelectedField.FieldName='codigo') and (entradas_detalle.State in [dsedit,dsinsert]) and (entradas_detallecodigo.AsString<>'') then begin qaux.SQL.Clear; qaux.SQL.Add('select id,id_propio,descripcion,ult_precio from proveedores_articulos where idempresa='+entradasidempresa.AsString+' and id='+''''+entradas_detallecodigo.asstring+''''+' and idproveedor='+entradasidproveedor.asstring); qaux.Open; if qaux.RecordCount>0 then begin entradas_detalledescripcion.Value:=qaux.Fields[2].AsString; entradas_detalleid_propio.Value:=qaux.Fields[1].AsString; entradas_detalleprecio.Value:=qaux.Fields[3].Asfloat; end; end; end; procedure Tentradas.DBGrid2DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); begin if (gdFocused in State) then begin if (Column.Field.FieldName = DBLookupComboBox2.DataField) then with DBLookupComboBox2 do begin Left := Rect.Left + DBGrid2.Left + 2; Top := Rect.Top + DBGrid2.Top + 2; Width := Rect.Right - Rect.Left; Width := Rect.Right - Rect.Left; Height := Rect.Bottom - Rect.Top; Visible := True; end; end end; procedure Tentradas.DBGrid2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if (key=115) then begin buscararticulosprov := Tbuscararticulosprov.Create(nil); try fbuscararticulosprov.filtro:=entradas_detallecodigo.AsString; buscararticulosprov.Edit1.Text:=entradas_detallecodigo.AsString; buscararticulosprov.showmodal(); finally if entradas_detalle.State in [dsbrowse] then entradas_detalle.Edit; entradas_detallecodigo.Value:=buscararticulosprov.qarticuloscodigo.AsString; entradas_detalledescripcion.value:=buscararticulosprov.qarticulosdescripcion.AsString; buscararticulosprov.Free; end; end; end; procedure Tentradas.DBGrid2KeyPress(Sender: TObject; var Key: Char); begin if (key = Chr(9)) then Exit; if (DBGrid2.SelectedField.FieldName = DBLookupComboBox2.DataField) then begin DBLookupComboBox2.SetFocus; SendMessage(DBLookupComboBox2.Handle, WM_Char, word(Key), 0); end end; procedure Tentradas.dsentradasStateChange(Sender: TObject); begin if entradas.State in [dsedit,dsinsert] then begin Panel14.Enabled:=true; btenuevo.enabled:=false; bteborrar.enabled:=false; btemodificar.enabled:=false; bteguardar.enabled:=true; btecancelar.enabled:=true; panel7.Visible:=false; dbgrid2.Visible:=false; end; if entradas.State in [dsbrowse] then begin Panel14.Enabled:=false; btenuevo.enabled:=true; bteborrar.enabled:=true; btemodificar.enabled:=true; bteguardar.enabled:=false; btecancelar.enabled:=false; panel7.Visible:=true; dbgrid2.Visible:=true; end; end; procedure Tentradas.entradas_detalleAfterPost(DataSet: TDataSet); var preciocompra,tipoiva,idempresa,codigoart:string; begin preciocompra:=entradas_detalleprecio.AsString; preciocompra:=stringreplace(preciocompra,',','.',[rfreplaceall]); tipoiva:=entradas_detalletipo_iva.AsString; tipoiva:=stringreplace(tipoiva,',','.',[rfreplaceall]); idempresa:=entradas_detalleidempresa.AsString; codigoart:=entradas_detalleid_propio.AsString; entradas.Refresh; entradas_detalle.Refresh; qaux.SQL.Clear; qaux.SQL.Add('update articulosf set ult_preciocompra='+preciocompra+',tipoiva=(select id from tipos_iva where tipo='+tipoiva+')'); qaux.SQL.Add(',stock=(select sum(stock) from entradas_detalle where stock>0 and id_propio='+''''+codigoart+''''+' and idempresa='+idempresa+')'); qaux.SQL.Add(',preciocoste='+preciocompra); qaux.SQL.Add(' where idempresa='+idempresa+' and codigo='+''''+codigoart+''''); qaux.execsql; end; procedure Tentradas.entradas_detalleBeforePost(DataSet: TDataSet); begin entradas_detalleidempresa.Value:=dm.tempresaid.AsInteger; if entradas_detallecodigo.asstring='' then entradas_detalle.Cancel; end; procedure Tentradas.FormCreate(Sender: TObject); begin with DBLookupComboBox2 do begin DataSource := dsentradasdetalle; // -> AdoTable1 -> DBGrid1 ListSource := dsarticulospropios; DataField := 'id_propio'; // from AdoTable1 - displayed in the DBGrid KeyField := 'codigo'; ListField := 'descripcion;codigo'; Visible := False; end; end; procedure Tentradas.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if (activecontrol is tdbgrid) then begin if Key = 13 then Key :=9; end; end; procedure Tentradas.FormKeyPress(Sender: TObject; var Key: Char); var formatos:tformatsettings; punto:string; begin GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, formatos); punto:=formatos.DecimalSeparator; if punto=',' then begin key:=upcase(key); if (key='.') and (activecontrol is tdbedit) then begin if (activecontrol as tdbedit).Field.DataType in [ftfloat,ftCurrency,ftbcd,ftfmtbcd] then key:=',' end; if (key='.') and (activecontrol is tdbgrid) then begin key:=','; end; end; if (activecontrol is tdbgrid) then begin if Key = #13 then Key :=#9; end; if (key=#13) then begin Perform(WM_NextDLGCtl,0,0); key:=#0; end; end; procedure Tentradas.FormShow(Sender: TObject); begin dm.tempresa.Open; qentradas.Close; qentradas.Open; entradas.Close; entradas.Open; entradas_detalle.Close; entradas_detalle.Open; qproveedores.Open; qarticulospropios.open; end; procedure Tentradas.qentradasBeforeOpen(DataSet: TDataSet); var filtrolocal:string; begin if filtro<>'' then begin begin filtrolocal:=' where nombre like('+''''+filtro+''''+') or nombre_corto like('+''''+filtro+''''+') or id like('+''''+filtro+''''+')'; end; if vertodos=0 then begin begin filtrolocal:=' where idproveedor in (select id from proveedores where nombre like('+''''+filtro+''''+')) or documento like('+''''+filtro+''''+') or id like('+''''+filtro+''''+')'; end; end; end; if filtro='' then begin filtrolocal:=''; end; qentradas.SQL.Clear; qentradas.SQL.Add('select * from entradas ' + filtrolocal +' order by fecha desc,idproveedor') end; procedure Tentradas.qentradasCalcFields(DataSet: TDataSet); begin qaux.SQL.Clear; qaux.SQL.Add('select nombre_corto from proveedores where id='+qentradasidproveedor.asstring+' and idempresa='+qentradasidempresa.asstring); qaux.Open; qentradasnomproveedor.Value:=qaux.Fields[0].AsString; end; procedure Tentradas.tbuscarChange(Sender: TObject); begin filtro:='%'+tbuscar.Text+'%'; qentradas.Close; qentradas.open; end; end.