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

unit uEditorCambioReferenciaGenerica;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uBizReferenciaGenerica, StdCtrls, JvExControls, JvComponent,
  JvgWizardHeader, ExtCtrls, cxStyles, cxCustomData, cxGraphics, cxFilter,
  cxData, cxDataStorage, cxEdit, DB, cxDBData, uDADataTable, cxGridLevel,
  cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGrid, cxGridBandedTableView,
  cxGridDBBandedTableView, cxCurrencyEdit, cxSpinEdit;

type
  IEditorCambioReferenciaGenerica = interface
  ['{11A66FAC-E782-43DE-A3E6-68473F351F43}']
    function GetItems : IBizReferenciaGenerica;
    procedure SetItems (const AValue : IBizReferenciaGenerica);
    property Items : IBizReferenciaGenerica read GetItems write SetItems;
  end;

  TfEditorCambioReferenciaGenerica = class(TForm, IEditorCambioReferenciaGenerica)
    bAceptar: TButton;
    bCancelar: TButton;
    JvgWizardHeader1: TJvgWizardHeader;
    Bevel1: TBevel;
    bComprobar: TButton;
    dsDataSource: TDADataSource;
    cxStyleRepository1: TcxStyleRepository;
    cxStyleEditable: TcxStyle;
    cxStyleNoEditable: TcxStyle;
    cxStyleProductor: TcxStyle;
    cxStyleNoEncontrado: TcxStyle;
    Label1: TLabel;
    lblImporteTotal: TLabel;
    procedure FormShow(Sender: TObject);
    procedure cxGridViewCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure bComprobarClick(Sender: TObject);
    procedure cxGridViewPRODUCTOStylesGetContentStyle(
      Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
      AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
    procedure bAceptarClick(Sender: TObject);
  private
    FItems : IBizReferenciaGenerica;
    FCodigoBarras : String;
    FHayErrores : Boolean;
  protected
    function GetItems : IBizReferenciaGenerica;
    procedure SetItems (const AValue : IBizReferenciaGenerica);
  public
    property Items : IBizReferenciaGenerica read GetItems write SetItems;
  end;


implementation

uses
  uEditorUtils, uDataModuleReferenciaGenerica,
  schReferenciaGenericaClient_Intf;

{$R *.dfm}

function ShowEditorCambioReferenciaGenerica (ABizObject : TDADataTableRules) : TModalResult;
var
  AEditor: TfEditorCambioReferenciaGenerica;
begin
  AEditor := TfEditorCambioReferenciaGenerica.Create(Application);
  try
    AEditor.Items := (ABizObject as IBizReferenciaGenerica);
    Result := AEditor.ShowModal;
    if Result = mrOk then
    begin
      {(ABizObject as IBizReferenciaGenerica).Edit;
      (ABizObject as IBizReferenciaGenerica).CODIGO := AEditor.Codigo;
      (ABizObject as IBizReferenciaGenerica).TALLA := AEditor.Talla;
      (ABizObject as IBizReferenciaGenerica).COLOR := AEditor.Color;
      (ABizObject as IBizReferenciaGenerica).CODIGO_BARRA := AEditor.CodigoBarras;
      (ABizObject as IBizReferenciaGenerica).PRODUCTO := AEditor.Label3.Caption;
      (ABizObject as IBizReferenciaGenerica).FECHACAMBIO := Now;
      (ABizObject as IBizReferenciaGenerica).ESTADO := rgResuelto;
      (ABizObject as IBizReferenciaGenerica).Post;
      (ABizObject as IBizReferenciaGenerica).DataTable.ApplyUpdates;}
    end;

{    if Result = mrIgnore then
    begin
      (ABizObject as IBizReferenciaGenerica).Delete;
      (ABizObject as IBizReferenciaGenerica).DataTable.ApplyUpdates;
    end;}

  finally
    AEditor.Release;
  end;
end;


{ TfEditorCambioReferenciaGenerica }

procedure TfEditorCambioReferenciaGenerica.FormShow(Sender: TObject);
begin
  FCodigoBarras := '';
  FHayErrores := False;
  FItems.DataTable.Open;
  cxGridView.DataController.GotoFirst;
end;

function TfEditorCambioReferenciaGenerica.GetItems: IBizReferenciaGenerica;
begin
  Result := FItems;
end;

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

procedure TfEditorCambioReferenciaGenerica.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 TfEditorCambioReferenciaGenerica.bComprobarClick(
  Sender: TObject);
var
  Descripcion : String;
  CodigoBarras : String;
  bEncontrado : Boolean;
  i : Integer;
  ABookmark : Pointer;
begin
  FHayErrores := False;
  FItems.DataTable.DisableControls;
  ABookmark := FItems.DataTable.GetBookMark;
  try
    FItems.First;
    for i := 0 to FItems.RecordCount - 1 do
    begin
      bEncontrado := dmReferenciaGenerica.GetProducto(FItems.CODIGO, FItems.TALLA,
        FItems.COLOR, CodigoBarras, Descripcion);
      FItems.Edit;
      if bEncontrado and (Pos('ERROR REFERENCIA', Descripcion) = 0) then
      begin
        FItems.PRODUCTO := Descripcion;
        FItems.CODIGO_BARRA := CodigoBarras;
        FItems.Estado := rgResuelto;
        FItems.FECHACAMBIO := Now;
      end
      else begin
        FHayErrores := True;
        FItems.PRODUCTO := '*** PRODUCTO NO ENCONTRADO ***';
      end;
      FItems.Post;
      FItems.Next;
    end;
  finally
    FItems.DataTable.EnableControls;
    FItems.DataTable.GotoBookmark(ABookmark);
    FItems.DataTable.FreeBookmark(ABookmark);
  end;
end;

procedure TfEditorCambioReferenciaGenerica.cxGridViewPRODUCTOStylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
begin
  if ARecord.Values[AItem.Index] = '*** PRODUCTO NO ENCONTRADO ***' then
    AStyle := cxStyleNoEncontrado;
end;

procedure TfEditorCambioReferenciaGenerica.bAceptarClick(Sender: TObject);
begin
  bComprobar.Click;
  if FHayErrores then
    if (Application.MessageBox('Hay códigos de producto que no se han encontrado o son incorrectos.' + #10#13 +
      '¿Desea guardar los cambios de aquellos que están bien?', 'Atención', MB_YESNO) = IDNO) then
        Exit;
  FItems.DataTable.ApplyUpdates;
  Close;
end;

initialization
  RegisterEditor(IBizReferenciaGenerica, ShowEditorCambioReferenciaGenerica, etItem);

finalization

end.

