| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from odoo import api, fields, models, _ |
| 3 |
from odoo.exceptions import ValidationError |
| 4 |
|
| 5 |
|
| 6 |
class ResPartner(models.Model): |
| 7 |
_name = 'res.partner' |
| 8 |
_inherit = ['res.partner', 'pos.load.mixin'] |
| 9 |
|
| 10 |
pos_order_count = fields.Integer( |
| 11 |
compute='_compute_pos_order', |
| 12 |
help="The number of point of sales orders related to this customer", |
| 13 |
groups="point_of_sale.group_pos_user", |
| 14 |
) |
| 15 |
pos_order_ids = fields.One2many('pos.order', 'partner_id', readonly=True) |
| 16 |
pos_contact_address = fields.Char('PoS Address', compute='_compute_pos_contact_address') |
| 17 |
invoice_emails = fields.Char(compute='_compute_invoice_emails', readonly=True) |
| 18 |
fiscal_position_id = fields.Many2one( |
| 19 |
'account.fiscal.position', |
| 20 |
string='Automatic Fiscal Position', |
| 21 |
compute='_compute_fiscal_position_id', |
| 22 |
help="Fiscal positions are used to adapt taxes and accounts for particular " |
| 23 |
"customers or sales orders/invoices. The default value comes from the customer.", |
| 24 |
) |
| 25 |
|
| 26 |
@api.depends(lambda self: self._display_address_depends()) |
| 27 |
def _compute_pos_contact_address(self): |
| 28 |
for partner in self: |
| 29 |
partner.pos_contact_address = partner._display_address(without_company=True) |
| 30 |
|
| 31 |
def _compute_application_statistics_hook(self): |
| 32 |
data_list = super()._compute_application_statistics_hook() |
| 33 |
if not self.env.user.has_group('point_of_sale.group_pos_user'): |
| 34 |
return data_list |
| 35 |
for partner in self.filtered('pos_order_count'): |
| 36 |
stat_info = {'iconClass': 'fa-shopping-bag', 'value': partner.pos_order_count, 'label': _('Shopping cart'), 'tagClass': 'o_tag_color_7'} |
| 37 |
data_list[partner.id].append(stat_info) |
| 38 |
return data_list |
| 39 |
|
| 40 |
@api.model |
| 41 |
def get_new_partner(self, config_id, domain, offset): |
| 42 |
config = self.env['pos.config'].browse(config_id) |
| 43 |
if len(domain) == 0: |
| 44 |
limited_partner_ids = {partner[0] for partner in config.get_limited_partners_loading(offset)} |
| 45 |
domain += [('id', 'in', list(limited_partner_ids))] |
| 46 |
new_partners = self.search(domain) |
| 47 |
else: |
| 48 |
# If search domain is not empty, we need to search inside all partners |
| 49 |
new_partners = self.search(domain, offset=offset, limit=100) |
| 50 |
fiscal_positions = new_partners.fiscal_position_id |
| 51 |
return { |
| 52 |
'res.partner': self._load_pos_data_read(new_partners, config), |
| 53 |
'account.fiscal.position': self.env['account.fiscal.position']._load_pos_data_read(fiscal_positions, config), |
| 54 |
} |
| 55 |
|
| 56 |
@api.model |
| 57 |
def _load_pos_data_domain(self, data, config): |
| 58 |
# Collect partner IDs from loaded orders |
| 59 |
loaded_order_partner_ids = {order['partner_id'] for order in data['pos.order']} |
| 60 |
|
| 61 |
# Extract partner IDs from the tuples returned by get_limited_partners_loading |
| 62 |
limited_partner_ids = {partner[0] for partner in config.get_limited_partners_loading()} |
| 63 |
|
| 64 |
limited_partner_ids.add(self.env.user.partner_id.id) # Ensure current user is included |
| 65 |
partner_ids = limited_partner_ids.union(loaded_order_partner_ids) |
| 66 |
return [('id', 'in', list(partner_ids))] |
| 67 |
|
| 68 |
def _compute_fiscal_position_id(self): |
| 69 |
for partner in self: |
| 70 |
partner.fiscal_position_id = self.env['account.fiscal.position'].with_company(self.env.company)._get_fiscal_position(partner) |
| 71 |
|
| 72 |
@api.model |
| 73 |
def _load_pos_data_fields(self, config): |
| 74 |
return [ |
| 75 |
'id', 'name', 'street', 'street2', 'city', 'state_id', 'country_id', 'vat', 'lang', 'phone', 'zip', 'email', |
| 76 |
'barcode', 'write_date', 'property_product_pricelist', 'parent_name', 'pos_contact_address', |
| 77 |
'invoice_emails', 'fiscal_position_id', 'is_company', 'property_account_receivable_id', |
| 78 |
] |
| 79 |
|
| 80 |
def _compute_pos_order(self): |
| 81 |
# retrieve all children partners and prefetch 'parent_id' on them |
| 82 |
all_partners = self.with_context(active_test=False).search_fetch( |
| 83 |
[('id', 'child_of', self.ids)], |
| 84 |
['parent_id'], |
| 85 |
) |
| 86 |
pos_order_data = self.env['pos.order']._read_group( |
| 87 |
domain=[('partner_id', 'in', all_partners.ids)], |
| 88 |
groupby=['partner_id'], aggregates=['__count'] |
| 89 |
) |
| 90 |
self_ids = set(self._ids) |
| 91 |
|
| 92 |
self.pos_order_count = 0 |
| 93 |
for partner, count in pos_order_data: |
| 94 |
while partner: |
| 95 |
if partner.id in self_ids: |
| 96 |
partner.pos_order_count += count |
| 97 |
partner = partner.parent_id |
| 98 |
|
| 99 |
@api.depends('email', 'child_ids.type', 'child_ids.email') |
| 100 |
def _compute_invoice_emails(self): |
| 101 |
for record in self: |
| 102 |
emails = [record.email] if record.email else [] |
| 103 |
emails.extend([child.email for child in record.child_ids if child.type == "invoice" and child.email]) |
| 104 |
record.invoice_emails = ', '.join(emails) if emails else '' |
| 105 |
|
| 106 |
def action_view_pos_order(self): |
| 107 |
''' |
| 108 |
This function returns an action that displays the pos orders from partner. |
| 109 |
''' |
| 110 |
action = self.env['ir.actions.act_window']._for_xml_id('point_of_sale.action_pos_pos_form') |
| 111 |
if self.is_company: |
| 112 |
action['domain'] = [('partner_id.commercial_partner_id', '=', self.id)] |
| 113 |
else: |
| 114 |
action['domain'] = [('partner_id', '=', self.id)] |
| 115 |
return action |
| 116 |
|
| 117 |
def open_commercial_entity(self): |
| 118 |
return { |
| 119 |
**super().open_commercial_entity(), |
| 120 |
**({'target': 'new'} if self.env.context.get('target') == 'new' else {}), |
| 121 |
} |
| 122 |
|
| 123 |
@api.ondelete(at_uninstall=False) |
| 124 |
def _unlink_if_pos_no_orders(self): |
| 125 |
if self.sudo().pos_order_ids: |
| 126 |
raise ValidationError(_('You cannot delete a customer that has point of sales orders. You can archive it instead.')) |