unit uViewArticulo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, ExtCtrls, StdCtrls, Buttons, DB, uDADataTable,
  DBCtrls, Grids, DBGrids, uBizArticulos, Mask, ComCtrls, uCustomView,
  JvComponent, JvFormAutoSize, cxControls, cxContainer, cxEdit, cxTextEdit,
  cxDBEdit, dxLayoutControl, cxCheckBox, PngSpeedButton, cxImage, ActnList,
  ImgList, PngImageList, cxGraphics, cxMaskEdit, cxDropDownEdit,
  uFamiliasController, uViewDatosYSeleccionProveedor, cxSpinEdit, cxCurrencyEdit,
  uArticulosController, uUnidadesMedidaController, uFabricantesController, uDAInterfaces;

type
  IViewArticulo = interface(IViewBase)
  ['{65206152-786D-47AE-A037-5EF480BC056C}']
    function GetArticulo: IBizArticulo;
    procedure SetArticulo(const Value: IBizArticulo);
    property Articulo: IBizArticulo read GetArticulo write SetArticulo;

    function GetController : IArticulosController;
    procedure SetController (const Value : IArticulosController);
    property Controller : IArticulosController read GetController write SetController;
  end;

  TfrViewArticulo = class(TfrViewBase, IViewArticulo)
    dsViewArticulo: TDADataSource;
    dxLayoutControlArticuloGroup_Root: TdxLayoutGroup;
    dxLayoutControlArticulo: TdxLayoutControl;
    dxLayoutControlArticuloGroup1: TdxLayoutGroup;
    dxLayoutControlArticuloItem3: TdxLayoutItem;
    eReferencia: TcxDBTextEdit;
    dxLayoutControlArticuloItem8: TdxLayoutItem;
    eDescripcion: TcxDBTextEdit;
    dxLayoutControlArticuloGroup2: TdxLayoutGroup;
    dxLayoutControlArticuloItem1: TdxLayoutItem;
    eNoComisionable: TcxDBCheckBox;
    dxLayoutControlArticuloGroup4: TdxLayoutGroup;
    OpenDialog1: TOpenDialog;
    SmallImages: TPngImageList;
    ActionList1: TActionList;
    actAnadir: TAction;
    actEliminar: TAction;
    dxLayoutControlArticuloItem2: TdxLayoutItem;
    PngSpeedButton2: TPngSpeedButton;
    dxLayoutControlArticuloItem5: TdxLayoutItem;
    PngSpeedButton1: TPngSpeedButton;
    dxLayoutControlArticuloGroup3: TdxLayoutGroup;
    eImagen: TcxImage;
    dxLayoutControlArticuloItem6: TdxLayoutItem;
    dxLayoutControlArticuloGroup5: TdxLayoutGroup;
    dxLayoutControlArticuloItem4: TdxLayoutItem;
    cbFamilia: TcxDBComboBox;
    dxLayoutControlArticuloGroup6: TdxLayoutGroup;
    dxLayoutControlArticuloItem7: TdxLayoutItem;
    cbNoInventariable: TcxDBCheckBox;
    dxLayoutControlArticuloItem9: TdxLayoutItem;
    cbUnidadMedida: TcxDBComboBox;
    dxLayoutControlArticuloItem10: TdxLayoutItem;
    cbFabricante: TcxDBComboBox;
    dxLayoutControlArticuloItem11: TdxLayoutItem;
    eReferenciaFabr: TcxDBTextEdit;
    dxLayoutControlArticuloGroup10: TdxLayoutGroup;
    dxLayoutControlArticuloGroup14: TdxLayoutGroup;
    procedure actEliminarExecute(Sender: TObject);
    procedure actAnadirExecute(Sender: TObject);
    procedure cbFamiliaPropertiesInitPopup(Sender: TObject);
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure cbFamiliaPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure PrecioNetoChanged(Sender: TObject);
    procedure cbUnidadMedidaPropertiesInitPopup(Sender: TObject);
    procedure cbUnidadMedidaPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure cbFabricantePropertiesInitPopup(Sender: TObject);
    procedure cbFabricantePropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
  private
    procedure CargarImagen;
    procedure SalvarImagen;
  protected
    FFamiliasController: IFamiliasController;
    FUnidadesController: IUnidadesMedidaController;
    FFabricantesController: IFabricantesController;    
    FController: IArticulosController;
    FArticulo: IBizArticulo;
    function GetArticulo: IBizArticulo;
    procedure SetArticulo(const Value: IBizArticulo); virtual;
    function GetController : IArticulosController;
    procedure SetController (const Value : IArticulosController);
    procedure OnProveedorChanged(Sender: TObject);
  public
    property Articulo: IBizArticulo read GetArticulo write SetArticulo;
    property Controller : IArticulosController read GetController write SetController;
  end;

implementation

uses
  uDataModuleArticulos, uSistemaFunc, uBizFamilias, uBizContactos,
  uBizUnidadesMedida, uBizFabricantes;

{$R *.dfm}


{ TfrViewArticulos }

{
******************************* TfrViewArticulos ********************************
}

procedure TfrViewArticulo.actAnadirExecute(Sender: TObject);
begin
{  eImagen.LoadFromFile;
  if Assigned(eImagen.Picture) then
    SalvarImagen;}
end;

procedure TfrViewArticulo.actEliminarExecute(Sender: TObject);
begin
{  inherited;
  eImagen.Clear;
  FArticulo.QuitarImagen;}
end;

procedure TfrViewArticulo.CargarImagen;
{var
  aAux : String;}
begin
{  if FArticulo.HayImagen then
  begin
    ShowHourglassCursor;
    try
      aAux := DarFicheroJPGTemporal;
      FArticulo.SalvarImagen(aAux);
      try
        eImagen.Picture.LoadFromFile(aAux);
      except
        on EInvalidGraphic do
          ShowMessage('error');
      end;
    finally
      DeleteFile(aAux);
      HideHourglassCursor;
    end;
  end;}
end;

procedure TfrViewArticulo.cbFabricantePropertiesInitPopup(Sender: TObject);
begin
  inherited;
  with cbFabricante.Properties.Items do
  begin
    if (Count = 0) then
    begin
      ShowHourglassCursor;
      BeginUpdate;
      try
        Clear;
        AddStrings(FFabricantesController.DarListaFabricantes);
      finally
        EndUpdate;
        HideHourglassCursor;
      end;
    end;
  end;
end;

procedure TfrViewArticulo.cbFabricantePropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  AFabricantes : IBizFabricante;
begin
  inherited;
  with (Sender as TcxDBComboBox) do
  begin
    if (Length(DisplayValue) > 0) and (DisplayValue <> FArticulo.FAMILIA) then
      if Properties.LookupItems.IndexOf(DisplayValue) < 0 then
      begin
        ShowHourglassCursor;
        Properties.LookupItems.BeginUpdate;
        AFabricantes := FFabricantesController.BuscarTodos;
        AFabricantes.DataTable.Active := True;
        try
          FFabricantesController.Anadir(AFabricantes);
          AFabricantes.DESCRIPCION := DisplayValue;
          FFabricantesController.Guardar(AFabricantes);
          Properties.LookupItems.Add(DisplayValue);
        finally
          Properties.LookupItems.EndUpdate;
          HideHourglassCursor;
          AFabricantes := NIL;
        end;
      end;
  end;
end;

procedure TfrViewArticulo.cbFamiliaPropertiesInitPopup(Sender: TObject);
begin
  inherited;
  with cbFamilia.Properties.Items do
  begin
    if (Count = 0) then
    begin
      ShowHourglassCursor;
      BeginUpdate;
      try
        Clear;
        AddStrings(FFamiliasController.DarListaFamilias);
      finally
        EndUpdate;
        HideHourglassCursor;
      end;
    end;
  end;
end;

procedure TfrViewArticulo.cbFamiliaPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  AFamilias : IBizFamilia;
begin
  inherited;
  with (Sender as TcxDBComboBox) do
  begin
    if (Length(DisplayValue) > 0) and (DisplayValue <> FArticulo.FAMILIA) then
      if Properties.LookupItems.IndexOf(DisplayValue) < 0 then
      begin
        ShowHourglassCursor;
        Properties.LookupItems.BeginUpdate;
        AFamilias := FFamiliasController.BuscarTodos;
        AFamilias.DataTable.Active := True;
        try
          FFamiliasController.Anadir(AFamilias);
          AFamilias.DESCRIPCION := DisplayValue;
          FFamiliasController.Guardar(AFamilias);
          Properties.LookupItems.Add(DisplayValue);
        finally
          Properties.LookupItems.EndUpdate;
          HideHourglassCursor;
          AFamilias := NIL;
        end;
      end;
  end;
end;

procedure TfrViewArticulo.cbUnidadMedidaPropertiesInitPopup(Sender: TObject);
begin
  inherited;
  with cbUnidadMedida.Properties.Items do
  begin
    if (Count = 0) then
    begin
      ShowHourglassCursor;
      BeginUpdate;
      try
        Clear;
        AddStrings(FUnidadesController.DarListaUnidadesMedida);
      finally
        EndUpdate;
        HideHourglassCursor;
      end;
    end;
  end;
end;

procedure TfrViewArticulo.cbUnidadMedidaPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
var
  AUnidades : IBizUnidadMedida;
begin
  inherited;
  with (Sender as TcxDBComboBox) do
  begin
    if (Length(DisplayValue) > 0) and (DisplayValue <> FArticulo.FAMILIA) then
      if Properties.LookupItems.IndexOf(DisplayValue) < 0 then
      begin
        ShowHourglassCursor;
        Properties.LookupItems.BeginUpdate;
        AUnidades := FUnidadesController.BuscarTodos;
        AUnidades.DataTable.Active := True;
        try
          FUnidadesController.Anadir(AUnidades);
          AUnidades.DESCRIPCION := DisplayValue;
          FUnidadesController.Guardar(AUnidades);
          Properties.LookupItems.Add(DisplayValue);
        finally
          Properties.LookupItems.EndUpdate;
          HideHourglassCursor;
          AUnidades := NIL;
        end;
      end;
  end;
end;

procedure TfrViewArticulo.CustomViewCreate(Sender: TObject);
begin
  inherited;
  FFamiliasController := TFamiliasController.Create;
  FUnidadesController := TUnidadesMedidaController.Create;
  FFabricantesController := TFabricantesController.Create;  
end;

procedure TfrViewArticulo.CustomViewDestroy(Sender: TObject);
begin
  FFamiliasController := Nil;
  FUnidadesController := NIL;
  FFabricantesController := NIL;  
  inherited;
end;

function TfrViewArticulo.GetArticulo: IBizArticulo;
begin
  Result := FArticulo;
end;

function TfrViewArticulo.GetController: IArticulosController;
begin
  Result := FController;
end;

procedure TfrViewArticulo.OnProveedorChanged(Sender: TObject);
begin
  if Assigned(FArticulo) then
  begin
    FArticulo.Edit;
//    FArticulo.ID_PROVEEDOR := frViewDatosYSeleccionProveedor1.Proveedor.ID;
//    FArticulo.DESCUENTO := frViewDatosYSeleccionProveedor1.Proveedor.DESCUENTO;
  end;
end;

procedure TfrViewArticulo.PrecioNetoChanged(Sender: TObject);
begin
  if Assigned(FController) then
     FController.CalcularPrecioNeto(FArticulo);
end;

procedure TfrViewArticulo.SalvarImagen;
var
  aAux : String;
begin
{  ShowHourglassCursor;
  try
    aAux := DarFicheroJPGTemporal;
    eImagen.Picture.SaveToFile(aAux);
    FArticulo.CargarImagen(aAux);
    Self.Modified := True;
  finally
    DeleteFile(aAux);
    HideHourglassCursor;
  end;}
end;

procedure TfrViewArticulo.SetArticulo(const Value: IBizArticulo);
begin
  FArticulo := Value;
//  frViewDatosYSeleccionProveedor1.OnProveedorChanged := NIL;

  if Assigned(FArticulo) then
  begin
    dsViewArticulo.DataTable := FArticulo.DataTable;
//    frViewDatosYSeleccionProveedor1.Controller := FController.ProveedoresController;
//    frViewDatosYSeleccionProveedor1.Proveedor := (FController.ProveedoresController.Buscar(FArticulo.ID_PROVEEDOR) as IBizProveedor);
//    frViewDatosYSeleccionProveedor1.OnProveedorChanged := OnProveedorChanged;

//    CargarImagen;
  end
  else begin
    dsViewArticulo.DataTable := NIL;
    FArticulo := NIL;
    eImagen.Clear;
  end;
end;

procedure TfrViewArticulo.SetController(const Value: IArticulosController);
begin
  FController := Value;
end;

end.

