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

            if (_billingReceivedInfoManager.Saved(model))
            {
                return(Json(new { info = "Saved", status = true }, JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json(new { info = "Please Set Value of Date and Amonut", 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());
        }
 private bool BillingInfoPerMonth(BillingReceivedInfoInput model)
 {
     return(Repository.Done());
 }