Revision 84fd22a0
Added by Benjamin Dauvergne about 12 years ago
extra/modules/abelium_domino_synchro.py | ||
---|---|---|
21 | 21 |
client.clear_cache() |
22 | 22 |
users = User.values() |
23 | 23 |
users_by_mail = dict(((user.email, user) for user in users)) |
24 |
users_by_code_interne = {} |
|
25 |
for user in users: |
|
26 |
if hasattr(user, 'abelium_domino_code_famille'): |
|
27 |
users_by_code_interne[user.abelium_domino_code_famille] = user |
|
24 | 28 |
try: |
25 |
invoices = client.get_invoices(state='TOUTES')
|
|
29 |
invoices = client.invoices
|
|
26 | 30 |
except abelium_domino_ws.DominoException, e: |
27 |
logger.error('failure to retrieve invoice list from domino '
|
|
31 |
logger.debug('domino cron: failure to retrieve invoice list from domino '
|
|
28 | 32 |
'for synchronization [error:%s]', e) |
29 | 33 |
return |
30 | 34 |
# import new invoices |
31 |
for invoice in invoices: |
|
35 |
logger.debug('domino cron: retrieved %i invoices', len(invoices)) |
|
36 |
for invoice_id, invoice in invoices.iteritems(): |
|
32 | 37 |
user = None |
33 |
for email in (invoice.family.email_pere, invoice.family.email_mere, |
|
34 |
invoice.family.adresse_internet): |
|
35 |
user = users_by_mail.get(email) |
|
36 |
if user: |
|
37 |
break |
|
38 |
else: |
|
39 |
continue |
|
38 |
if invoice.family.code_interne in users_by_code_interne: |
|
39 |
user = users_by_code_interne[invoice.family.code_interne] |
|
40 |
if user is None: |
|
41 |
for email in (invoice.family.email_pere, invoice.family.email_mere, |
|
42 |
invoice.family.adresse_internet): |
|
43 |
user = users_by_mail.get(email) |
|
44 |
if user: |
|
45 |
break |
|
46 |
else: |
|
47 |
continue |
|
40 | 48 |
external_id = '%s%s' % (DOMINO_ID_PREFIX, invoice.id) |
41 |
payment_invoice = Invoice.get_on_index(external_id, 'external_id',) |
|
49 |
payment_invoice = Invoice.get_on_index(external_id, 'external_id', ignore_errors=True)
|
|
42 | 50 |
if payment_invoice: |
43 | 51 |
continue |
44 | 52 |
payment_invoice = Invoice() |
... | ... | |
51 | 59 |
payment_invoice.date = invoice.creation |
52 | 60 |
payment_invoice.domino_synchro_date = datetime.now() |
53 | 61 |
payment_invoice.store() |
54 |
logger.info('remote invoice %s for family %s added to user %s invoices with id %s', |
|
62 |
logger.info('domino cron: remote invoice %s for family %s added to user %s invoices with id %s',
|
|
55 | 63 |
invoice.id, invoice.family.id, user.id, payment_invoice.id) |
56 | 64 |
|
57 | 65 |
# update invoices |
58 |
invoices_ids = dict(((invoice.id, invoice) for invoice in invoices))
|
|
66 |
invoices_ids = dict(invoices.iteritems())
|
|
59 | 67 |
for payment_invoice in Invoice.values(): |
60 |
if not payment_invoice.external_id.starswith(DOMINO_ID_PREFIX):
|
|
68 |
if payment_invoice.external_id is None or not payment_invoice.external_id.startswith(DOMINO_ID_PREFIX):
|
|
61 | 69 |
continue # not a payment related to domino we skip |
62 | 70 |
i = payment_invoice.external_id[len(DOMINO_ID_PREFIX):] |
63 | 71 |
i = int(i) |
... | ... | |
67 | 75 |
# invoice has been paid (locally or not) but remote invoice has |
68 | 76 |
# been deleted do, we do nothing. |
69 | 77 |
continue |
70 |
if getattr(payment_invoice, 'domino_knows_its_paid'): |
|
78 |
if getattr(payment_invoice, 'domino_knows_its_paid', None) or getattr(payment_invoice, 'paid_by_domino', None):
|
|
71 | 79 |
# synchronization of payment already done, skip |
72 | 80 |
continue |
73 | 81 |
transactions = Transaction.get_with_indexed_value('invoice_ids', payment_invoice.id) |
74 | 82 |
if not transactions: |
75 |
logger.warning("invoice %s is marked paid but does " |
|
83 |
logger.warning("domino cron: invoice %s is marked paid but does "
|
|
76 | 84 |
"not have any linked transaction.", payment_invoice.id) |
77 | 85 |
details = '' # no details about the payment, problem |
78 | 86 |
else: |
... | ... | |
83 | 91 |
client.pay_invoice([invoice], invoice.montant, details, |
84 | 92 |
payment_invoice.paid_date) |
85 | 93 |
except abelium_domino_ws.DominoException, e: |
86 |
logger.error('invoice %s has been paid, but the remote system ' |
|
94 |
logger.error('domino cron: invoice %s has been paid, but the remote system '
|
|
87 | 95 |
'is unreachable, notification will be done again ' |
88 | 96 |
'later [error: %s]', invoice.id, e) |
89 | 97 |
else: |
90 | 98 |
# memorize the date of synchronization |
91 | 99 |
payment_invoice.domino_knows_its_paid = datetime.now() |
92 | 100 |
payment_invoice.store() |
93 |
logger.info('invoice %s has been paid; remote system has been ' |
|
101 |
logger.info('domino cron: domino: invoice %s has been paid; remote system has been '
|
|
94 | 102 |
'notified', payment_invoice.id) |
95 | 103 |
else: # unpaid |
96 | 104 |
if not invoice: |
97 |
logger.info('remote invoice %s disapearred, so its ' |
|
105 |
logger.info('domino cron: remote invoice %s disapearred, so its '
|
|
98 | 106 |
'still-unpaid local counterpart invoice %s was deleted.', |
99 | 107 |
i, payment_invoice.id) |
100 | 108 |
payment_invoice.remove_self() |
... | ... | |
104 | 112 |
payment_invoice.paid_date = datetime.now() |
105 | 113 |
payment_invoice.paid_by_domino = True |
106 | 114 |
payment_invoice.store() |
107 |
logging.info('remote invoice %s has beend paid, '
|
|
115 |
logger.info('domino cron: remote invoice %s has beend paid, '
|
|
108 | 116 |
'local invoice %s of user %s is now marked as paid.', |
109 | 117 |
invoice.id, payment_invoice.id, payment_invoice.user_id) |
110 | 118 |
else: # not invoice.paid() |
111 | 119 |
pass # still waiting for the payment |
112 | 120 |
|
113 | 121 |
get_publisher_class().register_cronjob(CronJob(function=synchronize_domino, |
114 |
hours=range(24), minutes=range(0,60,10))) |
|
122 |
hours=range(0, 24), minutes=range(0, 60))) |
Also available in: Unified diff
[domino] finish family update workflow
- when creating or updating a family, store the family.code_interne value into
the user object for later retrieval,
- add web service API to retrieve person to contact for children