{*******************************************************}
{                                                       }
{       Administración de puntos de venta               }
{                                                       }
{       Copyright (C) 2006 Rodax Software S.L.          }
{                                                       }
{*******************************************************}

unit uEditorAjustarRecepciones;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvExControls, JvComponent,
  JvgWizardHeader, ExtCtrls, cxStyles, cxCustomData, uBizRecepciones,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxCurrencyEdit, uDADataTable, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, dxLayoutControl,
  cxContainer, cxTextEdit, cxDBEdit, cxMemo, cxMaskEdit, cxDropDownEdit,
  cxCheckBox, cxCalendar, cxLabel, cxDBLabel;

type
  IEditorAjustarRecepciones = interface
  ['{549191AB-7CF0-464A-872B-ECBCD0088A25}']
    function GetItems : IBizRecepciones;
    procedure SetItems (const AValue : IBizRecepciones);
    property Items : IBizRecepciones read GetItems write SetItems;
  end;

  TfEditorAjustarRecepciones = class(TForm, IEditorAjustarRecepciones)
    bAceptar: TButton;
    bCancelar: TButton;
    JvgWizardHeader1: TJvgWizardHeader;
    Bevel1: TBevel;
    dsDataSource: TDADataSource;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Group1: TdxLayoutGroup;
    dxLayoutControl1Group2: TdxLayoutGroup;
    dxLayoutControl1Group3: TdxLayoutGroup;
    dxLayoutControl1Group4: TdxLayoutGroup;
    dxLayoutControl1Group5: TdxLayoutGroup;
    dxLayoutControl1Item9: TdxLayoutItem;
    Bevel2: TBevel;
    dxLayoutControl1Group8: TdxLayoutGroup;
    dxLayoutControl1Item10: TdxLayoutItem;
    edtEnv40: TcxDBMaskEdit;
    dxLayoutControl1Group9: TdxLayoutGroup;
    dxLayoutControl1Item11: TdxLayoutItem;
    edtEnv38: TcxDBMaskEdit;
    dxLayoutControl1Item12: TdxLayoutItem;
    edtEnv42: TcxDBMaskEdit;
    dxLayoutControl1Item13: TdxLayoutItem;
    edtEnv44: TcxDBMaskEdit;
    dxLayoutControl1Item14: TdxLayoutItem;
    edtEnv46: TcxDBMaskEdit;
    dxLayoutControl1Item15: TdxLayoutItem;
    edtEnv48: TcxDBMaskEdit;
    dxLayoutControl1Item16: TdxLayoutItem;
    Label1: TLabel;
    dxLayoutControl1Item2: TdxLayoutItem;
    edtRec38: TcxDBMaskEdit;
    dxLayoutControl1Item3: TdxLayoutItem;
    edtRec40: TcxDBMaskEdit;
    dxLayoutControl1Item4: TdxLayoutItem;
    edtRec42: TcxDBMaskEdit;
    dxLayoutControl1Item20: TdxLayoutItem;
    edtRec44: TcxDBMaskEdit;
    dxLayoutControl1Item21: TdxLayoutItem;
    edtRec46: TcxDBMaskEdit;
    dxLayoutControl1Item22: TdxLayoutItem;
    edtRec48: TcxDBMaskEdit;
    dxLayoutControl1Item23: TdxLayoutItem;
    Label2: TLabel;
    dxLayoutControl1Item24: TdxLayoutItem;
    edtRecR48: TcxCurrencyEdit;
    dxLayoutControl1Item25: TdxLayoutItem;
    edtRecR46: TcxCurrencyEdit;
    dxLayoutControl1Item26: TdxLayoutItem;
    edtRecR44: TcxCurrencyEdit;
    dxLayoutControl1Item27: TdxLayoutItem;
    edtRecR42: TcxCurrencyEdit;
    dxLayoutControl1Item28: TdxLayoutItem;
    edtRecR40: TcxCurrencyEdit;
    dxLayoutControl1Item29: TdxLayoutItem;
    edtRecR38: TcxCurrencyEdit;
    dxLayoutControl1Group10: TdxLayoutGroup;
    dxLayoutControl1Group11: TdxLayoutGroup;
    dxLayoutControl1Group12: TdxLayoutGroup;
    dxLayoutControl1Group13: TdxLayoutGroup;
    dxLayoutControl1Group14: TdxLayoutGroup;
    dxLayoutControl1Group15: TdxLayoutGroup;
    dxLayoutControl1Item30: TdxLayoutItem;
    Label3: TLabel;
    dxLayoutControl1Group16: TdxLayoutGroup;
    edtFecha: TcxDBLabel;
    dxLayoutControl1Item31: TdxLayoutItem;
    edtCentro: TcxDBLabel;
    dxLayoutControl1Item17: TdxLayoutItem;
    edtProducto: TcxDBLabel;
    dxLayoutControl1Item32: TdxLayoutItem;
    edtColor: TcxDBLabel;
    dxLayoutControl1Item33: TdxLayoutItem;
    edtAlbaran: TcxDBLabel;
    dxLayoutControl1Item1: TdxLayoutItem;
    cbEstadoAlbaran: TcxComboBox;
    dxLayoutControl1Item5: TdxLayoutItem;
    memEstadoAlbaran: TcxMemo;
    dxLayoutControl1Item6: TdxLayoutItem;
    cbContabilizarAlbaran: TcxCheckBox;
    dxLayoutControl1Item8: TdxLayoutItem;
    dxLayoutControl1Group7: TdxLayoutGroup;
    dxLayoutControl1Group6: TdxLayoutGroup;
    procedure FormShow(Sender: TObject);
    procedure cxGridViewCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure bAceptarClick(Sender: TObject);
  private
    FItems : IBizRecepciones;
    FHayErrores : Boolean;
    FAlbaranModificado : Boolean;    
    procedure CargarValores;
    procedure SalvarValores;
    procedure ModificarAlbaran;
  protected
    function GetItems : IBizRecepciones;
    procedure SetItems (const AValue : IBizRecepciones);
  public
    property Items : IBizRecepciones read GetItems write SetItems;
  end;


implementation

uses
  uEditorUtils, uDataModuleRecepciones;

{$R *.dfm}

function ShowEditorAjustarRecepciones (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorAjustarRecepciones;
begin
  AEditor := TfEditorAjustarRecepciones.Create(Application);
  try
    AEditor.Items := (ABizObject as IBizRecepciones);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;


{ TfEditorCambioReferenciaGenerica }

procedure TfEditorAjustarRecepciones.FormShow(Sender: TObject);
begin
  FHayErrores := False;
  FAlbaranModificado := False;
  FItems.DataTable.Open;

  cbEstadoAlbaran.Enabled := Length(FItems.DataTable.FieldByName('ALBARAN').AsString) > 0;
  cbContabilizarAlbaran.Enabled := cbEstadoAlbaran.Enabled;
  memEstadoAlbaran.Enabled := cbEstadoAlbaran.Enabled;

  CargarValores;
end;

function TfEditorAjustarRecepciones.GetItems: IBizRecepciones;
begin
  Result := FItems;
end;

procedure TfEditorAjustarRecepciones.SetItems(
  const AValue: IBizRecepciones);
begin
  FItems := AValue;
  if Assigned(FItems) then
    dsDataSource.DataTable := FItems.DataTable; 
end;

procedure TfEditorAjustarRecepciones.cxGridViewCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if ((AViewInfo.Item as TcxGridDBBandedColumn).Position.RowIndex = 1) then
  begin
    ADone := True;
    with AViewInfo do begin
      ACanvas.FillRect(Bounds);
      ACanvas.DrawTexT(Text, TextAreaBounds, 0);
      ACanvas.FrameRect(Bounds, BorderColor[bBottom], 1, [bBottom]);
    end;
  end;
end;

procedure TfEditorAjustarRecepciones.bAceptarClick(Sender: TObject);
begin
  ShowHourglassCursor;
  try
    SalvarValores;
    FItems.DataTable.ApplyUpdates;
    if FAlbaranModificado then
      ModificarAlbaran;
  finally
    HideHourglassCursor;
  end;
  Close;
end;

procedure TfEditorAjustarRecepciones.CargarValores;
begin
  with FItems.DataTable do
  begin
    if FieldByName('R38').AsInteger <> FieldByName('RR38').AsInteger then
      edtRecR38.Value := FieldByName('RR38').AsInteger
    else
      edtRecR38.Value := FieldByName('R38').AsInteger;

    if FieldByName('R40').AsInteger <> FieldByName('RR40').AsInteger then
      edtRecR40.Value := FieldByName('RR40').AsInteger
    else
      edtRecR40.Value := FieldByName('R40').AsInteger;

    if FieldByName('R42').AsInteger <> FieldByName('RR42').AsInteger then
      edtRecR42.Value := FieldByName('RR42').AsInteger
    else
      edtRecR42.Value := FieldByName('R42').AsInteger;

    if FieldByName('R44').AsInteger <> FieldByName('RR44').AsInteger then
      edtRecR44.Value := FieldByName('RR44').AsInteger
    else
      edtRecR44.Value := FieldByName('R44').AsInteger;

    if FieldByName('R46').AsInteger <> FieldByName('RR46').AsInteger then
      edtRecR46.Value := FieldByName('RR46').AsInteger
    else
      edtRecR46.Value := FieldByName('R46').AsInteger;

    if FieldByName('R48').AsInteger <> FieldByName('RR48').AsInteger then
      edtRecR48.Value := FieldByName('RR48').AsInteger
    else
      edtRecR48.Value := FieldByName('R48').AsInteger;

    if Length(FieldByName('ESTADO_ALBARAN').AsString) = 0 then
      cbEstadoAlbaran.Text := cbEstadoAlbaran.Properties.Items[0]
    else
      cbEstadoAlbaran.Text := FieldByName('ESTADO_ALBARAN').AsString;

    cbContabilizarAlbaran.Checked := (FieldByName('CONTABILIZAR_ALBARAN').AsString = 'S');
    memEstadoAlbaran.Lines.Clear;
    if Length(FieldByName('OBSERVACIONES_ALBARAN').AsString) > 0 then
      memEstadoAlbaran.Lines.Add(FieldByName('OBSERVACIONES_ALBARAN').AsString)
  end;
end;

procedure TfEditorAjustarRecepciones.SalvarValores;
var
  aux : String;
begin
  FAlbaranModificado := False;
  with FItems.DataTable do
  begin
    Edit;
    FieldByName('RR38').AsVariant := edtRecR38.Value;
    FieldByName('RR40').AsVariant := edtRecR40.Value;
    FieldByName('RR42').AsVariant := edtRecR42.Value;
    FieldByName('RR44').AsVariant := edtRecR44.Value;
    FieldByName('RR46').AsVariant := edtRecR46.Value;
    FieldByName('RR48').AsVariant := edtRecR48.Value;

    if cbEstadoAlbaran.Text = cbEstadoAlbaran.Properties.Items[0] then
      aux := ''
    else
      aux := cbEstadoAlbaran.Text;

    if FieldByName('ESTADO_ALBARAN').AsString <> aux then
    begin
      FAlbaranModificado := True;
      FieldByName('ESTADO_ALBARAN').AsString := aux;
    end;

    if cbContabilizarAlbaran.Checked then
      aux := 'S'
    else
      aux := 'N';

    if FieldByName('CONTABILIZAR_ALBARAN').AsString <> aux then
    begin
      FAlbaranModificado := True;
      FieldByName('CONTABILIZAR_ALBARAN').AsString := aux;
    end;

    FieldByName('OBSERVACIONES_ALBARAN').AsString := memEstadoAlbaran.Lines.Text;

    Post;
  end;
end;

procedure TfEditorAjustarRecepciones.ModificarAlbaran;
var
  bm : TBookMark ;
  AAlbaran : String;
  AEstado : String;
  AContabilizar : String;
begin
  { Si se ha cambiado el albarán, hay
    que cambiarlo en el resto de filas en las que aparezca }

  AAlbaran := FItems.DataTable.FieldByName('ALBARAN').AsString;
  AEstado := FItems.DataTable.FieldByName('ESTADO_ALBARAN').AsString;
  AContabilizar := FItems.DataTable.FieldByName('CONTABILIZAR_ALBARAN').AsString;

  bm := FItems.DataTable.GetBookMark;
  FItems.DataTable.DisableControls;
  FItems.DataTable.DisableEventHandlers;
  FItems.DataTable.LogChanges := False;
  try
    FItems.DataTable.Filter := 'ALBARAN = ''' + AAlbaran + '''';
    FItems.DataTable.Filtered := True;
    FItems.DataTable.First;

    repeat
      FItems.Edit;
      FItems.DataTable.FieldByName('ESTADO_ALBARAN').AsString := AEstado;
      FItems.DataTable.FieldByName('CONTABILIZAR_ALBARAN').AsString := AContabilizar;
      FItems.Post;
      FItems.DataTable.Next
    until FItems.DataTable.EOF;

    FItems.DataTable.GotoBookMark(bm);
  finally
    FItems.DataTable.Filter := '';
    FItems.DataTable.Filtered := False;
    FItems.DataTable.FreeBookMark(BM);
    FItems.DataTable.LogChanges := True;
    FItems.DataTable.EnableEventHandlers;
    FItems.DataTable.EnableControls;
  end;
end;

initialization
  RegisterEditor(IBizRecepciones, ShowEditorAjustarRecepciones, etItem);

finalization

end.

