Create a Odoo email sales report, with Python.
Tags: python
Published: 2023-06-26 21:18:50

## Odoo Email Noty: Sales Reporting Tool

I created a Python script helps you conveniently pull sales data from your Odoo server and compile it into a detailed email report. 

Here is the repo. 

https://github.com/tunahorse/Odoo_email_noty

## Features

- Pull sales data from an Odoo server
- Segment sales data 
- Generate a detailed report of total orders and total amounts for both segments
- Send the generated report via email, customized with the details of each order

## Get user input for date
date_input = input("Please enter a dateThe script you've provided is a Python script that pulls sales data from an Odoo server and sends an email with the sales report. The report contains a summary of total orders and the total order amount, as well as details of each order.

Here's a detailed breakdown of what each part of the script does:

1. The script starts by importing several Python libraries which are required for its operation:

    - `xmlrpc.client`: This is used to make remote procedure call (RPC) to the Odoo server.
    - `ssl`: This is used to create a secure connection to the Odoo server.
    - `datetime`, `date`, `timedelta`: These are used to manipulate dates.
    - `email.mime.multipart` and `email.mime.text`: These are used to create and manipulate the email to be sent.
    - `smtplib`: This is used to send the email.
    - `configparser`: This is used to read the configuration file which contains the necessary details to connect to the Odoo server and the email server.
    - `pprint`: This is used to pretty-print data for debugging purposes.

2. The script reads the configuration file named 'config.ini' using `configparser`. This file contains necessary information such as the Odoo server details (URL, database name, username, and password) and the email server details (SMTP server, port, from address, to address, and password).

3. It creates an SSL context to allow connection to a self-signed Odoo server.

4. It connects to the Odoo server using `xmlrpc.client.ServerProxy` and authenticates with the server using the username and password from the config file.

5. It takes user input for a date. If no date is entered or if the entered date is not valid, it uses today's date.

6. It fetches sales data from the Odoo server using the `models.execute_kw` function. It uses the 'sale.order' model and the 'search_read' method to fetch sales orders for the specified date.

7. It calculates the total number of orders and the total order amount by iterating over the fetched sales data.

8. It creates an email using `MIMEMultipart` and sets the 'From', 'To', and 'Subject' fields.

9. It creates the body of the email by adding the total number of orders and the total order amount. It then iterates over the sales data again to add the details of each order to the email body.

10. It attaches the body to the email using `msg.attach`.

11. It connects to the SMTP server using `smtplib.SMTP`, logs in with the 'From' email address and password, and sends the email using `server.sendmail`.

12. Finally, it disconnects from the SMTP server using `server.quit`.

Remember to replace all the placeholder values in the 'config.ini' file with your actual details, and also make sure that the Odoo server is properly set up and contains the 'sale.order' model with the required fields.