unit uViewEmpresa;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, ExtCtrls, StdCtrls, Buttons, DB, uDADataTable,
  DBCtrls, Grids, DBGrids, uBizEmpresas, Mask, ComCtrls, uCustomView,
  JvComponent, JvFormAutoSize, cxControls, cxContainer, cxEdit, cxTextEdit,
  cxDBEdit, dxLayoutControl, dxLayoutLookAndFeels, cxMemo, cxMaskEdit,
  cxSpinEdit, cxImage, JvExControls, JvBitmapButton, ActnList, ImgList,
  PngImageList, TB2Item, TBX, TB2Dock, TB2Toolbar, PngSpeedButton, uDAInterfaces,
  cxGraphics, cxDropDownEdit, uEmpresasController;

type
  IViewEmpresa = interface(IViewBase)
    ['{876DCEBD-9E92-491A-84CE-498B1A84B525}']
    function GetEmpresa: IBizEmpresa;
    procedure SetEmpresa(const Value: IBizEmpresa);
    property Empresa: IBizEmpresa read GetEmpresa write SetEmpresa;
    function GetController : IEmpresasController;
    procedure SetController (const Value : IEmpresasController);
    property Controller : IEmpresasController read GetController write SetController;
  end;
  
  TfrViewEmpresa = class(TfrViewBase, IViewEmpresa)
    DADataSource: TDADataSource;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Group1: TdxLayoutGroup;
    dxLayoutControl1Group2: TdxLayoutGroup;
    dxLayoutControl1Group3: TdxLayoutGroup;
    dxLayoutControl1Group4: TdxLayoutGroup;
    dxLayoutControl1Group5: TdxLayoutGroup;
    dxLayoutControl1Group6: TdxLayoutGroup;
    dxLayoutControl1Group7: TdxLayoutGroup;
    dxLayoutControl1Item1: TdxLayoutItem;
    eCalle: TcxDBTextEdit;
    dxLayoutControl1Item2: TdxLayoutItem;
    cbProvincia: TcxDBComboBox;
    dxLayoutControl1Item3: TdxLayoutItem;
    cbPoblacion: TcxDBComboBox;
    dxLayoutControl1Item4: TdxLayoutItem;
    eCodigoPostal: TcxDBTextEdit;
    dxLayoutControl1Item5: TdxLayoutItem;
    ePaginaWeb: TcxDBTextEdit;
    dxLayoutControl1Item6: TdxLayoutItem;
    eMailParticular: TcxDBTextEdit;
    dxLayoutControl1Item7: TdxLayoutItem;
    eMailTrabajo: TcxDBTextEdit;
    cxDBMemo1: TcxDBMemo;
    dxLayoutControl1Item8: TdxLayoutItem;
    dxLayoutControl1Item9: TdxLayoutItem;
    eTlfParticular: TcxDBTextEdit;
    dxLayoutControl1Item10: TdxLayoutItem;
    eTlfTrabajo: TcxDBTextEdit;
    dxLayoutControl1Item11: TdxLayoutItem;
    eTlfMovil: TcxDBTextEdit;
    dxLayoutControl1Item12: TdxLayoutItem;
    eFax: TcxDBTextEdit;
    dxLayoutControl1Item13: TdxLayoutItem;
    eNombre: TcxDBTextEdit;
    dxLayoutControl1Item14: TdxLayoutItem;
    eNIFCIF: TcxDBTextEdit;
    dxLayoutControl1Item15: TdxLayoutItem;
    memRegistroMercantil: TcxDBMemo;
    dxLayoutControl1Group10: TdxLayoutGroup;
    dxLayoutControl1Group9: TdxLayoutGroup;
    dxLayoutControl1Group11: TdxLayoutGroup;
    ActionList1: TActionList;
    actAnadir: TAction;
    actEliminar: TAction;
    SmallImages: TPngImageList;
    OpenDialog1: TOpenDialog;
    cxDBImage1: TcxDBImage;
    dxLayoutControl1Item17: TdxLayoutItem;
    PngSpeedButton1: TPngSpeedButton;
    dxLayoutControl1Item19: TdxLayoutItem;
    PngSpeedButton2: TPngSpeedButton;
    dxLayoutControl1Item20: TdxLayoutItem;
    dxLayoutControl1Group12: TdxLayoutGroup;
    dxLayoutControl1Group13: TdxLayoutGroup;
    dxLayoutControl1Item18: TdxLayoutItem;
    cbFormaPago: TcxComboBox;
    dxLayoutControl1Item16: TdxLayoutItem;
    cbTipoIVA: TcxComboBox;
    procedure actAnadirExecute(Sender: TObject);
    procedure actEliminarExecute(Sender: TObject);
    procedure actEliminarUpdate(Sender: TObject);
    procedure actAnadirUpdate(Sender: TObject);
    procedure eProvinciaPropertiesInitPopup(Sender: TObject);
    procedure ePoblacionPropertiesInitPopup(Sender: TObject);
    procedure FormaPagoEditValueChanged(Sender: TObject);
    procedure TipoIVAEditValueChanged(Sender: TObject);
    procedure CustomViewShow(Sender: TObject);

  private
    FEmpresa: IBizEmpresa;
    FController : IEmpresasController;
    FProvincias : TStringList;
    FPoblaciones : TStringList;
    FFormasPago: TStringList;
    FTiposIVA: TStringList;
    function GetEmpresa: IBizEmpresa;
    procedure SetEmpresa(const Value: IBizEmpresa);
    function GetController : IEmpresasController;
    procedure SetController (const Value : IEmpresasController);
    procedure CargarProvincias;
    procedure CargarPoblaciones;

  public
    property Controller : IEmpresasController read GetController write SetController;
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
  end;

implementation
{$R *.dfm}

uses uROClasses, uROTypes, uProvinciasPoblacionesController, uStringsUtils;

{ TfrViewEmpresas }

{
******************************* TfrViewEmpresa ********************************
}
procedure TfrViewEmpresa.actAnadirExecute(Sender: TObject);
{var
  StdStream: TMemoryStream;
  StreamRO: IROStream;
}
begin
  inherited;
  cxDBImage1.LoadFromFile;

{if not OpenDialog1.Execute then
  Exit;
  try
    StdStream := TMemoryStream.Create;
    StdStream.LoadFromFile(OpenDialog1.FileName);
    StreamRO := NewROStream(StdStream,False);
    DADataSource.DataTable.Edit;
//    DADataSource.DataTable.FieldByName('LOGOTIPO').Clear;
    DADataSource.DataTable.FieldByName('LOGOTIPO').LoadFromStream(StreamRO);

    DADataSource.DataTable.Post;
  finally
    StdStream.Free;
  end;
}
end;

procedure TfrViewEmpresa.actAnadirUpdate(Sender: TObject);
begin
  inherited;
//  (Sender as TAction).Enabled := cxDBImage1.Picture.Graphic.Empty;
end;

procedure TfrViewEmpresa.actEliminarExecute(Sender: TObject);
begin
  inherited;
  cxDBImage1.Clear;

{  DADataSource.DataTable.Edit;
  DADataSource.DataTable.FieldByName('LOGOTIPO').AsVariant := Null;
  DADataSource.DataTable.Post;
}
end;

procedure TfrViewEmpresa.actEliminarUpdate(Sender: TObject);
begin
  inherited;
//  (Sender as TAction).Enabled := not cxDBImage1.Picture.Graphic.Empty;
end;

procedure TfrViewEmpresa.CargarPoblaciones;
var 
  i : integer;
   AID : Integer;
begin
  AID := StrToInt(FProvincias.Values[cbProvincia.Text]);
  with TProvinciasPoblacionesController.Create do
  begin
    try
      FPoblaciones := DarListaPoblaciones(AID);
      with cbPoblacion.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FPoblaciones.Count - 1 do
            Add(FPoblaciones.Names[i]);
        finally
          EndUpdate;
        end;
      end;
    finally
      Free;
    end;
  end;
end;

procedure TfrViewEmpresa.CargarProvincias;
var
  i : integer;
begin
  with TProvinciasPoblacionesController.Create do
  begin
    try
      FProvincias := DarListaProvincias;

      with cbProvincia.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FProvincias.Count - 1 do
            Add(FProvincias.Names[i]);
        finally
          EndUpdate;
        end;
      end;
    finally
      Free;
    end;
  end;
end;

constructor TfrViewEmpresa.Create(AOwner : TComponent);
begin
  inherited;
  FProvincias := NIL;
  FPoblaciones := NIL;
end;

procedure TfrViewEmpresa.CustomViewShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;

  //Activamos la forma de pago que tenga la empresa
  cbFormaPago.ItemIndex := 0;
  for i := 0 to FFormasPago.Count - 1 do
      if IntToStr(FEmpresa.ID_FORMA_PAGO) = FFormasPago.Values[FFormasPago.Names[i]] then
         cbFormaPago.ItemIndex := i;

  cbFormaPago.Properties.OnEditValueChanged := FormaPagoEditValueChanged;

  //Activamos el tipo iva que tenga la empresa
  cbTipoIVA.ItemIndex := 0;
  for i := 0 to FTiposIVA.Count - 1 do
      if IntToStr(FEmpresa.ID_TIPO_IVA) = FTiposIVA.Values[FTiposIVA.Names[i]] then
         cbTipoIVA.ItemIndex := i;

  cbTipoIVA.Properties.OnEditValueChanged := TipoIVAEditValueChanged;
end;

destructor TfrViewEmpresa.Destroy;
begin
  FController := Nil;

  if Assigned(FTiposIVA) then
    FreeANDNIL(FTiposIVA);

  if Assigned(FFormasPago) then
    FreeANDNIL(FFormasPago);

  if Assigned(FProvincias) then
    FreeANDNIL(FProvincias);

  if Assigned(FPoblaciones) then
    FreeANDNIL(FPoblaciones);

  inherited;
end;

procedure TfrViewEmpresa.ePoblacionPropertiesInitPopup(Sender: TObject);
begin
  ShowHourglassCursor;
  try
    FreeANDNIL(FPoblaciones);
    if not Assigned(FProvincias) then
      CargarProvincias;
    if not EsCadenaVacia(cbProvincia.Text) and (FProvincias.IndexOfName(cbProvincia.Text) <> -1) then
      CargarPoblaciones
  finally
    HideHourglassCursor;
  end;
end;

procedure TfrViewEmpresa.eProvinciaPropertiesInitPopup(Sender: TObject);
begin
  ShowHourglassCursor; 
  try
    if not Assigned(FProvincias) then
      CargarProvincias;
  finally
    HideHourglassCursor;
  end;
end;

procedure TfrViewEmpresa.FormaPagoEditValueChanged(Sender: TObject);
var
 IDFormaPago: String;
begin
  IDFormaPago := '';
  if Assigned(FFormasPago) then
     IDFormaPago := FFormasPago.Values[cbFormaPago.EditValue];

  Controller.AsignarIDFormaPago(FEmpresa, IDFormaPago);
end;

function TfrViewEmpresa.GetController: IEmpresasController;
begin
  Result := FController;
end;

function TfrViewEmpresa.GetEmpresa: IBizEmpresa;
begin
  Result := FEmpresa;
end;

procedure TfrViewEmpresa.SetController(const Value: IEmpresasController);
var
  i: Integer;
begin
  FController := Value;

  if Assigned(FController) then
  begin
    FFormasPago := FController.DarListaFormasPago;
    if Assigned(FFormasPago) then
      with cbFormaPago.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FFormasPago.Count - 1 do
              Add(FFormasPago.Names[i]);
        finally
          EndUpdate;
        end;
      end;

    FTiposIVA := FController.DarListaTiposIVA;
    if Assigned(FTiposIVA) then
      with cbTipoIVA.Properties.Items do
      begin
        BeginUpdate;
        try
          Clear;
          for i := 0 to FTiposIVA.Count - 1 do
              Add(FTiposIVA.Names[i]);
        finally
          EndUpdate;
        end;
      end;
  end;
end;

procedure TfrViewEmpresa.SetEmpresa(const Value: IBizEmpresa);
begin
  FEmpresa := Value;
  if Assigned(FEmpresa) then
    DADataSource.DataTable := FEmpresa.DataTable
  else
    DADataSource.DataTable := NIL;
end;

procedure TfrViewEmpresa.TipoIVAEditValueChanged(Sender: TObject);
var
 IDTipoIVA: String;
begin
  IDTipoIVA := '';
  if Assigned(FTiposIVA) then
     IDTipoIVA := FTiposIVA.Values[cbTipoIVA.EditValue];

  Controller.AsignarIDTipoIVA(FEmpresa, IDTipoIVA);
end;

initialization
  RegisterClass(TfrViewEmpresa);

finalization
  UnRegisterClass(TfrViewEmpresa);

end.

