71 lines
2.8 KiB
Python
71 lines
2.8 KiB
Python
import re
|
|
from odoo import api, SUPERUSER_ID
|
|
|
|
env = api.Environment(cr, SUPERUSER_ID, {})
|
|
|
|
# The pattern Odoo core uses: re.search('([0-9-]){14,}', order.pos_reference).group(0)
|
|
# It needs at least 14 consecutive digits or hyphens, e.g. "00001-001-0001"
|
|
PATTERN = re.compile(r'([0-9-]){14,}')
|
|
|
|
print("=" * 60)
|
|
print("Scanning ALL pos.orders for bad pos_reference values...")
|
|
print("=" * 60)
|
|
|
|
# Find all orders that are in an open session (these are the ones loaded on POS open)
|
|
open_sessions = env['pos.session'].search([('state', '=', 'opened')])
|
|
print(f"Open sessions found: {open_sessions.mapped('name')}")
|
|
|
|
bad_orders = []
|
|
all_orders = env['pos.order'].search([('session_id', 'in', open_sessions.ids)])
|
|
print(f"Total orders in open sessions: {len(all_orders)}")
|
|
|
|
for order in all_orders:
|
|
ref = order.pos_reference or ''
|
|
if not PATTERN.search(ref):
|
|
bad_orders.append(order)
|
|
print(f" BAD ORDER id={order.id}, pos_reference='{ref}', name='{order.name}'")
|
|
|
|
print(f"\nTotal bad orders found: {len(bad_orders)}")
|
|
|
|
if bad_orders:
|
|
print("\nOptions:")
|
|
print(" 1. DELETE bad orders")
|
|
print(" 2. FIX pos_reference to a valid format")
|
|
print("\nApplying FIX: setting pos_reference to valid format...")
|
|
|
|
for order in bad_orders:
|
|
old_ref = order.pos_reference
|
|
# Generate a valid reference using the order ID padded to match the format
|
|
new_ref = f"Order {order.id:05d}-001-0001"
|
|
order.write({'pos_reference': new_ref})
|
|
print(f" Fixed order id={order.id}: '{old_ref}' -> '{new_ref}'")
|
|
|
|
print(f"\nFixed {len(bad_orders)} orders.")
|
|
print("Please restart the POS session and try again.")
|
|
else:
|
|
print("\nNo bad orders in open sessions. Checking ALL orders...")
|
|
|
|
# Also check orders not in any session (orphan orders)
|
|
all_pos_orders = env['pos.order'].search([])
|
|
print(f"Total pos.orders in database: {len(all_pos_orders)}")
|
|
|
|
really_bad = []
|
|
for order in all_pos_orders:
|
|
ref = order.pos_reference or ''
|
|
if not PATTERN.search(ref):
|
|
really_bad.append(order)
|
|
print(f" BAD ORDER id={order.id}, pos_reference='{ref}', session={order.session_id.name}, state={order.state}")
|
|
|
|
print(f"\nTotal bad orders in entire DB: {len(really_bad)}")
|
|
if really_bad:
|
|
for order in really_bad:
|
|
old_ref = order.pos_reference
|
|
new_ref = f"Order {order.id:05d}-001-0001"
|
|
order.write({'pos_reference': new_ref})
|
|
print(f" Fixed id={order.id}: '{old_ref}' -> '{new_ref}'")
|
|
print(f"\nFixed {len(really_bad)} orders. Try opening POS again.")
|
|
else:
|
|
print("\nAll pos_references look valid!")
|
|
print("The error might be from a DIFFERENT cause.")
|
|
print("Check: is the pricelist_id or sequence_id returning None?")
|