Ejemplo n.º 1
0
        public Task <List <GenerateBillResult> > GenerateBill(long societyId, DateTime generationDate)
        {
            var taskresult = Task.Run(async() =>
            {
                using (var context = new DbContext())
                {
                    var result = new List <GenerateBillResult>();

                    var flats = (from msf in context.MapsSocietiesToFacilities
                                 join fl in context.Floors on msf.FacilityId equals fl.FacilityId
                                 join ft in context.Flats on fl.Id equals ft.FloorId
                                 where msf.SocietyId == societyId
                                 select new
                    {
                        flat = ft,
                        society = msf.Society
                    })
                                .ToList();

                    foreach (var item in flats)
                    {
                        //check if bill is already generated for the flat
                        if (context.Bills.Any(b => b.BillType == (byte)EnBillType.Monthly && b.Month == generationDate.Month && b.Year == generationDate.Year && b.FlatId == item.flat.Id))
                        {
                            continue;
                        }

                        var flatowner = await _flatOwnerRepository.CurrentOwner(item.flat.Id);
                        if (flatowner != null && flatowner.IsApproved)
                        {
                            var mclines      = await _maintenanceCostRepository.GetCostLineForBillGeneration(item.flat.Id, societyId, generationDate);
                            var billinglines = new List <BillingLine>();

                            foreach (var mc in mclines)
                            {
                                var bline        = new BillingLine();
                                bline.Definition = mc.Definition.Name;

                                if (mc.Definition.For2Wheeler)
                                {
                                    var total2Wheeler = item.flat.Vehicles.Count(f => f.Type == (byte)EnParkingType.TwoWheeler);
                                    if (total2Wheeler > 0)
                                    {
                                        var fromSecond = item.society.Second2Wheeler;

                                        bline.Amount      = !fromSecond.HasValue ? total2Wheeler * mc.Amount : mc.Amount + ((total2Wheeler - 1) * fromSecond.Value);
                                        bline.BaseAmount  = mc.Amount;
                                        bline.OtherAmount = fromSecond;
                                        bline.OnArea      = mc.Definition.CalculationOnPerSftArea ? item.flat.TotalArea : new Nullable <int>();
                                        billinglines.Add(bline);
                                    }
                                }
                                else if (mc.Definition.For4Wheeler)
                                {
                                    var total4Wheeler = item.flat.Vehicles.Count(f => f.Type == (byte)EnParkingType.FourWheeler);
                                    if (total4Wheeler > 0)
                                    {
                                        var fromSecond = item.society.Second4Wheeler;

                                        bline.Amount      = !fromSecond.HasValue ? total4Wheeler * mc.Amount : mc.Amount + ((total4Wheeler - 1) * fromSecond.Value);
                                        bline.BaseAmount  = mc.Amount;
                                        bline.OtherAmount = fromSecond;
                                        bline.OnArea      = mc.Definition.CalculationOnPerSftArea ? item.flat.TotalArea : new Nullable <int>();
                                        billinglines.Add(bline);
                                    }
                                }
                                else
                                {
                                    bline.Amount     = mc.Amount * (mc.Definition.CalculationOnPerSftArea ? item.flat.TotalArea : 1);
                                    bline.BaseAmount = mc.Amount;
                                    bline.OnArea     = mc.Definition.CalculationOnPerSftArea ? item.flat.TotalArea : new Nullable <int>();
                                    billinglines.Add(bline);
                                }
                            }

                            //get pending amount
                            decimal pendingAmount = await GetPendingAmountForFlat(item.flat.Id);
                            var latest            = await LatestBill(item.flat.Id);

                            if (pendingAmount != 0)
                            {
                                var arrears        = new BillingLine();
                                arrears.Definition = "Arrears";
                                arrears.Amount     = pendingAmount;
                                arrears.BaseAmount = pendingAmount;
                                arrears.OnArea     = null;

                                billinglines.Add(arrears);
                            }

                            //calculate interest
                            if (latest != null &&
                                ((latest.BillType == (byte)EnBillType.Opening && item.society.OpeningInterest) || latest.BillType == (byte)EnBillType.Monthly) &&
                                item.society.InterestPercent.HasValue &&
                                pendingAmount > 0)
                            {
                                decimal amt = Math.Round(pendingAmount * (item.society.InterestPercent.Value / 100), 2);

                                var bline        = new BillingLine();
                                bline.Definition = "Interest";
                                bline.Amount     = amt;
                                bline.BaseAmount = amt;
                                bline.OnArea     = null;

                                billinglines.Add(bline);
                            }

                            var max            = context.Bills.Count(b => b.SocietyId == societyId) > 0 ? context.Bills.Where(b => b.SocietyId == societyId).Max(b => b.ReceiptNo) : 0;
                            var bill           = new Bill();
                            bill.Name          = flatowner.Name;
                            bill.ReceiptNo     = max + 1;
                            bill.Date          = generationDate;
                            bill.Amount        = billinglines.Sum(b => b.Amount);
                            bill.Month         = bill.Date.Month;
                            bill.Year          = bill.Date.Year;
                            bill.FlatId        = item.flat.Id;
                            bill.SocietyId     = societyId;
                            bill.BillingLines  = billinglines;
                            bill.Pending       = pendingAmount;
                            bill.MonthlyAmount = bill.Amount - bill.Pending;
                            bill.BillType      = (byte)EnBillType.Monthly;

                            //add new bill
                            context.Bills.Add(bill);
                            context.SaveChanges();

                            var reply = new GenerateBillResult();

                            reply.Name   = flatowner.Name;
                            reply.Email  = flatowner.EmailId;
                            reply.Amount = bill.Amount;
                            reply.Month  = bill.Month;
                            reply.Year   = bill.Year;

                            result.Add(reply);
                        }
                    }
                    return(result);
                }
            });

            return(taskresult);
        }
Ejemplo n.º 2
0
        public Task <List <UploadReply> > UploadOpeningBills(List <UploadBillDetail> bills, long facilityId, long societyId, DateTime generatedDate)
        {
            var taskResult = Task.Run(async() =>
            {
                using (var context = new DbContext())
                {
                    var result = new List <UploadReply>();

                    foreach (var bill in bills)
                    {
                        var reply = new UploadReply();
                        reply.Id  = bill.SrNo;

                        var sb = new StringBuilder();

                        if (string.IsNullOrEmpty(bill.FlatName))
                        {
                            sb.Append(",FlatName");
                        }

                        if (bill.Amount == 0)
                        {
                            sb.Append(",Amount");
                        }

                        if (sb.Length > 0)
                        {
                            reply.IsSuccess = false;
                            reply.Message   = sb.ToString().Trim(',');
                            result.Add(reply);
                            continue;
                        }

                        var flat = context
                                   .Flats
                                   .FirstOrDefault(f => f.Name.Equals(bill.FlatName, StringComparison.InvariantCultureIgnoreCase) && f.Floor.FacilityId == facilityId);

                        if (flat == null)
                        {
                            reply.IsSuccess = false;
                            reply.Message   = "Flat not found";
                            result.Add(reply);
                            continue;
                        }

                        var flatowner = await _flatOwnerRepository.CurrentOwner(flat.Id);
                        if (flatowner == null)
                        {
                            reply.IsSuccess = false;
                            reply.Message   = "Flat owner not found";
                            result.Add(reply);
                            continue;
                        }
                        //---------------------------------------------------------------------------------//

                        var billinglines = new List <BillingLine>();

                        var opening        = new BillingLine();
                        opening.Definition = "Opening";
                        opening.Amount     = bill.Amount;
                        opening.BaseAmount = bill.Amount;
                        opening.OnArea     = null;

                        billinglines.Add(opening);


                        //get pending amount
                        decimal pendingAmount = await GetPendingAmountForFlat(flat.Id);

                        if (pendingAmount != 0)
                        {
                            var arrears        = new BillingLine();
                            arrears.Definition = "Arrears";
                            arrears.Amount     = pendingAmount;
                            arrears.BaseAmount = pendingAmount;
                            arrears.OnArea     = null;

                            billinglines.Add(arrears);
                        }


                        //---------------------------------------------------------------------------------//

                        var max = context.Bills.Count(b => b.SocietyId == societyId) > 0 ?
                                  context.Bills.Where(b => b.SocietyId == societyId).Max(b => b.ReceiptNo) :
                                  0;

                        var item           = new Bill();
                        item.Name          = flatowner.Name;
                        item.ReceiptNo     = max + 1;
                        item.Date          = generatedDate;
                        item.Amount        = billinglines.Sum(b => b.Amount);
                        item.Month         = generatedDate.Month;
                        item.Year          = generatedDate.Year;
                        item.FlatId        = flat.Id;
                        item.SocietyId     = societyId;
                        item.BillingLines  = billinglines;
                        item.Pending       = pendingAmount;
                        item.MonthlyAmount = item.Amount - item.Pending;
                        item.BillType      = (byte)EnBillType.Opening;
                        item.Note          = bill.Note;

                        //add bill
                        context.Bills.Add(item);
                        context.SaveChanges();

                        reply.IsSuccess = true;
                        reply.Message   = "Done";
                        result.Add(reply);
                    }
                    return(result);
                }
            });

            return(taskResult);
        }