//public static void Delete(string remitNo)
        //{
        //    RemittanceModel remittance = new RemittanceModel(service.GetByRemitNo(remitNo));
        //    foreach (RemittanceDetailModel remittanceDetail in remittance.Remittances)
        //    {
        //        ////Change status to 'Confirm' of the receipt to be deleted.
        //        ReceiptViewModel receipt = ReceiptHelper.GetReceipt(remittanceDetail.ReceiptId.ToString());
        //        receipt.Status = "Confirm";
        //        ReceiptHelper.SaveReceipt(receipt);
        //    }

        //    service.Delete(remitNo, AuthenticationHelper.CompanyId.Value);
        //}

        public static void Delete(string remitNo)
        {
            RemittanceModel remittance = new RemittanceModel(service.GetByRemitNo(remitNo));
            if (remittance != null)
            {
                remittance.Remittances = GetRemittanceDetail(remittance.RemitNo).ToList();
                if (remittance.Remittances != null && remittance.Remittances.Count() > 0)
                {
                    foreach (RemittanceDetailModel item in remittance.Remittances)
                    {
                        ////Change status to 'Confirm' of the receipt to be deleted.
                        ReceiptViewModel receipt = ReceiptHelper.GetReceipt(item.ReceiptId.ToString());
                        receipt.Status = "Confirm";
                        ReceiptHelper.SaveReceipt(receipt);
                    }
                }
            }

            service.Delete(remitNo, AuthenticationHelper.CompanyId.Value);
        }
        public static void Update(RemittanceModel remittanceModel)
        {
            var savedDetail = getRemittanceByRemitNo(remittanceModel.RemitNo);
            if (savedDetail.Count() > remittanceModel.Remittances.Count())
            {
                var tobeDeleted = savedDetail.Take(savedDetail.Count() - remittanceModel.Remittances.Count());
                foreach (var item in tobeDeleted)
                {
                    ////Change status to 'Confirm' of the receipt to be deleted.
                    ReceiptViewModel receipt = ReceiptHelper.GetReceipt(item.ReceiptId.ToString());
                    receipt.Status = "Confirm";
                    ReceiptHelper.SaveReceipt(receipt);

                    ////Delete receipt to be remitted.
                    service.DeleteRemittanceDetail(item.Id.ToString(), AuthenticationHelper.CompanyId.Value);
                }
                savedDetail = getRemittanceByRemitNo(remittanceModel.RemitNo);
            }

            foreach (var detail in remittanceModel.Remittances)
            {
                detail.RemitDate = remittanceModel.RemitDate;
                detail.RemitNo = remittanceModel.RemitNo;
                detail.SOBId = remittanceModel.SOBId;
                detail.BankId = remittanceModel.BankId;
                detail.BankAccountId = remittanceModel.BankAccountId;

                Remittance entity = getEntityByModel(detail, savedDetail.Count());
                if (entity.IsValid())
                {
                    if (savedDetail.Count() > 0)
                    {
                        entity.Id = savedDetail.FirstOrDefault().Id;
                        savedDetail.Remove(savedDetail.FirstOrDefault(rec => rec.Id == entity.Id));
                        service.Update(entity);
                    }
                    else
                        service.Insert(entity);
                }

                ////Change status to Remit of the receipt.
                ReceiptViewModel receipt = ReceiptHelper.GetReceipt(detail.ReceiptId.ToString());
                receipt.Status = "Remit";
                ReceiptHelper.SaveReceipt(receipt);
            }
        }
 public ActionResult Create(long bankId, long bankAccountId)
 {
     SessionHelper.Bank = BankHelper.GetBank(bankId.ToString());
     SessionHelper.BankAccount = BankHelper.GetBankAccount(bankAccountId);
     RemittanceModel model = SessionHelper.Remittance;
     
     if (model == null)
     {
         model = new RemittanceModel
         {
             BankAccountId = bankAccountId,
             BankId = bankId,
             SOBId = SessionHelper.SOBId,
             RemitNo = "New",
             RemitDate = Const.CurrentDate,
             Remittances = new List<RemittanceDetailModel>()
         };
         SessionHelper.DocumentDate = model.RemitDate;
         SessionHelper.Remittance = model;
     }
     return View("Edit", model);
 }