Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

odoo 12 - Sales order line

Hello i'm doing a code with python 3.7 and i need to delete in a order of Sale al Products with quantity with 0. I'm stuck, i know that it mus be easy but not for me at these time. My last update code:

init.py

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import api, fields
from . import models
from . import wizard
from . import report
from . import sale
from . import sale_order
from . import stock_no0

manifiest.py

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
   {
        'name' : 'Stock_0',
        'version' : '0.1',
        'summary': 'Invoices & Payments',
        'sequence': 15,
        'description': """
    Stock No 0
    ====================      
        """,
        'category': 'Invoicing Management',
        'website': 'http://no_website.com',
        'depends' : ['base_setup', 'product', 'sale', 'analytic', 'portal', 'digest'],
        'data': [
        'views/boton.xml', 
        ],
        'installable': True,
        'application': True,
        'auto_install': False,
    }

stock_no0.py

class SaleOrder(models.Model):

_inherit = 'sale.order'
@api.onchange
def onchange_product_id_check_availability(self):
    for ordr_ln in self.order_line:
        if ordr_ln['qty_available'] < 0:
                warning_mess = {'title': 'Opps! No hay stock',
                                'message': 'No hay suficiente Stock '}  


@api.multi
def action_delete(self):
    for order in self:
        order.order_line.filtered(lambda l: not l.product_uom_qty).unlink()

The view in XML

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <record model="ir.ui.view" id="view_order_form">
        <field name="name">sale.order.form</field>
        <field name="model">sale.order</field>
        <field name="inherit_id" ref="sale.view_order_form"/>
        <field name="arch" type="xml">
            <xpath expr="/form/sheet/notebook/page/field[@name='order_line']" position="before">
                <button name="action_delete" type="object" string="Clean" groups="sales_team.group_sale_manager"/>
            </xpath>
        </field>
    </record>
</odoo>

And the error is AttributeError: type object 'sale.order' has no attribute 'action_delete'.

These code must be to Odoo 12

Thanks you.

question from:https://stackoverflow.com/questions/66062509/sales-order-line

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You got the NameError: name 'models' is not defined because you did not import models.

You can use filtered to get lines with quantity equal to 0 and then call unlink method on the result (record set).

from odoo import models, api


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    @api.multi
    def action_delete(self):
        for order in self:
            order.order_line.filtered(lambda l: not l.product_uom_qty).unlink()

In this case the button type should be object and its name should be the method name.

<button name="action_delete" type="object" string="Clean"  groups="sales_team.group_sale_manager"/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...