Ejemplo n.º 1
0
        // POST: api/SoaApi
        public HttpResponseMessage Post([FromBody] SoaModel model)
        {
            try
            {
                var prevSoa = Uow.Soas.GetAll()
                              .Where(s => s.ClientId == model.ClientId &&
                                     s.InsuranceProviderId == model.InsuranceProviderId &&
                                     s.Status.Name.ToLower() == "unpaid")
                              .FirstOrDefault();
                if (prevSoa != null)
                {
                    prevSoa.StatusId = AttributeProviderSvc.GetSoaStatusIdFromName("closed");
                    Uow.Soas.Update(prevSoa);
                    //Uow.SaveChanges();
                    LogEdit(prevSoa);
                }

                // To include time in Issue Date for checking against Invoice list
                DateTime dateTimeNow = DateTime.Now;
                if ((model.IssueDate.Year == dateTimeNow.Year) &&
                    (model.IssueDate.Month == dateTimeNow.Month) &&
                    (model.IssueDate.Day == dateTimeNow.Day))
                {
                    model.IssueDate = dateTimeNow;
                }

                // EDWIN
                var list = Uow.Invoices.GetAll()
                           .Where(i => i.Policy.ClientId == model.ClientId &&
                                  i.Policy.InsuranceProviderId == model.InsuranceProviderId &&
                                  i.Status.Name.ToLower() == "unpaid" &&
                                  i.IssueDate < model.IssueDate)
                           .OrderBy(i => i.IssueDate)
                           .Select(i => new { TotalAmountDue = i.TotalAmountDue, InvoiceIssueDate = i.IssueDate })
                           .ToList();
                model.TotalAmountDue = list.Sum(i => i.TotalAmountDue);

                if (model.TotalAmountDue <= 0)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, new { errorMessage = "Unable to create SOA record; Invoice not yet approved or sent to client" }));
                }
                // EDWIN END

                model.StatusId = AttributeProviderSvc.GetSoaStatusIdFromName("unpaid");

                var entity = AutoMapper.Mapper.Map <Soa>(model);
                // EDWIN
                var firstInvoice = list.FirstOrDefault();
                if (firstInvoice != null)
                {
                    entity.StartDate = firstInvoice.InvoiceIssueDate;
                }
                else
                {
                    entity.StartDate = DateTime.Now;
                }
                // EDWIN END

                Uow.Soas.Add(entity);
                Uow.SaveChanges();

                LogAdd(entity);

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Create(SoaModel model)
        {
            try
            {
                var prevSoa = Uow.Soas.GetAll()
                              .Where(s => s.ClientId == model.ClientId &&
                                     s.InsuranceProviderId == model.InsuranceProviderId &&
                                     s.Status.Name.ToLower() == "unpaid")
                              .FirstOrDefault();
                if (prevSoa != null)
                {
                    prevSoa.StatusId = AttributeProviderSvc.GetSoaStatusIdFromName("closed");
                    Uow.Soas.Update(prevSoa);
                    Uow.SaveChanges();
                    LogEdit(prevSoa);
                }

                // To include time in Issue Date for checking against Invoice list
                DateTime dateTimeNow = DateTime.Now;
                if ((model.IssueDate.Year == dateTimeNow.Year) &&
                    (model.IssueDate.Month == dateTimeNow.Month) &&
                    (model.IssueDate.Day == dateTimeNow.Day))
                {
                    model.IssueDate = dateTimeNow;
                }

                // EDWIN
                var list = Uow.Invoices.GetAll()
                           .Where(i => i.Policy.ClientId == model.ClientId &&
                                  i.Policy.InsuranceProviderId == model.InsuranceProviderId &&
                                  i.Status.Name.ToLower() == "unpaid" &&
                                  i.IssueDate < model.IssueDate)
                           .OrderBy(i => i.IssueDate)
                           .Select(i => new { TotalAmountDue = i.TotalAmountDue, InvoiceIssueDate = i.IssueDate })
                           .ToList();
                model.TotalAmountDue = list.Sum(i => i.TotalAmountDue);
                // EDWIN END

                model.StatusId = AttributeProviderSvc.GetSoaStatusIdFromName("unpaid");

                var entity = AutoMapper.Mapper.Map <Soa>(model);
                // EDWIN
                var firstInvoice = list.FirstOrDefault();
                if (firstInvoice != null)
                {
                    entity.StartDate = firstInvoice.InvoiceIssueDate;
                }
                else
                {
                    entity.StartDate = DateTime.Now;
                }
                // EDWIN END

                Uow.Soas.Add(entity);
                Uow.SaveChanges();

                LogAdd(entity);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Index", new { error = ex.Message }));
            }
        }