示例#1
0
        /// <summary>
        /// Validate Status
        /// Validate Availabilty
        /// Update Offer
        /// Update availability
        /// Cancel other offers if exists
        /// Notify to vessel owners cancelled if exists
        /// Notify to project owner by Signal and Mail
        /// Generate Agreement Report and send Mail
        /// </summary>
        /// <param name="model"></param>
        public RequestResult <List <AlertModel> > Accept(OfferModel model, int currentPersonId)
        {
            RequestResult <List <AlertModel> > resp         = new RequestResult <List <AlertModel> >();
            MailServices               MailServ             = new MailServices();
            ITemplate                  factory              = new TemplateMessagesFactory();
            VesselServices             vesselServices       = new VesselServices();
            VesselAvailabilityServices availabilityServices = new VesselAvailabilityServices();
            AlertServices              alertServices        = new AlertServices();
            AlertTemplateServices      templateServices     = new AlertTemplateServices();
            List <AlertModel>          lstAlertToSend       = new List <AlertModel>();
            ProjectServices            projectServices      = new ProjectServices();

            TransactionOptions scopeOptions = new TransactionOptions();

            scopeOptions.IsolationLevel = IsolationLevel.Serializable;
            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, scopeOptions))
            {
                try
                {
                    //Validate
                    if (model.OfferId == null)
                    {
                        throw new Exception("REQUIRED OfferId");
                    }

                    if (model.VesselAdmin.PersonId == null)
                    {
                        throw new Exception("VesselAdmin.PersonId REQUIRED");
                    }

                    OfferModel val = GetById((int)model.OfferId);
                    if (val == null)
                    {
                        throw new Exception("NOT_FOUND");
                    }

                    if (val.Status != OfferModel.NEW)
                    {
                        if (!(val.Status == OfferModel.FIX && val.VesselAdmin.PersonId == currentPersonId))
                        {
                            throw new Exception("STATUS_NOT_VALID");
                        }
                    }

                    VesselAvailabilityModel availabilityModel = new VesselAvailabilityModel()
                    {
                        ReasonId  = VesselAvailabilityModel.DEFAULT,
                        VesselId  = val.Vessel.VesselId,
                        StartDate = val.Project.StartDate,
                        EndDate   = val.Project.EndDate
                    };
                    int val2 = vesselServices.EvalAvailability(availabilityModel);
                    if (val2 > 0)
                    {
                        throw new Exception("NOT_AVAILABILITY");
                    }

                    //Update Offer
                    model.Status = OfferModel.ACCEPTED;
                    var respOffer = InsUpd(model);
                    if (respOffer.Status != Status.Success)
                    {
                        throw new Exception(respOffer.Message);
                    }

                    //Update vessel availabilty
                    var respAvailability = availabilityServices.InsUpd(availabilityModel);
                    if (respAvailability.Status != Status.Success)
                    {
                        throw new Exception(respAvailability.Message);
                    }

                    //Update Project Status
                    var respStatus = projectServices.UpdateStatus(val.Project.ProjectId, ProjectModel.STATUS_FIXED);
                    if (respStatus.Status != Status.Success)
                    {
                        throw new Exception(respStatus.Message);
                    }

                    //Get new values
                    val = GetById((int)model.OfferId);

                    // Cancel other offers if exists
                    List <OfferModel>           lstNotifyCancel = CancelOthers((int)val.OfferId);
                    Dictionary <string, string> values          = new Dictionary <string, string>();
                    values.Add("FLAG", val.Vessel.Country.Name);
                    values.Add("HOMEPORT", val.Vessel.HomePort.Name);

                    AlertTemplateModel template = templateServices.GetById(4);
                    foreach (OfferModel offerCancelled in lstNotifyCancel)
                    {
                        if (!values.ContainsKey("FOLIO"))
                        {
                            values.Add("FOLIO", offerCancelled.Project.Folio);
                        }
                        else
                        {
                            values["FOLIO"] = offerCancelled.Project.Folio;
                        }

                        AlertModel alertCancelled = alertServices.GetWithValues(template, values);
                        alertCancelled.To = offerCancelled.ProjectAdmin.PersonId;
                        lstAlertToSend.Add(alertCancelled);
                    }


                    //Notify to project owner
                    values = new Dictionary <string, string>();
                    values.Add("IMO", val.Vessel.Imo);
                    values.Add("VESSELNAME", val.Vessel.Name);
                    values.Add("FOLIO", val.Project.Folio);
                    AlertModel alertAccepted = alertServices.GetWithValues(5, values);
                    alertAccepted.To = val.ProjectAdmin.PersonId;
                    lstAlertToSend.Add(alertAccepted);
                    var respAlert = alertServices.InsUpd(lstAlertToSend);
                    if (respAlert != null)
                    {
                        throw new Exception(respAlert.Message);
                    }

                    //Send mail
                    //Generate Agreement Report and send to mail
                    List <MailAttachments> agreementReportProject = new List <MailAttachments>();

                    SystemVariableServices        SVS   = new SystemVariableServices();
                    Dictionary <string, string[]> param = new Dictionary <string, string[]>();
                    string EgulfUrl = SVS.GetSystemVariableValue("EgulfWeb");
                    param.Add("{Enfasis}", new string[] { val.Vessel.Imo, val.Vessel.Name, val.Project.Folio });
                    param.Add("{Btn_url}", new string[] { EgulfUrl });

                    ReportServices ReportServ = new ReportServices();
                    agreementReportProject.Add(ReportServ.AgreementReportAttachment((int)model.OfferId, (int)TypeUser.Project));
                    MailServ.SendMailWithAttachment(factory.GetTemplate(val.ProjectAdmin.Email, "OfferAccepted", param), agreementReportProject);

                    param = new Dictionary <string, string[]>();
                    param.Add("{Enfasis}", new string[] { val.Project.Folio });
                    param.Add("{Btn_url}", new string[] { EgulfUrl });

                    List <MailAttachments> agreementReportVessel = new List <MailAttachments>();
                    agreementReportVessel.Add(ReportServ.AgreementReportAttachment((int)model.OfferId, (int)TypeUser.Vessel));
                    MailServ.SendMailWithAttachment(factory.GetTemplate(val.VesselAdmin.Email, "YouAcceptedOffer", param), agreementReportVessel);

                    resp.Data = lstAlertToSend;

                    ts.Complete();
                }
                catch (Exception ex)
                {
                    ts.Dispose();
                    resp = new RequestResult <List <AlertModel> >()
                    {
                        Status = Status.Error, Message = ex.Message
                    };
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                    throw ex;
                }
            }

            return(resp);
        }