# How to Create and Manage Rental Agreements

## Accessing Contract Management

1. **Login** as a Company Admin
2. Navigate to: `https://localhost/real_estate/public/company/contracts`
3. Or click on **"Rental Agreements"** in your dashboard sidebar

---

## Creating a New Rental Agreement

### Step 1: Click "New Agreement"
- Located at the top right of the contracts list page
- Opens the contract creation form

### Step 2: Fill in Contract Details

#### Basic Information
- **Contract Type**: Choose between:
  - 📋 Rental Agreement (for tenants)
  - 🏠 Sale Agreement (for property sales)

- **Property**: Select from your company's available properties
  - Shows property name, location, and auto-fills the price
  
- **Customer**: Select the tenant/buyer from your customer list
  - Only users with "tenant" role appear here

#### Financial Details
- **Total Amount**: Enter the contract value
  - Auto-filled from property price (you can modify)
  
- **Currency**: Choose TZS or USD

- **Payment Type**: Select:
  - 💰 Full Payment - One-time payment
  - 📅 Installment Payment - Multiple payments
    - If installment: Enter number of installments and amount per installment

#### Contract Period
- **Start Date**: When the contract begins
- **End Date**: When the contract ends (must be after start date)

#### Additional Information
- **Terms & Conditions**: Optional - Add rental terms, rules, etc.
- **Additional Notes**: Optional - Any extra information

### Step 3: Submit
- Click **"Create Contract"** button
- Contract is saved in **Draft** status

---

## Managing Contracts

### Viewing Contract List
The main page shows all contracts with:
- 🔍 Search by contract number or customer name
- 📊 Filter by status (Draft, Active, Completed, Terminated)
- 📋 Filter by type (Rental, Sale)
- 🏠 Filter by specific property

### Contract Actions

#### View Details
- Click the 👁️ (eye) icon next to any contract
- See complete contract information
- View payment summary and balance
- Access related invoices, payments, and receipts

#### Activate Contract
For **Draft**contracts only:
1. Click the ✓ (checkmark) icon
2. Contract status changes to **Active**
3. Timestamp recorded as "signed_at"

#### Create Invoice
- Click the 📄 (document) icon
- Automatically links invoice to contract
- Useful for billing tenants

#### Download PDF
- Click the 📥 (download) icon
- Generates professional PDF document
- Includes all contract details and terms
- Ready for printing or sharing

---

## Contract Statuses Explained

### 📝 Draft
- Initial status when created
- Can be edited or deleted
- Not yet legally binding
- Click "Activate" when ready

### ✅ Active
- Contract is in effect
- Tenant can occupy property
- Invoices can be generated
- Payments tracked against contract

### ✔️ Completed
- Contract fulfilled successfully
- All payments received
- Lease term ended normally
- Property returned

### ❌ Terminated
- Contract ended early
- Breach of terms
- Mutual agreement to end
- May have outstanding balances

---

## Creating from a Booking

If you have approved bookings, you can convert them to contracts:

1. Go to Bookings page
2. Find an **Approved** booking
3. Look for "Create Contract" option
4. Pre-filled information from booking
5. Complete remaining details
6. Submit to create contract
7. Booking automatically marked as "Completed"

---

## Payment Tracking

The system automatically tracks:

### Payment Summary
- **Total Amount**: Contract value
- **Paid Amount**: Sum of all completed payments
- **Balance**: Remaining amount due
- **Payment Status**:
  - 🟢 Paid - Fully paid
  - 🟡 Partial - Some payments made
  - 🔴 Unpaid - No payments yet

### Related Records
View directly from contract:
- 📄 Invoices generated
- 💰 Payments received
- 🧾 Receipts issued

---

## Tips for Success

### Best Practices
1. ✅ Always add clear terms and conditions
2. ✅ Use realistic start and end dates
3. ✅ Set appropriate installment plans if needed
4. ✅ Activate contracts immediately after signing
5. ✅ Generate invoices promptly
6. ✅ Keep contracts organized with filters

### Common Uses
- 🏠 Standard residential leases (1 year)
- 🏢 Commercial property rentals (2-5 years)
- 📅 Short-term rentals (month-to-month)
- 💎 Property sale agreements
- 🔄 Contract renewals (create new, reference old)

---

## Troubleshooting

### Can't find a property?
- Ensure property belongs to your company
- Check property status is "Available" or "Rented"
- Contact super admin if property is system-owned

### Can't find a customer?
- Customer must have "tenant" role
- Customer account must be active
- Create new tenant account if needed

### Contract won't activate?
- Check that all required fields are filled
- Ensure dates are valid (end after start)
- Verify property and customer selections

### Need to edit a contract?
- Draft contracts: Can be fully edited
- Active contracts: Should terminate and create new
- Avoid editing active contracts to maintain records

---

## Quick Reference

| Action | Location | Icon |
|--------|----------|------|
| Create New | Contracts page | ➕ |
| View Details | Contracts list | 👁️ |
| Activate | Contracts list | ✓ |
| Create Invoice | Contracts list | 📄 |
| Download PDF | Contracts list | 📥 |
| Search | Top of page | 🔍 |
| Filter | Top of page | 📊 |

---

## Support

For technical issues or questions:
- Check the main documentation
- Contact system administrator
- Review audit logs for changes
