| 1 |
|
| 2 |
|
| 3 |
from odoo import api, fields, models |
| 4 |
|
| 5 |
from odoo.addons.sale.models.sale_order import SALE_ORDER_STATE |
| 6 |
|
| 7 |
|
| 8 |
class SaleReport(models.Model): |
| 9 |
_name = 'sale.report' |
| 10 |
_description = "Sales Analysis Report" |
| 11 |
_auto = False |
| 12 |
_rec_name = 'date' |
| 13 |
_order = 'date desc' |
| 14 |
|
| 15 |
@api.model |
| 16 |
def _get_done_states(self): |
| 17 |
return ['sale'] |
| 18 |
|
| 19 |
|
| 20 |
name = fields.Char(string="Order Reference", readonly=True) |
| 21 |
date = fields.Datetime(string="Order Date", readonly=True) |
| 22 |
partner_id = fields.Many2one(comodel_name='res.partner', string="Customer", readonly=True) |
| 23 |
company_id = fields.Many2one(comodel_name='res.company', readonly=True) |
| 24 |
pricelist_id = fields.Many2one(comodel_name='product.pricelist', readonly=True) |
| 25 |
team_id = fields.Many2one(comodel_name='crm.team', string="Sales Team", readonly=True) |
| 26 |
user_id = fields.Many2one(comodel_name='res.users', string="Salesperson", readonly=True) |
| 27 |
state = fields.Selection(selection=SALE_ORDER_STATE, string="Status", readonly=True) |
| 28 |
invoice_status = fields.Selection( |
| 29 |
selection=[ |
| 30 |
('upselling', "Upselling Opportunity"), |
| 31 |
('invoiced', "Fully Invoiced"), |
| 32 |
('to invoice', "To Invoice"), |
| 33 |
('no', "Nothing to Invoice"), |
| 34 |
], string="Order Invoice Status", readonly=True) |
| 35 |
|
| 36 |
campaign_id = fields.Many2one(comodel_name='utm.campaign', string="Campaign", readonly=True) |
| 37 |
medium_id = fields.Many2one(comodel_name='utm.medium', string="Medium", readonly=True) |
| 38 |
source_id = fields.Many2one(comodel_name='utm.source', string="Source", readonly=True) |
| 39 |
|
| 40 |
|
| 41 |
commercial_partner_id = fields.Many2one( |
| 42 |
comodel_name='res.partner', string="Customer Entity", readonly=True) |
| 43 |
country_id = fields.Many2one( |
| 44 |
comodel_name='res.country', string="Customer Country", readonly=True) |
| 45 |
industry_id = fields.Many2one( |
| 46 |
comodel_name='res.partner.industry', string="Customer Industry", readonly=True) |
| 47 |
partner_zip = fields.Char(string="Customer ZIP", readonly=True) |
| 48 |
state_id = fields.Many2one(comodel_name='res.country.state', string="Customer State", readonly=True) |
| 49 |
|
| 50 |
|
| 51 |
order_reference = fields.Reference( |
| 52 |
string='Order', |
| 53 |
selection=[('sale.order', 'Sales Order')], |
| 54 |
aggregator="count_distinct", |
| 55 |
) |
| 56 |
|
| 57 |
categ_id = fields.Many2one( |
| 58 |
comodel_name='product.category', string="Product Category", readonly=True) |
| 59 |
product_id = fields.Many2one( |
| 60 |
comodel_name='product.product', string="Product Variant", readonly=True) |
| 61 |
product_tmpl_id = fields.Many2one( |
| 62 |
comodel_name='product.template', string="Product", readonly=True) |
| 63 |
product_uom_id = fields.Many2one(comodel_name='uom.uom', string="Unit", readonly=True) |
| 64 |
product_uom_qty = fields.Float(string="Qty Ordered", readonly=True) |
| 65 |
qty_to_deliver = fields.Float(string="Qty To Deliver", readonly=True) |
| 66 |
qty_delivered = fields.Float(string="Qty Delivered", readonly=True) |
| 67 |
qty_to_invoice = fields.Float(string="Qty To Invoice", readonly=True) |
| 68 |
qty_invoiced = fields.Float(string="Qty Invoiced", readonly=True) |
| 69 |
price_subtotal = fields.Monetary(string="Untaxed Total", readonly=True) |
| 70 |
price_total = fields.Monetary(string="Total", readonly=True) |
| 71 |
untaxed_amount_to_invoice = fields.Monetary(string="Untaxed Amount To Invoice", readonly=True) |
| 72 |
untaxed_amount_invoiced = fields.Monetary(string="Untaxed Amount Invoiced", readonly=True) |
| 73 |
line_invoice_status = fields.Selection( |
| 74 |
selection=[ |
| 75 |
('upselling', "Upselling Opportunity"), |
| 76 |
('invoiced', "Fully Invoiced"), |
| 77 |
('to invoice', "To Invoice"), |
| 78 |
('no', "Nothing to Invoice"), |
| 79 |
], string="Invoice Status", readonly=True) |
| 80 |
|
| 81 |
weight = fields.Float(string="Gross Weight", readonly=True) |
| 82 |
volume = fields.Float(string="Volume", readonly=True) |
| 83 |
price_unit = fields.Float(string="Unit Price", aggregator='avg', readonly=True) |
| 84 |
discount = fields.Float(string="Discount %", readonly=True, aggregator='avg') |
| 85 |
discount_amount = fields.Monetary(string="Discount Amount", readonly=True) |
| 86 |
|
| 87 |
|
| 88 |
nbr = fields.Integer(string="# of Lines", readonly=True) |
| 89 |
currency_id = fields.Many2one(comodel_name='res.currency', readonly=True) |
| 90 |
|
| 91 |
def _with_sale(self): |
| 92 |
return "" |
| 93 |
|
| 94 |
def _select_sale(self): |
| 95 |
select_ = f""" |
| 96 |
MIN(l.id) AS id, |
| 97 |
l.product_id AS product_id, |
| 98 |
l.invoice_status AS line_invoice_status, |
| 99 |
t.uom_id AS product_uom_id, |
| 100 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(l.product_uom_qty * u.factor / u2.factor) ELSE 0 END AS product_uom_qty, |
| 101 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(l.qty_delivered * u.factor / u2.factor) ELSE 0 END AS qty_delivered, |
| 102 |
CASE WHEN l.product_id IS NOT NULL THEN SUM((l.product_uom_qty - l.qty_delivered) * u.factor / u2.factor) ELSE 0 END AS qty_to_deliver, |
| 103 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(l.qty_invoiced * u.factor / u2.factor) ELSE 0 END AS qty_invoiced, |
| 104 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(l.qty_to_invoice * u.factor / u2.factor) ELSE 0 END AS qty_to_invoice, |
| 105 |
CASE WHEN l.product_id IS NOT NULL THEN AVG(l.price_unit |
| 106 |
/ {self._case_value_or_one('s.currency_rate')} |
| 107 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 108 |
) ELSE 0 |
| 109 |
END AS price_unit, |
| 110 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(l.price_total |
| 111 |
/ {self._case_value_or_one('s.currency_rate')} |
| 112 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 113 |
) ELSE 0 |
| 114 |
END AS price_total, |
| 115 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(l.price_subtotal |
| 116 |
/ {self._case_value_or_one('s.currency_rate')} |
| 117 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 118 |
) ELSE 0 |
| 119 |
END AS price_subtotal, |
| 120 |
CASE WHEN l.product_id IS NOT NULL OR l.is_downpayment THEN SUM(l.untaxed_amount_to_invoice |
| 121 |
/ {self._case_value_or_one('s.currency_rate')} |
| 122 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 123 |
) ELSE 0 |
| 124 |
END AS untaxed_amount_to_invoice, |
| 125 |
CASE WHEN l.product_id IS NOT NULL OR l.is_downpayment THEN SUM(l.untaxed_amount_invoiced |
| 126 |
/ {self._case_value_or_one('s.currency_rate')} |
| 127 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 128 |
) ELSE 0 |
| 129 |
END AS untaxed_amount_invoiced, |
| 130 |
COUNT(*) AS nbr, |
| 131 |
s.name AS name, |
| 132 |
s.date_order AS date, |
| 133 |
s.state AS state, |
| 134 |
s.invoice_status as invoice_status, |
| 135 |
s.partner_id AS partner_id, |
| 136 |
s.user_id AS user_id, |
| 137 |
s.company_id AS company_id, |
| 138 |
s.campaign_id AS campaign_id, |
| 139 |
s.medium_id AS medium_id, |
| 140 |
s.source_id AS source_id, |
| 141 |
t.categ_id AS categ_id, |
| 142 |
s.pricelist_id AS pricelist_id, |
| 143 |
s.team_id AS team_id, |
| 144 |
p.product_tmpl_id, |
| 145 |
partner.commercial_partner_id AS commercial_partner_id, |
| 146 |
partner.country_id AS country_id, |
| 147 |
partner.industry_id AS industry_id, |
| 148 |
partner.state_id AS state_id, |
| 149 |
partner.zip AS partner_zip, |
| 150 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(p.weight * l.product_uom_qty * u.factor / u2.factor) ELSE 0 END AS weight, |
| 151 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(p.volume * l.product_uom_qty * u.factor / u2.factor) ELSE 0 END AS volume, |
| 152 |
l.discount AS discount, |
| 153 |
CASE WHEN l.product_id IS NOT NULL THEN SUM(l.price_unit * l.product_uom_qty * l.discount / 100.0 |
| 154 |
/ {self._case_value_or_one('s.currency_rate')} |
| 155 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 156 |
) ELSE 0 |
| 157 |
END AS discount_amount, |
| 158 |
{self.env.company.currency_id.id} AS currency_id, |
| 159 |
concat('sale.order', ',', s.id) AS order_reference""" |
| 160 |
|
| 161 |
additional_fields_info = self._select_additional_fields() |
| 162 |
template = """, |
| 163 |
%s AS %s""" |
| 164 |
for fname, query_info in additional_fields_info.items(): |
| 165 |
select_ += template % (query_info, fname) |
| 166 |
|
| 167 |
return select_ |
| 168 |
|
| 169 |
def _case_value_or_one(self, value): |
| 170 |
return f"""CASE COALESCE({value}, 0) WHEN 0 THEN 1.0 ELSE {value} END""" |
| 171 |
|
| 172 |
def _select_additional_fields(self): |
| 173 |
"""Hook to return additional fields SQL specification for select part of the table query. |
| 174 |
|
| 175 |
:returns: mapping field -> SQL computation of field, will be converted to '_ AS _field' in the final table definition |
| 176 |
:rtype: dict |
| 177 |
""" |
| 178 |
return {} |
| 179 |
|
| 180 |
def _from_sale(self): |
| 181 |
currency_table = self.env['res.currency']._get_simple_currency_table(self.env.companies) |
| 182 |
currency_table = self.env.cr.mogrify(currency_table).decode(self.env.cr.connection.encoding) |
| 183 |
return f""" |
| 184 |
sale_order_line l |
| 185 |
LEFT JOIN sale_order s ON s.id=l.order_id |
| 186 |
JOIN res_partner partner ON s.partner_id = partner.id |
| 187 |
LEFT JOIN product_product p ON l.product_id=p.id |
| 188 |
LEFT JOIN product_template t ON p.product_tmpl_id=t.id |
| 189 |
LEFT JOIN uom_uom u ON u.id=l.product_uom_id |
| 190 |
LEFT JOIN uom_uom u2 ON u2.id=t.uom_id |
| 191 |
JOIN {currency_table} ON account_currency_table.company_id = s.company_id |
| 192 |
""" |
| 193 |
|
| 194 |
def _where_sale(self): |
| 195 |
return """ |
| 196 |
l.display_type IS NULL""" |
| 197 |
|
| 198 |
def _group_by_sale(self): |
| 199 |
return """ |
| 200 |
l.product_id, |
| 201 |
l.order_id, |
| 202 |
l.price_unit, |
| 203 |
l.invoice_status, |
| 204 |
t.uom_id, |
| 205 |
t.categ_id, |
| 206 |
s.name, |
| 207 |
s.date_order, |
| 208 |
s.partner_id, |
| 209 |
s.user_id, |
| 210 |
s.state, |
| 211 |
s.invoice_status, |
| 212 |
s.company_id, |
| 213 |
s.campaign_id, |
| 214 |
s.medium_id, |
| 215 |
s.source_id, |
| 216 |
s.pricelist_id, |
| 217 |
s.team_id, |
| 218 |
p.product_tmpl_id, |
| 219 |
partner.commercial_partner_id, |
| 220 |
partner.country_id, |
| 221 |
partner.industry_id, |
| 222 |
partner.state_id, |
| 223 |
partner.zip, |
| 224 |
l.is_downpayment, |
| 225 |
l.discount, |
| 226 |
s.id, |
| 227 |
account_currency_table.rate""" |
| 228 |
|
| 229 |
def _query(self): |
| 230 |
with_ = self._with_sale() |
| 231 |
return f""" |
| 232 |
{"WITH" + with_ + "(" if with_ else ""} |
| 233 |
SELECT {self._select_sale()} |
| 234 |
FROM {self._from_sale()} |
| 235 |
WHERE {self._where_sale()} |
| 236 |
GROUP BY {self._group_by_sale()} |
| 237 |
{")" if with_ else ""} |
| 238 |
""" |
| 239 |
|
| 240 |
@property |
| 241 |
def _table_query(self): |
| 242 |
return self._query() |
| 243 |
|
| 244 |
@api.readonly |
| 245 |
def action_open_order(self): |
| 246 |
self.ensure_one() |
| 247 |
return { |
| 248 |
'res_model': self.order_reference._name, |
| 249 |
'type': 'ir.actions.act_window', |
| 250 |
'views': [[False, 'form']], |
| 251 |
'res_id': self.order_reference.id, |
| 252 |
} |