Advertisement
FacundoCruz

2do

Oct 16th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.13 KB | None | 0 0
  1. unit u_GestionProductos;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   Grids, u_ABMProductos, u_Global;
  10.  
  11. type
  12.  
  13.   { TfrmGestionProductos }
  14.  
  15.   TfrmGestionProductos = class(TForm)
  16.     btnSalir: TButton;
  17.     btnAltas: TButton;
  18.     btnBajas: TButton;
  19.     btnModificaciones: TButton;
  20.     btnBuscar: TButton;
  21.     edtBuscar: TEdit;
  22.     grdProductos: TStringGrid;
  23.     Label1: TLabel;
  24.     Label2: TLabel;
  25.     procedure btnAltasClick(Sender: TObject);
  26.     procedure btnBajasClick(Sender: TObject);
  27.     procedure btnBuscarClick(Sender: TObject);
  28.     procedure btnModificacionesClick(Sender: TObject);
  29.     procedure btnSalirClick(Sender: TObject);
  30.     procedure FormActivate(Sender: TObject);
  31.     procedure FormCreate(Sender: TObject);
  32.     procedure grdProductosMouseUp(Sender: TObject; Button: TMouseButton;
  33.       Shift: TShiftState; X, Y: Integer);
  34.     procedure mostrarRegistro();
  35.     procedure grdProductosDblClick(Sender: TObject);
  36.  
  37.   private
  38.  
  39.   public
  40.      procedure MostrarProductos(var f:TFicheroProductos;codigoBusqueda:Cadena20);
  41.   end;
  42.  
  43. var
  44.   frmGestionProductos: TfrmGestionProductos;
  45.  
  46. implementation
  47.  
  48. {$R *.lfm}
  49.  
  50. { TfrmGestionProductos }
  51.  
  52. procedure TfrmGestionProductos.MostrarProductos(var f: TFicheroProductos; codigoBusqueda: Cadena20);
  53. var
  54.  fila : integer;
  55.   R : TProducto;
  56.   codigoCad, precioCad  : Cadena20;
  57. begin
  58.   try
  59.     reset(f);
  60.     grdProductos.RowCount := 1;
  61.       while not eof(f) do
  62.          begin
  63.           read(f, R);
  64.           codigoCad := IntToStr(R.codigo);
  65.           Str(R.precio:7:2, precioCad);
  66.           if (codigoCad = codigoBusqueda) or (codigoBusqueda = '') then
  67.             begin
  68.               grdProductos.RowCount:= grdProductos.RowCount + 1;
  69.               fila :=  grdProductos.RowCount - 1;
  70.               grdProductos.Cells[1,fila] := IntToStr(R.codigo);
  71.               grdProductos.Cells[2,fila] := R.descripcion;
  72.               grdProductos.Cells[3,fila] := precioCad;
  73.               grdProductos.Cells[4,fila] := IntToStr(R.stock);
  74.             end;
  75.          end;
  76.       closeFile(f);
  77.     except
  78.       on E: EInOutError do
  79.       ShowMessage('File handling error occurred. Details: '+E.ClassName+'/'+E.Message);
  80.   end;
  81. end;
  82.  
  83. procedure TfrmGestionProductos.btnAltasClick(Sender: TObject);
  84. begin
  85.   frmABM.Show;
  86.   frmABM.lblTitulo.Caption:='Nuevo Producto';
  87.   frmABM.btnABM.Caption:='Alta';
  88.   frmABM.edtCodigo.Enabled:=true;
  89.   frmABM.inicializarComponentes();
  90. end;
  91.  
  92. procedure TfrmGestionProductos.btnBajasClick(Sender: TObject);
  93. var
  94.   mensaje: Cadena70;
  95.   codigo : Cadena20;
  96. begin
  97.   codigo := grdProductos.Cells[1, grdProductos.Row];
  98.   if codigo <> '' then
  99.     begin
  100.       mensaje  := '¿Confirma eliminar el producto = ' + codigo + '?';
  101.       if MessageDlg(mensaje, mtConfirmation, [mbOk, mbCancel], 0) = mrOk then
  102.         begin
  103.           bajaProducto(f,codigo);
  104.           MostrarProductos(f,'');
  105.         end
  106.     end
  107.   else
  108.     ShowMessage('Debe buscar un código válido');
  109.   edtBuscar.Text := '';
  110. end;
  111.  
  112. procedure TfrmGestionProductos.btnBuscarClick(Sender: TObject);
  113. var
  114.   posicion : integer;
  115.   codigo: Cadena20;
  116. begin
  117.   codigo := edtBuscar.Text;
  118.   if(codigo = '') then
  119.      MostrarProductos(f,'')
  120.   else
  121.     begin
  122.        posicion := buscarProducto(f, StrToInt(codigo));
  123.        if (posicion <> NO_ENCONTRADO) then
  124.           mostrarProductos(f,codigo)
  125.     end;
  126. end;
  127.  
  128. procedure TfrmGestionProductos.btnModificacionesClick(Sender: TObject);
  129. var
  130.   posicion:integer;
  131.   codigo: String;
  132. begin
  133.   try
  134.       codigo := grdProductos.Cells[1, grdProductos.Row];
  135.       posicion := buscarProducto(f, StrToInt(codigo));
  136.       if (posicion = NO_ENCONTRADO) then
  137.           ShowMessage('El producto con ese código no existe')
  138.       else
  139.         begin
  140.           frmABM.Show;
  141.           frmABM.lblTitulo.Caption := 'Modificación de producto';
  142.           frmABM.btnABM.Caption:='Modificar';
  143.           frmABM.edtCodigo.Enabled:=false;
  144.           mostrarRegistro();
  145.         end;
  146.   except
  147.     on e: EConvertError do
  148.        showMessage('Debe seleccionar un producto de la lista...')
  149.    end;
  150. end;
  151.  
  152. procedure TfrmGestionProductos.btnSalirClick(Sender: TObject);
  153. begin
  154.   close;
  155. end;
  156.  
  157. procedure TfrmGestionProductos.FormActivate(Sender: TObject);
  158. begin
  159.   MostrarProductos(f,''); //muestra todos los productos
  160.   edtBuscar.Text := '';
  161. end;
  162.  
  163. procedure TfrmGestionProductos.FormCreate(Sender: TObject);
  164. begin
  165.   crearFichero(f);
  166.   MostrarProductos(f,''); //muestra todos los product
  167. end;
  168.  
  169. procedure TfrmGestionProductos.grdProductosMouseUp(Sender: TObject;
  170.   Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  171. begin
  172.  
  173. end;
  174.  
  175. procedure TfrmGestionProductos.mostrarRegistro();
  176. begin
  177.   frmABM.edtCodigo.Caption := grdProductos.Cells[1, grdProductos.Row];
  178.   frmABM.edtDescripcion.Caption := grdProductos.Cells[2, grdProductos.Row];
  179.   frmABM.edtPrecio.Caption := grdProductos.Cells[3, grdProductos.Row];
  180.   frmABM.edtStock.Caption :=grdProductos.Cells[4, grdProductos.Row];
  181. end;
  182.  
  183. procedure TfrmGestionProductos.grdProductosDblClick(Sender: TObject);
  184. begin
  185.  
  186. end;
  187.  
  188. end.
  189.                                
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement