public FarmerSummaryDetail GetFarmerSummary(QueryFarmerDetails farmerQuery) { var result = new FarmerSummaryDetail(); try { var farmerCode = farmerQuery.FarmerCode.Trim(); var farmerMobile = farmerQuery.Mobile; DateTime to; DateTime from; var farmer = _ctx.tblCommodityOwner.FirstOrDefault(x => x.Code == farmerCode); if (farmer == null) { result.StatusCode = (int) FarmerQueryStatusCode.CodeNotFound; result.StatusDetail = string.Format(@"Farmer with status code '{0}' not found", farmerCode); result.SummaryDetail = null; return result; } if (farmerMobile != farmer.PhoneNo || farmerMobile != farmer.OfficeNo) { result.StatusCode = (int)FarmerQueryStatusCode.NumberNotAuthenticated; result.StatusDetail = string.Format("The number used to Query does not belong to this farmer"); result.SummaryDetail = null; return result; } if (farmerQuery.Month!=string.Empty) { var month=DateTime.ParseExact(farmerQuery.Month, "MMMM", CultureInfo.CurrentCulture).Month; var daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, month); from = new DateTime(DateTime.Now.Year, month, 1); to = new DateTime(DateTime.Now.Year, month, daysInMonth); } else { from = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); to = DateTime.Now; } var farmerId = farmer.Id; var farmerCommPurchaseNotes = _ctx.tblSourcingDocument.Where( n => n.CommodityOwnerId == farmerId && n.DocumentTypeId == (int) DocumentType.CommodityPurchaseNote && n.DocumentStatusId == (int) DocumentSourcingStatus.Confirmed && n.DocumentDate >= from && n.DocumentDate <= to).OrderByDescending(p=>p.DocumentDate); var farmerLineItems = farmerCommPurchaseNotes.SelectMany(n => n.tblSourcingLineItem); var items = farmerLineItems.GroupBy(n => n.CommodityId) .Select( group => new FarmerSummary() { CummWeight = group.Sum(n => (n.Weight == null ? 0 : n.Weight.Value)), CommodityId = group.Key.Value }).ToList(); var dto = items.Select(Map).ToList(); var latestDocumentDate = farmerCommPurchaseNotes.Select(p => p.DocumentDate).FirstOrDefault(); var asAtDate = farmerCommPurchaseNotes.Count() >1 ? latestDocumentDate : to; result.FarmerCode = farmer.Code; result.FarmerName = string.Format("{0} {1} {2}", farmer.FirstName, farmer.LastName, farmer.Surname); result.AsAtDate = asAtDate.ToString("g"); result.StatusCode = (int) FarmerQueryStatusCode.Success; result.StatusDetail = "Succesful"; result.SummaryDetail = dto; return result; } catch (Exception ex) { result.StatusCode =(int) FarmerQueryStatusCode.Failure; result.StatusDetail = string.Format("Error {0}", ex.Message); result.SummaryDetail = null; return result; } }
public HttpResponseMessage GetFarmerSummaryDetails(QueryFarmerDetails farmerQuery) { var serviceType = farmerQuery.ServiceType; var summary = new FarmerSummaryDetail(); switch (serviceType) { case ServiceType.Cummulative: summary= _transactionsSummary.GetFarmerSummary(farmerQuery); break; case ServiceType.Other: break; } return Request.CreateResponse(HttpStatusCode.OK, summary); }