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

unit uEditorAjustarDevoluciones;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, JvExControls, JvComponent,
  JvgWizardHeader, ExtCtrls, cxStyles, cxCustomData, uBizDevoluciones,
  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
  IEditorAjustarDevoluciones = interface
  ['{F75441C6-D0D4-42AF-ABB7-FC531222F0DD}']
    function GetItems : IBizDevoluciones;
    procedure SetItems (const AValue : IBizDevoluciones);
    property Items : IBizDevoluciones read GetItems write SetItems;
  end;

  TfEditorAjustarDevoluciones = class(TForm, IEditorAjustarDevoluciones)
    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;
    edtHoja: TcxDBLabel;
    dxLayoutControl1Item1: TdxLayoutItem;
    cbEstadoHoja: TcxComboBox;
    dxLayoutControl1Item5: TdxLayoutItem;
    memEstadoHoja: TcxMemo;
    dxLayoutControl1Item6: TdxLayoutItem;
    cbContabilizarHoja: 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 : IBizDevoluciones;
    FHayErrores : Boolean;
    FAlbaranModificado : Boolean;
    procedure CargarValores;
    procedure SalvarValores;
    procedure ModificarAlbaran;    
  protected
    function GetItems : IBizDevoluciones;
    procedure SetItems (const AValue : IBizDevoluciones);
  public
    property Items : IBizDevoluciones read GetItems write SetItems;
  end;


implementation

uses
  uEditorUtils, uDataModuleDevoluciones,
  uDAInterfaces;

{$R *.dfm}

function ShowEditorAjustarDevoluciones (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorAjustarDevoluciones;
begin
  AEditor := TfEditorAjustarDevoluciones.Create(Application);
  try
    AEditor.Items := (ABizObject as IBizDevoluciones);
    Result := AEditor.ShowModal;
  finally
    AEditor.Release;
  end;
end;


{ TfEditorCambioReferenciaGenerica }

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

  cbEstadoHoja.Enabled := Length(FItems.DataTable.FieldByName('NUMHOJA').AsString) > 0;
  cbContabilizarHoja.Enabled := cbEstadoHoja.Enabled;
  memEstadoHoja.Enabled := cbEstadoHoja.Enabled;

  CargarValores;;
end;

function TfEditorAjustarDevoluciones.GetItems: IBizDevoluciones;
begin
  Result := FItems;
end;

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

procedure TfEditorAjustarDevoluciones.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 TfEditorAjustarDevoluciones.bAceptarClick(Sender: TObject);
begin
  SalvarValores;
  FItems.DataTable.ApplyUpdates;
  ModificarAlbaran;
  Close;
end;

procedure TfEditorAjustarDevoluciones.CargarValores;
begin
  with FItems.DataTable do
  begin
    if FieldByName('E38').AsInteger <> FieldByName('ER38').AsInteger then
      edtRecR38.Value := FieldByName('ER38').AsInteger
    else
      edtRecR38.Value := FieldByName('E38').AsInteger;

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

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

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

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

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

    if Length(FieldByName('ESTADO_HOJA').AsString) = 0 then
      cbEstadoHoja.Text := cbEstadoHoja.Properties.Items[0];

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

    cbContabilizarHoja.Checked := (FieldByName('CONTABILIZAR_HOJA').AsString = 'S');
    memEstadoHoja.Lines.Clear;
    if Length(FieldByName('OBSERVACIONES_HOJA').AsString) > 0 then
      memEstadoHoja.Lines.Add(FieldByName('OBSERVACIONES_HOJA').AsString)
  end;
end;

procedure TfEditorAjustarDevoluciones.SalvarValores;
var
  aux : String;
begin
  FAlbaranModificado := False;
  with FItems.DataTable do
  begin
    Edit;
    FieldByName('ER38').AsFloat := edtRecR38.Value;
    FieldByName('ER40').AsFloat := edtRecR40.Value;
    FieldByName('ER42').AsFloat := edtRecR42.Value;
    FieldByName('ER44').AsFloat := edtRecR44.Value;
    FieldByName('ER46').AsFloat := edtRecR46.Value;
    FieldByName('ER48').AsFloat := edtRecR48.Value;

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

    if FieldByName('ESTADO_HOJA').AsString <> aux then
      FieldByName('ESTADO_HOJA').AsString := aux;

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

    if FieldByName('CONTABILIZAR_HOJA').AsString <> aux then
      FieldByName('CONTABILIZAR_HOJA').AsString := aux;

    FieldByName('OBSERVACIONES_HOJA').AsString := memEstadoHoja.Lines.Text;
    Post;
  end;
end;

procedure TfEditorAjustarDevoluciones.ModificarAlbaran;
var
  bm : TBookMark ;
  AHoja : 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 }

  AHoja := FItems.DataTable.FieldByName('NUMHOJA').AsString;
  AEstado := FItems.DataTable.FieldByName('ESTADO_HOJA').AsString;
  AContabilizar := FItems.DataTable.FieldByName('CONTABILIZAR_HOJA').AsString;

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

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

    try
      FItems.DataTable.GotoBookMark(bm);
    except
        // No hacer nada
    end;
  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(IBizDevoluciones, ShowEditorAjustarDevoluciones, etItem);

finalization

end.

