public bool UpdatePurchaseOrderDetailItem(List <PurchaseOrderDetail> podlist) { try { foreach (PurchaseOrderDetail i in podlist) //updates all purchase order detail status { podrepo.UpdatePurchaseOrderDetail(i); } PurchaseOrder po = porepo.FindPOByPOid((int)podlist[0].PurchaseOrderId); //retrieving back the parent PO of the purchaseorderdetail int receivedcounter = 0; foreach (PurchaseOrderDetail j in po.PurchaseOrderDetails) //checking status of every purchaseorderdetail in the PO. if status is received, counter++ { if (j.Status == Status.PurchaseOrderDetailStatus.received) { receivedcounter++; } } if (receivedcounter == po.PurchaseOrderDetails.Count()) //if counter of received matches total purchaseorderdetail, means all items received. update PO status to completed { po.Status = Status.PurchaseOrderStatus.completed; porepo.UpdatePoStatus(po); } return(true); } catch (Exception exception) { throw exception; } }
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); }