0

我正在尝试使用一种方法打印多张发票,到目前为止我已经尝试了两种方法,从在 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
            }

上面的代码返回范围内的所有发票,但打印后我无法移动它

方法二:

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>
4

1 回答 1

0

我解决了它:

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。

于 2017-06-16T12:59:32.750 回答