public static void PurgeAutomaticallyEnteredInventory(int itemID, int storeID, int year)
 {
     YearEnd yearEnd = new YearEnd();
     yearEnd.Where.ItemID.Value = itemID;
     yearEnd.Where.StoreID.Value = storeID;
     yearEnd.Where.Year.Value = year;
     yearEnd.Where.AutomaticallyEntered.Value = true;
     yearEnd.Query.Load();
     yearEnd.DeleteAll();
     yearEnd.Save();
 }
示例#2
0
        public void GenerateAutomaticInventoryByUnit()
        {
            var ethDate = new EthiopianDate.EthiopianDate();
            var stores  = new Stores();

            stores.GetActiveStores();
            var rec     = new ReceiveDoc();
            var itm     = new Items();
            var yearEnd = new YearEnd();
            var balance = new Balance();

            if (!InventoryRequiredForHandlingUnit(false))
            {
                return;
            }
            while (!stores.EOF) //This needs to be done for each store and for each item
            {
                //bereket
                //itm.ExcludeNeverReceivedItemsNoCategoryForHandlingUnit(stores.ID);
                itm.ExcludeNeverReceivedItemsNoCategoryForHandlingUnitOptimized(stores.ID, ethDate.Year);
                while (!itm.EOF) //For each item
                {
                    var receivedoc = rec.GetDistinctUnitIDFromReceivedDoc(itm.ID);
                    foreach (var dr in receivedoc.Rows.Cast <DataRow>().Where(dr => !DoesBalanceExistByUnit(ethDate.Year, itm.ID, stores.ID, true, Convert.ToInt32(dr["UnitID"]))))
                    {
                        //need optimization
                        //we can exclude the already calculated items when we exclude never recieved items
                        yearEnd.LoadByItemIDStoreAndYearAndUnit(itm.ID, stores.ID, ethDate.Year, true, Convert.ToInt32(dr["UnitID"]));

                        if (yearEnd.RowCount > 0)
                        {
                            continue;
                        }

                        yearEnd.AddNew();
                        yearEnd.ItemID  = itm.ID;
                        yearEnd.StoreID = stores.ID;
                        yearEnd.Year    = ethDate.Year;

                        //need optimization
                        //atleast we can get the value directly by filtering using storeid and itemid rather than
                        //selecting all items in all stores and filter it by code
                        //yearEnd.EBalance = balance.GetSOHByUnit(itm.ID, stores.ID, ethDate.Month, ethDate.Year, Convert.ToInt32(dr["UnitID"]));
                        yearEnd.EBalance             = balance.GetSOHByUnitOptimized(itm.ID, stores.ID, ethDate.Month, ethDate.Year, Convert.ToInt32(dr["UnitID"]));
                        yearEnd.PhysicalInventory    = yearEnd.EBalance;
                        yearEnd.AutomaticallyEntered = true;
                        yearEnd.UnitID = Convert.ToInt32(dr["UnitID"]);
                        yearEnd.Save();
                    }
                    itm.MoveNext();
                }
                stores.MoveNext();
            }
        }
示例#3
0
        public static void PurgeAutomaticallyEnteredInventory(int itemID, int storeID, int year)
        {
            YearEnd yearEnd = new YearEnd();

            yearEnd.Where.ItemID.Value  = itemID;
            yearEnd.Where.StoreID.Value = storeID;
            yearEnd.Where.Year.Value    = year;
            yearEnd.Where.AutomaticallyEntered.Value = true;
            yearEnd.Query.Load();
            yearEnd.DeleteAll();
            yearEnd.Save();
        }
 /// <summary>
 /// Year End without the physical store information
 /// </summary>
 /// <param name="_itemID">The _item ID.</param>
 /// <param name="_storeID">The _store ID.</param>
 /// <param name="_unitID">The _unit ID.</param>
 /// <param name="_endingBal">The _ending bal.</param>
 /// <param name="_physicalInventory">The _physical inventory.</param>
 /// <param name="year">The year.</param>
 public static void AddNewYearEndEntry(int _itemID, int _storeID, int _unitID, long _endingBal, long _physicalInventory, int year)
 {
     BLL.YearEnd yEnd = new YearEnd();
     yEnd.AddNew();
     yEnd.StoreID = _storeID;
     yEnd.ItemID = _itemID;
     yEnd.UnitID = _unitID;
     yEnd.PhysicalInventory = _physicalInventory;
     yEnd.EBalance = _endingBal;
     yEnd.Year = year;
     yEnd.Save();
 }
 /// <summary>
 /// Year End without the physical store information
 /// </summary>
 /// <param name="_itemID">The _item ID.</param>
 /// <param name="_storeID">The _store ID.</param>
 /// <param name="_unitID">The _unit ID.</param>
 /// <param name="_endingBal">The _ending bal.</param>
 /// <param name="_physicalInventory">The _physical inventory.</param>
 /// <param name="year">The year.</param>
 public static void AddNewYearEndEntry(int _itemID, int _storeID, int _unitID, long _endingBal, long _physicalInventory, int year)
 {
     BLL.YearEnd yEnd = new YearEnd();
     yEnd.AddNew();
     yEnd.StoreID           = _storeID;
     yEnd.ItemID            = _itemID;
     yEnd.UnitID            = _unitID;
     yEnd.PhysicalInventory = _physicalInventory;
     yEnd.EBalance          = _endingBal;
     yEnd.Year = year;
     yEnd.Save();
 }
示例#6
0
        public void GenerateAutomaticInventory()
        {
            var ethDate = new EthiopianDate.EthiopianDate();

            if (InventoryRequired(false))
            {
                Stores stores = new Stores();
                stores.GetActiveStores();
                while (!stores.EOF) //This needs to be done for each store
                {
                    if (!DoesBalanceExist(ethDate.Year, stores.ID, true))
                    //If Inventory information hasn't already been filled for this store
                    {
                        var itm = new Items();

                        //bereket
                        //itm.ExcludeNeverReceivedItemsNoCategory(stores.ID);
                        itm.ExcludeNeverReceivedItemsNoCategoryOptimized(stores.ID, ethDate.Year);
                        while (!itm.EOF) //For each time
                        {
                            var yearEnd = new YearEnd();
                            var balance = new Balance();

                            yearEnd.LoadByItemIDStoreAndYear(itm.ID, stores.ID, ethDate.Year, true);

                            if (yearEnd.RowCount > 0)
                            {
                                return;
                            }

                            //YearEnd.PurgeAutomaticallyEnteredInventory(itm.ID, stores.ID, ethDate.Year);
                            yearEnd.AddNew();
                            yearEnd.ItemID  = itm.ID;
                            yearEnd.StoreID = stores.ID;
                            yearEnd.Year    = ethDate.Year;

                            //bereket
                            //yearEnd.EBalance = balance.GetSOH(itm.ID, stores.ID, 10, ethDate.Year);
                            yearEnd.EBalance             = balance.GetSOHOptimized(itm.ID, stores.ID, 10, ethDate.Year);
                            yearEnd.PhysicalInventory    = yearEnd.EBalance;
                            yearEnd.AutomaticallyEntered = true;
                            yearEnd.UnitID = 0;
                            yearEnd.Save();
                            itm.MoveNext();
                        }
                    }
                    stores.MoveNext();
                }
            }
        }
        /// <summary>
        /// Year End with the physical store information
        /// </summary>
        /// <param name="_itemID">The _item ID.</param>
        /// <param name="_storeID">The _store ID.</param>
        /// <param name="_unitID">The _unit ID.</param>
        /// <param name="_endingBal">The _ending bal.</param>
        /// <param name="_physicalInventory">The _physical inventory.</param>
        /// <param name="year">The year.</param>
        /// <param name="physicalStoreID">The physical store ID.</param>
        public static void AddNewYearEndEntry(int _itemID, int _storeID, int _unitID, long _endingBal, long _physicalInventory, int year, int physicalStoreID)
        {
            BLL.YearEnd yEnd = new YearEnd();
            yEnd.LoadByItemUnitStoreAndPhysicalStore(_itemID, _unitID, _storeID, physicalStoreID, year);

            if (yEnd.RowCount == 0)
                yEnd.AddNew();
            yEnd.StoreID = _storeID;
            yEnd.ItemID = _itemID;
            yEnd.UnitID = _unitID;
            yEnd.PhysicalInventory = _physicalInventory;
            yEnd.EBalance = _endingBal;
            yEnd.Year = year;
            yEnd.PhysicalStoreID = physicalStoreID;
            yEnd.Save();
        }
示例#8
0
        public static void PurgeAutomaticallyEnteredInventoryForUnit(int itemID, int storeID, int year, int unitID)
        {
            YearEnd yearEnd = new YearEnd();

            yearEnd.Where.ItemID.Value  = itemID;
            yearEnd.Where.StoreID.Value = storeID;
            yearEnd.Where.Year.Value    = year;
            yearEnd.Where.AutomaticallyEntered.Value = true;
            yearEnd.Where.UnitID.Value = unitID;
            yearEnd.Query.Load();
            DataTable dt = yearEnd.DefaultView.ToTable();

            PurgeAutomaticallyEnteredInventoryDetail(dt);
            yearEnd.DeleteAll();
            yearEnd.Save();
        }
        /// <summary>
        /// Year End with the physical store information
        /// </summary>
        /// <param name="_itemID">The _item ID.</param>
        /// <param name="_storeID">The _store ID.</param>
        /// <param name="_unitID">The _unit ID.</param>
        /// <param name="_endingBal">The _ending bal.</param>
        /// <param name="_physicalInventory">The _physical inventory.</param>
        /// <param name="year">The year.</param>
        /// <param name="physicalStoreID">The physical store ID.</param>
        public static void AddNewYearEndEntry(int _itemID, int _storeID, int _unitID, long _endingBal, long _physicalInventory, int year, int physicalStoreID)
        {
            BLL.YearEnd yEnd = new YearEnd();
            yEnd.LoadByItemUnitStoreAndPhysicalStore(_itemID, _unitID, _storeID, physicalStoreID, year);

            if (yEnd.RowCount == 0)
            {
                yEnd.AddNew();
            }
            yEnd.StoreID           = _storeID;
            yEnd.ItemID            = _itemID;
            yEnd.UnitID            = _unitID;
            yEnd.PhysicalInventory = _physicalInventory;
            yEnd.EBalance          = _endingBal;
            yEnd.Year            = year;
            yEnd.PhysicalStoreID = physicalStoreID;
            yEnd.Save();
        }
        private bool SaveInventoryWithOutUnit()
        {
            var yEnd = new YearEnd();
            var rec = new ReceiveDoc();

            if (IsValid())
            {
                dtDate.Value = DateTime.Now;
                DateTime dtCurent = new DateTime();
                dtDate.CustomFormat = "MM/dd/yyyy";
                dtCurent = ConvertDate.DateConverter(dtDate.Text);

                int year = dtCurent.Year;
                if (XtraMessageBox.Show("Are You Sure, You want to save this Transaction?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DataTable yearEndTable = (DataTable)grdYearEnd.DataSource;

                    for (int i = 0; i < dtBB.Rows.Count; i++)
                    {
                        int id = 0;
                        int storeID = Convert.ToInt32(cboStores.EditValue);
                        if (dtBB.Rows[i]["ItemID"] != DBNull.Value)
                        {
                            var itemID = Convert.ToInt32(dtBB.Rows[i]["ItemID"]);
                            //var unitID = Convert.ToInt32(dtBB.Rows[i]["UnitID"]);
                            id = yEnd.LoadByItemIDStoreAndYear(itemID, storeID, year, false);
                        }
                        if (id != 0 && !ReferenceEquals(yearEndTable.Rows[i]["Physical Inventory"], string.Empty))//There is already a manual entry in the yearend table.
                        {
                            //yEnd.LoadByPrimaryKey(id);
                            yEnd.BBalance = Convert.ToInt64(FilterNumbers(yearEndTable.Rows[i]["Beginning Balance"].ToString()));
                            yEnd.EBalance = Int64.Parse(FilterNumbers(yearEndTable.Rows[i]["Ending Balance(SOH)"].ToString()), NumberStyles.AllowThousands);
                            yEnd.PhysicalInventory = Convert.ToInt64(FilterNumbers(yearEndTable.Rows[i]["Physical Inventory"].ToString()));
                            //yEnd.BatchNo = yearEndTable.Rows[i]["Batch No."].ToString();
                            yEnd.UnitID = 0;
                            yEnd.Remark = yearEndTable.Rows[i]["Remark"].ToString();
                            yEnd.AutomaticallyEntered = false;
                            yEnd.Save();
                        }
                        else//There is no entry in the yearend table under this item name that has been entered manually.
                        {
                            DataRow cRow = dtBB.Rows[i];
                            if (!ReferenceEquals(cRow["Physical Inventory"], string.Empty) && cRow["ItemID"] != DBNull.Value)
                            {
                                var itemID = Convert.ToInt32(cRow["ItemID"]);
                                var unitID = 0;
                                YearEnd.PurgeAutomaticallyEnteredInventoryForUnit(itemID, storeID, year, unitID);
                                //if (areAllBatchesPhyInventoryNullValue == false) //We want to add an inventory record if at least one of the batches has a non empty value.
                                //{
                                yEnd.AddNew();
                                yEnd.ItemID = itemID;
                                yEnd.StoreID = storeID;
                                yEnd.Year = year;
                                yEnd.BBalance = Convert.ToInt64(cRow["Beginning Balance"]);
                                Int64 endBal = Convert.ToInt64(cRow["Ending Balance(SOH)"]);
                                //yEnd.BatchNo = cRow["Batch No."].ToString();
                                yEnd.EBalance = endBal;// Convert.ToInt64(yearEndTable.Rows[i].Cells[5].Value);

                                if (cRow["Physical Inventory"] != DBNull.Value)
                                    yEnd.PhysicalInventory = Convert.ToInt64(cRow["Physical Inventory"]);

                                //yEnd.PhysicalInventory = physicalInventoryTotal;
                                yEnd.UnitID = 0;
                                 yEnd.Remark = cRow["Remark"].ToString();

                                yEnd.AutomaticallyEntered = cRow["Physical Inventory"] == DBNull.Value;
                                yEnd.Save();
                                //}

                                //long physicalInventoryTotal = 0;
                                //bool areAllBatchesPhyInventoryNullValue = true;
                                //if (endBal != yEnd.PhysicalInventory)
                                if (true)
                                {
                                    int k = i + 1;

                                    if (k < dtBB.Rows.Count)
                                    {
                                        while (Convert.ToInt32(dtBB.Rows[k]["RecID"]) != -1)
                                        {
                                            if (dtBB.Rows[k]["Physical Inventory"] != DBNull.Value)
                                            {
                                                //areAllBatchesPhyInventoryNullValue = false;
                                                long batchPhysicalInventory =
                                                    Convert.ToInt64(dtBB.Rows[k]["Physical Inventory"]);
                                                //physicalInventoryTotal += batchPhysicalInventory;
                                                rec.LoadByPrimaryKey(Convert.ToInt32(dtBB.Rows[k]["RecID"]));
                                                rec.QuantityLeft = Convert.ToInt64(batchPhysicalInventory);
                                                rec.Remark = "Physical Inventory";
                                                rec.UnitID = 0;
                                                rec.Out = rec.QuantityLeft == 0;
                                                rec.Save();
                                            }
                                            k++;
                                            if (k >= yearEndTable.Rows.Count)
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    XtraMessageBox.Show("Transaction Succsfully Saved!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// Change the UnitID of one item to another UnitID.
        /// </summary>
        /// <param name="storeID"></param>
        /// <param name="itemID"></param>
        /// <param name="fromUnitID"></param>
        /// <param name="toUnitID"></param>
        public static void ChangeItemUnit(int storeID, int itemID, int fromUnitID, int toUnitID)
        {
            MyGeneration.dOOdads.TransactionMgr transaction = MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();
            try
            {
                transaction.BeginTransaction();
                ItemUnit iuFrom = new ItemUnit(), iuTo = new ItemUnit(), iuExecute = new ItemUnit();
                iuFrom.LoadByPrimaryKey(fromUnitID);
                iuTo.LoadByPrimaryKey(toUnitID);

                var receiveTableFilterQuery = HCMIS.Repository.Queries.ItemUnit.SelectChangeItemUnit(storeID, itemID, iuFrom.ID);

                string query;
                // Update Issue Doc
                query =
                    HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitIssueDoc(iuTo.QtyPerUnit, iuFrom.QtyPerUnit, receiveTableFilterQuery);
                iuExecute.LoadFromRawSql(query);

                // Update disposal table
                query =
                    HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitLossAndAdjustment(iuTo.QtyPerUnit, iuFrom.QtyPerUnit, receiveTableFilterQuery);
                iuExecute.LoadFromRawSql(query);

                // Update Order.
                query =
                    HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitOrderDetail(itemID, iuFrom.ID, iuFrom.QtyPerUnit, iuTo.QtyPerUnit);

                iuExecute.LoadFromRawSql(query);

                // Update Pick List Detail
                query =
                    HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitPickListDetail(iuTo.QtyPerUnit, iuFrom.QtyPerUnit, receiveTableFilterQuery);

                iuExecute.LoadFromRawSql(query);

                // change the Weighted Average Log
                try
                {
                    query = HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitMovingAverageHistory(storeID, iuTo.ID, iuFrom.ID);

                    iuExecute.LoadFromRawSql(query);
                }

                catch
                {

                }

                BLL.ReceiveDoc rd = new ReceiveDoc();
                rd.LoadByItemIDUnitIDStoreID(itemID, iuFrom.ID, storeID);
                rd.ChangeUnitID(iuTo.ID);

                //Change the YearEnd
                //If there is already an entry with the other UnitID, we need to merge the two.

                BLL.YearEnd yEndFrom = new YearEnd();
                yEndFrom.Where.UnitID.Value = iuFrom.ID;
                yEndFrom.Where.ItemID.Value = itemID;
                yEndFrom.Where.StoreID.Value = storeID;
                yEndFrom.Query.Load();

                if (yEndFrom.RowCount > 0)
                {
                    //There is a year end entry.
                    //If there is one by the new UnitID, we merge them.  If there is no entry by the new UnitID, we just change the values.
                    BLL.YearEnd yEndTo = new YearEnd();
                    yEndTo.Where.UnitID.Value = iuTo.ID;
                    yEndTo.Where.ItemID.Value = itemID;
                    yEndTo.Where.StoreID.Value = storeID;
                    yEndTo.Query.Load();
                    if (yEndTo.RowCount > 0)
                    {
                        //We need to merge.
                        if (!yEndTo.IsColumnNull("EndingPrice"))
                            yEndTo.EndingPrice += yEndFrom.EndingPrice;
                        else
                            yEndTo.EndingPrice = yEndFrom.EndingPrice;
                        if (!yEndTo.IsColumnNull("EBalance"))
                            yEndTo.EBalance += yEndFrom.EBalance;
                        else
                            yEndTo.EBalance = yEndFrom.EBalance;
                        if (!yEndTo.IsColumnNull("PhysicalInventory"))
                            yEndTo.PhysicalInventory += yEndFrom.PhysicalInventory;
                        else
                            yEndTo.PhysicalInventory = yEndFrom.PhysicalInventory;
                        if (!yEndTo.IsColumnNull("PhysicalInventoryPrice"))
                            yEndTo.PhysicalInventoryPrice += yEndFrom.PhysicalInventoryPrice;
                        else
                            yEndTo.PhysicalInventoryPrice = yEndFrom.PhysicalInventoryPrice;

                        yEndTo.Save();
                        yEndFrom.MarkAsDeleted();
                        yEndFrom.Save();
                    }
                    else
                    {
                        yEndFrom.UnitID = iuTo.ID;
                        yEndFrom.Save();
                    }
                }
                transaction.CommitTransaction();
            }
            catch(Exception ex)
            {
                transaction.RollbackTransaction();
                throw ex;
            }
        }
        /// <summary>
        /// Handles the processing of balance
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnProcessBalance_Click(object sender, EventArgs e)
        {
            Items itm = new Items();
            YearEnd yEnd = new YearEnd();
            Balance bal = new Balance();

            DataTable dtItm = itm.GetAllItems(1);
            dtDate.Value = DateTime.Now;
            DateTime dtCurent = Convert.ToDateTime(dtDate.Text);

            foreach (DataRow dr in dtItm.Rows)
            {
                yEnd.AddNew();
                yEnd.StoreID = Convert.ToInt32(cboStore.SelectedValue);
                yEnd.ItemID = Convert.ToInt32(dr["ID"]);
                yEnd.Year = Convert.ToInt32(cboYear.SelectedItem);
                yEnd.Month = (Convert.ToInt32(cboYear.SelectedItem) == dtCurent.Year) ? dtCurent.Month : 10;
                yEnd.BBalance = 0;
                yEnd.EBalance = 0;
                yEnd.PhysicalInventory = 0;
                yEnd.Save();
            }
        }
        /// <summary>
        /// Change the UnitID of one item to another UnitID.
        /// </summary>
        /// <param name="storeID"></param>
        /// <param name="itemID"></param>
        /// <param name="fromUnitID"></param>
        /// <param name="toUnitID"></param>
        public static void ChangeItemUnit(int storeID, int itemID, int fromUnitID, int toUnitID)
        {
            MyGeneration.dOOdads.TransactionMgr transaction = MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();
            try
            {
                transaction.BeginTransaction();
                ItemUnit iuFrom = new ItemUnit(), iuTo = new ItemUnit(), iuExecute = new ItemUnit();
                iuFrom.LoadByPrimaryKey(fromUnitID);
                iuTo.LoadByPrimaryKey(toUnitID);

                var receiveTableFilterQuery = HCMIS.Repository.Queries.ItemUnit.SelectChangeItemUnit(storeID, itemID, iuFrom.ID);

                string query;
                // Update Issue Doc
                query =
                    HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitIssueDoc(iuTo.QtyPerUnit, iuFrom.QtyPerUnit, receiveTableFilterQuery);
                iuExecute.LoadFromRawSql(query);

                // Update disposal table
                query =
                    HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitLossAndAdjustment(iuTo.QtyPerUnit, iuFrom.QtyPerUnit, receiveTableFilterQuery);
                iuExecute.LoadFromRawSql(query);

                // Update Order.
                query =
                    HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitOrderDetail(itemID, iuFrom.ID, iuFrom.QtyPerUnit, iuTo.QtyPerUnit);

                iuExecute.LoadFromRawSql(query);

                // Update Pick List Detail
                query =
                    HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitPickListDetail(iuTo.QtyPerUnit, iuFrom.QtyPerUnit, receiveTableFilterQuery);

                iuExecute.LoadFromRawSql(query);

                // change the Weighted Average Log
                try
                {
                    query = HCMIS.Repository.Queries.ItemUnit.UpdateChangeItemUnitMovingAverageHistory(storeID, iuTo.ID, iuFrom.ID);

                    iuExecute.LoadFromRawSql(query);
                }

                catch
                {
                }

                BLL.ReceiveDoc rd = new ReceiveDoc();
                rd.LoadByItemIDUnitIDStoreID(itemID, iuFrom.ID, storeID);
                rd.ChangeUnitID(iuTo.ID);

                //Change the YearEnd
                //If there is already an entry with the other UnitID, we need to merge the two.

                BLL.YearEnd yEndFrom = new YearEnd();
                yEndFrom.Where.UnitID.Value  = iuFrom.ID;
                yEndFrom.Where.ItemID.Value  = itemID;
                yEndFrom.Where.StoreID.Value = storeID;
                yEndFrom.Query.Load();

                if (yEndFrom.RowCount > 0)
                {
                    //There is a year end entry.
                    //If there is one by the new UnitID, we merge them.  If there is no entry by the new UnitID, we just change the values.
                    BLL.YearEnd yEndTo = new YearEnd();
                    yEndTo.Where.UnitID.Value  = iuTo.ID;
                    yEndTo.Where.ItemID.Value  = itemID;
                    yEndTo.Where.StoreID.Value = storeID;
                    yEndTo.Query.Load();
                    if (yEndTo.RowCount > 0)
                    {
                        //We need to merge.
                        if (!yEndTo.IsColumnNull("EndingPrice"))
                        {
                            yEndTo.EndingPrice += yEndFrom.EndingPrice;
                        }
                        else
                        {
                            yEndTo.EndingPrice = yEndFrom.EndingPrice;
                        }
                        if (!yEndTo.IsColumnNull("EBalance"))
                        {
                            yEndTo.EBalance += yEndFrom.EBalance;
                        }
                        else
                        {
                            yEndTo.EBalance = yEndFrom.EBalance;
                        }
                        if (!yEndTo.IsColumnNull("PhysicalInventory"))
                        {
                            yEndTo.PhysicalInventory += yEndFrom.PhysicalInventory;
                        }
                        else
                        {
                            yEndTo.PhysicalInventory = yEndFrom.PhysicalInventory;
                        }
                        if (!yEndTo.IsColumnNull("PhysicalInventoryPrice"))
                        {
                            yEndTo.PhysicalInventoryPrice += yEndFrom.PhysicalInventoryPrice;
                        }
                        else
                        {
                            yEndTo.PhysicalInventoryPrice = yEndFrom.PhysicalInventoryPrice;
                        }

                        yEndTo.Save();
                        yEndFrom.MarkAsDeleted();
                        yEndFrom.Save();
                    }
                    else
                    {
                        yEndFrom.UnitID = iuTo.ID;
                        yEndFrom.Save();
                    }
                }
                transaction.CommitTransaction();
            }
            catch (Exception ex)
            {
                transaction.RollbackTransaction();
                throw ex;
            }
        }
        public void GenerateAutomaticInventoryByUnit()
        {
            var ethDate = new EthiopianDate.EthiopianDate();
            var stores = new Stores();
            stores.GetActiveStores();
            var rec = new ReceiveDoc();
            var itm = new Items();
            var yearEnd = new YearEnd();
            var balance = new Balance();

            if (!InventoryRequiredForHandlingUnit(false)) return;
            while (!stores.EOF) //This needs to be done for each store and for each item
            {
                //bereket
                //itm.ExcludeNeverReceivedItemsNoCategoryForHandlingUnit(stores.ID);
                itm.ExcludeNeverReceivedItemsNoCategoryForHandlingUnitOptimized(stores.ID, ethDate.Year);
                while (!itm.EOF) //For each item
                {
                    var receivedoc = rec.GetDistinctUnitIDFromReceivedDoc(itm.ID);
                    foreach (var dr in receivedoc.Rows.Cast<DataRow>().Where(dr =>!DoesBalanceExistByUnit(ethDate.Year, itm.ID, stores.ID, true,Convert.ToInt32(dr["UnitID"]))))
                    {
                        //need optimization
                        //we can exclude the already calculated items when we exclude never recieved items
                        yearEnd.LoadByItemIDStoreAndYearAndUnit(itm.ID, stores.ID, ethDate.Year, true, Convert.ToInt32(dr["UnitID"]));

                        if (yearEnd.RowCount > 0) continue;

                        yearEnd.AddNew();
                        yearEnd.ItemID = itm.ID;
                        yearEnd.StoreID = stores.ID;
                        yearEnd.Year = ethDate.Year;

                        //need optimization
                        //atleast we can get the value directly by filtering using storeid and itemid rather than
                        //selecting all items in all stores and filter it by code
                        //yearEnd.EBalance = balance.GetSOHByUnit(itm.ID, stores.ID, ethDate.Month, ethDate.Year, Convert.ToInt32(dr["UnitID"]));
                        yearEnd.EBalance = balance.GetSOHByUnitOptimized(itm.ID, stores.ID, ethDate.Month, ethDate.Year, Convert.ToInt32(dr["UnitID"]));
                        yearEnd.PhysicalInventory = yearEnd.EBalance;
                        yearEnd.AutomaticallyEntered = true;
                        yearEnd.UnitID = Convert.ToInt32(dr["UnitID"]);
                        yearEnd.Save();

                    }
                    itm.MoveNext();
                }
                stores.MoveNext();
            }
        }
        public void GenerateAutomaticInventory()
        {
            var ethDate = new EthiopianDate.EthiopianDate();
            if (InventoryRequired(false))
            {
                Stores stores = new Stores();
                stores.GetActiveStores();
                while (!stores.EOF) //This needs to be done for each store
                {
                  if (!DoesBalanceExist(ethDate.Year, stores.ID, true))
                        //If Inventory information hasn't already been filled for this store
                    {
                        var itm = new Items();

                      //bereket
                      //itm.ExcludeNeverReceivedItemsNoCategory(stores.ID);
                        itm.ExcludeNeverReceivedItemsNoCategoryOptimized(stores.ID, ethDate.Year);
                        while (!itm.EOF) //For each time
                        {
                            var yearEnd = new YearEnd();
                            var balance = new Balance();

                            yearEnd.LoadByItemIDStoreAndYear(itm.ID, stores.ID, ethDate.Year, true);

                            if (yearEnd.RowCount > 0)
                            {
                                return;
                            }

                            //YearEnd.PurgeAutomaticallyEnteredInventory(itm.ID, stores.ID, ethDate.Year);
                            yearEnd.AddNew();
                            yearEnd.ItemID = itm.ID;
                            yearEnd.StoreID = stores.ID;
                            yearEnd.Year = ethDate.Year;

                            //bereket
                            //yearEnd.EBalance = balance.GetSOH(itm.ID, stores.ID, 10, ethDate.Year);
                            yearEnd.EBalance = balance.GetSOHOptimized(itm.ID, stores.ID, 10, ethDate.Year);
                            yearEnd.PhysicalInventory = yearEnd.EBalance;
                            yearEnd.AutomaticallyEntered = true;
                            yearEnd.UnitID = 0;
                            yearEnd.Save();
                            itm.MoveNext();
                        }
                    }
                    stores.MoveNext();
                }

            }
        }
        private void btnDoEndOfYear_Click(object sender, EventArgs e)
        {
            var result = XtraMessageBox.Show("Are you sure you want to perform the year End activity automatically?",
                                             "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if(result==System.Windows.Forms.DialogResult.No)
            {
                return;
            }

            var confirm =
                XtraMessageBox.Show(
                    "Performing the year end automatically will make HCMIS take the present stock as the beginning balance of the next fiscal year.  You will not be able to use the Inventory page to input inventory data.  Do you want to proceed?",
                    "Confirmation", MessageBoxButtons.YesNo);
            if(confirm==DialogResult.No)
            {
                return;
            }

            btnDoEndOfYear.Enabled = false;
            int year = ConvertDate.GetEthiopianYear(DateTimeHelper.ServerDateTime);
            Activity stores = new Activity();
            stores.LoadAll();

            BLL.CommodityType commodityType = new BLL.CommodityType();
            commodityType.LoadAll();

            while (!commodityType.EOF)
            {
                while (!stores.EOF)
                {
                    DataTable dtbl = Balance.GetSohForAllItems(stores.ID, commodityType.ID, year, 10);
                    YearEnd ye = new YearEnd();
                    foreach (DataRow dr in dtbl.Rows)
                    {
                        if (dr["ID"] == DBNull.Value || dr["UnitID"] == DBNull.Value)
                            continue;

                        ye.AddNew();

                        ye.ItemID = Convert.ToInt32(dr["ID"]);
                        ye.UnitID = Convert.ToInt32(dr["UnitID"]);
                        ye.StoreID = stores.ID;
                        ye.Year = year;

                        ye.PhysicalInventory = ye.EBalance = Convert.ToInt32(dr["SOH"]);
                        ye.PhysicalInventoryPrice = ye.EndingPrice = Convert.ToDecimal(Convert.ToDouble(dr["Price"]));
                        ye.Save();
                    }
                    stores.MoveNext();
                }
                commodityType.MoveNext();
            }
            XtraMessageBox.Show("Year End has been populated");
            btnDoEndOfYear.Enabled = !BLL.YearEnd.IsPerformedForYear(EthiopianDate.EthiopianDate.Now.Year);
        }
        private void BtnAddBatchClick(object sender, EventArgs e)
        {
            if (!dxValidationProvider1.Validate()) return;
            var rec = new ReceiveDoc();
            var yEnd = new YearEnd();

            switch (VisibilitySetting.HandleUnits)
            {
                case 1:
                    {
                        rec.AddNew();
                        rec.ItemID = _itemid;
                        rec.BatchNo = txtBatchNo.Text;
                        rec.StoreID = _storeid;
                        rec.ExpDate = (DateTime) dateEditExpiryDate.EditValue;
                        rec.Date = _date;
                        rec.UnitID = 0;
                        rec.RefNo = txtRefNo.Text;
                        rec.Out = false;
                        rec.EurDate = DateTime.Now.Subtract(TimeSpan.FromDays(35));
                        rec.NoOfPack = Convert.ToInt32(txtPackQty.Text);
                        rec.QtyPerPack = Convert.ToInt32(txtQtyPerPack.Text);
                        rec.Quantity = (Convert.ToInt32(txtPackQty.Text))*(Convert.ToInt32(txtQtyPerPack.Text));
                        rec.QuantityLeft = (Convert.ToInt32(txtPackQty.Text))*(Convert.ToInt32(txtQtyPerPack.Text));
                        rec.Cost = Convert.ToDouble(txtPrice.Text);
                        rec.Save();

                        yEnd.AddNew();
                        yEnd.ItemID = _itemid;
                        yEnd.StoreID = _storeid;
                        yEnd.Year = _date.Year;
                        yEnd.AutomaticallyEntered = true;
                        yEnd.BBalance = 0;
                        yEnd.EBalance = 0;
                        yEnd.Save();

                    }
                    break;
                default:
                    {
                        rec.AddNew();
                        rec.ItemID = _itemid;
                        rec.BatchNo = txtBatchNo.Text;
                        rec.StoreID = _storeid;
                        rec.UnitID = _unitid;
                        rec.ExpDate = (DateTime) dateEditExpiryDate.EditValue;
                        rec.NoOfPack = Convert.ToInt32(txtPackQty.Text);
                        rec.Quantity = Convert.ToInt32(txtPackQty.Text);
                        rec.QuantityLeft = Convert.ToInt32(txtPackQty.Text);
                        rec.QtyPerPack = Convert.ToInt32(txtQtyPerPack.Text);
                        rec.Date = _date;
                        rec.RefNo = txtRefNo.Text;
                        rec.Out = false;
                        rec.EurDate = DateTime.Now.Subtract(TimeSpan.FromDays(35));
                        rec.Cost = Convert.ToDouble(txtPrice.Text);
                        rec.Save();

                        yEnd.AddNew();
                        yEnd.ItemID = _itemid;
                        yEnd.StoreID = _storeid;
                        yEnd.UnitID = _unitid;
                        yEnd.AutomaticallyEntered = true;
                        yEnd.BBalance = 0;
                        yEnd.EBalance = 0;
                        yEnd.Save();
                    }
                    break;
            }
            XtraMessageBox.Show("New Batch successfully added.", "Success");
            Close();
        }