# app/contract_generator.py from jinja2 import Template from datetime import datetime import os class ContractGenerator: def __init__(self): self.contract_template = """ Independent Contractor Agreement

🏗️ RATERIGHT INDEPENDENT CONTRACTOR AGREEMENT

Date: {{ date }}

📋 Contract Parties:

Hirer (Contractor): {{ hirer_name }} (ABN: {{ hirer_abn }})

Worker: {{ contractor_name }} (ABN: {{ contractor_abn }})

Location: {{ job_location }}

💼 Job Details

Job: {{ job_title }}

Description: {{ job_description }}

Hourly Rate: ${{ hourly_rate }} per hour ({{ gst_note }})

Risk Level: {{ risk_level }}

🛡️ Safety & Insurance Requirements

Insurance Required: ${{ insurance_amount }} public liability

White Card: {{ white_card_required }}

Safety Controls: {{ safety_controls }}

⚖️ Legal Terms

✅ This is an independent contractor relationship, not employment

✅ Worker provides own tools and controls work methods

✅ Payment within 7 days of completed work

✅ Either party may terminate with 7 days notice

✅ Compliant with Australian WHS Act 2011 and Fair Work Act

✍️ Digital Agreement

Hirer Acceptance: {{ hirer_signature_date }}

Worker Acceptance: {{ contractor_signature_date }}

Contract ID: RR-{{ contract_id }}

""" def generate_contract_data(self, job, hirer, worker, application): """Generate contract data from job and user info""" # Determine insurance amount based on risk insurance_amounts = { 'low': '$1,000,000', 'medium': '$2,000,000', 'high': '$5,000,000', 'extreme': '$10,000,000' } risk_level = getattr(job.category, 'whs_risk_level', 'medium') if hasattr(job, 'category') else 'medium' return { 'date': datetime.now().strftime('%d %B %Y'), 'job_title': job.title, 'job_description': job.description, 'job_location': job.location, 'hourly_rate': f'{job.hourly_rate:.2f}', 'gst_note': 'plus GST if applicable', # Parties 'hirer_name': f'{hirer.first_name} {hirer.last_name}', 'hirer_abn': hirer.abn_number or 'Pending', 'contractor_name': f'{worker.first_name} {worker.last_name}', 'contractor_abn': worker.abn_number or 'Pending', # Risk and safety 'risk_level': risk_level.upper(), 'insurance_amount': insurance_amounts.get(risk_level.lower(), '$2,000,000'), 'white_card_required': 'Required' if job.white_card_required else 'Not Required', 'safety_controls': self._get_safety_controls(risk_level), # Signatures (will be filled when signed) 'hirer_signature_date': 'Pending', 'contractor_signature_date': 'Pending', 'contract_id': f'{job.id}-{application.id}-{datetime.now().strftime("%Y%m%d")}' } def _get_safety_controls(self, risk_level): """Get safety controls based on risk level""" controls = { 'low': 'Basic PPE, site induction', 'medium': 'PPE, SWMS review, toolbox talks', 'high': 'Full SWMS, licensing verification, daily briefings', 'extreme': 'SafeWork NSW notification, specialist licensing, continuous monitoring' } return controls.get(risk_level.lower(), 'Standard safety protocols') def render_contract_html(self, contract_data): """Render contract as HTML""" template = Template(self.contract_template) return template.render(**contract_data) def save_contract_html(self, contract_data, filename): """Save contract as HTML file""" html_content = self.render_contract_html(contract_data) # Create contracts directory if it doesn't exist os.makedirs('contracts', exist_ok=True) filepath = f'contracts/{filename}' with open(filepath, 'w', encoding='utf-8') as f: f.write(html_content) return filepath # Test function def test_contract_generation(): """Test the contract generator with dummy data""" class DummyJob: def __init__(self): self.id = 1 self.title = "Formwork Installation" self.description = "Install formwork for concrete slab, 3 days work" self.location = "Parramatta, NSW" self.hourly_rate = 45.00 self.white_card_required = True class DummyUser: def __init__(self, name, abn): self.first_name = name.split()[0] self.last_name = name.split()[1] if len(name.split()) > 1 else "" self.abn_number = abn class DummyApplication: def __init__(self): self.id = 123 # Create test data job = DummyJob() hirer = DummyUser("John Smith", "12 345 678 901") worker = DummyUser("Mike Johnson", "98 765 432 109") application = DummyApplication() # Generate contract generator = ContractGenerator() contract_data = generator.generate_contract_data(job, hirer, worker, application) # Save as HTML filename = f"test_contract_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html" filepath = generator.save_contract_html(contract_data, filename) print(f"✅ Test contract generated: {filepath}") print(f"📄 Contract ID: {contract_data['contract_id']}") return filepath if __name__ == "__main__": test_contract_generation()