# Subscription System - Quick Reference Card

## 🚀 Quick Start (5 Minutes)

```bash
# 1. Import database
mysql -u root -p < database/master_subscription_db.sql

# 2. Update credentials in:
#    application/config/master_database.php

# 3. Done! System is ready to use
```

---

## 📍 Key Files

| File | Purpose |
|------|---------|
| `database/master_subscription_db.sql` | Master database schema |
| `application/config/master_database.php` | Database connection |
| `application/config/subscription.php` | System settings |
| `application/libraries/Subscription_validator.php` | Core validation logic |
| `application/hooks/Subscription_check.php` | Auto-check on page load |
| `application/views/comman/subscription_alert.php` | Dashboard alert |

---

## ⚙️ Configuration Settings

```php
// application/config/subscription.php

// Cache: 1 hour = 3600 seconds
$config['subscription_cache_duration'] = 3600;

// Warnings: 7 days before expiry
$config['subscription_alert_warning'] = 7;

// Block expired users
$config['subscription_block_on_expire'] = TRUE;

// Your support contact
$config['subscription_support_email'] = 'mahfuzcmt@gmail.com';
```

---

## 🎯 Common Tasks

### Add New Client
```sql
INSERT INTO master_subscriptions.client_subscriptions
(tenant_name, company_name, database_name, subscription_plan, start_date, end_date, is_active, contact_email)
VALUES
('clientname', 'Company Name', 'webPos_clientname', 'monthly', CURDATE(), DATE_ADD(CURDATE(), INTERVAL 1 MONTH), 1, 'email@client.com');
```

### Renew Subscription
```sql
UPDATE master_subscriptions.client_subscriptions
SET end_date = DATE_ADD(end_date, INTERVAL 1 YEAR)
WHERE tenant_name = 'clientname';
```

### Suspend Client
```sql
UPDATE master_subscriptions.client_subscriptions
SET is_active = 0
WHERE tenant_name = 'clientname';
```

### Check Expiring Soon
```sql
SELECT tenant_name, company_name, end_date, DATEDIFF(end_date, CURDATE()) as days_left
FROM master_subscriptions.client_subscriptions
WHERE end_date BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 7 DAY)
AND is_active = 1;
```

---

## 🧪 Testing

### Test Expired Subscription
```sql
-- Set to expired
UPDATE master_subscriptions.client_subscriptions
SET end_date = DATE_SUB(CURDATE(), INTERVAL 5 DAY)
WHERE tenant_name = 'default';

-- Clear cache: Visit http://localhost/pos/subscription/clear_cache
-- Reload dashboard → Should see expiration page

-- Restore
UPDATE master_subscriptions.client_subscriptions
SET end_date = '2099-12-31'
WHERE tenant_name = 'default';
```

---

## 🔧 Troubleshooting

| Problem | Solution |
|---------|----------|
| "Subscription not found" | Add tenant to master database |
| Alerts not showing | Clear cache, check dashboard includes alert component |
| Access not blocked | Verify `subscription_block_on_expire = TRUE` |
| Hook not running | Check `enable_hooks = TRUE` in config.php |

---

## 📊 Status Levels

| Status | Meaning | Color | Access |
|--------|---------|-------|--------|
| **Active** | Valid, >7 days left | Green/Blue | ✅ Allowed |
| **Warning** | Valid, ≤7 days left | Yellow | ✅ Allowed |
| **Grace Period** | Expired, within grace | Orange | ✅ Allowed |
| **Expired** | Past grace period | Red | ❌ Blocked |
| **Suspended** | Manually disabled | Gray | ❌ Blocked |

---

## 💻 Code Examples

### Check in Controller
```php
$this->load->library('subscription_validator');
$status = $this->subscription_validator->check_validity();

if ($status['is_valid']) {
    // Allow access
} else {
    // Block or warn
}
```

### Get Details
```php
$sub = $this->subscription_validator->get_subscription_details();
echo $sub->company_name;
echo $sub->end_date;
```

### Clear Cache
```php
$this->subscription_validator->clear_cache('tenant_name');
```

---

## 🔐 Important URLs

| URL | Purpose | Access |
|-----|---------|--------|
| `/subscription/status` | View subscription details | Admin only |
| `/subscription/clear_cache` | Force cache refresh | Admin only |
| `/subscription/expired` | Expiration notice page | Auto-redirect |

---

## 📞 Support

**Email**: mahfuzcmt@gmail.com
**Phone**: +8801975585960
**Docs**: SUBSCRIPTION_SETUP_GUIDE.md

---

## ✨ Key Features

- ✅ 1-hour caching
- ✅ Auto-validation on every page load
- ✅ 7-day advance warnings
- ✅ 3-day grace period
- ✅ Multi-tenant support
- ✅ Audit trail
- ✅ Customizable alerts

---

**Version**: 1.0.0
**Last Updated**: 2025-01-20
