unit CalendarioRecepciones;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, CalendarioBasico, Menus, cxStyles, cxGraphics, cxEdit,
  cxScheduler, cxSchedulerStorage, cxSchedulerCustomControls,
  cxSchedulerCustomResourceView, cxSchedulerDayView,
  cxSchedulerDateNavigator, cxSchedulerWeekView, ActnList,
  cxSchedulerDBStorage, cxControls, RdxBotones, ExtCtrls, RdxPaneles,
  RdxBarras, DB, IBCustomDataSet, dxPSGlbl, dxPSUtl, dxPSEngn, dxPrnPg,
  dxBkgnd, dxWrap, dxPrnDev, dxPSCompsProvider, dxPSFillPatterns,
  dxPSEdgePatterns, dxPrnDlg, dxPSCore, dxPScxCommon, 
  cxSchedulerTimeGridView, cxSchedulerUtils, cxSchedulerYearView,
  dxPScxScheduler2Lnk, ImgList, Grids, DBGrids;

type
  TfrCalendarioRecepciones = class(TfrCalendarioBasico)
    dsCitas: TDataSource;
    TablaCitas: TIBDataSet;
    pnlTipoCalendario: TPanel;
    bCocinasBanos: TRdxBotonLateral;
    bArmarios: TRdxBotonLateral;
    ImageList1: TImageList;
    actCocinas: TAction;
    actArmarios: TAction;
    procedure RdxFrameShow(Sender: TObject);
    procedure cxSchedulerGetEventDisplayText(Sender: TObject;
      AEvent: TcxSchedulerControlEvent; var AText: String);
    procedure cxSchedulerGetEventHintText(Sender: TObject;
      AEvent: TcxSchedulerControlEvent; var AText: String);
    procedure actCocinasExecute(Sender: TObject);
    procedure actArmariosExecute(Sender: TObject);
  private
    procedure TablaCitasBeforePost(DataSet: TDataSet);
    procedure TablaCitasAfterPost(DataSet: TDataSet);
    procedure TablaCitasFilterRecord(DataSet: TDataSet; var Accept: Boolean);
  protected
    procedure InicializarEvento (var aEvent : TcxSchedulerControlEvent); override;
    procedure InicializarCampos; override;
  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
  end;

var
  frCalendarioRecepciones: TfrCalendarioRecepciones;

implementation

uses BaseDatos, TablaCitas, TablaDocumentos;

{$R *.dfm}

{ TfrCalendarioRecepciones }

constructor TfrCalendarioRecepciones.Create(AOwner: TComponent);
begin
  inherited;
  dsCitas.DataSet := TablaCitas;

  with TablaCitas do
  begin
    Database := dmBaseDatos.BD;
    Transaction := dmBaseDatos.Transaccion;
    SelectSQL.Assign(dmTablaCitas.sqlConsultarRecepciones);
    SelectSQL.Add('WHERE TIPOTAREA IN (');
    SelectSQL.Add('''' + TextoTipoCita[Ord(tcRecepcion)] + ''', ');
    SelectSQL.Add('''' + TextoTipoCita[Ord(tcAviso)] + ''', ');
    SelectSQL.Add('''' + TextoTipoCita[Ord(tcEntregaCliente)] + ''')');

    InsertSQL.Assign(dmTablaCitas.sqlInsertar);
    ModifySQL.Assign(dmTablaCitas.sqlModificar);
    DeleteSQL.Assign(dmTablaCitas.sqlEliminar);

    RefreshSQL.Assign(dmTablaCitas.sqlRefrescar);
    RefreshSQL.Add('and TIPOTAREA IN (');
    RefreshSQL.Add('''' + TextoTipoCita[Ord(tcRecepcion)] + ''', ');
    RefreshSQL.Add('''' + TextoTipoCita[Ord(tcAviso)] + ''', ');
    RefreshSQL.Add('''' + TextoTipoCita[Ord(tcEntregaCliente)] + ''')');

    BeforePost := TablaCitasBeforePost;
    AfterPost := TablaCitasAfterPost;
    Prepare;
    OnFilterRecord := TablaCitasFilterRecord;
    Filtered := True;
  end;
end;

destructor TfrCalendarioRecepciones.Destroy;
begin
  TablaCitas.Close;
  inherited;
end;

procedure TfrCalendarioRecepciones.InicializarCampos;
var
  aField : TcxSchedulerDBStorageField;
begin
  inherited;
  Exit;
  with cxSchedulerDBStorage.CustomFields do
  begin
    try
      BeginUpdate;
      Clear;

      aField := Add;
      aField.FieldName := 'TIPOTAREA';

      aField := Add;
      aField.FieldName := 'CODIGOCONTRATO';

      aField := Add;
      aField.FieldName := 'NOMBRECLIENTE';

      aField := Add;
      aField.FieldName := 'PARTIDA';

      aField := Add;
      aField.FieldName := 'COMPLETADA';

      aField := Add;
      aField.FieldName := 'NOMBREPROVEEDOR';
    finally
      EndUpdate;
    end;
  end;
end;

procedure TfrCalendarioRecepciones.InicializarEvento(
  var aEvent: TcxSchedulerControlEvent);
begin
  inherited;
  with AEvent do
  begin
    SetCustomFieldValueByName('TIPOTAREA', TextoTipoCita[Ord(tcRecepcion)]);
    AllDayEvent := True;
  end;
end;

procedure TfrCalendarioRecepciones.TablaCitasAfterPost(DataSet: TDataSet);
begin
  dmBaseDatos.Transaccion.CommitRetaining;
end;

procedure TfrCalendarioRecepciones.TablaCitasBeforePost(DataSet: TDataSet);
begin
  if (DataSet.FieldByName('ID').AsVariant = -1) or
     (VarIsNull(DataSet.FieldByName('ID').AsVariant)) then
  begin
    DataSet.Edit;
    DataSet.FieldByName('ID').AsVariant := dmTablaCitas.DarNuevoCodigo;
  end;
end;

procedure TfrCalendarioRecepciones.RdxFrameShow(Sender: TObject);
begin
  inherited;
  try
    cxScheduler.BeginUpdate;
    TablaCitas.Open;
    cxSchedulerDBStorage.DataSource := dsCitas;
  finally;
    cxScheduler.EndUpdate;
  end;
  actMes.Execute;
end;

procedure TfrCalendarioRecepciones.cxSchedulerGetEventDisplayText(
  Sender: TObject; AEvent: TcxSchedulerControlEvent; var AText: String);
var
  Nombre : String;
  Proveedor : String;
  Tipo : String;
begin
  inherited;

  if not VarIsNull(AEvent.GetCustomFieldValueByName('NOMBRECLIENTE')) then
  begin
    Nombre := VarToStr(AEvent.GetCustomFieldValueByName('NOMBRECLIENTE'));
    Proveedor := VarToStr(AEvent.GetCustomFieldValueByName('NOMBREPROVEEDOR'));
    Tipo := VarToStr(AEvent.GetCustomFieldValueByName('TIPOTAREA')) + ' ' + VarToStr(AEvent.GetCustomFieldValueByName('PARTIDA'));
    AText := Nombre + ' - ' + Tipo + ' (' + Proveedor + ')';
  end;
end;

procedure TfrCalendarioRecepciones.cxSchedulerGetEventHintText(
  Sender: TObject; AEvent: TcxSchedulerControlEvent; var AText: String);
var
  Nombre : String;
  Proveedor : String;
  Tipo : String;
begin
  inherited;

  if not VarIsNull(AEvent.GetCustomFieldValueByName('NOMBRECLIENTE')) then
  begin
    Nombre := VarToStr(AEvent.GetCustomFieldValueByName('NOMBRECLIENTE'));
    Proveedor := VarToStr(AEvent.GetCustomFieldValueByName('NOMBREPROVEEDOR'));
    Tipo := VarToStr(AEvent.GetCustomFieldValueByName('TIPOTAREA')) + ' ' + VarToStr(AEvent.GetCustomFieldValueByName('PARTIDA'));
    AText := Nombre + #10#13 + Tipo + #10#13 + Proveedor;
  end;
end;

procedure TfrCalendarioRecepciones.TablaCitasFilterRecord(
  DataSet: TDataSet; var Accept: Boolean);
begin
  if bCocinasBanos.Down then
    Accept := (Pos(DataSet.FieldByName('CODIGODOCUMENTO').AsString, 'MCOCINA,MBANO') > 0)
  else
    Accept := (Pos(DataSet.FieldByName('CODIGODOCUMENTO').AsString, 'ARMARIOS') > 0)
end;

procedure TfrCalendarioRecepciones.actCocinasExecute(Sender: TObject);
begin
  inherited;
  TablaCitas.Filtered := False;
  TablaCitas.Filtered := True;
end;

procedure TfrCalendarioRecepciones.actArmariosExecute(Sender: TObject);
begin
  inherited;
  TablaCitas.Filtered := False;
  TablaCitas.Filtered := True;
end;

end.
