Created
June 8, 2012 14:24
-
-
Save robrocker7/2895877 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from datetime import datetime | |
import settings | |
from django.conf import settings as dsettings | |
from fdfgen import forge_fdf | |
from tempfile import NamedTemporaryFile | |
from subprocess import call | |
class Beneficiary(object): | |
FILE_PATH = os.path.join(settings.PROJECT_ROOT, 'src', 'apps', 'pdfs', 'static', 'pdfs', '04-BeneDesignation.pdf') | |
EXPORT_PATH = os.path.join(dsettings.MEDIA_ROOT, 'forms') | |
def __init__(self, data): | |
self.data = data | |
self.mapped_data = {} | |
self.primaries = 0 | |
self.contingents = 0 | |
self.documents = 0 | |
self.primary_doc_count = 0 | |
self.contingent_doc_count = 0 | |
self.client = self.data.client | |
self.beneficiaries = self.data.beneficiaries.all() | |
self.file_path = [] | |
self.full_name = '%s %s' % ( | |
self.client.profile.first_name, self.client.profile.last_name) | |
phone = self.client.phone_numbers.all()[0] | |
self.phone = phone.number | |
#create docs in mapped_data | |
for bene in self.beneficiaries: | |
if bene.bene_type == 'primary': | |
if self.primaries % 2: | |
self.primary_doc_count = self.primary_doc_count + 1 | |
self.primaries = self.primaries + 1 | |
elif bene.bene_type == 'contingent': | |
if self.contingents % 2: | |
self.contingent_doc_count = self.contingent_doc_count + 1 | |
self.contingents = self.contingents + 1 | |
self.documents = max([self.primary_doc_count, self.contingent_doc_count]) | |
for x in range(0, self.documents+1): | |
document_name = 'DOCUMENT_%s' % x | |
self.mapped_data[document_name] = {} | |
self.primaries = 0 | |
self.primary_doc_count = 0 | |
self.contingents = 0 | |
self.contingent_doc_count = 0 | |
for bene in self.beneficiaries: | |
if bene.bene_type == 'primary': | |
self.populate_primary( | |
self.mapped_data['DOCUMENT_%s' % self.primary_doc_count], bene) | |
self.primaries = self.primaries + 1 | |
if not self.primaries % 2: | |
self.primary_doc_count = self.primary_doc_count + 1 | |
# reset documents | |
for bene in self.beneficiaries: | |
if bene.bene_type == 'contingent': | |
self.populate_contingent( | |
self.mapped_data['DOCUMENT_%s' % self.contingent_doc_count], bene) | |
self.contingents = self.contingents + 1 | |
if not self.contingents % 2: | |
self.contingent_doc_count = self.contingent_doc_count + 1 | |
for x in range(0, self.documents+1): | |
self.generate_bene_pdf(x) | |
def generate_bene_pdf(self, document_num): | |
doc_data = self.mapped_data['DOCUMENT_%s' % document_num] | |
doc_data['FULL NAME'] = self.full_name | |
doc_data['PHONE NO'] = self.phone | |
formatted_list = [] | |
for key, value in doc_data.iteritems(): | |
formatted_list.append((key, value)) | |
# generate PDF | |
fdf = forge_fdf("", formatted_list, [], [], []) | |
tmp_file = NamedTemporaryFile(delete=False) | |
tmp_file.write(fdf) | |
tmp_file.close() | |
# check to see if a file exists for the client | |
client_file = '%s_%s_%s' % (self.client.profile.last_name.upper(), | |
self.client.profile.first_name.upper(), | |
self.client.pk) | |
client_file_path = os.path.join(self.EXPORT_PATH, client_file) | |
if not os.path.exists(client_file_path): | |
os.mkdir(client_file_path) | |
today = datetime.today() | |
application_name = 'BENEDESIGNATION_%s_%s-%s-%s-%s-%s.pdf' % ( | |
document_num, | |
today.month, | |
today.day, | |
today.year, | |
today.hour, | |
today.minute) | |
call([ | |
"pdftk", self.FILE_PATH, | |
"fill_form", tmp_file.name, | |
"output", os.path.join(client_file_path, application_name), | |
"flatten"]) | |
self.file_path.append(os.path.join(client_file, application_name)) | |
os.unlink(tmp_file.name) | |
def populate_primary(self, document_data, bene): | |
if not self.primaries % 2: | |
document_data['BENEFICIARY NAME'] = '%s %s' % ( | |
bene.first_name, bene.last_name) | |
document_data['RELATIONSHIP'] = bene.relationship | |
document_data['BENEFICIARY ADDRESS'] = bene.get_street_address() | |
document_data['BENEFICIARY CITY'] = bene.city | |
document_data['BENEFICIARY STATE'] = bene.state | |
document_data['BENEFICIARY ZIP'] = bene.zip_code | |
document_data['BENEFICIARY SOCIAL SECURITY NUMBER'] = bene.social | |
document_data['BENEFICIARY BIRTH DATE'] = bene.date_of_birth | |
document_data['BENEFICIARY TELEPHONE NUMBER'] = '' | |
document_data['PERCENT'] = bene.percentage | |
else: | |
document_data['BENEFICIARY NAME_2'] = '%s %s' % ( | |
bene.first_name, bene.last_name) | |
document_data['RELATIONSHIP_2'] = bene.relationship | |
document_data['BENEFICIARY ADDRESS_2'] = bene.get_street_address() | |
document_data['BENEFICIARY CITY 2'] = bene.city | |
document_data['BENEFICIARY STATE 2'] = bene.state | |
document_data['BENEFICIARY ZIP 2'] = bene.zip_code | |
document_data['BENEFICIARY SOCIAL SECURITY NUMBER_2'] = bene.social | |
document_data['BENEFICIARY BIRTH DATE_2'] = bene.date_of_birth | |
document_data['BENEFICIARY TELEPHONE NUMBER_2'] = '' | |
document_data['PERCENT 2'] = bene.percentage | |
def populate_contingent(self, document_data, bene): | |
if not self.contingents % 2: | |
document_data['BENEFICIARY NAME_3'] = '%s %s' % ( | |
bene.first_name, bene.last_name) | |
document_data['RELATIONSHIP_3'] = bene.relationship | |
document_data['BENEFICIARY ADDRESS_3'] = bene.get_street_address() | |
document_data['BENEFICIARY CITY 3'] = bene.city | |
document_data['BENEFICIARY STATE 3'] = bene.state | |
document_data['BENEFICIARY ZIP 3'] = bene.zip_code | |
document_data['BENEFICIARY SOCIAL SECURITY NUMBER_3'] = bene.social | |
document_data['BENEFICIARY BIRTH DATE_3'] = bene.date_of_birth | |
document_data['BENEFICIARY TELEPHONE NUMBER_3'] = '' | |
document_data['PERCENT 3'] = bene.percentage | |
else: | |
document_data['BENEFICIARY NAME_4'] = '%s %s' % ( | |
bene.first_name, bene.last_name) | |
document_data['RELATIONSHIP_4'] = bene.relationship | |
document_data['BENEFICIARY ADDRESS_4'] = bene.get_street_address() | |
document_data['BENEFICIARY CITY 4'] = bene.city | |
document_data['BENEFICIARY STATE 4'] = bene.state | |
document_data['BENEFICIARY ZIP 4'] = bene.zip_code | |
document_data['BENEFICIARY SOCIAL SECURITY NUMBER_4'] = bene.social | |
document_data['BENEFICIARY BIRTH DATE_4'] = bene.date_of_birth | |
document_data['BENEFICIARY TELEPHONE NUMBER_4'] = '' | |
document_data['PERCENT 4'] = bene.percentage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment