unit uDialogListaFacturasEnvioEMail;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uDialogBase, ActnList, StdCtrls, ExtCtrls, ComCtrls,
  uDialogListaEnvioEMail, uIDialogListaFacturasEnvioEMail, uFacturasClienteController,
  uBizFacturasCliente;

type
  TfDialogListaFacturasEnvioEMail = class(TfDialogListaEnvioEMail, IDialogListaFacturasEnvioEMail)
    procedure actCancelarExecute(Sender: TObject);
    procedure actAceptarUpdate(Sender: TObject);
    procedure actAceptarExecute(Sender: TObject);
  private
    FFacturas : IBizFacturaCliente;
    FController : IFacturasClienteController;
  protected
    function GetFacturas: IBizFacturaCliente;
    procedure SetFacturas(const Value: IBizFacturaCliente);

    procedure RellenarLista;
    procedure BorrarLista;
  public
    constructor Create(AOwner: TComponent); override;
    property Facturas: IBizFacturaCliente read GetFacturas write SetFacturas;
  end;

implementation

uses
  schFacturasClienteClient_Intf, uStringsUtils, cxControls;

{$R *.dfm}


{ TfDialogListaFacturasEnvioEMail }

procedure TfDialogListaFacturasEnvioEMail.actAceptarExecute(Sender: TObject);
var
  i : Integer;
begin
  ShowHourglassCursor;
  Application.ProcessMessages;
  try
    FFacturas.First;
    try
      for i := 0 to FFacturas.DataTable.RecordCount - 1 do
      begin
        if EsCadenaVacia(FFacturas.Cliente.EMAIL_ADMINISTRACION) then
          Continue;

        if FController.EnviarFacturaPorEMail(FFacturas, True, FFacturas.Cliente.EMAIL_ADMINISTRACION) then
        begin
          ListView1.Items[i].SubItems[2] := 'Correo generado';
          ModalResult := mrOk;
        end
        else
          ListView1.Items[i].SubItems[2] := 'Error al generar correo';
        Self.Update;
        Application.ProcessMessages;
        FFacturas.Next;
      end;
      Button2.Enabled := False;
    finally
      ListView1.Items.EndUpdate;
    end;
  finally
    HideHourglassCursor;
  end;

  Button1.SetFocus;
end;

procedure TfDialogListaFacturasEnvioEMail.actAceptarUpdate(Sender: TObject);
begin
  inherited;
  (Sender as TAction).Enabled := (ListView1.Items.Count > 0)
end;

procedure TfDialogListaFacturasEnvioEMail.actCancelarExecute(Sender: TObject);
begin
  inherited;
  ModalResult := mrOk;
end;

procedure TfDialogListaFacturasEnvioEMail.BorrarLista;
begin
  ListView1.Items.Clear;
end;

constructor TfDialogListaFacturasEnvioEMail.Create(AOwner: TComponent);
begin
  inherited;
  ModalResult := mrCancel;
  FController := TFacturasClienteController.Create;
end;

function TfDialogListaFacturasEnvioEMail.GetFacturas: IBizFacturaCliente;
begin
  Result := FFacturas;
end;

procedure TfDialogListaFacturasEnvioEMail.RellenarLista;
var
  i : Integer;
begin
  ListView1.Items.BeginUpdate;
  ListView1.Items.Clear;

  FFacturas.First;
  FFacturas.DataTable.DisableControls;
  try
    for i := 0 to FFacturas.DataTable.RecordCount - 1 do
    begin
      FController.RecuperarCliente(FFacturas);
      FFacturas.Cliente.DataTable.Active := True;

      with ListView1.Items.Add do
      begin
        Caption := FFacturas.REFERENCIA;
        SubItems.Add(FFacturas.NOMBRE);
        SubItems.Add(FFacturas.Cliente.EMAIL_ADMINISTRACION);

        if EsCadenaVacia(FFacturas.Cliente.EMAIL_ADMINISTRACION) then
          SubItems.Add('Sin dirección e-mail administrativa')
        else
          SubItems.Add('')
      end;
      FFacturas.Next;
    end;
  finally
    FFacturas.DataTable.EnableControls;
    ListView1.Items.EndUpdate;
  end;
end;

procedure TfDialogListaFacturasEnvioEMail.SetFacturas(
  const Value: IBizFacturaCliente);
begin
  FFacturas := Value;

  if Assigned(FFacturas) then
  begin
    FFacturas.DataTable.Open;
    RellenarLista;
  end
  else
    BorrarLista;
end;

end.
