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

unit uEditorAjustarTraspasos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvExControls, JvComponent,
  JvgWizardHeader, ExtCtrls, cxStyles, cxCustomData, uBizTraspasos,
  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
  IEditorAjustarTraspasos = interface
  ['{40E4185E-595B-48DF-AFA8-E86509F7354F}']
    function GetItems : IBizTraspasos;
    procedure SetItems (const AValue : IBizTraspasos);
    property Items : IBizTraspasos read GetItems write SetItems;
  end;

  TfEditorAjustarTraspasos = class(TForm, IEditorAjustarTraspasos)
    bAceptar: TButton;
    bCancelar: TButton;
    JvgWizardHeader1: TJvgWizardHeader;
    Bevel1: TBevel;
    dsDataSource: TDADataSource;
    dxLayoutControl1Group_Root: TdxLayoutGroup;
    dxLayoutControl1: TdxLayoutControl;
    dxLayoutControl1Group1: TdxLayoutGroup;
    dxLayoutControl1Group3: TdxLayoutGroup;
    dxLayoutControl1Group5: TdxLayoutGroup;
    dxLayoutControl1Item9: TdxLayoutItem;
    Bevel2: TBevel;
    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;
    edtProducto: TcxDBLabel;
    dxLayoutControl1Item32: TdxLayoutItem;
    edtColor: TcxDBLabel;
    dxLayoutControl1Item33: TdxLayoutItem;
    cbEstado: TcxComboBox;
    dxLayoutControl1Item5: TdxLayoutItem;
    memObservaciones: TcxMemo;
    dxLayoutControl1Item6: TdxLayoutItem;
    cbContabilizar: TcxCheckBox;
    dxLayoutControl1Item8: TdxLayoutItem;
    dxLayoutControl1Group17: TdxLayoutGroup;
    dxLayoutControl1Group7: TdxLayoutGroup;
    dxLayoutControl1Group6: TdxLayoutGroup;
    dxLayoutControl1Item7: TdxLayoutItem;
    Bevel3: TBevel;
    dxLayoutControl1Item1: TdxLayoutItem;
    lblCentroOrigen: TLabel;
    dxLayoutControl1Item10: TdxLayoutItem;
    lblCentroDestino: TLabel;
    dxLayoutControl1Group2: TdxLayoutGroup;
    procedure FormShow(Sender: TObject);
    procedure cxGridViewCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure bAceptarClick(Sender: TObject);
  private
    FItems : IBizTraspasos;
    FHayErrores : Boolean;
    procedure CargarValores;
    procedure SalvarValores;
  protected
    function GetItems : IBizTraspasos;
    procedure SetItems (const AValue : IBizTraspasos);
  public
    property Items : IBizTraspasos read GetItems write SetItems;
  end;


implementation

uses
  uEditorUtils, uDataModuleTraspasos;

{$R *.dfm}

function ShowEditorAjustarTraspasos (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorAjustarTraspasos;
begin
  AEditor := TfEditorAjustarTraspasos.Create(Application);
  try
    AEditor.Items := (ABizObject as IBizTraspasos);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;


{ TfEditorCambioReferenciaGenerica }

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

  CargarValores;
end;

function TfEditorAjustarTraspasos.GetItems: IBizTraspasos;
begin
  Result := FItems;
end;

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

procedure TfEditorAjustarTraspasos.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 TfEditorAjustarTraspasos.bAceptarClick(Sender: TObject);
begin
  ShowHourglassCursor;
  try
    SalvarValores;
    FItems.DataTable.ApplyUpdates;
  finally
    HideHourglassCursor;
  end;
  Close;
end;

procedure TfEditorAjustarTraspasos.CargarValores;
begin
  with FItems.DataTable do
  begin
    if FieldByName('T38').AsInteger <> FieldByName('TR38').AsInteger then
      edtRecR38.Value := FieldByName('TR38').AsInteger
    else
      edtRecR38.Value := FieldByName('T38').AsInteger;

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

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

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

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

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

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

    if FieldByName('TIPO').AsString = 'S' then
    begin
      lblCentroOrigen.Caption := FieldByName('CENTRO').AsString;
      lblCentroDestino.Caption := FieldByName('CENTRO2').AsString;
    end
    else begin
      lblCentroOrigen.Caption := FieldByName('CENTRO2').AsString;
      lblCentroDestino.Caption := FieldByName('CENTRO').AsString;
    end;

    cbContabilizar.Checked := (FieldByName('CONTABILIZAR').AsString = 'S');
    memObservaciones.Lines.Clear;
    if Length(FieldByName('OBSERVACIONES').AsString) > 0 then
      memObservaciones.Lines.Add(FieldByName('OBSERVACIONES').AsString)
  end;
end;

procedure TfEditorAjustarTraspasos.SalvarValores;
var
  aux : String;
begin
  with FItems.DataTable do
  begin
    Edit;
    FieldByName('TR38').AsVariant := edtRecR38.Value;
    FieldByName('TR40').AsVariant := edtRecR40.Value;
    FieldByName('TR42').AsVariant := edtRecR42.Value;
    FieldByName('TR44').AsVariant := edtRecR44.Value;
    FieldByName('TR46').AsVariant := edtRecR46.Value;
    FieldByName('TR48').AsVariant := edtRecR48.Value;

    if Length(memObservaciones.Lines.Text) > 0 then
      FieldByName('OBSERVACIONES').AsString := memObservaciones.Lines.Text
    else
      FieldByName('OBSERVACIONES').AsVariant := null;

    if cbContabilizar.Checked then
      FieldByName('CONTABILIZAR').AsString := 'S'
    else
      FieldByName('CONTABILIZAR').AsString := 'N';


    if cbEstado.Text = cbEstado.Properties.Items[0] then
      FieldByName('ESTADO_TRASPASO').AsVariant := null
    else
      FieldByName('ESTADO_TRASPASO').AsString := cbEstado.Text;

    Post;
  end;
end;


initialization
  RegisterEditor(IBizTraspasos, ShowEditorAjustarTraspasos, etItem);

finalization

end.

