Exemplo n.º 1
0
        public ActionResult AddWOItem(LaborPartsModel model)
        {
            var workOrder = repository.SearchFor <SageWorkOrder>(x => x.WorkOrder == model.WorkOrder).SingleOrDefault();

            if (workOrder == null)
            {
                return(Error("Work Order does not exist", $"There are no Work Orders with ID: {model.WorkOrder}. workOrder == null"));
            }

            var workOrderItem = Mapper.Map <SageWorkOrderItem>(model);

            workOrderItem.Employee = UserModel.Name;

            if (model.WorkOrderItem == 0)
            {
                workOrderItem.WorkOrder = model.WorkOrder;
                workOrderItem.TotalSale = workOrderItem.Quantity * workOrderItem.UnitSale;
                if (workOrderItem.ItemType == "Parts")
                {
                    workOrderItem.PartsSale = workOrderItem.UnitSale;
                }
                else
                {
                    workOrderItem.LaborSale = workOrderItem.UnitSale;
                }

                var result = sageApiProxy.AddWorkOrderItem(workOrderItem);
                if (result != null && result.IsSucceed && result.Entity != null)
                {
                    var woiResult = sageApiProxy.GetWorkorderItemsByWorkOrderId(workOrder.WorkOrder);
                    if (woiResult.IsSucceed)
                    {
                        workOrder.WorkOrderItems = woiResult.Entities;
                        repository.Update(workOrder);
                        _hub.UpdateSageWorkOrder(workOrder);
                    }
                }
                else
                {
                    _log.ErrorFormat("Was not able to save workorderItem to sage. !result.IsSucceed");
                    return(Error("Work order item save failed", $"AddWorkOrderItem method IsSucceed==false. {result?.ErrorMassage}"
                                 ));
                }
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            else
            {
                var resultUpdate = sageApiProxy.EditWorkOrderItem(workOrderItem);
                if (resultUpdate != null && resultUpdate.IsSucceed && resultUpdate.Entity != null)
                {
                    var woiResult = sageApiProxy.GetWorkorderItemsByWorkOrderId(workOrder.WorkOrder);
                    if (woiResult.IsSucceed)
                    {
                        workOrder.WorkOrderItems = woiResult.Entities;
                        repository.Update(workOrder);
                    }
                }
                else
                {
                    _log.ErrorFormat("Was not able to update workorderItem to sage. !result.IsSucceed");
                    return(Error("Work order item update failed", $"EditWorkOrderItem method IsSucceed==false. {resultUpdate?.ErrorMassage}."
                                 ));
                }
                return(Json(resultUpdate, JsonRequestBehavior.AllowGet));
            }
        }
Exemplo n.º 2
0
        private void ResolveWorkOrderItems(WorkOrderModel model)
        {
            var workOrderItems     = Mapper.Map <IEnumerable <SageWorkOrderItem> >(model.PartsAndLabors);
            var workOrderFromMongo = _repository.SearchFor <SageWorkOrder>(x => x.WorkOrder == model.WorkOrder).Single();

            if (workOrderItems != null)
            {
                foreach (var workOrderItem in workOrderItems)
                {
                    if ((workOrderFromMongo.WorkOrderItems != null && workOrderFromMongo.WorkOrderItems.Contains(workOrderItem)))
                    {
                        _sageApiProxy.EditWorkOrderItem(workOrderItem);
                    }
                    else
                    {
                        workOrderItem.WorkOrder = Convert.ToInt32(model.WorkOrder);
                        workOrderItem.TotalSale = workOrderItem.Quantity * workOrderItem.UnitSale;
                        if (workOrderItem.ItemType == "Parts")
                        {
                            workOrderItem.PartsSale = workOrderItem.UnitSale;
                        }
                        else
                        {
                            workOrderItem.LaborSale = workOrderItem.UnitSale;
                        }
                        if (workOrderItem.WorkOrderItem == 0)
                        {
                            _sageApiProxy.AddWorkOrderItem(workOrderItem);
                        }
                        else
                        {
                            _sageApiProxy.EditWorkOrderItem(workOrderItem);
                        }
                    }
                }


                if (workOrderFromMongo.WorkOrderItems != null)
                {
                    var idsToRemove = new List <long>();

                    foreach (var woItem in workOrderFromMongo.WorkOrderItems)
                    {
                        if (!workOrderItems.Select(x => x.WorkOrderItem).Contains(woItem.WorkOrderItem))
                        {
                            idsToRemove.Add(woItem.WorkOrderItem);
                        }
                    }

                    if (idsToRemove.Any())
                    {
                        _sageApiProxy.DeleteWorkOrderItems(Convert.ToInt32(workOrderFromMongo.WorkOrder), idsToRemove);
                    }
                }
            }
            else
            {
                if (workOrderFromMongo.WorkOrderItems != null)
                {
                    var idsToRemove = workOrderFromMongo.WorkOrderItems.Select(x => x.WorkOrderItem);

                    if (idsToRemove.Any())
                    {
                        _sageApiProxy.DeleteWorkOrderItems(Convert.ToInt32(workOrderFromMongo.WorkOrder), idsToRemove);
                    }
                }
            }
        }