Created
June 8, 2012 14:20
-
-
Save robrocker7/2895847 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 Application(object): | |
# this map is used for 1 to 1 field mapping, not all fields are listed | |
# here. Some will be generated depending on the information in the | |
# passed data | |
FILE_PATH = os.path.join(settings.PROJECT_ROOT, 'src', 'apps', 'pdfs', 'static', 'pdfs', '03-Application.pdf') | |
EXPORT_PATH = os.path.join(dsettings.MEDIA_ROOT, 'forms') | |
def __init__(self, data): | |
self.data = data | |
self.mapped_data = {} | |
self.client = self.data.client | |
self.mapped_data['FIRST NAME'] = self.client.profile.first_name | |
self.mapped_data['LAST NAME'] = self.client.profile.last_name | |
self.mapped_data['BIRTH DATE'] = self.client.date_of_birth | |
self.mapped_data['STREET ADDRESS'] = self.client.home_address.get_street_address() | |
self.mapped_data['CITY'] = self.client.home_address.city | |
self.mapped_data['STATE'] = self.client.home_address.state | |
self.mapped_data['ZIP'] = self.client.home_address.zip_code | |
self.mapped_data['EMAIL ADDRESS'] = self.client.profile.email | |
self.mapped_data['SOCIAL SECURITY NUMBER'] = self.client.social | |
# first number | |
phone = self.client.phone_numbers.all()[0] | |
self.mapped_data['HOME TELEPHONE'] = phone.number | |
self.mapped_data['DAYTIME TELEPHONE'] = phone.number | |
# Populate Account Type Radio | |
if self.data.ira_type == 'traditional_ira': | |
self.mapped_data['Radio Button1'] = '0' | |
elif self.data.ira_type == 'roth_ira': | |
self.mapped_data['Radio Button1'] = '1' | |
elif self.data.ira_type == 'roth_conversion': | |
self.mapped_data['Radio Button1'] = '1' | |
elif self.data.ira_type == 'sep_ira': | |
self.mapped_data['Radio Button1'] = '2' | |
elif self.data.ira_type == 'roth_ira': | |
self.mapped_data['Radio Button1'] = '3' | |
# Populate Fund Your Account | |
if self.data.transfer_method == 'transfer': | |
self.mapped_data['Radio Button2'] = '1' | |
elif self.data.transfer_method == 'rollover': | |
self.mapped_data['Radio Button2'] = '0' | |
self.mapped_data['Prior Custodian Plan Name'] = self.data.custodians.all()[0].name | |
self.mapped_data['Expected Rollover Amount'] = self.data.custodians.all()[0].liquidate_cash | |
elif self.data.transfer_method == 'direct_rollover': | |
self.mapped_data['Radio Button2'] = '0' | |
self.mapped_data['Prior Custodian Plan Name'] = self.data.custodians.all()[0].name | |
self.mapped_data['Expected Rollover Amount'] = self.data.custodians.all()[0].liquidate_cash | |
# set date | |
self.mapped_data['Date'] = self.data.date_created.month | |
self.mapped_data['undefined'] = self.data.date_created.day | |
self.mapped_data['undefined_2'] = self.data.date_created.year | |
formatted_list = [] | |
for key, value in self.mapped_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 = 'APPLICATION_%s-%s-%s-%s-%s.pdf' % ( | |
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 = os.path.join(client_file, application_name) | |
os.unlink(tmp_file.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment