Exemple #1
0
    def get(self):
        self.set_header("Content-Type", "application/json")
        self.set_header('Access-Control-Allow-Origin', '*')

        ret = {}

        try:
            session = Session()
            masterdao = MasterDAO()

            masters = []

            master_ids = masterdao.get_all_master_ids()
            for mid in master_ids:
                master_info = {}

                basic = masterdao.get_master_basic_info(mid)
                rating_clean, rating_master = masterdao.get_master_rating(mid)
                total_salary = masterdao.get_master_total_granted_salary(mid)
                last_month_salary = masterdao.get_master_last_month_granted_salary(
                    mid)
                prefered_area = masterdao.get_master_prefered_area(mid)
                start_times, end_times = masterdao.get_master_working_time(mid)

                working_startdate = masterdao.get_master_working_start_date(
                    mid)
                completed_cleaning_count = masterdao.get_master_completed_cleaning_count(
                    mid)

                bank_name, bank_code, account_no = masterdao.get_master_account(
                    mid)

                master_info['master_id'] = mid
                master_info['name'] = basic['name']
                master_info['phone'] = basic['phone']
                master_info['img'] = basic['img']
                master_info['age'] = basic['age']
                master_info['gender'] = basic['gender']
                master_info['address'] = basic['address']
                master_info['pet_alergy'] = basic['pet_alergy']
                master_info['manager_name'] = basic['manager_name']
                master_info['manager_phone'] = basic['manager_phone']
                master_info['cardinal'] = basic['cardinal']
                master_info['level'] = basic['level']
                master_info['need_route'] = basic['need_route']
                master_info['t_size'] = basic['t_size']
                master_info['rating_clean'] = float(rating_clean)
                master_info['rating_master'] = float(rating_master)
                master_info['total_salary'] = int(total_salary)
                master_info['last_month_salary'] = int(last_month_salary)
                master_info['prefered_area'] = prefered_area
                master_info['prefered_area_list'] = [
                    area for area in prefered_area.split(',')
                ]
                master_info['start_times'] = start_times
                master_info['end_times'] = end_times
                master_info['active'] = basic['active']

                master_info['working_startdate'] = working_startdate
                master_info['cleaning_count'] = completed_cleaning_count
                master_info['account'] = {
                    'name': bank_name,
                    'code': bank_code,
                    'account_no': account_no
                }

                masters.append(master_info)

            ret['response'] = masters
            self.set_status(Response.RESULT_OK)

        except Exception, e:
            session.rollback()

            print_err_detail(e)
            self.set_status(Response.RESULT_SERVERERROR)
            add_err_message_to_response(ret, err_dict['err_mysql'])
Exemple #2
0
    def get(self):
        self.set_header("Content-Type", "application/json")
        week = self.get_argument('week', 0)  # n weeks before
        week = int(week)

        ret = {}

        FRIDAY = 4
        SATURDAY = 5
        CANCELED_RATE = 0.5

        try:
            session = Session()
            userdao = UserDAO()
            masterdao = MasterDAO()
            bookingdao = BookingDAO()

            # get last saturday
            now = dt.datetime.now()
            if week != 0:
                offset = ((now.weekday() - FRIDAY) % 7) + (week - 1) * 7
                now -= dt.timedelta(days=offset)

            offset = (now.weekday() - SATURDAY) % 7
            last_saturday = (now - dt.timedelta(days=offset)).date()
            now = now.date()

            result = session.query(Booking, Master, User, UserAddress) \
                            .join(Master, Booking.master_id == Master.id) \
                            .join(User, Booking.user_id == User.id) \
                            .join(UserAddress, and_(Booking.user_id == UserAddress.user_id, Booking.addr_idx == UserAddress.user_addr_index)) \
                            .filter(Master.active != 0) \
                            .filter(and_(func.date(Booking.start_time) >= last_saturday, func.date(Booking.start_time) <= now)) \
                            .filter(or_(Booking.cleaning_status == BC.BOOKING_COMPLETED, \
                            and_(Booking.cleaning_status == BC.BOOKING_CANCELED, \
                            or_(Booking.payment_status == BC.BOOKING_CANCELED_REFUND, \
                            Booking.payment_status == BC.BOOKING_CANCELED_CHARGE)))) \
                            .order_by(Booking.master_id, Booking.start_time) \
                            .all()

            weekly_salary = 0

            master_salaries = []

            start_period = last_saturday
            end_period = now

            last_saturday = dt.datetime.strftime(last_saturday, '%Y%m%d')
            now = dt.datetime.strftime(now, '%Y%m%d')

            prev_master_id = None

            for row in result:
                if row.Booking.user_id == '81c2f5c1-7295-489d-8bb0-334121060ae3':
                    continue

                user_name = userdao.get_user_name(row.Booking.user_id)
                org_price = row.Booking.price_with_task
                status = row.Booking.cleaning_status
                appointment_index = row.Booking.appointment_index
                appointment_type = row.Booking.appointment_type
                start_time = row.Booking.start_time
                end_time = row.Booking.estimated_end_time
                cleaning_duration = row.Booking.cleaning_duration / 6
                charging_price = row.Booking.charging_price
                is_dirty = row.Booking.is_dirty
                is_b2b = row.User.is_b2b

                duration_in_minutes = (
                    end_time - start_time
                ).seconds / 360  # 계산을 단순하게 하기 위해 60 * 60이 아닌 60 * 6으로 나눔. 그뒤 10배 커지는 것을 방지하기 위해 시급에서 10 나눈 값만 곱함
                minutes_for_salary = duration_in_minutes

                #if duration_in_minutes > cleaning_duration: # 30분의 시간이 더 더해지는 경우가 존재. 그 경우, 해당 시간은 임금에 반영 되지 않음
                #    if appointment_index == 1 and (appointment_type == BC.ONE_TIME_A_MONTH or appointment_type == BC.TWO_TIME_A_MONTH or appointment_type == BC.FOUR_TIME_A_MONTH):
                #        minutes_for_salary = duration_in_minutes - 5

                if is_dirty == 1:
                    minutes_for_salary -= 20

                house_type = row.UserAddress.kind
                house_size = row.UserAddress.size

                extra_charge = 0
                if row.Booking.cleaning_status == BC.BOOKING_COMPLETED:
                    if is_b2b:
                        weekly_salary = int(minutes_for_salary *
                                            (row.Booking.wage_per_hour / 10))
                    else:
                        # 오피스텔 13평 이하, 주택 7평 이하는 시급 14000
                        weekly_salary = 0
                        if start_time >= dt.datetime(2017, 1, 1):
                            weekly_salary = minutes_for_salary * BC.SALARY_FOR_ONETIME_IN_HOUR
                        else:
                            if appointment_type in BC.REGULAR_CLEANING_DICT:
                                weekly_salary = minutes_for_salary * BC.SALARY_FOR_REGULAR_IN_HOUR
                            else:
                                weekly_salary = minutes_for_salary * BC.SALARY_FOR_ONETIME_IN_HOUR

                        if start_time.weekday(
                        ) in BC.WEEKEND and start_time >= dt.datetime(
                                2016, 12, 17):
                            weekly_salary += BC.WEEKEND_ADDED_SALARY

                    extra_charge = bookingdao.get_extra_charge(row.Booking.id)
                    weekly_salary += extra_charge

                elif row.Booking.payment_status == BC.BOOKING_CANCELED_CHARGE or \
                    row.Booking.payment_status == BC.BOOKING_CANCELED_REFUND:
                    weekly_salary = int(charging_price * CANCELED_RATE)

                if prev_master_id == None or prev_master_id != row.Booking.master_id:  # 변함
                    salary = {}
                    salary_detail = []
                    bank_name, bank_code, account_no = masterdao.get_master_account(
                        row.Booking.master_id)

                    salary['weekly_salary'] = 0
                    salary['master_name'] = row.Master.name
                    salary['master_account'] = {
                        'bank_name': bank_name,
                        'bank_code': bank_code,
                        'account_no': account_no
                    }
                    salary['master_phone'] = row.Master.phone
                    salary['weekly_salary'] += weekly_salary
                    salary[
                        'penalty_amount'] = 0  #masterdao.get_master_penalties(row.Booking.master_id, start_period, end_period)

                    if weekly_salary > 0:
                        salary_detail.append({
                            'org_price':
                            org_price,
                            'charging_price':
                            charging_price,
                            'salary':
                            weekly_salary,
                            'extra_charge':
                            extra_charge,
                            'status':
                            status,
                            'start_time':
                            dt.datetime.strftime(start_time, '%Y-%m-%d %H:%M'),
                            'end_time':
                            dt.datetime.strftime(end_time, '%Y-%m-%d %H:%M'),
                            'cleaning_index':
                            appointment_index,
                            'user_name':
                            user_name
                        })

                    salary['salary_detail'] = salary_detail

                    master_salaries.append(salary)
                else:
                    salary['weekly_salary'] += weekly_salary
                    if weekly_salary > 0:
                        salary_detail.append({
                            'org_price':
                            org_price,
                            'charging_price':
                            charging_price,
                            'salary':
                            weekly_salary,
                            'extra_charge':
                            extra_charge,
                            'status':
                            status,
                            'start_time':
                            dt.datetime.strftime(start_time, '%Y-%m-%d %H:%M'),
                            'end_time':
                            dt.datetime.strftime(end_time, '%Y-%m-%d %H:%M'),
                            'cleaning_index':
                            appointment_index,
                            'user_name':
                            user_name
                        })

                prev_master_id = row.Master.id

            total_salaries = 0
            for ms in master_salaries:
                if start_time >= dt.datetime(2017, 1, 1):
                    ms['weekly_salary'] = int(ms['weekly_salary'])
                else:
                    ms['weekly_salary'] = int(ms['weekly_salary'] * 0.967)
                total_salaries += (ms['weekly_salary'] - ms['penalty_amount'])
                ms['actual_weekly_salary'] = '{:,}'.format(
                    ms['weekly_salary'] - ms['penalty_amount'])

            total_salaries = '{:,}'.format(total_salaries)

            master_salaries = sorted(master_salaries,
                                     key=lambda x: (x['master_name']))

            ret['response'] = {
                'date_from': last_saturday,
                'date_to': now,
                'master_salaries': master_salaries,
                'total_salaries': total_salaries
            }
            self.set_status(Response.RESULT_OK)

        except Exception, e:
            session.rollback()

            print_err_detail(e)
            self.set_status(Response.RESULT_SERVERERROR)
            add_err_message_to_response(ret, err_dict['err_mysql'])
Exemple #3
0
    def post(self):
        self.set_header("Content-Type", "application/json")
        self.set_header('Access-Control-Allow-Origin', '*')

        search_term = self.get_argument('search_term', '')

        ret = {}

        try:
            session = Session()
            masterdao = MasterDAO()

            if search_term == None or search_term == '' or len(search_term) == 1 or search_term == '010':
                self.set_status(Response.RESULT_OK)
                add_err_ko_message_to_response(ret, '잘못된 파라미터 입니다.')
                return

            print 'searching master term : ' + search_term

            masters = []
            results  = session.query(Master) \
                                    .filter(or_(Master.phone.like(search_term + '%'), Master.name.like('%' + search_term + '%'))) \
                                    .filter(Master.active != 0) \
                                    .all()

            print results

            total_counts = 0
            for result in results:
                mid = result.id
                print mid
                master_info = {}

                basic = masterdao.get_master_basic_info(mid)
                rating_clean, rating_master = masterdao.get_master_rating(mid)
                total_salary = masterdao.get_master_total_granted_salary(mid)
                last_month_salary = masterdao.get_master_last_month_granted_salary(mid)
                prefered_area = masterdao.get_master_prefered_area(mid)
                start_times, end_times = masterdao.get_master_working_time(mid)

                working_startdate = masterdao.get_master_working_start_date(mid)
                completed_cleaning_count = masterdao.get_master_completed_cleaning_count(mid)

                bank_name, bank_code, account_no = masterdao.get_master_account(mid)

                master_info['master_id']    = mid
                master_info['name']         = basic['name']
                master_info['phone']        = basic['phone']
                master_info['img']          = basic['img']
                master_info['age']          = basic['age']
                master_info['gender']       = basic['gender']
                master_info['address']      = basic['address']
                master_info['pet_alergy']   = basic['pet_alergy']
                master_info['manager_name'] = basic['manager_name']
                master_info['manager_phone'] = basic['manager_phone']
                master_info['cardinal']     = basic['cardinal']
                master_info['level']        = basic['level']
                master_info['need_route']    = basic['need_route']
                master_info['t_size']        = basic['t_size']
                master_info['rating_clean'] = float(rating_clean)
                master_info['rating_master'] = float(rating_master)
                master_info['total_salary'] = int(total_salary)
                master_info['last_month_salary'] = int(last_month_salary)
                master_info['prefered_area'] = prefered_area
                master_info['prefered_area_list'] = [area for area in prefered_area.split(',')]
                master_info['start_times']  = start_times
                master_info['end_times']    = end_times
                master_info['active']       = basic['active']

                master_info['working_startdate']    = working_startdate
                master_info['cleaning_count']    = completed_cleaning_count
                master_info['account'] = {'name' : bank_name, 'code' : bank_code, 'account_no' : account_no}

                masters.append(master_info)
                total_counts += 1

            print masters

            ret['response'] = {'counts' : total_counts, 'masters' : masters }
            self.set_status(Response.RESULT_OK)

        except Exception, e:
            session.rollback()

            print_err_detail(e)
            self.set_status(Response.RESULT_SERVERERROR)
            add_err_message_to_response(ret, err_dict['err_mysql'])