Пример #1
0
        public ResponseMessage CreateDispatch(TWCCDispatchPayload payload)
        {
            try {
                string dispatchNo     = string.Empty;
                string structCode     = string.Empty;
                int    dispReuseCount = 0;
                int    projectId      = 1;
                List <dispatchedStrucCount> result = new List <dispatchedStrucCount>();
                string count = string.Format("select count(*) as cnt from dispatch_requirement dr inner join disp_req_structure drs on  dr.id  = drs.dispreq_id inner join project_structure ps    on drs.proj_struct_id  = ps.id  where ps.structure_id = {0} and dr.sitereq_id = {1}", payload.StructureId, payload.siteRequirementId);
                result = _context.Set <dispatchedStrucCount>().FromSqlRaw(count).ToList();
                int dispatchedQuantity = result.FirstOrDefault().cnt;
                int excedCout          = dispatchedQuantity + payload.Quantity;
                int totalcount         = _context.SiteReqStructure.Single(s => s.SiteReqId == payload.siteRequirementId && s.StructId == payload.StructureId).Quantity ?? 0;
                if (totalcount < excedCout)
                {
                    throw new ValueNotFoundException("Dispatch quantity should match the required quantity.");
                }
                ServiceType servType = _context.ServiceType.Where(x => x.Id == payload.ServiceTypeId).FirstOrDefault();
                if (servType.Name == commonEnum.ServiceType.Fabrication.ToString())
                {
                    dispReuseCount = _context.DispatchRequirement.Include(m => m.Servicetype).Where(x => x.DispatchNo.Contains("DC")).Count() + 1;
                }
                else if (servType.Name == commonEnum.ServiceType.OutSourcing.ToString())
                {
                    dispReuseCount = _context.DispatchRequirement.Include(m => m.Servicetype).Where(x => x.DispatchNo.Contains("DC")).Count() + 1;
                }
                else if (servType.Name == commonEnum.ServiceType.Reuse.ToString())
                {
                    dispReuseCount = _context.DispatchRequirement.Include(m => m.Servicetype).Where(x => x.DispatchNo.Contains("DA")).Count() + 1;
                }

                SiteRequirement siteReqr = _context.SiteRequirement.Include(c => c.SiteReqStructure).Where(x => x.Id == payload.siteRequirementId).FirstOrDefault();
                //   for structure id override in multiple component
                if (servType.Name == commonEnum.ServiceType.Fabrication.ToString() || servType.Name == commonEnum.ServiceType.OutSourcing.ToString())
                {
                    // int structCount = _context.ProjectStructure.Count () + 1;
                    // structCode = constantVal.StructureIdPrefix + structCount.ToString ().PadLeft (6, '0');
                    dispatchNo = constantVal.DispVendorPrefix + dispReuseCount.ToString().PadLeft(6, '0');
                }
                if (servType.Name == commonEnum.ServiceType.Reuse.ToString())
                {
                    dispatchNo = constantVal.DispReusePrefix + dispReuseCount.ToString().PadLeft(6, '0');
                }
                ResponseMessage responseMessage = new ResponseMessage();
                using (var transaction = _context.Database.BeginTransaction()) {
                    try {
                        DispatchRequirement dispReq = new DispatchRequirement();
                        dispReq.CreatedAt       = DateTime.Now;
                        dispReq.CreatedBy       = payload.CreatedBy; //TODO
                        dispReq.DispatchNo      = dispatchNo;
                        dispReq.RoleId          = 1;                 // TODO
                        dispReq.ServicetypeId   = payload.ServiceTypeId;
                        dispReq.SitereqId       = payload.siteRequirementId;
                        dispReq.SiteReqStructid = _context.SiteReqStructure.Where(x => x.SiteReqId == payload.siteRequirementId && x.StructId == payload.StructureId).FirstOrDefault().Id;
                        dispReq.Status          = Util.GetDescription(commonEnum.SiteDispatchSatus.NEW).ToString();
                        dispReq.StatusInternal  = Util.GetDescription(commonEnum.SiteDispatchSatus.NEW).ToString();
                        dispReq.ToProjectid     = payload.ToProjectId;
                        dispReq.Quantity        = payload.Quantity;
                        _context.DispatchRequirement.Add(dispReq);
                        _context.SaveChanges();
                        int structCountDb = _context.ProjectStructure.Count();
                        for (int iQty = 1; iQty <= payload.Quantity; iQty++)
                        {
                            if (servType.Name == commonEnum.ServiceType.Fabrication.ToString() || servType.Name == commonEnum.ServiceType.OutSourcing.ToString())
                            {
                                int structCount = structCountDb + iQty;
                                structCode = constantVal.StructureIdPrefix + structCount.ToString().PadLeft(6, '0');
                            }

                            ProjectStructure projectStructure = new ProjectStructure();
                            if (servType.Name == commonEnum.ServiceType.Fabrication.ToString() || servType.Name == commonEnum.ServiceType.OutSourcing.ToString())
                            {
                                SiteReqStructure siteRequirementStructure = _context.SiteReqStructure.Where(x => x.SiteReqId == payload.siteRequirementId && x.StructId == payload.StructureId).FirstOrDefault();

                                projectStructure.StructureId            = payload.StructureId;
                                projectStructure.StructCode             = structCode;
                                projectStructure.ProjectId              = payload.ToProjectId;
                                projectStructure.DrawingNo              = "";
                                projectStructure.ComponentsCount        = 0;
                                projectStructure.StructureAttributesVal = siteRequirementStructure != null ? siteRequirementStructure.StructureAttributesVal : "";
                                projectStructure.EstimatedWeight        = 0;
                                projectStructure.StructureStatus        = Util.GetDescription(commonEnum.StructureStatus.NOTAVAILABLE).ToString();
                                projectStructure.CurrentStatus          = Util.GetDescription(commonEnum.StructureInternalStatus.DISPATCHINPROGRESS).ToString();
                                projectStructure.IsDelete  = false;
                                projectStructure.CreatedBy = payload.CreatedBy;
                                projectStructure.CreatedAt = DateTime.Now;
                                _context.ProjectStructure.Add(projectStructure);
                                _context.SaveChanges();
                            }
                            else
                            {
                                ProjectStructure structDB = _context.ProjectStructure.Where(x => x.Id == payload.ProjectStructureId).FirstOrDefault();
                                structDB.CurrentStatus   = Util.GetDescription(commonEnum.StructureInternalStatus.DISPATCHINPROGRESS).ToString();
                                structDB.StructureStatus = Util.GetDescription(commonEnum.StructureStatus.NOTAVAILABLE).ToString();
                                _context.SaveChanges();
                            }

                            DispReqStructure dispStrcture = new DispReqStructure();
                            dispStrcture.ProjStructId = servType.Name == commonEnum.ServiceType.Fabrication.ToString() || servType.Name == commonEnum.ServiceType.OutSourcing.ToString() ? projectStructure.Id : payload.ProjectStructureId;
                            dispStrcture.DispreqId    = dispReq.Id;
                            if (servType.Name == commonEnum.ServiceType.Reuse.ToString())
                            {
                                dispStrcture.FromProjectId = payload.FromProjectId;
                                dispStrcture.SurplusDate   = payload.SurplusFromDate;
                            }
                            dispStrcture.DispStructStatus = Util.GetDescription(commonEnum.SiteDispStructureStatus.NEW).ToString();
                            _context.DispReqStructure.Add(dispStrcture);
                            _context.SaveChanges();


                            if (servType.Name == commonEnum.ServiceType.Reuse.ToString())
                            {
                                var componentList = _context.Component.Where(x => x.ProjStructId == payload.ProjectStructureId).ToList();
                                foreach (Component comp in componentList)
                                {
                                    DispStructureComp dsc = new DispStructureComp();
                                    dsc.DispStructureId = dispStrcture.Id;
                                    dsc.DispCompId      = comp.Id;
                                    _context.DispStructureComp.Add(dsc);
                                }
                                _context.SaveChanges();
                            }
                        }

                        /***update site requirement Structure status  ***/

                        CodeList         dispQty = _context.Query <CodeList>().FromSqlRaw(string.Format("select  count(*) as Id,'' as Name , 0 as ServiceTypeId from dispatch_requirement dr inner join disp_req_structure drs on dr.id = drs.dispreq_id  inner join project_structure ps  on  drs.proj_struct_id = ps.id where ps.structure_id ={0} and dr.sitereq_id ={1} and drs.disp_struct_status <> '{2}'", payload.StructureId, payload.siteRequirementId, Util.GetDescription(commonEnum.SiteDispStructureStatus.REJECT).ToString())).FirstOrDefault();
                        var              dispatchedStrucCount = dispQty.Id;
                        SiteRequirement  dbSiteReq            = _context.SiteRequirement.Where(x => x.Id == payload.siteRequirementId).FirstOrDefault();
                        SiteReqStructure dbSiteReqStructure   = _context.SiteReqStructure.Where(x => x.SiteReqId == payload.siteRequirementId && x.StructId == payload.StructureId).FirstOrDefault();
                        if (dbSiteReqStructure.Quantity > dispatchedStrucCount)
                        {
                            dbSiteReqStructure.Status = Util.GetDescription(commonEnum.SiteRequiremntStatus.PARTIALLYDISPATCHED).ToString();
                        }
                        else
                        {
                            dbSiteReqStructure.Status = Util.GetDescription(commonEnum.SiteRequiremntStatus.DISPATCHED).ToString();
                        }
                        _context.SaveChanges();


                        /***update site requirement status ***/
                        List <SiteReqStructure> lstReqStructure = _context.SiteReqStructure.Where(x => x.SiteReqId == payload.siteRequirementId).ToList();
                        if (lstReqStructure.Count() > 0 && lstReqStructure.Where(x => x.Status != Util.GetDescription(commonEnum.SiteRequiremntStatus.DISPATCHED).ToString()).Count() > 0)
                        {
                            dbSiteReq.Status         = Util.GetDescription(commonEnum.SiteRequiremntStatus.PARTIALLYDISPATCHED).ToString();
                            dbSiteReq.StatusInternal = Util.GetDescription(commonEnum.SiteRequiremntStatus.PARTIALLYDISPATCHED).ToString();
                        }
                        else
                        {
                            dbSiteReq.Status         = Util.GetDescription(commonEnum.SiteRequiremntStatus.DISPATCHED).ToString();
                            dbSiteReq.StatusInternal = Util.GetDescription(commonEnum.SiteRequiremntStatus.DISPATCHED).ToString();
                        }
                        _context.SaveChanges();


                        DisreqStatusHistory dispatchReqStatusHistory = new DisreqStatusHistory();
                        dispatchReqStatusHistory.DispatchNo     = dispatchNo;
                        dispatchReqStatusHistory.DispreqId      = dispReq.Id;
                        dispatchReqStatusHistory.Status         = Util.GetDescription(commonEnum.SiteDispatchSatus.NEW).ToString();
                        dispatchReqStatusHistory.StatusInternal = Util.GetDescription(commonEnum.SiteDispatchSatus.NEW).ToString();
                        dispatchReqStatusHistory.Notes          = "";
                        dispatchReqStatusHistory.RoleId         = payload.RoleId;
                        dispatchReqStatusHistory.CreatedBy      = payload.CreatedBy;
                        dispatchReqStatusHistory.CreatedAt      = DateTime.Now;
                        _context.DisreqStatusHistory.Add(dispatchReqStatusHistory);
                        _context.SaveChanges();
                        transaction.Commit();
                        responseMessage = new ResponseMessage()
                        {
                            Message = "Saved Successfully"
                        };
                    } catch (Exception ex) {
                        transaction.Rollback();
                        responseMessage = new ResponseMessage()
                        {
                            Message = "Error was found. Exception : " + ex.Message
                        };
                        throw ex;
                    }
                }

                return(responseMessage);
            } catch (Exception ex) {
                throw ex;
            }
        }