2015-05-27 41 views
7

Tôi đang tạo báo cáo bằng qWeb trong Odoo 8. Các tệp PDF được tạo này được lưu với tên "mặc định". Tôi muốn đặt tên cụ thể cho mọi tệp được tạo (không phải sau khi tệp đã được lưu, nhưng trong thời gian "thế hệ").Cách đặt tên PDF trong báo cáo qWeb, Odoo?

Điều đó có khả thi không? Nếu có, làm thế nào để làm điều đó?

Xin cảm ơn trước.

Trả lời

2

Trong chung Báo cáo Qweb Menu bạn có thể in Báo cáo Qweb của bạn trong Odoo 8,0

<?xml version="1.0" encoding="utf-8"?> 
<openerp> 
    <data> 
     <report 
      id="report_sale_order" 
      string="Quotation/Order" 
      model="sale.order" 
      report_type="qweb-pdf" 
      file="sale.report_saleorder" 
      name="sale.report_saleorder" 
     /> 
    </data> 
</openerp> 

Trong <report> Tag có các thuộc tính khác nhau để in báo cáo trong Qweb Nếu bạn muốn thay đổi tên của đã in PDF của bạn, sau đó tên Thuộc tính quan trọng hơn đối với chúng tôi.

Dựa trên tên thuộc tính báo cáo tên tập tin PDF của chúng tôi đi kèm qua nghe trong một generalize bạn nên đặt tên cơ sở thuộc tính trên your_module_name.report_name

Nếu bạn muốn thay đổi tên tùy chỉnh lại file PDF của bạn sau đó thay đổi thuộc tính name theo tên báo cáo ngọt ngào của bạn.

Tôi hy vọng điều này nên hữu ích cho bạn .. :)

+0

Cảm ơn. Thực sự nó giúp, nhưng tôi cần phải in 'mỗi' PDF với một tên tùy chỉnh. Ví dụ: Tôi muốn in mọi công việc với tên nó có trong cơ sở dữ liệu. Vì vậy, bất kỳ trợ giúp? – Breba

+0

Có thể là nó có thể nhưng tôi không biết về nó nếu tôi tìm thấy trong tương lai sau đó chắc chắn yêu cầu nó cho bạn. –

3

Trong Odoo 8 bạn có thể vá các report_download phương pháp addons/báo cáo/controllers/main.py như dưới đây (giữa BẮT ĐẦU FIX và END). Sau đó, nó sẽ sử dụng mã cho thuộc tính tệp đính kèm trong định nghĩa hành động báo cáo. Khi hệ thống sau đó sẽ luôn lưu tệp dưới dạng tệp đính kèm trong cơ sở dữ liệu, bạn có thể thay đổi hành vi thêm để chỉ lưu trong cơ sở dữ liệu khi attachment_use được đặt thành True. Bằng cách này, không cần thêm trường bổ sung và lượt xem thay đổi.

@route(['/report/download'], type='http', auth="user") 
def report_download(self, data, token): 
    """This function is used by 'qwebactionmanager.js' in order to trigger the download of 
    a pdf/controller report. 

    :param data: a javascript array JSON.stringified containg report internal url ([0]) and 
    type [1] 
    :returns: Response with a filetoken cookie and an attachment header 
    """ 
    requestcontent = simplejson.loads(data) 
    url, type = requestcontent[0], requestcontent[1] 
    try: 
     if type == 'qweb-pdf': 
      reportname = url.split('/report/pdf/')[1].split('?')[0] 

      docids = None 
      if '/' in reportname: 
       reportname, docids = reportname.split('/') 

      if docids: 
       # Generic report: 
       response = self.report_routes(reportname, docids=docids, converter='pdf') 
       ##### FIX START: switch reportname with the evaluated attachment attribute of the action if available 
       docids = [int(i) for i in docids.split(',')] 
       report_obj = request.registry['report'] 
       cr, uid, context = request.cr, request.uid, request.context 
       report = report_obj._get_report_from_name(cr, uid, reportname) 
       if report.attachment: 
        obj = report_obj.pool[report.model].browse(cr, uid, docids[0]) 
        reportname=eval(report.attachment, {'object': obj, 'time': time}).split('.pdf')[0] 
       ##### FIX END  
      else: 
       # Particular report: 
       data = url_decode(url.split('?')[1]).items() # decoding the args represented in JSON 
       response = self.report_routes(reportname, converter='pdf', **dict(data)) 

      response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % reportname) 
      response.set_cookie('fileToken', token) 
      return response 
     elif type =='controller': 
      reqheaders = Headers(request.httprequest.headers) 
      response = Client(request.httprequest.app, BaseResponse).get(url, headers=reqheaders, follow_redirects=True) 
      response.set_cookie('fileToken', token) 
      return response 
     else: 
      return 
    except Exception, e: 
     se = _serialize_exception(e) 
     error = { 
      'code': 200, 
      'message': "Odoo Server Error", 
      'data': se 
     } 
     return request.make_response(html_escape(simplejson.dumps(error))) 

Bạn có thể thiết lập các thuộc tính tập tin đính kèm của một hành động báo cáo ví dụ theo cách này:

<?xml version="1.0" encoding="UTF-8"?> 
<openerp> 
    <data> 
    <!-- rename the file names of the standard rfq report --> 
     <record id="purchase.report_purchase_quotation" model="ir.actions.report.xml"> 
      <field name="attachment">'RFQ_'+object.name+'.pdf'</field> 
     </record>  
    </data> 
</openerp> 

Đây là bản vá của _check_attachment của đối tượng báo cáo trong addons/báo cáo/report.py để sửa đổi hành vi attachment_use:

@api.v7 
def _check_attachment_use(self, cr, uid, ids, report): 
    """ Check attachment_use field. If set to true and an existing pdf is already saved, load 
    this one now. Else, mark save it. 
    """ 
    save_in_attachment = {} 
    save_in_attachment['model'] = report.model 
    save_in_attachment['loaded_documents'] = {} 

    if report.attachment: 
     for record_id in ids: 
      obj = self.pool[report.model].browse(cr, uid, record_id) 
      filename = eval(report.attachment, {'object': obj, 'time': time}) 

      # If the user has checked 'Reload from Attachment' 
      if report.attachment_use: 
       alreadyindb = [('datas_fname', '=', filename), 
           ('res_model', '=', report.model), 
           ('res_id', '=', record_id)] 
       attach_ids = self.pool['ir.attachment'].search(cr, uid, alreadyindb) 
       if attach_ids: 
        # Add the loaded pdf in the loaded_documents list 
        pdf = self.pool['ir.attachment'].browse(cr, uid, attach_ids[0]).datas 
        pdf = base64.decodestring(pdf) 
        save_in_attachment['loaded_documents'][record_id] = pdf 
        _logger.info('The PDF document %s was loaded from the database' % filename) 

        continue # Do not save this document as we already ignore it 

      # FIX START (commenting out below lines and indenting the else clause one level down) 
      # If the user has checked 'Save as Attachment Prefix' 
      #~ if filename is False: 
       #~ # May be false if, for instance, the 'attachment' field contains a condition 
       #~ # preventing to save the file. 
       #~ continue 
       else: 
        save_in_attachment[record_id] = filename # Mark current document to be saved 
      # FIX END 
    return save_in_attachment 
+0

Tôi có thể xác nhận nó hoạt động như một sự quyến rũ. –

2

addons \ báo cáo \ controllers \ main.py

Line: 127

response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % reportname) 

thay đổi nó với

invoicename="ma_facture" #create a variable you can add date for example stfftime("%d-%m-%Y") 
response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % invoicename) 
+0

Xin chào Usman Maqbool, tôi đã chấp thuận đề xuất chỉnh sửa cuối cùng của bạn. Tôi muốn đề xuất rằng, khi chỉnh sửa, bạn không đặt tên của các ngôn ngữ lập trình trong 'đánh dấu mã' và xóa" Cảm ơn "khỏi bài đăng. Trân trọng! –

+1

al-right, tôi sẽ chăm sóc lần sau. –

+0

@ S.L. Barth bây giờ hãy loại bỏ lệnh cấm và tôi đã nhận được sai lầm của tôi ... xin vui lòng. –

2
<report 
     id="report_sale_order" 
     string="Quotation/Order" 
     model="sale.order" 
     report_type="qweb-pdf" 
     file="sale.report_saleorder" 
     name="sale.report_saleorder" 
/> 

là chính xác cho tên chuỗi được một tên in PDF làm

2

Bạn có thể đặt tên báo cáo động bằng cấu hình nhưng sẽ áp dụng khi bạn in một báo cáo.

Dưới đây là ví dụ về In tên tùy chỉnh trong báo cáo. Tạo một trường trong ir.actions.report.xml, trong đó người dùng có thể định cấu hình tên báo cáo.

from openerp import models, fields 
class IrActionsReportXml(models.Model): 
    _inherit = 'ir.actions.report.xml' 

    download_filename = fields.Char(
     'Download filename') 

Bây giờ bạn cần tạo hai tệp.

  1. Báo cáo điều khiển

    from openerp import http 
    from openerp.addons.mail.models import mail_template 
    from openerp.addons.report.controllers.main import ReportController 
    from openerp.addons.web.controllers.main import content_disposition 
    
    
    class ReportController(ReportController): 
        @http.route([ 
         '/report/<path:converter>/<reportname>', 
         '/report/<path:converter>/<reportname>/<docids>', 
        ]) 
        def report_routes(self, reportname, docids=None, converter=None, **data): 
         response = super(ReportController, self).report_routes(
          reportname, docids=docids, converter=converter, **data) 
         if docids: 
          docids = [int(i) for i in docids.split(',')] 
         report_xml = http.request.session.model('ir.actions.report.xml') 
         report_ids = report_xml.search(
          [('report_name', '=', reportname)]) 
         for report in report_xml.browse(report_ids): 
          if not report.download_filename: 
           continue 
          objects = http.request.session.model(report.model)\ 
           .browse(docids or []) 
          generated_filename = mail_template.mako_template_env\ 
           .from_string(report.download_filename)\ 
           .render({ 
            'objects': objects, 
            'o': objects[:1], 
            'object': objects[:1], 
            'ext': report.report_type.replace('qweb-', ''), 
           }) 
          response.headers['Content-Disposition'] = content_disposition(
           generated_filename) 
         return response 
    
        @http.route(['/report/download']) 
        def report_download(self, data, token): 
         response = super(ReportController, self).report_download(data, token) 
         # if we got another content disposition before, ditch the one added 
         # by super() 
         last_index = None 
         for i in range(len(response.headers) - 1, -1, -1): 
          if response.headers[i][0] == 'Content-Disposition': 
           if last_index: 
            response.headers.pop(last_index) 
           last_index = i 
         return response 
    

2.Report.py

import json 
    from openerp import http 
    from openerp.addons.web.controllers import main 
    from openerp.addons.mail.models import mail_template 


    class Reports(main.Reports): 
     @http.route('/web/report', type='http', auth="user") 
     @main.serialize_exception 
     def index(self, action, token): 
      result = super(Reports, self).index(action, token) 
      action = json.loads(action) 
      context = dict(http.request.context) 
      context.update(action["context"]) 
      report_xml = http.request.env['ir.actions.report.xml'] 
      reports = report_xml.search([ 
       ('report_name', '=', action['report_name']), 
       ('download_filename', '!=', False)]) 
      for report in reports: 
       objects = http.request.session.model(context['active_model'])\ 
        .browse(context['active_ids']) 
       generated_filename = mail_template.mako_template_env\ 
        .from_string(report.download_filename)\ 
        .render({ 
         'objects': objects, 
         'o': objects[0], 
         'object': objects[0], 
        }) 
       result.headers['Content-Disposition'] = main.content_disposition(
        generated_filename) 
      return result 

cộng đồng Odoo Cung cấp cho chúng ta một mô-đun mặc định cho tên báo cáo tùy chỉnh. bạn có thể trực tiếp cài đặt mô-đun này và đặt tên báo cáo như: $ {o.name}

Đây o có nghĩa là hồ sơ của bạn.

Dưới đây là liên kết của mô-đun cộng đồng odoo dành cho V8 và V9.

https://www.odoo.com/apps/modules/9.0/report_custom_filename/

https://www.odoo.com/apps/modules/8.0/report_custom_filename/ Điều này có thể giúp bạn.

0

Cách dễ dàng để thay đổi báo cáo tên tập tin PDF trong Odoo v10:

Chỉ cần thêm mã này, nơi bạn viết thẻ báo cáo, và cung cấp cho cùng một ID trong cả hai, báo cáo và hồ sơ bổ sung mới. Như sau:

<report id="report_quote_temp" string="Quote Template" model="sale.order" report_type="qweb-pdf" 
    file="custom_template.report_quote_custom" name="custom_template.report_quote_custom" 
    menu="True" paperformat="custom_template.custom_paperformat" /> 

<record id="report_quote_temp" model="ir.actions.report.xml"> 
    <field name="print_report_name">'SALES_'+object.name+'.pdf'</field> 
</record> 

Nó sẽ in tập tin báo cáo như SALES_SO004.pdf, nơi SO004 là bán hoặc số ngoặc kép.

+0

Cảm ơn bạn clemens .... –

Các vấn đề liên quan