"""Fix invoice 1127 + assign products to proper Nexa categories.""" # 1. Get target account IDs acct_4320 = env['account.account'].search([('code', '=', '4320')], limit=1) # Hardware Resale acct_4230 = env['account.account'].search([('code', '=', '4230')], limit=1) # Tech Support hourly print(f"4320 Hardware Resale id={acct_4320.id}; 4230 Tech Support id={acct_4230.id}") # 2. Repoint invoice 1127's lines inv = env['account.move'].browse(62485) print(f"Invoice {inv.name} (id={inv.id}) - {inv.partner_id.name}") HW_CODES = {'P620','CUSPC','RTX5090','DRW','N6100','WD8TB','NV3500','UDM-PRO','ST4000VN008','12U-600','18U-600','2UD15','UBCP004'} SVC_CODES = {'SETUP'} # Note: posted invoice lines are protected; we update the journal items directly via SQL hw_changed = sv_changed = 0 for line in inv.line_ids: if not line.product_id: continue sku = line.product_id.default_code if sku in HW_CODES and line.account_id != acct_4320: env.cr.execute("UPDATE account_move_line SET account_id = %s WHERE id = %s", (acct_4320.id, line.id)) hw_changed += 1 elif sku in SVC_CODES and line.account_id != acct_4230: env.cr.execute("UPDATE account_move_line SET account_id = %s WHERE id = %s", (acct_4230.id, line.id)) sv_changed += 1 print(f"Repointed {hw_changed} hardware lines -> 4320, {sv_changed} service lines -> 4230") # 3. Reassign product categories pc_hardware = env.ref('nexa_coa_setup.pc_resale_hardware') pc_techsupport = env.ref('nexa_coa_setup.pc_tech_support') print(f"Hardware category id={pc_hardware.id}; Tech Support category id={pc_techsupport.id}") prods_changed = 0 for prod in env['product.product'].search([('default_code', 'in', list(HW_CODES | SVC_CODES))]): sku = prod.default_code target = pc_hardware if sku in HW_CODES else pc_techsupport if prod.product_tmpl_id.categ_id != target: prod.product_tmpl_id.categ_id = target.id prods_changed += 1 print(f" {sku} {prod.name} -> category '{target.name}'") print(f"Reassigned {prods_changed} product templates") env.cr.commit() print(">>> done <<<")