259 lines
11 KiB
C#
259 lines
11 KiB
C#
using Sarand.BusinessMgr.Net.Standard.Base.App;
|
|
using Sarand.BusinessMgr.Net.Standard.Base.Bus;
|
|
using Sarand.BusinessMgr.Net.Standard.Dbas.Bus;
|
|
using Sarand.BusinessMgr.Net.Standard.Dbas.Internal;
|
|
using Sarand.BusinessMgr.Net.Standard.Dbas.UpdateLog;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.CodeTypes;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Interfaces;
|
|
using Sarand.Net.Standard.Base.ItemDocs;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Sarand.Net.Standard.Dbas.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.Linq;
|
|
using static Sarand.BusinessMgr.Net.Standard.Base.App.Sbm;
|
|
|
|
namespace Sarand.BusinessMgr.Net.Standard.Dbas.Mgr
|
|
{
|
|
//=======================================================================
|
|
// Account manager - Associations - Utilities.
|
|
//=======================================================================
|
|
partial class SbmAccountMgr_Dbas
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Utilities.
|
|
private List<StringPair> GetLogProperties(SbmAssociation association)
|
|
{
|
|
return new List<StringPair>()
|
|
{
|
|
new StringPair("Id",SysId.ToString(association.Id)),
|
|
new StringPair("Code",association.Code),
|
|
new StringPair("Status",association.StatusCode)
|
|
};
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Association types in use.
|
|
public override List<Sbm.AssociationType> GetAssociationTypesInUse()
|
|
{
|
|
List<Sbm.AssociationType> types = new List<Sbm.AssociationType>();
|
|
String colAssociation_Type = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Association_Type);
|
|
// Construct the query.
|
|
DbQuery qry = new DbQuery(Database.DbmsType);
|
|
qry.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Association));
|
|
qry.Columns.Add("DISTINCT " + SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_Type), colAssociation_Type);
|
|
qry.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_AffIdOwner) + "=" + AffiliateId_Owner.ToString());
|
|
using (DbResultSet dbResultSet = DbResultSet_DataReader.ExecuteQuery(Database, qry.BuildQuery()))
|
|
{
|
|
if (dbResultSet != null && dbResultSet.NextRecord())
|
|
{
|
|
NamedValue.Map values = dbResultSet.GetRecordValues();
|
|
types.Add((Sbm.AssociationType)values.GetValue(colAssociation_Type).AsLong());
|
|
}
|
|
}
|
|
return types;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Internal associations.
|
|
public override HashSet<SysId> GetInternalAssociationAffiliateIdsOwner()
|
|
{
|
|
ClearErrorState();
|
|
HashSet<SysId> affiliateIdsOwner = new HashSet<SysId>();
|
|
String colAffIdOwner = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Association_AffIdOwner);
|
|
SbmAssociation.Params prmAssociation = new SbmAssociation.Params();
|
|
prmAssociation.AssociationType = AssociationType.Internal;
|
|
prmAssociation.StatusCat = SbmAssociation.StatusCat.Active;
|
|
// Construct the query.
|
|
DbQuery qry = new DbQuery(Database.DbmsType);
|
|
qry.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Association), aliasAssociation);
|
|
qry.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_Type, aliasAssociation), colAffIdOwner);
|
|
qry.Filters.AddRange(GetAssociationQueryFilter(prmAssociation));
|
|
using (DbResultSet dbResultSet = DbResultSet_DataReader.ExecuteQuery(Database, qry.BuildQuery()))
|
|
{
|
|
if (dbResultSet != null && dbResultSet.NextRecord())
|
|
{
|
|
NamedValue.Map values = dbResultSet.GetRecordValues();
|
|
SysId affiliteIdOwner = new SysId(values.GetValue(colAffIdOwner).AsLong());
|
|
if (SysId.IsValid(affiliteIdOwner))
|
|
affiliateIdsOwner.Add(affiliteIdOwner);
|
|
}
|
|
}
|
|
return affiliateIdsOwner;
|
|
}
|
|
|
|
public override List<SysId> GetMemberTargetIds(SysId affiliateIdOwner)
|
|
{
|
|
List<SysId> affiliateIncludeIds = new List<SysId>();
|
|
List<SysId> affiliateExcludeIds = new List<SysId>();
|
|
return GetTargetIds(affiliateIdOwner, Sbm.AssociationType.Member, affiliateIncludeIds, affiliateExcludeIds);
|
|
}
|
|
public override List<SysId> GetCustomerTargetIds(List<SysId> affiliateIncludeIds, List<SysId> affiliateExcludeIds)
|
|
{
|
|
return GetTargetIds(AffiliateId_Owner, Sbm.AssociationType.Customer, affiliateIncludeIds, affiliateExcludeIds);
|
|
}
|
|
public List<SysId> GetTargetIds(SysId affiliateIdOwner, Sbm.AssociationType associationType, List<SysId> affiliateIncludeIds, List<SysId> affiliateExcludeIds)
|
|
{
|
|
List<SysId> affiliateIdsTarget = new List<SysId>();
|
|
if (Database == null) return affiliateIdsTarget;
|
|
bool validConfig = true;
|
|
if (!SysId.IsValid(affiliateIdOwner) || (int)associationType < 1)
|
|
{
|
|
#if _DEBUG
|
|
SATRACE("SapAssociationMgr_Odbc::GetTargetIds Invalid owner or association type.\n");
|
|
#endif
|
|
validConfig = false;
|
|
}
|
|
else if (associationType == Sbm.AssociationType.Customer || associationType == Sbm.AssociationType.Supplier)
|
|
{
|
|
if (affiliateIdOwner != AffiliateId_Owner)
|
|
{
|
|
#if _DEBUG
|
|
SATRACE("SapAssociationMgr_Odbc::GetTargetIds Invalid customer/supplier owner.\n");
|
|
#endif
|
|
validConfig = false;
|
|
}
|
|
}
|
|
if (validConfig)
|
|
{
|
|
String colAffiliateIdTarget = "AffIdTgt";
|
|
DbQuery qry = new DbQuery(Database.DbmsType);
|
|
qry.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Association));
|
|
qry.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_AffIdTarget), colAffiliateIdTarget);
|
|
qry.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_AffIdOwner) + "=" + affiliateIdOwner.ToString());
|
|
qry.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_Type) + "=" + ((int)associationType).ToString());
|
|
if (affiliateIncludeIds.Count > 0)
|
|
qry.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_AffIdTarget) + " IN(" + affiliateIncludeIds.Concat(",") + ")");
|
|
if (affiliateExcludeIds.Count > 0)
|
|
qry.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_AffIdTarget) + " NOT IN(" + affiliateExcludeIds.Concat(",") + ")");
|
|
|
|
using (DbResultSet dbResultSet = DbResultSet_DataReader.ExecuteQuery(Database, qry.ToString()))
|
|
{
|
|
if (dbResultSet != null && dbResultSet.NextRecord())
|
|
{
|
|
NamedValue.Map values = dbResultSet.GetRecordValues();
|
|
affiliateIdsTarget.Add(new SysId(StringUtil.ToInt(values.GetValue(colAffiliateIdTarget))));
|
|
}
|
|
}
|
|
}
|
|
return affiliateIdsTarget;
|
|
}
|
|
public override List<SysId> GetCustomerAssocIdsForTargets(List<SysId> affiliateIdsTarget)
|
|
{
|
|
return GetAssocIdsForTargets(AffiliateId_Owner, Sbm.AssociationType.Customer, affiliateIdsTarget);
|
|
}
|
|
public override List<SysId> GetAssocIdsForTargets(SysId affiliateIdOwner, Sbm.AssociationType associationType, List<SysId> affiliateIdsTarget)
|
|
{
|
|
List<SysId> associations = new List<SysId>();
|
|
if (Database == null) return associations;
|
|
bool validConfig = true;
|
|
if (!SysId.IsValid(affiliateIdOwner) || (int)associationType < 1)
|
|
{
|
|
#if _DEBUG
|
|
SATRACE("SapAssociationMgr_Odbc::GetAssocIdsForTargets Invalid owner or association type.\n");
|
|
#endif
|
|
validConfig = false;
|
|
}
|
|
else if (associationType == Sbm.AssociationType.Customer || associationType == Sbm.AssociationType.Supplier)
|
|
{
|
|
if (affiliateIdOwner != AffiliateId_Owner)
|
|
{
|
|
#if _DEBUG
|
|
SATRACE("SapAssociationMgr_Odbc::GetAssocIdsForTargets Invalid customer/supplier owner.\n");
|
|
#endif
|
|
validConfig = false;
|
|
}
|
|
}
|
|
if (validConfig)
|
|
{
|
|
String colAssociationId = "AssocId";
|
|
DbQuery qry = new DbQuery(Database.DbmsType);
|
|
qry.Tables.Add(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Association));
|
|
qry.Columns.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_Id), colAssociationId);
|
|
qry.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_AffIdOwner) + "=" + affiliateIdOwner.ToString());
|
|
qry.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_Type) + "=" + ((int)associationType).ToString());
|
|
qry.Filters.Add(SbmDbDfn.GetColumnNameQual(SbmDbDfn.ColumnIdx.Association_AffIdTarget) + " IN " + affiliateIdsTarget.Concat(","));
|
|
|
|
using (DbResultSet dbResultSet = DbResultSet_DataReader.ExecuteQuery(Database, qry.ToString()))
|
|
{
|
|
if (dbResultSet != null && dbResultSet.NextRecord())
|
|
{
|
|
NamedValue.Map values = dbResultSet.GetRecordValues();
|
|
associations.Add(new SysId(StringUtil.ToInt(values.GetValue(colAssociationId))));
|
|
}
|
|
}
|
|
}
|
|
return associations;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Associated data.
|
|
public override bool AssociationHasTransactions(SysId associationId)
|
|
{
|
|
if (!SysId.IsValid(associationId)) return false;
|
|
String filter = $"{SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Transaction_AssocId)}={associationId}";
|
|
return Database.GetRowCount(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Transaction), filter) > 0;
|
|
}
|
|
public override bool AssociationHasRequirements(SysId associationId)
|
|
{
|
|
if (!SysId.IsValid(associationId)) return false;
|
|
String filter = $"{SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Requirement_AssocId)}={associationId}";
|
|
return Database.GetRowCount(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Requirement), filter) > 0;
|
|
}
|
|
public override bool AssociationHasConstituents(SysId associationId)
|
|
{
|
|
if (!SysId.IsValid(associationId)) return false;
|
|
String filter = $"{SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Constituent_AssocId)}={associationId}";
|
|
return Database.GetRowCount(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Constituent), filter) > 0;
|
|
}
|
|
public override bool AssociationIsDeliveryContractor(SysId associationId)
|
|
{
|
|
// Is associated with transaction for delivery.
|
|
if (!SysId.IsValid(associationId)) return false;
|
|
String filter = $"{SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Transaction_AssIdDeliver)}={associationId}";
|
|
return Database.GetRowCount(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Transaction), filter) > 0;
|
|
}
|
|
public override bool ClearAssociationDependentData(SbmAssociation association, DbUpdateInfo dbUpdateInfo)
|
|
{
|
|
ClearErrorState();
|
|
if (association == null || !SysId.IsValid(association.Id)) return false;
|
|
|
|
// Delete interface data items.
|
|
if (!HasErrorState())
|
|
{
|
|
if (InterfaceConsole != null)
|
|
{
|
|
if (!InterfaceConsole.ClearInterfaceDataItems(association.GetItemId(), dbUpdateInfo))
|
|
SetErrorState("Failed to clear interface data items - " + InterfaceConsole.ErrorStateMessage);
|
|
}
|
|
else SetErrorState("failed to initialise interface console");
|
|
}
|
|
// Remove document references
|
|
if (!HasErrorState())
|
|
{
|
|
if (ItemDocConsole != null)
|
|
{
|
|
if (!ItemDocConsole.ClearItemDocs(association.GetItemId(), dbUpdateInfo))
|
|
SetErrorState(ItemDocConsole.ErrorStateMessage);
|
|
}
|
|
else SetErrorState("Failed to retrieve item document console");
|
|
}
|
|
// Remove data item references.
|
|
if (!HasErrorState())
|
|
{
|
|
if (!DataItemMgr.ClearDataItem(association.GetItemId(), dbUpdateInfo))
|
|
SetErrorState(DataItemMgr.ErrorStateMessage);
|
|
}
|
|
return OK;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|