Wednesday 30 March 2022

Issue with formatting date in Qweb Odoo

Yes, This happens in odoo v12 or later versions.

In your SO, PO or any document/record date is 26/03/2022 but when you print/download pdf report it show like 26/03/2023

<span t-field="request_date_to" t-options='{"widget": "date", "format": "dd MMM Y"}'/>

Above line is printing date like : 26 Mar 2023 insted 26 Mar 2022


To fix this you need to use like this

<span t-esc="datetime.datetime.strptime(date_to, '%Y-%m-%d').strftime('%d %b %Y')"/>
<span t-esc="o.date_invoice.strftime('%d %b %Y')"/>
<span t-esc="datetime.datetime.strftime(date_to, '%d %b %Y')" /> 


It will print correct date like : 26 Mar 2022


Share:

Monday 25 September 2017

Override xml action in odoo

Yes, you can override the xml action in Odoo any version like below.

Here i will tell how Sale Order action can be override by example.

Default Sales Orders action is

<record id="action_orders" model="ir.actions.act_window">
            <field name="name">Sales Orders</field>
            <field name="type">ir.actions.act_window</field>
            <field name="res_model">sale.order</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,kanban,form,calendar,pivot,graph</field>
            <field name="search_view_id" ref="sale_order_view_search_inherit_sale"/>
            <field name="context">{}</field>
            <field name="domain">[('state', 'not in', ('draft', 'sent', 'cancel'))]</field>
            <field name="help" type="html">
                <p class="oe_view_nocontent_create">
                    Create a Quotation, the first step of a new sale.
                </p><p>
                    Once the quotation is confirmed, it becomes a sales order.
                    You'll be able to invoice it and collect payments.
                    From the <i>Sales Orders</i> menu, you can track delivery
                    orders or services.
                </p>
        </field>
</record>

To override just change that part which you want to change and add module name before external id of action like below...

<record id="sale.action_orders" model="ir.actions.act_window">
       <field name="domain">[('is_a_booking','=',False),('state', 'not in', ('draft', 'sent', 'cancel'))]</field>

   </record>

Thanks.
Share:

Saturday 23 September 2017

Changing SQL constraints in inheriting models in Odoo 8

In OpenERP 7 there was an established pattern that allowed creators of inheriting model classes to modify sql constrained defined in a parent class. You just needed to define a new constraint with the same name (the technique is for example described in this forum thread).
In Odoo 8 this no longer works. If you define a constrain with the same name as the one in parent model, Odoo tries to use them both, but the parent’s constraints are executed later, so child constraint is immediately overwritten by the parent constraint. I created a ticket in Odoo’s bug tracker about the issue, but for now you can use a simple workaround.
The workaround consists of changing the list of SQL constraints in _auto_init method on model class. This will run after Odoo already performed its (faulty?) merge of constraints from all parents and children, but before it sends them to the database.
Example 1

class Foo(models.Model):
    _inherit = 'my.foo'

    # ...

    def _auto_init(self, cr, context=None):
        self._sql_constraints = [
            ('name_uniq', 'unique(name)', 'Two foos with the same name? Impossible!')
        ]
        super(Foo, self)._auto_init(cr, context)

Alternatively you could try to use the pattern that worked in OpenERP 7 and just reverse the _sql_constraints list to fix the order in which they are sent to the database:
Example 2

class Foo(models.Model):
    _inherit = 'my.foo'

    # ...

    _sql_constraints = [
        ('name_uniq', 'unique(name)', 'Two foos with the same name? Impossible!')
    ]

    def _auto_init(self, cr, context=None):
        self._sql_constraints = self._sql_constraints.reverse()
        super(Foo, self)._auto_init(cr, context)

Please note that those examples are not equivalent. In the first example parent’s list of constraints will be replaced by the new list. In the second example those two list will be merged.
Share:

Showing a menu badge in Odoo

Odoo allows programmers to display badges with numeric values next to menu items:









To use this functionality, you need to configure your model. Odoo provides an _ir.needaction_mixin for you to inherit from:

_inherit = ['ir.needaction_mixin']

Then you need to define a method called  _needaction_domain_get. It should return a domain selecting those objects that should be counted towards the number on the badge.


@api.model
def _needaction_domain_get(self):
    return [('state', '=', 'new')]
Complete example

class Horse(models.Model):
    _name = 'horse'
    _inherit = ['ir.needaction_mixin']

    STATES = [
        ('healthy', "Healthy horse"),
        ('sick', "Sick horse"),
    ]

    name = fields.Char(required=True)
    state = fields.Selection(STATES, default='healthy')

    @api.model
    def _needaction_domain_get(self):
        """
        Show a count of sick horses on the menu badge.
        An exception: don't show the count to Bob,
        because he worries too much!
        """
        if self.env.user.name == "Bob":
            return False  # don't show to Bob!
        return [('state', '=', 'sick')]
More Control

Instead of defining _needaction_domain_get method you could alternatively define a _needaction_count method and return a number of your choice directly:

@api.model
def _needaction_count(self, domain=None):
    return 12


Another example, making use of the domain argument:

@api.model
def _needaction_count(self, domain=None):
    """
    Count all objects in a view, deducting a dozen
    before displaying on the badge
    (we don't want to alarm people with big numbers)
    """
    return self.search_count(domain or []) - 12


Share: