private void SaveReceipt(string isNext)
        {
            ReceiptBatch receiptBatch;
            Receipt receipt = null;
            OpenItemReceiptAllocation openItemReceiptAllocation;
            isChanged = true;

            try
            {
                receiptBatch = ReceiptBatchFunctions.Get(ReceiptBatchID);

                receipt = new Receipt();
                receipt.ID = receiptID;
                receipt.ApplyToTypeID = Receipt.ApplyToTypeID;
                receipt.InternalReference = Receipt.InternalReference;

                receipt.ReceiptBatchID = ReceiptBatchID;
                receipt.ContractID = Receipt.ContractID;
                receipt.QuoteID = Receipt.QuoteID;
                receipt.InvoiceAssetId = Receipt.InvoiceAssetId;
                receipt.Reference = reference;
                receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                receipt.LastDateModified = DateTime.Now;
                receipt.ReceiptDate = receiptDate;

                receipt.NetAmountReceived = Receipt.NetAmountReceived.GetValueOrDefault() * -1;
                receipt.GSTAmountReceived = Receipt.GSTAmountReceived.GetValueOrDefault() * -1;
                receipt.FIDAmountReceived = Receipt.FIDAmountReceived.GetValueOrDefault() * -1;
                receipt.SDAmountReceived = Receipt.SDAmountReceived.GetValueOrDefault() * -1;
                receipt.GrossAmountReceived = Receipt.GrossAmountReceived * -1;

                if (batchType == ReceiptBatchType.Dishonour)
                {
                    receipt.DishonourReceiptDetail = new DishonourReceiptDetail { ReceiptID = receiptID, DishonouredReceiptID = Receipt.ID, ReasonCodeID = reasonSelected != 0 ? (int?)reasonSelected : null };
                }
                else
                {
                    receipt.ReversalReceiptDetail = new ReversalReceiptDetail { ReceiptID = receiptID, LinkedReceiptID = Receipt.ID, ReasonCodeID = reasonSelected != 0 ? (int?)reasonSelected : null, ReversalTypeID = (int)ReversalTypes.Reversal };
                }

                foreach (OpenItemReceiptAllocation allocation in DishonourReversalFunctions.GetOpenItemReceiptAllocations(Receipt.ID))
                {
                    openItemReceiptAllocation = new OpenItemReceiptAllocation();

                    openItemReceiptAllocation.ReceiptID = receiptID;
                    openItemReceiptAllocation.OpenItemID = allocation.OpenItemID;
                    openItemReceiptAllocation.NetAmountApplied = allocation.NetAmountApplied * -1;
                    openItemReceiptAllocation.GSTAmountApplied = allocation.GSTAmountApplied * -1;
                    openItemReceiptAllocation.FIDAmountApplied = allocation.FIDAmountApplied * -1;
                    openItemReceiptAllocation.SDAmountApplied = allocation.SDAmountApplied * -1;
                    openItemReceiptAllocation.GrossAmountApplied = allocation.GrossAmountApplied * -1;
                    receipt.OpenItemReceiptAllocations.Add(openItemReceiptAllocation);
                }

                if (receiptBatch.Receipts == null)
                {
                    receiptBatch.Receipts = new List<Receipt>();
                }

                if (receiptID == 0)
                {
                    receiptBatch.NumberOfEntries++;
                }
                else
                {
                    receiptBatch.GrossBatchTotal -= ReceiptFunctions.Get(receiptID).GrossAmountReceived;
                }

                receiptBatch.GrossBatchTotal += receipt.GrossAmountReceived;

                receiptBatch.Receipts.Add(receipt);

                if (batchType == ReceiptBatchType.Reversals && ReturnValue != null && (ReturnValue.Item1 == (int)ReAllocateReceiptTo.Suspense || ReturnValue.Item2.HasValue))
                {
                    receipt = new Receipt();

                    receipt.ID = reAllocationReceiptID.GetValueOrDefault();

                    switch (ReturnValue.Item1)
                    {
                        case (int)ReAllocateReceiptTo.Contract:
                            receipt.ApplyToTypeID = (int)ReceiptApplyTo.Contract;
                            receipt.ContractID = ReturnValue.Item2;
                            receipt.InternalReference = receipt.ContractID.Value.ToString();
                            break;
                        case (int)ReAllocateReceiptTo.Quote:
                            receipt.ApplyToTypeID = (int)ReceiptApplyTo.Quote;
                            receipt.QuoteID = ReturnValue.Item2;
                            receipt.InternalReference = receipt.QuoteID.Value.ToString();
                            break;
                        case (int)ReAllocateReceiptTo.Suspense:
                            receipt.ApplyToTypeID = (int)ReceiptApplyTo.Suspense;
                            break;
                    }

                    receipt.ReceiptBatchID = ReceiptBatchID;
                    receipt.Reference = ReturnValue.Item3;
                    receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                    receipt.LastDateModified = DateTime.Now;
                    receipt.ReceiptDate = receiptDate;

                    receipt.NetAmountReceived = Receipt.NetAmountReceived.GetValueOrDefault();
                    receipt.GSTAmountReceived = Receipt.GSTAmountReceived.GetValueOrDefault();
                    receipt.FIDAmountReceived = Receipt.FIDAmountReceived.GetValueOrDefault();
                    receipt.SDAmountReceived = Receipt.SDAmountReceived.GetValueOrDefault();
                    receipt.GrossAmountReceived = Receipt.GrossAmountReceived;

                    if (receiptID == 0)
                    {
                        receipt.ReversalReceiptDetail = new ReversalReceiptDetail { ReasonCodeID = reasonSelected, ReversalTypeID = (int)ReversalTypes.Reallocation };
                        receipt.ReversalReceiptDetail.Receipt = receiptBatch.Receipts.FirstOrDefault();
                    }
                    else
                    {
                        receipt.ReversalReceiptDetail = new ReversalReceiptDetail { ReceiptID = reAllocationReceiptID.GetValueOrDefault(), LinkedReceiptID = receiptID, ReasonCodeID = reasonSelected, ReversalTypeID = (int)ReversalTypes.Reallocation };
                    }

                    if (ReturnValue.Item1 != (int)ReAllocateReceiptTo.Suspense)
                    {
                        foreach (OpenItemReceiptAllocation allocation in DishonourReversalFunctions.GetOpenItemReceiptAllocations(Receipt.ID))
                        {
                            openItemReceiptAllocation = new OpenItemReceiptAllocation();

                            openItemReceiptAllocation.ReceiptID = reAllocationReceiptID.GetValueOrDefault();
                            openItemReceiptAllocation.OpenItemID = allocation.OpenItemID;
                            openItemReceiptAllocation.NetAmountApplied = allocation.NetAmountApplied;
                            openItemReceiptAllocation.GSTAmountApplied = allocation.GSTAmountApplied;
                            openItemReceiptAllocation.FIDAmountApplied = allocation.FIDAmountApplied;
                            openItemReceiptAllocation.SDAmountApplied = allocation.SDAmountApplied;
                            openItemReceiptAllocation.GrossAmountApplied = allocation.GrossAmountApplied;
                            receipt.OpenItemReceiptAllocations.Add(openItemReceiptAllocation);
                        }
                    }

                    if (reAllocationReceiptID.GetValueOrDefault() == 0)
                    {
                        receiptBatch.NumberOfEntries++;
                    }
                    else
                    {
                        receiptBatch.GrossBatchTotal -= ReceiptFunctions.Get(reAllocationReceiptID.GetValueOrDefault()).GrossAmountReceived;
                    }

                    receiptBatch.GrossBatchTotal += receipt.GrossAmountReceived;
                    receiptBatch.Receipts.Add(receipt);
                }

                ReceiptBatchFunctions.Save(receiptBatch);

                isChanged = false;

                if (Convert.ToBoolean(isNext))
                {
                    OriginalReceiptID = null;
                    SetReceiptDefaults(ReceiptBatchID);
                    SetIcon();
                    ReturnValue = null;
                }
                else
                {
                    Close();
                }
            }
            catch (Exception ex)
            {
                ExceptionLogger.WriteLog(ex);
                ShowErrorMessage("Error occurred while Saving Receipt", "Add Receipt - Error");
            }
            finally
            {
                IsBusy = false;
            }
        }
        private void SaveReceipt(string isNext, bool isChequePayment)
        {
            List<Receipt> receipts;
            Receipt receipt = null;
            OpenItemReceiptAllocation openItemReceiptAllocation;
            CashReceiptDetail cashReceiptDetail;
            int refNo = 0;
            isChanged = true;

            try
            {
                receipts = new List<Receipt>();

                if (IsApplyPaymentsApplicable)
                {
                    foreach (OpenItemSearch openItem in openItems.Where(item => item.AmountDue > 0).OrderBy(item => item.OpenItemID))
                    {
                        if (refNo != openItem.ContractNo || IsApplyPaymentsApplicable == false)
                        {
                            receipt = new Receipt();

                            if (receipts.Count == 0 && receiptID != 0)
                            {
                                receipt.ID = Receipt.ID;
                            }

                            receipt.ReceiptBatchID = Receipt.ReceiptBatchID;
                            receipt.ReceiptDate = Receipt.ReceiptDate;
                            receipt.ApplyToTypeID = Receipt.ApplyToTypeID;
                            receipt.Reference = Receipt.Reference;
                            receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                            receipt.ContractID = openItem.ContractNo;
                            receipt.InternalReference = receipt.ContractID.ToString();

                            if (receiptApplyTo == ReceiptApplyTo.Invoice)
                            {
                                receipt.InvoiceAssetId = business.InvoiceFunctions.GetInvoiceAssetID(openItem.ContractNo, applyToObjectID.GetValueOrDefault());
                                receipt.InternalReference = receipt.InvoiceAssetId.GetValueOrDefault().ToString();
                            }

                            if (batchType == ReceiptBatchType.CashCheque)
                            {
                                if (isChequePayment)
                                {
                                    receipt.ChequeReceiptDetail = new ChequeReceiptDetail();
                                    receipt.ChequeReceiptDetail.AccountName = Receipt.ChequeReceiptDetail.AccountName;
                                    receipt.ChequeReceiptDetail.BankName = Receipt.ChequeReceiptDetail.BankName;
                                    receipt.ChequeReceiptDetail.BSBNumber = Receipt.ChequeReceiptDetail.BSBNumber;
                                    receipt.ChequeReceiptDetail.ChequeNumber = Receipt.ChequeReceiptDetail.ChequeNumber;
                                    receipt.ChequeReceiptDetail.PaymentTypeID = (int)paymentType;
                                    receipt.ChequeReceiptDetail.ReceiptID = receipt.ID;
                                    receipt.ChequeReceiptDetail.Receipt = receipt;
                                }
                                else
                                {
                                    receipt.ChequeReceiptDetail = null;
                                    cashReceiptDetail = new CashReceiptDetail();
                                    cashReceiptDetail.ReceiptID = receipt.ID;

                                    if (receipts.Count == 0 && Receipt.CashReceiptDetails != null && Receipt.CashReceiptDetails.Count > 0)
                                    {
                                        cashReceiptDetail.ID = Receipt.CashReceiptDetails.FirstOrDefault().ID;
                                    }

                                    cashReceiptDetail.PaymentTypeID = (int)paymentType;
                                    receipt.CashReceiptDetails = new List<CashReceiptDetail>();
                                    receipt.CashReceiptDetails.Add(cashReceiptDetail);
                                }
                            }
                            else if (batchType == ReceiptBatchType.DirectDebit)
                            {
                                receipt.DirectDebitReceiptDetails = new List<DirectDebitReceiptDetail>();

                                if (receipts.Count == 0)
                                {
                                    receipt.DirectDebitReceiptDetails.Add(new DirectDebitReceiptDetail { LesseeBankAccountID = DDCCAccountID, ID = Receipt.DirectDebitReceiptDetails.FirstOrDefault().ID, ReceiptID = Receipt.ID });
                                }
                                else
                                {
                                    receipt.DirectDebitReceiptDetails.Add(new DirectDebitReceiptDetail { LesseeBankAccountID = DDCCAccountID });
                                }
                            }
                            else if (batchType == ReceiptBatchType.CreditCard)
                            {
                                receipt.CreditCardReceiptDetails = new List<CreditCardReceiptDetail>();

                                if (receipts.Count == 0)
                                {
                                    receipt.CreditCardReceiptDetails.Add(new CreditCardReceiptDetail { LesseeBankAccountID = DDCCAccountID, ID = Receipt.CreditCardReceiptDetails.FirstOrDefault().ID, ReceiptID = Receipt.ID });
                                }
                                else
                                {
                                    receipt.CreditCardReceiptDetails.Add(new CreditCardReceiptDetail { LesseeBankAccountID = DDCCAccountID });
                                }
                            }                          

                            receipt.OpenItemReceiptAllocations = new List<OpenItemReceiptAllocation>();
                            receipts.Add(receipt);
                            refNo = openItem.ContractNo;
                        }

                        openItemReceiptAllocation = new OpenItemReceiptAllocation();
                        openItemReceiptAllocation.OpenItemID = openItem.OpenItemID;
                        openItemReceiptAllocation.ReceiptID = receiptID;
                        openItemReceiptAllocation.GrossAmountDue = openItem.AmountDue;
                        openItemReceiptAllocation.GrossAmountApplied = Convert.ToDecimal(openItem.AmountApplied);

                        receipt.OpenItemReceiptAllocations.Add(openItemReceiptAllocation);
                    }
                }
                else
                {
                    receipt = new Receipt();

                    receipt.ID = Receipt.ID;
                    receipt.ReceiptBatchID = Receipt.ReceiptBatchID;
                    receipt.ReceiptDate = Receipt.ReceiptDate;
                    receipt.ApplyToTypeID = Receipt.ApplyToTypeID;
                    receipt.Reference = Receipt.Reference;
                    receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                    receipt.GrossAmountReceived = amountReceived;
                    receipt.NetAmountReceived = amountReceived;

                    if (receiptApplyTo == ReceiptApplyTo.Quote)
                    {
                        receipt.QuoteID = applyToObjectID.GetValueOrDefault();
                        receipt.InternalReference = receipt.QuoteID.ToString();
                    }

                    if (batchType == ReceiptBatchType.CashCheque)
                    {
                        if (isChequePayment)
                        {
                            receipt.ChequeReceiptDetail = new ChequeReceiptDetail();
                            receipt.ChequeReceiptDetail.AccountName = Receipt.ChequeReceiptDetail.AccountName;
                            receipt.ChequeReceiptDetail.BankName = Receipt.ChequeReceiptDetail.BankName;
                            receipt.ChequeReceiptDetail.BSBNumber = Receipt.ChequeReceiptDetail.BSBNumber;
                            receipt.ChequeReceiptDetail.ChequeNumber = Receipt.ChequeReceiptDetail.ChequeNumber;
                            receipt.ChequeReceiptDetail.PaymentTypeID = (int)paymentType;
                            receipt.ChequeReceiptDetail.ReceiptID = receipt.ID;
                            receipt.ChequeReceiptDetail.Receipt = receipt;
                        }
                        else
                        {
                            receipt.ChequeReceiptDetail = null;
                            receipt.CashReceiptDetails = new List<CashReceiptDetail>();
                            receipt.CashReceiptDetails.Add(new CashReceiptDetail() { ReceiptID = receipt.ID, PaymentTypeID = (int)paymentType });

                            if (receipts.Count == 0 && Receipt.CashReceiptDetails != null && Receipt.CashReceiptDetails.Count > 0)
                            {
                                receipt.CashReceiptDetails.FirstOrDefault().ID = Receipt.CashReceiptDetails.FirstOrDefault().ID;
                            }
                        }
                    }
                    else if (batchType == ReceiptBatchType.DirectDebit)
                    {
                        receipt.DirectDebitReceiptDetails = new List<DirectDebitReceiptDetail>();

                        if (receipts.Count == 0)
                        {
                            receipt.DirectDebitReceiptDetails.Add(new DirectDebitReceiptDetail { LesseeBankAccountID = DDCCAccountID, ID = Receipt.DirectDebitReceiptDetails.FirstOrDefault().ID, ReceiptID = Receipt.ID });
                        }
                        else
                        {
                            receipt.DirectDebitReceiptDetails.Add(new DirectDebitReceiptDetail { LesseeBankAccountID = DDCCAccountID });
                        }
                    }
                    else if (batchType == ReceiptBatchType.CreditCard)
                    {
                        receipt.CreditCardReceiptDetails = new List<CreditCardReceiptDetail>();

                        if (receipts.Count == 0)
                        {
                            receipt.CreditCardReceiptDetails.Add(new CreditCardReceiptDetail { LesseeBankAccountID = DDCCAccountID, ID = Receipt.CreditCardReceiptDetails.FirstOrDefault().ID, ReceiptID = Receipt.ID });
                        }
                        else
                        {
                            receipt.CreditCardReceiptDetails.Add(new CreditCardReceiptDetail { LesseeBankAccountID = DDCCAccountID });
                        }
                    }                    

                    receipts.Add(receipt);
                }

                ReceiptFunctions.Save(receipts);

                isChanged = false;

                if (Convert.ToBoolean(isNext))
                {
                    receiptID = 0;
                    SetReceiptDefaults(Receipt.ReceiptBatchID);
                    SetIcon();
                    RaisePropertyChanged("IsReceiptinEditMode");
                }
                else
                {
                    Close();
                }
            }
            catch (Exception ex)
            {
                ExceptionLogger.WriteLog(ex);
                ShowErrorMessage("Error occurred while Saving Receipt", "Add Receipt - Error");
                IsBusy = false;
            }
        }
        private void OnSave()
        {
            List<Receipt> receipts;
            ReceiptBatch receiptBatch;
            Receipt receipt;
            CashReceiptDetail cashReceiptDetail;
            PaymentType paymentType;
            OpenItemReceiptAllocation openItemReceiptAllocation;
            bool confirmResult = true;

            try
            {
                IsBusy = true;

                receipts = new List<Receipt>();

                foreach (CashReceiptExcel excelItem in Receipts)
                {
                    receipt = new Receipt();

                    paymentType = CashReceiptExcelImportViewModel.CashChequePaymentTypes.Where(payType => payType.PaymentTypeDesc.ToLower() == excelItem.PaymentMethod.ToLower()).FirstOrDefault();

                    receipt.ReceiptBatchID = receiptBatchID;
                    receipt.ReceiptDate = Convert.ToDateTime(excelItem.ReceiptDate);
                    receipt.ApplyToTypeID = (int)ReceiptApplyTo.Contract;
                    receipt.ContractID = Convert.ToInt32(excelItem.ContractID);
                    receipt.Reference = excelItem.ReferenceNo;
                    receipt.GrossAmountReceived = Math.Round(Convert.ToDecimal(excelItem.AmountReceived), 2);
                    receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                    receipt.InternalReference = receipt.ContractID.ToString();

                    if (paymentType.SCPaymentTypeId == (int)SCPaymentTypes.Cheque)
                    {
                        receipt.ChequeReceiptDetail = new ChequeReceiptDetail();
                        receipt.ChequeReceiptDetail.PaymentTypeID = paymentType.PaymentTypeId;
                        receipt.ChequeReceiptDetail.ChequeNumber = excelItem.ChequeNumber;
                        receipt.ChequeReceiptDetail.AccountName = excelItem.AccountName;
                        receipt.ChequeReceiptDetail.BankName = excelItem.BankName;
                        receipt.ChequeReceiptDetail.BSBNumber = excelItem.BSBNumber;
                    }
                    else
                    {
                        cashReceiptDetail = new CashReceiptDetail();
                        cashReceiptDetail.PaymentTypeID = paymentType.PaymentTypeId;
                        receipt.CashReceiptDetails.Add(cashReceiptDetail);
                    }

                    receipt.OpenItemReceiptAllocations = new List<OpenItemReceiptAllocation>();

                    foreach (OpenItemSearch openItem in excelItem.OpenItems)
                    {
                        openItemReceiptAllocation = new OpenItemReceiptAllocation();
                        openItemReceiptAllocation.OpenItemID = openItem.OpenItemID;
                        openItemReceiptAllocation.GrossAmountDue = openItem.AmountDue;
                        openItemReceiptAllocation.GrossAmountApplied = Convert.ToDecimal(openItem.AmountApplied);

                        receipt.OpenItemReceiptAllocations.Add(openItemReceiptAllocation);
                    }

                    receipts.Add(receipt);
                }

                if (receipts.Where(item => item.ReceiptDate > DateTime.Today).Count() > 0)
                {
                    UIConfirmation.Raise(
                           new ConfirmationWindowViewModel(this) { Content = "One or more Receipt Date(s) are in the future. Select OK to continue or Cancel to modify", Icon = "Question.ico", Title = "Receipt Date Confirmation" },
                           (popupCallBack) =>
                           {
                               if (popupCallBack.Confirmed)
                               {
                                   ReceiptFunctions.Save(receipts);
                                   Close();
                               }
                           });
                }
                else
                {
                    ReceiptFunctions.Save(receipts);
                    Close();
                }

                IsBusy = false;
            }
            catch (Exception ex)
            {
                ExceptionLogger.WriteLog(ex);
                ShowErrorMessage("Error encountered while saving Excel entries.", "Excel Import - Error");
            }
            finally
            {
                IsBusy = false;
            }
        }
        private void SaveReceipt()
        {
            ReceiptBatch receiptBatch;
            Receipt receipt = null;
            OpenItemReceiptAllocation openItemReceiptAllocation;

            try
            {
                receiptBatch = ReceiptBatchFunctions.Get(receiptBatchID);

                if (receiptBatch.Receipts == null)
                {
                    receiptBatch.Receipts = new List<Receipt>();
                }

                foreach (DishonourReversalReceiptSummary reversalReceipt in reversalReceipts)
                {
                    receipt = new Receipt();

                    switch (reallocateTo)
                    {
                        case (int)ReAllocateReceiptTo.Contract:
                            receipt.ApplyToTypeID = (int)ReceiptApplyTo.Contract;
                            receipt.ContractID = ReAllocateToObjectID;

                            if (reversalReceipt.ApplyToObjectID.HasValue)
                            {
                                receipt.InternalReference = reversalReceipt.ApplyToObjectID.Value.ToString();
                            }
                            break;
                        case (int)ReAllocateReceiptTo.Quote:
                            receipt.ApplyToTypeID = (int)ReceiptApplyTo.Quote;
                            receipt.QuoteID = ReAllocateToObjectID;

                            if (reversalReceipt.ApplyToObjectID.HasValue)
                            {
                                receipt.InternalReference = reversalReceipt.ApplyToObjectID.Value.ToString();
                            }
                            break;
                        case (int)ReAllocateReceiptTo.Suspense:
                            receipt.ApplyToTypeID = (int)ReceiptApplyTo.Suspense;
                            break;
                    }

                    receipt.ReceiptBatchID = receiptBatchID;
                    receipt.Reference = Reference;

                    receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId; ;
                    receipt.LastDateModified = DateTime.Now;
                    receipt.ReceiptDate = DateTime.Today; // receiptDate;

                    receipt.NetAmountReceived = reversalReceipt.NetAmount * (-1);
                    receipt.GSTAmountReceived = reversalReceipt.GSTAmount * (-1);
                    receipt.FIDAmountReceived = reversalReceipt.FIDAmount * (-1);
                    receipt.SDAmountReceived = reversalReceipt.SDAmount * (-1);
                    receipt.GrossAmountReceived = reversalReceipt.GrossAmount * (-1);

                    receipt.ReversalReceiptDetail = new ReversalReceiptDetail { LinkedReceiptID = reversalReceipt.ID, ReasonCodeID = reasonSelected, ReversalTypeID = (int)ReversalTypes.Reallocation };

                    if (reallocateTo != (int)ReAllocateReceiptTo.Suspense)
                    {
                        foreach (OpenItemReceiptAllocation allocation in DishonourReversalFunctions.GetOpenItemReceiptAllocations(reversalReceipt.ID))
                        {
                            openItemReceiptAllocation = new OpenItemReceiptAllocation();

                            openItemReceiptAllocation.OpenItemID = allocation.OpenItemID;
                            openItemReceiptAllocation.NetAmountApplied = allocation.NetAmountApplied;
                            openItemReceiptAllocation.GSTAmountApplied = allocation.GSTAmountApplied;
                            openItemReceiptAllocation.FIDAmountApplied = allocation.FIDAmountApplied;
                            openItemReceiptAllocation.SDAmountApplied = allocation.SDAmountApplied;
                            openItemReceiptAllocation.GrossAmountApplied = allocation.GrossAmountApplied;
                            receipt.OpenItemReceiptAllocations.Add(openItemReceiptAllocation);
                        }
                    }

                    receiptBatch.NumberOfEntries++;
                    receiptBatch.GrossBatchTotal += receipt.GrossAmountReceived;

                    receiptBatch.Receipts.Add(receipt);
                }

                ReceiptBatchFunctions.Save(receiptBatch);
                ReturnValue = true;

                Close();
            }
            catch (Exception ex)
            {
                ExceptionLogger.WriteLog(ex);
                ShowErrorMessage("Error occurred while Saving Re-Allocation Receipt", "Receipt Reallocation - Error");
            }
            finally
            {
                IsBusy = false;
            }
        }
        private void OnSave()
        {
            ReceiptBatch receiptBatch;
            Receipt receipt;
            BankAccount bankAccount;

            DishonourReversalReceiptExcel dishonourReversalReceiptDetail;
            OpenItemReceiptAllocation openItemReceiptAllocation;

            try
            {
                receiptBatch = ReceiptBatchFunctions.Get(receiptBatchID);
                bankAccount = ReceiptBatchFunctions.GetBankAccount(batchType, ReceiptFunctions.Get(Convert.ToInt32(Receipts.FirstOrDefault().ReceiptID)).ReceiptBatchID);

                if (DishonourReversalFunctions.IsReceiptsBelongToSameBank(Receipts.Select(item => Convert.ToInt32(item.ReceiptID)).ToList()) == false)
                {
                    ShowMessage("All Receipts to be imported into this Batch must belong to single Bank Account", "Excel Import - Receipts Validation");
                    return;
                }
                else if (receiptBatch.BankAccountID.HasValue && receiptBatch.BankAccountID != bankAccount.ID)
                {
                    bankAccount = ReceiptBatchFunctions.GetBankAccount(batchType, receiptBatchID);
                    ShowMessage("Receipts of bank \"" + bankAccount.BankName + "\" can only be imported into this batch", "Excel Import - Receipts Validation");
                    return;
                }

                IsBusy = true;

                receiptBatch.BankAccountID = bankAccount.ID;
                receiptBatch.FilterDescription = "Bank: " + bankAccount.BankName;

                receiptBatch.Receipts = new List<Receipt>();

                foreach (DishonourReversalReceiptExcel excelItem in Receipts)
                {
                    receipt = new Receipt();

                    receipt.ReceiptBatchID = receiptBatchID;
                    receipt.ReceiptDate = Convert.ToDateTime(excelItem.ReceiptDate);
                    receipt.ApplyToTypeID = (int)ReceiptApplyTo.Contract;
                    receipt.ContractID = Convert.ToInt32(excelItem.ContractID);
                    receipt.Reference = excelItem.Reference;
                    receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                    receipt.InternalReference = receipt.ContractID.ToString();
                    receipt.LastDateModified = DateTime.Now;

                    receipt.FIDAmountReceived = 0;
                    receipt.SDAmountReceived = 0;
                    receipt.GSTAmountReceived = 0;
                    receipt.NetAmountReceived = 0;
                    receipt.GrossAmountReceived = 0;

                    if (batchType == ReceiptBatchType.Dishonour)
                    {
                        receipt.DishonourReceiptDetail = new DishonourReceiptDetail();
                        receipt.DishonourReceiptDetail.DishonouredReceiptID = Convert.ToInt32(excelItem.ReceiptID);

                        if (excelItem.ReasonCodeID > 0)
                        {
                            receipt.DishonourReceiptDetail.ReasonCodeID = excelItem.ReasonCodeID;
                        }
                    }
                    else if (batchType == ReceiptBatchType.Reversals)
                    {
                        receipt.ReversalReceiptDetail = new ReversalReceiptDetail();
                        receipt.ReversalReceiptDetail.LinkedReceiptID = Convert.ToInt32(excelItem.ReceiptID);
                        receipt.ReversalReceiptDetail.ReversalTypeID = (int)ReversalTypes.Reversal;

                        if (excelItem.ReasonCodeID > 0)
                        {
                            receipt.ReversalReceiptDetail.ReasonCodeID = excelItem.ReasonCodeID;
                        }
                    }

                    foreach (OpenItemReceiptAllocation allocation in DishonourReversalFunctions.GetOpenItemReceiptAllocations(Convert.ToInt32(excelItem.ReceiptID)))
                    {
                        openItemReceiptAllocation = new OpenItemReceiptAllocation();

                        openItemReceiptAllocation.OpenItemID = allocation.OpenItemID;
                        openItemReceiptAllocation.NetAmountApplied = allocation.NetAmountApplied * -1;
                        openItemReceiptAllocation.GSTAmountApplied = allocation.GSTAmountApplied * -1;
                        openItemReceiptAllocation.FIDAmountApplied = allocation.FIDAmountApplied * -1;
                        openItemReceiptAllocation.SDAmountApplied = allocation.SDAmountApplied * -1;
                        openItemReceiptAllocation.GrossAmountApplied = allocation.GrossAmountApplied * -1;
                        receipt.OpenItemReceiptAllocations.Add(openItemReceiptAllocation);

                        receipt.FIDAmountReceived += openItemReceiptAllocation.FIDAmountApplied;
                        receipt.SDAmountReceived += openItemReceiptAllocation.SDAmountApplied;
                        receipt.GSTAmountReceived += openItemReceiptAllocation.GSTAmountApplied;
                        receipt.NetAmountReceived += openItemReceiptAllocation.NetAmountApplied;
                        receipt.GrossAmountReceived += openItemReceiptAllocation.GrossAmountApplied.GetValueOrDefault();
                    }

                    receiptBatch.GrossBatchTotal += receipt.GrossAmountReceived;
                    receiptBatch.NumberOfEntries++;

                    receiptBatch.Receipts.Add(receipt);
                }

                if (receiptBatch.Receipts.Where(item => item.ReceiptDate > DateTime.Today).Count() > 0)
                {
                    UIConfirmation.Raise(
                           new ConfirmationWindowViewModel(this) { Content = "One or more Receipt Date(s) are in the future. Select OK to continue or Cancel to modify", Icon = "Question.ico", Title = "Receipt Date Confirmation" },
                           (popupCallBack) =>
                           {
                               if (popupCallBack.Confirmed)
                               {                                   
                                    ReceiptBatchFunctions.Save(receiptBatch);
                                    Close();
                               }
                           });
                }
                else
                {
                    ReceiptBatchFunctions.Save(receiptBatch);
                    Close();
                }

                IsBusy = false;
            }
            catch (Exception ex)
            {
                ExceptionLogger.WriteLog(ex);
                ShowErrorMessage("Error encountered while Importing Excel Entries", "Excel Import - Error");
            }
            finally
            {
                IsBusy = false;
            }
        }
        private void SaveReceiptBatch()
        {
            PopupWindow popupWindow;
            AccountingParam accountingParam;
            ReceiptBatch newReceiptBatch;
            Receipt receipt = null;
            List<OpenItemReceiptAllocation> receiptAllocations = null;
            List<businessModel.DirectDebitException> DDExceptions = null;
            List<businessModel.CreditCardException> CCExceptions = null;
            IEnumerable<int> openItemIDs = null;
            OpenItemReceiptAllocation newAllocation;

            try
            {
                int contractID = 0;
                isChanged = true;
                bool isNew;

                accountingParam = SystemFunctions.GetAccountingParam();
                isNew = receiptBatch.ID == 0;

                receiptBatch.BatchStatusID = (int)ReceiptBatchStatus.Created;

                if (accountingParam.CurrentAccountingDate.HasValue)
                {
                    receiptBatch.AccountingDate = accountingParam.CurrentAccountingDate;
                    receiptBatch.BatchMonth = accountingParam.CurrentAccountingDate.Value.ToString("MMM yyyy");
                }
                else
                {
                    receiptBatch.AccountingDate = receiptBatch.ReceiptDate;
                    receiptBatch.BatchMonth = receiptBatch.ReceiptDate.ToString("MMM yyyy");
                }

                receiptBatch.CreatedByUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;

                if (receiptBatchType == ReceiptBatchType.CashCheque)
                {
                    ReceiptBatchFunctions.Save(receiptBatch);

                    if (isNew && createFromList.Where(from => from.ID == createFrom).FirstOrDefault().Description.ToLower().Trim() == ReceiptBatchCreateFrom.Import.ToString().ToLower().Trim())
                    {
                        ReturnValue = new NavigationItem() { ReceiptID = receiptBatch.ID, ReceiptText = receiptBatch.ID.ToString() + "-" + SelectedBatch, BatchStatus = (int)ReceiptBatchStatus.Created, BatchTypeID = receiptBatch.BatchTypeID };

                        IsBusy = true;
                        popupWindow = new PopupWindow(ViewAssemblies.ReceiptsViewAssembly, "CashCheque.CashReceiptExcelImport", true);
                        popupWindow.Parameters.Add(receiptBatch.ID);
                        Popup.Raise(popupWindow);
                        receiptBatch = ReceiptBatchFunctions.Get(receiptBatch.ID);
                        IsBusy = false;
                    }
                    else
                    {
                        ReturnValue = new NavigationItem() { ReceiptID = receiptBatch.ID, ReceiptText = receiptBatch.ID.ToString() + "-" + SelectedBatch, BatchStatus = (int)ReceiptBatchStatus.Created, BatchTypeID = receiptBatch.BatchTypeID };
                        Close();
                    }
                }
                else if (receiptBatchType == ReceiptBatchType.DirectDebit || receiptBatchType == ReceiptBatchType.CreditCard || receiptBatchType == ReceiptBatchType.AutoReceipts)
                {
                    if (IsDateRangeApplicable)
                    {
                        receiptAllocations = DDCCBatchFunctions.GetDDCCOpenItemsReceiptAllocation(receiptBatchType, fromDate, toDate, IsIntercompanySelected, selectedinternalCompanyBanks);

                        openItemIDs = receiptAllocations.Select(item => item.OpenItemID).Distinct();

                        if (receiptBatchType == ReceiptBatchType.DirectDebit)
                        {
                            DDExceptions = DDCCBatchFunctions.GetDDExceptions(fromDate, toDate, IsIntercompanySelected,
                                ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId, selectedinternalCompanyBanks);
                        }
                        else if (receiptBatchType == ReceiptBatchType.CreditCard)
                        {
                            CCExceptions = DDCCBatchFunctions.GetCCExceptions(fromDate, toDate, IsIntercompanySelected, ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId, selectedinternalCompanyBanks);
                        }
                    }

                    if (receiptBatchType == ReceiptBatchType.AutoReceipts)
                    {
                        newReceiptBatch = receiptBatch.Copy();

                        if (IsDateRangeApplicable)
                        {
                            newReceiptBatch.DateRangeFilterDescription = "From " + fromDate.ToString("dd/MM/yyyy") + " to " + toDate.ToString("dd/MM/yyyy");
                            newReceiptBatch.Receipts = new List<Receipt>();

                            foreach (OpenItemReceiptAllocation allocation in receiptAllocations.OrderBy(item => item.OpenItem.ContractID))
                            {
                                if (contractID != allocation.OpenItem.Contract.ContractId)
                                {
                                    receipt = new Receipt();
                                    receipt.ApplyToTypeID = (int)ReceiptApplyTo.Contract;
                                    receipt.ContractID = allocation.OpenItem.Contract.ContractId;
                                    receipt.InternalReference = receipt.ContractID.ToString();
                                    receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                                    receipt.LastDateModified = DateTime.Now;
                                    receipt.ReceiptDate = receiptBatch.ReceiptDate;

                                    receipt.GSTAmountReceived = 0;
                                    receipt.SDAmountReceived = 0;
                                    receipt.FIDAmountReceived = 0;
                                    receipt.NetAmountReceived = 0;
                                    receipt.GrossAmountReceived = 0;

                                    receipt.OpenItemReceiptAllocations = new List<OpenItemReceiptAllocation>();
                                    newReceiptBatch.Receipts.Add(receipt);
                                    newReceiptBatch.NumberOfEntries += 1;
                                    contractID = allocation.OpenItem.Contract.ContractId;
                                }

                                receipt.GSTAmountReceived += allocation.GSTAmountApplied.GetValueOrDefault();
                                receipt.SDAmountReceived += allocation.SDAmountApplied.GetValueOrDefault();
                                receipt.FIDAmountReceived += allocation.FIDAmountApplied.GetValueOrDefault();
                                receipt.NetAmountReceived += allocation.NetAmountApplied;
                                receipt.GrossAmountReceived += allocation.GrossAmountApplied.GetValueOrDefault();
                                newReceiptBatch.GrossBatchTotal += allocation.GrossAmountApplied.GetValueOrDefault();
                                allocation.OpenItem.Contract = null;
                                allocation.OpenItem = null;

                                receipt.OpenItemReceiptAllocations.Add(allocation);
                            }
                        }

                        ReceiptBatchFunctions.Save(newReceiptBatch, openItemIDs, fromDate, toDate);
                        ReturnValue = new NavigationItem() { ReceiptID = newReceiptBatch.ID, ReceiptText = newReceiptBatch.ID.ToString() + "-" + SelectedBatch, BatchStatus = (int)ReceiptBatchStatus.Created, BatchTypeID = newReceiptBatch.BatchTypeID };
                    }
                    else
                    {
                        foreach (InternalCompanyBank internalCompBank in selectedinternalCompanyBanks)
                        {
                            newReceiptBatch = receiptBatch.Copy();
                            newReceiptBatch.BankAccountID = internalCompBank.BankID;
                            newReceiptBatch.InternalCompanyNodeID = internalCompBank.NodeID;

                            if (isBankSelected)
                            {
                                newReceiptBatch.FilterDescription = "Bank: " + internalCompBank.BankName;
                            }
                            else if (IsIntercompanySelected)
                            {
                                newReceiptBatch.FilterDescription = "Internal Company: " + internalCompBank.InternalCompany;
                            }

                            if (IsDateRangeApplicable)
                            {
                                newReceiptBatch.DateRangeFilterDescription = "From " + fromDate.ToString("dd/MM/yyyy") + " to " + toDate.ToString("dd/MM/yyyy");
                                newReceiptBatch.Receipts = new List<Receipt>();

                                foreach (OpenItemReceiptAllocation allocation in receiptAllocations.Where(item => IsIntercompanySelected ? item.OpenItem.Contract.InternalCoyNodeId == internalCompBank.NodeID :
                                        item.OpenItem.Contract.LessorBankAccountId == internalCompBank.BankID))
                                {
                                    if (contractID != allocation.OpenItem.Contract.ContractId)
                                    {
                                        receipt = new Receipt();
                                        receipt.ApplyToTypeID = (int)ReceiptApplyTo.Contract;
                                        receipt.ContractID = allocation.OpenItem.Contract.ContractId;
                                        receipt.InternalReference = receipt.ContractID.ToString();
                                        receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                                        receipt.LastDateModified = DateTime.Now;
                                        receipt.ReceiptDate = receiptBatch.ReceiptDate;

                                        receipt.GSTAmountReceived = 0;
                                        receipt.SDAmountReceived = 0;
                                        receipt.FIDAmountReceived = 0;
                                        receipt.NetAmountReceived = 0;
                                        receipt.GrossAmountReceived = 0;

                                        if (receiptBatchType == ReceiptBatchType.DirectDebit)
                                        {
                                            receipt.DirectDebitReceiptDetails = new List<DirectDebitReceiptDetail>();
                                            receipt.DirectDebitReceiptDetails.Add(new DirectDebitReceiptDetail { LesseeBankAccountID = allocation.OpenItem.Contract.LesseeBankAccountId.GetValueOrDefault() });
                                        }
                                        else if (receiptBatchType == ReceiptBatchType.CreditCard)
                                        {
                                            receipt.CreditCardReceiptDetails = new List<CreditCardReceiptDetail>();
                                            receipt.CreditCardReceiptDetails.Add(new CreditCardReceiptDetail { LesseeBankAccountID = allocation.OpenItem.Contract.LesseeBankAccountId.GetValueOrDefault() });
                                        }

                                        receipt.OpenItemReceiptAllocations = new List<OpenItemReceiptAllocation>();
                                        newReceiptBatch.Receipts.Add(receipt);
                                        newReceiptBatch.NumberOfEntries += 1;
                                        contractID = allocation.OpenItem.Contract.ContractId;
                                    }

                                    receipt.GSTAmountReceived += allocation.GSTAmountApplied.GetValueOrDefault();
                                    receipt.SDAmountReceived += allocation.SDAmountApplied.GetValueOrDefault();
                                    receipt.FIDAmountReceived += allocation.FIDAmountApplied.GetValueOrDefault();
                                    receipt.NetAmountReceived += allocation.NetAmountApplied;
                                    receipt.GrossAmountReceived += allocation.GrossAmountApplied.GetValueOrDefault();
                                    newReceiptBatch.GrossBatchTotal += allocation.GrossAmountApplied.GetValueOrDefault();
                                    allocation.OpenItem.Contract = null;
                                    allocation.OpenItem = null;

                                    receipt.OpenItemReceiptAllocations.Add(allocation);
                                }

                                if (receiptBatchType == ReceiptBatchType.DirectDebit)
                                {
                                    newReceiptBatch.DirectDebitExceptions = DDExceptions.Where(item => (IsIntercompanySelected ? item.InternalCoyNodeId == internalCompBank.NodeID :
                                        (item.LessorBankAccountID == internalCompBank.BankID || item.LessorBankAccountID == null || item.LessorBankAccountID == -1))).
                                        Select(item => new model.DirectDebitException { AccountNumber = item.AccountNumber, AccountTitle = item.AccountTitle, BSBNumber = item.BSBNumber,
                                            CompanyName = item.CompanyName, ContractID = item.ContractID, CustomerName = item.CustomerName, DateCreated = item.DateCreated, Exception = item.Exception,
                                            GrossAmount = item.GrossAmount, ID = item.ID, LesseeBankAccountID = item.LesseeBankAccountID, LessorAccountNumber = item.LessorAccountNumber, LessorAccountTitle = item.LessorAccountTitle,
                                            LessorBankAccountID = item.LessorBankAccountID, LessorBSBNumber = item.LessorBSBNumber, PaymentDate = item.PaymentDate,  ReceiptBatchID = item.ReceiptBatchID, 
                                            UpdateRequired = item.UpdateRequired, UserID = item.UserID }).ToList();
                                }
                                else if (receiptBatchType == ReceiptBatchType.CreditCard)
                                {
                                    newReceiptBatch.CreditCardExceptions = CCExceptions.Where(item => (IsIntercompanySelected ? item.InternalCoyNodeId == internalCompBank.NodeID :
                                            (item.LessorBankAccountID == internalCompBank.BankID || item.LessorBankAccountID == null || item.LessorBankAccountID == -1))).Select(item => 
                                                new model.CreditCardException { AccountTitle = item.AccountTitle, CardNumber = item.CardNumber, CompanyName = item.CompanyName, CDTFMerchantNumber = item.CDTFMerchantNumber, 
                                                    ContractID = item.ContractID, CustomerName = item.CustomerName, DateCreated = item.DateCreated, Exception = item.Exception, ExpiryDate = item.ExpiryDate,
                                                    GrossAmount = item.GrossAmount, ID = item.ID, LesseeBankAccountID = item.LesseeBankAccountID, LessorAccountTitle = item.LessorAccountTitle, LessorBankAccountID = item.LessorBankAccountID,
                                                    MerchantNumber = item.MerchantNumber, PaymentDate = item.PaymentDate, ReceiptBatchID = item.ReceiptBatchID, UserID = item.UserID }).ToList();
                                }
                            }

                            ReceiptBatchFunctions.Save(newReceiptBatch, openItemIDs, fromDate, toDate);

                            ReturnValue = new NavigationItem() { ReceiptID = newReceiptBatch.ID, ReceiptText = newReceiptBatch.ID.ToString() + "-" + SelectedBatch, BatchStatus = (int)ReceiptBatchStatus.Created, BatchTypeID = newReceiptBatch.BatchTypeID };
                        }
                    }

                    Close();
                }
                else if (receiptBatchType == ReceiptBatchType.Dishonour || receiptBatchType == ReceiptBatchType.Reversals)
                {
                    if (selectedReceipts != null)
                    {
                        var banksList = selectedReceipts.Select(item => ((DishonourReversalReceiptSearch)item).BankAccountID).Distinct().ToList();

                        foreach (int bankID in banksList)
                        {
                            newReceiptBatch = receiptBatch.Copy();
                            newReceiptBatch.BankAccountID = bankID;
                            newReceiptBatch.FilterDescription = "Bank: " + ((DishonourReversalReceiptSearch)selectedReceipts.Where(item => ((DishonourReversalReceiptSearch)item).BankAccountID == bankID).FirstOrDefault()).BankName;
                            newReceiptBatch.Receipts.Clear();

                            foreach (DishonourReversalReceiptSearch selectedReceipt in selectedReceipts.Where(item => ((DishonourReversalReceiptSearch)item).BankAccountID == bankID))
                            {
                                receipt = new Receipt();

                                if (selectedReceipt.ContractNo != null)
                                {
                                    receipt.ApplyToTypeID = (int)ReceiptApplyTo.Contract;
                                    receipt.InternalReference = selectedReceipt.ContractNo.ToString();
                                }
                                else
                                {
                                    receipt.ApplyToTypeID = (int)ReceiptApplyTo.Quote;
                                    receipt.InternalReference = selectedReceipt.QuoteID.ToString();
                                }
                                receipt.ContractID = selectedReceipt.ContractNo;
                                receipt.QuoteID = selectedReceipt.QuoteID;

                                if (receiptBatchType == ReceiptBatchType.Dishonour)
                                {
                                    receipt.Reference = "Dishonour";
                                }
                                else
                                {
                                    receipt.Reference = "Reversal";
                                }

                                receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                                receipt.LastDateModified = DateTime.Now;
                                receipt.ReceiptDate = receiptBatch.ReceiptDate;

                                receipt.NetAmountReceived = selectedReceipt.NetAmountReceived.GetValueOrDefault() * -1;
                                receipt.GSTAmountReceived = selectedReceipt.GSTAmountReceived.GetValueOrDefault() * -1;
                                receipt.FIDAmountReceived = selectedReceipt.FIDAmountReceived.GetValueOrDefault() * -1;
                                receipt.SDAmountReceived = selectedReceipt.SDAmountReceived.GetValueOrDefault() * -1;
                                receipt.GrossAmountReceived = selectedReceipt.AmountReceived * -1;

                                if (receiptBatchType == ReceiptBatchType.Dishonour)
                                {
                                    receipt.DishonourReceiptDetail = new DishonourReceiptDetail { DishonouredReceiptID = selectedReceipt.ReceiptID };
                                }
                                else
                                {
                                    receipt.ReversalReceiptDetail = new ReversalReceiptDetail { LinkedReceiptID = selectedReceipt.ReceiptID, ReversalTypeID = (int)ReversalTypes.Reversal };
                                }

                                foreach (OpenItemReceiptAllocation allocation in DishonourReversalFunctions.GetOpenItemReceiptAllocations(selectedReceipt.ReceiptID))
                                {
                                    newAllocation = new OpenItemReceiptAllocation();

                                    newAllocation.OpenItemID = allocation.OpenItemID;
                                    newAllocation.NetAmountApplied = allocation.NetAmountApplied * -1;
                                    newAllocation.GSTAmountApplied = allocation.GSTAmountApplied * -1;
                                    newAllocation.FIDAmountApplied = allocation.FIDAmountApplied * -1;
                                    newAllocation.SDAmountApplied = allocation.SDAmountApplied * -1;
                                    newAllocation.GrossAmountApplied = allocation.GrossAmountApplied * -1;
                                    receipt.OpenItemReceiptAllocations.Add(newAllocation);
                                }

                                newReceiptBatch.Receipts.Add(receipt);
                                newReceiptBatch.GrossBatchTotal += receipt.GrossAmountReceived;
                                newReceiptBatch.NumberOfEntries++;
                            }

                            ReceiptBatchFunctions.Save(newReceiptBatch);
                            ReturnValue = new NavigationItem() { ReceiptID = newReceiptBatch.ID, ReceiptText = newReceiptBatch.ID.ToString() + "-" + SelectedBatch, BatchStatus = (int)ReceiptBatchStatus.Created, BatchTypeID = newReceiptBatch.BatchTypeID };
                        }

                        Close();
                    }
                    else
                    {
                        ReceiptBatchFunctions.Save(receiptBatch);

                        if (isNew && createFromList.Where(from => from.ID == createFrom).FirstOrDefault().Description.ToLower().Trim() == ReceiptBatchCreateFrom.Import.ToString().ToLower().Trim())
                        {
                            ReturnValue = new NavigationItem() { ReceiptID = receiptBatch.ID, ReceiptText = receiptBatch.ID.ToString() + "-" + SelectedBatch, BatchStatus = (int)ReceiptBatchStatus.Created, BatchTypeID = receiptBatch.BatchTypeID };

                            IsBusy = true;
                            popupWindow = new PopupWindow(ViewAssemblies.ReceiptsViewAssembly, "DishonourReversal.DishonourReversalReceiptExcelImport", true);
                            popupWindow.Parameters.Add(receiptBatchType);
                            popupWindow.Parameters.Add(receiptBatch.ID);
                            Popup.Raise(popupWindow);
                            receiptBatch = ReceiptBatchFunctions.Get(receiptBatch.ID);
                            IsBusy = false;
                        }
                        else
                        {
                            ReturnValue = new NavigationItem() { ReceiptID = receiptBatch.ID, ReceiptText = receiptBatch.ID.ToString() + "-" + SelectedBatch, BatchStatus = (int)ReceiptBatchStatus.Created, BatchTypeID = receiptBatch.BatchTypeID };
                            Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogger.WriteLog(ex);
                ShowErrorMessage("Error encountered while Creating New Batch.", "Create Batch - Error");
            }
            finally
            {
                IsBusy = false;                     
            }
        }
        private void SaveReceiptBatch()
        {
            OpenItemReceiptAllocation newAllocation;
            Receipt receipt = null;

            try
            {
                isChanged = true;

                if (selectedReceipts != null)
                {
                    receiptBatch.BankAccountID = ((DishonourReversalReceiptSearch)selectedReceipts.FirstOrDefault()).BankAccountID;
                    receiptBatch.FilterDescription = "Bank: " + ((DishonourReversalReceiptSearch)selectedReceipts.FirstOrDefault()).BankName;

                    foreach (DishonourReversalReceiptSearch selectedReceipt in selectedReceipts)
                    {
                        receipt = new Receipt();

                        if (selectedReceipt.ContractNo != null)
                        {
                            receipt.ApplyToTypeID = (int)ReceiptApplyTo.Contract;
                            receipt.InternalReference = selectedReceipt.ContractNo.ToString();
                        }
                        else
                        {
                            receipt.ApplyToTypeID = (int)ReceiptApplyTo.Quote;
                            receipt.InternalReference = selectedReceipt.QuoteID.ToString();
                        }

                        receipt.ReceiptBatchID = receiptBatch.ID;
                        receipt.ContractID = selectedReceipt.ContractNo;
                        receipt.QuoteID = selectedReceipt.QuoteID;

                        if (batchType == (int)ReceiptBatchType.Dishonour)
                        {
                            receipt.Reference = "Dishonour";
                        }
                        else
                        {
                            receipt.Reference = "Reversal";
                        }

                        receipt.LastUserID = ((OperationsPrincipal)Thread.CurrentPrincipal).Identity.User.UserEntityId;
                        receipt.LastDateModified = DateTime.Now;

                        if (batchType == (int)ReceiptBatchType.Reversals)
                        {
                            receipt.ReceiptDate = selectedReceipt.ReceiptDate;
                        }
                        else
                        {
                            receipt.ReceiptDate = ReceiptDate;
                        }

                        receipt.NetAmountReceived = selectedReceipt.NetAmountReceived.GetValueOrDefault() * -1;
                        receipt.GSTAmountReceived = selectedReceipt.GSTAmountReceived.GetValueOrDefault() * -1;
                        receipt.FIDAmountReceived = selectedReceipt.FIDAmountReceived.GetValueOrDefault() * -1;
                        receipt.SDAmountReceived = selectedReceipt.SDAmountReceived.GetValueOrDefault() * -1;
                        receipt.GrossAmountReceived = selectedReceipt.AmountReceived * -1;

                        if (batchType == (int)ReceiptBatchType.Dishonour)
                        {
                            receipt.DishonourReceiptDetail = new DishonourReceiptDetail { DishonouredReceiptID = selectedReceipt.ReceiptID };
                        }
                        else
                        {
                            receipt.ReversalReceiptDetail = new ReversalReceiptDetail { LinkedReceiptID = selectedReceipt.ReceiptID, ReversalTypeID = (int)ReversalTypes.Reversal };
                        }

                        foreach (OpenItemReceiptAllocation allocation in DishonourReversalFunctions.GetOpenItemReceiptAllocations(selectedReceipt.ReceiptID))
                        {
                            newAllocation = new OpenItemReceiptAllocation();

                            newAllocation.OpenItemID = allocation.OpenItemID;
                            newAllocation.NetAmountApplied = allocation.NetAmountApplied * -1;
                            newAllocation.GSTAmountApplied = allocation.GSTAmountApplied * -1;
                            newAllocation.FIDAmountApplied = allocation.FIDAmountApplied * -1;
                            newAllocation.SDAmountApplied = allocation.SDAmountApplied * -1;
                            newAllocation.GrossAmountApplied = allocation.GrossAmountApplied * -1;
                            receipt.OpenItemReceiptAllocations.Add(newAllocation);
                        }

                        receiptBatch.Receipts.Add(receipt);
                        receiptBatch.GrossBatchTotal += receipt.GrossAmountReceived;
                        receiptBatch.NumberOfEntries++;
                    }

                    ReceiptBatchFunctions.Save(receiptBatch);
                }

                Close();
            }
            catch (Exception ex)
            {
                ExceptionLogger.WriteLog(ex);
                ShowErrorMessage("Error encountered while Saving receipt entries.", "Add Receipts - Error");
            }
            finally
            {
                IsBusy = false;
            }
        }