Files
Odoo-Modules/run_poynt_sync.py
gsinghpal c66bdf5089 changes
2026-04-03 15:45:18 -04:00

108 lines
3.6 KiB
Python

from datetime import date, timedelta
import logging
_logger = logging.getLogger('poynt_sync')
Batch = env['poynt.settlement.batch']
provider = env['payment.provider'].search([('code', '=', 'poynt'), ('state', '=', 'enabled')], limit=1)
if not provider:
print('ERROR: No active Poynt provider found', flush=True)
else:
print(f'Provider: {provider.name} (ID {provider.id})', flush=True)
print(f'Business ID: {provider.poynt_business_id}', flush=True)
# Process each day from 2024-01-01 to today
# Skip weekends (Saturday batched with Friday→Monday)
start_date = date(2024, 1, 1)
end_date = date.today() - timedelta(days=1) # yesterday
current = start_date
batches_created = 0
batches_with_txns = 0
total_lines = 0
errors = []
while current <= end_date:
weekday = current.weekday()
# Skip Saturday — batched with Sunday→Monday
if weekday == 5:
current += timedelta(days=1)
continue
# For Sunday, fetch Fri+Sat+Sun
if weekday == 6:
txn_from = current - timedelta(days=2) # Friday
else:
txn_from = current
settlement_date = current + timedelta(days=1)
# If settlement falls on weekend, push to Monday
if settlement_date.weekday() == 5:
settlement_date += timedelta(days=2)
elif settlement_date.weekday() == 6:
settlement_date += timedelta(days=1)
# Check if batch already exists
existing = Batch.search([
('provider_id', '=', provider.id),
('transaction_date', '=', txn_from),
])
if existing:
current += timedelta(days=1)
continue
try:
batch = Batch.create({
'provider_id': provider.id,
'transaction_date': txn_from,
'settlement_date': settlement_date,
})
batch.action_fetch_transactions()
batches_created += 1
line_count = len(batch.line_ids)
if line_count > 0:
batches_with_txns += 1
total_lines += line_count
# Try to match deposit
try:
batch.action_match_deposit()
except Exception:
pass
# Try to match customers
try:
batch.action_match_customers()
except Exception:
pass
else:
# No transactions — delete empty batch
batch.unlink()
batches_created -= 1
except Exception as e:
err_msg = str(e)[:100]
if err_msg not in [e[:100] for e in errors]:
errors.append(str(e))
print(f' Error on {current}: {err_msg}', flush=True)
if batches_created % 30 == 0 and batches_created > 0:
env.cr.commit()
print(f' Progress: {batches_created} batches, {total_lines} lines, date={current}', flush=True)
current += timedelta(days=1)
env.cr.commit()
print(f'\nDONE:', flush=True)
print(f' Batches created: {batches_created}', flush=True)
print(f' Batches with transactions: {batches_with_txns}', flush=True)
print(f' Total transaction lines: {total_lines}', flush=True)
print(f' Errors: {len(errors)}', flush=True)
# Summary of batch states
all_batches = Batch.search([('provider_id', '=', provider.id)])
matched = all_batches.filtered(lambda b: b.state == 'matched')
print(f' Matched to deposits: {len(matched)} batches', flush=True)