使用单一方法 openerp 6.1 打印多个报告

print multiple reports using a single method openerp 6.1

我正在尝试使用一种方法打印多张发票,到目前为止我已经尝试了两种方法,在 openerp 中使用一个按钮和调用它的方法,并通过外部定义一个 python 脚本从外部调用打印,但脚本因内部服务器错误而失败。 这两种方法, 在按钮上调用了 1 个方法

def update_invoice_report(self,cr,uid,ids,context=None):
    compare_date=datetime.now().date().strftime("%Y-%m-%d")
    val=[]
    count=0
    # file_name='/home/chaitalikelsukar/Downloads/invoice_print (*).pdf'
# for j in self.browse(cr,uid,ids):
    # if not j.model_name :
    #   raise osv.except_osv(('Alert'),('Select Model Invoice'))
    # if not j.unique_number:
    #   raise osv.except_osv(('Alert'),('Enter Number'))
    use_date=compare_date
    # if j.model_name=='invoice':
    query="select id from invoice_adhoc_master where invoice_date >= '2017-05-01' and invoice_date < '2017-06-01'"
    cr.execute(query)
    list1=cr.fetchall()
    print list1
    contract_search=[i[0] for i in list1 if i[0]]
    if contract_search:
        for i in contract_search:
            data =self.pool.get('invoice.adhoc.master').read(cr,uid,contract_search,context)
            datas={

                'ids':ids,
                'model':'invoice.adhoc.master',
                'form':data
            }
            return {
                'type':'ir.actions.report.xml',
                'report_name':'invoice_print'
                'datas':datas
            }

上面的代码returns范围内的所有发票,打印后无法移动

第二种方法:

def awesome():
        f = open('config.txt', 'r')
        lines = f.readlines()
        # line_iter= iter(lines)
        login_credentials = []
        file_name = ''
        dbname=''
        line =lines[0]
        string_pdf =''
        # print line
        # try:
        if (True):
            dbname=str(line.split('=')[1]).split('\n')[0]
            # print dbname

            if dbname:
                # connect_string="dbname='"+dbname+"' user='"+'openerp'+"' host='"+localhost+"' password='"+'openerp'+"' "
                # else:
                connect_string="dbname='"+dbname+"' user='"+'openerp'+"' host='"+'localhost'+"' password='"+'openerp'+"' "
                conn_pg = psycopg2.connect(connect_string)
                pg_cursor = conn_pg.cursor()
                # pg_cursor.execute("")
                pg_cursor.execute("SELECT id from res_company where dbname='%s'"%dbname)
                invoice_ids = pg_cursor.fetchall()
                if tuple(invoice_ids):
                    pg_cursor.execute("SELECT company_id from account_account limit 1")
                    invoice_ids =pg_cursor.fetchone()
                pg_cursor.execute("select vpn_ip_address,port,dbname,pwd,user_name from res_company where id =%s"%invoice_ids)
                company_data=pg_cursor.fetchone()
                # print invoice_ids,company_data
                vpn_ip_addr = company_data[0]
                port =company_data[1]
                dbname = company_data[2]
                pwd = company_data[3]
                user_name = company_data[4]
                pg_cursor.execute("select login from res_users where id='%s'"%user_name)
                user_name=pg_cursor.fetchone()[0]
                username = user_name
                pwd = pwd
                db=dbname = dbname
                # print dbname
                log = ('http://%s:%s/xmlrpc/common')%(vpn_ip_addr,port)
                obj = ('http://%s:%s/xmlrpc/object')%(vpn_ip_addr,port)
                print_sock = ('http://%s:%s/xmlrpc/report')%(vpn_ip_addr,port)
                model='invoice.adhoc.master'

                sock_common = xmlrpclib.ServerProxy (log)
                uid = sock_common.login(dbname, username, pwd)
                sock = xmlrpclib.ServerProxy(obj)
                print_sock=xmlrpclib.ServerProxy(print_sock)
                pg_cursor.execute("select id from invoice_adhoc_master where invoice_date >='2017-05-01' and invoice_date < '2017-05-31'")
                invoice_ids=pg_cursor.fetchall()
                for i in invoice_ids:
                    data= sock.execute(dbname, uid, pwd, 'invoice.adhoc.master', 'read',i)
                    print data
                    # id_report = print_sock.report(dbname, uid, pwd, model, i, {'model': 'invoice_print', 'id': i[0], 'report_type':'pdf'})
                    id_report = print_sock.report(dbname,uid,pwd,model,i,{'type':'ir.actions.report.xml','report_name':'invoice_print','datas':data})
            # return {
            #       'type': 'ir.actions.report.xml',
            #       'report_name': 'invoice_print',
            #       'datas': datas,
            #       }
                    partner_ids= sock.execute(dbname, uid, pwd, 'invoice.adhoc.master', 'print_invoice',i)
                    # id_report = print_sock.report(dbname,uid,pwd,model,i,partner_ids)
                    report = print_sock.report_get(db, uid, pwd, id_report)
                    # string_pdf = base64.decodestring(report)
                    file_pdf = open('/tmp/file1.pdf','w')
                    file_pdf.write(partner_ids)
                    file_pdf.close()
                    # print_sock.report_get(db, uid, pwd, id_report)
                    # print partner_ids,"\n\n\n\n",id_report
                    # m
                    # report_id = print_sock.report(db, uid, pwd, 'invoice.adhoc.master', partner_ids, {'model': 'invoice.adhoc.master', 'id': partner_ids, 'report_type': 'pdf'})
                    # time.sleep(3);
                    # state = False
                    # attempt = 0
                    # while not state:
                    #   state = report['state']
                    #   if not state:
                    #       time.sleep(1)
                    #       attempt += 1
                    #   if attempt > 200:
                    #       print 'Abort, too long delay'
                    return partner_ids
                    # return {

这行失败

                report = print_sock.report_get(db, uid, pwd, id_report)

xmlrpclib.protocolerror:<protocolerror for localhost:8765/xmlrpc/report: 500 internal server error>

我解决了:

def awesome():
    f = open('config.txt', 'r')
    lines = f.readlines()
    # line_iter= iter(lines)
    login_credentials = []
    file_name = ''
    dbname=''
    line =lines[0]
    string_pdf =''
    base_path='/home/chaitalikelsukar/Desktop/report_dump/'
    # print line
    # try:
    if (True):
        dbname=str(line.split('=')[1]).split('\n')[0]
        # print dbname

        if dbname:
            # connect_string="dbname='"+dbname+"' user='"+'openerp'+"' host='"+localhost+"' password='"+'openerp'+"' "
            # else:
            connect_string="dbname='"+dbname+"' user='"+'openerp'+"' host='"+'localhost'+"' password='"+'openerp'+"' "
            conn_pg = psycopg2.connect(connect_string)
            pg_cursor = conn_pg.cursor()
            # pg_cursor.execute("")
            pg_cursor.execute("SELECT id from res_company where dbname='%s'"%dbname)
            invoice_ids = pg_cursor.fetchall()
            if tuple(invoice_ids):
                pg_cursor.execute("SELECT company_id from account_account limit 1")
                invoice_ids =pg_cursor.fetchone()
            pg_cursor.execute("select vpn_ip_address,port,dbname,pwd,user_name from res_company where id =%s"%invoice_ids)
            company_data=pg_cursor.fetchone()
            # print invoice_ids,company_data
            vpn_ip_addr = company_data[0]
            port =company_data[1]
            dbname = company_data[2]
            pwd = company_data[3]
            user_name = company_data[4]
            pg_cursor.execute("select login from res_users where id='%s'"%user_name)
            user_name=pg_cursor.fetchone()[0]
            username = user_name
            pwd = pwd
            db=dbname = dbname
            # print dbname
            log = ('http://%s:%s/xmlrpc/common')%(vpn_ip_addr,port)
            obj = ('http://%s:%s/xmlrpc/object')%(vpn_ip_addr,port)
            # print_sock = ('http://%s:%s/xmlrpc/report')%(vpn_ip_addr,port)
            printsock = xmlrpclib.ServerProxy('http://localhost:8765/xmlrpc/report')
            model='invoice.adhoc.master'

            sock_common = xmlrpclib.ServerProxy (log)
            uid = sock_common.login(dbname, username, pwd)
            sock = xmlrpclib.ServerProxy(obj)
            # print_sock=xmlrpclib.ServerProxy(print_sock)
            pg_cursor.execute("select id from invoice_adhoc_master where invoice_date >='2017-05-01' and invoice_date < '2017-05-31' and invoice_number is not null")
            invoice_ids=pg_cursor.fetchall()
            for i in invoice_ids:
                # data= sock.execute(dbname, uid, pwd, 'invoice.adhoc.master', 'read',i)
                pg_cursor.execute("select invoice_number from invoice_adhoc_master where id = '%s' limit 1"%i)
                invoice_number=pg_cursor.fetchone()
                file_name=base_path+invoice_number[0]+'.pdf'
                # print data
                # id_report = print_sock.report(dbname, uid, pwd, model, i, {'model': 'invoice.adhoc.master', 'id': i[0], 'report_type':'pdf'})
                id_report = printsock.report(dbname, uid, pwd, 'invoice_print', i, {'model': 'invoice.adhoc.master', 'id': i[0], 'report_type':'pdf'})
                time.sleep(5)
                state = False
                attempt = 0
                while not state:

                    report = printsock.report_get(dbname, uid, pwd, id_report)

                    # id_report = print_sock.report(dbname,uid,pwd,model,i,{'type':'ir.actions.report.xml','report_name':'invoice_print','datas':data})
            # return {
            #       'type': 'ir.actions.report.xml',
            #       'report_name': 'invoice_print',
            #       'datas': datas,
            #       }
                    state = report['state']
                    if not state:
                        time.sleep(1)
                        attempt += 1
                    if attempt>200:
                        print 'Printing aborted, too long delay !'
                    # partner_ids= sock.execute(dbname, uid, pwd, 'invoice.adhoc.master', 'print_invoice',i)
                    # id_report = print_sock.report(dbname,uid,pwd,model,i,partner_ids)
                    # report = print_sock.report_get(dbname, uid, pwd, id_report)
                    print report
                    string_pdf = base64.decodestring(report['result'])
                    file_pdf = open(file_name,'w')
                    file_pdf.write(string_pdf)
                    file_pdf.close()
                    # print_sock.report_get(db, uid, pwd, id_report)
                    # print partner_ids,"\n\n\n\n",id_report
                    # m
                    # report_id = print_sock.report(db, uid, pwd, 'invoice.adhoc.master', partner_ids, {'model': 'invoice.adhoc.master', 'id': partner_ids, 'report_type': 'pdf'})
                    # time.sleep(3);
                    # state = False
                    # attempt = 0
                    # while not state:
                    #   state = report['state']
                    #   if not state:
                    #       time.sleep(1)
                    #       attempt += 1
                    #   if attempt > 200:
                    #       print 'Abort, too long delay'
    return True

线路问题:

            id_report = printsock.report(dbname, uid, pwd, 'invoice_print', i, {'model': 'invoice.adhoc.master', 'id': i[0], 'report_type':'pdf'})

我在报告 py 中的报告名称是 report.invoice_print 而不是默认的 report.invoice.adhoc.master。