Revision 2f2a7de9
Added by Mikaël Ates almost 12 years ago
calebasse/facturation/invoice_header.py | ||
---|---|---|
13 | 13 |
from invoice_template import InvoiceTemplate |
14 | 14 |
from ..pdftk import PdfTk |
15 | 15 |
from batches import build_batches |
16 |
from calebasse.utils import get_nir_control_key |
|
16 | 17 |
|
17 | 18 |
class Counter(object): |
18 | 19 |
def __init__(self, initial_value=0): |
... | ... | |
159 | 160 |
ctx['TOTAL'] = total1+total2 |
160 | 161 |
return [tpl.generate(ctx)] |
161 | 162 |
|
163 |
def render_not_cmpp_header(invoicing, counter): |
|
164 |
header_template='facturation/bordereau_not_cmpp_header.html' |
|
165 |
management_codes = dict() |
|
166 |
total_acts = 0 |
|
167 |
for invoice in invoicing.invoice_set.all(): |
|
168 |
total_acts += invoice.acts.count() |
|
169 |
if invoice.policy_holder_management_code: |
|
170 |
management_codes.setdefault(invoice.policy_holder_management_code, []).append(invoice) |
|
171 |
list_management_codes = list() |
|
172 |
for mc, invoices in management_codes.iteritems(): |
|
173 |
dic = dict() |
|
174 |
dic['code'] = mc |
|
175 |
dic['title'] = invoices[0].policy_holder_management_code_name |
|
176 |
dic['nb_files'] = len(invoices) |
|
177 |
nb_acts = 0 |
|
178 |
for invoice in invoices: |
|
179 |
nb_acts += invoice.acts.count() |
|
180 |
dic['nb_acts'] = nb_acts |
|
181 |
list_management_codes.append(dic) |
|
182 |
list_management_codes.sort(key=lambda dic: dic['code']) |
|
183 |
ctx = { |
|
184 |
'now': datetime.datetime.now(), |
|
185 |
'service': invoicing.service, |
|
186 |
'start_date': invoicing.start_date, |
|
187 |
'end_date': invoicing.end_date, |
|
188 |
'counter': counter, |
|
189 |
'list_management_codes': list_management_codes, |
|
190 |
'total_files': invoicing.invoice_set.count(), |
|
191 |
'total_acts': total_acts |
|
192 |
} |
|
193 |
prefix = '%s-invoicing-header-%s' % ( |
|
194 |
invoicing.service.slug, invoicing.id) |
|
195 |
return render_to_pdf_file( |
|
196 |
(header_template, ), ctx, prefix=prefix, delete=True) |
|
197 |
|
|
198 |
def render_not_cmpp_content(invoicing, counter): |
|
199 |
header_template='facturation/bordereau_not_cmpp_content.html' |
|
200 |
total_acts = 0 |
|
201 |
list_patients = list() |
|
202 |
for invoice in invoicing.invoice_set.all(): |
|
203 |
total_acts += invoice.acts.count() |
|
204 |
policy_holder = '' |
|
205 |
if invoice.policy_holder_last_name and invoice.policy_holder_first_name: |
|
206 |
policy_holder = invoice.policy_holder_last_name.upper() + ' ' + invoice.policy_holder_first_name |
|
207 |
nir = None |
|
208 |
if invoice.policy_holder_social_security_id: |
|
209 |
nir = invoice.policy_holder_social_security_id + ' ' + str(get_nir_control_key(invoice.policy_holder_social_security_id)) |
|
210 |
health_center = '' |
|
211 |
tp = '' |
|
212 |
cai = '' |
|
213 |
if invoice.policy_holder_healthcenter: |
|
214 |
health_center = invoice.policy_holder_healthcenter.name |
|
215 |
if invoice.policy_holder_healthcenter.large_regime: |
|
216 |
tp = invoice.policy_holder_healthcenter.large_regime.code |
|
217 |
cai = invoice.policy_holder_healthcenter.health_fund |
|
218 |
name = '' |
|
219 |
if invoice.patient_last_name and invoice.patient_first_name: |
|
220 |
name = invoice.patient_last_name.upper() + ' ' + invoice.patient_first_name |
|
221 |
list_patients.append({\ |
|
222 |
'code' : invoice.policy_holder_management_code, |
|
223 |
'policy_holder': policy_holder, |
|
224 |
'nir': nir, |
|
225 |
'health_center': health_center, |
|
226 |
'tp': tp, |
|
227 |
'cai': cai, |
|
228 |
'cen': invoice.policy_holder_other_health_center, |
|
229 |
'number': invoice.patient_id, |
|
230 |
'name': name, |
|
231 |
'birth_date': invoice.patient_birthdate, |
|
232 |
'inscription_date': invoice.patient_entry_date, |
|
233 |
'sortie_date': invoice.patient_exit_date, |
|
234 |
'nb_actes': invoice.acts.count()}) |
|
235 |
list_patients.sort(key=lambda dic: dic['code']) |
|
236 |
ctx = { |
|
237 |
'now': datetime.datetime.now(), |
|
238 |
'service': invoicing.service, |
|
239 |
'start_date': invoicing.start_date, |
|
240 |
'end_date': invoicing.end_date, |
|
241 |
'counter': counter, |
|
242 |
'total_files': invoicing.invoice_set.count(), |
|
243 |
'total_acts': total_acts, |
|
244 |
'patients': list_patients |
|
245 |
} |
|
246 |
prefix = '%s-invoicing-content-%s' % ( |
|
247 |
invoicing.service.slug, invoicing.id) |
|
248 |
return render_to_pdf_file( |
|
249 |
(header_template, ), ctx, prefix=prefix, delete=True) |
|
250 |
|
|
162 | 251 |
|
163 | 252 |
def render_invoicing(invoicing, delete=False, headers=True, invoices=True): |
164 | 253 |
service = invoicing.service |
165 | 254 |
now = datetime.datetime.now() |
166 |
batches_by_health_center = build_batches(invoicing) |
|
167 |
centers = sorted(batches_by_health_center.keys()) |
|
168 |
all_files = [] |
|
169 | 255 |
output_file = None |
170 |
counter = Counter(1)
|
|
256 |
all_files = []
|
|
171 | 257 |
try: |
172 |
for center in centers: |
|
173 |
if headers is not True and headers is not False and headers != center: |
|
174 |
continue |
|
175 |
for batch in batches_by_health_center[center]: |
|
176 |
files = batches_files(service, invoicing, center, |
|
177 |
[batch], delete=delete, |
|
178 |
headers=headers, invoices=invoices, counter=counter) |
|
179 |
all_files.extend(files) |
|
258 |
if service.name == 'CMPP': |
|
259 |
batches_by_health_center = build_batches(invoicing) |
|
260 |
centers = sorted(batches_by_health_center.keys()) |
|
261 |
counter = Counter(1) |
|
262 |
for center in centers: |
|
263 |
if headers is not True and headers is not False and headers != center: |
|
264 |
continue |
|
265 |
for batch in batches_by_health_center[center]: |
|
266 |
files = batches_files(service, invoicing, center, |
|
267 |
[batch], delete=delete, |
|
268 |
headers=headers, invoices=invoices, counter=counter) |
|
269 |
all_files.extend(files) |
|
270 |
else: |
|
271 |
counter = Counter(1) |
|
272 |
header = render_not_cmpp_header(invoicing, counter) |
|
273 |
all_files.append(header) |
|
274 |
content = render_not_cmpp_content(invoicing, counter) |
|
275 |
all_files.append(content) |
|
180 | 276 |
output_file = tempfile.NamedTemporaryFile(prefix='%s-invoicing-%s-' % |
181 | 277 |
(service.slug, invoicing.id), suffix='-%s.pdf' % now, delete=False) |
182 | 278 |
pdftk = PdfTk() |
... | ... | |
199 | 295 |
pass |
200 | 296 |
|
201 | 297 |
|
202 |
|
|
203 | 298 |
def batches_files(service, invoicing, health_center, batches, delete=False, |
204 | 299 |
headers=True, invoices=True, counter=None): |
205 | 300 |
files = [] |
Also available in: Unified diff
facturation: Add rendering functions for not CMPP services.