{
===============================================================================
  Copyright (©) 2007. Rodax Software.
===============================================================================
  Los contenidos de este fichero son propiedad de Rodax Software titular del
  copyright. Este fichero sólo podrá ser copiado, distribuido y utilizado,
  en su totalidad o en parte, con el permiso escrito de Rodax Software, o de
  acuerdo con los términos y condiciones establecidas en el acuerdo/contrato
  bajo el que se suministra.
  -----------------------------------------------------------------------------
  Web:  www.rodax-software.com
===============================================================================
  Fecha primera versión: 
  Versión actual: 1.0.0
  Fecha versión actual: 
===============================================================================
  Modificaciones:

    Fecha         Comentarios
    ---------------------------------------------------------------------------
===============================================================================
}

unit uDataModuleTiendaWeb;

interface

uses
  SysUtils, Classes, DB, uDAClientDataModule, uDADataTable, uDABINAdapter,
  uDAScriptingProvider, uDACDSDataTable, uROWinInetHttpChannel, uROTypes,
  uROServiceComponent, uRORemoteService, uROClient, uROBinMessage,
  uDADesigntimeCall,

  uIDataModuleTiendaWeb, uBizTiendaWeb, uBizOscAddressBook, uBizOscCustomers,
  uBizOscManufacturers, uBizOscProducts, uBizOscOrderProducts, uBizOscOrders;

type
  TDataModuleTiendaWeb = class(TDAClientDataModule, IDataModuleTiendaWeb)
    RORemoteService: TRORemoteService;
    DABinAdapter: TDABINAdapter;
    DADesigntimeCall1: TDADesigntimeCall;
    LoginRemoteService: TRORemoteService;
    ROWinInetHTTPChannel1: TROWinInetHTTPChannel;
    ROBinMessage1: TROBinMessage;
    tbl_TiendaWeb: TDACDSDataTable;
    ds_TiendaWeb: TDADataSource;
    tbl_osc_AddressBook: TDACDSDataTable;
    ds_osc_AddressBook: TDADataSource;
    tbl_osc_Customers: TDACDSDataTable;
    ds_osc_Customers: TDADataSource;
    tbl_osc_Manufacturers: TDACDSDataTable;
    ds_osc_Manufacturers: TDADataSource;
    tbl_osc_Products: TDACDSDataTable;
    ds_osc_Products: TDADataSource;
    tbl_osc_Orders: TDACDSDataTable;
    ds_osc_Orders: TDADataSource;
    tbl_osc_Orders_products: TDACDSDataTable;
    ds_osc_Orders_products: TDADataSource;
    procedure DAClientDataModuleCreate(Sender: TObject);
  protected
    function DarNombreGenerador(DataSetName: String): String; virtual;
    function _CloneDataTable (const ADataTable : TDACDSDataTable): TDACDSDataTable; overload;

    function _GetOSCAddressBook: IBizOscAddressBook;
    function _GetOSCOrderProducts: IBizOSCOrderProducts;
  public
    function GetNextID(const DataSetName : String) : Integer;
    function GetItems: IBizTiendaWeb;
    function GetItem(const ID_Empresa : Integer) : IBizTiendaWeb;
    function NewItem : IBizTiendaWeb;

    function GetOSCCustomers: IBizOSCCustomer;
    function GetOSCCustomer(const IDLocal : Integer): IBizOSCCustomer;
    function NewOSCCustomer : IBizOSCCustomer;

    function GetOSCManufacturers: IBizOSCManufacturer;
    function GetOSCManufacturer(const IDLocal : Integer): IBizOSCManufacturer;
    function NewOSCManufacturer : IBizOSCManufacturer;

    function GetOSCProducts: IBizOSCProduct;
    function GetOSCProduct(const IDLocal : Integer): IBizOSCProduct;
    function NewOSCProduct : IBizOSCProduct;

    function GetNextIDOSC(const DataSetName : String) : Integer;

    function GetOSCOrders: IBizOscOrder;
    function GetOSCOrder(const IDLocal : Integer): IBizOscOrder;

  end;

implementation

{$R *.DFM}

uses
  FactuGES_Intf, uDataModuleConexion, uDataTableUtils, cxControls, uDAInterfaces,
  schTiendaWebClient_Intf;

{ TDataModuleTiendaWeb }


// Descomentar si tiene informe
// function TDataModuleTiendaWeb.GetReport(const AIBizTiendaWebID: Integer): Binary;
// begin
//   Result := (RORemoteService as IsrvTiendaWeb).GenerateReport(AIBizTiendaWebID)
// end;

procedure TDataModuleTiendaWeb.DAClientDataModuleCreate(Sender: TObject);
begin
  RORemoteService.Channel := dmConexion.Channel;
  RORemoteService.Message := dmConexion.Message;
end;

function TDataModuleTiendaWeb.DarNombreGenerador(DataSetName: String): String;
begin
  Result := '';
  if DataSetName = nme_TiendaWeb then
    Result := 'GEN_TIENDA_WEB_ID'
end;

function TDataModuleTiendaWeb.GetNextID(const DataSetName: String): Integer;
var
  aGeneratorName : String;
begin
  aGeneratorName := DarNombreGenerador(DataSetName);
  Result := (RORemoteService as IsrvTiendaWeb).GetNextAutoInc(aGeneratorName)
end;

function TDataModuleTiendaWeb.GetNextIDOSC(const DataSetName: String): Integer;
begin
  Result := (RORemoteService as IsrvTiendaWeb).GetNextAutoIncOSC(DataSetName);
end;

function TDataModuleTiendaWeb._CloneDataTable(const ADataTable : TDACDSDataTable): TDACDSDataTable;
begin
  Result := NIL;

  if not Assigned(ADataTable) then
    raise Exception.Create ('No se ha asignado la tabla de origen (CloneDataTable)');

  Result := TDACDSDataTable.Create(NIL);
  CloneDataTable(ADataTable, Result);
end;

function TDataModuleTiendaWeb._GetOSCAddressBook: IBizOscAddressBook;
var
  AAddressBook : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AAddressBook := _CloneDataTable(tbl_osc_AddressBook);
    with AAddressBook do
    begin
      BusinessRulesID := BIZ_CLIENT_OSC_ADDRESSBOOK;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (AAddressBook as IBizOscAddressBook);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb._GetOSCOrderProducts: IBizOscOrderProducts;
var
  AOrderProducts : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AOrderProducts := _CloneDataTable(tbl_osc_Orders_products);
    with AOrderProducts do
    begin
      BusinessRulesID := BIZ_CLIENT_OSC_ORDERPRODUCTS;
      DetailOptions := DetailOptions -
        [dtDisableLogOfCascadeDeletes, dtDisableLogOfCascadeUpdates];
    end;
    Result := (AOrderProducts as IBizOscOrderProducts);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.GetItems: IBizTiendaWeb;
var
  AIBizTiendaWeb : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AIBizTiendaWeb := _CloneDataTable(tbl_TiendaWeb);
    AIBizTiendaWeb.BusinessRulesID := BIZ_CLIENT_TIENDAWEB;

    Result := (AIBizTiendaWeb as IBizTiendaWeb);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.GetItem(const ID_Empresa: Integer): IBizTiendaWeb;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetItems;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText('TIENDA_WEB.' + fld_TiendaWebID_EMPRESA + ' = ' + IntToStr(ID_Empresa));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.NewItem: IBizTiendaWeb;
begin
  Result := GetItem(ID_NULO)
end;

function TDataModuleTiendaWeb.NewOSCCustomer: IBizOSCCustomer;
begin
  Result := GetOSCCustomer(ID_NULO)
end;

function TDataModuleTiendaWeb.NewOSCManufacturer: IBizOSCManufacturer;
begin
  Result := GetOSCManufacturer(ID_NULO)
end;

function TDataModuleTiendaWeb.NewOSCProduct: IBizOSCProduct;
begin
  Result := GetOSCProduct(ID_NULO)
end;

function TDataModuleTiendaWeb.GetOSCCustomer(
  const IDLocal: Integer): IBizOSCCustomer;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetOSCCustomers;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText(fld_osc_Customersrdx_customers_id_local + ' = ' + IntToStr(IDLocal));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.GetOSCCustomers: IBizOSCCustomer;
var
  ACustomer : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    ACustomer := _CloneDataTable(tbl_osc_Customers);
    ACustomer.BusinessRulesID := BIZ_CLIENT_OSC_CUSTOMER;

    with TBizOSCCustomer(ACustomer.BusinessEventsObj) do
    begin
      AddressBook := _GetOSCAddressBook;
    end;

    Result := (ACustomer as IBizOSCCustomer);
  finally
    HideHourglassCursor;
  end;
end;


function TDataModuleTiendaWeb.GetOSCManufacturer(
  const IDLocal: Integer): IBizOSCManufacturer;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetOSCManufacturers;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText(fld_osc_Manufacturersrdx_manufacturers_id_local + ' = ' + IntToStr(IDLocal));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.GetOSCManufacturers: IBizOSCManufacturer;
var
  AManufacturer : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AManufacturer := _CloneDataTable(tbl_osc_Manufacturers);
    AManufacturer.BusinessRulesID := BIZ_CLIENT_OSC_MANUFACTURER;

    Result := (AManufacturer as IBizOSCManufacturer);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.GetOSCOrder(const IDLocal: Integer): IBizOscOrder;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetOSCOrders;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText(fld_osc_Ordersorders_id + ' = ' + IntToStr(IDLocal));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.GetOSCOrders: IBizOscOrder;
var
  AOrder : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AOrder := _CloneDataTable(tbl_osc_Orders);
    AOrder.BusinessRulesID := BIZ_CLIENT_OSC_ORDER;

    with TBizOscOrder(AOrder.BusinessEventsObj) do
    begin
      OrderProducts := _GetOSCOrderProducts;
    end;

    Result := (AOrder as IBizOscOrder);
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.GetOSCProduct(
  const IDLocal: Integer): IBizOSCProduct;
begin
  ShowHourglassCursor;
  try
    Result := Self.GetOSCProducts;
    with Result.DataTable.Where do
    begin
      if NotEmpty then
        AddOperator(opAND);
      OpenBraket;
      AddText(fld_osc_Productsrdx_products_id_local + ' = ' + IntToStr(IDLocal));
      CloseBraket;
    end;
  finally
    HideHourglassCursor;
  end;
end;

function TDataModuleTiendaWeb.GetOSCProducts: IBizOSCProduct;
var
  AProduct : TDACDSDataTable;
begin
  ShowHourglassCursor;
  try
    AProduct := _CloneDataTable(tbl_osc_Products);
    AProduct.BusinessRulesID := BIZ_CLIENT_OSC_PRODUCT;

    Result := (AProduct as IBizOSCProduct);
  finally
    HideHourglassCursor;
  end;
end;

end.
