gpt4 book ai didi

python - 如何在 Odoo 中返回两个变量?

转载 作者:行者123 更新时间:2023-11-30 22:04:49 25 4
gpt4 key购买 nike

我需要检查两个条件并将详细信息打印在报告上。但问题是我无法返回这两个变量。我将提交代码并在下面提及更多内容。

类 TaxDetailReport(models.TransientModel): _name = 'tax.detail.report'

start_date = fields.Datetime(required=True)
end_date = fields.Datetime(required=True)
vat_oman_id = fields.Many2one('vat.oman.import', string="VAT Oman ID")

@api.multi
def generate_report(self):
for file in self:
if file.start_date and file.end_date:
record_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
('date', '<=', self.end_date),
('account_tax_id.type_tax_use', '=', 'sale')
])
purchase_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
('date', '<=', self.end_date),
('account_tax_id.type_tax_use', '=', 'purchase')
])
else:
raise UserError("Record does not exist")

result['file'] = {'print': [(record_ids,purchase_ids)]}
return self.env["report"].get_action(result, 'kg_oman_vat.report_tax_details')

我需要返回那些 product_idsrecord_idsgenerate_report 是向导中的按钮。

class VATOmanImport(models.Model):
_name = 'vat.oman.import'
_rec_name = 'partner_id'
_description = 'Oman VAT Import'

partner_id = fields.Many2one('res.partner', string="Name", required=True)
invoice_desc = fields.Char(string="Description", help="Invoice Description")
date = fields.Date(string="Date")
account_tax_id = fields.Many2one('account.tax', string="Tax Type")
state_id = fields.Many2one('res.country.state', string="State", required=True,
domain="[('country_id', '=','Oman')]")
invoice_amount = fields.Float(string="Invoice Amount", required=True)
tax_amount = fields.Float(string="Total Tax", compute='_compute_tax_amount')
company_id = fields.Many2one('res.company', string='Company', index=True,
default=lambda self: self.env.user.company_id)

上面刚刚提到的是主类,需要从这里获取详细信息。

有什么解决办法吗?希望有人帮忙。

最佳答案

据我了解,您希望在报告中显示这些数据。

因此您需要了解报告中对您有帮助的内容。

您可以像在Python代码中一样调用t-esct-set中的方法。

假设我想在报告中显示一个复杂的值,那么我要做的是:

我创建了一个方法,仅计算并返回我想要打印的值。

  @api.multi
def calculate_complicated_value(self):
.....
.....
.....
return value

在我的模板中,我可以调用此方法并打印

  <t t-foreach="docs" t-as="rec">

<!-- I prefer always to save it in a variable first -->
<t t-set='result' t-value='rec.calculate_complicated_value()'/>
<!-- And here I can loop on my result or handle anything the method call returns -->

我更喜欢这种技术,而不是像 Odoo 开发人员在标准模块中那样在我的 get_action 调用上传递数据。

您可以了解如何将数据传递到报告模板并向他们展示您需要创建额外的 AbstractModel 并且名称必须以 report 开头。

根据您的情况,您可以尝试以下解决方案:

     _name = 'tax.detail.report'

@api.multi
def generate_report(self):
# in report docs = self
return return self.env["report"].get_action(self, 'kg_oman_vat.report_tax_details')


@api.multi
def compute_purschaces(self):
# i don't think you need to loop here because you are calling
# the method with one record in the report
# you can refactor it later if you want
for file in self:
if file.start_date and file.end_date:
record_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
('date', '<=', self.end_date),
('account_tax_id.type_tax_use', '=', 'sale')
])
purchase_ids = self.env['vat.oman.import'].search([('date', '>=', self.start_date),
('date', '<=', self.end_date),
('account_tax_id.type_tax_use', '=', 'purchase')
])
return {'record_ids': record_ids, 'purchase_ids': purchase_ids}
else:
# raising error from report calls is not a good thing the message will look ugly ^^
# try to add this check in generate_report so the message look nice for your user
raise UserError("Record does not exist")
return False

在您的模板中

           <t t-foreach="docs" t-as="rec">

<t t-set="result" t-value="rec.compute_purschaces()"/>

<!-- now if you want to show record_ids in table or something you can access like this result['record_ids'] -->
<t t-foreach="result['record_ids']" t-as="record">
........
.......

在您的报告操作中,模型应为:'tax.detail.report'

  <report
...
...
model="tax.detail.report"
.....
.....
./>

这就是我的做法,它比将额外参数 data 传递给 get_action 调用并创建特殊的 AbstractModel 更容易在数据进入模板之前对其进行处理,以确保文档设置正确等等。希望你明白这个想法

关于python - 如何在 Odoo 中返回两个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53172234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com