Exemplo n.º 1
0
        // refresh latest payment data
        // this table data is sensitive with high update rate env
        // TODO: ensure that high transaction rate will not affect this table
        public static int UpdatePaymentByCourse(DBManager db, Course course)
        {
            // load all courses in the same group
            Course[] coursesSameGroup = Course.LoadListFromDB(db, " WHERE paid_group_id=" + course._paidGroupID + " ORDER BY " + ORDER_BY);

            int allIncome = 0; // in the same group

            foreach (Course c in coursesSameGroup)
            {
                c.LoadPaidGroup(db);

                int            thisIncome = 0;
                Registration[] reg        = Registration.LoadListFromDB(db, " WHERE course_id=" + c._courseID);
                for (int i = 0; i < reg.Length; i++)
                {
                    thisIncome += reg[i]._discountedCost;
                }

                Payment payment = new Payment();
                if (!payment.LoadFromDB(db, " course_id=" + c._courseID)) // not found, add new
                {
                    payment._courseID      = c._courseID;
                    payment._sumAllCost    = thisIncome;
                    payment._sumMaxPayable = c._paidGroup.GetMaxPayableByRate(allIncome, thisIncome);
                    payment._sumPaidCost   = 0;
                    payment._lastPaidDate  = DateTime.Now;
                    payment._paidRound     = c._paidGroup._currentRound;
                    payment._status        = Payment.STATUS_OK;
                    payment.AddToDB(db);
                }
                else
                {
                    // collect historical data
                    payment.LoadHistory(db);

                    payment._sumAllCost    = thisIncome;
                    payment._sumMaxPayable = c._paidGroup.GetMaxPayableByRate(allIncome, thisIncome);
                    payment._sumPaidCost   = payment.GetHistoricalSumPaidCost();
                    payment._lastPaidDate  = payment.GetLatestPaidDate();
                    payment._paidRound     = c._paidGroup._currentRound;
                    payment._status        = Payment.STATUS_OK;
                    payment.UpdateToDB(db);
                }
                allIncome += thisIncome;
            }
            return(0);
        }