Forum > Windows

help please :c

(1/2) > >>

deewfrank:
Hi, first sorry for my english, im from Chile, i have a program who load an grid in lazarus.

but in the other grid i need to calculate the
total sales of every product with a button click(in my program are 10 products)
and i dont know how to do it :( any little help please and sorry for my very bad english, hope you can understand, here is my code

(pic of my first grid with 10000 reg)


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,  ComCtrls, StdCtrls, Types; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    ImageList1: TImageList;    ImageList2: TImageList;    ImageList3: TImageList;    ImageList4: TImageList;    MUESTRA_DATOS: TButton;    PageControl: TPageControl;    StringGrid1: TStringGrid;    StringGrid2: TStringGrid;    VER_VENTAS: TTabSheet;    VER_TOTAL_VENTAS: TTabSheet;    procedure Button1Click(Sender: TObject);    procedure MUESTRA_DATOSClick(Sender: TObject);    procedure StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;              aRect: TRect; aState: TGridDrawState);    procedure StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;              aState: TGridDrawState);    procedure StringGrid2DrawCell(Sender: TObject; aCol, aRow: Integer;              aRect: TRect; aState: TGridDrawState);  private    { private declarations }  public    { public declarations }  end;   type  eFicha = record    reg   : Integer;    prod  : Integer;    venta : Integer;    fpago : Integer;  end; var  Form1: TForm1;  registros : array[1..10000] of eFicha;  ingresa: eFicha;  archivo1 : File of eFicha;  archivo2 : File of eFicha;  archivo3 : File of eFicha;  totalventa1 : Integer;  totalventa2 : Integer;  totalventaprod1 : Integer;  totalventaprod2 : Integer;  c : Integer;  d : Integer;  e : Integer;  x : Integer; implementation {$R *.lfm} procedure cargadatos; var   e : integer;   precios: array [1..10] of  Integer = (1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000);   productos: array [1..10] of Integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);   formapago: array [1..4] of Integer = (0, 1, 2, 3); begin   randomize;   for e := 1 to 10000 do   begin     registros[e].reg := e;     registros[e].prod := (productos[low(productos) + random(length(productos))]);     registros[e].venta := (precios[low(precios) + random(length(precios))]);     registros[e].fpago := (formapago[low(formapago) + random(length(formapago))]);   end;end; procedure escribedatos; var  x : integer; begin  assignfile(archivo1,'registros.bin');  rewrite(archivo1);  for x := 1 to 10000 do  try    ingresa.reg:= registros[x].reg;    ingresa.prod:= registros[x].prod;    ingresa.venta:= registros[x].venta;    ingresa.fpago:= registros[x].fpago;    write(archivo1,ingresa);  except    writeLn('Error al escribir en archivo');  end;  close(archivo1);end; { TForm1 }procedure TForm1.MUESTRA_DATOSClick(Sender: TObject); var  x : integer; begin   assignfile(archivo2,'registros.bin');   c:=0;   x:=1;   reset(archivo2);   while NOT EOF(archivo2)do   begin       seek(archivo2,c);       BlockRead(archivo2,registros[x],1);       With Form1.StringGrid1 do       begin         RowCount:=RowCount+1;         Row:=RowCount;         Cells[0,Row]:= IntToStr(registros[x].reg);         Cells[1,Row]:= IntToStr(registros[x].prod);         Cells[2,Row]:= '$'+IntToStr(registros[x].venta);         Cells[3,Row]:= IntToStr(registros[x].fpago);         x:=x+1;       end;       c:=c+1;   end;   CloseFile(archivo2);end; procedure TForm1.Button1Click(Sender: TObject);begin     for d := 1 to 10000 do     x:=1;     With Form1.StringGrid2 do     begin        Cells[1,1]:= IntToStr(registros[x].venta);        Cells[1,2]:= IntToStr(registros[x].venta);        Cells[1,3]:= IntToStr(totalventaprod1);        Cells[1,4]:= IntToStr(totalventaprod2);        x:=x+1;     end;end;  procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState); begin     With Form1.StringGrid1 do     if (aCol = 3) then     begin        if cells[aCol,aRow] = '0' then        begin             ImageList1.Draw(Canvas, aRect.Left, aRect.Top,0);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '1' then        begin             ImageList1.Draw(Canvas, aRect.Left, aRect.Top,1);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '2' then        begin            ImageList1.Draw(Canvas, aRect.Left, aRect.Top,2);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '3' then        begin            ImageList1.Draw(Canvas, aRect.Left, aRect.Top,3);        end;     end;      With Form1.StringGrid1 do     if (aCol = 1) then     begin        if cells[aCol,aRow] = '1' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,0);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '2' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,1);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '3' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,2);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '4' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,3);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '5' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,4);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '6' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,5);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '7' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,6);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '8' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,7);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '9' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,8);        end;        With Form1.StringGrid1 do        if cells[aCol,aRow] = '10' then        begin             ImageList2.Draw(Canvas, aRect.Left, aRect.Top,9);        end;     end;end; procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer; aState: TGridDrawState); var  ts: TTextStyle;begin  if (ACol < 5) and (ARow = 0) then  begin    ts := StringGrid1.Canvas.TextStyle;    ts.Alignment := taCenter;    StringGrid1.Canvas.Font.Style := [fsBold];    StringGrid1.Canvas.TextStyle := ts;  end;  if (ACol = 2) and (ARow > 0) then  begin    ts := StringGrid1.Canvas.TextStyle;    ts.Alignment := taRightJustify;    StringGrid1.Canvas.Font.Style := [fsBold];    StringGrid1.Canvas.TextStyle := ts;  end;  if (ACol = 0) and (ARow > 0) then  begin    ts := StringGrid1.Canvas.TextStyle;    ts.Alignment := taCenter;    StringGrid1.Canvas.Font.Style := [fsBold];    StringGrid1.Canvas.TextStyle := ts;  end;end; procedure TForm1.StringGrid2DrawCell(Sender: TObject; aCol, aRow: Integer;  aRect: TRect; aState: TGridDrawState);begin     With Form1.StringGrid2 do     if (ACol = 0) and (ARow > 0) then     begin          if cells[aCol,aRow] = '0' then          begin               ImageList3.Draw(Canvas, aRect.Left, aRect.Top,0);          end;          if cells[aCol,aRow] = '1' then          begin               ImageList3.Draw(Canvas, aRect.Left, aRect.Top,1);          end;          if cells[aCol,aRow] = '2' then          begin               ImageList3.Draw(Canvas, aRect.Left, aRect.Top,2);          end;          if cells[aCol,aRow] = '3' then          begin               ImageList3.Draw(Canvas, aRect.Left, aRect.Top,3);          end;     end;     With Form1.StringGrid2 do     if (ACol > 0) and (ARow = 0) then     begin          if cells[aCol,aRow] = '0' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,0);          end;          if cells[aCol,aRow] = '1' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,1);          end;          if cells[aCol,aRow] = '2' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,2);          end;          if cells[aCol,aRow] = '3' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,3);          end;          if cells[aCol,aRow] = '4' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,4);          end;          if cells[aCol,aRow] = '5' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,5);          end;          if cells[aCol,aRow] = '6' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,6);          end;          if cells[aCol,aRow] = '7' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,7);          end;          if cells[aCol,aRow] = '8' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,8);          end;          if cells[aCol,aRow] = '9' then          begin               ImageList4.Draw(Canvas, aRect.Left, aRect.Top,9);          end;     end;end; begin  cargadatos;  escribedatos;end.  

lainz:
Tenes que iterar sobre "registros": guardar el total en $ y el total en cantidades para cada uno de los 10 productos, después ya sabés como usar esos valores para ponerlos en la otra TStringGrid.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  i: integer;  p1,..,p10: integer; // totales $  c1,..,c10: integer; // totales cantidadbegin  p1 := 0;  ...  p10 := 0;  c1 := 0;  ...  c10 := 0;   for i := 1 to 10000 do  begin    case registros[i].prod of      1: begin         p1 := p1 + registros[i].venta;         c1 := c1 + 1;      end;      2: begin       end;      3: begin       end;      4: begin       end;      5: begin       end;      6: begin       end;      7: begin       end;      8: begin       end;      9: begin       end;      10: begin       end;    end;  end;end;  

Es una forma de hacerlo. Otra forma es tener un array de todos los productos con registros y directamente iterar sobre ello:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---typeresultados = record  total_dinero: integer;  total_cantidad: integer;end; productos: array[1..10] of resultados; for i:=0 to 10000 dobegin  productos[registros.prod].total_dinero += registros[i].venta;  productos[registros.prod].total_cantidad += 1;end;

deewfrank:

--- Quote from: lainz on August 04, 2016, 10:27:44 pm ---Tenes que iterar sobre "registros": guardar el total en $ y el total en cantidades para cada uno de los 10 productos, después ya sabés como usar esos valores para ponerlos en la otra TStringGrid.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  i: integer;  p1,..,p10: integer; // totales $  c1,..,c10: integer; // totales cantidadbegin  p1 := 0;  ...  p10 := 0;  c1 := 0;  ...  c10 := 0;   for i := 1 to 10000 do  begin    case registros[i].prod of      1: begin         p1 := p1 + registros[i].venta;         c1 := c1 + 1;      end;      2: begin       end;      3: begin       end;      4: begin       end;      5: begin       end;      6: begin       end;      7: begin       end;      8: begin       end;      9: begin       end;      10: begin       end;    end;  end;end;  

Es una forma de hacerlo. Otra forma es tener un array de todos los productos con registros y directamente iterar sobre ello:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---typeresultados = record  total_dinero: integer;  total_cantidad: integer;end; productos: array[1..10] of resultados; for i:=0 to 10000 dobegin  productos[registros.prod].total_dinero += registros[i].venta;  productos[registros.prod].total_cantidad += 1;end;
--- End quote ---

Muchas gracias por responder!!, intentare hacer lo que me dijiste, esta parte me tiene super enredado  :(

PD: Dejo una foto del recuadro del cual hablo, saludos.

lainz:
En este caso como el resultado tiene que ir por tarjeta tenés que usar un registro para cada tarjeta:

type
resultados = record
  total_dinero_tarjeta1: integer; //efectivo
  total_dinero_tarjeta2: integer; // master
  total_dinero_tarjeta3: integer; //visa
  total_dinero_tarjeta4: integer; //cheque
end;

usarias el mismo array

productos: array[1..10] of resultados; // todos los productos tienen los 4 medios de pago

sumar los 4 totales no es dificil.

simplemente usa un if--else para saber en que tarjeta guardar.

deewfrank:

--- Quote from: lainz on August 04, 2016, 11:58:25 pm ---En este caso como el resultado tiene que ir por tarjeta tenés que usar un registro para cada tarjeta:

type
resultados = record
  total_dinero_tarjeta1: integer; //efectivo
  total_dinero_tarjeta2: integer; // master
  total_dinero_tarjeta3: integer; //visa
  total_dinero_tarjeta4: integer; //cheque
end;

usarias el mismo array

productos: array[1..10] of resultados; // todos los productos tienen los 4 medios de pago

sumar los 4 totales no es dificil.

simplemente usa un if--else para saber en que tarjeta guardar.

--- End quote ---

Muchísimas gracias  lainz!! ya tengo casi todo listo, un abrazo desde Chile, gracias por ayudarme  :D

Navigation

[0] Message Index

[#] Next page

Go to full version