78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
import json
|
|
import logging
|
|
from odoo import api, fields, models
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
class PosOrder(models.Model):
|
|
_inherit = 'pos.order'
|
|
|
|
x_logged_cancellations = fields.Text(string='Logged Cancellations', default='[]')
|
|
|
|
@api.model
|
|
def _process_order(self, order, existing_order):
|
|
# Extract frontend cancelled lines log
|
|
cancelled_lines = order.get('x_cancelled_lines', [])
|
|
|
|
res = super()._process_order(order, existing_order)
|
|
|
|
pos_order = self.browse(res) if isinstance(res, int) else res
|
|
|
|
if pos_order and cancelled_lines:
|
|
pos_order._log_cancelled_lines_to_chatter(cancelled_lines)
|
|
|
|
return res
|
|
|
|
def _log_cancelled_lines_to_chatter(self, cancelled_lines):
|
|
self.ensure_one()
|
|
try:
|
|
logged_ids = json.loads(self.x_logged_cancellations or '[]')
|
|
except Exception:
|
|
logged_ids = []
|
|
|
|
new_cancellations = []
|
|
for cancel in cancelled_lines:
|
|
cancel_id = cancel.get('id')
|
|
if cancel_id and cancel_id not in logged_ids:
|
|
new_cancellations.append(cancel)
|
|
logged_ids.append(cancel_id)
|
|
|
|
if new_cancellations:
|
|
body = "<strong>Product Cancellation/Reduction Log:</strong><ul>"
|
|
for cancel in new_cancellations:
|
|
if cancel.get('action') == 'delete':
|
|
action_str = f"Deleted completely (quantity was {cancel.get('qty', 0)})"
|
|
else:
|
|
action_str = f"Reduced quantity by {cancel.get('cancelled_qty', 0)} (from {cancel.get('qty', 0)} to {float(cancel.get('qty', 0)) - float(cancel.get('cancelled_qty', 0))})"
|
|
body += f"<li><strong>{cancel.get('product_name')}</strong>: {action_str} by <strong>{cancel.get('employee_name', 'Unknown')}</strong></li>"
|
|
body += "</ul>"
|
|
self.message_post(body=body)
|
|
self.write({'x_logged_cancellations': json.dumps(logged_ids)})
|
|
|
|
def action_pos_order_cancel(self):
|
|
# Capture the cashier / employee who cancelled the order
|
|
employee_id = self.env.context.get('cancelled_by_employee_id')
|
|
employee_name = "Unknown Employee"
|
|
if employee_id:
|
|
employee = self.env['hr.employee'].browse(employee_id)
|
|
if employee.exists():
|
|
employee_name = employee.name
|
|
else:
|
|
employee_name = self.env.user.name
|
|
|
|
res = super().action_pos_order_cancel()
|
|
|
|
for order in self:
|
|
order.message_post(body=f"<strong>Order Cancelled</strong> by <strong>{employee_name}</strong>")
|
|
|
|
return res
|
|
|
|
def _prepare_pos_log(self, body):
|
|
if self.employee_id and hasattr(self.employee_id, 'pos_role') and self.employee_id.pos_role:
|
|
role_selection = dict(self.env['hr.employee']._fields['pos_role'].selection)
|
|
role_name = role_selection.get(self.employee_id.pos_role, "Cashier")
|
|
from markupsafe import Markup
|
|
return body + Markup("<br/>") + f"{role_name} {self.employee_id.name}"
|
|
return super()._prepare_pos_log(body)
|
|
|