Skip to content

Instantly share code, notes, and snippets.

@darrenkopp
Created June 20, 2012 04:14
Show Gist options
  • Save darrenkopp/2958095 to your computer and use it in GitHub Desktop.
Save darrenkopp/2958095 to your computer and use it in GitHub Desktop.
The creation.cs file has the code that is run on creation. code in update.cs is always run (create/update). The code in save is always run (create/update).
namespace DBI.Synchronization.Processing.ServiceManagement.Tasks
{
internal static class ActivityExtensions
{
internal static SMTask CreateChild(this long parentId, SMWorkOrderTaskTypeEnum type)
{
var parent = SMTaskFactory.Factory.Fetch(parentId);
var child = parent.AddChildTask();
child.TaskType = type;
child.Parent = parent;
child.WorkOrderObject = parent.WorkOrderObject;
child.WorkTypeDbKey = child.WorkOrderItemObject.WorkTypeDbKey = parent.WorkTypeDbKey;
child.DepartmentDbKey = child.WorkOrderItemObject.DepartmentDbKey = parent.DepartmentDbKey;
child.SMSaleLocationID = child.WorkOrderItemObject.SMSaleLocationID = parent.SMSaleLocationID;
child.RateSheet = child.WorkOrderItemObject.RateSheet = parent.RateSheet;
child.SMTechnicianID = parent.SMTechnicianID;
child.TaskSourceTypeEnum = parent.TaskSourceTypeEnum;
child.JobCostingTypeEnum = child.WorkOrderItemObject.JobCostingTypeEnum = parent.JobCostingTypeEnum;
child.SMAgreementServiceDbKey = child.WorkOrderItemObject.SMAgreementServiceDbKey = parent.SMAgreementServiceDbKey;
// prefill job
if (child.IsFlatRate())
PrefillJob(parent.WorkOrderObject.JCJob, child.FlatRateLaborRevenueWorkOrderItemObject, child.FlatRatePartsRevenueWorkOrderItemObject);
else
PrefillJob(parent.WorkOrderObject.JCJob, child.WorkOrderItemObject);
child.WorkOrderItemObject.PrefillTaxInformation();
child.WorkOrderItemObject.PrefillProduct();
return child;
}
internal static void PrefillJob(Sage.Accounting.ProjectCosting.IPCProjectItem job, params SMWorkOrderItem[] items)
{
if (items == null || items.Length == 0)
return;
foreach (var item in items)
{
item.PCProjectItemDbKey = DbKey.Null;
item.JCJob = job;
}
}
internal static void PrefillTaxInformation(this SMWorkOrderItem item)
{
if (item.SYSTaxStatusDbKey.ToLong() == null)
item.PrefillTaxFields();
}
static T GetByLocation<T>(SaleLocationEnum location, Func<T> center, Func<T> site)
{
return (location == SaleLocationEnum.ServiceCenter) ? center() : site();
}
internal static void PrefillProduct(this SMWorkOrderItem item)
{
if (item.ProductDbKey.ToLong() == null)
item.ProductDbKey = GetProduct(item);
}
static DbKey GetProduct(SMWorkOrderItem item)
{
var type = (SMWorkOrderTaskTypeEnum)item.SMWorkOrderItemTypeDbKey.ToLong().Value;
switch (type)
{
case SMWorkOrderTaskTypeEnum.FlatRateLaborRevenue:
return item.WorkTypeObject.FlatRateLaborProductDbKey;
case SMWorkOrderTaskTypeEnum.FlatRatePartsRevenue:
return item.WorkTypeObject.FlatRatePartsProductDbKey;
case SMWorkOrderTaskTypeEnum.Labor:
return item.WorkTypeObject.LaborProductDbKey;
case SMWorkOrderTaskTypeEnum.Miscellaneous:
return (item.SMMiscellaneousItemDbKey.ToLong() == null || item.MiscellaneousItem.ProductDbKey.ToLong() == null)
? item.WorkTypeObject.MiscellaneousProductDbKey
: item.MiscellaneousItem.ProductDbKey;
case SMWorkOrderTaskTypeEnum.Parts:
return item.WorkTypeObject.PartsProductDbKey;
default:
return DbKey.Null;
}
}
}
}
public void Persist(SMTask entity)
{
using (var rootTransaction = new PersistentObjectTransaction(entity, Sage.ObjectStore.Builder.TransactionMode.Required))
{
// update to make sure we have task id
entity.Update();
if (entity.TaskType == SMWorkOrderTaskTypeEnum.FlatRate)
{
PersistItem(entity, entity.FlatRateLaborRevenueWorkOrderItemObject);
PersistItem(entity, entity.FlatRatePartsRevenueWorkOrderItemObject);
}
if (entity.SMWorkOrderDbKey.ToLong().HasValue && TASKS_WITH_ITEM.Contains(entity.TaskType))
{
PersistItem(entity, entity.WorkOrderItemObject);
}
entity.Update();
rootTransaction.Commit();
}
}
void PersistItem(SMTask task, SMWorkOrderItem item)
{
using (var transaction = new PersistentObjectTransaction(item, Sage.ObjectStore.Builder.TransactionMode.Required))
{
item.AssociatedTask = task;
if (item.SMWorkOrderDbKey.ToLong() == null)
item.SMWorkOrderDbKey = task.SMWorkOrderDbKey;
PrefillWorkOrderItemFields(item);
item.PrefillTaxInformation();
if (item.WorkOrderItemType == SMWorkOrderTaskTypeEnum.Labor)
item.UnitCost = item.CalculateLaborUnitCost(item.Technician, item.PayTypeObject);
item.Update(false);
transaction.Commit();
}
task.WorkOrderItemDbKey = item.SMWorkOrderItemID;
}
private void PrefillWorkOrderItemFields(SMWorkOrderItem item)
{
var task = item.AssociatedTask;
if (task.WorkTypeDbKey.ToLong() == null)
task.WorkTypeDbKey = task.ParentTaskObject.WorkTypeDbKey;
item.PrefillProduct();
}
public void Map(LaborActivityDTO source, SMTask target)
{
var item = target.WorkOrderItemObject;
item.WorkOrderItemType = SMWorkOrderTaskTypeEnum.Labor;
item.PayTypeDbKey = source.PayTypeId.ToDbKey();
target.SMTechnicianID = item.SMTechnicianDbKey = source.TechnicianId.ToDbKey();
target.CostQuantity = item.CostQuantity = source.UnitCostQuantity;
target.UnitCost = item.UnitCost = source.UnitCostRate;
target.BilledQuantity = item.BilledQuantity = source.UnitBilledQuantity;
target.UnitSale = item.UnitSale = source.UnitBilledRate;
target.ServiceLocationEquipmentDbKey = item.ServiceLocationEquipmentDbKey = source.EquipmentId.ToDbKey();
item.SYSTaxGroupDbKey = source.TaxGroupId.ToDbKey();
item.SYSTaxStatusDbKey = source.TaxStatusId.ToDbKey();
if (source.CompletionDate.HasValue)
{
item.Completed = true;
item.AccountingDateTime = source.CompletionDate.GetValueOrDefault();
}
else
{
item.Completed = false;
}
var repairCodeId = source.RepairCodeId.ToDbKey();
if (repairCodeId != item.SMRepairCodeDbKey)
{
item.SMRepairCodeDbKey = repairCodeId;
item.PrefillCostCodeAndCategory();
}
target.Description = item.Description = source.Description ?? (item.RepairCode != null ? item.RepairCode.Description : "");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment