Messaging System of Openerp v7 - odoo

Can anyone share how I can change the "from" field value when sending a message because it always has the same email address?
--the address of the outgoing mail server I configure.

You can even send mail without making use of email template.
You can use mail.message & mail.mail objects.
def send_mail(cr, uid, ids, context=context):
mail_server_obj = self.pool.get('ir.mail_server')
mail_message_obj = self.pool.get('mail.message')
mail_mail_obj = self.pool.get('mail.mail')
for id in ids:
mail_message_id = mail_message_obj.create(cr, uid, {'email_from': 'from_add', 'model': 'model_name', 'res_id': id, 'subject': 'subject_name', 'body': 'your_html_body'}, context=context)
mail_server_ids = mail_server_obj.search(cr, uid, [], context=context)
mail_mail_id = mail_mail_obj.create(cr, uid, {'mail_message_id': mail_message_id, 'mail_server_id': mail_server_ids and mail_server_ids[0], 'state': 'outgoing', 'email_from': 'from_add', 'email_to': 'to_add', 'body_html': 'your_html_body'}, context=context)
if mail_mail_id:
mail_mail_obj.send(cr, uid, [mail_mail_id], context=context)
return True

There are many ways of sending mails. The good way is by creating a email template.
First create one email template.
def send_email(self, cr, uid, ids, context=None):
email_template_obj = self.pool.get('email.template')
template_ids = email_template_obj.search(cr, uid, [('model_id.model', '=', 'sale.order')])
if template_ids:
for id in ids:
values = email_template_obj.generate_email(cr, uid, template_ids[0], id, context=context)
print "values:: ", values
values['subject'] = your_subject
values['email_to'] = your_mail_to_address
values['email_cc'] = your_cc_address
values['body_html'] = your_body_html_part
values['body'] = your_body_html_part
mail_mail_obj = self.pool.get('mail.mail')
msg_id = mail_mail_obj.create(cr, uid, values, context=context)
if msg_id:
mail_mail_obj.send(cr, uid, [msg_id], context=context)
return True
Hope this will solve your problem.
Thank you.

Change your email preference from the top right menu showing your login.

Related

Odoo validate invoice from code

I creating an invoice from another model, and want to get validated not draft
But internal number and total are not generated with my code.
invoice_id = self.pool.get('account.invoice').create(cr, uid,{
'partner_id':self.supplier.id,
'name' : 'Faltante mercaderia',
'journal_id': journal_id,
'account_id':account_id,
'type': 'in_refund',
})
self.pool.get('account.invoice.line').create(cr, uid,{
'invoice_id' : invoice_id,
'name' : 'Faltante mercaderia %s: %s' %(self.type,self.number),
'quantity' : self.dif_final,
'price_unit':self.tarifa_dif / 1000,
})
a = self.env['account.invoice'].browse([invoice_id])
a.invoice_validate()
I also try adding a.action_number()
This code should work for you:
inv_obj = self.pool.get('account.invoice')
inv_obj.button_compute(cr, uid, [invoice_id], context=context, set_total=True)
inv_obj.action_date_assign(cr, uid, invoice_id, context=context)
inv_obj.action_move_create(cr, uid, invoice_id, context=context)
inv_obj.action_number(cr, uid, invoice_id, context=context)
inv_obj.invoice_validate(cr, uid, invoice_id, context=context)
You can check by calling all above methods and let me know.
validate button sends a signal to a workflow, you just need to send the same signal:
a.signal_workflow('invoice_open')

Send reports automatically via email openerp

Odoo has the Reporting part and I was wondering how (if its even possible) to send reports via mail (and create an automated action for it)?
Let's say we have to crm.phonecall.report model, how to take it's information and use it in an email template? I tried making an email template with that model and then add the same xml text as it is in the Phonecall Analysis, but that didn't work. So all the help is really appreciated.
Following code will be enough to fulfill your mentioned requirements. It calls the email template and then attached a report attachment and finally send an email.
email = email_obj.browse(cr, uid, template_id)
attachment_obj = self.pool.get('ir.attachment')
ir_actions_report = self.pool.get('ir.actions.report.xml')
matching_reports = ir_actions_report.search(
cr, uid, [('name', '=', 'Report_Name_here')])
if matching_reports:
report = ir_actions_report.browse(cr, uid, matching_reports[0])
report_service = 'report.' + report.report_name
service = netsvc.LocalService(report_service)
(result, format) = service.create(
cr, uid, mrp_ids, {'model': self._name, 'start_date': start, 'end_date': end}, context=context)
if not report.attachment:
file_name = "Production Sale Report " + datetime.strftime(datetime.now().date(), "%Y-%m-%d") + ".pdf"
attachment_id = attachment_obj.create(cr, uid,
{
'name': file_name,
'datas': result,
'datas_fname': file_name,
'type': 'binary'
}, context=context)
email_obj.write(cr, uid, template_id, {'email_from': email.email_from,
'email_to': email.email_to,
'subject': email.subject,
'body_html': email.body_html,
'email_recipients': email.email_recipients,
'attachment_ids': [(6, 0, [attachment_id])],
})
email_obj.send_mail(cr, uid, template_id, False, True, context=context)
Hope this will solve your problem. Cheers

Get the value of one selection field

How I affect the value of a field section to a field char.
return { value{'field_char' : field_selection.value !!!}}
If you need to affect it as in onchange event, you just set a parameter to your function with the value you whant of the selection:
<field name="field_selection" onchange="do_change(field_selection)" />
and then, in your function get the parameter, and affect it to your field_char:
def do_change(self, cr, uid, ids, selection_val, context=None):
return {
value{
'field_char' : selection_val
}
}
related to
field1: fields.selection([('a','A')], 'Field1'),
field2: fields.char('Field2'),
just try to
fields2 = fields['a']
only pass key it's return value of key
Override create and write method for that,
def create(self, cr, uid, vals, context={}):
if not vals.get('field_selection',False):
vals.update('field_char') = vals.get('field_selection','')
return super(class_name,self).create(cr, uid, vals, context=context)
def write(self, cr, uid, ids, vals, context={}):
if not vals.get('field_selection',False):
vals.update('field_char') = vals.get('field_selection','')
return super(class_name,self).write(cr, uid, ids, vals, context=context)

Run a report from a wizard

I have a wizard with a button. On button action I want to run a report and leave the PDF on the server. I have the above code fragment that creates a report with web service. But in a wizard context I have normally only the uid (I think).
What will be the equivalent way to get the report to disk in a wizard ?
def reportToDisk(self, cr, uid, ids, context=None):
dbname = 'db'
username = 'user'
pwd = 'pass'
model = 'sale.order'
report_name = 'doc.sale'
sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
uid = sock_common.login(dbname, username, pwd)
sock = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/object')
ids = sock.execute(dbname, uid, pwd, model, 'search',[])[0:1]
sock_report = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/report')
id_report = sock_report.report(
dbname, uid, pwd, report_name, ids,{'model': model, 'id': ids[0], 'report_type':'pdf'}
)
cont = True
while cont:
report = sock_report.report_get(dbname, uid, pwd, id_report)
cont = not report['state']
string_pdf = base64.decodestring(report['result'])
file_pdf = open('/home/arch-in/file.pdf','w')
file_pdf.write(string_pdf)
file_pdf.close()
Return the report action on your button click(It can be wizard button or view button, it just works with button click return) like following:
def btn_clik_action(self, cr, uid, ids, context=None):
if context == None:
context = {}
value = {
'type': 'ir.actions.report.xml',
'report_name':'report.name.(servicename)',
'datas': {
'model':'model.name',
'id': ids and ids[0] or False,
'ids': ids and ids or [],
'report_type': 'pdf'
},
'nodestroy': True
}
Just returning the report action will give you file of the report which basically you don't need to write or anything.
Thank YOu

How can i use onchange function to change price of account invoice line price_unit

I would like to use onchange to set account invoice price to a default price set in res.partner. Could anyone give me an example of this ?
Thanks,
Joe
There are lots of examples of onchange in addons.
For example, if you want to change the value of 'name' field and want to set product's name based on selected product:
def onchange_product_id(self, cr, uid, ids, product_id, context=None):
if product_id:
prod = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
return {'value': {'name': prod.name}}
return {}
Write this in your_invoice_view.xml:
<field name="product_id" on_change="product_id_change(product_id, parent.partner_id, context)"/>
Write onchange in your_invoice.py under "invoice_line" model(class):
def onchange_product_id_change(self, cr, uid, ids, product_id, partner_id context=None):
if product_id and partner_id:
product_brw = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
partner_brw = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
/*write your logic */
return {'value': {'price_unit': your calculated value}}
return {}