Last active
November 6, 2020 11:45
-
-
Save jev-odoo/324a9603676a7e9ea490a149327cdbdf to your computer and use it in GitHub Desktop.
POS - Check Payments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # FOR VERSION 10,11,12,13,... | |
| # Set sessions ids between the brackets, separated by a coma | |
| # I.E. SESSION_ID = [12, 18, 132] or SESSION_ID = [4] | |
| SESSION_ID = [] | |
| MODE = 'check' | |
| # DO NOT MODIFY | |
| VALID_VERSIONS = (10, 11, 12, 13,) | |
| VALID_SESSION_STATES = ('opened', 'closing_control') | |
| VALID_MODES = ('check', 'repair') | |
| MESSAGES = [] | |
| def iter_to_string(iterable): | |
| return ', '.join(str(x) for x in iterable) | |
| def check_mode(): | |
| if MODE not in VALID_MODES: | |
| raise Warning('Invalid mode. (Actual mode: {}, Valid modes: {})'.format(MODE, iter_to_string(VALID_MODES))) | |
| commit_message = 'rollbacking' | |
| if MODE == 'repair': | |
| commit_message = 'commiting' | |
| return commit_message | |
| def get_version(env): | |
| latest_version = env['ir.module.module'].search([('name', '=', 'base')]).latest_version.replace('~', '-') | |
| actual_version = 0 | |
| for version in VALID_VERSIONS: | |
| if latest_version.startswith(str(version)) or latest_version.startswith('saas-%s' % str(version)): | |
| actual_version = version | |
| if not actual_version: | |
| raise Warning('Invalid version. (Actual version: {} - Valid versions: {})'.format(actual_version, iter_to_string(VALID_VERSIONS))) | |
| return actual_version | |
| def get_session(env): | |
| sessions = env['pos.session'] | |
| if not SESSION_ID: | |
| raise Warning('Please define the session id.') | |
| sessions = sessions.search([('id', 'in', SESSION_ID)]) | |
| if not sessions: | |
| raise Warning('No session found. ids: {}'.format(iter_to_string(SESSION_ID))) | |
| return sessions | |
| def check_order_payments(session, order, version, double_check=False): | |
| order_amount = order.amount_total | |
| if version < 13: | |
| currency_id = order.config_id.currency_id | |
| payment_amount = sum(x.amount for x in order.statement_ids) | |
| else: | |
| currency_id = order.currency_id | |
| payment_amount = sum(x.amount for x in order.payment_ids) | |
| if not currency_id.is_zero(order_amount - payment_amount): | |
| amount_diff = order_amount - payment_amount | |
| found_or_persists = 'found' if not double_check else 'persists' | |
| MESSAGES.append('Difference {} in payment. (session_id: {} - order_id: {} - order_amount: {} - payment_amount: {} - diff: {})'.format(found_or_persists, session.id, order.id, order_amount, payment_amount, amount_diff)) | |
| return amount_diff | |
| return False | |
| def repair_payment(version, session, order, payment_method, amount_diff): | |
| if version < 13: | |
| order.write({'statement_ids': [(0, 0, {'name': order.name, 'statement_id': payment_method.id, 'amount': amount_diff})]}) | |
| else: | |
| order.write({'payment_ids': [(0, 0, {'payment_method_id': payment_method.id, 'amount': amount_diff})]}) | |
| check_order_payments(session, order, version, double_check=True) | |
| def check_payment(version, session): | |
| error = False | |
| if version < 13: | |
| payment_method = session.statement_ids.filtered(lambda x: x.journal_type == 'cash')[:1] | |
| else: | |
| payment_method = session.payment_method_ids.filtered('is_cash_count')[:1] | |
| for order in session.order_ids: | |
| amount_diff = check_order_payments(session, order, version) | |
| error = error or bool(amount_diff) | |
| if amount_diff: | |
| repair_payment(version, session, order, payment_method, amount_diff) | |
| if error: | |
| session.action_pos_session_closing_control() | |
| MESSAGES.append('Session successfully closed. (id: {})'.format(session.id)) | |
| else: | |
| MESSAGES.append('No difference found in payment. All Seems OK (session_id: {})'.format(session.id)) | |
| def process(env): | |
| version = get_version(env) | |
| commit_message = check_mode() | |
| sessions = get_session(env) | |
| for session in sessions: | |
| if session.state not in VALID_SESSION_STATES: | |
| MESSAGES.append('Invalid session state, skipping... (id: {} - Actual state: {} - Valid states: {})'.format(session.id, session.state, iter_to_string(VALID_SESSION_STATES))) | |
| continue | |
| try: | |
| check_payment(version, session) | |
| MESSAGES.append('{} mode detected, {} changes. (session_id: {})'.format(MODE, commit_message, session.id)) | |
| if MODE == 'repair': | |
| env.cr.commit() | |
| else: | |
| env.cr.rollback() | |
| except Exception as e: | |
| MESSAGES.append('Error closing session. (id: {} - error: {}'.format(session.id, str(e))) | |
| env.cr.rollback() | |
| if MESSAGES: | |
| raise Warning('\n'.join(MESSAGES)) | |
| else: | |
| raise Warning('Payment seems OK.') | |
| process(env) |
Author
Indeed.
I forgot this line during the conversion to Server Action ;)
Thank you ;)
I propose to add this script to : https://github.com/odoo/support-tools/tree/master/scripts/pos
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would adapt lines 74-78 this way to make it work in <=V12
if version < 13:payment_amount = sum(x.amount for x in order.statement_ids)currency = order.config_id.currency_idelse:payment_amount = sum(x.amount for x in order.payment_ids)currency = order.currency_idif not currency.is_zero(order_amount - payment_amount):