Ahmed Hached
Ahmed Hached

Reputation: 150

Send an Email to multiple customers containing multiple products

I'm trying to send an email to multiple partners containing multiple products with Odoo.

   #OdepoOffer.py

     # -*- coding: utf-8 -*-

from openerp import models, fields, api

"""
Class nomRelation Pour la gestion des noms des relations
"""
class odepoOffer(models.Model):
    _name = 'odepo.offer'
    name = fields.Char('Nom Offre' ,size=32)
    odepoContactId = fields.Many2many('res.partner', ondelete='no action', string="Panier Clients")
    odepoProductId = fields.Many2many('product.product', ondelete='no action', string="Panier Produits")

    @api.multi
    def email_partner(self):
        '''
        This function opens a window to compose an email, with the edi sale template message loaded by default
        '''
        self.ensure_one()
        ir_model_data = self.env['ir.model.data']
        try:
            compose_form_id = ir_model_data.get_object_reference('mail', 'email_compose_message_wizard_form')[1]
        except ValueError:
            compose_form_id = False

        # It's worth noting that Odoo 9 uses 'mail.template' whereas Odoo 8 uses 'email.template'
        # template_id = self.env['email.template'].search([('name', '=', 'Odepo Offer')], limit=1)
        ctx = dict()
        ctx.update({
            'default_model': 'res.partner',
            'default_res_id': False,
            'default_use_template': True,
            'default_template_id': False,
            'default_composition_mode': 'comment',
            'email_to':'[email protected]',
            'subject':'mario',
            'skip_notification': True,
        })
        return {
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'mail.compose.message',
            'views': [(compose_form_id, 'form')],
            'view_id': compose_form_id,
            'target': 'new',
            'context': ctx,
        }
                #       values['subject'] = val
                # values['email_to'] = val1
                # values['body_html'] = val2
                # values['body'] = val3
                # values['res_id'] = False
                # values['attachment_ids'] = val4

and the view:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>

        <record model="ir.ui.view" id="view_odepo_offer_tree">
            <field name="name">odepo.offer.tree</field>         
            <field name="model">odepo.offer</field>
            <field name="arch" type="xml">
                <form string="Odepo Offre">
                    <group>
                        <field name="name" string="Name"/>   
                        <field name="odepoContactId" widget="many2many_tags"  options="{'no_create_edit': True}"/>  
                        <field name="odepoProductId" widget="many2many_tags"  options="{'no_create_edit': True}"/>
                        <button name="email_partner" type="object" string="Create Email" class="oe_highlight"/>
                    </group>
                </form>                            
            </field>
        </record>


        <!--<record id="odepo_offer_email_header_form" model="ir.ui.view">
            <field name="name">odepo.offer.header</field>
            <field name="model">odepo.offer</field>
            <field name="inherit_id" ref="view_odepo_offer_tree"/>
            <field name="arch" type="xml">
                <xpath expr="//form/sheet" position="before">
                    <button name="email_partner" type="object" string="Create Email" class="oe_highlight"/>
                </xpath>
            </field>
        </record>-->


        <act_window id="action_view_wizard" name="Mr" res_model="odepo.offer" view_mode="tree,form"/>
            <menuitem name="Gestion Des Offres" id="gestion_offer_id" parent="odepo_contact.odepo_config_id" sequence="10" />
                <menuitem name="Noms Des Offres" id="sub_gestion_offer_id" parent="gestion_offer_id" sequence="11" action="action_view_wizard"/>
    </data>
</openerp>

For now, i'm able to create an offer and name it and fill the odepoContactId with my contacts, and odepoProductId with my products.

Then, i click on the button send email, i have a wizard that pop up with the mail compose. The problem, is that i'm unable to add the contacts in my odepoContactId field to the recipients in the mail compose that pop up in the wizard. I Tried to pass an email adress throw the context but with no luck

Upvotes: 0

Views: 950

Answers (1)

Keval Mehta
Keval Mehta

Reputation: 664

You need to inherit "mail.compose.message" model because in that model all partners are there. In that model "partner_ids" are there which is may2many relation with "res.partner"

You can take reference from Sales

Upvotes: 0

Related Questions