unit uViewProveedorArticulo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, uViewBase, cxGraphics, DB, uDAInterfaces, uDADataTable, cxMaskEdit,
  cxDropDownEdit, cxLookupEdit, cxDBLookupEdit, cxDBLookupComboBox,
  dxLayoutControl, cxContainer, cxEdit, cxTextEdit, cxDBEdit, cxControls,
  cxSpinEdit, cxCurrencyEdit, uProveedoresController, uBizContactos, cxCheckBox;

type
  TfrViewProveedorArticulo = class(TfrViewBase)
    dxLayoutControlArticulo: TdxLayoutControl;
    eReferenciaProv: TcxDBTextEdit;
    dxLayoutGroup1: TdxLayoutGroup;
    dxLayoutControlArticuloGroup3: TdxLayoutGroup;
    dxLayoutGroup2: TdxLayoutGroup;
    dxLayoutControlArticuloItem3: TdxLayoutItem;
    dxLayoutControlArticuloItem1: TdxLayoutItem;
    eProveedor: TcxDBLookupComboBox;
    dsProveedores: TDADataSource;
    dxLayoutControlArticuloItem2: TdxLayoutItem;
    ePrecioCoste: TcxDBCurrencyEdit;
    dxLayoutControlArticuloItem4: TdxLayoutItem;
    eDescuento: TcxDBSpinEdit;
    dxLayoutControlArticuloItem5: TdxLayoutItem;
    ePrecioNeto: TcxDBCurrencyEdit;
    dxLayoutControlArticuloItem6: TdxLayoutItem;
    ePrecioPorte: TcxDBCurrencyEdit;
    dxLayoutControlArticuloItem7: TdxLayoutItem;
    cbTiendaWeb: TcxDBCheckBox;
    procedure CustomViewCreate(Sender: TObject);
    procedure CustomViewDestroy(Sender: TObject);
    procedure OnProveedorChange(Sender: TObject);
    procedure PrecioNetoChanged(Sender: TObject);
    procedure CustomViewShow(Sender: TObject);
  protected
    FProveedoresController : IProveedoresController;
    FProveedores : IBizProveedor;
  public
    { Public declarations }
  end;

var
  frViewProveedorArticulo: TfrViewProveedorArticulo;

implementation
{$R *.dfm}

procedure TfrViewProveedorArticulo.CustomViewCreate(Sender: TObject);
begin
  inherited;
  FProveedoresController := TProveedoresController.Create;
  FProveedores := (FProveedoresController.BuscarTodos as IBizProveedor);
  if Assigned(FProveedores) then
  begin
    dsProveedores.DataTable := FProveedores.DataTable;
    FProveedores.DataTable.Active := True;
  end;
end;

procedure TfrViewProveedorArticulo.CustomViewDestroy(Sender: TObject);
begin
  ePrecioCoste.Properties.OnEditValueChanged := NIL;
  eDescuento.Properties.OnEditValueChanged := NIL;
  eProveedor.Properties.OnEditValueChanged := NIL;
  FProveedoresController := NIL;
  FProveedores := NIL;
  inherited;
end;

procedure TfrViewProveedorArticulo.CustomViewShow(Sender: TObject);
begin
  inherited;
  eProveedor.Properties.OnEditValueChanged := OnProveedorChange;
  ePrecioCoste.Properties.OnEditValueChanged := PrecioNetoChanged;
  eDescuento.Properties.OnEditValueChanged := PrecioNetoChanged;
end;

procedure TfrViewProveedorArticulo.OnProveedorChange(Sender: TObject);
begin
  inherited;
  FProveedores.first;
  if FProveedores.Locate('ID', eProveedor.EditValue) then
  begin
    eDescuento.EditValue := FProveedores.DESCUENTO;
    cbTiendaWeb.DataBinding.Field.AsInteger := FProveedores.TIENDA_WEB;
  end;
end;

procedure TfrViewProveedorArticulo.PrecioNetoChanged(Sender: TObject);
begin
  ePrecioNeto.EditValue := ePrecioCoste.EditValue - ((ePrecioCoste.EditValue * eDescuento.EditValue)/100)
end;

end.
