public void AddSite(int contractID, int siteDetailID)
        {
            var siteDetailRepo = new SiteDetailRepository()
            {
                DB = DB
            };
            var siteDetail = siteDetailRepo.Get(siteDetailID);

            ContractDetail e = new ContractDetail();

            e.ContractID = contractID;
            e.SiteID     = siteDetail.SiteID;

            e.Format         = siteDetail.Format;
            e.Type           = siteDetail.Type;
            e.Height         = siteDetail.Height;
            e.Width          = siteDetail.Width;
            e.SiteDetailName = siteDetail.Name;

            DB.ContractDetails.AddObject(e);

            Save();

            ContractDetailRepository contractDetailRepository = new ContractDetailRepository();

            contractDetailRepository.CopyTimeline(e.ID);
        }
        public SiteMonitoring Create(Action <SiteMonitoring> updateMethod, IEnumerable <HttpPostedFileBase> files, int?contractDetailID, string[] noteList)
        {
            var contractDetailRepository = new ContractDetailRepository()
            {
                DB = DB
            };

            var contractDetail = contractDetailRepository.Get(contractDetailID.Value);

            SiteMonitoring e = new SiteMonitoring();

            updateMethod(e);

            e.ContractDetail = contractDetail;

            Save();

            PicasaRepository picasaRepository = new PicasaRepository();

            picasaRepository.DB = DB;

            picasaRepository.UploadPhoto(e, files, noteList, false);

            Save();

            return(e);
        }
        public ContractDetail DeleteTheLastTimeline(int contractDetailID)
        {
            ContractDetailRepository contractDetailRepo = new ContractDetailRepository()
            {
                DB = DB
            };
            var r            = contractDetailRepo.Get(contractDetailID);
            var lastTimeline = r.ContractDetailTimelines.OrderByDescending(r1 => r1.Order).FirstOrDefault();

            if (lastTimeline != null)
            {
                if (r.SiteMonitorings.Where(r1 => r1.Order == lastTimeline.Order).Count() == 0)
                {
                    DB.DeleteObject(lastTimeline);
                    Save();
                }
            }
            return(r);
        }
        public ContractDetailTimeline Add(int contractDetailID, int timeCount, string timeUnit)
        {
            timeCount = timeCount < 1 ? 1 : timeCount;
            ContractDetailRepository contractDetailRepo = new ContractDetailRepository()
            {
                DB = DB
            };
            var contractDetail = contractDetailRepo.Get(contractDetailID);

            var lastTimeline = contractDetail.ContractDetailTimelines.OrderByDescending(r => r.Order).FirstOrDefault();

            var newTimeline = new ContractDetailTimeline();

            if (lastTimeline == null)
            {
                newTimeline.Order    = 1;
                newTimeline.FromDate = contractDetail.EffectiveDate.HasValue ? contractDetail.EffectiveDate : DateTime.Now.Date;
            }
            else
            {
                newTimeline.Order    = lastTimeline.Order + 1;
                newTimeline.FromDate = lastTimeline.ToDate.Value.AddDays(1);
            }

            if (timeUnit.ToLower() == "day")
            {
                newTimeline.ToDate = newTimeline.FromDate.Value.AddDays(timeCount).AddDays(-1);
            }
            else if (timeUnit.ToLower() == "month")
            {
                newTimeline.ToDate = newTimeline.FromDate.Value.AddMonths(timeCount).AddDays(-1);
            }

            newTimeline.ContractDetailID = contractDetailID;

            DB.AddToContractDetailTimelines(newTimeline);

            Save();

            return(newTimeline);
        }
        public SiteMonitoring InitWithDefaultValue(int?ContractDetailID)
        {
            SiteMonitoring e = new SiteMonitoring();

            e.ContractDetailID = ContractDetailID;

            e.Working = true;
            e.Clean   = true;
            e.CreativeGoodConditon = true;
            e.ExternalInterference = false;
            e.Vandalism            = false;
            ContractDetailRepository cdrepo = new ContractDetailRepository();
            ContractDetail           cd     = cdrepo.Get(ContractDetailID.ToInt());

            e.ContractDetail = cd;
            e.NoOfBullbs     = cd.Site.FrontlitNumerOfLamps;
            e.Site           = cd.Site;
            e.ProductID      = cd.ProductID;
            e.Product        = cd.Product;
            return(e);
        }
        public IEnumerable <SelectListItem> FilterOrderForCreate(int contractDetailID)
        {
            ContractDetailRepository contractDetailRepository = new ContractDetailRepository();
            SiteMonitoringRepository siteMonitoringRepository = new SiteMonitoringRepository();

            var contractDetail = contractDetailRepository.Get(contractDetailID);

            var orderList = contractDetail.SiteMonitorings.Select(r => r.Order).Distinct();

            var timelineList = contractDetail.ContractDetailTimelines
                               .ToList()
                               .Where(r => !orderList.Contains(r.Order))
                               .OrderBy(r => r.Order)
                               .Select(r => new SelectListItem()
            {
                Value = r.Order.ToString(), Text = string.Format("{0}: {1} - {2}", r.Order, String.Format("{0:d}", r.FromDate), String.Format("{0:d}", r.ToDate))
            });

            ;

            return(timelineList);
        }