392 lines
16 KiB
C#
392 lines
16 KiB
C#
using Sarand.BusinessMgr.Net.Standard.Base.App;
|
|
using Sarand.BusinessMgr.Net.Standard.Base.Bus;
|
|
using Sarand.BusinessMgr.Net.Standard.Base.Mgr;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
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.Base;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Sarand.BusinessMgr.Net.Standard.Dbas.Bus
|
|
{
|
|
//=======================================================================
|
|
class SbmCommodity_Dbas : SbmCommodity
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private readonly SbmInternals internals;
|
|
private List<SbmCostEstimate> costEstimates = new List<SbmCostEstimate>();
|
|
private List<SbmItemProcess> itemProcesses = new List<SbmItemProcess>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SbmCommodity_Dbas(SbmInternals internals, SysId id, SysId affiliateIdOwner, Sbm.CommodityType commodityType, String code, ushort flags) :
|
|
base(internals.UserContext, id, affiliateIdOwner, commodityType, code)
|
|
{
|
|
this.internals = internals;
|
|
Flags = flags;
|
|
}
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
// NB Do not dispose the Internals - owned by manager.
|
|
}
|
|
base.Dispose(disposing);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
|
|
//-------------------------------------------------------------------
|
|
// Associated data.
|
|
//-------------------------------------------------------------------
|
|
public override bool HasProcesses()
|
|
{
|
|
ClearErrorState();
|
|
if (!SysId.IsValid(Id)) return SetErrorState("invalid item");
|
|
return internals.ProcessMgr.HasItemProcesses(Sbm.ItemType.Commodity, Id);
|
|
}
|
|
public override bool HasRequirements()
|
|
{
|
|
ClearErrorState();
|
|
if (!SysId.IsValid(Id)) return false;
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null)
|
|
{
|
|
String tableRequirement = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Requirement);
|
|
String filter = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Requirement_CommodityId) + "=" + Id.ToString();
|
|
return db.GetRowCount(tableRequirement, /*SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Requirement_Id),*/ filter) > 0;
|
|
}
|
|
return false;
|
|
}
|
|
public override bool IsSalesBudgetTemplate()
|
|
{
|
|
ClearErrorState();
|
|
if (!SysId.IsValid(Id)) return false;
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null)
|
|
{
|
|
String tableCommodity = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Commodity);
|
|
String filter = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Commodity_CommodityIdSalesProj) + "=" + Id.ToString();
|
|
return db.GetRowCount(tableCommodity, /*SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Commodity_Id),*/ filter) > 0;
|
|
}
|
|
return false;
|
|
}
|
|
public override bool HasComponents()
|
|
{
|
|
ClearErrorState();
|
|
return false;
|
|
}
|
|
public override bool HasPortfolioItems()
|
|
{
|
|
ClearErrorState();
|
|
if (!SysId.IsValid(Id)) return false;
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null)
|
|
{
|
|
String tablePortfolioItem = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.PortfolioItem);
|
|
if (db.TableExists(tablePortfolioItem))
|
|
{
|
|
String colPortfolioItem_Id = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.PortfolioItem_Id);
|
|
List<String> filters = new List<String>();
|
|
filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.PortfolioItem_CommodityId) + "=" + Id.ToString());
|
|
return db.GetRowCount(tablePortfolioItem, /*colPortfolioItem_Id,*/ filters.Concat(" AND ", "(", ")")) > 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public override bool HasContainers()
|
|
{
|
|
ClearErrorState();
|
|
if (!SysId.IsValid(Id)) return false;
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null)
|
|
{
|
|
String tableContainer = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Container);
|
|
if (db.TableExists(tableContainer))
|
|
{
|
|
String colContainerItem_ContainerId = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.ContainerItem_ContainerId);
|
|
List<String> filters = new List<String>();
|
|
filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.ContainerItem_ItemType) + "=" + Sbm.ItemType.Commodity.ToString());
|
|
filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.ContainerItem_ItemId) + "=" + Id.ToString());
|
|
return db.GetRowCount(tableContainer, /*colContainerItem_ContainerId,*/ filters.Concat(" AND ", "(", ")")) > 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public override bool HasCustomerDetails()
|
|
{
|
|
ClearErrorState();
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null)
|
|
{
|
|
String tableCommodityCustomer = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.CommodityCustomer);
|
|
if (db.TableExists(tableCommodityCustomer))
|
|
{
|
|
List<String> filters = new List<String>();
|
|
filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.CommodityCustomer_CommodityId) + "=" + Id.ToString());
|
|
return db.GetRowCount(tableCommodityCustomer, /*"1",*/ filters.Concat(" AND ", "(", ")")) > 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public override bool HasSupplierDetails()
|
|
{
|
|
ClearErrorState();
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null)
|
|
{
|
|
String tableCommoditySupplier = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.CommoditySupplier);
|
|
if (db.TableExists(tableCommoditySupplier))
|
|
{
|
|
List<String> filters = new List<String>();
|
|
filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.CommoditySupplier_CommodityId) + "=" + Id.ToString());
|
|
return db.GetRowCount(tableCommoditySupplier, /*"1",*/ filters.Concat(" AND ", "(", ")")) > 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public override bool HasMovements()
|
|
{
|
|
ClearErrorState();
|
|
if (!SysId.IsValid(Id)) return false;
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null)
|
|
{
|
|
String tableMovement = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.Movement);
|
|
if (db.TableExists(tableMovement))
|
|
{
|
|
String colMovement_Id = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Movement_Id);
|
|
List<String> filters = new List<String>();
|
|
filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Movement_ItemType));
|
|
filters.Add(SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.Movement_ItemId) + "=" + Id.ToString());
|
|
return db.GetRowCount(tableMovement, /*colMovement_Id,*/ filters.Concat(" AND ", "(", ")")) > 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public override bool HasStockLevels()
|
|
{
|
|
ClearErrorState();
|
|
if (!SysId.IsValid(Id)) return false;
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null)
|
|
{
|
|
String tableStockLevel = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.StockLevel);
|
|
if (db.TableExists(tableStockLevel))
|
|
{
|
|
String colStockLevel_CommodityId = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.StockLevel_CommodityId);
|
|
String filter = colStockLevel_CommodityId + "=" + Id.ToString();
|
|
return db.GetRowCount(tableStockLevel, filter) > 0;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
public override bool ClearDependentData()
|
|
{
|
|
ClearErrorState();
|
|
if (!SysId.IsValid(Id)) return false;
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null && !HasErrorState())
|
|
{
|
|
if (!HasErrorState())
|
|
{
|
|
// Remove data item references.
|
|
if (!internals.DataItemMgr.ClearDataItem(Sbm.ItemType.Commodity, Id))
|
|
SetErrorState(internals.DataItemMgr.ErrorState.ToString());
|
|
}
|
|
if (!HasErrorState())
|
|
{
|
|
// ToDo SaItemDocConsole for Remove document references.
|
|
SaItemDocConsole itemDocConsole = internals.ItemDocConsole;
|
|
if (itemDocConsole != null)
|
|
{
|
|
if (!itemDocConsole.ClearItemDocs(new ItemId((short)Sbm.ItemType.Commodity, Id), SaItemDocConsole.Category.Document, internals.DbUpdateInfo))
|
|
SetErrorState(itemDocConsole.ErrorState.ToString());
|
|
}
|
|
else SetErrorState("Failed to retrieve item document console");
|
|
itemDocConsole = null;
|
|
}
|
|
if (!HasErrorState())
|
|
{
|
|
// Remove interface references.
|
|
SaInterfaceConsole interfaceConsole = internals.InterfaceConsole;
|
|
if (interfaceConsole != null)
|
|
{
|
|
if (!interfaceConsole.ClearInterfaceDataItems(new ItemId((short)Sbm.ItemType.Commodity, Id), internals.DbUpdateInfo))
|
|
SetErrorState(interfaceConsole.ErrorState.ToString());
|
|
}
|
|
else SetErrorState("Failed to retrieve interface console");
|
|
interfaceConsole = null;
|
|
}
|
|
if (!HasErrorState())
|
|
{
|
|
// Remove customer commodity details.
|
|
List<StringPair> columnNameValues = new List<StringPair>();
|
|
String tableCommodityCustomer = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.CommodityCustomer);
|
|
String filter = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.CommodityCustomer_CommodityId) + "=" + Id.ToString();
|
|
if (db != null && db.TableExists(tableCommodityCustomer) && db.ExecuteDelete(tableCommodityCustomer, filter) > 0)
|
|
SetErrorState("Failed to clear customer commodity details");
|
|
}
|
|
if (!HasErrorState())
|
|
{
|
|
// Remove Supplier commodity details.
|
|
String tableCommoditySupplier = SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.CommoditySupplier);
|
|
String filter = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.CommoditySupplier_CommodityId) + "=" + Id.ToString();
|
|
if (db != null && db.TableExists(tableCommoditySupplier) && db.ExecuteDelete(tableCommoditySupplier, filter) > 0)
|
|
SetErrorState("Failed to clear Supplier commodity details");
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
//---------------------------------------------------------------
|
|
// Commodity Cost Estimates.
|
|
public override List<SbmCostEstimate> GetCostEstimates()
|
|
{
|
|
//ClearErrorState();
|
|
List<SbmCostEstimate> costEstimatesCopy = null;
|
|
if (costEstimates == null && SysId.IsValid(Id))
|
|
{
|
|
//***************************************************************
|
|
// 15-Dec MPE Fix - Commodity cost estimates were stored as requirement cost estimates. Here we convert the data for this commodity.
|
|
ISacsDatabase db = internals.BusMgrDatabase;
|
|
if (db != null /*&& SbmGlobals.GetConsole().GetVersion().GetName() == "MPE" && Id.ToInt() < 6000*/)
|
|
{
|
|
String colItemLink_Type = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.ItemLink_Type);
|
|
String colItemLink_ItemTypeFrom = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.ItemLink_ItemTypeFrom);
|
|
String colItemLink_ItemIdFrom = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.ItemLink_ItemIdFrom);
|
|
String colItemLink_CreateOn = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.ItemLink_CreateOn);
|
|
String colItemLink_UpdateOn = SbmDbDfn.GetColumnName(SbmDbDfn.ColumnIdx.ItemLink_UpdateOn);
|
|
List<String> filters = new List<String>();
|
|
// Identify cost estimates for requirements with the same Id as this commodity which were created since the 1-Jan-2006
|
|
// (to avoid updating cost estimates that are more likely to belong to requirements - cost estimates were introduced against commodities during 2006).
|
|
filters.Add("(" + colItemLink_Type + "=" + Sbm.ItemLinkType.ItemCostEstimate.ToString() + " OR " + colItemLink_Type + "=" + Sbm.ItemLinkType.Requirement_CostEstimate.ToString() + ")");
|
|
filters.Add(colItemLink_ItemTypeFrom + "=" + Sbm.ItemType.Requirement.ToString());
|
|
filters.Add(colItemLink_ItemIdFrom + "=" + Id.ToString());
|
|
filters.Add(colItemLink_CreateOn + ">=" + DbQuery.FormatForCondition(new DateTime(1, 1, 2006/*2006,1,1*/).ToString()));
|
|
if (db.GetRowCount(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.ItemLink), /*colItemLink_Type, rowCount,*/ filters.Concat(" AND ", "(", ")")) > 0)
|
|
{
|
|
// The link type and 'From' item type are changed to reflect the commodity relationship.
|
|
List<StringPair> values = new List<StringPair>();
|
|
SacsDatabase.AddValuePair(values, colItemLink_Type, ((int)Sbm.ItemLinkType.ItemCostEstimate).ToString(), false);
|
|
SacsDatabase.AddValuePair(values, colItemLink_ItemTypeFrom, ((int)Sbm.ItemType.Commodity).ToString(), false);
|
|
SacsDatabase.AddValuePair(values, colItemLink_UpdateOn, DbQuery.FormatForCondition(new DateTime().ToString()), false);
|
|
db.ExecuteUpdate(SbmDbDfn.GetTableName(SbmDbDfn.TableIdx.ItemLink), values, filters.Concat(" AND ", "(", ")"));
|
|
}
|
|
}
|
|
//***************************************************************
|
|
SbmItemLink.Params prmItemLink = new SbmItemLink.Params();
|
|
prmItemLink.ItemLinkType = Sbm.ItemLinkType.ItemCostEstimate;
|
|
prmItemLink.ItemTypeFrom = Sbm.ItemType.Commodity;
|
|
prmItemLink.ItemIdFrom = Id;
|
|
List<SbmItemLink> itemLinks = internals.DataItemMgr.GetItemLinkList(prmItemLink);
|
|
if (itemLinks != null)
|
|
{
|
|
foreach (SbmItemLink itemLink in itemLinks)
|
|
{
|
|
SbmCostEstimate costEstimate = new SbmCostEstimate(UserContext, itemLink.DataContent);
|
|
costEstimate.Notes = itemLink.Notes;
|
|
costEstimate.Changed = false;
|
|
if (costEstimates == null) costEstimates = new List<SbmCostEstimate>();
|
|
costEstimates.Add(costEstimate);
|
|
}
|
|
// Clean-up.
|
|
itemLinks.Clear();
|
|
}
|
|
}
|
|
// Clone the list.
|
|
costEstimatesCopy = costEstimates.Clone();
|
|
return costEstimatesCopy;
|
|
}
|
|
public override bool SetCostEstimates(List<SbmCostEstimate> costEstimatesNew)
|
|
{
|
|
ClearErrorState();
|
|
// Clear the existing list.
|
|
if (costEstimates != null) costEstimates.Clear();
|
|
// Copy all items from the specified list.
|
|
costEstimates = costEstimatesNew.Clone();
|
|
return true;
|
|
}
|
|
public override bool CommitCostEstimates()
|
|
{
|
|
ClearErrorState();// Do not attempt any updates unless the list has been populated.
|
|
if (costEstimates != null && SysId.IsValid(Id))
|
|
{
|
|
// Build an item link list corresponding to the list of cost estimates.
|
|
List<SbmItemLink> itemLinks = new List<SbmItemLink>();
|
|
foreach (SbmCostEstimate costEstimate in costEstimates)
|
|
{
|
|
if (costEstimate == null) continue;
|
|
SbmItemLink itemLink = internals.DataItemMgr.CreateItemLink(Sbm.ItemLinkType.ItemCostEstimate, Sbm.ItemType.Commodity, Id, Sbm.ItemType.Unknown, new SysId());
|
|
if (itemLink != null)
|
|
{
|
|
itemLink.Seq = (short)costEstimate.CodeSeq;
|
|
itemLink.DataContent = costEstimate.ToString();
|
|
itemLink.Notes = costEstimate.Notes;
|
|
itemLinks.Add(itemLink);
|
|
itemLink = null; // Now owned by the list.
|
|
}
|
|
}
|
|
// Store these details.
|
|
if (internals.DataItemMgr.StoreFromItemLinks(Sbm.ItemLinkType.ItemCostEstimate, Sbm.ItemType.Commodity, Id, itemLinks, internals.DbUpdateInfo))
|
|
{
|
|
}
|
|
else
|
|
{
|
|
SetErrorState("Failed to save cost estimate item links - " + internals.DataItemMgr.ErrorState);
|
|
}
|
|
if (itemLinks != null)
|
|
itemLinks.Clear();
|
|
}
|
|
return !HasErrorState();
|
|
}
|
|
//---------------------------------------------------------------
|
|
// Commodity processes.
|
|
public override List<SbmItemProcess> GetProcesses(bool activeOnly)
|
|
{
|
|
//ToDo need to creat SbmProcessMgr_Dbas-ItemProcess inorder to do processMgr.???
|
|
ClearErrorState();
|
|
List<SbmItemProcess> itemProcesesCopy = null;
|
|
if (activeOnly)
|
|
{
|
|
// Retrieve the list of 'active' processes for this item.
|
|
internals.ProcessMgr.GetProcessesForItem(Sbm.ItemType.Commodity, Id, true);
|
|
}
|
|
else
|
|
{
|
|
// Full list required - check whether the internal list has been populated yet.
|
|
if (itemProcesses == null && SysId.IsValid(Id))
|
|
{
|
|
// Get the list of processes.
|
|
internals.ProcessMgr.GetProcessesForItem(Sbm.ItemType.Commodity, Id, false);
|
|
}
|
|
// Clone the list.
|
|
itemProcesesCopy = itemProcesses.Clone();
|
|
}
|
|
if (itemProcesesCopy != null) itemProcesesCopy.Sort();
|
|
return itemProcesesCopy;
|
|
}
|
|
public override bool SetProcesses(List<SbmItemProcess> itemProcessesNew)
|
|
{
|
|
ClearErrorState();
|
|
if (itemProcesses != null) itemProcesses.Clear();
|
|
// Copy all items from the specified list.
|
|
itemProcesses = itemProcessesNew.Clone();
|
|
return !HasErrorState();
|
|
}
|
|
public override bool CommitProcesses()
|
|
{
|
|
ClearErrorState();
|
|
if (!internals.ProcessMgr.StoreItemProcesses(Sbm.ItemType.Commodity, Id, itemProcesses, internals.DbUpdateInfo))
|
|
SetErrorState(internals.ProcessMgr.ErrorState.ToString());
|
|
return !HasErrorState();
|
|
}
|
|
}
|
|
//=======================================================================
|
|
}
|