public void AddInvoiceIntoQBLog(int invoiceId, decimal totalAvgCostAmt)
        {
            // Check duplicate log.
            smART.Model.QBLog parentQBLog = _repository.Find <Model.QBLog>(o => o.Source_ID == invoiceId && (o.Source_Type == "Invoice")).FirstOrDefault();
            if (parentQBLog != null)
            {
                return;
            }

            // Get invoice.
            string[]      predicates = { "Booking.Sales_Order_No.Party", "Sales_Order_No.Party" };
            Model.Invoice entity     = _repository.Find <Model.Invoice>(o => o.ID == invoiceId, predicates).FirstOrDefault();

            if (entity != null && entity.Invoice_Status == "Closed")
            {
                // Get last entry to generate group.
                Model.QBLog latestQBLog = _repository.GetAll <Model.QBLog>().LastOrDefault();
                int         group       = latestQBLog != null ? latestQBLog.Group + 1 : 1;

                if (entity.Invoice_Type == "Exports")
                {
                    AddExportInvoiceIntoQBLog(entity, group, totalAvgCostAmt);
                }
            }
        }
        //public override VModel.PaymentReceipt Add(VModel.PaymentReceipt addObject) {
        //  VModel.PaymentReceipt insertedObjectBusiness = addObject;
        //  try {
        //    Model.PaymentReceipt newModObject = Mapper.Map<VModel.PaymentReceipt, Model.PaymentReceipt>(addObject);

        //    newModObject.Party = _repository.GetQuery<Model.Party>().SingleOrDefault(o => o.ID == addObject.Party.ID);
        //    newModObject.Account_Name = _repository.GetQuery<Model.Bank>().SingleOrDefault(o => o.ID == addObject.Account_Name.ID);

        //    if (addObject.Booking != null)
        //      newModObject.Booking = _repository.GetQuery<Model.Booking>().SingleOrDefault(o => o.ID == addObject.Booking.ID);

        //    if (addObject.Party_Address != null)
        //      newModObject.Party_Address = _repository.GetQuery<Model.AddressBook>().SingleOrDefault(o => o.ID == addObject.Party_Address.ID);

        //    if (Adding(insertedObjectBusiness, newModObject, _dbContext)) {

        //      Model.PaymentReceipt insertedObject = _repository.Add<Model.PaymentReceipt>(newModObject);
        //      _repository.SaveChanges();

        //      insertedObjectBusiness = Mapper.Map<Model.PaymentReceipt, VModel.PaymentReceipt>(insertedObject);
        //      Added(insertedObjectBusiness, newModObject, _dbContext);

        //    }
        //  }
        //  catch (Exception ex) {
        //    bool rethrow;
        //    rethrow = LibraryExceptionHandler.HandleException(ref ex, insertedObjectBusiness.Updated_By, insertedObjectBusiness.GetType().Name, insertedObjectBusiness.ID.ToString());
        //    if (rethrow)
        //      throw ex;
        //  }
        //  return insertedObjectBusiness;
        //}

        //protected override void Modify(Expression<Func<Model.PaymentReceipt, bool>> predicate, VModel.PaymentReceipt modObject, string[] includePredicate = null) {
        //  try {
        //    Model.PaymentReceipt newModObject = Mapper.Map<VModel.PaymentReceipt, Model.PaymentReceipt>(modObject);

        //    newModObject.Party = _repository.GetQuery<Model.Party>().SingleOrDefault(o => o.ID == modObject.Party.ID);
        //    newModObject.Account_Name = _repository.GetQuery<Model.Bank>().SingleOrDefault(o => o.ID == modObject.Account_Name.ID);

        //    if (modObject.Booking != null)
        //      newModObject.Booking = _repository.GetQuery<Model.Booking>().SingleOrDefault(o => o.ID == modObject.Booking.ID);

        //    if (modObject.Party_Address!= null)
        //      newModObject.Party_Address = _repository.GetQuery<Model.AddressBook>().SingleOrDefault(o => o.ID == modObject.Party_Address.ID);

        //    if (Modifying(modObject, newModObject, _dbContext)) {
        //      _repository.Modify<Model.PaymentReceipt>(predicate, newModObject, includePredicate);
        //      _repository.SaveChanges();
        //      Modified(modObject, newModObject, _dbContext);
        //    }
        //  }
        //  catch (Exception ex) {
        //    bool rethrow;
        //    rethrow = LibraryExceptionHandler.HandleException(ref ex, modObject.Updated_By, modObject.GetType().Name, modObject.ID.ToString());
        //    if (rethrow)
        //      throw ex;
        //  }
        //}

        //public IEnumerable<VModel.PaymentReceipt> GetReceiptsByPagging(out int totalRows, int page, int pageSize, string sortColumn, string sortType, string[] includePredicate = null, IList<IFilterDescriptor> filters = null) {
        //  string trasnType = EnumTransactionType.Receipt.ToString();
        //  IEnumerable<Model.PaymentReceipt> modEnumeration = _repository.FindByPaging<Model.PaymentReceipt>(out totalRows, o => o.Transaction_Type == trasnType,
        //                                                                                  page, pageSize, sortColumn, sortType, includePredicate, filters);
        //  IEnumerable<VModel.PaymentReceipt> busEnumeration = Map(modEnumeration);
        //  return busEnumeration;
        //}

        //public IEnumerable<VModel.PaymentReceipt> GetPaymentsByPagging(out int totalRows, int page, int pageSize, string sortColumn, string sortType, string[] includePredicate = null, IList<IFilterDescriptor> filters = null) {
        //  string trasnType = EnumTransactionType.Payment.ToString();
        //  IEnumerable<Model.PaymentReceipt> modEnumeration = _repository.FindByPaging<Model.PaymentReceipt>(out totalRows, o => o.Transaction_Type == trasnType,
        //                                                                                  page, pageSize, sortColumn, sortType, includePredicate, filters);
        //  IEnumerable<VModel.PaymentReceipt> busEnumeration = Map(modEnumeration);

        //  return busEnumeration;

        //private void AddInQBLog(smART.ViewModel.PaymentReceipt businessEntity, smART.Model.PaymentReceipt modelEntity, smART.Model.smARTDBContext dbContext) {
        //  try {

        //    if (businessEntity.Active_Ind == true && businessEntity.Transaction_Status == "Closed") {
        //      // Add in QBLog
        //      smART.Model.QBLog qbLog = new Model.QBLog();
        //      qbLog.Source_ID = businessEntity.ID;
        //      qbLog.Parent_ID = businessEntity.ID;
        //      qbLog.Account_No = "";
        //      qbLog.Account_Name = "Gold Star Metalex";
        //      qbLog.Created_By = businessEntity.Created_By;
        //      qbLog.Created_Date = businessEntity.Created_Date;
        //      qbLog.Last_Updated_Date = businessEntity.Last_Updated_Date;
        //      qbLog.Updated_By = businessEntity.Updated_By;
        //      qbLog.Debit_Amt = businessEntity.Applied_Amount;
        //      qbLog.Remarks = string.Format("Payment ID: {0}", businessEntity.ID);
        //      qbLog = dbContext.T_QB_Log.Add(qbLog);
        //      dbContext.SaveChanges();
        //      // Update QBstatus
        //    }
        //  }
        //  catch (Exception ex) {
        //    bool rethrow;
        //    rethrow = BusinessRuleExceptionHandler.HandleException(ref ex, businessEntity.Updated_By, businessEntity.GetType().Name, businessEntity.ID.ToString());
        //    if (rethrow)
        //      throw ex;
        //  }
        //}

        private Model.QBLog AddInQBLog(smART.Model.QBLog newModObject, smART.Model.BaseEntity targetEntity)
        {
            // Add in QBLog
            newModObject.Created_By        = targetEntity.Created_By;
            newModObject.Created_Date      = DateTime.Now;
            newModObject.Last_Updated_Date = DateTime.Now;
            newModObject.Updated_By        = targetEntity.Updated_By;
            newModObject.Status            = smART.Common.EnumPostingStatus.Pending.ToString();
            _repository.Add <Model.QBLog>(newModObject);
            _repository.SaveChanges();
            return(newModObject);
        }
        public void AddPaymentReceiptIntoQBLog(int paymentId)
        {
            // Check duplicate log.
            smART.Model.QBLog parentQBLog = _repository.Find <Model.QBLog>(o => o.Source_ID == paymentId && (o.Source_Type == "Payment" || o.Source_Type == "Receipt")).FirstOrDefault();
            if (parentQBLog != null)
            {
                return;
            }

            // Get payment.
            string[]             paymentPredicates = { "Party", "Account_Name" };
            Model.PaymentReceipt entity            = _repository.Find <Model.PaymentReceipt>(o => o.ID == paymentId, paymentPredicates).FirstOrDefault();

            if (entity != null && entity.Transaction_Status == "Closed")
            {
                // Get last entry to generate group.
                Model.QBLog latestQBLog = _repository.GetAll <Model.QBLog>().LastOrDefault();
                int         group       = latestQBLog != null ? latestQBLog.Group + 1 : 1;

                if (entity.Transaction_Type == "Payment")
                {
                    // For Scale Ticket Payment.
                    if (entity.Payment_Receipt_Type == "Tickets")
                    {
                        AddScalePaymentIntoQBLog(entity, group);
                    }
                    //// For Expense Payment.
                    //else if (entity.Payment_Receipt_Type == "Expenses")
                    //  AddExpensePaymentIntoQBLog(entity, group);
                }
                else if (entity.Transaction_Type == "Receipt")
                {
                    AddReceiptIntoQBLog(entity, group);
                }
            }
        }