diff --git a/force_update_bills_to_paid.py b/force_update_bills_to_paid.py new file mode 100644 index 0000000..aeea6e5 --- /dev/null +++ b/force_update_bills_to_paid.py @@ -0,0 +1,74 @@ +# Run this script using the Odoo shell: +# ./.venv/bin/python ./odoo/odoo-bin shell -d kipasdbclone7 -c odoo.conf < force_update_bills_to_paid.py + +import sys + +# List of vendor bills to force update to Paid. +# You can add or remove bill names as needed before running. +BILL_NAMES = [ + 'BILL/2025/10/0138', + 'BILL/2025/10/0137', + 'BILL/2025/10/0129', + 'BILL/2025/10/0111', + 'BILL/2025/10/0105', + 'BILL/2025/10/0094', + 'BILL/2025/10/0093', + 'BILL/2025/10/0088', + 'BILL/2025/10/0174', + 'BILL/2025/10/0076', + 'BILL/2025/10/0065', +] + +def force_paid_bills(env): + bills = env['account.move'].search([ + ('name', 'in', BILL_NAMES), + ('move_type', '=', 'in_invoice') + ]) + + if not bills: + print("No matching vendor bills found.") + return + + print(f"Found {len(bills)} vendor bills to update.") + + for bill in bills: + if bill.payment_state == 'paid': + print(f"[{bill.name}] is already Paid. Skipping.") + continue + + print(f"[{bill.name}] Updating state to 'paid'...") + + # Use raw SQL to prevent Odoo ORM from overriding or triggering validations + # Updating the payment state and amount residual on the move + env.cr.execute(""" + UPDATE account_move + SET payment_state = 'paid', + amount_residual = 0, + amount_residual_signed = 0 + WHERE id = %s + """, (bill.id,)) + + # Updating the residual on the payable lines so it doesn't show "Amount Due" inside the form + payable_lines = bill.line_ids.filtered(lambda l: l.account_id.account_type in ('liability_payable', 'asset_receivable')) + if payable_lines: + env.cr.execute(""" + UPDATE account_move_line + SET amount_residual = 0, + amount_residual_currency = 0 + WHERE id IN %s + """, (tuple(payable_lines.ids),)) + + print("Committing changes to database...") + env.cr.commit() + + # Invalidate cache so subsequent ORM calls in this session see the changes + env.invalidate_all() + print("Update complete!") + +if __name__ == '__main__': + # When running via `odoo-bin shell`, the `env` variable is automatically available. + try: + force_paid_bills(env) + except NameError: + print("Please run this script using Odoo shell:") + print("Example: ./.venv/bin/python ./odoo/odoo-bin shell -d -c odoo.conf < force_update_bills_to_paid.py")