microbiano
17-10-2010, 06:05:53
planteo el problema.
resulta que tengo un StringGrid el cual hace la funcion de detalle de factura, bien pues resulta que cuando agrego un producto hago lo siguiente:
1. si el registro ya esta en el Tstringgrid tomo los valores y le sumo lo que el usuario introduce, en caso de no existeis busco ese registro en la base de datos y loa grego al TstringGrid.
el problema surge en que el ciclo for cuando busca el registro en el TstrigGrid no funciona alguien tiene una idea adjunto todo el codigo de cuando pierde el foco un Tedit.
procedure TFfactura.txtcodigoExit(Sender: TObject);
var wcodigobarras: string;
wprecio_total:Double;
wprecio_venta:Double;
i:Integer;
{variables para modificaciones}
wcodigomodificar,wdescripcionmodificar:string;
wcantidadmodificar,wventa_totalmodificada:Integer;
wdescuentomodificar,wpreciomodificar:Double;
wtotalmodificar:Double;
begin
wcodigobarras:=Trim(Ffactura.txtcodigo.Text);
if wcodigobarras<>'' then
begin
//recorro todos los registros del Tstring grig
//ESte form es el que solo se ejecuta una vez aunque el tString tenga 10 filas por ejemplo
For i:=1 to Ffactura.SGFact.RowCount-1 do
begin
if Ffactura.SGFact.Cells[0,i]= Trim(wcodigobarras) then
begin
{tomo los registros que existen en el flexgrid de acuerdo al registro que introdujeron}
wcodigomodificar:=Ffactura.SGFact.Cells[0,i]; //codigo barras
wdescripcionmodificar:=Ffactura.SGFact.Cells[1,i]; //descripcion
wcantidadmodificar:=StrToInt(Ffactura.SGFact.Cells[2,i]); //cantidad venta
wpreciomodificar:=wprecioventa;
wdescuentomodificar:=StrToFloat(Ffactura.SGFact.Cells[4,i]);//descuento
{valido la variable de cantidad para ver si es mayor a uno}
if wcantidad_venta<>0 then
begin
Ffactura.SGFact.Cells[0,i]:=wcodigobarras;//codigobarras
Ffactura.SGFact.Cells[1,i]:=wdescripcionmodificar;//descripcion
wventa_totalmodificada:=wcantidadmodificar+wcantidad_venta;//suma cantidad
Ffactura.SGFact.Cells[2,i]:=IntToStr(wventa_totalmodificada);//asigno cantidad a celda
wprecio_total:=(wventa_totalmodificada*wprecioventa);//multiplico la cantidad por precio
Ffactura.SGFact.Cells[5,i] := Format('%8.2f',[wprecio_total]);
calcula_totales;
wcantidad_venta:=0;
Ffactura.txtcodigo.Text:='';
Ffactura.txtcodigo.SetFocus;
Exit;
end
else
begin
Ffactura.SGFact.Cells[0,i]:=wcodigobarras;//codigobarras
Ffactura.SGFact.Cells[1,i]:=wdescripcionmodificar;//descripcion
wventa_totalmodificada:=wcantidadmodificar+1;//suma cantidad
Ffactura.SGFact.Cells[2,i]:=IntToStr(wventa_totalmodificada);//asigno cantidad a celda
wprecio_total:=(wventa_totalmodificada*wprecioventa);//multiplico la cantidad por precio
Ffactura.SGFact.Cells[5,i] := Format('%8.2f',[wprecio_total]);
calcula_totales;
wcantidad_venta:=0;
Ffactura.txtcodigo.Text:='';
Ffactura.txtcodigo.SetFocus;
Exit;
end;
{termino la informacion proporcionada}
end
else {si no existe el registro en el flexgrid lo busco en la base de datos y lo agrego}
begin
//MessageDlg('agregando registros nuevos a la lista', mtError, [MBOK], 0);
with fmodulo.Qry_Temp2 do
begin
Close;
sql.clear;
SQL.Add('select * from articulo');
SQL.Add('where codigobarras=:Pcodigobarras');
Parameters.ParamByName('pcodigobarras').Value:=Trim(wcodigobarras);
try
Open;
if not Fmodulo.Qry_Temp2.IsEmpty then
begin
while not Fmodulo.Qry_Temp2.Eof do
begin
If Cuenta > 1 then SGFact.RowCount := SGFact.RowCount + 1;
wprecioventa:=StrToFloat(FieldByname('precioventa').AsVariant);
Ffactura.SGFact.Cells[0,cuenta] := Ffactura.txtcodigo.Text;
Ffactura.SGFact.Cells[1,Cuenta]:=(FieldByname('nombre').AsString);
{verifico cuantos productos asigno para la venta
y determino el importe total}
if wcantidad_venta<>0 then
begin
Ffactura.SGFact.Cells[2,Cuenta]:=IntToStr(wcantidad_venta);
wprecio_total:=(wcantidad_venta*wprecioventa);
end
else
begin
Ffactura.SGFact.Cells[2,Cuenta]:='1';
wcantidad_venta:=StrToInt(SGFact.Cells[2,cuenta]);
wprecio_total:=(wcantidad_venta*wprecioventa);
end;
wprecio_venta:=FieldByname('precioventa').AsFloat;
Ffactura.SGFact.Cells[3,Cuenta]:=Format('%8.2n',[wprecio_venta]);
wcantidad_venta:=StrToInt(SGFact.Cells[2,cuenta]);
SGFact.Cells[4,Cuenta]:='0.00';
//SGFact.Cells[5,Cuenta]:=Format('%8.2f',[wprecio_total]);
Ffactura.SGFact.Cells[5,Cuenta] := Format('%8.2f',[wprecio_total]);
Cuenta:=cuenta+1;
calcula_totales;
Next;
Ffactura.txtcodigo.Text:='';
Ffactura.txtcodigo.SetFocus;
{limpio la variable de venta}
wcantidad_venta:=0;
Exit;
end;
end
else
begin
MessageDlg('Registro'+chr(13)+'No localizado', mtError, [MBOK], 0);
Ffactura.txtcodigo.Text:='';
Ffactura.txtcodigo.SetFocus;
Exit;
end;
except
on E:EOleException do
begin
MessageDlg(Format('Error: %s Codigo: %d', [E.Message, E.ErrorCode]), mtError, [mbOK], 0);
end;
end;
end;
end;
end;
end;
end;
resulta que tengo un StringGrid el cual hace la funcion de detalle de factura, bien pues resulta que cuando agrego un producto hago lo siguiente:
1. si el registro ya esta en el Tstringgrid tomo los valores y le sumo lo que el usuario introduce, en caso de no existeis busco ese registro en la base de datos y loa grego al TstringGrid.
el problema surge en que el ciclo for cuando busca el registro en el TstrigGrid no funciona alguien tiene una idea adjunto todo el codigo de cuando pierde el foco un Tedit.
procedure TFfactura.txtcodigoExit(Sender: TObject);
var wcodigobarras: string;
wprecio_total:Double;
wprecio_venta:Double;
i:Integer;
{variables para modificaciones}
wcodigomodificar,wdescripcionmodificar:string;
wcantidadmodificar,wventa_totalmodificada:Integer;
wdescuentomodificar,wpreciomodificar:Double;
wtotalmodificar:Double;
begin
wcodigobarras:=Trim(Ffactura.txtcodigo.Text);
if wcodigobarras<>'' then
begin
//recorro todos los registros del Tstring grig
//ESte form es el que solo se ejecuta una vez aunque el tString tenga 10 filas por ejemplo
For i:=1 to Ffactura.SGFact.RowCount-1 do
begin
if Ffactura.SGFact.Cells[0,i]= Trim(wcodigobarras) then
begin
{tomo los registros que existen en el flexgrid de acuerdo al registro que introdujeron}
wcodigomodificar:=Ffactura.SGFact.Cells[0,i]; //codigo barras
wdescripcionmodificar:=Ffactura.SGFact.Cells[1,i]; //descripcion
wcantidadmodificar:=StrToInt(Ffactura.SGFact.Cells[2,i]); //cantidad venta
wpreciomodificar:=wprecioventa;
wdescuentomodificar:=StrToFloat(Ffactura.SGFact.Cells[4,i]);//descuento
{valido la variable de cantidad para ver si es mayor a uno}
if wcantidad_venta<>0 then
begin
Ffactura.SGFact.Cells[0,i]:=wcodigobarras;//codigobarras
Ffactura.SGFact.Cells[1,i]:=wdescripcionmodificar;//descripcion
wventa_totalmodificada:=wcantidadmodificar+wcantidad_venta;//suma cantidad
Ffactura.SGFact.Cells[2,i]:=IntToStr(wventa_totalmodificada);//asigno cantidad a celda
wprecio_total:=(wventa_totalmodificada*wprecioventa);//multiplico la cantidad por precio
Ffactura.SGFact.Cells[5,i] := Format('%8.2f',[wprecio_total]);
calcula_totales;
wcantidad_venta:=0;
Ffactura.txtcodigo.Text:='';
Ffactura.txtcodigo.SetFocus;
Exit;
end
else
begin
Ffactura.SGFact.Cells[0,i]:=wcodigobarras;//codigobarras
Ffactura.SGFact.Cells[1,i]:=wdescripcionmodificar;//descripcion
wventa_totalmodificada:=wcantidadmodificar+1;//suma cantidad
Ffactura.SGFact.Cells[2,i]:=IntToStr(wventa_totalmodificada);//asigno cantidad a celda
wprecio_total:=(wventa_totalmodificada*wprecioventa);//multiplico la cantidad por precio
Ffactura.SGFact.Cells[5,i] := Format('%8.2f',[wprecio_total]);
calcula_totales;
wcantidad_venta:=0;
Ffactura.txtcodigo.Text:='';
Ffactura.txtcodigo.SetFocus;
Exit;
end;
{termino la informacion proporcionada}
end
else {si no existe el registro en el flexgrid lo busco en la base de datos y lo agrego}
begin
//MessageDlg('agregando registros nuevos a la lista', mtError, [MBOK], 0);
with fmodulo.Qry_Temp2 do
begin
Close;
sql.clear;
SQL.Add('select * from articulo');
SQL.Add('where codigobarras=:Pcodigobarras');
Parameters.ParamByName('pcodigobarras').Value:=Trim(wcodigobarras);
try
Open;
if not Fmodulo.Qry_Temp2.IsEmpty then
begin
while not Fmodulo.Qry_Temp2.Eof do
begin
If Cuenta > 1 then SGFact.RowCount := SGFact.RowCount + 1;
wprecioventa:=StrToFloat(FieldByname('precioventa').AsVariant);
Ffactura.SGFact.Cells[0,cuenta] := Ffactura.txtcodigo.Text;
Ffactura.SGFact.Cells[1,Cuenta]:=(FieldByname('nombre').AsString);
{verifico cuantos productos asigno para la venta
y determino el importe total}
if wcantidad_venta<>0 then
begin
Ffactura.SGFact.Cells[2,Cuenta]:=IntToStr(wcantidad_venta);
wprecio_total:=(wcantidad_venta*wprecioventa);
end
else
begin
Ffactura.SGFact.Cells[2,Cuenta]:='1';
wcantidad_venta:=StrToInt(SGFact.Cells[2,cuenta]);
wprecio_total:=(wcantidad_venta*wprecioventa);
end;
wprecio_venta:=FieldByname('precioventa').AsFloat;
Ffactura.SGFact.Cells[3,Cuenta]:=Format('%8.2n',[wprecio_venta]);
wcantidad_venta:=StrToInt(SGFact.Cells[2,cuenta]);
SGFact.Cells[4,Cuenta]:='0.00';
//SGFact.Cells[5,Cuenta]:=Format('%8.2f',[wprecio_total]);
Ffactura.SGFact.Cells[5,Cuenta] := Format('%8.2f',[wprecio_total]);
Cuenta:=cuenta+1;
calcula_totales;
Next;
Ffactura.txtcodigo.Text:='';
Ffactura.txtcodigo.SetFocus;
{limpio la variable de venta}
wcantidad_venta:=0;
Exit;
end;
end
else
begin
MessageDlg('Registro'+chr(13)+'No localizado', mtError, [MBOK], 0);
Ffactura.txtcodigo.Text:='';
Ffactura.txtcodigo.SetFocus;
Exit;
end;
except
on E:EOleException do
begin
MessageDlg(Format('Error: %s Codigo: %d', [E.Message, E.ErrorCode]), mtError, [mbOK], 0);
end;
end;
end;
end;
end;
end;
end;