public void UpdateExpiredContracts(List <Contract> contracts)
 {
     foreach (var contract in contracts)
     {
         var assignment = contract.Assignment;
         WorkflowServiceClient.TriggerWorkflowAction(contract.Id, EntityType.Contract, assignment.WorkflowDefinitionId, assignment.WorkflowInstanceId,
                                                     assignment.WorkflowVersion.Value, WorkflowActionName.Expired, "Expired Contract", this._workContext.CurrentUser.Id);
     }
 }
        public void AutoCloseServiceRequest(long?serviceRequestId)
        {
            if (serviceRequestId == null)
            {
                return;
            }

            ServiceRequest serviceRequestToClose = _serviceRequestRepository.GetById(serviceRequestId);
            var            assignment            = _assignmentRepository.GetById(serviceRequestToClose.Id);

            WorkflowServiceClient.TriggerWorkflowAction(serviceRequestToClose.Id, EntityType.ServiceRequest, assignment.WorkflowDefinitionId, assignment.WorkflowInstanceId,
                                                        assignment.WorkflowVersion.Value, WorkflowActionName.Close, "ServiceRequest", this._workContext.CurrentUser.Id);
        }
Exemplo n.º 3
0
        public virtual PurchaseOrder CreatePO(RequestForQuotationVendor rfqVendor)
        {
            var po = new PurchaseOrder
            {
                SiteId = rfqVendor.RequestForQuotation.SiteId,
                RequestForQuotationId       = rfqVendor.RequestForQuotation.Id,
                RequestForQuotationVendorId = rfqVendor.Id,
                RequestorId     = rfqVendor.RequestForQuotation.RequestorId,
                DateRequired    = rfqVendor.RequestForQuotation.DateRequired,
                ShipToAddressId = rfqVendor.RequestForQuotation.ShipToAddressId,
                VendorId        = rfqVendor.VendorId,
                Priority        = rfqVendor.RequestForQuotation.Priority,
                CreatedUserId   = this._workContext.CurrentUser.Id
            };

            _purchaseOrderRepository.Insert(po);
            var vendorItems = rfqVendor.RequestForQuotationVendorItems.Where(r => r.IsAwarded == true);

            foreach (var item in vendorItems)
            {
                po.PurchaseOrderItems.Add(new PurchaseOrderItem
                {
                    Sequence        = item.RequestForQuotationItem.Sequence,
                    ItemId          = item.RequestForQuotationItem.ItemId,
                    QuantityOrdered = item.QuantityQuoted,
                    UnitPrice       = item.UnitPriceQuoted
                });
            }

            string number = _autoNumberService.GenerateNextAutoNumber(_dateTimeHelper.ConvertToUserTime(DateTime.UtcNow, DateTimeKind.Utc), po);

            po.Number = number;

            this._dbContext.SaveChanges();
            //start workflow
            var workflowInstanceId = WorkflowServiceClient.StartWorkflow(po.Id, EntityType.PurchaseOrder, 0, this._workContext.CurrentUser.Id);

            this._dbContext.Detach(po);
            po = _purchaseOrderRepository.GetById(po.Id);
            this._dbContext.Detach(po.Assignment);
            // trigger Submit action
            WorkflowServiceClient.TriggerWorkflowAction(po.Id, EntityType.PurchaseOrder, po.Assignment.WorkflowDefinitionId, workflowInstanceId,
                                                        po.Assignment.WorkflowVersion.Value, WorkflowActionName.Submit, "", this._workContext.CurrentUser.Id);
            return(po);
        }
Exemplo n.º 4
0
        public virtual RequestForQuotation CreateRFQ(PurchaseRequest pr)
        {
            var rfq = new RequestForQuotation
            {
                SiteId            = pr.SiteId,
                PurchaseRequestId = pr.Id,
                RequestorId       = pr.RequestorId,
                DateRequired      = pr.DateRequired,
                Priority          = pr.Priority,
                CreatedUserId     = this._workContext.CurrentUser.Id
            };

            _requestForQuotationRepository.Insert(rfq);
            foreach (var prItem in pr.PurchaseRequestItems)
            {
                rfq.RequestForQuotationItems.Add(new RequestForQuotationItem
                {
                    Sequence          = prItem.Sequence,
                    ItemId            = prItem.ItemId,
                    QuantityRequested = prItem.QuantityRequested
                });
            }

            string number = _autoNumberService.GenerateNextAutoNumber(_dateTimeHelper.ConvertToUserTime(DateTime.UtcNow, DateTimeKind.Utc), rfq);

            rfq.Number = number;

            this._dbContext.SaveChanges();
            //start workflow
            var workflowInstanceId = WorkflowServiceClient.StartWorkflow(rfq.Id, EntityType.RequestForQuotation, 0, this._workContext.CurrentUser.Id);

            this._dbContext.Detach(rfq);
            rfq = _requestForQuotationRepository.GetById(rfq.Id);
            this._dbContext.Detach(rfq.Assignment);
            // trigger Submit action
            WorkflowServiceClient.TriggerWorkflowAction(rfq.Id, EntityType.RequestForQuotation, rfq.Assignment.WorkflowDefinitionId, workflowInstanceId,
                                                        rfq.Assignment.WorkflowVersion.Value, WorkflowActionName.Submit, "", this._workContext.CurrentUser.Id);
            return(rfq);
        }
        public virtual WorkOrder CreateNextWorkOrder(PreventiveMaintenance pm,
                                                     DateTime startDateTime,
                                                     DateTime endDateTime)
        {
            DateTime prevDateTime = DateTime.MinValue;

            // if there's already an active WO for this PM
            // then return
            WorkOrder existingWork = _workOrderRepository.GetAll().
                                     Where(w => w.PreventiveMaintenanceId == pm.Id &&
                                           w.Assignment.Name != WorkflowStatus.Closed &&
                                           w.Assignment.Name != WorkflowStatus.Cancelled).FirstOrDefault();

            if (existingWork != null)
            {
                return(null);
            }
            // copy work parameters
            WorkOrder workOrder = new WorkOrder();

            workOrder.CreatedUserId         = this._workContext.CurrentUser.Id;
            workOrder.RequestedDateTime     = DateTime.UtcNow;
            workOrder.ExpectedStartDateTime = startDateTime;
            workOrder.DueDateTime           = endDateTime;
            workOrder.LocationId            = pm.LocationId;
            workOrder.AssetId                 = pm.AssetId;
            workOrder.SiteId                  = pm.SiteId;
            workOrder.WorkType                = pm.WorkType;
            workOrder.WorkCategoryId          = pm.WorkCategoryId;
            workOrder.FailureGroupId          = pm.FailureGroupId;
            workOrder.Description             = workOrder.Name = pm.Description;
            workOrder.PreventiveMaintenanceId = pm.Id;
            workOrder.Priority                = pm.Priority;
            workOrder.ContractId              = pm.ContractId;
            workOrder.IsNew = false;
            foreach (var pmLabor in pm.PMLabors)
            {
                var workOrderLabor = new WorkOrderLabor();
                workOrderLabor.TeamId       = pmLabor.TeamId;
                workOrderLabor.CraftId      = pmLabor.CraftId;
                workOrderLabor.TechnicianId = pmLabor.TechnicianId;
                workOrderLabor.PlanHours    = pmLabor.PlanHours;
                workOrderLabor.StandardRate = pmLabor.StandardRate;
                workOrderLabor.OTRate       = pmLabor.OTRate;
                workOrderLabor.PlanTotal    = pmLabor.PlanTotal;
                workOrder.WorkOrderLabors.Add(workOrderLabor);
            }
            foreach (var pmTask in pm.PMTasks)
            {
                var workOrderTask = new WorkOrderTask();
                workOrder.TaskGroupId        = pm.TaskGroupId;
                workOrderTask.Sequence       = pmTask.Sequence;
                workOrderTask.Description    = pmTask.Description;
                workOrderTask.AssignedUserId = pmTask.AssignedUserId;
                workOrder.WorkOrderTasks.Add(workOrderTask);
            }

            foreach (var pmItem in pm.PMItems)
            {
                var workOrderItem = new WorkOrderItem();
                workOrderItem.StoreId       = pmItem.StoreId;
                workOrderItem.ItemId        = pmItem.ItemId;
                workOrderItem.UnitPrice     = pmItem.UnitPrice;
                workOrderItem.PlanQuantity  = pmItem.PlanQuantity;
                workOrderItem.PlanTotal     = pmItem.PlanTotal;
                workOrderItem.PlanToolHours = pmItem.PlanToolHours;
                workOrderItem.ToolRate      = pmItem.ToolRate;
                workOrder.WorkOrderItems.Add(workOrderItem);
            }

            foreach (var pmServiceItem in pm.PMServiceItems)
            {
                var workOrderServiceItem = new WorkOrderServiceItem();
                workOrderServiceItem.ServiceItemId = pmServiceItem.ServiceItemId;
                workOrderServiceItem.Description   = pmServiceItem.Description;
                workOrderServiceItem.PlanUnitPrice = pmServiceItem.PlanUnitPrice;
                workOrderServiceItem.PlanQuantity  = pmServiceItem.PlanQuantity;
                workOrderServiceItem.PlanTotal     = pmServiceItem.PlanTotal;
                workOrder.WorkOrderServiceItems.Add(workOrderServiceItem);
            }

            foreach (var pmMiscCost in pm.PMMiscCosts)
            {
                var workOrderMiscCost = new WorkOrderMiscCost();
                workOrderMiscCost.Sequence      = pmMiscCost.Sequence;
                workOrderMiscCost.Description   = pmMiscCost.Description;
                workOrderMiscCost.PlanQuantity  = pmMiscCost.PlanQuantity;
                workOrderMiscCost.PlanUnitPrice = pmMiscCost.PlanUnitPrice;
                workOrderMiscCost.PlanTotal     = pmMiscCost.PlanTotal;
                workOrder.WorkOrderMiscCosts.Add(workOrderMiscCost);
            }

            if (!pm.FirstWorkExpectedStartDateTime.HasValue || workOrder.ExpectedStartDateTime.Value <= pm.EndDateTime.Value)
            {
                string number = _autoNumberService.GenerateNextAutoNumber(DateTime.Now, workOrder);
                workOrder.Number = number;
                _workOrderRepository.InsertAndCommit(workOrder);

                // copy attachments
                CopyAttachments(workOrder.PreventiveMaintenance.PMTasks.ToList(), workOrder.WorkOrderTasks.ToList());
                this._dbContext.SaveChanges();

                // start WO workflow
                var workflowInstanceId = WorkflowServiceClient.StartWorkflow(workOrder.Id, EntityType.WorkOrder, 0, this._workContext.CurrentUser.Id);
                this._dbContext.Detach(workOrder);
                workOrder = _workOrderRepository.GetById(workOrder.Id);
                this._dbContext.Detach(workOrder.Assignment);
                var assignment = _assignmentRepository.GetById(workOrder.AssignmentId);

                // trigger action
                WorkflowServiceClient.TriggerWorkflowAction(workOrder.Id, EntityType.WorkOrder, assignment.WorkflowDefinitionId, assignment.WorkflowInstanceId,
                                                            assignment.WorkflowVersion.Value, WorkflowActionName.Submit, "PreventiveMaintenance", this._workContext.CurrentUser.Id);
            }
            return(workOrder);
        }
        public virtual WorkOrder CreateNextWorkOrderForPM(WorkOrder workOrder)
        {
            WorkOrder newWork = null;
            var       pm      = workOrder.PreventiveMaintenance;

            // only time-based
            if (pm != null && pm.FirstWorkExpectedStartDateTime.HasValue)
            {
                DateTime startDateTime = workOrder.ExpectedStartDateTime.Value;
                DateTime endDateTime   = workOrder.DueDateTime.Value;

                var isValidDate = _calendarService.DetermineNextDate(workOrder.PreventiveMaintenance, ref startDateTime, ref endDateTime);

                if (isValidDate && startDateTime <= workOrder.PreventiveMaintenance.EndDateTime.Value)
                {
                    // create a new work order
                    newWork = new WorkOrder();
                    newWork.CreatedUserId         = this._workContext.CurrentUser.Id;
                    newWork.RequestedDateTime     = DateTime.UtcNow;
                    newWork.ExpectedStartDateTime = startDateTime;
                    newWork.DueDateTime           = endDateTime;
                    newWork.LocationId            = workOrder.PreventiveMaintenance.LocationId;
                    newWork.AssetId                 = workOrder.PreventiveMaintenance.AssetId;
                    newWork.SiteId                  = workOrder.SiteId;
                    newWork.WorkType                = workOrder.PreventiveMaintenance.WorkType;
                    newWork.WorkCategoryId          = workOrder.PreventiveMaintenance.WorkCategoryId;
                    newWork.FailureGroupId          = workOrder.PreventiveMaintenance.FailureGroupId;
                    newWork.Description             = newWork.Name = workOrder.PreventiveMaintenance.Description;
                    newWork.Priority                = workOrder.PreventiveMaintenance.Priority;
                    newWork.PreventiveMaintenanceId = workOrder.PreventiveMaintenanceId;
                    newWork.ContractId              = pm.ContractId;
                    newWork.IsNew = false;

                    foreach (var pmLabor in workOrder.PreventiveMaintenance.PMLabors)
                    {
                        var workOrderLabor = new WorkOrderLabor();
                        workOrderLabor.TeamId       = pmLabor.TeamId;
                        workOrderLabor.CraftId      = pmLabor.CraftId;
                        workOrderLabor.TechnicianId = pmLabor.TechnicianId;
                        workOrderLabor.PlanHours    = pmLabor.PlanHours;
                        workOrderLabor.StandardRate = pmLabor.StandardRate;
                        workOrderLabor.OTRate       = pmLabor.OTRate;
                        workOrderLabor.PlanTotal    = pmLabor.PlanTotal;
                        newWork.WorkOrderLabors.Add(workOrderLabor);
                    }

                    foreach (var pmTask in workOrder.PreventiveMaintenance.PMTasks)
                    {
                        var workOrderTask = new WorkOrderTask();
                        workOrder.TaskGroupId        = workOrder.PreventiveMaintenance.TaskGroupId;
                        workOrderTask.Sequence       = pmTask.Sequence;
                        workOrderTask.Description    = pmTask.Description;
                        workOrderTask.AssignedUserId = pmTask.AssignedUserId;
                        newWork.WorkOrderTasks.Add(workOrderTask);
                    }

                    foreach (var pmItem in workOrder.PreventiveMaintenance.PMItems)
                    {
                        var workOrderItem = new WorkOrderItem();
                        workOrderItem.StoreId       = pmItem.StoreId;
                        workOrderItem.ItemId        = pmItem.ItemId;
                        workOrderItem.UnitPrice     = pmItem.UnitPrice;
                        workOrderItem.PlanQuantity  = pmItem.PlanQuantity;
                        workOrderItem.PlanTotal     = pmItem.PlanTotal;
                        workOrderItem.PlanToolHours = pmItem.PlanToolHours;
                        workOrderItem.ToolRate      = pmItem.ToolRate;
                        newWork.WorkOrderItems.Add(workOrderItem);
                    }

                    foreach (var pmServiceItem in workOrder.PreventiveMaintenance.PMServiceItems)
                    {
                        var workOrderServiceItem = new WorkOrderServiceItem();
                        workOrderServiceItem.ServiceItemId = pmServiceItem.ServiceItemId;
                        workOrderServiceItem.Description   = pmServiceItem.Description;
                        workOrderServiceItem.PlanUnitPrice = pmServiceItem.PlanUnitPrice;
                        workOrderServiceItem.PlanQuantity  = pmServiceItem.PlanQuantity;
                        workOrderServiceItem.PlanTotal     = pmServiceItem.PlanTotal;
                        newWork.WorkOrderServiceItems.Add(workOrderServiceItem);
                    }

                    foreach (var pmMiscCost in workOrder.PreventiveMaintenance.PMMiscCosts)
                    {
                        var workOrderMiscCost = new WorkOrderMiscCost();
                        workOrderMiscCost.Sequence      = pmMiscCost.Sequence;
                        workOrderMiscCost.Description   = pmMiscCost.Description;
                        workOrderMiscCost.PlanQuantity  = pmMiscCost.PlanQuantity;
                        workOrderMiscCost.PlanUnitPrice = pmMiscCost.PlanUnitPrice;
                        workOrderMiscCost.PlanTotal     = pmMiscCost.PlanTotal;
                        newWork.WorkOrderMiscCosts.Add(workOrderMiscCost);
                    }

                    string number = _autoNumberService.GenerateNextAutoNumber(DateTime.Now, newWork);
                    newWork.Number = number;
                    _workOrderRepository.InsertAndCommit(newWork);

                    // copy attachments
                    _pmService.CopyAttachments(workOrder.PreventiveMaintenance.PMTasks.ToList(), newWork.WorkOrderTasks.ToList());
                    this._dbContext.SaveChanges();

                    // Start workflow
                    var workflowInstanceId = WorkflowServiceClient.StartWorkflow(newWork.Id, EntityType.WorkOrder, 0, this._workContext.CurrentUser.Id);
                    //Nguyen Le: reload work because there's data changes from workflow, need to detach first because of EF caching
                    this._dbContext.Detach(newWork);
                    newWork = _workOrderRepository.GetById(newWork.Id);

                    this._dbContext.Detach(newWork.Assignment);
                    var assignment = _assignmentRepository.GetById(newWork.AssignmentId);
                    WorkflowServiceClient.TriggerWorkflowAction(newWork.Id, EntityType.WorkOrder, assignment.WorkflowDefinitionId, assignment.WorkflowInstanceId,
                                                                assignment.WorkflowVersion.Value, WorkflowActionName.Submit, "WorkOrder", this._workContext.CurrentUser.Id);
                }
                else
                {
                    _pmService.ClosePM(pm);
                }
            }
            return(newWork);
        }