예제 #1
0
    public string sendRFQ()
    {
        List <RFQ>      resultRFQs = new List <RFQ>();
        GatewayResponse response   = new GatewayResponse();
        String          s;
        NewRFQScreen    newRFQScreen;

        try
        {
            s            = new StreamReader(Request.InputStream).ReadToEnd();
            newRFQScreen = JsonConvert.DeserializeObject <NewRFQScreen>(s);
        }
        catch (Exception ex)
        {
            response.ErrorThrown         = true;
            response.ResponseDescription = "ERROR: When trying to parse JSON in server. " + ex.Message;
            return(JsonConvert.SerializeObject(response));
        }

        RFQNumberCRUD rfqNumberCRUD  = new RFQNumberCRUD();
        bomDetailCRUD bomDetail_CRUD = new bomDetailCRUD();
        UserCRUD      user_CRUD      = new UserCRUD();

        string baseBOMAttachmentsPath = ConfigurationManager.AppSettings["BOMLineAttachments"];
        string baseRFQAttachmentsPath = ConfigurationManager.AppSettings["RFQAttachmentsSent"];

        string strAuthUser = HttpContext.Current.User.Identity.Name;
        User   user        = user_CRUD.readById(strAuthUser);

        List <SIFDetail> EAUsList = newRFQScreen.SIFVolumesList;

        if (EAUsList.Count == 0)
        {
            response.ErrorThrown         = true;
            response.ResponseDescription = "ERROR: Cannot create RFQ without EAU specified.";
            return(JsonConvert.SerializeObject(response));
        }

        List <RFQEAV> newEAUList = new List <RFQEAV>();

        foreach (SIFDetail eau in EAUsList)
        {
            RFQEAV eauParsed = new RFQEAV();
            eauParsed.Volume = eau.ProjectedAnnualVolume;
            eauParsed.Year   = eau.ProgramYear;
            RFQEAV alreadyExists = newEAUList.Find(eauInternal => eauInternal.Volume == eauParsed.Volume);
            if (alreadyExists == null)
            {
                newEAUList.Add(eauParsed);
            }
            else
            {
                alreadyExists.Year = alreadyExists.Year.Trim() + ", " + eauParsed.Year.Trim();
            }
        }

        List <BOMDetail> bomDetailList = newRFQScreen.BomDetailList;


        ConnectionManager CM = new ConnectionManager();

        Data_Base_MNG.SQL DM = CM.getDataManager();

        if (newRFQScreen.SupplierList.Count > 0)
        {
            foreach (Supplier supplier in newRFQScreen.SupplierList)
            {
                foreach (BOMDetail component in bomDetailList)
                {
                    RFQNumberEntity rfqNumber = new RFQNumberEntity();

                    rfqNumber.BOMDetailKey = component.Id;
                    rfqNumber.SifHeaderKey = newRFQScreen.SIFHeaderID;
                    rfqNumber.RFQNumber    = rfqNumberCRUD.generateNewRFQNumber(rfqNumber.SifHeaderKey);

                    if (rfqNumber.RFQNumber == -1)
                    {
                        response.ErrorThrown         = true;
                        response.ResponseDescription = "ERROR: There was an error generating a new RFQ number.";
                        return(JsonConvert.SerializeObject(response));
                    }

                    CM = new ConnectionManager();
                    DM = CM.getDataManager();

                    /*Begin Transaction*/
                    DM.Open_Connection("Send New RFQ");

                    RFQ             rfq = new RFQ();
                    RFQNumberEntity rfqNumberGenereated = rfqNumberCRUD.create_return_object(rfqNumber, ref DM);
                    if (rfqNumberCRUD.ErrorOccur)
                    {
                        response.ErrorThrown         = true;
                        response.ResponseDescription = "ERROR:" + rfqNumberCRUD.ErrorMessage;
                        return(JsonConvert.SerializeObject(response));
                    }
                    else
                    {
                        //rfq.SentAttachmentsFolder = newRFQScreen.FolderAttachments;

                        newRFQScreen.FolderAttachments = newRFQScreen.FolderAttachments.Trim();
                        if (newRFQScreen.FolderAttachments != "")
                        {
                            if (System.IO.Directory.Exists((baseRFQAttachmentsPath + newRFQScreen.FolderAttachments)))
                            {
                                DirectoryInfo directory = new DirectoryInfo((baseRFQAttachmentsPath + newRFQScreen.FolderAttachments));
                                if (directory.GetFiles().Length > 0)
                                {
                                    string folderName = "";
                                    do
                                    {
                                        DateTime date = DateTime.Now;
                                        folderName = date.Year.ToString() + date.Month.ToString() +
                                                     date.Day.ToString() + "_" + MD5HashGenerator.GenerateKey(date);
                                    } while (Directory.Exists((baseRFQAttachmentsPath + folderName)));
                                    Directory.CreateDirectory((baseRFQAttachmentsPath + folderName));
                                    rfq.SentAttachmentsFolder = folderName;


                                    foreach (FileInfo file in directory.GetFiles())
                                    {
                                        try
                                        {
                                            File.Copy(file.FullName, (baseRFQAttachmentsPath + rfq.SentAttachmentsFolder + @"\" + file.Name), true);
                                        }
                                        catch (Exception ex)
                                        {
                                            response.ErrorThrown         = true;
                                            response.ResponseDescription = "ERROR:" + ex.Message;
                                            return(JsonConvert.SerializeObject(response));
                                        }
                                    }
                                }
                            }
                        }

                        foreach (Attachment bomAttachment in component.AttachmentsList)
                        {
                            if (rfq.SentAttachmentsFolder.Trim() == "")
                            {
                                string folderName = "";
                                do
                                {
                                    DateTime date = DateTime.Now;
                                    folderName = date.Year.ToString() + date.Month.ToString() +
                                                 date.Day.ToString() + "_" + MD5HashGenerator.GenerateKey(date);
                                } while (Directory.Exists((baseRFQAttachmentsPath + folderName)));
                                Directory.CreateDirectory((baseRFQAttachmentsPath + folderName));
                                rfq.SentAttachmentsFolder = folderName;
                            }
                            string currentPathAttachmentSource = baseBOMAttachmentsPath + bomAttachment.Directory + @"\" + bomAttachment.FileName;
                            string currentPathAttachmentTarget = baseRFQAttachmentsPath + rfq.SentAttachmentsFolder + @"\" + bomAttachment.FileName;
                            try
                            {
                                File.Copy(currentPathAttachmentSource, currentPathAttachmentTarget, true);
                            }
                            catch (Exception e)
                            {
                                response.ErrorThrown         = true;
                                response.ResponseDescription = "ERROR:" + e.Message;
                                return(JsonConvert.SerializeObject(response));
                            }
                        }


                        RfqCRUD rfqCRUD = new RfqCRUD();

                        rfq.SupplierId       = supplier.Id;
                        rfq.SupplierName     = supplier.SupplierName;
                        rfq.SentToVendor     = DateTime.Now;
                        rfq.LastEmail        = supplier.ContactEmail;
                        rfq.Status           = "PENDING";
                        rfq.BomDetailId      = component.Id;
                        rfq.Um               = component.Um;
                        rfq.RfqNumberKey     = rfqNumberGenereated.Id;
                        rfq.RfqGenerated     = rfqNumberGenereated.RfqGenerated;
                        rfq.DueDate          = newRFQScreen.DueDate;
                        rfq.MarketSectorID   = newRFQScreen.MarketSectorID;
                        rfq.DrawingLevel     = newRFQScreen.DrawingLevel;
                        rfq.TargetPrice      = newRFQScreen.TargetPrice;
                        rfq.CommentsToVendor = newRFQScreen.CommentsToVendor;


                        rfq.CreatedBy = strAuthUser;

                        string idGenerated = rfqCRUD.createAndReturnIdGenerated(rfq, component, ref DM);

                        if (!rfqCRUD.ErrorOccur)
                        {
                            rfq.Id = long.Parse(idGenerated);

                            foreach (RFQEAV eau in newEAUList)
                            {
                                RFQEAVCRUD rfqEAV_CRUD      = new RFQEAVCRUD();
                                RFQEAV     rfqEAV_toPersist = new RFQEAV();
                                rfqEAV_toPersist.RfqHeaderKey = long.Parse(idGenerated);
                                rfqEAV_toPersist.Volume       = eau.Volume * component.Qty;
                                rfqEAV_toPersist.Year         = eau.Year;
                                rfqEAV_toPersist.Status       = "CREATED";
                                rfqEAV_CRUD.createAndReturnIdGenerated(rfqEAV_toPersist, ref DM);
                                if (rfqEAV_CRUD.ErrorOccur)
                                {
                                    response.ErrorThrown         = true;
                                    response.ResponseDescription = "ERROR:" + rfqEAV_CRUD.ErrorMessage;
                                    return(JsonConvert.SerializeObject(response));
                                }
                            }

                            TokenCRUD token_CRUD = new TokenCRUD();
                            Token     token      = new Token();
                            token.Subject     = "RFQ";
                            token.SubjectKey  = long.Parse(idGenerated);
                            token.TokenNumber = MD5HashGenerator.GenerateKey(DateTime.Now);
                            if (token_CRUD.create(token, ref DM))
                            {
                                Email       NewMail = new Email();
                                MailMessage Message = new MailMessage();
                                try
                                {
                                    Message.From = new MailAddress("*****@*****.**", "*****@*****.**");
                                    Message.To.Add(new MailAddress(supplier.ContactEmail.ToString()));
                                    Message.Subject      = "Request For Quote";
                                    Message.IsBodyHtml   = true;
                                    Message.BodyEncoding = System.Text.Encoding.UTF8;

                                    var url             = ResolveUrl("~/Vendor/Email_RFQ_Request.htm");
                                    var strEmailContent = HTMLContent(url);
                                    strEmailContent = strEmailContent.Replace("{BuyerName}", user.Name);
                                    strEmailContent = strEmailContent.Replace("{BuyerPhone}", user.Phone1);
                                    strEmailContent = strEmailContent.Replace("{BuyerEmail}", user.Email);
                                    strEmailContent = strEmailContent.Replace("{RFQ Number}", rfqNumber.RfqGenerated);
                                    strEmailContent = strEmailContent.Replace("{Part Number}", component.PartNumber);
                                    strEmailContent = strEmailContent.Replace("{RFQLink}", "http://" + Request.Url.Authority + Request.ApplicationPath + "/Vendor/RFQHandler.ashx?token=" + token.TokenNumber);

                                    Message.Body = strEmailContent;


                                    NewMail.SendMail(Message);
                                }
                                catch (Exception ex)
                                {
                                    DM.RollBack();
                                    response.ErrorThrown         = true;
                                    response.ResponseDescription = "ERROR: Could not send email to: " + supplier.ContactEmail.ToString() + "; " + ex.Message;
                                    return(JsonConvert.SerializeObject(response));
                                }
                            }
                            else
                            {
                                response.ErrorThrown         = true;
                                response.ResponseDescription = "ERROR: " + token_CRUD.ErrorMessage;
                                return(JsonConvert.SerializeObject(response));
                            }
                        }
                        else
                        {
                            response.ErrorThrown         = true;
                            response.ResponseDescription = "ERROR: " + rfqCRUD.ErrorMessage;
                            return(JsonConvert.SerializeObject(response));
                        }
                    }

                    DM.CommitTransaction();
                    DM.Close_Open_Connection();

                    if (DM.ErrorOccur)
                    {
                        response.ErrorThrown         = true;
                        response.ResponseDescription = "ERROR: " + DM.Error_Mjs;
                        return(JsonConvert.SerializeObject(response));
                    }
                    resultRFQs.Add(rfq);
                }
            }
        }

        response.ErrorThrown         = false;
        response.ResponseDescription = "RFQs created successfully.";
        response.Result = resultRFQs;
        return(JsonConvert.SerializeObject(response));
    }
예제 #2
0
    public string selectRFQ(long rfqHeaderKey, long eavKey, float newBomCost)
    {
        saveRFQSummary();

        RfqCRUD        rfqCRUD        = new RfqCRUD();
        RfqSummaryCRUD rfqSummaryCRUD = new RfqSummaryCRUD();

        RFQ rfq = rfqCRUD.readById(rfqHeaderKey);
        //List<RFQSummary> rfqSummaryList = rfqSummaryCRUD.readByBOMDetailIDAndEAU(rfq.BomDetailId, rfqSummary.EstimatedAnnualVolume);
        List <RFQ> rfqList = rfqCRUD.readByBOMDetailKey(rfq.BomDetailId);

        RFQEAVCRUD rfqEAV_CRUD = new RFQEAVCRUD();
        RFQEAV     eavSelected = rfqEAV_CRUD.readById(eavKey);

        bomDetailCRUD bomDetailCRUD = new bomDetailCRUD();
        BOMDetail     bomDetail     = bomDetailCRUD.readById(rfq.BomDetailId);

        ConnectionManager CM = new ConnectionManager();

        Data_Base_MNG.SQL DM = CM.getDataManager();

        List <RFQEAV> eavList = new List <RFQEAV>();

        foreach (RFQ rfqObj in rfqList)
        {
            eavList.AddRange(rfqEAV_CRUD.readByParentID(rfqObj.Id));
        }

        /*Begin Transaction*/
        DM.Open_Connection("RFQ Selection");

        foreach (RFQ rfqObj in rfqList)
        {
            foreach (RFQEAV eavCurrent in eavList)
            {
                if (eavCurrent.Id != eavKey && eavCurrent.RfqHeaderKey == rfqObj.Id)
                {
                    eavCurrent.Status = "DISMISSED";
                    if (rfqObj.NoQuote == true)
                    {
                        eavCurrent.Status = "NO QUOTE";
                    }
                    if (!rfqEAV_CRUD.update(eavCurrent, ref DM))
                    {
                        return("ERROR:" + rfqEAV_CRUD.ErrorMessage);
                    }
                }
            }
            if (rfqObj.Id != rfq.Id)
            {
                rfqObj.Status = "DISMISSED";
                if (rfqObj.NoQuote == true)
                {
                    rfqObj.Status = "NO QUOTE";
                }
                if (!rfqCRUD.update(rfqObj, ref DM))
                {
                    return("ERROR:" + rfqCRUD.ErrorMessage);
                }
            }
        }


        if (rfq != null)
        {
            rfq.Status = "SELECTED";
            if (!rfqCRUD.update(rfq, ref DM))
            {
                return("ERROR:" + rfqCRUD.ErrorMessage);
            }
            if (eavSelected != null)
            {
                eavSelected.Status = "SELECTED";
                if (!rfqEAV_CRUD.update(eavSelected, ref DM))
                {
                    return("ERROR:" + rfqEAV_CRUD.ErrorMessage);
                }

                if (bomDetail != null)
                {
                    if (newBomCost > -1)
                    {
                        bomDetail.Cost = newBomCost;
                    }
                    bomDetail.Status = "Processed";
                    if (!bomDetailCRUD.update(bomDetail, ref DM))
                    {
                        return("ERROR:" + bomDetailCRUD.ErrorMessage);
                    }
                }
                else
                {
                    DM.RollBack();
                    return("ERROR:There was an error retrieving BOM Detail for BomDetailKey = " + rfq.BomDetailId);
                }
            }
            else
            {
                DM.RollBack();
                return("ERROR:There was an error retrieving EAV entity for eavKey = " + eavKey);
            }
        }
        else
        {
            DM.RollBack();
            return("ERROR:Could not retrieve RFQ for ID = " + rfqHeaderKey);
        }

        DM.CommitTransaction();
        DM.Close_Open_Connection();

        if (DM.ErrorOccur)
        {
            return("ERROR:" + DM.Error_Mjs);
        }
        return(getRFQSummarybyBOMDetailID(rfq.BomDetailId));
    }