如何在openerp 7中为自定义模块创建自定义配置

时间:2014-01-15 07:55:41

标签: openerp-7

如何在openerp 7中为自定义模块创建自定义配置,如下所示

enter image description here

我的python文件

import logging
import shipment_utility
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF
from openerp.osv import fields, osv

_logger = logging.getLogger(__name__)

class shipment_config_settings(osv.osv_memory):
    _name = 'shipment.config.settings'
    _inherit = 'res.config.settings'
    _columns = {
        'default_us_custom_id': fields.many2one('res.partner', 'Us Customs', domain=[('supplier', '=', 1)], help='Select The Us Customs For Generation of Custom statements Invoices'),
        'default_invoice_account_id': fields.many2one('account.account', 'Invoice Account',),
        'default_invoice_journal_id': fields.many2one('account.journal', 'Invoice Journal',),
        'default_invoice_line_account_id': fields.many2one('account.account', 'Invoice line Account',),
        'default_invoice_line_product_id': fields.many2one('product.product', 'Default Product',),
    }

    _defaults = {
        'default_us_custom_id': 1,
        #'default_invoice_account_id': "",
    }

    def get_default_us_custom_id(self, cr, uid, fields, context=None):
        search_ids = self.search(cr, uid, [])
        first_id = search_ids and min(search_ids)
        _logger.warning("ids " + str(search_ids))
        if not search_ids:
            return {'default_us_custom_id': ''}
        us_customs = self.browse(cr, uid, first_id, context)
        res = {}
        for field in fields:
            #_logger.warning("field " + str(field))
            res[field] = getattr(us_customs, field).id
        return res

    """def set_default_username(self, cr, uid, ids, context=None):
        #some code...
        #you can get the field content from some table and return it
        #as a example
        config = self.browse(cr, uid, ids[0], context)
        new_username = config.default_us_custom_id.id
        self.pool.get('shipment.config.settings').write(cr, uid, uid, {'default_us_custom_id': new_username})

class shipment_config_settings_wizard(osv.osv_memory):
    _name = 'shipment.config.settings.wizard'
    _inherit = 'shipment.config.settings'
    _columns = {
        'default_us_custom_id': fields.many2one('res.partner', 'Us Customs', default_model='res.config.settings'),
    }"""


    def create(self, cr, uid, values, context=None):
        search_ids = self.search(cr, uid, [])
        first_id = search_ids and min(search_ids)
        #us_customs = self.browse(cr, uid, first_id, context)
        #_logger.warning("us custom " + str(us_customs.default_us_custom_id.name))
        res = {}
        #for field, val in values.iteritems():
        #    #_logger.warning("key = %s, val = %s" % (field, val))
        #    res[field] = val
        # DOWN STATEMENT IS DOING THE SAME WORK AS ABOVE COMMENTED LOOP
        res.update(values)
        if not first_id:
            return super(shipment_config_settings, self).create(cr, uid, res, context)

        self.write(cr, uid, first_id, res)
        return first_id

我的观看文件

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <!-- Shipment config wizard -->
        <record id="view_shipment_config_settings" model="ir.ui.view">
            <field name="name">Shipment Settings</field>
            <field name="model">shipment.config.settings</field>
            <field name="arch" type="xml">
                <form string="Configure Shipment" version="7.0" class="oe_form_configuration">
                    <header>
                        <button string="Apply" type="object" name="execute" class="oe_highlight"/>
                        or
                        <button string="Cancel" type="object" name="cancel" class="oe_link"/>
                    </header>
                    <div name="shipment.settings">
                        <separator string="Shipment Configuration"/>
                        <group name="Supplier">
                            <group>
                                <field name="default_us_custom_id" class="oe_inline"/>
                                <field name="default_invoice_account_id" class="oe_inline"/>
                                <field name="default_invoice_journal_id" class="oe_inline"/>
                                <field name="default_invoice_line_account_id" class="oe_inline"/>
                                <field name="default_invoice_line_product_id" class="oe_inline"/>
                            </group>
                            <group>
                                <label for="default_us_custom_id" string="Select US Custom Supplier"/>
                            </group>
                        </group>
                    </div>

                    <div name="config_other"/>
                </form>
            </field>
        </record>

        <record id="action_shipment_config" model="ir.actions.act_window">
            <field name="name">Configure Shipment</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">shipment.config.settings</field>
            <field name="view_id" ref="view_shipment_config_settings"/>
            <field name="view_mode">form</field>
            <field name="target">inline</field>
        </record>

    <menuitem id="base.menu_shipment_config" name="Shipment" parent="base.menu_config" sequence="10"
              action="action_shipment_config"/>

        <menuitem id="menu_item_config" parent="menu_section_config" name="US Custom Invoicing"
                  action="action_shipment_config"/>

    </data>
</openerp>

1 个答案:

答案 0 :(得分:0)

模块的设置需要保持在company级别:

# company.py
from openerp.osv import fields, osv

class res_company(osv.Model):
    _name = 'shipment.config.settings'
    _inherit = 'res.config.settings'
    _columns = {
        'default_us_custom_id': fields.many2one(
            'res.partner', 'Us Customs',
            domain=[('supplier', '=', 1)],
            help='Select The US Customs For Generation of ...',
            ),
        'default_invoice_account_id': fields.many2one(
            'account.account', 'Invoice Account',
            ),
        'default_invoice_journal_id': fields.many2one(
            'account.journal', 'Invoice Journal',
            ),
        'default_invoice_line_account_id': fields.many2one(
            'account.account', 'Invoice line Account',
            ),
        'default_invoice_line_product_id': fields.many2one(
            'product.product', 'Default Product',
            ),
    }
    _defaults = {
        'default_us_custom_id': 1,
    }

在您的货件配置文件中,您需要使用映射回公司字段的相关字段:

# shipment.py
import logging
import shipment_utility
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DF
from openerp.osv import fields, osv

_logger = logging.getLogger(__name__)

class shipment_config_settings(osv.osv_memory):
    _name = 'shipment.config.settings'
    _inherit = 'res.config.settings'
    _columns = {
        'company_id': fields.many2one('res.company', 'Company', required=True),
        'default_us_custom_id': fields.related(
            'company_id', 'default_us_custom_id',
            string='US Customs', type='many2one',
            help='Select The Us Customs For Generation of ...',
            ),
        'default_invoice_account_id': fields.related(
            'res.company', 'default_invoice_account_id',
            string='Invoice Account', type='many2one',
            ),
        'default_invoice_journal_id': fields.related(
            'res.company', 'default_invoice_journal_id',
            string='Invoice Journal', type='many2one',
            ),
        'default_invoice_line_account_id': fields.related(
            'res.company', 'default_invoice_line_account_id',
            string='Invoice line Account', type='many2one',
            ),
        'default_invoice_line_product_id': fields.related(
            'res.company', 'default_invoice_line_product_id',
            string='Default Product', type='many2one',
            ),
    }

备注_defaults位于company.py而不是shipment_settings.py; <{1}}中的其他方法不需要。