无法使用关闭的游标

2024-05-23 22:39:53 发布

您现在位置:Python中文网/ 问答频道 /正文

在对res.partner模型进行了一些操作之后,我在第二次调用make\u-po方法时得到了这个异常。如果我正确地停止并重新启动我的stacku方法,则停止并重新启动我的stacku方法:

class procurement_order(osv.osv):
    _inherit = 'procurement.order'

    counter = 0
    global_po_id = None
    line_counter = 0
    first_line = None
    def make_po(self, cr, uid, ids, context=None):
        _logger = logging.getLogger(__name__)
        """ Resolve the purchase from procurement, which may result in a new PO creation, a new PO line creation or a quantity change on existing PO line.
        Note that some operations (as the PO creation) are made as SUPERUSER because the current user may not have rights to do it (mto product launched by a sale for example)
        @return: dictionary giving for each procurement its related resolving PO line.
        OVERRIDE: If we already have a purchase order on a draft state with the same product, do not append the lines.
        Instead create a new Purchase Order
        """
        company = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id
        po_obj = self.pool.get('purchase.order')
        seq_obj = self.pool.get('ir.sequence')
        sales_orders = self.pool.get('sale.order')
        po_line = self.pool.get('purchase.order.line')

        # Get the procurement ID ("key" == Procurement ID, "value" == PO line)
        procurement_purchase_order = super(procurement_order, self).make_po(cr, uid, ids, context=context)
        key, value = procurement_purchase_order.popitem()
        procurement_purchase_order[key] = value

        # Search for the equivalent purchase.order.line
        p_o_line_returned_id = po_line.search(cr, uid, [('id', '=', value)])
        p_o_line_returned = po_line.browse(cr, uid, p_o_line_returned_id, context=context)

        # get purchase order from its line
        purchase_order_from_line_ID = po_obj.search(cr, uid, [('id', '=', p_o_line_returned.order_id.id)])
        purchase_order_from_line = po_obj.browse(cr, uid, purchase_order_from_line_ID, context=context)

        # Get me the current procurement
        current_procurement_ID = self.search(cr, uid, [('id', '=', key)])
        current_procurement = self.browse(cr, uid, current_procurement_ID, context=context)

        # Get me the line ids of the lines that are related to purchase_order_from_line_ID
        lines_IDS = po_line.search(cr, uid, [('order_id', '=', purchase_order_from_line_ID[0])])
        _logger.warning('lines_IDS' + str(lines_IDS))

        # Check the source of all the lines in thelines_IDS
        for line in po_line.browse(cr, uid, lines_IDS, context=context):
            _logger.warning('First line value ' + str(procurement_order.first_line))
            _logger.warning('')
            if line.procurement_ids != 0:
                if procurement_order.line_counter == 0:
                    procurement_order.first_line = line 
                    procurement_order.line_counter += 1

                    # Compare the first line procurement.group_id with the current procurement.group_id
                if procurement_order.first_line.procurement_ids and procurement_order.first_line.procurement_ids[0].group_id == current_procurement[0].group_id:
                    _logger.warning('GROUP IDS ARE EQUAL')
                else:
                    _logger.warning('GROUP IDS ARE NOT EQUAL')

                    # if we have inequality between the group ids then that means that the current "line" was created by a different SO
                    to_remove = []
                    to_remove.append(line.id)

                    # from the current_procurement.group_id get the PO.line and remove it
                    temp_group_id = current_procurement[0].group_id.id
                    _logger.warning(str(temp_group_id))
                    temp_line_for_removal_ID = po_line.search(cr, uid, [('id', '=', temp_group_id)])
                    po_line.unlink(cr, uid, temp_line_for_removal_ID, context=context)


                    _logger.warning('Removed from Purchase Order ' + str(purchase_order_from_line_ID[0]) + ' Lines ' + str(to_remove))
                    pass_ids = []
                    # for all the procrements that belong to my SO
                    # for procurement in self.browse(cr, uid, ids, context=context):
                    partner = self._get_product_supplier(cr, uid, current_procurement, context=context)
                    if not partner:
                        self.message_post(cr, uid, [current_procurement.id], _('There is no supplier associated to product %s') % (current_procurement.product_id.name))
                        procurement_purchase_order[current_procurement.id] = False
                    else:
                        schedule_date = self._get_purchase_schedule_date(cr, uid, current_procurement, company, context=context)
                        purchase_date = self._get_purchase_order_date(cr, uid, current_procurement, company, schedule_date, context=context) 
                        line_vals = self._get_po_line_values_from_proc(cr, uid, current_procurement, partner, company, schedule_date, context=context)  # get the lines from here
                        name = seq_obj.get(cr, uid, 'purchase.order') or _('PO: %s') % current_procurement.name
                        po_vals = {
                        'name': name,
                        'origin': current_procurement.origin,
                        'partner_id': partner.id,
                        'location_id': current_procurement.location_id.id,
                        'picking_type_id': current_procurement.rule_id.picking_type_id.id,
                        'pricelist_id': partner.property_product_pricelist_purchase.id,
                        'currency_id': partner.property_product_pricelist_purchase and partner.property_product_pricelist_purchase.currency_id.id or current_procurement.company_id.currency_id.id,
                        'date_order': purchase_date.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                        'company_id': current_procurement.company_id.id,
                        'fiscal_position': po_obj.onchange_partner_id(cr, uid, None, partner.id, context=context)['value']['fiscal_position'],
                        'payment_term_id': partner.property_supplier_payment_term.id or False,
                        'dest_address_id': current_procurement.partner_dest_id.id,
                    }

                    # The first time create the PO and add a line to it
                    if procurement_order.counter == 0:
                        po_id = self.create_procurement_purchase_order(cr, uid, current_procurement, po_vals, line_vals, context=context)
                        procurement_order.global_po_id = po_id
                        po_line_id = po_obj.browse(cr, uid, po_id, context=context).order_line[0].id
                        pass_ids.append(current_procurement.id)
                        procurement_purchase_order[current_procurement.id] = po_line_id
                        self.write(cr, uid, [current_procurement.id], {'purchase_line_id': po_line_id}, context=context)
                        _logger.warning('New Purchase order created ' + str(po_id))
                    else:
                        # All the other lines from the procurements will be appended here
                        proc_order = self.pool.get('procurement.order')
                        purchase_order_created = po_obj.browse(cr, uid, procurement_order.global_po_id, context=context)  # get the Purchase Order
                        line_vals = proc_order._get_po_line_values_from_proc(cr, uid, current_procurement, partner, company, schedule_date, context=context)  # Get the line from the procurement
                        po_line_obj = self.pool.get('purchase.order.line')
                        line_vals['order_id'] = purchase_order_created[0].id
                        # if there exists a line with the same id, do not add it
                        if len(po_line_obj.search(cr, uid, [('order_id', '=', purchase_order_created[0].id)])) == 0:
                            po_line_id = po_line_obj.create(cr, uid, line_vals, context=context)
                            _logger.warning('Appending line ' + str(line_vals) + 'to existing Purchase order ' + str(purchase_order_created))
                        else:
                            _logger.warning('Line has already been appended, dismissed')
                    if pass_ids:
                        self.message_post(cr, uid, pass_ids, body=_("Draft Purchase Order created"), context=context)
                    procurement_order.counter += 1
        self.pool.get('purchase.order.line').split_lines(cr, uid, [procurement_purchase_order[ids[0]]], context=context)
        return procurement_purchase_order

堆栈跟踪:

^{pr2}$

Tags: thefromselfidpartneruidgetcontext
1条回答
网友
1楼 · 发布于 2024-05-23 22:39:53

我想是因为你误用了奥多奥。首先,在Odoo中,你从来没有像这样声明类级别的属性。。。在

counter = 0
global_po_id = None
line_counter = 0
first_line = None

然后你在这里提到:

^{pr2}$

问题是采购订单的类型osv公司但这行代码应该使用浏览记录。当它试图跟踪跟踪跟踪并从它认为是浏览记录的数据项时,它到达了需要使用游标而没有光标可使用的地步,因此出现了错误。在

最好想想osv公司类作为一个只读元类,它描述数据库中的表,但也允许您定义一些静态或类方法。在

您使用的采购订单应该是浏览的结果。在

my_procurement_order = self.browse(cr, uid, ids[0], context=context)

下一个问题是您不能在运行时为browse记录分配像first_line这样的值,它们是只读的(注意,这在odoo8newapi中发生了变化,但这是另一回事)。在

这里的一个常见做法是维护类似本地词典的内容,您可以参考这些内容:

第一行={我的采购_订单.id:a_行}

相关问题 更多 >