Project

General

Profile

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

calebasse / calebasse / facturation / batches.py @ 017f4158

1
# -*- coding: utf-8 -*-
2

    
3
from collections import defaultdict
4

    
5
class Batch(object):
6
    def __init__(self, number, invoices):
7
        self.number = number
8
        self.health_center = invoices[0].health_center
9
        self.invoices = invoices
10
        self.number_of_invoices = len(invoices)
11
        self.total = sum(invoice.decimal_amount for invoice in invoices)
12
        self.number_of_acts = sum(len(invoice.list_dates.split('$')) for invoice in invoices)
13
        self.start_date = min(invoice.start_date for invoice in invoices)
14
        self.end_date = max(invoice.end_date for invoice in invoices)
15

    
16
    def __str__(self):
17
        return '%s pour %s (%d factures)' % (self.number, self.health_center, self.number_of_invoices)
18

    
19
def build_batches(invoicing):
20
    invoices = invoicing.invoice_set.order_by('number')
21
    prebatches = defaultdict(lambda:[])
22
    for invoice in invoices:
23
        prebatches[(invoice.health_center, invoice.batch)].append(invoice)
24
    batches_by_health_center = defaultdict(lambda:[])
25
    # FIXME / A REVOIR car il ne pas depasser 999 lignes par batches_by_health_center...
26
    for health_center, batch_number in sorted(prebatches.keys()):
27
        hc = health_center.hc_invoice or health_center
28
        batches_by_health_center[hc].append(Batch(batch_number,
29
            prebatches[(health_center, batch_number)]))
30
    return batches_by_health_center
(4-4/16)