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);
                }
            }
        }
Ejemplo n.º 2
0
        public bool SetProductsAssignedInSR(int SRId,
                                            List <StationeryRetrievalRequisitionFormProduct> srrfpList,
                                            List <StationeryRetrievalProduct> srpList,
                                            List <StationeryRetrievalRequisitionForm> srrfList)
        {
            using (IDbContextTransaction transcat = db.Database.BeginTransaction())
            {
                try
                {
                    DateTime assignDate = DateTime.Now;
                    Dictionary <int, int> _prodCountCheckDict = new Dictionary <int, int>();

                    //Find SR
                    StationeryRetrieval _sr = db.StationeryRetrievals.Find(SRId);

                    //Find SRPList through db
                    List <StationeryRetrievalProduct> _srpList = new List <StationeryRetrievalProduct>();

                    //Find SRRFList through db
                    List <StationeryRetrievalRequisitionForm> _srrfList = new List <StationeryRetrievalRequisitionForm>();

                    //Find pList through db
                    List <Product> _pList = new List <Product>();

                    //Find pList through db
                    List <RequisitionForm> _rfList = new List <RequisitionForm>();

                    //Find rfpList through db
                    List <RequisitionFormsProduct> _rfpList = new List <RequisitionFormsProduct>();

                    //Find SRRFPList THROUGH db
                    List <StationeryRetrievalRequisitionFormProduct> _srrfpList = new List <StationeryRetrievalRequisitionFormProduct>();

                    foreach (StationeryRetrievalProduct srp in srpList)
                    {
                        StationeryRetrievalProduct _srp = db.StationeryRetrievalProduct.Find(srp.Id);
                        _srp.ProductReceivedTotal = _srp.ProductReceivedTotal;
                        _srpList.Add(_srp);
                        _pList.Add(_srp.Product);
                    }

                    foreach (StationeryRetrievalRequisitionForm srrf in srrfList)
                    {
                        StationeryRetrievalRequisitionForm _srrf = db.StationeryRetrievalRequisitionForms.Find(srrf.Id);
                        _srrfList.Add(_srrf);
                        _rfList.Add(_srrf.RequisitionForm);
                    }

                    foreach (StationeryRetrievalRequisitionFormProduct srrfp in srrfpList)
                    {
                        if (srrfp.ProductAssigned < 0)
                        {
                            throw new Exception("You cannot assign less than 0");
                        }
                        StationeryRetrievalRequisitionFormProduct _srrfp = db.StationeryRetrievalRequisitionFormProducts.Find(srrfp.Id);
                        if (_srrfp.RFP.ProductBalanceForSR < _srrfp.ProductAssigned)
                        {
                            throw new Exception("You cannot have a value less than the productApproved");
                        }
                        ;
                        _srrfpList.Add(_srrfp);
                    }

                    foreach (RequisitionForm _rf in _rfList)
                    {
                        List <RequisitionFormsProduct> _rfpIndividualList = db.RequisitionFormsProducts
                                                                            .Where(x => x.RequisitionForm == _rf)
                                                                            .ToList();
                        foreach (RequisitionFormsProduct _rfpIndividual in _rfpIndividualList)
                        {
                            _rfpList.Add(_rfpIndividual);
                        }
                    }



                    //Assign Dictionary
                    for (int i = 0; i < _srpList.Count; i++)
                    {
                        int p = _srpList[i].Product.Id;
                        _prodCountCheckDict[p] = 0;
                    }

                    //Update Dict Value for Checking
                    for (int i = 0; i < _srrfpList.Count; i++)
                    {
                        int p2 = _srrfpList[i].RFP.Product.Id;
                        //Check total quantity collected is the same as quantity assigned
                        _prodCountCheckDict[p2] = _prodCountCheckDict[p2] + srrfpList[i].ProductAssigned;
                        Debug.WriteLine(_srrfpList[i].RFP.Product.Id + ": " + _prodCountCheckDict[p2]);
                    }

                    //This is to check if the value received is the sme as the value that is assigned.
                    foreach (KeyValuePair <int, int> entry in _prodCountCheckDict)
                    {
                        for (int i = 0; i < _srpList.Count; i++)
                        {
                            if (entry.Key == _srpList[i].Product.Id)
                            {
                                if (entry.Value != _srpList[i].ProductReceivedTotal)
                                {
                                    throw new Exception("The quantity doesnt match");
                                }
                            }
                        }
                    }

                    //update SRP Quantity
                    foreach (StationeryRetrievalProduct _srp in _srpList)
                    {
                        db.StationeryRetrievalProduct.Update(_srp);
                        db.SaveChanges();
                    }

                    //Update SRRFP quantity
                    for (int i = 0; i < srrfpList.Count; i++)
                    {
                        StationeryRetrievalRequisitionFormProduct _srrfp = FindSRRFPById(srrfpList[i].Id);
                        _srrfp.ProductAssigned = srrfpList[i].ProductAssigned;
                        db.StationeryRetrievalRequisitionFormProducts.Update(_srrfp);
                        db.SaveChanges();
                    }

                    //Update SRRF Status
                    foreach (StationeryRetrievalRequisitionForm _srrf in _srrfList)
                    {
                        _srrf.SRRFStatus = SRRFStatus.Assigned;
                        db.Update(_srrf);
                        db.SaveChanges();
                    }

                    //foreach (RequisitionForm _rf in _rfList)
                    //{
                    //    _rf.RFStatus = RFStatus.NotCompleted;
                    //    db.RequisitionForms.Update(_rf);
                    //    db.SaveChanges();
                    //}

                    //Validation
                    foreach (RequisitionFormsProduct _rfp in _rfpList)
                    {
                        List <StationeryRetrievalRequisitionFormProduct> _srrfpCheckList = _srrfpList.Where(x => x.RFP == _rfp).ToList();
                        int sumofProd = _srrfpCheckList.Select(x => x.ProductAssigned).Sum();
                        if (_rfp.ProductApproved < sumofProd)
                        {
                            throw new Exception("Sum of Assigned Products exceeded ProductsApproved");
                        }
                        foreach (StationeryRetrievalRequisitionFormProduct _srrfp in _srrfpCheckList)
                        {
                            if (_rfp.Product.Id == _srrfp.RFP.Product.Id)
                            {
                                if (_rfp.ProductApproved < _rfp.ProductCollectedTotal + _srrfp.ProductAssigned)
                                {
                                    throw new Exception("Sum of Collected Products with Product Assigned is more than the Sum of the Assigned Products");
                                }
                            }
                        }
                    }

                    //Update SR Status
                    _sr.SRStatus       = SRStatus.Assigned;
                    _sr.SRAssignedDate = assignDate;
                    db.StationeryRetrievals.Update(_sr);
                    db.SaveChanges();

                    transcat.Commit();
                    return(true);
                }
                catch
                {
                    transcat.Rollback();
                    return(false);
                }
            }
        }
        public bool UpdateDeliveryOrder(List <DeliveryOrderSupplierProduct> dospList, int empid, DeliveryOrder DO)
        {
            using (IDbContextTransaction transcat = db.Database.BeginTransaction())
            {
                try
                {
                    Dictionary <int, int> _prodCountCheckDict2 = new Dictionary <int, int>();

                    Employee _emp = db.Employees.Find(empid);

                    //FindDO
                    DeliveryOrder _do = db.DeliveryOrders.Find(DO.Id);

                    //FindPO
                    PurchaseOrder _po = db.PurchaseOrders.Find(_do.PurchaseOrder.Id);

                    //FindList of dosp
                    List <DeliveryOrderSupplierProduct> _dospList = db.DeliveryOrderSupplierProducts
                                                                    .Where(x => x.DeliveryOrder.Id == _do.Id)
                                                                    .ToList();



                    //Check InventoryTransaction Count
                    int count = invtService.FindInvTransByTodayCount();
                    //Define invtrans Code

                    foreach (DeliveryOrderSupplierProduct _dosp in _dospList)
                    {
                        foreach (DeliveryOrderSupplierProduct dosp in dospList)
                        {
                            if (dosp.Id == _dosp.Id)
                            {
                                _dosp.DOQuantityReceived = dosp.DOQuantityReceived;
                                db.Update(_dosp);
                                db.SaveChanges();

                                count++;
                                string invtransCode = "IT" + "/" + _do.DOReceivedDate.Date.ToString("ddMMyy") + "/" + count.ToString();

                                InventoryTransaction _it = new InventoryTransaction()
                                {
                                    InventoryTransComments = _do.DOCode,
                                    Product    = _dosp.PurchaseOrderSupplierProduct.SupplierProduct.Product,
                                    ProductId  = _dosp.PurchaseOrderSupplierProduct.SupplierProduct.Product.Id,
                                    Employee   = _do.ReceivedBy,
                                    EmployeeId = _do.ReceivedBy.Id,
                                    InventoryChangeQuantity = _dosp.DOQuantityReceived,
                                    ITStatus           = Enums.ITStatus.Auto,
                                    InventoryTransDate = _do.DOReceivedDate.Date,
                                    ITCode             = invtransCode,
                                    ProductPrice       = _dosp.PurchaseOrderSupplierProduct.POUnitPrice
                                };
                                db.Add(_it);
                                db.SaveChanges();
                                //Adjust Inventory Quantity
                                Product _p = db.Products.Find(_it.Product.Id);
                                _p.InventoryQuantity = _p.InventoryQuantity + _it.InventoryChangeQuantity;

                                db.Products.Update(_p);
                            }
                        }
                    }

                    _do.DOStatus = Enums.DOStatus.Completed;
                    db.Update(_do);

                    //Find All DO with respect to the po that needs to be updated
                    List <DeliveryOrder> doListCheck = db.DeliveryOrders
                                                       .Where(x => x.PurchaseOrder == _po)
                                                       .ToList();

                    //Find List of Products in the PO
                    List <Product> prodListCheck = db.PurchaseOrderSupplierProducts.Where(x => x.PurchaseOrder == _po)
                                                   .Select(x => x.SupplierProduct.Product)
                                                   .Distinct()
                                                   .ToList();

                    Dictionary <int, int> _prodCountCheckDict = new Dictionary <int, int>();

                    //Assign Dictionary
                    for (int i = 0; i < prodListCheck.Count; i++)
                    {
                        int p = prodListCheck[i].Id;
                        _prodCountCheckDict[p] = 0;
                    }

                    //Update Dict Value for Checking
                    for (int i = 0; i < prodListCheck.Count; i++)
                    {
                        List <DeliveryOrderSupplierProduct> _dosrList = FindDOSRByProductIdAndPO(_po, prodListCheck[i].Id);
                        foreach (DeliveryOrderSupplierProduct _dosr in _dosrList)
                        {
                            int p2 = _dosr.PurchaseOrderSupplierProduct.SupplierProduct.Product.Id;
                            _prodCountCheckDict[p2] = _prodCountCheckDict[p2] + _dosr.DOQuantityReceived;
                        }
                    }

                    bool isCompleted = true;
                    //Check Dict Value
                    foreach (KeyValuePair <int, int> entry in _prodCountCheckDict)
                    {
                        PurchaseOrderSupplierProduct _posr = db.PurchaseOrderSupplierProducts
                                                             .Where(x => x.SupplierProduct.Product.Id == entry.Key).First();
                        if (entry.Value != _posr.POQuantityRequested)
                        {
                            isCompleted = false;
                            break;
                        }
                    }
                    //If all items are completed, then check to save as PO Completed or Not Completed
                    if (isCompleted == false)
                    {
                        _po.POStatus = Enums.POStatus.NotCompleted;
                        db.Update(_po);
                        db.SaveChanges();
                    }
                    else
                    {
                        _po.POStatus = Enums.POStatus.Completed;
                        db.Update(_po);
                        db.SaveChanges();
                    }

                    transcat.Commit();
                    return(true);
                }
                catch
                {
                    transcat.Rollback();
                    return(false);
                }
            }
        }