Project

General

Profile

Download (3.59 KB) Statistics
| Branch: | Tag: | Revision:

root / extra / modules / abelium_domino_synchro.py @ 5aa61d1d

1
from datetime import datetime
2
from collections import defaultdict
3

    
4
from qommon.cron import CronJob
5
from qommon.publisher import get_publisher_class
6

    
7
from wcs.users import User
8

    
9
from abelium_domino_ui import get_client, is_activated
10
from payments import Invoice, Transaction
11

    
12
def synchronize_domino(publisher):
13
    if not is_activated(publisher):
14
        return
15
    client = get_client(publisher)
16
    users = User.values()
17

    
18
from collections import defaultdict
19

    
20
from qommon.cron import CronJob
21
from qommon.publisher import get_publisher_class
22

    
23
from wcs.users import User
24

    
25
from abelium_domino_ui import get_client, is_activated
26

    
27
DOMINO_ID_PREFIX = 'DOMINO-'
28

    
29
def synchronize_domino(publisher):
30
    if not is_activated(publisher):
31
        return
32
    client = get_client(publisher)
33
    users = User.values()
34
    users_by_mail = dict(((user.email, user) for user in users))
35
    invoices = client.get_invoices(state='TOUTES')
36
    invoices_paid = []
37
    # import new invoices
38
    for invoice in invoices:
39
        user = None
40
        for email in (invoice.family.email_pere, invoice.family.email_mere,
41
                invoice.family.adresse_internet):
42
            user = users_by_mail.get(email)
43
            if user:
44
                break
45
        else:
46
            continue
47
        external_id = '%s%s' % (DOMINO_ID_PREFIX, invoice.id)
48
        payment_invoice = Invoice.get_on_index(external_id, 'external_id',)
49
        if payment_invoice:
50
            continue
51
        payment_invoice = Payment()
52
        payment_invoice.user_id = user.id
53
        payment_invoice.external_id = external_id
54
        payment_invoice.regie_id = cfg.get(REGIE_ID)
55
        payment_invoice.formdef_id = None
56
        payment_invoice.formdata_id = None
57
        payment_invoice.amount = invoice.montant
58
        payment_invoice.date = invoice.creation
59
        payment_invoice.domino_synchro_date = datetime.now()
60
        payment_invoice.store()
61

    
62
    # update invoices
63
    invoices_ids = dict(((invoice.id, invoice) for invoice in invoices))
64
    for payment_invoice in Invoice.values():
65
        if not payment_invoice.external_id.starswith(DOMINO_ID_PREFIX):
66
            continue
67
        i = payment_invoice.external_id[len(DOMINO_ID_PREFIX):]
68
        i = int(i)
69
        invoice = invoice_ids.get(i)
70
        if payment_invoice.paid:
71
            if not invoice:
72
                continue
73
            if getattr(payment_invoice, 'domino_knows_its_paid'):
74
                continue
75
            transactions = Transaction.get_with_indexed_value('invoice_ids', payment_invoice.id)
76
            if not transactions:
77
                details = '' # not normal, add warning logs
78
            else:
79
                details = repr(transactions[0].__dict__)
80
            if invoice.montant != payment_invoice.amount:
81
                pass # add warning logs
82
            client.pay_invoice([invoice], invoice.montant, details,
83
                    payment_invoice.paid_date)
84
            # memorize the date of synchronization
85
            payment_invoice.domino_knows_its_paid = datetime.now()
86
            payment_invoice.store()
87
        else: # unpaid
88
            if not invoice:
89
                # invoice disappeared
90
                payment_invoice.remove_self()
91
            elif invoice.paid():
92
                    payment_invoice.paid = True
93
                    # cannot do better :(
94
                    payment_invoice.paid_date = datetime.now()
95
                    payment_invoice.paid_by_domino = True
96
                    payment_invoice.store()
97
            else: # not invoice.paid()
98
                pass
99

    
100
get_publisher_class().register_cronjob(CronJob(function=synchronize_domino,
101
    hours=range(24), minutes=range(0,60,10)))
(2-2/30)