public void RemoveUnPaidPurchaseExpenses(string scaleId)
 {
     // If TEmpEntityList has no Items.
     if (TempEntityList != null)
     {
         int intScaleId = Convert.ToInt32(scaleId);
         if (intScaleId > 0)
         {
             // Get all scale exepenses.
             IEnumerable <ExpensesRequest> scaleExps = from exp in TempEntityList
                                                       where exp.Reference_Table == "Scale" && exp.Reference_ID == intScaleId
                                                       select exp;
             if (scaleExps != null)
             {
                 IList <ExpensesRequest> removeExpenses = new List <ExpensesRequest>();
                 ScaleLibrary            scaleLib       = new ScaleLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
                 foreach (var item in scaleExps)
                 {
                     // Get scale entity.
                     Scale scale = scaleLib.GetByID(item.Reference_ID.ToString(), new string[] { "Dispatch_Request_No" });
                     if (scale != null && scale.Dispatch_Request_No != null && scale.Dispatch_Request_No.ID > 0)
                     {
                         // Get dispatcher id.
                         int dispatcherId = scale.Dispatch_Request_No.ID;
                         // Get all dispatcher expenses.
                         IEnumerable <ExpensesRequest> dispatcherExps = from exp in TempEntityList
                                                                        where exp.Reference_Table == "DispatcherRequest" && exp.Reference_ID == dispatcherId
                                                                        select exp;
                         if (dispatcherExps != null)
                         {
                             foreach (var dispExp in dispatcherExps)
                             {
                                 // Add dispatcher expense in removeExpense list.
                                 removeExpenses.Add(dispExp);
                             }
                         }
                     }
                     // Add scale expense in removeExpense list.
                     removeExpenses.Add(item);
                 }
                 // Delete scale and dispatcher expense.
                 foreach (var removeExp in removeExpenses)
                 {
                     TempEntityList.Remove(removeExp);
                 }
             }
         }
     }
 }
        public override ActionResult _Delete(string id, GridCommand command, string MasterID = null, bool isNew = false)
        {
            string filePath;
            string fileName;

            try {
                if (isNew)
                {
                    //TODO: Delete entity with id
                    TEntity entity = TempEntityList.SingleOrDefault(m => m.ID == int.Parse(id));
                    filePath = _fileHelper.GetTempSourceDirByFileRefId(entity.Document_RefId.ToString());// Path.Combine(Configuration.GetsmARTTempDocPath(), entity.Document_RefId.ToString());
                    fileName = entity.Document_Name;
                    TempEntityList.Remove(entity);
                }
                else
                {
                    TEntity entity = Library.GetByID(id);

                    filePath = _fileHelper.GetSourceDirByFileRefId(entity.Document_RefId.ToString());
                    fileName = entity.Document_Name;

                    Library.Delete(id);
                }

                _fileHelper.RemoveFile(fileName, filePath);
            }
            catch (Exception ex) {
                ModelState.AddModelError("Error", ex.Message);
            }
            if (string.IsNullOrEmpty(MasterID))
            {
                return(Display(command, isNew));
            }
            else
            {
                return(Display(command, MasterID, isNew));
            }
        }
        public ActionResult _Index(GridCommand command, int id)
        {
            int totalRows = 0;

            //Delete entity from TempEntityList if exits.
            if (TempEntityList != null && TempEntityList.Count > 0)
            {
                IList <SettlementDetails> settlementDetails = (from data in TempEntityList
                                                               where data.Scale_Details_ID.Scale.ID == id
                                                               select data as SettlementDetails).ToList <SettlementDetails>();

                foreach (var item in settlementDetails)
                {
                    TempEntityList.Remove(item);
                }
            }


            //Get all unsettled scale details tickets
            string dbContextConnectionString        = ConfigurationHelper.GetsmARTDBContextConnectionString();
            ScaleDetailsLibrary scaleDetailsLibrary = new smART.Library.ScaleDetailsLibrary();

            scaleDetailsLibrary.Initialize(dbContextConnectionString);
            IEnumerable <ScaleDetails> scaleDetailsList = scaleDetailsLibrary.GetAllByPagingByParentID
                                                              (out totalRows,
                                                              int.Parse(id.ToString()),
                                                              1,
                                                              20,
                                                              "",
                                                              "Asc",
                                                              new string[] { "Scale", "Scale.Purchase_Order", "Item_Received", "Apply_To_Item" }
                                                              );


            // Create temp settlement details collection by scale details tickets
            int iValue = 1;
            PurchaseOrderItemLibrary poItemLibrary = new smART.Library.PurchaseOrderItemLibrary();

            poItemLibrary.Initialize(dbContextConnectionString);

            foreach (var item in scaleDetailsList)
            {
                SettlementDetails settlementDetails = new SettlementDetails();
                settlementDetails.ID = iValue;
                settlementDetails.Scale_Details_ID  = item;
                settlementDetails.Actual_Net_Weight = settlementDetails.Scale_Details_ID.NetWeight;

                //Get rate from PO Item
                if (item.Scale.Purchase_Order != null)
                {
                    PurchaseOrderItem poItem = poItemLibrary.GetPOItemByItemCode(item.Scale.Purchase_Order.ID, item.Item_Received.ID, null);
                    // If po item exits then update rate and amount.
                    if (poItem != null)
                    {
                        settlementDetails.Rate     = poItem.Price;
                        settlementDetails.Item_UOM = poItem.Ordered_Qty_UOM;

                        // If po item unit is not lbs.
                        if (!string.IsNullOrWhiteSpace(settlementDetails.Item_UOM) && settlementDetails.Item_UOM.ToLower() != "lbs")
                        {
                            UOMConversionLibrary uomConvLib = new UOMConversionLibrary();
                            uomConvLib.Initialize(dbContextConnectionString);
                            UOMConversion uomConv = uomConvLib.GetByUOM(poItem.Ordered_Qty_UOM, "LBS");
                            // When convertion factor not exits.
                            if (uomConv != null)
                            {
                                settlementDetails.Item_UOM_Conv_Fact = (decimal)uomConv.Factor;
                                settlementDetails.Item_UOM_NetWeight = settlementDetails.Actual_Net_Weight / (decimal)uomConv.Factor;
                                settlementDetails.Item_UOM_NetWeight = decimal.Round(settlementDetails.Item_UOM_NetWeight, 3, MidpointRounding.AwayFromZero);
                            }
                            else
                            {
                                settlementDetails.Item_UOM_Conv_Fact = 1;
                                settlementDetails.Item_UOM_NetWeight = settlementDetails.Actual_Net_Weight;
                            }
                        }
                        // When Unit is default. Set default unit.
                        else
                        {
                            SetDefaultUnit(settlementDetails);
                        }
                        settlementDetails.Amount = settlementDetails.Item_UOM_NetWeight * poItem.Price;
                        settlementDetails.Amount = decimal.Round(settlementDetails.Amount, 2, MidpointRounding.AwayFromZero);
                    }
                    else
                    {
                        // When PO Item not exists.Set default unit.
                        SetDefaultUnit(settlementDetails);
                    }
                }
                // When PO not exists.Set default unit.
                else
                {
                    SetDefaultUnit(settlementDetails);
                }
                settlementDetails.Settlement_ID = new Settlement();
                settlementDetails.Price_List_ID = new PriceList();

                TempEntityList.Add(settlementDetails);
                iValue++;
            }

            return(Display(command, id.ToString(), true));
        }