Esempio n. 1
0
        public bool saveReceivedProds3(List <StationeryRetrievalProduct> srpList,
                                       int SRid,
                                       Employee warehousepacker,
                                       Employee storeclerk,
                                       List <StationeryRetrievalRequisitionForm> srrfList)
        {
            using (IDbContextTransaction transcat = db.Database.BeginTransaction())
            {
                try
                {
                    //Check for store clerk and warehouse packer
                    Employee _whpacker = empService.FindByUsernameAndPassword(warehousepacker.Username.ToLower(), warehousepacker.Password);
                    Employee _stclerk  = empService.FindByUsernameAndPassword(storeclerk.Username.ToLower(), storeclerk.Password);

                    if (_whpacker.EmployeeType.EmployeeTypeName != "Warehouse Packer")
                    {
                        throw new Exception("The user type is wrong");
                    }

                    if (_stclerk.EmployeeType.EmployeeTypeName != "Store Clerk" && _stclerk.EmployeeType.EmployeeTypeName != "Store Supervisor" && _stclerk.EmployeeType.EmployeeTypeName != "Store Manager")
                    {
                        throw new Exception("The user type is wrong");
                    }

                    if (_whpacker == null || _stclerk == null)
                    {
                        throw new Exception("The username or password is wrong, please login again");
                    }

                    DateTime transactDate = DateTime.Now;

                    //Create RF List
                    List <RequisitionForm> _rfList = new List <RequisitionForm>();

                    //Create new empty list to save to db
                    List <StationeryRetrievalProduct> _srpList = new List <StationeryRetrievalProduct>();

                    //Create new empty list to save to db
                    List <StationeryRetrievalRequisitionForm> _srrfList = new List <StationeryRetrievalRequisitionForm>();

                    //Find sr
                    StationeryRetrieval _sr = db.StationeryRetrievals.Find(SRid);
                    _sr.SRStatus        = SRStatus.PendingAssignment;
                    _sr.WarehousePacker = _whpacker;
                    _sr.SRRetrievalDate = transactDate;
                    _sr.StoreClerk      = _stclerk;
                    db.StationeryRetrievals.Update(_sr);
                    db.SaveChanges();



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

                    foreach (StationeryRetrievalProduct srp in srpList)
                    {
                        if (srp.ProductReceivedTotal < 0)
                        {
                            throw new Exception("This value should not be negative");
                        }

                        count++;

                        StationeryRetrievalProduct _srp = db.StationeryRetrievalProduct.Find(srp.Id);
                        _srp.ProductReceivedTotal = srp.ProductReceivedTotal;
                        _srpList.Add(_srp);
                        db.StationeryRetrievalProduct.Update(_srp);
                        db.SaveChanges();

                        //Check for logic in terms of each RFP should not have more than what was specified


                        //create itCode
                        string invtransCode = "IT" + "/" + DateTime.Now.ToString("ddMMyy") + "/" + count.ToString();

                        //Create It
                        InventoryTransaction _it = new InventoryTransaction()
                        {
                            EmployeeId = _sr.StoreClerk.Id,
                            ProductId  = _srp.Product.Id,
                            Employee   = _sr.StoreClerk,
                            InventoryChangeQuantity = -_srp.ProductReceivedTotal,
                            InventoryTransComments  = _sr.SRCode,
                            InventoryTransDate      = _sr.SRRetrievalDate,
                            ITStatus = ITStatus.Auto,
                            ITCode   = invtransCode,
                            Product  = _srp.Product
                        };

                        db.InventoryTransactions.Add(_it);
                        db.SaveChanges();



                        //Adjust Inventory Quantity
                        Product _p = db.Products.Find(_srp.Product.Id);
                        _p.InventoryQuantity = _p.InventoryQuantity + _it.InventoryChangeQuantity;
                        if (_p.InventoryQuantity < 0)
                        {
                            throw new Exception(_p.InventoryQuantity + " is not enough");
                        }
                        db.Products.Update(_p);
                        db.SaveChanges();
                    }

                    foreach (StationeryRetrievalRequisitionForm srrf in srrfList)
                    {
                        StationeryRetrievalRequisitionForm _srrf = db.StationeryRetrievalRequisitionForms.Find(srrf.Id);
                        _srrf.SRRFStatus = SRRFStatus.PendingAssignment;
                        db.StationeryRetrievalRequisitionForms.Update(_srrf);
                        db.SaveChanges();

                        RequisitionForm _rf = db.RequisitionForms.Find(_srrf.RequisitionForm.Id);
                        _rf.RFStatus = RFStatus.Ongoing;

                        db.RequisitionForms.Update(_rf);
                        db.SaveChanges();
                    }
                    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);
                }
            }
        }