{*******************************************************}
{                                                       }
{       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, cxButtonEdit,
  cxTextEdit, cxEditRepositoryItems;

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;
    cxGrid: TcxGrid;
    cxGridView: TcxGridDBBandedTableView;
    cxGridViewRecID: TcxGridDBBandedColumn;
    cxGridViewNUMINF: TcxGridDBBandedColumn;
    cxGridViewCLAVE1: TcxGridDBBandedColumn;
    cxGridViewCLAVE2: TcxGridDBBandedColumn;
    cxGridViewCLAVE3: TcxGridDBBandedColumn;
    cxGridViewFECHA: TcxGridDBBandedColumn;
    cxGridViewCODCENTRO: TcxGridDBBandedColumn;
    cxGridViewCENTRO: TcxGridDBBandedColumn;
    cxGridViewCODVENDEDOR: TcxGridDBBandedColumn;
    cxGridViewVENDEDOR: TcxGridDBBandedColumn;
    cxGridViewNUMTALON: TcxGridDBBandedColumn;
    cxGridViewCODIGO_BARRA: TcxGridDBBandedColumn;
    cxGridViewCODIGO: TcxGridDBBandedColumn;
    cxGridViewPRODUCTO: TcxGridDBBandedColumn;
    cxGridViewCOLOR: TcxGridDBBandedColumn;
    cxGridViewTALLA: TcxGridDBBandedColumn;
    cxGridViewVTAPROCESO: TcxGridDBBandedColumn;
    cxGridViewVTAFINAL: TcxGridDBBandedColumn;
    cxGridViewVTATERM: TcxGridDBBandedColumn;
    cxGridViewVALTOT: TcxGridDBBandedColumn;
    cxGridViewESTADO: TcxGridDBBandedColumn;
    cxGridViewFECHACAMBIO: TcxGridDBBandedColumn;
    cxGridLevel: TcxGridLevel;
    lblImporteTotal: TLabel;
    cxGridViewOperacion: TcxGridDBBandedColumn;
    cxEditRepository1: TcxEditRepository;
    cxEditButtonDesglosar: TcxEditRepositoryButtonItem;
    cxEditButtonEliminarDesglose: TcxEditRepositoryButtonItem;
    cxGridViewNUMCON: TcxGridDBBandedColumn;
    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);
    procedure cxGridViewOperacionGetProperties(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AProperties: TcxCustomEditProperties);
    procedure cxEditButtonDesglosarPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxEditButtonEliminarDesglosePropertiesButtonClick(
      Sender: TObject; AButtonIndex: Integer);
  private
    FItems : IBizReferenciaGenerica;
    FCodigoBarras : String;
    FHayErrores : Boolean;
    procedure DesglosarReferencia;
  protected
    function GetItems : IBizReferenciaGenerica;
    procedure SetItems (const AValue : IBizReferenciaGenerica);
  public
    property Items : IBizReferenciaGenerica read GetItems write SetItems;
  end;


implementation

uses
  uEditorUtils, uDataModuleReferenciaGenerica,
  schReferenciaGenericaClient_Intf, Math;

{$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 VarToStrDef(AViewInfo.GridRecord.Values[cxGridViewNUMCON.Index], '0') <> '0' then
    ACanvas.Brush.Color := clInfoBk;

  if ((AViewInfo.Item as TcxGridDBBandedColumn).Position.RowIndex = 1) then
  begin
    ADone := True;
    with AViewInfo do begin
      ACanvas.FillRect(Bounds);
      ACanvas.DrawTexT(Text, TextAreaBounds, 0);

      if (AViewInfo.Item.Index = cxGridViewCOLOR.Index) or
         (AViewInfo.Item.Index = cxGridViewTALLA.Index) or
         (AViewInfo.Item.Index = cxGridViewCODIGO.Index) or
         (AViewInfo.Item.Index = cxGridViewVALTOT.Index) then
        ACanvas.FrameRect(Bounds, BorderColor[bBottom], 1, [bBottom, bTop, bLeft, bRight])
      else
        ACanvas.FrameRect(Bounds, BorderColor[bBottom], 1, [bBottom]);
    end;
  end
  else
    if (AViewInfo.Item.Index = cxGridViewVTAPROCESO.Index) or
       (AViewInfo.Item.Index = cxGridViewVTAFINAL.Index) or
       (AViewInfo.Item.Index = cxGridViewVTATERM.Index) then
    begin
        ADone := True;
      with AViewInfo do
      begin
        ACanvas.FillRect(Bounds);
        ACanvas.DrawTexT(Text, TextAreaBounds, 0);
        ACanvas.FrameRect(Bounds, BorderColor[bBottom], 1, [bBottom, bTop, bLeft, bRight]);
      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;

procedure TfEditorCambioReferenciaGenerica.cxGridViewOperacionGetProperties(
  Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
  var AProperties: TcxCustomEditProperties);
begin
  if Assigned(ARecord) then
  begin
    // Sólo aparecerá el botón '+' si alguna cantidad es mayor que 1
    if (Abs(StrToIntDef(ARecord.DisplayTexts[cxGridViewVTAPROCESO.Index], 0)) > 1) or
       (Abs(StrToIntDef(ARecord.DisplayTexts[cxGridViewVTAFINAL.Index], 0)) > 1) or
       (Abs(StrToIntDef(ARecord.DisplayTexts[cxGridViewVTATERM.Index], 0)) > 1) then
      AProperties := cxEditButtonDesglosar.Properties
    else
      if (StrToIntDef(ARecord.DisplayTexts[cxGridViewNUMCON.Index], 0) > 0) then
        AProperties := cxEditButtonEliminarDesglose.Properties
  end;
end;

procedure TfEditorCambioReferenciaGenerica.DesglosarReferencia;
begin
  FItems.DesglosarReferencia;
end;

procedure TfEditorCambioReferenciaGenerica.cxEditButtonDesglosarPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  DesglosarReferencia;
end;

procedure TfEditorCambioReferenciaGenerica.cxEditButtonEliminarDesglosePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  FItems.Delete;
end;

initialization
  RegisterEditor(IBizReferenciaGenerica, ShowEditorCambioReferenciaGenerica, etItem);

finalization

end.

