예제 #1
0
        /// <summary>
        /// Tính toán chi phí cho repeater item lưu thông tin expense
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private Expense ExpenseCalculate(RepeaterItemEventArgs e)
        {
            #region -- Thông tin chung của expense --

            Expense expense = (Expense)e.Item.DataItem;
            // Khi tính chi phí thì chỉ tính theo khách đã check-in
            ICriterion criterion = Expression.And(Expression.Eq("StartDate", expense.Date.Date),
                                                  Expression.Eq("Status", StatusType.Approved));
            // Bỏ deleted và cả transfer
            criterion = Expression.And(Expression.Eq("Deleted", false), criterion);
            criterion = Expression.And(Expression.Eq("IsTransferred", false), criterion);

            // Nếu là trang báo cáo chi phí từng tàu thì chỉ lấy theo tàu đó
            if (ActiveCruise != null)
            {
                criterion = Expression.And(criterion, Expression.Eq("Cruise", ActiveCruise));
            }

            IList bookings =
                Module.BookingGetByCriterion(criterion, null, 0, 0);

            int adult = 0;
            int child = 0;
            //int baby = 0;
            foreach (Booking booking in bookings)
            {
                adult += booking.Adult;
                child += booking.Child;
            }
            _pax += adult + child;

            #endregion

            if (ActiveCruise != null)
            {
                GetCurrentCruiseTable(expense.Date, ActiveCruise);
            }

            _currentTotal = 0; // Tổng cho ngày hiện tại

            // Chi phí tháng/năm chỉ được tính vào chi phí ngày nếu cho phép tính trung bình
            if (PeriodExpenseAvg)
            {
                #region -- Chi phí tháng --

                // Nếu là tính chi phí cho một tàu thì chia chi phí bình thường
                if (expense.Cruise != null)
                {
                    // Nếu có chạy hoặc là tháng chưa kết thúc
                    if (bookings.Count > 0 ||
                        expense.Date.Month + expense.Date.Year * 12 >= DateTime.Today.Month + DateTime.Today.Year * 12)
                    {
                        // Tính chi phí tháng
                        Literal litMonth = e.Item.FindControl("litMonth") as Literal;
                        if (litMonth != null)
                        {
                            DateTime dateMonth = new DateTime(expense.Date.Year, expense.Date.Month, 1);
                            if (!_monthExpense.ContainsKey(dateMonth))
                            {
                                int runcount; // Số ngày tàu chạy trong tháng, chỉ phục vụ việc tính chi phí trung bình
                                // Không cần tính lại trong mỗi lần lặp
                                // Nếu là tháng chưa kết thúc
                                if (dateMonth.AddMonths(1) > DateTime.Today)
                                {
                                    runcount = dateMonth.AddMonths(1).Subtract(dateMonth).Days;
                                }
                                else
                                {
                                    runcount = Module.RunningDayCount(expense.Cruise, expense.Date.Year,
                                                                      expense.Date.Month);
                                }

                                Expense monthExpense = Module.ExpenseGetByDate(expense.Cruise, dateMonth);
                                if (monthExpense.Id < 0)
                                {
                                    Module.SaveOrUpdate(monthExpense);
                                }
                                double total = Module.CopyMonthlyCost(monthExpense);
                                _monthExpense.Add(dateMonth, total / runcount);
                            }

                            litMonth.Text  = _monthExpense[dateMonth].ToString("#,0.#");
                            _month        += _monthExpense[dateMonth];
                            _currentTotal += _monthExpense[dateMonth];
                        }
                    }
                }
                else // Nếu là tính chi phí tổng hợp thì tính cho tất cả các tàu rồi cộng lại
                {
                    IList  cruises  = Module.CruiseGetAll();
                    double monthAll = 0; // tổng chi phí tháng trung bình
                    foreach (Cruise cruise in cruises)
                    {
                        DateTime dateMonth = new DateTime(expense.Date.Year, expense.Date.Month, 1);
                        // Nếu chưa có bảng chi phí cho tàu hiện tại
                        if (!_monthExpenseCruise.ContainsKey(cruise))
                        {
                            _monthExpenseCruise.Add(cruise, new Dictionary <DateTime, double>());
                            // Tạo một từ điển trắng để lưu dữ liệu
                        }

                        // Nếu chưa có chi phí của tàu hiện tại trong tháng hiện tại
                        if (!_monthExpenseCruise[cruise].ContainsKey(dateMonth))
                        {
                            int runcount;
                            // Nếu là tháng chưa kết thúc
                            if (dateMonth.AddMonths(1) > DateTime.Today)
                            {
                                runcount = dateMonth.AddMonths(1).Subtract(dateMonth).Days;
                            }
                            else
                            {
                                runcount = Module.RunningDayCount(cruise, expense.Date.Year, expense.Date.Month);
                            }

                            Expense monthExpense = Module.ExpenseGetByDate(cruise, dateMonth);
                            if (monthExpense.Id < 0)
                            {
                                Module.SaveOrUpdate(monthExpense);
                            }
                            double total = Module.CopyMonthlyCost(monthExpense);
                            _monthExpenseCruise[cruise].Add(dateMonth, total / runcount);
                        }

                        bool isRun = false;
                        if (dateMonth.AddMonths(1) <= DateTime.Today)
                        {
                            foreach (Booking booking in bookings)
                            {
                                if (booking.Cruise == cruise)
                                {
                                    isRun = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            isRun = true; // Nếu là tháng chưa kết thúc thì mặc định là mọi ngày tàu đều chạy
                        }


                        if (isRun)
                        {
                            monthAll += _monthExpenseCruise[cruise][dateMonth];
                            // cộng thêm chi phí cho tàu này, ngày này khi tàu có chạy
                        }
                    }

                    _month += monthAll;

                    Literal litMonth = e.Item.FindControl("litMonth") as Literal;
                    if (litMonth != null)
                    {
                        litMonth.Text = monthAll.ToString("#,0.#");
                    }
                }

                #endregion

                #region -- Chi phí năm --

                if (expense.Cruise != null)
                {
                    // Nếu có chạy hoặc năm chưa kết thúc
                    if (bookings.Count > 0 || expense.Date.Year >= DateTime.Today.Year)
                    {
                        // Tính chi phí năm
                        Literal litYear = e.Item.FindControl("litYear") as Literal;
                        if (litYear != null)
                        {
                            DateTime dateYear = new DateTime(expense.Date.Year, 1, 1);
                            int      runcount;
                            // Nếu là năm chưa kết thúc
                            if (dateYear.AddYears(1) > DateTime.Today)
                            {
                                runcount = dateYear.AddYears(1).Subtract(dateYear).Days;
                            }
                            else
                            {
                                runcount = Module.RunningDayCount(expense.Cruise, expense.Date.Year, 0);
                            }
                            if (!_yearExpense.ContainsKey(dateYear))
                            {
                                Expense yearExpense = Module.ExpenseGetByDate(expense.Cruise, dateYear);
                                if (yearExpense.Id < 0)
                                {
                                    Module.SaveOrUpdate(yearExpense);
                                }
                                double total = Module.CopyYearlyCost(yearExpense);
                                _yearExpense.Add(dateYear, total / runcount);
                            }

                            litYear.Text   = _yearExpense[dateYear].ToString("#,0.#");
                            _year         += _yearExpense[dateYear];
                            _currentTotal += _yearExpense[dateYear];
                        }
                    }
                }
                else
                {
                    IList  cruises = Module.CruiseGetAll();
                    double yearAll = 0; // tổng chi phí tháng trung bình
                    foreach (Cruise cruise in cruises)
                    {
                        DateTime dateYear = new DateTime(expense.Date.Year, 1, 1);

                        // Nếu chưa có bảng chi phí cho tàu hiện tại
                        if (!_yearExpenseCruise.ContainsKey(cruise))
                        {
                            _yearExpenseCruise.Add(cruise, new Dictionary <DateTime, double>());
                            // Tạo một từ điển trắng để lưu dữ liệu
                        }

                        // Nếu chưa có chi phí của tàu hiện tại trong năm hiện tại
                        if (!_yearExpenseCruise[cruise].ContainsKey(dateYear))
                        {
                            int runcount;
                            // Nếu là năm chưa kết thúc
                            if (dateYear.AddYears(1) > DateTime.Today)
                            {
                                runcount = dateYear.AddYears(1).Subtract(dateYear).Days;
                            }
                            else
                            {
                                runcount = Module.RunningDayCount(cruise, expense.Date.Year, 0);
                            }

                            Expense yearExpense = Module.ExpenseGetByDate(cruise, dateYear);
                            if (yearExpense.Id < 0)
                            {
                                Module.SaveOrUpdate(yearExpense);
                            }
                            double total = Module.CopyYearlyCost(yearExpense);

                            _yearExpenseCruise[cruise].Add(dateYear, total / runcount);
                        }

                        bool isRun = false;
                        if (dateYear.AddYears(1) <= DateTime.Today)
                        {
                            foreach (Booking booking in bookings)
                            {
                                if (booking.Cruise == cruise)
                                {
                                    isRun = true;
                                    break;
                                }
                            }
                        }
                        else
                        {
                            isRun = true; // Nếu là năm chưa kết thúc thì mặc định là mọi ngày tàu đều chạy
                        }


                        if (isRun)
                        {
                            yearAll += _yearExpenseCruise[cruise][dateYear];
                            // cộng thêm chi phí cho tàu này, ngày này khi tàu có chạy
                        }
                    }

                    _year += yearAll;

                    Literal litYear = e.Item.FindControl("litYear") as Literal;
                    if (litYear != null)
                    {
                        litYear.Text = yearAll.ToString("#,0.#");
                    }
                }

                #endregion
            }

            if (_cruiseTable == null && ActiveCruise != null)
            {
                throw new Exception("Hai phong cruise price table is out of valid");
            }

            // Lấy về bảng giá đã tính
            _currentCostMap = expense.Calculate(AllCostTypes, GetCurrentTable, GetCurrentDailyTable, GetCurrentCruiseTable,
                                                expense.Cruise,
                                                bookings, Module, PartnershipManager);
            return(expense);
        }