public bool GenerateQuoteFromPr(List <PurchaseRequestDetail> prdlist, int clerkid) { List <PurchaseRequestDetail> prdlistwithnull = new List <PurchaseRequestDetail>(); foreach (PurchaseRequestDetail prd in prdlist) { if (prd.VenderQuote == null || prd.VenderQuote == "") { prdlistwithnull.Add(prd); } } if (prdlistwithnull.Count() < 1) { return(true); } else { List <PurchaseRequestDetail> pnull = prdlistwithnull.OrderBy(m => m.SupplierId).ToList(); Dictionary <string, List <PurchaseRequestDetail> > pdict = new Dictionary <string, List <PurchaseRequestDetail> >(); foreach (PurchaseRequestDetail prd in pnull) { prd.Product = prepo.FindProductById(prd.ProductId); if (!pdict.ContainsKey(prd.SupplierId)) { List <PurchaseRequestDetail> prdlist1 = new List <PurchaseRequestDetail>(); prdlist1.Add(prd); pdict.Add(prd.SupplierId, prdlist1); } else { pdict[prd.SupplierId].Add(prd); } } foreach (var r in pdict) { Supplier supplier = srepo.FindSupplierById(r.Value[0].SupplierId); Employee clerk = erepo.FindEmpById(clerkid); EmailModel email = new EmailModel(); List <PurchaseRequestDetail> List_of_PR_tosend = pdict[r.Key]; Task.Run(async() => { EmailTemplates.RequestQuoteTemplate rfq = new EmailTemplates.RequestQuoteTemplate(clerk, supplier, List_of_PR_tosend); email.emailTo = supplier.Email; email.emailSubject = rfq.subject; email.emailBody = rfq.body; await mailservice.SendRFQEmailAsync(email, clerk); }); } } return(true); }
public bool UpdatePr(List <PurchaseRequestDetail> prdlist, int supid, long approveddate) { foreach (PurchaseRequestDetail prd in prdlist) { purreqrepo.UpdateApprovedPrItems(prd, supid, approveddate); } if (prdlist[0].Status == Status.PurchaseRequestStatus.approved) { List <PurchaseRequestDetail> prdlist2 = purreqrepo.FindPurchaseReq(prdlist[0].PurchaseRequestId); //getting back all the PurchaseRequestDetail with extra information for Emailing List <PurchaseRequestDetail> sortedprlist = prdlist2.GroupBy(m => m.SupplierId).SelectMany(m => m).ToList(); Dictionary <string, List <PurchaseRequestDetail> > pdict = new Dictionary <string, List <PurchaseRequestDetail> >(); //create dictionary of supplier Id as key, and List<PRD> as value for further creation of PO foreach (PurchaseRequestDetail prd in sortedprlist) { if (!pdict.ContainsKey(prd.SupplierId)) { List <PurchaseRequestDetail> prdlist1 = new List <PurchaseRequestDetail>(); prdlist1.Add(prd); pdict.Add(prd.SupplierId, prdlist1); } else { //List<PurchaseRequestDetail> prdlist2 = pdict[prd.SupplierId]; pdict[prd.SupplierId].Add(prd); //pdict[prd.SupplierId] = prdlist2; } } foreach (var r in pdict) //for each supplierId in the dictionary, create PO { PurchaseOrder po = new PurchaseOrder(); po.OrderedByClerkId = r.Value[0].CreatedByClerkId; po.SupplierId = r.Value[0].SupplierId; po.OrderedDate = approveddate; po.Status = Status.PurchaseOrderStatus.pending; po.CollectionPointId = crepo.GetStoreCollectionPoint().Id; po.ApprovedBySupId = supid; po.SupplyByDate = approveddate + 604800000; //Add 7 days to ordered date double totalprice = 0; foreach (PurchaseRequestDetail pr in pdict[r.Key]) { totalprice += pr.TotalPrice; } po.TotalPrice = totalprice; PurchaseOrder newpo = porepo.Create(po); List <PurchaseRequestDetail> List_of_PRD_toaddinPO = pdict[r.Key]; foreach (PurchaseRequestDetail z in List_of_PRD_toaddinPO) { PurchaseOrderDetail pod = new PurchaseOrderDetail(); pod.PurchaseOrderId = newpo.Id; pod.PurchaseRequestDetailId = z.Id; pod.ProductId = z.ProductId; pod.QtyPurchased = z.ReorderQty; pod.TotalPrice = z.TotalPrice; pod.Status = Status.PurchaseOrderDetailStatus.pending; podrepo.CreatePurchaseOrderDetail(pod); } PurchaseOrder savedpo = porepo.FindPOByPOid(newpo.Id); //Latest PO after persisting List <PurchaseOrderDetail> savedpod = podrepo.FindPoDetails(savedpo.Id); //Latest POD after persisting Employee clerk = erepo.FindEmpById(r.Value[0].CreatedByClerkId); Supplier supplier = srepo.FindSupplierById(r.Value[0].SupplierId); EmailModel email = new EmailModel(); Task.Run(async() => { EmailTemplates.ApprovedPRtemplate apt = new EmailTemplates.ApprovedPRtemplate(clerk, supplier, savedpod, savedpo); email.emailTo = supplier.Email; email.emailSubject = apt.subject; email.emailBody = apt.body; await mailservice.SendEmailwithccAsync(email, clerk); }); } } else //when rejected { // to pull out purchase request id and date List <PurchaseRequestDetail> prdlist2 = purreqrepo.FindPurchaseReq(prdlist[0].PurchaseRequestId); //getting back all the PurchaseRequestDetail with extra information for Emailing long submitdate = (long)prdlist2[0].SubmitDate; int prid = prdlist2[0].Id; string remarks = prdlist2[0].Remarks; Employee clerk = erepo.FindEmpById(prdlist2[0].CreatedByClerkId); Employee sup = erepo.FindEmpById(supid); EmailModel email = new EmailModel(); Task.Run(async() => { EmailTemplates.RejectedPRtemplate apt = new EmailTemplates.RejectedPRtemplate(clerk, sup, prid, submitdate, remarks); email.emailTo = clerk.Email; email.emailSubject = apt.subject; email.emailBody = apt.body; await mailservice.SendEmailAsync(email); }); } return(true); }