public RetrievalDetail(int id, Retrieval retrievalId, Item itemId, Department departmentId, int neededQty, int actualQty)
 {
     this.Id = id;
     this.Retrieval = retrievalId;
     this.Item = itemId;
     this.Department = departmentId;
     this.NeededQty = neededQty;
     this.ActualQty = actualQty;
 }
 public RetrievalDetail(int id, Retrieval retrievalId, Item itemId, Department departmentId, int neededQty, int actualQty)
 {
     this.Id         = id;
     this.Retrieval  = retrievalId;
     this.Item       = itemId;
     this.Department = departmentId;
     this.NeededQty  = neededQty;
     this.ActualQty  = actualQty;
 }
        public ManageStationeryRetrievalListControl(int retrievalId)
        {
            init();

            retrieval = new Retrieval();
            retrieval.Id = retrievalId;
            retrieval = retrievalBroker.GetRetrieval(retrieval);

            retrievalList = new Dictionary<string, List<object>>();
            unfulfilledRetrievalList = new Dictionary<string, List<object>>();

            foreach (RetrievalDetail retrievalDetail in retrieval.RetrievalDetails)
            {
                if (unfulfilledRetrievalList.ContainsKey(retrievalDetail.Item.Id.Trim()))
                {
                    retrievalInfo = unfulfilledRetrievalList[retrievalDetail.Item.Id];
                    retrievalInfo[2] = Converter.objToInt(retrievalInfo[2]) + Converter.objToInt(retrievalDetail.NeededQty);
                    ((RetrievalTable)retrievalInfo[4]).Add(retrievalDetail.Department.Name, retrievalDetail.NeededQty, retrievalDetail.ActualQty.HasValue ? retrievalDetail.ActualQty.Value : 0, DateTime.Now);// retrievalDetail.Requisition.CreatedDate);

                    // have the same total with current balance / fulfilled
                    //if (Converter.objToInt(retrievalInfo[2]) <= Converter.objToInt(retrievalInfo[3]))
                    //{
                    //    ((RetrievalTable)retrievalInfo[4]).FullFill();
                    //    retrievalList.Add(requisitionDetail.Item.Id, retrievalInfo);
                    //    unfulfilledRetrievalList.Remove(requisitionDetail.Item.Id);
                    //}
                    //else
                    //{
                    unfulfilledRetrievalList[retrievalDetail.Item.Id] = retrievalInfo;
                    //}
                }
                else
                {
                    retrievalInfo = new List<object>();
                    retrievalInfo.Add(retrievalDetail.Item.Description);
                    retrievalInfo.Add(retrievalDetail.Item.Bin);
                    retrievalInfo.Add(retrievalDetail.NeededQty);
                    int currentItemBalance = itemBroker.GetCurrentBalance(retrievalDetail.Item);
                    retrievalInfo.Add(currentItemBalance);
                    retrievalInfo.Add(new RetrievalTable(retrievalDetail.Department.Name, retrievalDetail.NeededQty, retrievalDetail.ActualQty.HasValue ? retrievalDetail.ActualQty.Value : 0, DateTime.Now));// requisitionCollectionDetail.Requisition.CreatedDate));

                    //if (requisitionDetail.Qty <= currentItemBalance)
                    //{
                    //    retrievalList.Add(requisitionDetail.Item.Id, retrievalInfo);
                    //}
                    //else
                    //{
                    unfulfilledRetrievalList.Add(retrievalDetail.Item.Id.Trim(), retrievalInfo);
                    //}
                }
            }
        }
        /// <summary>
        /// Logically delete the Item table by setting the status to 2 in the Item table
        ///  Return Constants.DB_STATUS
        /// </summary>
        /// <param name="retrieval"></param>
        /// <returns></returns>
        public Constants.DB_STATUS Delete(Retrieval retrieval)
        {
            Constants.DB_STATUS status = Constants.DB_STATUS.UNKNOWN;

            try
            {
                Retrieval retrieve = inventory.Retrievals.Where(rObj => rObj.Id == retrieval.Id).First();
                retrieve.Status = 2;
                inventory.SaveChanges();
                status = Constants.DB_STATUS.SUCCESSFULL;
            }
            catch (Exception e)
            {
                status = Constants.DB_STATUS.FAILED;
            }
            return status;
        }
        /// <summary>
        /// Update RetrievalDetail data to RetrievalDetail Table according to the RetrievalDetail Parameter
        /// Return Constants.DB_STATUS
        /// </summary>
        /// <param name="retrievalDetail"></param>
        /// <returns></returns>
        public Constants.DB_STATUS Update(RetrievalDetail retrievalDetail)
        {
            Constants.DB_STATUS status = Constants.DB_STATUS.UNKNOWN;
            try
            {
                RetrievalDetail rDetail = inventory.RetrievalDetails.Where(rObj => rObj.Id == retrievalDetail.Id).First();
                if (!rDetail.Equals(null))
                {
                    retrieval = inventory.Retrievals.Where(r => r.Id == retrievalDetail.Retrieval.Id).First();
                    Item item = inventory.Items.Where(i => i.Id == retrievalDetail.Item.Id).First();
                    Department department = inventory.Departments.Where(d => d.Id == retrievalDetail.Department.Id).First();

                    rDetail.Retrieval = retrieval;
                    rDetail.Item = item;
                    rDetail.Department = department;
                    rDetail.NeededQty = retrievalDetail.NeededQty;
                    rDetail.ActualQty = retrievalDetail.ActualQty;
                    inventory.SaveChanges();
                    status = Constants.DB_STATUS.SUCCESSFULL;
                }
            }
            catch (Exception e)
            {
                status = Constants.DB_STATUS.FAILED;
            }
            return status;
        }
        /// <summary>
        ///  Update Retrieval data to Retrieval Table according to the Retrieval Parameter
        ///    Return Constants.DB_STATUS 
        /// </summary>
        /// <param name="retrieval"></param>
        /// <returns></returns>
        public Constants.DB_STATUS Update(Retrieval retrieval)
        {
            Constants.DB_STATUS status = Constants.DB_STATUS.UNKNOWN;

            try
            {
                Retrieval retrievalObj = inventory.Retrievals.Where(rObj => rObj.Id == retrieval.Id).First();
                Employee createdBy = inventory.Employees.Where(e => e.Id == retrieval.CreatedBy.Id).First();

                retrievalObj.Id = retrieval.Id;
                retrievalObj.CreatedBy = createdBy;
                retrievalObj.CreatedDate = retrieval.CreatedDate;
                foreach (RetrievalDetail retrievalDetail in retrieval.RetrievalDetails)
                {
                    this.Update(retrievalDetail);
                }
                inventory.SaveChanges();
                status = Constants.DB_STATUS.SUCCESSFULL;
            }
            catch (Exception e)
            {
                status = Constants.DB_STATUS.FAILED;
            }
            return status;
        }
        /// <summary>
        /// Insert Retrieval data to the Retrieval Table according to the Retrieval Parameter
        ///  Return Constants.DB_STATUS 
        /// </summary>
        /// <param name="newRetrieval"></param>
        /// <returns></returns>
        public Constants.DB_STATUS Insert(Retrieval newRetrieval)
        {
            Constants.DB_STATUS status = Constants.DB_STATUS.UNKNOWN;
            try
            {
                inventory.AddToRetrievals(newRetrieval);
                //foreach (RetrievalDetail retrievalDetail in newRetrieval.RetrievalDetails)
                //{
                //    this.Insert(retrievalDetail);
                //}
                inventory.SaveChanges();
                status = Constants.DB_STATUS.SUCCESSFULL;
            }
            catch (Exception e)
            {
                status = Constants.DB_STATUS.FAILED;
            }

            return status;
        }
        public int GetSumRetrievalDetailQty(QTY_TYPE quantityType, Retrieval retrieval)
        {
            int? totalQty = 0;
            switch (quantityType)
            {
                case QTY_TYPE.ACTUAL_QTY:
                    totalQty = retrieval.RetrievalDetails.Where(x=> x.Retrieval.Id == retrieval.Id).Sum(x => x.ActualQty).Value;
                    break;
                case QTY_TYPE.NEEDED_QTY:
                    totalQty = retrieval.RetrievalDetails.Where(x => x.Retrieval.Id == retrieval.Id).Sum(x => x.NeededQty);
                    break;
            }

            return totalQty.HasValue ? totalQty.Value : 0;
        }
        //public int GetRetrievalId()
        //{
        //    var maxRetrievalId = inventory.Retrievals.Max(xObj => xObj.Id) + 1;
        //    return maxRetrievalId;
        //}
        /// <summary>
        /// Retrieve the Retrieval Detail information  from Retrieval Table according to the Retrieval Parameter
        /// </summary>
        /// <param name="retrieval"></param>
        /// <returns></returns>
        public Retrieval GetRetrieval(Retrieval retrieval)
        {
            try
            {
                retrieval = inventory.Retrievals.Where(robj => robj.Id == retrieval.Id).First();
                //if (!retrieval.Equals(null))
                //{
                //    //var retrievalDetailResult = from rd in inventory.RetrievalDetails
                //    //                            where rd.Retrieval.Id == retrieval.Id
                //    //                            select rd;
                //    //foreach (RetrievalDetail rd in retrievalDetailResult)
                //    //{
                //    //    retrieval.RetrievalDetails.Add(rd);

                //    // }
                //    return retrieval;
                //}
            }
            catch (Exception e)
            {
                retrieval = null;
            }
            return retrieval;
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Retrievals EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToRetrievals(Retrieval retrieval)
 {
     base.AddObject("Retrievals", retrieval);
 }
 /// <summary>
 /// Create a new Retrieval object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="createdDate">Initial value of the CreatedDate property.</param>
 /// <param name="status">Initial value of the Status property.</param>
 public static Retrieval CreateRetrieval(global::System.Int32 id, global::System.DateTime createdDate, global::System.Int32 status)
 {
     Retrieval retrieval = new Retrieval();
     retrieval.Id = id;
     retrieval.CreatedDate = createdDate;
     retrieval.Status = status;
     return retrieval;
 }
        public ManageStationeryRetrievalListControl(object obj)
        {
            if (obj is List<RequisitionCollection>)
            {
                init();
                requisitionCollectionBroker = new RequisitionCollectionBroker(inventory);
                List<RequisitionCollection> requisitionCollectionList = (List<RequisitionCollection>)obj;

                retrieval = new Retrieval(retrievalBroker.GetRetrievalId(), DateTime.Now, Util.GetEmployee(employeeBroker), Converter.objToInt(Constants.VISIBILITY_STATUS.SHOW));

                retrievalList = new Dictionary<string, List<object>>();
                unfulfilledRetrievalList = new Dictionary<string, List<object>>();

                foreach (RequisitionCollection requisitionCollection in requisitionCollectionList)
                {
                    foreach (RequisitionCollectionDetail requisitionCollectionDetail in requisitionCollection.RequisitionCollectionDetails)
                    {
                        foreach (RequisitionDetail requisitionDetail in requisitionCollectionDetail.Requisition.RequisitionDetails)
                        {
                            if (unfulfilledRetrievalList.ContainsKey(requisitionDetail.Item.Id.Trim()))
                            {
                                retrievalInfo = unfulfilledRetrievalList[requisitionDetail.Item.Id];
                                retrievalInfo[2] = Converter.objToInt(retrievalInfo[2]) + Converter.objToInt(requisitionDetail.Qty);
                                ((RetrievalTable)retrievalInfo[4]).Add(requisitionCollectionDetail.Requisition.Department.Name, requisitionDetail.Qty, 0, requisitionCollectionDetail.Requisition.CreatedDate);

                                // have the same total with current balance / fulfilled
                                //if (Converter.objToInt(retrievalInfo[2]) <= Converter.objToInt(retrievalInfo[3]))
                                //{
                                //    ((RetrievalTable)retrievalInfo[4]).FullFill();
                                //    retrievalList.Add(requisitionDetail.Item.Id, retrievalInfo);
                                //    unfulfilledRetrievalList.Remove(requisitionDetail.Item.Id);
                                //}
                                //else
                                //{
                                    unfulfilledRetrievalList[requisitionDetail.Item.Id] = retrievalInfo;
                                //}
                            }
                            else
                            {
                                retrievalInfo = new List<object>();
                                retrievalInfo.Add(requisitionDetail.Item.Description);
                                retrievalInfo.Add(requisitionDetail.Item.Bin);
                                retrievalInfo.Add(requisitionDetail.Qty);
                                int currentItemBalance = itemBroker.GetCurrentBalance(requisitionDetail.Item);
                                retrievalInfo.Add(currentItemBalance);
                                retrievalInfo.Add(new RetrievalTable(requisitionCollectionDetail.Requisition.Department.Name, requisitionDetail.Qty, 0, requisitionCollectionDetail.Requisition.CreatedDate));

                                //if (requisitionDetail.Qty <= currentItemBalance)
                                //{
                                //    retrievalList.Add(requisitionDetail.Item.Id, retrievalInfo);
                                //}
                                //else
                                //{
                                    unfulfilledRetrievalList.Add(requisitionDetail.Item.Id.Trim(), retrievalInfo);
                                //}
                            }
                        }
                    }
                    requisitionCollection.Status = Converter.objToInt(Constants.COLLECTION_STATUS.NEED_TO_COLLECT);
                    requisitionCollectionBroker.Update(requisitionCollection);
                }

                List<string> keyToBeRemoved = new List<string>();
                foreach (string key in unfulfilledRetrievalList.Keys)
                {
                    retrievalInfo = unfulfilledRetrievalList[key];
                    if (((RetrievalTable)retrievalInfo[4]).GetTotalQty("NeededQty") <= Converter.objToInt(retrievalInfo[3]))
                    {
                        ((RetrievalTable)retrievalInfo[4]).FullFill();
                        retrievalList.Add(key, retrievalInfo);
                        keyToBeRemoved.Add(key);
                    }
                }

                foreach (string key in keyToBeRemoved)
                {
                    unfulfilledRetrievalList.Remove(key);
                }

                int addedId = 0;

                foreach (string key in retrievalList.Keys)
                {
                    retrievalInfo = retrievalList[key];
                    Item item = new Item();
                    item.Id = key;
                    item = itemBroker.GetItem(item);

                    foreach(DataRow dr in ((RetrievalTable)retrievalInfo[4]).Rows)
                    {
                        RetrievalDetail retrievalDetail = new RetrievalDetail();
                        retrievalDetail.Id = retrievalBroker.GetRetrievalDetailId() + (addedId++);
                        retrievalDetail.Item = item;

                        Department department = new Department();
                        department.Name = dr["Department"].ToString();
                        department = departmentBroker.GetDepartment(department);
                        retrievalDetail.Department = department;

                        retrievalDetail.NeededQty = Converter.objToInt(dr["NeededQty"]);
                        retrievalDetail.ActualQty = Converter.objToInt(dr["ActualQty"]);

                        retrieval.RetrievalDetails.Add(retrievalDetail);
                    }
                }

                foreach (string key in unfulfilledRetrievalList.Keys)
                {
                    retrievalInfo = unfulfilledRetrievalList[key];
                    Item item = new Item();
                    item.Id = key;
                    item = itemBroker.GetItem(item);

                    foreach (DataRow dr in ((RetrievalTable)retrievalInfo[4]).Rows)
                    {
                        RetrievalDetail retrievalDetail = new RetrievalDetail();
                        retrievalDetail.Id = retrievalBroker.GetRetrievalDetailId() + (addedId++);
                        retrievalDetail.Item = item;

                        Department department = new Department();
                        department.Name = dr["Department"].ToString();
                        department = departmentBroker.GetDepartment(department);
                        retrievalDetail.Department = department;

                        retrievalDetail.NeededQty = Converter.objToInt(dr["NeededQty"]);
                        retrievalDetail.ActualQty = Converter.objToInt(dr["ActualQty"]);

                        retrieval.RetrievalDetails.Add(retrievalDetail);
                    }
                }

                if (retrievalBroker.Insert(retrieval) == Constants.DB_STATUS.FAILED)
                {
                    // do something
                }
            }
        }