public bool SaveEmployeesFromCSV(List <CSVEmployee> empList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { foreach (CSVEmployee e in empList) { Employee eSave = new Employee(); eSave.Username = e.Username; eSave.Password = e.Password; eSave.Email = e.Email; eSave.Firstname = e.Firstname; eSave.Lastname = e.Lastname; eSave.PhoneNumber = e.PhoneNumber; eSave.EmployeeType = etService.FindEmployeeTypeByEmployeeTypeName(e.EmployeeType); //eSave.Firstname = db.Employees.Where(x=>x.Id)(e.SupervisedById); eSave.Department = dService.FindDepartmentByDepartmentName(e.Department); //eSave.OriginalEmployeeType = etService.FindEmployeeTypeByEmployeeTypeName(e.OriginalEmployeeType); eSave.TempDeptHeadType = etService.FindEmployeeTypeByEmployeeTypeName(e.TemporaryHead); db.Employees.Add(eSave); } db.SaveChanges(); transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool SaveProductsFromCSV(List <CSVProduct> pList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { foreach (CSVProduct p in pList) { Product pSave = new Product(); pSave.ProductCode = p.ProductCode; pSave.ProductCategory = pcService.findProductCategory(p.ProductCategory); pSave.ProductName = p.ProductName; pSave.Units = p.Units; pSave.ReorderLevel = p.ReorderLevel; pSave.ReorderQuantity = p.ReorderQuantity; pSave.InventoryLocation = p.InventoryLocation; pSave.InventoryQuantity = p.InventoryQuantity; pSave.MLReorderQuantity = p.MLReorderQuantity; pSave.MLReorderLevel = p.MLReorderLevel; db.Products.Add(pSave); } db.SaveChanges(); transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool CreateTransactionsManual2(List <Product> pList, List <int> itQuantity, Employee emp, string comments) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { List <Product> _productList = new List <Product>(); foreach (Product p in pList) { Product _p = db.Products.Find(p.Id); _productList.Add(_p); } SupplierProduct sp = db.SupplierProducts .Where(x => x.PriorityLevel == 1) .First(); List <int> _itQuantity = itQuantity; Employee _emp = empService.GetEmployeeById(emp.Id); for (int i = 0; i < _productList.Count; i++) { int InvTransCheckList = FindInvTransByTodayCount(); int count = InvTransCheckList + 1; //Define invtrans Code string invtransCode = "IT" + "/" + DateTime.Now.ToString("ddMMyy") + "/" + count.ToString(); InventoryTransaction itSave = new InventoryTransaction() { EmployeeId = _emp.Id, ProductId = _productList[i].Id, Employee = _emp, InventoryChangeQuantity = _itQuantity[i], InventoryTransComments = comments, InventoryTransDate = DateTime.Now, ITStatus = Enums.ITStatus.PendingApproval, ITCode = invtransCode, Product = _productList[i], ProductPrice = sp.ProductPrice }; db.InventoryTransactions.Add(itSave); db.SaveChanges(); } ; transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public void CancelDLBasedOnId(int id) { DelegationForm _df = db.DelegationForms.Find(id); _df.DLStatus = DLStatus.Cancelled; db.DelegationForms.Update(_df); db.SaveChanges(); }
public bool UpdateCollectionPoint(Employee emp, CollectionPoint cp) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { CollectionPoint _cp = db.CollectionPoints.Find(cp.Id); Employee _emp = db.Employees.Find(emp.Id); if (_emp.EmployeeType.EmployeeTypeName != "Department Representative") { throw new Exception("This is not a dept rep"); } Department _dept = db.Departments.Find(emp.Department.Id); _dept.CollectionPoint = _cp; _dept.DeptRepId = _emp.Id; _dept.deptRep = _emp; db.Update(_dept); db.SaveChanges(); transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool SaveSupplierProductsFromCSV(List <CSVSupplierProduct> csvList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { foreach (CSVSupplierProduct sp in csvList) { SupplierProduct spSave = new SupplierProduct(); spSave.ProductPrice = sp.ProductPrice; spSave.PriorityLevel = sp.PriorityLevel; spSave.SPAvailStatus = (SPAvailStatus)Enum.Parse(typeof(SPAvailStatus), sp.SPAvailStatus); spSave.Product = db.Products.Where(x => x.ProductCode == sp.ProductCode).First(); spSave.Supplier = db.Suppliers.Where(x => x.SupplierName == sp.SupplierName).First(); db.SupplierProducts.Add(spSave); } db.SaveChanges(); transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool SaveSuppliersFromCSV(List <CSVSupplier> csvList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { foreach (CSVSupplier s in csvList) { Supplier sSave = new Supplier(); sSave.SupplierCode = s.SupplierCode; sSave.SupplierName = s.SupplierName; sSave.Address = s.Address; sSave.ContactName = s.ContactName; sSave.FaxNumber = s.FaxNumber; sSave.PhoneNumber = s.PhoneNumber; sSave.GSTregistrationNumber = s.GSTregistrationNumber; sSave.SupplierEmail = s.SupplierEmail; db.Suppliers.Add(sSave); } db.SaveChanges(); transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool SaveProductCategoryFromCSV(List <CSVProductCategory> pcList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { foreach (CSVProductCategory pc in pcList) { ProductCategory pcSave = new ProductCategory(); pcSave.ProductCategoryName = pc.ProductCategoryName; db.ProductCategories.Add(pcSave); } db.SaveChanges(); transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool SaveEmployeeTypesFromCSV(List <CSVEmployeeType> etList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { foreach (CSVEmployeeType et in etList) { EmployeeType etSave = new EmployeeType(); etSave.EmployeeTypeName = et.EmployeeType; db.EmployeeTypes.Add(etSave); } db.SaveChanges(); transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool CreatePurchaseOrder(Employee emp, string comment, List <Product> productList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { DateTime createDate = DateTime.Now; string commentSave = comment; // Assign the employee Employee empCheck = db.Employees .Where(x => x.Id == emp.Id) .FirstOrDefault(); //Create empty list of supplier products List <SupplierProduct> _spList = new List <SupplierProduct>(); for (int i = 0; i < productList.Count; i++) { SupplierProduct sp = db.SupplierProducts .OrderBy(x => x.PriorityLevel) .Where(x => x.Product == productList[i]) .Where(x => x.SPAvailStatus == Enums.SPAvailStatus.Available) .First(); if (sp == null) { sp = db.SupplierProducts .OrderBy(x => x.PriorityLevel) .Where(x => x.Product == productList[i]) .First(); } _spList.Add(sp); Debug.WriteLine(sp.Supplier.SupplierName); } //Find the total of PO before adding PO int count = FindPOsByDateToday(); IEnumerable <Supplier> suppliers = _spList.Select(x => x.Supplier).Distinct(); foreach (Supplier sup in suppliers) { count++; Debug.WriteLine(sup.SupplierName); //Assign a PO Code string poCode = "PO" + "/" + createDate.ToString("ddMMyy") + "/" + count.ToString(); //Create a PO PurchaseOrder _po = new PurchaseOrder() { supplier = sup, DeliverTo = "Logic University", expectedDeliveryDate = DateTime.Now.AddDays(10), IssuedBy = empCheck, POCode = poCode, POComments = comment, POIssueDate = DateTime.Now, POStatus = Enums.POStatus.Issued }; db.Add(_po); db.SaveChanges(); //Find the Products of this particular Supplier based on the filtered list previously generated List <SupplierProduct> spListIndividualSupplier = _spList .Where(x => x.Supplier == sup) .ToList(); foreach (SupplierProduct _sp in spListIndividualSupplier) { PurchaseOrderSupplierProduct _posr = new PurchaseOrderSupplierProduct() { POUnitPrice = _sp.ProductPrice, PurchaseOrder = _po, SupplierProduct = _sp, //POQuantityRequested = productList.Where(x => x.Id == _sp.Product.Id).First().ProductRequested, POQuantityRequested = db.Products.Find(productList.Where(x => x.Id == _sp.Product.Id).First().Id).ReorderQuantity, }; db.PurchaseOrderSupplierProducts.Add(_posr); db.SaveChanges(); } } transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool UpdateRequistionProduct(Employee emp, string comment, List <Product> pList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { if (pList == null) { throw new Exception("No products to be stored as a Requisition Form"); } bool isAllProductsQuantityLessThanZero = true; foreach (Product p in pList) { if (p.ProductRequested >= 0) { isAllProductsQuantityLessThanZero = false; break; } } if (isAllProductsQuantityLessThanZero == true) { throw new Exception("Quantity of Product should be at least 0"); } //find employee Employee _emp = db.Employees.Find(emp.Id); //Find the list of rfs on that day by the same employee List <RequisitionForm> rfCheckList = FindRequisitionFormByDeptToday(_emp); //Find the total no of rfs sent on the same day by the same department int count = rfCheckList.Count + 1; //rfCreateDate DateTime createDate = DateTime.Now; //Define RF Code string rfCode = "RF" + "/" + _emp.Department.DeptCode + "/" + createDate.ToString("ddMMyy") + "/" + count.ToString(); //Create New RF RequisitionForm _rf = new RequisitionForm() { RFCode = rfCode, Employee = _emp, RFStatus = RFStatus.Submitted, RFDate = createDate, RFComments = comment }; db.RequisitionForms.Add(_rf); db.SaveChanges(); foreach (Product p in pList) { Product _p = db.Products.Find(p.Id); RequisitionFormsProduct _rfp = new RequisitionFormsProduct() { RequisitionForm = _rf, Product = _p, ProductRequested = p.ProductRequested }; db.RequisitionFormsProducts.Add(_rfp); db.SaveChanges(); } transcat.Commit(); var message = new MimeMessage(); message.From.Add(new MailboxAddress("IMS System", "*****@*****.**")); message.To.Add(new MailboxAddress(_emp.SupervisedBy.Firstname + " " + _emp.SupervisedBy.Lastname, _emp.SupervisedBy.Email)); message.Subject = "Requisition Form for Approval - " + _rf.RFCode; message.Body = new TextPart("plain") { Text = "You have an outstanding Requisition Form - " + _rf.RFCode + "\nthis is pending for approval" }; EmailUtil eUtil = new EmailUtil(); eUtil.SendEmail(message); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool CreateDisbursementForm(List <StationeryRetrievalRequisitionForm> srrfList, Employee emp, string comment) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { DateTime createDate = DateTime.Now; //find employee Employee _emp = db.Employees.Find(emp.Id); //Create empty df List to save List <DisbursementForm> _dfList = new List <DisbursementForm>(); //Create empty list of srrf List <StationeryRetrievalRequisitionForm> _srrfList = new List <StationeryRetrievalRequisitionForm>(); //Find all SRRF and provide a full mapping properly foreach (StationeryRetrievalRequisitionForm srrf in srrfList) { StationeryRetrievalRequisitionForm _srrf = db.StationeryRetrievalRequisitionForms.Find(srrf.Id); _srrfList.Add(_srrf); } //Find all the distinct Departments IEnumerable <Department> deptList = _srrfList.Select(x => x.RequisitionForm.Employee.Department).Distinct(); foreach (Department dept in deptList) { //Dict to check for Total Product Approved Based on a particular product Dictionary <int, int> _prodCountCheckDict1 = new Dictionary <int, int>(); //Dict to check for Total Product Collected Based on a particular product Dictionary <int, int> _prodCountCheckDict2 = new Dictionary <int, int>(); //Find the total of DOs before adding NEW DO int count = FindDFsByDateTodayAndDept(dept); count++; //Create empty dfp List to save List <DisbursementFormProduct> _dfpList = new List <DisbursementFormProduct>(); //Create an empty dfrflist to save List <DisbursementFormRequisitionForm> _dfrfList = new List <DisbursementFormRequisitionForm>(); //Create an empty dfrflist to save List <DisbursementFormRequisitionFormProduct> _dfrfpList = new List <DisbursementFormRequisitionFormProduct>(); //Find Filtered list of srrf based on dept List <StationeryRetrievalRequisitionForm> _srrfListFilteredByDept = _srrfList .Where(x => x.RequisitionForm.Employee.Department == dept) .ToList(); DateTime deliveryDate = createDate.Date.AddDays(1); if (dept.CollectionPoint.CollectionName == "Stationery Store - Administration Building") { deliveryDate = deliveryDate.AddHours(9).AddMinutes(30); } else if (dept.CollectionPoint.CollectionName == "Management School") { deliveryDate = deliveryDate.AddHours(11).AddMinutes(0); } else if (dept.CollectionPoint.CollectionName == "Medical School") { deliveryDate = deliveryDate.AddHours(9).AddMinutes(30); } else if (dept.CollectionPoint.CollectionName == "Engineering School") { deliveryDate = deliveryDate.AddHours(11).AddMinutes(0); } else if (dept.CollectionPoint.CollectionName == "Science School") { deliveryDate = deliveryDate.AddHours(9).AddMinutes(30); } else if (dept.CollectionPoint.CollectionName == "University Hospital") { deliveryDate = deliveryDate.AddHours(11).AddMinutes(0); } //Assign a DF Code string dfCode = "DF" + "/" + dept.DeptCode + "/" + createDate.ToString("ddMMyy") + "/" + count.ToString(); //Create a DF DisbursementForm _df = new DisbursementForm() { DFDate = createDate, DFStatus = DFStatus.Created, DFCode = dfCode, DFComments = comment, DFDeliveryDate = deliveryDate, DeptRep = db.Employees .Where(x => x.Department == dept) .Where(x => x.EmployeeType.EmployeeTypeName == "Department Representative") .First(), //DeptRep = db.Employees.Find(dept.DeptRepId), CollectionPoint = dept.CollectionPoint, StoreClerk = _emp }; db.DisbursementForms.Add(_df); _dfList.Add(_df); db.SaveChanges(); //Find All distinct RFs in filtered srrfList IEnumerable <RequisitionForm> rfListFilteredByDept = _srrfListFilteredByDept.Select(x => x.RequisitionForm).Distinct(); //Create a consolidated RFPList based on all rf in a dept List <RequisitionFormsProduct> rfpConsolidatedListFilteredByDept = new List <RequisitionFormsProduct>(); foreach (RequisitionForm rfFilteredByDept in rfListFilteredByDept) { DisbursementFormRequisitionForm _dfrf = new DisbursementFormRequisitionForm() { DisbursementForm = _df, RequisitionForm = rfFilteredByDept, DFRFStatus = DFRFStatus.PendingDelivery }; _dfrfList.Add(_dfrf); db.DisbursementFormRequisitionForms.Add(_dfrf); db.SaveChanges(); //Find RFPList based on RF List <RequisitionFormsProduct> rfpListFilteredByDept = db.RequisitionFormsProducts .Where(x => x.RequisitionForm == rfFilteredByDept) .ToList(); //Find All RFP in a dept and add it to the consolidated list foreach (RequisitionFormsProduct rfpFilteredByDept in rfpListFilteredByDept) { rfpConsolidatedListFilteredByDept.Add(rfpFilteredByDept); } } //Find All independent Products in consolidated RFP in a dept IEnumerable <Product> pListFilteredByDept = rfpConsolidatedListFilteredByDept .Select(x => x.Product).Distinct(); //Create DFRFP based on RFP foreach (RequisitionFormsProduct rfpConsolidatedFilteredByDept in rfpConsolidatedListFilteredByDept) { DisbursementFormRequisitionFormProduct _dfrfp = new DisbursementFormRequisitionFormProduct() { DisbursementForm = _df, RequisitionFormsProduct = rfpConsolidatedFilteredByDept, ProductCollected = 0 }; _dfrfpList.Add(_dfrfp); db.DisbursementFormRequisitionFormProducts.Add(_dfrfp); db.SaveChanges(); } //Assign product as key to dictionary and set default values to 0 foreach (Product p in pListFilteredByDept) { _prodCountCheckDict1[p.Id] = 0; _prodCountCheckDict2[p.Id] = 0; } //Find All SRRFP pertaining to this DF List <StationeryRetrievalRequisitionFormProduct> _srrfpList = new List <StationeryRetrievalRequisitionFormProduct>(); List <StationeryRetrieval> _srList = new List <StationeryRetrieval>(); IEnumerable <RequisitionForm> _rfListFilteredByDept = _srrfListFilteredByDept.Select(x => x.RequisitionForm).Distinct(); IEnumerable <StationeryRetrieval> _srListFilteredByDept = _srrfListFilteredByDept.Select(x => x.StationeryRetrieval).Distinct(); foreach (StationeryRetrieval _srFilteredByDept in _srListFilteredByDept) { List <StationeryRetrievalRequisitionFormProduct> _srrfpListIndividual = db.StationeryRetrievalRequisitionFormProducts .Where(x => x.SR == _srFilteredByDept) .ToList(); foreach (RequisitionForm _rfFilteredByDept in _rfListFilteredByDept) { _srrfpListIndividual = _srrfpListIndividual .Where(x => x.RFP.RequisitionForm == _rfFilteredByDept) .ToList(); } foreach (StationeryRetrievalRequisitionFormProduct _srrfpIndividual in _srrfpListIndividual) { _srrfpList.Add(_srrfpIndividual); } } //Create DFP from Ienumerable products foreach (Product p in pListFilteredByDept) { int prodToDelivery = 0; foreach (RequisitionFormsProduct rfpConsolidatedFilteredByDept in rfpConsolidatedListFilteredByDept) { if (p.Id == rfpConsolidatedFilteredByDept.Product.Id) { _prodCountCheckDict1[p.Id] = _prodCountCheckDict1[p.Id] + rfpConsolidatedFilteredByDept.ProductApproved; _prodCountCheckDict2[p.Id] = _prodCountCheckDict2[p.Id] + rfpConsolidatedFilteredByDept.ProductCollectedTotal; } } foreach (StationeryRetrievalRequisitionFormProduct _srrfp in _srrfpList) { if (p.Id == _srrfp.RFP.Product.Id) { prodToDelivery = prodToDelivery + _srrfp.ProductAssigned; } } //CheckProductToReceive DisbursementFormProduct _dfp = new DisbursementFormProduct() { DisbursementForm = _df, Product = p, ProductToDeliverTotal = prodToDelivery }; _dfpList.Add(_dfp); db.DisbursementFormProducts.Add(_dfp); db.SaveChanges(); } ////Create DFP from Ienumerable products //foreach (Product p in pListFilteredByDept) //{ // foreach (RequisitionFormsProduct rfpConsolidatedFilteredByDept in rfpConsolidatedListFilteredByDept) // { // if (p.Id == rfpConsolidatedFilteredByDept.Product.Id) // { // _prodCountCheckDict1[p.Id] = _prodCountCheckDict1[p.Id] + rfpConsolidatedFilteredByDept.ProductApproved; // _prodCountCheckDict2[p.Id] = _prodCountCheckDict2[p.Id] + rfpConsolidatedFilteredByDept.ProductCollectedTotal; // } // } // DisbursementFormProduct _dfp = new DisbursementFormProduct() // { // DisbursementForm = _df, // Product = p, // ProductToDeliverTotal = _prodCountCheckDict1[p.Id] - _prodCountCheckDict2[p.Id] // }; // _dfpList.Add(_dfp); // db.DisbursementFormProducts.Add(_dfp); // db.SaveChanges(); //} } foreach (StationeryRetrievalRequisitionForm _srrf in _srrfList) { _srrf.SRRFStatus = SRRFStatus.Completed; db.StationeryRetrievalRequisitionForms.Update(_srrf); db.SaveChanges(); } transcat.Commit(); foreach (DisbursementForm _df in _dfList) { var message = new MimeMessage(); message.From.Add(new MailboxAddress("IMS System", "*****@*****.**")); message.To.Add(new MailboxAddress(_df.DeptRep.Firstname + " " + _df.DeptRep.Lastname, _df.DeptRep.Email)); message.Subject = "[ " + _df.DFCode + " ] Status: Created"; message.Body = new TextPart("plain") { Text = "[ " + _df.DFCode + " ]: Created" + "\nDisbursement From Comments: " + _df.DFComments }; EmailUtil eUtil = new EmailUtil(); eUtil.SendEmail(message); } return(true); } catch { transcat.Rollback(); return(false); } } }
public bool SaveStationaryRetrievalProducts(List <SRProductViewModel> productlist, Employee emp, string comment, List <int> selectedrequisiton, List <StationeryRetrievalProduct> srpList) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { Employee _emp = db.Employees.Find(emp.Id); List <RequisitionForm> rfList = FindRFListByRF(selectedrequisiton); //Create empty srrfList List <StationeryRetrievalRequisitionForm> _srrfList = new List <StationeryRetrievalRequisitionForm>(); //Create empty rfpList List <RequisitionFormsProduct> _rfpList = new List <RequisitionFormsProduct>(); //Create empty srrfpList List <StationeryRetrievalRequisitionFormProduct> _srrfpList = new List <StationeryRetrievalRequisitionFormProduct>(); //Create empty srpList List <StationeryRetrievalProduct> _srpList = new List <StationeryRetrievalProduct>(); //Find the list of rfs on that day by the same employee int count = FindStationaryRetrievalFormByDeptTodayCount(_emp) + 1; //Define RF Code string srfCode = "SR" + "/" + DateTime.Now.ToString("ddMMyy") + "/" + count.ToString(); //Create New SRF and SRFP //DateTime ddToday = DateTime.Now; StationeryRetrieval srfForm = new StationeryRetrieval() { StoreClerk = _emp, SRCode = srfCode, SRStatus = SRStatus.Open, SRComments = comment, SRDate = DateTime.Now }; db.StationeryRetrievals.Add(srfForm); db.SaveChanges(); foreach (RequisitionForm rf in rfList) { StationeryRetrievalRequisitionForm _srrf = new StationeryRetrievalRequisitionForm() { StationeryRetrieval = srfForm, RequisitionForm = rf, SRRFStatus = SRRFStatus.Open }; db.Add(_srrf); _srrfList.Add(_srrf); db.SaveChanges(); List <RequisitionFormsProduct> individualrfpList = db.RequisitionFormsProducts .Where(x => x.RequisitionForm == rf) .ToList(); foreach (RequisitionFormsProduct individualrfp in individualrfpList) { _rfpList.Add(individualrfp); } } foreach (RequisitionFormsProduct _rfp in _rfpList) { StationeryRetrievalRequisitionFormProduct _srrfp = new StationeryRetrievalRequisitionFormProduct() { SR = srfForm, RFP = _rfp, ProductAssigned = 0 }; _srrfpList.Add(_srrfp); db.Add(_srrfp); db.SaveChanges(); } if (productlist == null) { throw new Exception("No products to be stored as a Stationary Retrieval Form"); } else { foreach (SRProductViewModel p in productlist) { Product product = db.Products.Where(x => x.ProductName == p.productname).FirstOrDefault(); StationeryRetrievalProduct _srfProduct = new StationeryRetrievalProduct(); _srfProduct.StationeryRetrieval = srfForm; _srfProduct.Product = product; _srfProduct.ProductRequestedTotal = p.productqty; db.StationeryRetrievalProduct.Add(_srfProduct); db.SaveChanges(); } foreach (var requ_id in selectedrequisiton) { RequisitionForm requform = db.RequisitionForms.Where(x => x.Id == requ_id).FirstOrDefault(); requform.RFStatus = RFStatus.Ongoing; db.SaveChanges(); } } transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }
public bool CreateDeliveryOrder(int poid, string doComments, int empid) { using (IDbContextTransaction transcat = db.Database.BeginTransaction()) { try { Employee _emp = db.Employees.Find(empid); //check no of DOs today int count = FindDOsByDateToday() + 1; //Assign a PO Code string doCode = "DO" + "/" + DateTime.Now.ToString("ddMMyy") + "/" + count.ToString(); PurchaseOrder _poCheck = db.PurchaseOrders.Find(poid); if (_poCheck.POStatus == Enums.POStatus.Completed) { throw new Exception("This is not the right PO"); } //Create Delivery Order DeliveryOrder _do = new DeliveryOrder() { PurchaseOrder = _poCheck, DOCode = doCode, DOComments = doComments, DOReceivedDate = DateTime.Now, ReceivedBy = _emp, Supplier = _poCheck.supplier, DOStatus = Enums.DOStatus.Created }; db.DeliveryOrders.Add(_do); db.SaveChanges(); //Find POSR based on corresponding PO List <PurchaseOrderSupplierProduct> posrList = db.PurchaseOrderSupplierProducts .Where(x => x.PurchaseOrder == _poCheck) .ToList(); //Create Corresponding DOSR foreach (PurchaseOrderSupplierProduct posr in posrList) { DeliveryOrderSupplierProduct _dosr = new DeliveryOrderSupplierProduct() { PurchaseOrderSupplierProduct = posr, DeliveryOrder = _do, //DOQuantityReceived = posr.POQuantityRequested, DOQuantityReceived = 0, }; db.DeliveryOrderSupplierProducts.Add(_dosr); } db.SaveChanges(); transcat.Commit(); return(true); } catch { transcat.Rollback(); return(false); } } }