예제 #1
0
        public static void AddPoints(Models.Customers.Customer customer, Models.Customers.LoyaltyPartner partner, long points, string comments, Models.Core.User user)
        {
            if (points > 0)
            {
                using (var session = DataConnector.NHibernateConnector.OpenSession())
                {
                    using (var dbTransaction = session.BeginTransaction())
                    {
                        try
                        {
                            Models.Customers.LoyaltyTransaction transaction = new Models.Customers.LoyaltyTransaction();

                            transaction.TransactionDate = DateTime.Now;
                            if (points > 0)
                            {
                                transaction.PointsDestination = partner;
                            }
                            if (points < 0)
                            {
                                transaction.PointsOrigin = partner;
                            }
                            transaction.Points   = points;
                            transaction.User     = user;
                            transaction.Comments = comments;
                            transaction.Customer = customer;

                            session.Save(transaction);

                            var balanceObject = session.CreateCriteria(typeof(Models.Customers.LoyaltyBalance))
                                                .Add(Expression.Eq("Customer", customer))
                                                .Add(Expression.Eq("Partner", partner))
                                                .UniqueResult <Models.Customers.LoyaltyBalance>();

                            if (balanceObject == null)
                            {
                                balanceObject          = new Models.Customers.LoyaltyBalance();
                                balanceObject.Customer = customer;
                                balanceObject.Partner  = partner;
                                balanceObject.Points   = 0;
                            }

                            balanceObject.Points = balanceObject.Points + points;

                            session.SaveOrUpdate(balanceObject);

                            dbTransaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            dbTransaction.Rollback();
                            throw ex;
                        }
                    }
                }
            }
            else if (points < 0)
            {
                UsePoints(customer, partner, points, comments);
            }
        }
예제 #2
0
        public static void MovePoints(Models.Customers.Customer customer, Models.Customers.LoyaltyPartner fromPartner, Models.Customers.LoyaltyPartner toPartner, long points)
        {
            if (points != 0)
            {
                using (var session = DataConnector.NHibernateConnector.OpenSession())
                {
                    using (var dbTransaction = session.BeginTransaction())
                    {
                        try
                        {
                            Models.Customers.LoyaltyTransaction transaction = new Models.Customers.LoyaltyTransaction();

                            transaction.TransactionDate   = DateTime.Now;
                            transaction.PointsOrigin      = fromPartner;
                            transaction.PointsDestination = toPartner;
                            transaction.Points            = points;
                            transaction.Customer          = customer;

                            session.Save(transaction);

                            // Add points to destination partner balance

                            var balanceObjectDestination = session.CreateCriteria(typeof(Models.Customers.LoyaltyBalance))
                                                           .Add(Expression.Eq("Customer", customer))
                                                           .Add(Expression.Eq("Partner", toPartner))
                                                           .UniqueResult <Models.Customers.LoyaltyBalance>();

                            if (balanceObjectDestination == null)
                            {
                                balanceObjectDestination          = new Models.Customers.LoyaltyBalance();
                                balanceObjectDestination.Customer = customer;
                                balanceObjectDestination.Partner  = toPartner;
                                balanceObjectDestination.Points   = 0;
                            }

                            balanceObjectDestination.Points = balanceObjectDestination.Points + points;
                            session.SaveOrUpdate(balanceObjectDestination);

                            // Take points from origin partner balance

                            var balanceObjectOrigin = session.CreateCriteria(typeof(Models.Customers.LoyaltyBalance))
                                                      .Add(Expression.Eq("Customer", customer))
                                                      .Add(Expression.Eq("Partner", fromPartner))
                                                      .UniqueResult <Models.Customers.LoyaltyBalance>();

                            if (balanceObjectOrigin == null)
                            {
                                balanceObjectOrigin          = new Models.Customers.LoyaltyBalance();
                                balanceObjectOrigin.Customer = customer;
                                balanceObjectOrigin.Partner  = fromPartner;
                                balanceObjectOrigin.Points   = 0;
                            }

                            balanceObjectOrigin.Points = balanceObjectOrigin.Points - points;
                            session.SaveOrUpdate(balanceObjectOrigin);

                            dbTransaction.Commit();
                        }
                        catch (Exception ex)
                        {
                            dbTransaction.Rollback();
                            throw ex;
                        }
                    }
                }
            }
        }