/// <summary> /// Gets the balance. /// </summary> /// <param name="hubID">The hub ID.</param> /// <param name="commodityId">The commodity id.</param> /// <param name="shippingInstructionID">The shipping instruction ID.</param> /// <returns></returns> public SIBalance GetBalance(int hubID, int commodityId, int shippingInstructionID) { SIBalance siBalance = new SIBalance(); siBalance.Commodity = _unitOfWork.CommodityRepository.FindById(commodityId).Name; siBalance.SINumberID = shippingInstructionID; ProjectCode projectCode = GetProjectCodeForSI(hubID, commodityId, shippingInstructionID); siBalance.ProjectCodeID = projectCode.ProjectCodeID; siBalance.Project = projectCode.Value; ShippingInstruction si = _unitOfWork.ShippingInstructionRepository.FindById(shippingInstructionID); var availableBalance = (from v in si.Transactions where v.LedgerID == Cats.Models.Ledger.Constants.GOODS_ON_HAND_UNCOMMITED && commodityId == v.ParentCommodityID select v.QuantityInMT).DefaultIfEmpty().Sum(); var firstOrDefaultans = si.Transactions.FirstOrDefault(); if (firstOrDefaultans != null) siBalance.Program = firstOrDefaultans.Program.Name; siBalance.AvailableBalance = availableBalance; siBalance.SINumber = si.Value; // convert the amount which is in Quintals to ... MT siBalance.CommitedToFDP = (from v in si.DispatchAllocations where v.IsClosed == false && v.CommodityID == commodityId select v.Amount / 10).DefaultIfEmpty().Sum(); var utilGetDispatchedAllocationFromSiResult = _unitOfWork.ReportRepository.util_GetDispatchedAllocationFromSI(hubID, shippingInstructionID).FirstOrDefault(); if (utilGetDispatchedAllocationFromSiResult != null) if (utilGetDispatchedAllocationFromSiResult.Quantity != null) siBalance.CommitedToFDP -= utilGetDispatchedAllocationFromSiResult.Quantity.Value; siBalance.CommitedToOthers = (from v in si.OtherDispatchAllocations where v.IsClosed == false && v.CommodityID == commodityId select v.QuantityInMT).DefaultIfEmpty().Sum(); decimal ReaminingExpectedReceipts = 0; var allocation = _unitOfWork.ReceiptAllocationRepository.FindBy(r => r.SINumber == siBalance.SINumber).ToList(); var rAll = allocation .Where( p => { if (p.Commodity.ParentID == null) return p.CommodityID == commodityId; else return p.Commodity.ParentID == commodityId; } ) .Where(q => q.IsClosed == false); foreach (var receiptAllocation in rAll) { ReaminingExpectedReceipts = ReaminingExpectedReceipts + (receiptAllocation.QuantityInMT - GetReceivedAlready(receiptAllocation)); } siBalance.ReaminingExpectedReceipts = ReaminingExpectedReceipts; decimal newVariable = (siBalance.CommitedToFDP + siBalance.CommitedToOthers); siBalance.Dispatchable = siBalance.AvailableBalance - newVariable + ReaminingExpectedReceipts; if (newVariable > 0) siBalance.TotalDispatchable = siBalance.AvailableBalance - (siBalance.CommitedToFDP + siBalance.CommitedToOthers); else siBalance.TotalDispatchable = siBalance.AvailableBalance; return siBalance; }
/// <summary> /// Gets the balance In Units for non-food items. /// </summary> /// <param name="hubID">The hub ID.</param> /// <param name="commodityId">The commodity id.</param> /// <param name="shippingInstructionID">The shipping instruction ID.</param> /// <returns></returns> public SIBalance GetBalanceInUnit(int hubID, int commodityId, int shippingInstructionID) { SIBalance siBalance = new SIBalance(); siBalance.Commodity = _unitOfWork.CommodityRepository.FindById(commodityId).Name; siBalance.SINumberID = shippingInstructionID; ProjectCode projectCode = GetProjectCodeForSI(hubID, commodityId, shippingInstructionID); siBalance.ProjectCodeID = projectCode.ProjectCodeID; siBalance.Project = projectCode.Value; ShippingInstruction si = _unitOfWork.ShippingInstructionRepository.FindById(shippingInstructionID); var availableBalance = (from v in si.Transactions where v.LedgerID == Cats.Models.Ledger.Constants.GOODS_ON_HAND_UNCOMMITED && commodityId == v.ParentCommodityID select v.QuantityInUnit).DefaultIfEmpty().Sum(); var firstOrDefaultans = si.Transactions.FirstOrDefault(); if (firstOrDefaultans != null) siBalance.Program = firstOrDefaultans.Program.Name; siBalance.AvailableBalance = availableBalance; siBalance.SINumber = si.Value; // convert the amount which is in Quintals to ... MT siBalance.CommitedToFDP = (from v in si.DispatchAllocations where v.IsClosed == false && v.CommodityID == commodityId select v.AmountInUnit).DefaultIfEmpty().Sum(); //select v.Amount / 10).DefaultIfEmpty().Sum(); var utilGetDispatchedAllocationFromSiResult = _unitOfWork.ReportRepository.util_GetDispatchedAllocationFromSI(hubID, shippingInstructionID).FirstOrDefault(); if (utilGetDispatchedAllocationFromSiResult != null) if (utilGetDispatchedAllocationFromSiResult.QuantityInUnit != null) siBalance.CommitedToFDP -= utilGetDispatchedAllocationFromSiResult.QuantityInUnit.Value; siBalance.CommitedToOthers = (from v in si.OtherDispatchAllocations where v.IsClosed == false && v.CommodityID == commodityId select v.QuantityInUnit).DefaultIfEmpty().Sum(); decimal ReaminingExpectedReceipts = 0; //TODO:After Implementing ReceiptAllocationService please return here //var rAll = repository.ReceiptAllocation.FindBySINumber(siBalance.SINumber) // .Where( // p => // { // if (p.Commodity.ParentID == null) // return p.CommodityID == commodityId; // else // return p.Commodity.ParentID == commodityId; // } // ) // .Where(q => q.IsClosed == false); //foreach (var receiptAllocation in rAll) //{ // decimal Qunt = 0; // if (receiptAllocation.QuantityInUnit != null) // Qunt = receiptAllocation.QuantityInUnit.Value; // ReaminingExpectedReceipts = ReaminingExpectedReceipts + // (Qunt // - // repository.ReceiptAllocation.GetReceivedAlreadyInUnit(receiptAllocation)); //} siBalance.ReaminingExpectedReceipts = ReaminingExpectedReceipts; siBalance.Dispatchable = siBalance.AvailableBalance - (siBalance.CommitedToFDP + siBalance.CommitedToOthers) + ReaminingExpectedReceipts; siBalance.TotalDispatchable = siBalance.AvailableBalance - (siBalance.CommitedToFDP + siBalance.CommitedToOthers); return siBalance; }
public ActionResult GetBalance(int? siNumber, int? commodityId, string siNumberText) { if (siNumber.HasValue && commodityId.HasValue) { UserProfile user = _userProfileService.GetUser(User.Identity.Name); List<SIBalance> si = (from v in _dispatchAllocationService.GetUncommitedSIBalance( UserProfile.DefaultHub.HubID, commodityId.Value,user.PreferedWeightMeasurment) select v).ToList(); SIBalance sis = new SIBalance(); if(siNumber.Value == 0 ) sis = si.FirstOrDefault(v1 => v1.SINumber == siNumberText); else sis = si.FirstOrDefault(v1 => v1.SINumberID == siNumber.Value); decimal balance = sis.Dispatchable;// +ReaminingExpectedReceipts; return Json(balance, JsonRequestBehavior.AllowGet); } return Json(new EmptyResult()); }