public JsonResult Save(BillingInfoPerMonth model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new { info = "Failed", status = false }, JsonRequestBehavior.AllowGet));
            }

            if (_floorRentInfoManager.SaveOrUpdate(model))
            {
                return(Json(new { info = "Saved", status = true }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new { info = "Not Saved", status = false }, JsonRequestBehavior.AllowGet));
        }
        public bool Saved(BillingReceivedInfoInput model)
        {
            decimal rentBill          = 0;
            decimal commonBill        = 0;
            decimal electricityBill   = 0;
            decimal emElectricityBill = 0;
            decimal wasaBill          = 0;

            BillingInfoPerMonth        billingInfo        = new BillingInfoPerMonth();
            BillingInfoPerMonthManager billingInfoManager = new BillingInfoPerMonthManager();

            for (var i = 0; i < model.Amount.Length; i++)
            {
                if (model.Amount[i] < 1)
                {
                    continue;
                }
                var entity = new BillingReceivedInfo()
                {
                    TenantInfoId = model.TenantInfoId,
                    Date         = model.Date,
                    CreatedOn    = new LoginInfo().CurrentTime,
                    CreatedBy    = Log.UserId.ToString(),
                    IsActive     = true,
                    Amount       = model.Amount[i],
                    Note         = model.Note[i],
                    Purpose      = model.Purpose[i]
                };

                if (i == 0)
                {
                    rentBill = model.Amount[i];
                }
                else if (i == 1)
                {
                    commonBill = model.Amount[i];
                }
                else if (i == 2)
                {
                    electricityBill = model.Amount[i];
                }
                else if (i == 3)
                {
                    emElectricityBill = model.Amount[i];
                }
                else
                {
                    wasaBill = model.Amount[i];
                }

                Repository.SaveOrUpdate(entity);
            }

            billingInfo.Month        = Convert.ToString(model.Date.Month);
            billingInfo.Year         = Convert.ToString(model.Date.Year);
            billingInfo.TenantInfoId = model.TenantInfoId;

            var dateYearExist = billingInfoManager.GetAll().Where(x => x.Month == billingInfo.Month && x.Year == billingInfo.Year && x.TenantInfoId == billingInfo.TenantInfoId && x.IsActive).LastOrDefault();
            var exist         = billingInfoManager.GetAll().Where(x => x.TenantInfoId == billingInfo.TenantInfoId && x.IsActive).LastOrDefault();

            if (dateYearExist == null)
            {
                billingInfo.RentBill          = rentBill;
                billingInfo.CommonBill        = commonBill;
                billingInfo.ElectricityBill   = electricityBill;
                billingInfo.EmElectricityBill = emElectricityBill;
                billingInfo.WasaBill          = wasaBill;
            }
            else
            {
                billingInfo.Id                = dateYearExist.Id;
                billingInfo.RentBill          = dateYearExist.RentBill + rentBill;
                billingInfo.CommonBill        = dateYearExist.CommonBill + commonBill;
                billingInfo.ElectricityBill   = dateYearExist.ElectricityBill + electricityBill;
                billingInfo.EmElectricityBill = dateYearExist.EmElectricityBill + emElectricityBill;
                billingInfo.WasaBill          = dateYearExist.WasaBill + wasaBill;

                billingInfo.CurrentDuesRent          = dateYearExist.CurrentDuesRent;
                billingInfo.CurrentDuesCommon        = dateYearExist.CurrentDuesCommon;
                billingInfo.CurrentDuesElectricity   = dateYearExist.CurrentDuesElectricity;
                billingInfo.CurrentDuesEmElectricity = dateYearExist.CurrentDuesEmElectricity;
                billingInfo.CurrentDuesWasa          = dateYearExist.CurrentDuesWasa;

                billingInfo.OpeningBalanceRent          = dateYearExist.OpeningBalanceRent;
                billingInfo.OpeningBalanceCommon        = dateYearExist.OpeningBalanceCommon;
                billingInfo.OpeningBalanceElectricity   = dateYearExist.OpeningBalanceElectricity;
                billingInfo.OpeningBalanceEmElectricity = dateYearExist.OpeningBalanceEmElectricity;
                billingInfo.OpeningBalanceWasa          = dateYearExist.OpeningBalanceWasa;

                billingInfo.RemarksRent          = dateYearExist.RemarksRent;
                billingInfo.RemarksCommon        = dateYearExist.RemarksCommon;
                billingInfo.RemarksElectricity   = dateYearExist.RemarksElectricity;
                billingInfo.RemarksEmElectricity = dateYearExist.RemarksEmElectricity;
                billingInfo.RemarksWasa          = dateYearExist.RemarksWasa;

                billingInfo.CreatedBy = dateYearExist.CreatedBy;
                billingInfo.CreatedOn = dateYearExist.CreatedOn;
                billingInfo.UpdatedBy = Log.UserId.ToString();
                billingInfo.UpdatedOn = System.DateTime.Now;
                billingInfo.IsActive  = true;

                exist = null;

                billingInfoManager.SaveOrUpdate(billingInfo);
            }



            if (exist != null)
            {
                billingInfo.CurrentDuesRent          = exist.CurrentDuesRent;
                billingInfo.CurrentDuesCommon        = exist.CurrentDuesCommon;
                billingInfo.CurrentDuesElectricity   = exist.CurrentDuesElectricity;
                billingInfo.CurrentDuesEmElectricity = exist.CurrentDuesEmElectricity;
                billingInfo.CurrentDuesWasa          = exist.CurrentDuesWasa;

                billingInfo.OpeningBalanceRent          = exist.CurrentDuesRent + exist.OpeningBalanceRent;
                billingInfo.OpeningBalanceCommon        = exist.CurrentDuesCommon + exist.OpeningBalanceCommon;
                billingInfo.OpeningBalanceElectricity   = exist.CurrentDuesElectricity + exist.OpeningBalanceElectricity;
                billingInfo.OpeningBalanceEmElectricity = exist.CurrentDuesEmElectricity + exist.OpeningBalanceEmElectricity;
                billingInfo.OpeningBalanceWasa          = exist.CurrentDuesWasa + exist.OpeningBalanceWasa;

                billingInfo.RemarksRent          = exist.RemarksRent;
                billingInfo.RemarksCommon        = exist.RemarksCommon;
                billingInfo.RemarksElectricity   = exist.RemarksElectricity;
                billingInfo.RemarksEmElectricity = exist.RemarksEmElectricity;
                billingInfo.RemarksWasa          = exist.RemarksWasa;

                billingInfo.CreatedBy = exist.CreatedBy;
                billingInfo.CreatedOn = exist.CreatedOn;
                billingInfo.UpdatedBy = Log.UserId.ToString();
                billingInfo.UpdatedOn = System.DateTime.Now;
                billingInfo.IsActive  = true;

                billingInfoManager.SaveOrUpdate(billingInfo);
            }


            return(Repository.Done());
        }
        public override bool SaveOrUpdate(FloorRentInfo entity)
        {
            var exist      = Repository.GetById(entity.Id);
            var tenantList = tenantInfoManager.GetById(entity.TenantInfoId);

            entity.Id        = 0;
            entity.CreatedOn = new LoginInfo().CurrentTime;
            entity.CreatedBy = Log.UserId.ToString();
            entity.IsActive  = true;
            entity.IsCurrent = true;


            var TotalRent   = (entity.RentSpaceSFT * entity.SftRate);
            var TotalAIT    = (TotalRent * entity.LessAITPercent) / 100;
            var CurrentDues = TotalRent - TotalAIT - entity.AdvancePay;
            var CommonDues  = (entity.RentSpaceSFT * entity.CommonBillRateSFT);

            var CurrentDuesAIT = (CurrentDues * entity.RentAIT) / 100;
            var CommonDuesAIT  = (CommonDues * entity.CommonAIT) / 100;

            if (exist != null)
            {
                if (tenantList != null)
                {
                    tenantList.Id             = entity.TenantInfoId;
                    tenantList.LessAITPercent = entity.LessAITPercent;
                    tenantList.AdvancePay     = entity.AdvancePay;
                    tenantList.RentAIT        = CurrentDuesAIT;
                    tenantList.CommonAIT      = CommonDuesAIT;
                    tenantInfoManager.Update(tenantList);
                }

                exist.FloorLevel        = entity.FloorLevel;
                exist.LessAITPercent    = entity.LessAITPercent;
                exist.RentSpaceSFT      = entity.RentSpaceSFT;
                exist.SftRate           = entity.SftRate;
                exist.TenantInfoId      = entity.TenantInfoId;
                exist.AdvanceAdjustment = entity.AdvanceAdjustment;
                exist.AdvancePay        = entity.AdvancePay;
                exist.CommonBillRateSFT = entity.CommonBillRateSFT;
                exist.EffectiveDate     = entity.EffectiveDate;
                exist.ExpiryDate        = entity.ExpiryDate;
                exist.RentAIT           = entity.RentAIT;
                exist.CommonAIT         = entity.CommonAIT;

                //exist.IsCurrent = true;
                exist.UpdatedBy = Log.UserId.ToString();
                exist.UpdatedOn = System.DateTime.Now;
                Repository.Update(exist);
            }
            else
            {
                if (tenantList != null)
                {
                    tenantList.Id             = entity.TenantInfoId;
                    tenantList.LessAITPercent = entity.LessAITPercent;
                    tenantList.AdvancePay     = entity.AdvancePay;
                    tenantList.RentAIT        = CurrentDuesAIT;
                    tenantList.CommonAIT      = CommonDuesAIT;
                    tenantInfoManager.SaveOrUpdate(tenantList);
                }

                var repo       = new BillingInfoPerMonthRepository();
                var MonthNo    = entity.EffectiveDate.Month;
                var YearNo     = entity.EffectiveDate.Year;
                var tanentInfo = new TenantInfoRepository().GetById(entity.TenantInfoId);


                var existMonthActive = new BillingInfoPerMonthRepository().GetAll().FirstOrDefault(x => x.Month == MonthNo.ToString() &&
                                                                                                   x.Year == YearNo.ToString() && x.TenantInfoId == entity.TenantInfoId && x.IsActive == true);

                var existMonthInactive = new BillingInfoPerMonthRepository().GetAll().FirstOrDefault(x => x.Month == MonthNo.ToString() &&
                                                                                                     x.Year == YearNo.ToString() && x.TenantInfoId == entity.TenantInfoId && x.IsActive == false);

                if (existMonthActive != null)
                {
                    existMonthInactive = null;
                }

                if (existMonthActive == null || existMonthInactive != null)
                {
                    var newRow = new BillingInfoPerMonth
                    {
                        Month                       = MonthNo.ToString(),
                        Year                        = YearNo.ToString(),
                        TenantInfoId                = entity.TenantInfoId,
                        OpeningBalanceRent          = 0,
                        OpeningBalanceCommon        = 0,
                        OpeningBalanceElectricity   = 0,
                        OpeningBalanceWasa          = 0,
                        OpeningBalanceEmElectricity = 0,
                        CurrentDuesRent             = CurrentDues,
                        CurrentDuesCommon           = CommonDues,
                        CurrentDuesElectricity      = 0,
                        CurrentDuesEmElectricity    = 0,
                        CurrentDuesWasa             = 0,
                        CreatedOn                   = new LoginInfo().CurrentTime,
                        CreatedBy                   = Log.UserId.ToString(),
                        IsActive                    = true
                    };

                    repo.SaveOrUpdate(newRow);
                    repo.Done();
                }
                else
                {
                    existMonthActive.CurrentDuesRent   += CurrentDues;
                    existMonthActive.CurrentDuesCommon += CommonDues;
                    existMonthActive.IsActive           = true;
                    repo.SaveOrUpdate(existMonthActive);
                    repo.Done();
                }


                Repository.SaveOrUpdate(entity);
                Repository.Done();
            }


            return(true);
        }
        public bool Updating(BillingReceivedInfo entity)
        {
            var exist = Repository.GetById(entity.Id);

            BillingInfoPerMonth        billingInfo        = new BillingInfoPerMonth();
            BillingInfoPerMonthManager billingInfoManager = new BillingInfoPerMonthManager();

            billingInfo.Month        = Convert.ToString(entity.Date.Month);
            billingInfo.Year         = Convert.ToString(entity.Date.Year);
            billingInfo.TenantInfoId = entity.TenantInfoId;

            var dateYearExist = billingInfoManager.GetAll().Where(x => x.Month == billingInfo.Month && x.Year == billingInfo.Year && x.TenantInfoId == billingInfo.TenantInfoId && x.IsActive).LastOrDefault();

            if (exist != null)
            {
                entity.CreatedBy = exist.CreatedBy;
                entity.CreatedOn = exist.CreatedOn;
                entity.IsActive  = exist.IsActive;
                entity.UpdatedBy = Log.UserId.ToString();
                entity.UpdatedOn = new LoginInfo().CurrentTime;

                Repository.SaveOrUpdate(entity);

                if (entity.Purpose == "Rent")
                {
                    billingInfo.RentBill          = entity.Amount + dateYearExist.RentBill;
                    billingInfo.CommonBill        = dateYearExist.CommonBill;
                    billingInfo.ElectricityBill   = dateYearExist.ElectricityBill;
                    billingInfo.EmElectricityBill = dateYearExist.EmElectricityBill;
                    billingInfo.WasaBill          = dateYearExist.WasaBill;
                }
                else if (entity.Purpose == "Common Bill")
                {
                    billingInfo.CommonBill        = entity.Amount + dateYearExist.CommonBill;
                    billingInfo.RentBill          = dateYearExist.RentBill;
                    billingInfo.ElectricityBill   = dateYearExist.ElectricityBill;
                    billingInfo.EmElectricityBill = dateYearExist.EmElectricityBill;
                    billingInfo.WasaBill          = dateYearExist.WasaBill;
                }
                else if (entity.Purpose == "Electricity Bill")
                {
                    billingInfo.ElectricityBill   = entity.Amount + dateYearExist.ElectricityBill;
                    billingInfo.RentBill          = dateYearExist.RentBill;
                    billingInfo.CommonBill        = dateYearExist.CommonBill;
                    billingInfo.EmElectricityBill = dateYearExist.EmElectricityBill;
                    billingInfo.WasaBill          = dateYearExist.WasaBill;
                }
                else if (entity.Purpose == "Emergency Electricity Bill")
                {
                    billingInfo.EmElectricityBill = entity.Amount + dateYearExist.EmElectricityBill;
                    billingInfo.RentBill          = dateYearExist.RentBill;
                    billingInfo.CommonBill        = dateYearExist.CommonBill;
                    billingInfo.ElectricityBill   = dateYearExist.ElectricityBill;
                    billingInfo.WasaBill          = dateYearExist.WasaBill;
                }
                else
                {
                    billingInfo.WasaBill          = entity.Amount + dateYearExist.WasaBill;
                    billingInfo.RentBill          = dateYearExist.RentBill;
                    billingInfo.CommonBill        = dateYearExist.CommonBill;
                    billingInfo.ElectricityBill   = dateYearExist.ElectricityBill;
                    billingInfo.EmElectricityBill = dateYearExist.EmElectricityBill;
                }
                billingInfo.Id = dateYearExist.Id;
                billingInfo.CurrentDuesRent          = dateYearExist.CurrentDuesRent;
                billingInfo.CurrentDuesCommon        = dateYearExist.CurrentDuesCommon;
                billingInfo.CurrentDuesElectricity   = dateYearExist.CurrentDuesElectricity;
                billingInfo.CurrentDuesEmElectricity = dateYearExist.CurrentDuesEmElectricity;
                billingInfo.CurrentDuesWasa          = dateYearExist.CurrentDuesWasa;

                billingInfo.OpeningBalanceRent          = dateYearExist.OpeningBalanceRent;
                billingInfo.OpeningBalanceCommon        = dateYearExist.OpeningBalanceCommon;
                billingInfo.OpeningBalanceElectricity   = dateYearExist.OpeningBalanceElectricity;
                billingInfo.OpeningBalanceEmElectricity = dateYearExist.OpeningBalanceEmElectricity;
                billingInfo.OpeningBalanceWasa          = dateYearExist.OpeningBalanceWasa;

                billingInfo.RemarksRent          = dateYearExist.RemarksRent;
                billingInfo.RemarksCommon        = dateYearExist.RemarksCommon;
                billingInfo.RemarksElectricity   = dateYearExist.RemarksElectricity;
                billingInfo.RemarksEmElectricity = dateYearExist.RemarksEmElectricity;
                billingInfo.RemarksWasa          = dateYearExist.RemarksWasa;

                billingInfo.CreatedBy = dateYearExist.CreatedBy;
                billingInfo.CreatedOn = dateYearExist.CreatedOn;
                billingInfo.UpdatedBy = Log.UserId.ToString();
                billingInfo.UpdatedOn = System.DateTime.Now;
                billingInfo.IsActive  = true;


                billingInfoManager.SaveOrUpdate(billingInfo);
            }
            return(Repository.Done());
        }