def list(self):
     id = int(self.get_argument('id',0))
     name = self.get_argument('name','')
     page = int(self.get_argument('pageNum',1))
     psize = int(self.get_argument('numPerPage',20))
     if not name:
         q = AccountingFactoryStart.mgr().Q()
     else:
         fid = Factory.mgr().Q().extra("name LIKE '%%%s%%'"%name)
         if fid:
             fidlist = ','.join([str(i['id']) for i in fid])
             q = AccountingFactoryStart.mgr().Q().extra("factory_id in (%s)"%fidlist)
         else:
             q = []
     count = len(q)
     factories = q[(page-1)*psize:page*psize]
     for i in factories:
         i['time'] = i['time'].strftime('%Y-%m-%d')
     if factories:
         for i in factories:
             try: 
                 factory_name = Factory.mgr().Q().filter(id=i.factory_id)[0]['name']
             except Exception, e:
                 factory_name = ''
             i['factory_name'] = factory_name
Exemple #2
0
 def list(self):
     id = int(self.get_argument('id', 0))
     name = self.get_argument('name', '')
     page = int(self.get_argument('pageNum', 1))
     psize = int(self.get_argument('numPerPage', 20))
     if not name:
         q = AccountingFactoryStart.mgr().Q()
     else:
         fid = Factory.mgr().Q().extra("name LIKE '%%%s%%'" % name)
         if fid:
             fidlist = ','.join([str(i['id']) for i in fid])
             q = AccountingFactoryStart.mgr().Q().extra(
                 "factory_id in (%s)" % fidlist)
         else:
             q = []
     count = len(q)
     factories = q[(page - 1) * psize:page * psize]
     for i in factories:
         i['time'] = i['time'].strftime('%Y-%m-%d')
     if factories:
         for i in factories:
             try:
                 factory_name = Factory.mgr().Q().filter(
                     id=i.factory_id)[0]['name']
             except Exception, e:
                 factory_name = ''
             i['factory_name'] = factory_name
 def delete(self):
     id = int(self.get_argument('id'))
     #factory = Factory.mgr(ismaster=1).one(id)
     factory = AccountingFactoryStart.mgr(ismaster=1).Q().filter(factory_id=id)[0]
     #print factory
     factory and factory.delete()
     self.json2dwz('200','forward','dlist',forwardUrl='factory_accounting/list')
 def save(self):
     factory_id = self.get_argument('factory_id')
     time = str(self.get_argument('time'))
     time = datetime.datetime.strptime(time,'%Y-%m-%d')
     coefficient = float(self.get_argument('coefficient'))
     author = self.get_current_user()['name']
     if factory_id:
         f = AccountingFactoryStart.mgr(ismaster=True).Q().filter(factory_id=factory_id)[0]
         if not f:
             f = AccountingFactoryStart.new()
     f.time = time
     f.factory_id = factory_id
     f.coefficient = coefficient
     f.author = author
     f = f.save()
     self.json2dwz('200','closeCurrent','dlist',forwardUrl='factory_accounting/list')
Exemple #5
0
 def edit(self):
     id = int(self.get_argument('id'))
     factory = AccountingFactoryStart.mgr(ismaster=1).Q().filter(
         factory_id=id)[0]
     self.render(
         'data/factory_accounting_edit.html',
         factory=factory,
     )
Exemple #6
0
 def delete(self):
     id = int(self.get_argument('id'))
     #factory = Factory.mgr(ismaster=1).one(id)
     factory = AccountingFactoryStart.mgr(ismaster=1).Q().filter(
         factory_id=id)[0]
     #print factory
     factory and factory.delete()
     self.json2dwz('200',
                   'forward',
                   'dlist',
                   forwardUrl='factory_accounting/list')
Exemple #7
0
 def save(self):
     factory_id = self.get_argument('factory_id')
     time = str(self.get_argument('time'))
     time = datetime.datetime.strptime(time, '%Y-%m-%d')
     coefficient = float(self.get_argument('coefficient'))
     author = self.get_current_user()['name']
     if factory_id:
         f = AccountingFactoryStart.mgr(ismaster=True).Q().filter(
             factory_id=factory_id)[0]
         if not f:
             f = AccountingFactoryStart.new()
     f.time = time
     f.factory_id = factory_id
     f.coefficient = coefficient
     f.author = author
     f = f.save()
     self.json2dwz('200',
                   'closeCurrent',
                   'dlist',
                   forwardUrl='factory_accounting/list')
Exemple #8
0
    def businessmonthly(self):
        platform_id = int(self.get_argument('platform_id', 6))
        run_id = int(self.get_argument('run_id', 0))
        plan_id = int(self.get_argument('plan_id', 0))
        factory_id = int(self.get_argument('factory_id', 0))
        version_name = self.get_argument('version_name', '').replace('__', '.')
        product_name = self.get_argument('product_name', '')
        month = self.get_argument('month', '')
        page = int(self.get_argument('pageNum', 1))
        psize = int(self.get_argument('numPerPage', 20))
        query_mode = self.get_argument('query_mode', 'accounting_business')
        group = self.get_argument('group', '')
        action = self.get_argument('action', '')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        last_month = tody - datetime.timedelta(days=30)
        last_month = last_month.strftime('%Y-%m')
        order_field = self.get_argument('orderField', '')
        if not month or month == '未选择':
            month = last_month
        start = month + '-01'
        start = datetime.datetime.strptime(start, '%Y-%m-%d')
        next_month_start = start + datetime.timedelta(
            days=monthrange(start.year, start.month)[1])
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:], query_mode)
        basics = []
        stats = []

        for i in factory_list:
            factory_id = i['id']
            partner_list = Partnerv2.mgr().Q().filter(factory_id=i.id)[:]
            if not partner_list:
                continue
            q_start = AccountingFactoryStart.mgr().Q().filter(
                factory_id=partner_list[0]['factory_id'])
            start_time = None
            user = self.get_current_user()
            if user['name'] in SAFE_USER:
                start_time = datetime.datetime(2012, 12, 31)
            coefficient = 1.0
            if q_start:
                start_time = q_start[0]['time']
                coefficient = q_start[0]['coefficient']

            if not partner_list:
                continue
            if not start_time or start_time >= next_month_start:  #start_time
                stat = None
                continue
            elif start_time > start:
                start = start_time
            #stat = MonthlyBusinessStatDaily.mgr().get_monthly_busiess_stat_multy_days(start,next_month_start,partner_list)[0]
            stat = MonthlyBusinessStatDaily.mgr(
            ).get_monthly_busiess_stat_multy_days_proportion(
                start, next_month_start, partner_list)
            # Special factory
            if factory_id in special_factory_list:
                statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id][
                    'starttime']
                sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
                if start.strftime('%Y-%m-%d') >= statttime:
                    stat['fee'] = float(stat['fee']) * sfc
            # Special factory2
            #if factory_id in special_factory_list2:
            #    statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
            #    sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
            #    if start.strftime('%Y-%m-%d')>= statttime2:
            #        stat['fee'] = float(stat['fee']) * sfc2

            if stat['fee'] != None:
                stat['fee'] = int(stat['fee'])
                stat['factory'] = i.name
            else:
                stat['fee'] = '0'
                stat['factory'] = i.name

            # Special factory ...
            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-01-01':
                stat['fee'] = 483471
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-01-01':
                stat['fee'] = 966997

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-02-01':
                stat['fee'] = 778841
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-02-01':
                stat['fee'] = 1090431

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-03-01':
                stat['fee'] = 1027234
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-03-01':
                stat['fee'] = 1438187

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-04-01':
                stat['fee'] = 1003872
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-04-01':
                stat['fee'] = 1405475

            stats.append(stat)
        if order_field:
            stats.sort(cmp=lambda x, y: cmp(x[order_field], y[order_field]),
                       reverse=True)

        count = len(stats)
        groups = Factory.mgr().all_groups()
        if action == 'export':
            excel_title = [('factory', '厂商名称'), ('fee', '月收入')]
            xls = Excel().generate(excel_title, stats, 1)
            filename = 'accounting_bueiness_monthly_%s.xls' % (
                start.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition',
                            'attachment;filename=%s' % filename)
            self.finish(xls)
        else:
            self.render('data/accounting_businessmonthly.html',
                        count=count,
                        psize=psize,
                        page=page,
                        query_mode=query_mode,
                        stats=stats,
                        monthly=MONTHLY,
                        group=group,
                        groups=groups,
                        month=month,
                        order_field=order_field)
Exemple #9
0
    def businessdaily(self):
        platform_id = int(self.get_argument('platform_id', 6))
        run_id = int(self.get_argument('run_id', 0))
        plan_id = int(self.get_argument('plan_id', 0))
        factory_id = int(self.get_argument('factory_id', 0))
        version_name = self.get_argument('version_name', '').replace('__', '.')
        product_name = self.get_argument('product_name', '')
        page = int(self.get_argument('pageNum', 1))
        psize = int(self.get_argument('numPerPage', 20))
        query_mode = self.get_argument('query_mode', 'accounting_business')
        group = self.get_argument('group', '')
        order_field = self.get_argument('orderField', 'user_run')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = tody - datetime.timedelta(days=30)
        order_field = self.get_argument('orderField', '')

        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:], query_mode)
        #get facotry start
        for fact in factory_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(
                factory_id=fact['id'])
            if q_start:
                fact['start_time'] = q_start[0]['time']
                user = self.get_current_user()
                if user['name'] in SAFE_USER:
                    fact['start_time'] = datetime.datetime(2012, 12, 31)
                fact['coefficient'] = q_start[0]['coefficient']
            else:
                fact['start_time'] = None
                user = self.get_current_user()
                if user['name'] in SAFE_USER:
                    fact['start_time'] = datetime.datetime(2012, 12, 31)
                fact['coefficient'] = 1.0

        basics = []
        count = len(factory_list)

        #all_stats = Service.inst().stat.get_all_factstat('day',yest,tody)
        all_stats = Service.inst().stat.get_all_factstat_proportion(
            'day', yest, tody)
        for basic in all_stats:
            all_stats[basic]['sumfee'] = float(
                all_stats[basic]['consumefee']) + float(
                    all_stats[basic]['msgfee'])

        acc_stats = {}
        for i in factory_list:
            factory_id = i['id']
            if not i['start_time']:  #start_time
                basic = None
            elif i['start_time'] > yest:
                basic = None
            else:
                basic = all_stats.get(i.id, None)

            if basic:
                if factory_id in special_factory_list:
                    statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id][
                        'starttime']
                    sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id][
                        'coefficient']
                    for k in basic:
                        basic[k] = float(
                            basic[k]) * i['coefficient'] * sfc  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k, 0)
                else:
                    for k in basic:
                        basic[k] = float(
                            basic[k]) * i['coefficient']  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k, 0)
                if factory_id in special_factory_list2:
                    statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id][
                        'starttime']
                    sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id][
                        'coefficient']
                    for k in basic:
                        basic[k] = float(basic[k]) * sfc2  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k, 0)

                basic['title'] = i.name
                basics.append(basic)
        acc_stats['title'] = '总计'
        if order_field:
            basics.sort(cmp=lambda x, y: cmp(x[order_field], y[order_field]),
                        reverse=True)
        basics = basics[(page - 1) * psize:page * psize]
        if basics:
            basics += [acc_stats]
        groups = Factory.mgr().all_groups()
        self.render('data/accounting_businessdaily.html',
                    count=count,
                    psize=psize,
                    page=page,
                    query_mode=query_mode,
                    basics=basics,
                    factory_id=factory_id,
                    factory_list=factory_list,
                    start=None,
                    product_name='',
                    date=yest.strftime('%Y-%m-%d'),
                    order_field=order_field,
                    group=group,
                    groups=groups)
Exemple #10
0
    def productdaily(self):
        order_field = self.get_argument('orderField', 'user_run')
        platform_id = int(self.get_argument('platform_id', 6))
        run_id = int(self.get_argument('run_id', 0))
        plan_id = int(self.get_argument('plan_id', 0))
        version_name = self.get_argument('version_name', '').replace('__', '.')
        group = self.get_argument('group', '')
        psize = int(self.get_argument('numPerPage', 20))
        start = self.get_argument('start', '')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        action = self.get_argument('action', '')
        factory_id = int(self.get_argument('factory_id', 0))
        product_name = self.get_argument('product_name', '')
        page = int(self.get_argument('pageNum', 1))
        query_mode = self.get_argument('query_mode', 'accounting_product')
        if not start:
            start = yest
        else:
            start = datetime.datetime.strptime(start, '%Y-%m-%d')
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:], 'accounting_product')
        #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
        cuser = self.get_current_user()
        if len(factory_list) > 150 and (not cuser['is_staff']):
            factory_list = []
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
        #get facotry start time
        if partner_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(
                factory_id=partner_list[0]['factory_id'])
        else:
            q_start = None
        coefficient = 1.0
        if q_start:
            start_time = q_start[0]['time']
            coefficient = q_start[0]['coefficient']
        else:
            start_time = datetime.datetime(2014, 12, 31)
            user = self.get_current_user()
            if user['name'] in SAFE_USER:
                start_time = datetime.datetime(2012, 12, 31)
            coefficient = 1.0

        if start_time:
            if start_time > start:
                start = start_time

        scopeid_product, model_list = {}, []
        if factory_id == -1:
            #partnerid_list = [0]
            partner_list = []
        for i in partner_list:
            scopeid_list = []
            if not product_name:
                scopes = Scope.mgr().Q().filter(
                    platform_id=platform_id,
                    run_id=run_id,
                    plan_id=plan_id,
                    partner_id=i['partner_id'],
                    version_name=version_name).extra("product_name<>''")
            else:
                scopes = Scope.mgr().Q().filter(platform_id=platform_id,
                                                run_id=run_id,
                                                plan_id=plan_id,
                                                partner_id=i['partner_id'],
                                                version_name=version_name,
                                                product_name=product_name)
            for scope in scopes:
                scopeid_list.append(scope.id)
                scopeid_product[scope.id] = scope.product_name
            result = BasicStat.mgr().get_one_day_model_data_proportion(
                i['proportion'], scopeid_list, 'day', start)
            model_list += result
        count = len(model_list)
        for model in model_list:
            model['product_name'] = scopeid_product[model['scope_id']]
            model['time'] = start.strftime('%Y-%m-%d')

        #find key_list
        if not model_list:
            tmp = []
        else:
            tmp = model_list[0].keys()
        key_list = [
            i for i in tmp if i not in ('scope_id', 'time', 'product_name')
        ]

        #find product_name_list
        tmp = []
        for model in model_list:
            tmp.append(model['product_name'])
        tmp_set = set(tmp)
        product_name_list = [i for i in tmp_set]

        #get dft res_list
        res_list = []
        for i in product_name_list:
            res_model = {}
            res_model['product_name'] = i
            res_model['time'] = start.strftime('%Y-%m-%d')
            for key in key_list:
                res_model[key] = 0
            res_list.append(res_model)
        #Cumulative data
        for model in model_list:
            for res in res_list:
                if res['product_name'] == model['product_name']:
                    for key in key_list:
                        res[key] += model[key]

        count = len(res_list)
        res_list = self.bubblesort(res_list, order_field)
        #Special factory Coefficient
        if factory_id in special_factory_list:
            statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
            sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
            if start.strftime('%Y-%m-%d') >= statttime:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name', 'time'):
                            res[i] = float(
                                res[i]) * coefficient * sfc  # coefficient
                            res[i] = long(res[i])
            else:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name', 'time'):
                            res[i] = float(res[i]) * coefficient  # coefficient
                            res[i] = long(res[i])
        #Special factory Coefficient2
        if factory_id in special_factory_list2:
            statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
            sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
            if start.strftime('%Y-%m-%d') >= statttime2:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name', 'time'):
                            res[i] = float(res[i]) * sfc2  # coefficient
                            res[i] = long(res[i])

        if action == 'export':
            title = [('product_name', '机型'), ('time', '时间'),
                     ('user_run', '启动用户'), ('new_user_run', '新增启动用户'),
                     ('user_visit', '访问用户'), ('new_user_visit', '新增访问用户'),
                     ('pay_user', '付费用户'), ('active_user_visit', '活跃用户'),
                     ('visits', '访问PV'), ('cpay_down', '按章付费数'),
                     ('bpay_down', '按本付费数'), ('cpay_user', '按章付费用户'),
                     ('bpay_user', '按本付费用户'), ('cfree_down', '按章免费数'),
                     ('bfree_down', '按本免费数'), ('cfree_user', '按章免费用户'),
                     ('bfree_user', '按本免费用户')]
            xls = Excel().generate(title, res_list, 1)
            filename = 'factstat_product_%s.xls' % (yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition',
                            'attachment;filename=%s' % filename)
            self.finish(xls)
        else:
            res_list = res_list[(page - 1) * psize:page * psize]
            self.render('data/accounting_productdaily.html',
                        count=count,
                        psize=psize,
                        page=page,
                        factory_id=factory_id,
                        query_mode=query_mode,
                        product_name=product_name,
                        basics=res_list,
                        start=start.strftime('%Y-%m-%d'),
                        factory_list=factory_list,
                        order_field=order_field)
    def businessdaily(self):
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        factory_id = int(self.get_argument('factory_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        product_name = self.get_argument('product_name','')
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',20))
        query_mode = self.get_argument('query_mode','accounting_business')
        group = self.get_argument('group','')
        order_field = self.get_argument('orderField','user_run')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = tody - datetime.timedelta(days=30)
        order_field = self.get_argument('orderField','')

        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:],query_mode)
        #get facotry start 
        for fact in factory_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(factory_id=fact['id'])
            if q_start:
                fact['start_time'] = q_start[0]['time']
                user = self.get_current_user()
                if user['name'] in SAFE_USER:
                    fact['start_time'] = datetime.datetime(2012,12,31)
                fact['coefficient'] = q_start[0]['coefficient']
            else:
                fact['start_time'] = None
                user = self.get_current_user()
                if user['name'] in SAFE_USER:
                    fact['start_time'] = datetime.datetime(2012,12,31)
                fact['coefficient'] = 1.0

        basics = []
        count = len(factory_list)
        
        #all_stats = Service.inst().stat.get_all_factstat('day',yest,tody)
        all_stats = Service.inst().stat.get_all_factstat_proportion('day',yest,tody)
        for basic in all_stats:
            all_stats[basic]['sumfee'] = float(all_stats[basic]['consumefee']) + float(all_stats[basic]['msgfee'])
        
        acc_stats = {}
        for i in factory_list:
            factory_id = i['id']
            if not i['start_time']: #start_time
                basic = None
            elif i['start_time'] > yest: 
                basic = None
            else:
                basic = all_stats.get(i.id,None)
            
            if basic:
                if factory_id in special_factory_list:
                    statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
                    sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
                    for k in basic:
                        basic[k] = float(basic[k]) * i['coefficient'] * sfc  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k,0)
                else:
                    for k in basic:
                        basic[k] = float(basic[k]) * i['coefficient'] #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k,0)
                if factory_id in special_factory_list2:
                    statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
                    sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
                    for k in basic:
                        basic[k] = float(basic[k]) * sfc2  #coefficient
                        basic[k] = long(basic[k])
                        acc_stats[k] = basic[k] + acc_stats.get(k,0)

                basic['title'] = i.name
                basics.append(basic)
        acc_stats['title'] = '总计'
        if order_field:
            basics.sort(cmp=lambda x,y:cmp(x[order_field],y[order_field]),reverse=True)
        basics= basics[(page-1)*psize:page*psize]
        if basics:
            basics += [acc_stats]
        groups = Factory.mgr().all_groups()
        self.render('data/accounting_businessdaily.html',
                    count=count,psize=psize,page=page,query_mode=query_mode,basics=basics,
                    factory_id=factory_id,factory_list=factory_list,start=None,product_name='',
                    date=yest.strftime('%Y-%m-%d'),order_field=order_field,group=group,groups=groups)
 def edit(self):
     id = int(self.get_argument('id'))
     factory = AccountingFactoryStart.mgr(ismaster=1).Q().filter(factory_id=id)[0]
     self.render('data/factory_accounting_edit.html',
                 factory = factory,
                 )
from lib.utils import time_start
from model.stat import Scope, BasicStat, VisitStat, TopNStat, BookStat, ProductStat, MonthlyBusinessStatDaily, AccountingFactoryStart
from model.factory import Factory, Partner
from service import Service

if __name__ == "__main__":
    start = datetime.datetime(2014, 4, 1)
    end = datetime.datetime(2014, 5, 1)
    while start < end:
        print start
        _start = start
        _end = _start + datetime.timedelta(days=1)

        all_stats = Service.inst().stat.get_all_partner_fee_daily(
            'day', _start, _end)
        for i in all_stats.keys():
            coefficient = AccountingFactoryStart.mgr(
            ).get_coefficient_by_partner_id(i)
            if coefficient:
                s = MonthlyBusinessStatDaily.new()
                try:
                    s.time = start
                    s.partner_id = i
                    s.fee = all_stats[i] * coefficient[0]['coefficient']
                    #s.fee =  "%.01f" % s.fee
                    s.fee = int(s.fee)
                    s.save()
                except Exception, e:
                    logging.error('%s\n', str(e), exc_info=True)
        start += datetime.timedelta(days=1)
    def factorydaily(self):
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        factory_id = int(self.get_argument('factory_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        product_name = self.get_argument('product_name','')
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',20))
        query_mode = self.get_argument('query_mode','accounting_basic')
        group = self.get_argument('group','')
        order_field = self.get_argument('orderField','user_run')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = self.get_argument('start','')
        date = self.get_argument('date','')
        action = self.get_argument('action','')
        
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:],query_mode)
        #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
        cuser = self.get_current_user()
        if len(factory_list)>150 and (not cuser['is_staff']):
            factory_list = []
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
        #get facotry start time
        if partner_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(factory_id=partner_list[0]['factory_id'])
        else:
            q_start = None
        start_time = None
        user = self.get_current_user()
        if user['name'] in SAFE_USER:
            start_time = datetime.datetime(2012,12,31)
        coefficient = 1.0
        if q_start:
            start_time = q_start[0]['time']
            coefficient = q_start[0]['coefficient']
        # scope list
        scopeid_list = []
        scope_proportion_list = []
        for i in partner_list:
            scope = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                      partner_id=i.partner_id,version_name=version_name,product_name=product_name)[0]
            if scope:
                scopeid_list.append(scope.id)
                temp = {'proportion':i['proportion'],'scope_id':scope.id,'partner_id':i['partner_id']}
                scope_proportion_list.append(temp)
        # basic stat
        basics = []
        dft = dict([(i,0) for i in BasicStat._fields])
        if not start:
            start = tody - datetime.timedelta(days=7)
        else:
            start = datetime.datetime.strptime(start,'%Y-%m-%d') 
        _start,days = start,[]
        if start_time:
            if start_time > _start: #限定开始时间
                _start = start_time
        else:
            _start = datetime.datetime(3012,01,01) # 没配置的不现实
        while _start < tody:
            _end = _start + datetime.timedelta(days=1)
            #basic = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,mode='day',start=_start,end=_end)
            basic = BasicStat.mgr().get_data_by_multi_scope_proportion(scope_proportion_list,mode='day',start=_start,end=_end)
            #Special factory Coefficient 
            if factory_id in special_factory_list:
                statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
                sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
                if _start.strftime('%Y-%m-%d')>= statttime:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(int(basic[i])) * coefficient #要和商务月报数据保持一致,所以这么处理
                        basic[i] = "%.01f" % basic[i]
                        basic[i] = float(basic[i]) * sfc
                        basic[i] = int(basic[i])
                else:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(basic[i]) * coefficient
                        basic[i] = long(basic[i])
            #Special factory Coefficient2
            if factory_id in special_factory_list2:
                statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
                sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
                if _start.strftime('%Y-%m-%d')>= statttime2:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(basic[i]) * sfc2
                        basic[i] = int(basic[i])
            
            basic['title'] = _start.strftime('%Y-%m-%d')
            basics.append(basic)
            days.append(_start)
            _start = _end
        # for chart
        x_axis = ['%s'%i.strftime('%m-%d') for i in days]
        results = {}
        excludes = ('id','scope_id','mode','time','bfee','cfee','recharge','uptime','batch_fee','batch_pv','batch_uv','visits','user_visit',
                    'new_user_visit','active_user_visit','imei','user_retention')
        for k in [i for i in BasicStat._fields if i not in excludes]:
            results[k] ={'title':BasicStat._fieldesc[k],'data':','.join([str(i.get(k,0)) for i in basics])}
        for i in basics:
            i['feesum'] = float(i['consumefee'])+i['msgfee']
            i['feesum'] = int(i['feesum'])
        acc_stats = {}
        for basic in basics:
            for field in basic:
                if field != 'title':
                    basic[field] = int(basic[field])
                    acc_stats[field] = basic[field] + acc_stats.get(field,0)
        acc_stats['title'] = '总计'
        if basics:
            basics.append(acc_stats)
        acc = {'title':'总计'}
        if action == 'export':  
            excel_title = [('title','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),
            ('pay_user','付费用户'),
            ('cpay_down','章付费数'),('bpay_down','本付费数'),
            ('cpay_user','章付费用户'),('bpay_user','本付费用户'),
            ('cfree_down','章免费数'),('bfree_down','本免费数'),
            ('cfree_user','章免费用户'),('bfree_user','本免费用户'),
            ('feesum','收入')]
            xls = Excel().generate(excel_title,basics,1) 
            filename = 'accounting_factorydaily_%s.xls' % (yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition','attachment;filename=%s'%filename)
            self.finish(xls)
        else:
            self.render('data/accounting_factorydaily.html',
                    latform_id=platform_id,run_id=run_id,plan_id=plan_id,factory_id=factory_id,tpl_titles=ACCOUNTING_FACTORY_TITLE,
                    version_name=version_name,product_name=product_name,query_mode=query_mode,
                    basics=basics,start=start.strftime('%Y-%m-%d'),date=yest.strftime('%Y-%m-%d'),
                    factory_list=factory_list,x_axis=x_axis,results=results)
    def businessmonthly(self):
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        factory_id = int(self.get_argument('factory_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        product_name = self.get_argument('product_name','')
        month = self.get_argument('month','')
        page = int(self.get_argument('pageNum',1))
        psize = int(self.get_argument('numPerPage',20))
        query_mode = self.get_argument('query_mode','accounting_business')
        group = self.get_argument('group','')
        action = self.get_argument('action','') 
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        last_month = tody - datetime.timedelta(days=30)
        last_month = last_month.strftime('%Y-%m')
        order_field = self.get_argument('orderField','')
        if not month or month == '未选择':
            month = last_month
        start = month + '-01'
        start = datetime.datetime.strptime(start,'%Y-%m-%d')
        next_month_start = start + datetime.timedelta(days=monthrange(start.year,start.month)[1])
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:],query_mode)
        basics = []
        stats = []
        
        for i in factory_list:
            factory_id = i['id']
            partner_list = Partnerv2.mgr().Q().filter(factory_id=i.id)[:]
            if not partner_list:
                continue
            q_start = AccountingFactoryStart.mgr().Q().filter(factory_id=partner_list[0]['factory_id'])
            start_time = None
            user = self.get_current_user()
            if user['name'] in SAFE_USER:
                start_time = datetime.datetime(2012,12,31)
            coefficient = 1.0
            if q_start:
                start_time = q_start[0]['time']
                coefficient = q_start[0]['coefficient']
            
            if not partner_list:
                continue
            if not start_time or start_time >= next_month_start: #start_time
                stat = None
                continue
            elif start_time > start:
                start = start_time
            #stat = MonthlyBusinessStatDaily.mgr().get_monthly_busiess_stat_multy_days(start,next_month_start,partner_list)[0]
            stat = MonthlyBusinessStatDaily.mgr().get_monthly_busiess_stat_multy_days_proportion(start,next_month_start,partner_list)
            # Special factory 
            if factory_id in special_factory_list:
                statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
                sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
                if start.strftime('%Y-%m-%d')>= statttime:
                    stat['fee'] = float(stat['fee']) * sfc 
            # Special factory2
            #if factory_id in special_factory_list2:
            #    statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
            #    sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
            #    if start.strftime('%Y-%m-%d')>= statttime2:
            #        stat['fee'] = float(stat['fee']) * sfc2 
            
           
            if stat['fee'] != None:
                stat['fee'] = int(stat['fee'])
                stat['factory'] = i.name
            else:
                stat['fee'] = '0'
                stat['factory'] = i.name
            
            # Special factory ...
            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-01-01': 
                stat['fee'] = 483471
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-01-01':
                stat['fee'] = 966997

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-02-01': 
                stat['fee'] = 778841
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-02-01':
                stat['fee'] = 1090431

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-03-01': 
                stat['fee'] = 1027234
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-03-01':
                stat['fee'] = 1438187

            if i['id'] == 229 and start.strftime('%Y-%m-%d') == '2014-04-01': 
                stat['fee'] = 1003872
            if i['id'] == 2144 and start.strftime('%Y-%m-%d') == '2014-04-01':
                stat['fee'] = 1405475



            stats.append(stat)
        if order_field:
            stats.sort(cmp=lambda x,y:cmp(x[order_field],y[order_field]),reverse=True)
        
        count = len(stats)
        groups = Factory.mgr().all_groups()
        if action == 'export':
            excel_title = [('factory','厂商名称'),('fee','月收入')]
            xls = Excel().generate(excel_title,stats,1)
            filename = 'accounting_bueiness_monthly_%s.xls' % (start.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition','attachment;filename=%s'%filename)
            self.finish(xls)
        else:
            self.render('data/accounting_businessmonthly.html',
                count=count,psize=psize,page=page,query_mode=query_mode,stats=stats,monthly=MONTHLY,
                group=group,groups=groups,month=month,order_field=order_field)
Exemple #16
0
    def factorydaily(self):
        platform_id = int(self.get_argument('platform_id', 6))
        run_id = int(self.get_argument('run_id', 0))
        plan_id = int(self.get_argument('plan_id', 0))
        factory_id = int(self.get_argument('factory_id', 0))
        version_name = self.get_argument('version_name', '').replace('__', '.')
        product_name = self.get_argument('product_name', '')
        page = int(self.get_argument('pageNum', 1))
        psize = int(self.get_argument('numPerPage', 20))
        query_mode = self.get_argument('query_mode', 'accounting_basic')
        group = self.get_argument('group', '')
        order_field = self.get_argument('orderField', 'user_run')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        start = self.get_argument('start', '')
        date = self.get_argument('date', '')
        action = self.get_argument('action', '')

        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:], query_mode)
        #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
        cuser = self.get_current_user()
        if len(factory_list) > 150 and (not cuser['is_staff']):
            factory_list = []
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
        #get facotry start time
        if partner_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(
                factory_id=partner_list[0]['factory_id'])
        else:
            q_start = None
        start_time = None
        user = self.get_current_user()
        if user['name'] in SAFE_USER:
            start_time = datetime.datetime(2012, 12, 31)
        coefficient = 1.0
        if q_start:
            start_time = q_start[0]['time']
            coefficient = q_start[0]['coefficient']
        # scope list
        scopeid_list = []
        scope_proportion_list = []
        for i in partner_list:
            scope = Scope.mgr().Q().filter(platform_id=platform_id,
                                           run_id=run_id,
                                           plan_id=plan_id,
                                           partner_id=i.partner_id,
                                           version_name=version_name,
                                           product_name=product_name)[0]
            if scope:
                scopeid_list.append(scope.id)
                temp = {
                    'proportion': i['proportion'],
                    'scope_id': scope.id,
                    'partner_id': i['partner_id']
                }
                scope_proportion_list.append(temp)
        # basic stat
        basics = []
        dft = dict([(i, 0) for i in BasicStat._fields])
        if not start:
            start = tody - datetime.timedelta(days=7)
        else:
            start = datetime.datetime.strptime(start, '%Y-%m-%d')
        _start, days = start, []
        if start_time:
            if start_time > _start:  #限定开始时间
                _start = start_time
        else:
            _start = datetime.datetime(3012, 01, 01)  # 没配置的不现实
        while _start < tody:
            _end = _start + datetime.timedelta(days=1)
            #basic = BasicStat.mgr().get_data_by_multi_scope(scopeid_list,mode='day',start=_start,end=_end)
            basic = BasicStat.mgr().get_data_by_multi_scope_proportion(
                scope_proportion_list, mode='day', start=_start, end=_end)
            #Special factory Coefficient
            if factory_id in special_factory_list:
                statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id][
                    'starttime']
                sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
                if _start.strftime('%Y-%m-%d') >= statttime:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(int(
                            basic[i])) * coefficient  #要和商务月报数据保持一致,所以这么处理
                        basic[i] = "%.01f" % basic[i]
                        basic[i] = float(basic[i]) * sfc
                        basic[i] = int(basic[i])
                else:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(basic[i]) * coefficient
                        basic[i] = long(basic[i])
            #Special factory Coefficient2
            if factory_id in special_factory_list2:
                statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id][
                    'starttime']
                sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
                if _start.strftime('%Y-%m-%d') >= statttime2:
                    for i in basic.keys():  #乘以系数
                        basic[i] = float(basic[i]) * sfc2
                        basic[i] = int(basic[i])

            basic['title'] = _start.strftime('%Y-%m-%d')
            basics.append(basic)
            days.append(_start)
            _start = _end
        # for chart
        x_axis = ['%s' % i.strftime('%m-%d') for i in days]
        results = {}
        excludes = ('id', 'scope_id', 'mode', 'time', 'bfee', 'cfee',
                    'recharge', 'uptime', 'batch_fee', 'batch_pv', 'batch_uv',
                    'visits', 'user_visit', 'new_user_visit',
                    'active_user_visit', 'imei', 'user_retention')
        for k in [i for i in BasicStat._fields if i not in excludes]:
            results[k] = {
                'title': BasicStat._fieldesc[k],
                'data': ','.join([str(i.get(k, 0)) for i in basics])
            }
        for i in basics:
            i['feesum'] = float(i['consumefee']) + i['msgfee']
            i['feesum'] = int(i['feesum'])
        acc_stats = {}
        for basic in basics:
            for field in basic:
                if field != 'title':
                    basic[field] = int(basic[field])
                    acc_stats[field] = basic[field] + acc_stats.get(field, 0)
        acc_stats['title'] = '总计'
        if basics:
            basics.append(acc_stats)
        acc = {'title': '总计'}
        if action == 'export':
            excel_title = [('title', '时间'), ('user_run', '启动用户'),
                           ('new_user_run', '新增启动用户'), ('pay_user', '付费用户'),
                           ('cpay_down', '章付费数'), ('bpay_down', '本付费数'),
                           ('cpay_user', '章付费用户'), ('bpay_user', '本付费用户'),
                           ('cfree_down', '章免费数'), ('bfree_down', '本免费数'),
                           ('cfree_user', '章免费用户'), ('bfree_user', '本免费用户'),
                           ('feesum', '收入')]
            xls = Excel().generate(excel_title, basics, 1)
            filename = 'accounting_factorydaily_%s.xls' % (
                yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition',
                            'attachment;filename=%s' % filename)
            self.finish(xls)
        else:
            self.render('data/accounting_factorydaily.html',
                        latform_id=platform_id,
                        run_id=run_id,
                        plan_id=plan_id,
                        factory_id=factory_id,
                        tpl_titles=ACCOUNTING_FACTORY_TITLE,
                        version_name=version_name,
                        product_name=product_name,
                        query_mode=query_mode,
                        basics=basics,
                        start=start.strftime('%Y-%m-%d'),
                        date=yest.strftime('%Y-%m-%d'),
                        factory_list=factory_list,
                        x_axis=x_axis,
                        results=results)
from lib.utils import time_start
from model.stat import Scope,BasicStat,VisitStat,TopNStat,BookStat,ProductStat,MonthlyBusinessStatDaily,AccountingFactoryStart
from model.factory import Factory,Partner
from service import Service

if __name__ == "__main__":
    start = datetime.datetime(2014,4,1)
    end = datetime.datetime(2014,5,1)
    while start < end:
        print start
        _start = start
        _end = _start + datetime.timedelta(days=1)
        
        all_stats = Service.inst().stat.get_all_partner_fee_daily('day',_start,_end)
        for i in all_stats.keys():
            coefficient = AccountingFactoryStart.mgr().get_coefficient_by_partner_id(i)
            if coefficient:
                s = MonthlyBusinessStatDaily.new()
                try:
                    s.time = start
                    s.partner_id = i
                    s.fee = all_stats[i] * coefficient[0]['coefficient']
                    #s.fee =  "%.01f" % s.fee
                    s.fee = int(s.fee)
                    s.save()
                except Exception,e:
                    logging.error('%s\n',str(e),exc_info=True)
        start += datetime.timedelta(days=1) 


    def productdaily(self):
        order_field = self.get_argument('orderField','user_run')
        platform_id = int(self.get_argument('platform_id',6))
        run_id = int(self.get_argument('run_id',0))
        plan_id = int(self.get_argument('plan_id',0))
        version_name = self.get_argument('version_name','').replace('__','.')
        group = self.get_argument('group','')
        psize = int(self.get_argument('numPerPage',20))
        start = self.get_argument('start','')
        tody = self.get_date(1) + datetime.timedelta(days=1)
        yest = tody - datetime.timedelta(days=1)
        action = self.get_argument('action','')
        factory_id = int(self.get_argument('factory_id',0))
        product_name = self.get_argument('product_name','')
        page = int(self.get_argument('pageNum',1))
        query_mode = self.get_argument('query_mode','accounting_product') 
        if not start:
            start = yest
        else:
            start = datetime.datetime.strptime(start,'%Y-%m-%d')
        # Special factory
        special_factory_list = SPECIAL_FACTORY_COEFFICIENT.keys()
        special_factory_list2 = SPECIAL_FACTORY_COEFFICIENT2.keys()

        # factory
        q = Factory.mgr().Q()
        group and q.filter(group=group)
        factory_list = self.filter_factory_acc(q[:],'accounting_product')
        #for safety reason, some time a perm doesn't have a val, and in this Situation a factory CANN'T see all factory accouting data!!!
        cuser = self.get_current_user()
        if len(factory_list)>150 and (not cuser['is_staff']):
            factory_list = []
        if factory_id == 0 and factory_list:
            factory_id = factory_list[0].id
        partner_list = Partnerv2.mgr().Q().filter(factory_id=factory_id)[:]
        #get facotry start time
        if partner_list:
            q_start = AccountingFactoryStart.mgr().Q().filter(factory_id=partner_list[0]['factory_id'])
        else:
            q_start = None
        coefficient = 1.0
        if q_start:
            start_time = q_start[0]['time']
            coefficient = q_start[0]['coefficient']
        else:
            start_time = datetime.datetime(2014,12,31) 
            user = self.get_current_user()
            if user['name'] in SAFE_USER:
                start_time = datetime.datetime(2012,12,31)
            coefficient = 1.0

        if start_time:
            if start_time > start:
                start = start_time
        
        scopeid_product,model_list = {},[]
        if factory_id == -1:
            #partnerid_list = [0]
            partner_list = []
        for i in partner_list:
            scopeid_list = []
            if not product_name:
                scopes = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                partner_id=i['partner_id'],version_name=version_name).extra("product_name<>''")
            else:
                scopes = Scope.mgr().Q().filter(platform_id=platform_id,run_id=run_id,plan_id=plan_id,
                partner_id=i['partner_id'],version_name=version_name,product_name=product_name)
            for scope in scopes:
                scopeid_list.append(scope.id)
                scopeid_product[scope.id] = scope.product_name
            result = BasicStat.mgr().get_one_day_model_data_proportion(i['proportion'],scopeid_list,'day',start)
            model_list += result
        count = len(model_list)
        for model in model_list:
            model['product_name'] = scopeid_product[model['scope_id']]
            model['time'] = start.strftime('%Y-%m-%d') 
        
        #find key_list
        if not model_list:
            tmp = []
        else:
            tmp = model_list[0].keys()
        key_list = [i for i in tmp if i not in ('scope_id','time','product_name')]
                
        #find product_name_list
        tmp = []
        for model in model_list:
            tmp.append(model['product_name'])
        tmp_set = set(tmp)
        product_name_list = [i for i in tmp_set]
            
        #get dft res_list
        res_list = []
        for i in product_name_list:
            res_model = {}
            res_model['product_name'] = i
            res_model['time'] = start.strftime('%Y-%m-%d')
            for key in key_list:
                res_model[key] = 0
            res_list.append(res_model)
        #Cumulative data 
        for model in model_list:
            for res in res_list:
                if res['product_name'] == model['product_name']:
                    for key in key_list:
                        res[key] += model[key]
                
        count = len(res_list)
        res_list = self.bubblesort(res_list,order_field)
        #Special factory Coefficient 
        if factory_id in special_factory_list:
            statttime = SPECIAL_FACTORY_COEFFICIENT[factory_id]['starttime']
            sfc = SPECIAL_FACTORY_COEFFICIENT[factory_id]['coefficient']
            if start.strftime('%Y-%m-%d')>= statttime:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name','time'):
                            res[i] = float(res[i]) * coefficient * sfc # coefficient
                            res[i] = long(res[i])
            else:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name','time'):
                            res[i] = float(res[i]) * coefficient # coefficient
                            res[i] = long(res[i])
        #Special factory Coefficient2
        if factory_id in special_factory_list2:
            statttime2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['starttime']
            sfc2 = SPECIAL_FACTORY_COEFFICIENT2[factory_id]['coefficient']
            if start.strftime('%Y-%m-%d')>= statttime2:
                for res in res_list:
                    for i in res.keys():
                        if i not in ('product_name','time'):
                            res[i] = float(res[i]) * sfc2 # coefficient
                            res[i] = long(res[i])
 
        if action == 'export': 
            title = [('product_name','机型'),('time','时间'),('user_run','启动用户'),('new_user_run','新增启动用户'),('user_visit','访问用户'),
                ('new_user_visit','新增访问用户'),('pay_user','付费用户'),('active_user_visit','活跃用户'),('visits','访问PV'),('cpay_down','按章付费数'),
                ('bpay_down','按本付费数'),('cpay_user','按章付费用户'),('bpay_user','按本付费用户'),('cfree_down','按章免费数'),('bfree_down','按本免费数'),
                ('cfree_user','按章免费用户'),('bfree_user','按本免费用户')]
            xls = Excel().generate(title,res_list,1)
            filename = 'factstat_product_%s.xls' % (yest.strftime('%Y-%m-%d'))
            self.set_header('Content-Disposition','attachment;filename=%s'%filename)
            self.finish(xls)
        else:
            res_list = res_list[(page-1)*psize:page*psize]
            self.render('data/accounting_productdaily.html',
                    count=count,psize=psize,page=page,factory_id=factory_id,
                    query_mode=query_mode,product_name=product_name,basics=res_list,
                    start=start.strftime('%Y-%m-%d'),
                    factory_list=factory_list, order_field=order_field)