/*
          BusinessProcessState createdstate = new BusinessProcessState
                        {
                            DatePerformed = DateTime.Now,
                            PerformedBy = "System",
                            Comment = "Created workflow for Payment Request"

                        };
                        _PaymentRequestservice.Create(request);

                        BusinessProcess bp = _BusinessProcessService.CreateBusinessProcess(BP_PR,request.PaymentRequestID,
                                                                                           "PaymentRequest", createdstate);
                        request.BusinessProcessID = bp.BusinessProcessID;
         */
        public BusinessProcess CreateBusinessProcessForObject(int templateID, int DocumentID, string DocumentType,bool save=false)
        {
            StateTemplate startingTemplate = _unitOfWork.StateTemplateRepository.FindBy(s => s.ParentProcessTemplateID == templateID && s.StateType == 0).Single();
            BusinessProcess bp = new BusinessProcess { ProcessTypeID = templateID, DocumentID = DocumentID, DocumentType = DocumentType };
            BusinessProcessState StartingState = new BusinessProcessState
            {
                DatePerformed = DateTime.Now,
                PerformedBy = "System",
                Comment = "Created workflow for" + DocumentType

            };
             _unitOfWork.BusinessProcessRepository.Add(bp);

            StartingState.ParentBusinessProcess = bp;
            StartingState.StateID = startingTemplate.StateTemplateID;

            _unitOfWork.BusinessProcessStateRepository.Add(StartingState);
            bp.CurrentStateID = StartingState.BusinessProcessStateID;

             //   _unitOfWork.BusinessProcessRepository.Edit(bp);
              //  PromotWorkflow(StartingState);

            if (save)
            {
                _unitOfWork.Save();

            }
            return bp;
        }
        public ActionResult CreateKendo([DataSourceRequest] DataSourceRequest request, BusinessProcess item)
        {
            if (item != null && ModelState.IsValid)
            {
                _BusinessProcessService.Add(item);
            }

            return Json(new[] { item }.ToDataSourceResult(request, ModelState));
        }
        public BusinessProcess CreateBusinessProcess(int templateID, int DocumentID, string DocumentType, BusinessProcessState StartingState)
        {
            StateTemplate startingTemplate=_unitOfWork.StateTemplateRepository.FindBy(s => s.ParentProcessTemplateID == templateID && s.StateType == 0).Single();
            BusinessProcess bp = new BusinessProcess { ProcessTypeID = templateID, DocumentID = DocumentID, DocumentType = DocumentType };
            Add(bp);
            StartingState.ParentBusinessProcessID = bp.BusinessProcessID;
            StartingState.StateID = startingTemplate.StateTemplateID;
            PromotWorkflow(StartingState);

            return bp;
        }
        public ActionResult Create(BusinessProcess item)
        {
            if (ModelState.IsValid)
            {
                _BusinessProcessService.Add(item);

                return RedirectToAction("Index");
            }

            return View(item);
        }
 public BusinessProcess CreateBusinessProcess(int templateID, int documentID, string documentType, BusinessProcessState startingState)
 {
     var startingTemplate=_unitOfWork.StateTemplateRepository.FindBy(s => s.ParentProcessTemplateID == templateID && s.StateType == 0).FirstOrDefault();
     if (startingTemplate==null)
         return null;
     var bp = new BusinessProcess { ProcessTypeID = templateID, DocumentID = documentID, DocumentType = documentType };
     Add(bp);
     startingState.ParentBusinessProcessID = bp.BusinessProcessID;
     startingState.StateID = startingTemplate.StateTemplateID;
     PromotWorkflow(startingState);
     return bp;
 }
 public bool Add(BusinessProcess item)
 {
     _unitOfWork.BusinessProcessRepository.Add(item);
     _unitOfWork.Save();
     return true;
 }
 public bool Update(BusinessProcess item)
 {
     if (item == null) return false;
     _unitOfWork.BusinessProcessRepository.Edit(item);
     _unitOfWork.Save();
     return true;
 }
 public ActionResult EditKendo([DataSourceRequest] DataSourceRequest request, BusinessProcess item)
 {
     if (ModelState.IsValid)
     {
         _BusinessProcessService.Update(item);
         return Json(ModelState.ToDataSourceResult());
     }
     return Json(ModelState.ToDataSourceResult());
 }
        public ActionResult DeleteKendo([DataSourceRequest] DataSourceRequest request, BusinessProcess item)
        {
            if (item != null)
            {
                _BusinessProcessService.DeleteById(item.BusinessProcessID);
            }

            return Json(ModelState.ToDataSourceResult());
        }