public static DataView SOHForFinance(int activityId, int periodId)
 {
     var query = HCMIS.Repository.Queries.Inventory.SelectSOHForFinance(activityId, periodId);
     BLL.Inventory inventory = new Inventory();
     inventory.FlushData();
     inventory.LoadFromRawSql(query);
     return inventory.DefaultView;
 }
 public static DataView GetOverageShortageReport(int activityId, int warehouseId, string periodIds)
 {
     var query = HCMIS.Repository.Queries.Inventory.SelectGetOverageShortageReport(activityId, warehouseId, periodIds);
     BLL.Inventory inventory = new Inventory();
     inventory.FlushData();
     inventory.LoadFromRawSql(query);
     return inventory.DefaultView;
 }
 private void btnSave_Click(object sender, EventArgs e)
 {
     if (btnSave.Text == "Update")
     {
         var inventory = new Inventory();
         inventory.LoadByPrimaryKey(Convert.ToInt32(_dr["ID"]));
         inventory.UnitID = Convert.ToInt32(lkUnit.EditValue);
         inventory.ManufacturerID = Convert.ToInt32(lkManufacturer.EditValue);
         inventory.ExpiryDate = Convert.ToDateTime(dtExpiryDate.EditValue);
         inventory.BatchNo = txtBatchNo.Text;
         inventory.InventorySoundQuantity = Convert.ToDecimal(txtSoundQty.EditValue);
         inventory.InventoryDamagedQuantity = Convert.ToDecimal(txtDamagedQty.EditValue);
         inventory.InventoryExpiredQuantity = Convert.ToDecimal(txtExpiredQty.EditValue);
         inventory.Save();
         this.Close();
     }
     else
     {
         if (dxValidationProvider1.Validate() && validateBatchandExpiryDate() && validateExpiryQuantity() &&
             ValidateQuantity())
         {
             var inventory = new Inventory();
             inventory.AddNew();
             inventory.InventoryPeriodID = _inventoryPeriodID;
             inventory.PhysicalStoreID = _physicalStoreID;
             inventory.ActivityID = _activityID;
             inventory.ItemID = Convert.ToInt32(lkItem.EditValue);
             inventory.UnitID = Convert.ToInt32(lkUnit.EditValue);
             inventory.ManufacturerID = Convert.ToInt32(lkManufacturer.EditValue);
             if (txtBatchNo.EditValue != null)
             {
                 inventory.BatchNo = txtBatchNo.EditValue.ToString();
             }
             if (dtExpiryDate.EditValue != null)
             {
                 inventory.ExpiryDate = Convert.ToDateTime(dtExpiryDate.EditValue);
             }
             inventory.InventorySoundQuantity = Convert.ToDecimal(txtSoundQty.EditValue);
             inventory.InventoryDamagedQuantity = Convert.ToDecimal(txtDamagedQty.EditValue);
             inventory.InventoryExpiredQuantity = Convert.ToDecimal(txtExpiredQty.EditValue);
             inventory.RecordedDate = DateTimeHelper.ServerDateTime;
             inventory.RecordedBy = CurrentContext.UserId;
             inventory.IsDraft = true;
             inventory.Remarks = "New Entry";
             inventory.Save();
             if (Duplicate.CheckState == CheckState.Checked)
             {
                 ClearValues();
             }
             else
             {
                 this.Close();
             }
         }
     }
 }
        public static void CommitInventory(int periodId, int activityId, int physicalStoreId,DateTime ethiopianDate, int userId,BackgroundWorker backgroundWorker)
        {
            Inventory inventory = new Inventory();

            if (!InventoryPeriod.HasUnCommited(periodId, activityId))
            {
                throw new Exception("This inventory has been commited already,you are not allow to commit again.");
            }
            if(InventoryPeriod.HasInCompleteReceives(activityId,physicalStoreId))
            {
                throw new Exception("There are incompleted receives,you can only processed after canceling the receives.");
            }
            inventory.LoadByStoreAndActivity(activityId, physicalStoreId, periodId);

            try
            {
                var physicalStore = new PhysicalStore();
                physicalStore.LoadByPrimaryKey(physicalStoreId);
                inventory.InitializeCommit(userId, physicalStore.PhysicalStoreTypeID);

                inventory.Rewind();
                int count = 0;
                while (!inventory.EOF)
                {

                    string itemDetail = inventory.GetColumn("FullItemName")+" - " +inventory.GetColumn("ManufacturerName")
                        + " - " + inventory.GetColumn("Unit") + " - " + inventory.GetColumn("ExpiryDate")
                        + " - " + inventory.GetColumn("BatchNo");

                    if (inventory.IsColumnNull("IsDraft") || inventory.IsDraft)
                    {
                        inventory.Commit(ethiopianDate,backgroundWorker);
                    }

                    inventory.MoveNext();
                    count++;
                    backgroundWorker.ReportProgress(count,itemDetail);
                }

                inventory.FinishCommit();
            }
            catch (Exception exception)
            {
                inventory.CancelCommit(exception);
            }
        }
        public static void CommitSingle(int inventoryId,DateTime ethiopianDate,int userId)
        {
            Inventory inventory = new Inventory();
            inventory.LoadByPrimaryKey(inventoryId);
            if(!inventory.IsColumnNull("isDraft") && !inventory.IsDraft)
            {
                  throw new Exception("This inventory has been commited already,you are not allow to commit again.");

            }

            try
            {
                var physicalStore = new PhysicalStore();
                physicalStore.LoadByPrimaryKey(inventory.PhysicalStoreID);
                inventory.InitializeCommit(userId,physicalStore.PhysicalStoreTypeID);
                inventory.Commit(ethiopianDate);
                inventory.FinishCommit();
            }
            catch (Exception exp)
            {
                inventory.CancelCommit(exp);
            }
        }
 internal void LoadForInventory(Inventory inventory)
 {
     DateTime? expiryDate = null;
     if(!inventory.IsColumnNull("ExpiryDate"))
     {
         expiryDate = inventory.ExpiryDate;
     }
     string batchNo = "";
     if(!inventory.IsColumnNull("BatchNo"))
     {
         batchNo = inventory.BatchNo;
     }
     string query =
         HCMIS.Repository.Queries.ReceivePallet.SelectLoadForInventory(batchNo, expiryDate, inventory.ItemID, inventory.UnitID, inventory.ManufacturerID,
                                inventory.ActivityID, inventory.PhysicalStoreID);
     this.LoadFromRawSql(query);
 }
        public static void SaveInventoryRow(DataRow dataRow)
        {
            if (dataRow["ID"] != DBNull.Value)
            {
                Inventory inv = new Inventory();
                inv.LoadByPrimaryKey(Convert.ToInt32(dataRow["ID"]));

                inv.SetColumn("InventoryDamagedQuantity", dataRow["InventoryDamagedQuantity"]);
                inv.SetColumn("InventorySoundQuantity", dataRow["InventorySoundQuantity"]);
                inv.SetColumn("InventoryExpiredQuantity", dataRow["InventoryExpiredQuantity"]);
                inv.SetColumn("Remarks", dataRow["Remarks"]);
                if (dataRow["PalletLocationID"] != DBNull.Value)
                {
                    inv.PalletLocationID = Convert.ToInt32(dataRow["PalletLocationID"]);
                }
                if (dataRow["DamagedPalletLocationID"] != DBNull.Value)
                {
                    inv.DamagedPalletLocationID = Convert.ToInt32(dataRow["DamagedPalletLocationID"]);
                }
                inv.IsDraft = true;
                inv.Save();
            }
            else
            {
                Inventory inv = new Inventory();
                inv.AddNew();
                inv.InventoryPeriodID = Convert.ToInt32(dataRow["InventoryPeriodID"]);
                inv.PhysicalStoreID = Convert.ToInt32(dataRow["PhysicalStoreID"]);
                inv.ActivityID = Convert.ToInt32(dataRow["ActivityID"]);
                inv.ItemID = Convert.ToInt32(dataRow["ItemID"]);
                inv.UnitID = Convert.ToInt32(dataRow["UnitID"]);
                inv.ManufacturerID = Convert.ToInt32(dataRow["ManufacturerID"]);
                inv.SetColumn("BatchNo", dataRow["BatchNo"]);
                inv.SetColumn("ExpiryDate", dataRow["ExpiryDate"]);
                inv.SetColumn("InventoryDamagedQuantity", dataRow["InventoryDamagedQuantity"]);
                inv.SetColumn("InventorySoundQuantity", dataRow["InventorySoundQuantity"]);
                inv.SetColumn("InventoryExpiredQuantity", dataRow["InventoryExpiredQuantity"]);
                inv.RecordedDate = DateTimeHelper.ServerDateTime;
                inv.RecordedBy = CurrentContext.UserId;
                inv.IsDraft = true;
                inv.Remarks = "Stock out";
                inv.Save();
            }
        }
        private void startbgWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            MyGeneration.dOOdads.TransactionMgr transactionMgr = MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();

            try
            {
                // Add the inventory details.
                // For each Activity, populate the inventory.
                //ToDO: this grid reading in a non ui thread is dangerous
                //please don't do it this way.
                transactionMgr.BeginTransaction();
                int physicalStoreID = Convert.ToInt32(gridManageInvetoryView.GetFocusedDataRow()["ID"]);
                PhysicalStore physicalStore = new PhysicalStore();
                physicalStore.LoadByPrimaryKey(physicalStoreID);

                DateTime startDate = dtStartInventory.Value;
                if (!physicalStore.IsColumnNull("CurrentInventoryPeriodID"))
                {
                // create the inventory period
                InventoryPeriod oldPeriod = new InventoryPeriod();
                oldPeriod.LoadByPrimaryKey(physicalStore.CurrentInventoryPeriodID);
                oldPeriod.EndDate = dtStartInventory.Value;
                oldPeriod.Save();
                }
                InventoryPeriod period = new InventoryPeriod();
                period.AddNew();
                period.InventoryStatusID = InventoryPeriod.Constants.BEGIN_INVENTORY;
                period.PhysicalStoreID = physicalStoreID;
                period.StartDate = dtStartInventory.Value;
                period.EndDate = FiscalYear.Current.EndDate;
                period.StartedBy = CurrentContext.UserId;
                period.FiscalYearID = FiscalYear.Current.ID;
                if (memoEdit1.EditValue != null)
                {
                    period.Remark = memoEdit1.EditValue.ToString();
                }

                period.Save();
                //ChangePhysicalStoreToCurrentPeriod

                physicalStore = new PhysicalStore();
                physicalStore.LoadByPrimaryKey(physicalStoreID);
                physicalStore.CurrentInventoryPeriodID = period.ID;
                physicalStore.CurrentPeriodStartDate = period.StartDate;
                physicalStore.Save();

                Activity activity = new Activity();
                activity.LoadAll();
                int activityIndex = 1;
                while (!activity.EOF)
                {
                    // report that this activity is being processed.
                    startbgWorker.ReportProgress(activityIndex++, "Activity: " + activity.FullActivityName);

                    DataTable dtbl = Balance.GetBalanceByPhysicalStore(physicalStoreID, activity.ID, false);
                    decimal total = dtbl.Rows.Count;
                    decimal i = 0;
                    foreach (DataRow dr in dtbl.Rows)
                    {

                        Inventory inv = new Inventory();
                        inv.AddNew();

                        inv.IsDraft = true;
                        inv.PhysicalStoreID = physicalStoreID;
                        inv.RecordedBy = CurrentContext.UserId;
                        inv.RecordedDate = BLL.DateTimeHelper.ServerDateTime;

                        inv.InventoryPeriodID = period.ID;
                        inv.ItemID = Convert.ToInt32(dr["ID"]);
                        inv.UnitID = Convert.ToInt32(dr["UnitID"]);
                        inv.ActivityID = activity.ID;
                        inv.ManufacturerID = Convert.ToInt32(dr["ManufacturerID"]);
                        inv.SetColumn("BatchNo", dr["BatchNo"]);
                        inv.SetColumn("ExpiryDate", dr["ExpDate"]);
                        if (!inv.IsColumnNull("ExpiryDate") && inv.ExpiryDate < BLL.DateTimeHelper.ServerDateTime)
                        {
                            inv.SystemExpiredQuantity = Convert.ToDecimal(dr["SoundSOH"]);
                        }
                        else
                        {
                            inv.SystemSoundQuantity = Convert.ToDecimal(dr["SoundSOH"]);
                        }

                        inv.SystemDamagedQuantity = Convert.ToDecimal(dr["DamagedSOH"]);
                        inv.SetColumn("Cost", dr["Cost"]);
                        inv.Margin = dr["Margin"] == DBNull.Value ? 0 : Convert.ToDecimal(dr["Margin"]);
                        inv.SetColumn("PalletLocationID", dr["PalletLocationID"]);
                        inv.Save();

                        startbgWorker.ReportProgress(Convert.ToInt32((i / total) * 100), "Detail");
                        //inventory
                        i++;
                    }

                    activity.MoveNext();
                }
                transactionMgr.CommitTransaction();
                XtraMessageBox.Show("The new Inventory Period has been defined.");
            }
            catch (Exception exception)
            {
                transactionMgr.RollbackTransaction();
                XtraMessageBox.Show(exception.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnCommit_Click(object sender, EventArgs e)
        {
            if (!ValidateBeforeCommit(false)) return;
            if (gridLocationRelatedInventory.DataSource != null)
            {
                if ((gridLocationRelatedInventory.DataSource as DataTable).GetChanges() != null)
                {
                    XtraMessageBox.Show("Some Change have not been save,please save before commiting",
                        "Pending Changes...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (XtraMessageBox.Show("Are you sure you want to commit this change? You will not be able to undo this.",
                        "Confirmation...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }

                int physicalStoreID = Convert.ToInt32(lkInventoryStore.EditValue);
                int periodId = Convert.ToInt32(lkPeriod.EditValue);
                int activityID = Convert.ToInt32(lkInventoryAccount.EditValue);

                Inventory inventory = new Inventory();
                inventory.LoadByStoreAndActivity(activityID, physicalStoreID, periodId);
                InitializeProgressBar();

                lblDescription.Text =
                   string.Format(
                       "HCMIS is commiting inventory on <b>{0}-{1}</b>. Please do not close this window until this operation is complete.",
                       lkInventoryStore.Text, lkInventoryAccount.Text);

                labelTotalActivity.Text =
                       string.Format("HCMIS is processing Inventory for  <b>{0}</b> Entries.", inventory.RowCount);

                progressBarActivities.Properties.Maximum = inventory.RowCount;
                InventoryBgWorker.RunWorkerAsync();
            }
            else
            {
                XtraMessageBox.Show(
                        "No Record is available to commit,please Select a Warehouse and Activity to Continue",
                        "No Record...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }