【问题标题】:addition of months to date in Odoo 8在 Odoo 8 中添加迄今为止的月份
【发布时间】:2017-08-16 06:56:41
【问题描述】:

我希望此模块从两个字段计算到期日期,即制造日期和 bbm(最好在几个月之前)。到期字段应添加制造日期、bbm 并将其值显示为只读。下面是我正在使用的代码。

class omfed_items(osv.Model):
"""
items or products profilling
"""
_name = 'omfed.items'
_description = 'omfed items'
_order = 'code asc'

_columns = {
    'name': fields.char('Name', required=True),
    'code': fields.integer('Code', required=True),
    'category_id':fields.many2one('omfed.category','Category',required=True),
    'mfg_date':fields.date('Manufacturing Date'),
    'p_manager':fields.many2one('hr.employee','Product Manager'),
    'bbm':fields.integer('Best before use (in Months)'),
    'exp_date':fields.date(compute='addmonths', string="Expiry Date"),
}
def addmonths(self):
   try:
       targetdate = omfed_items.mfg_date
       targetmonths = omfed_items.bbm+targetdate.month
       if targetmonths%12 == 0:
            targetmonth = 12
       else:
            targetmonth = targetmonths%12
            if favorEoM == True:
                testdate = date+datetime.timedelta(days=1)
                if testdate.day == 1:
                    targetdate.replace(year=targetdate.year+int((targetmonths+1)/12),month=(targetmonth%12+1),day=1)
                    targetdate+=datetime.timedelta(days=-1)
                else:
                    targetdate.replace(year=targetdate.year+int(targetmonths/12),month=(targetmonth))
            else:
                targetdate.replace(year=targetdate.year+int(targetmonths/12),month=(targetmonth))
            return targetdate
   except:
     targetdate.replace(year=targetdate.year+int((targetmonths+1)/12),month=(targetmonth%12+1),day=1)
     targetdate+=datetime.timedelta(days=-1)
     return targetdate

class omfed_category(osv.Model):
    """ category """
    _name = 'omfed.category'
    _description = 'omfed category'
    _columns={
        'name': fields.char('Category Name',required=True),
        'code': fields.integer('Category Code',required=True),
        'items_id':fields.one2many('omfed.items','category_id','Items'),
    }

XML 文件

<?xml version="1.0"?>
<openerp>
   <data>
    <menuitem id='menu_omfed' name='OMFED'/>

    <record model="ir.ui.view" id="omfed_items_search_view">
        <field name="name">Search</field>
        <field name="model">omfed.items</field>
        <field name="arch" type="xml">
            <search string="Search">
                <field name="name" filter_domain="['|', ('name', 'ilike', self), ('note', 'ilike', self)]"/>
                <field name="code"/>
                <group expand="0" string="Group By">
                    <filter name="name" string="PRODUCT" context="{'group_by':'name'}" help="product"/>
                    <filter name="code" string="CODE" context="{'group_by':'code'}" help="product code"/>
                    <filter name="p_manager" string="PRODUCT MANAGER" context="{'group_by':'p_manager'}" help="product manager"/>
                </group>
            </search>
        </field>
    </record>

    <record model="ir.ui.view" id="omfed_items_tree_view">
        <field name="name">omfed_items_tree</field>
        <field name="model">omfed.items</field>
        <field name="arch" type="xml">
            <tree string="Products or Items">
                  <field name="name"/>
                  <field name="code"/>
                  <field name="category_id"/>
                  <field name="mfg_date"/>
                  <field name="bbm"/>
                  <field name="exp_date"/>
                  <field name="p_manager"/>
            </tree>
        </field>
    </record>

    <record model="ir.ui.view" id="omfed_items_form_view">
        <field name="name">omfed_items_form</field>
        <field name="model">omfed.items</field>
        <field name="arch" type="xml">
            <form string="Products or Items">
                        <group col="4" colspan="1">
                            <field name="name"/>
                            <field name="code"/>
                            <field name="category_id"/>
                            <field name="mfg_date" on_change="addmonths(mfg_date,bbm)"/>
                            <field name="bbm"/>
                            <field name="exp_date"/>
                            <field name="p_manager"/>
                        </group>
            </form>
        </field>
    </record>

    <record model="ir.actions.act_window" id="action_omfed_items_form">
        <field name="name">omfed_items</field>
        <field name="res_model">omfed.items</field>
        <field name="view_mode">tree,form</field>
    </record>
     <menuitem name="Items" parent="menu_omfed" id="menu_omfed_items" action="action_omfed_items_form" />
     <menuitem name="Dairy_Items" parent="menu_omfed_items" id="menu_omfed_dairy_items" action="action_omfed_items_form" />
    <record model="ir.ui.view" id="category_tree_view">
        <field name="name">category_tree</field>
        <field name="model">omfed.category</field>
        <field name="arch" type="xml">
            <tree string="Category of Items">
                <field name="name"/>
                <field name="code"/>
                <field name="items_id"/>
            </tree>
        </field>
    </record>
    <record model="ir.ui.view" id="category_form_view">
        <field name="name">category_form</field>
        <field name="model">omfed.category</field>
        <field name="arch" type="xml">
            <form sting="Category of Items">
                <group col="4" colspan="1">
                  <field name="name"/>
                  <field name="code"/>
                  <field name="items_id"/>
                </group>
            </form>
        </field>
    </record>
    <record model="ir.ui.view" id="category_search_view">
        <field name="name">category_search</field>
        <field name="model">omfed.category</field>
        <field name="arch" type="xml">
            <search string="Search">
                <field name="name"/>
                <field name="code"/>
                <field name="items_id"/>
                <group expand="0" string="Group By">
                    <filter name="cname" string="CATEGORY NAME" context="{'group_by':'cname'}" help="category name"/>
                    <filter name="ccode" string="CATEGORY CODE" context="{'group_by':'ccode'}" help="category code"/>
                </group>
            </search>
        </field>
    </record>
    <record model="ir.actions.act_window" id="action_omfed_category">
        <field name="name">omfed_categry</field>
        <field name="res_model">omfed.category</field>
        <field name="view_mode">tree,form</field>
    </record>
    <menuitem name="Categories" parent="menu_omfed_items" id="menu_omfed_category" action="action_omfed_category" />
</data>

提前感谢您的解决方案。

【问题讨论】:

    标签: module openerp odoo-8 openerp-8


    【解决方案1】:

    您可以使用以下代码:

    from datetime import datetime, timedelta
    self.exp_date = datetime.strptime(self.mfg_date, '%Y-%m-%d') + timedelta(days=(self.bbm * 30))
    

    【讨论】:

      猜你喜欢
      • 2015-04-22
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多