| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import api, fields, models |
| 5 |
|
| 6 |
|
| 7 |
class SaleReport(models.Model): |
| 8 |
_inherit = "sale.report" |
| 9 |
|
| 10 |
@api.model |
| 11 |
def _get_done_states(self): |
| 12 |
done_states = super()._get_done_states() |
| 13 |
done_states.extend(['paid', 'invoiced', 'done']) |
| 14 |
return done_states |
| 15 |
|
| 16 |
state = fields.Selection( |
| 17 |
selection_add=[ |
| 18 |
('paid', 'Paid'), |
| 19 |
('invoiced', 'Invoiced'), |
| 20 |
('done', 'Posted') |
| 21 |
], |
| 22 |
) |
| 23 |
|
| 24 |
order_reference = fields.Reference(selection_add=[('pos.order', 'POS Order')]) |
| 25 |
|
| 26 |
def _select_pos(self): |
| 27 |
select_ = f""" |
| 28 |
-MIN(l.id) AS id, |
| 29 |
l.product_id AS product_id, |
| 30 |
NULL AS line_invoice_status, |
| 31 |
t.uom_id AS product_uom_id, |
| 32 |
SUM(l.qty) AS product_uom_qty, |
| 33 |
SUM(l.qty_delivered) AS qty_delivered, |
| 34 |
SUM(l.qty - l.qty_delivered) AS qty_to_deliver, |
| 35 |
CASE WHEN pos.account_move IS NOT NULL THEN SUM(l.qty) ELSE 0 END AS qty_invoiced, |
| 36 |
CASE WHEN pos.account_move IS NULL THEN SUM(l.qty) ELSE 0 END AS qty_to_invoice, |
| 37 |
AVG(l.price_unit) |
| 38 |
/ MIN({self._case_value_or_one('pos.currency_rate')}) |
| 39 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 40 |
AS price_unit, |
| 41 |
SUM(SIGN(l.qty) * SIGN(l.price_unit) * ABS(l.price_subtotal_incl)) |
| 42 |
/ MIN({self._case_value_or_one('pos.currency_rate')}) |
| 43 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 44 |
AS price_total, |
| 45 |
SUM(SIGN(l.qty) * SIGN(l.price_unit) * ABS(l.price_subtotal)) |
| 46 |
/ MIN({self._case_value_or_one('pos.currency_rate')}) |
| 47 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 48 |
AS price_subtotal, |
| 49 |
(CASE WHEN pos.account_move IS NULL THEN SUM(l.price_subtotal) ELSE 0 END) |
| 50 |
/ MIN({self._case_value_or_one('pos.currency_rate')}) |
| 51 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 52 |
AS amount_to_invoice, |
| 53 |
(CASE WHEN pos.account_move IS NOT NULL THEN SUM(l.price_subtotal) ELSE 0 END) |
| 54 |
/ MIN({self._case_value_or_one('pos.currency_rate')}) |
| 55 |
* {self._case_value_or_one('account_currency_table.rate')} |
| 56 |
AS amount_invoiced, |
| 57 |
count(*) AS nbr, |
| 58 |
pos.name AS name, |
| 59 |
pos.date_order AS date, |
| 60 |
(CASE WHEN pos.state = 'done' THEN 'sale' ELSE pos.state END) AS state, |
| 61 |
NULL as invoice_status, |
| 62 |
pos.partner_id AS partner_id, |
| 63 |
pos.user_id AS user_id, |
| 64 |
pos.company_id AS company_id, |
| 65 |
NULL AS campaign_id, |
| 66 |
NULL AS medium_id, |
| 67 |
NULL AS source_id, |
| 68 |
t.categ_id AS categ_id, |
| 69 |
pos.pricelist_id AS pricelist_id, |
| 70 |
pos.crm_team_id AS team_id, |
| 71 |
p.product_tmpl_id, |
| 72 |
partner.commercial_partner_id AS commercial_partner_id, |
| 73 |
partner.country_id AS country_id, |
| 74 |
partner.industry_id AS industry_id, |
| 75 |
partner.state_id AS state_id, |
| 76 |
partner.zip AS partner_zip, |
| 77 |
(SUM(p.weight) * l.qty) AS weight, |
| 78 |
(SUM(p.volume) * l.qty) AS volume, |
| 79 |
l.discount AS discount, |
| 80 |
SUM((l.price_unit * l.discount * l.qty / 100.0 |
| 81 |
/ {self._case_value_or_one('pos.currency_rate')} |
| 82 |
* {self._case_value_or_one('account_currency_table.rate')})) |
| 83 |
AS discount_amount, |
| 84 |
{self.env.company.currency_id.id} AS currency_id, |
| 85 |
concat('pos.order', ',', pos.id) AS order_reference""" |
| 86 |
|
| 87 |
additional_fields = self._select_additional_fields() |
| 88 |
additional_fields_info = self._fill_pos_fields(additional_fields) |
| 89 |
template = """, |
| 90 |
%s AS %s""" |
| 91 |
for fname, value in additional_fields_info.items(): |
| 92 |
select_ += template % (value, fname) |
| 93 |
return select_ |
| 94 |
|
| 95 |
def _available_additional_pos_fields(self): |
| 96 |
"""Hook to replace the additional fields from sale with the one from pos_sale.""" |
| 97 |
return { |
| 98 |
'warehouse_id': 'picking.warehouse_id', |
| 99 |
} |
| 100 |
|
| 101 |
def _fill_pos_fields(self, additional_fields): |
| 102 |
"""Hook to fill additional fields for the pos_sale. |
| 103 |
|
| 104 |
:param additional_fields: Dictionary mapping fields with their values |
| 105 |
:type additional_fields: dict[str, Any] |
| 106 |
""" |
| 107 |
filled_fields = {x: 'NULL' for x in additional_fields} |
| 108 |
for fname, value in self._available_additional_pos_fields().items(): |
| 109 |
if fname in additional_fields: |
| 110 |
filled_fields[fname] = value |
| 111 |
return filled_fields |
| 112 |
|
| 113 |
def _from_pos(self): |
| 114 |
currency_table = self.env['res.currency']._get_simple_currency_table(self.env.companies) |
| 115 |
return """ |
| 116 |
pos_order_line l |
| 117 |
JOIN pos_order pos ON l.order_id = pos.id |
| 118 |
LEFT JOIN res_partner partner ON (pos.partner_id=partner.id OR pos.partner_id = NULL) |
| 119 |
LEFT JOIN product_product p ON l.product_id=p.id |
| 120 |
LEFT JOIN product_template t ON p.product_tmpl_id=t.id |
| 121 |
LEFT JOIN uom_uom u ON u.id=t.uom_id |
| 122 |
LEFT JOIN pos_session session ON session.id = pos.session_id |
| 123 |
LEFT JOIN pos_config config ON config.id = session.config_id |
| 124 |
LEFT JOIN stock_picking_type picking ON picking.id = config.picking_type_id |
| 125 |
JOIN {currency_table} ON account_currency_table.company_id = pos.company_id |
| 126 |
""".format( |
| 127 |
currency_table=self.env.cr.mogrify(currency_table).decode(self.env.cr.connection.encoding), |
| 128 |
) |
| 129 |
|
| 130 |
def _where_pos(self): |
| 131 |
return """ |
| 132 |
l.sale_order_line_id IS NULL""" |
| 133 |
|
| 134 |
def _group_by_pos(self): |
| 135 |
return """ |
| 136 |
l.order_id, |
| 137 |
l.product_id, |
| 138 |
l.price_unit, |
| 139 |
l.discount, |
| 140 |
l.qty, |
| 141 |
t.uom_id, |
| 142 |
t.categ_id, |
| 143 |
pos.id, |
| 144 |
pos.name, |
| 145 |
pos.date_order, |
| 146 |
pos.partner_id, |
| 147 |
pos.user_id, |
| 148 |
pos.state, |
| 149 |
pos.company_id, |
| 150 |
pos.pricelist_id, |
| 151 |
p.product_tmpl_id, |
| 152 |
partner.commercial_partner_id, |
| 153 |
partner.country_id, |
| 154 |
partner.industry_id, |
| 155 |
partner.state_id, |
| 156 |
partner.zip, |
| 157 |
u.factor, |
| 158 |
pos.crm_team_id, |
| 159 |
account_currency_table.rate, |
| 160 |
picking.warehouse_id""" |
| 161 |
|
| 162 |
def _query(self): |
| 163 |
res = super()._query() |
| 164 |
return res + f"""UNION ALL ( |
| 165 |
SELECT {self._select_pos()} |
| 166 |
FROM {self._from_pos()} |
| 167 |
WHERE {self._where_pos()} |
| 168 |
GROUP BY {self._group_by_pos()} |
| 169 |
) |
| 170 |
""" |