# Deploy — New Earning Report sources

## Files changed

| File | New / Modified | What changed |
|---|---|---|
| `earningsourcemigration.php` | **NEW** | Re-labels old rows, imports past discounts — run once, then delete |
| `includes/wallet.php` | Modified | New source categories, role-aware source lists, legacy aliasing, `report` wallet |
| `includes/functions.php` | Modified | 3-level subscription commission rewritten, `recordOwnDiscount()` added |
| `includes/shares.php` | Modified | Sale referral → `ref_sale`, seller share → `sales_earning`, discount recorded |
| `includes/earning_view.php` | Modified | Role-aware sources, discount card, report-only badges, new filter list |
| `subscription.php` | Modified | Pays the 3-level commission on both wallet and Razorpay purchases |
| `servercheck.php` | Modified | Checks the new markers |

## Run the migration

```
https://yourdomain.com/earningsourcemigration.php
```

Then **delete it**. It creates no tables — it only reclassifies existing rows and
imports past discounts. Safe to re-run.

---

## The new sources

| Source | Where it comes from | Wallet | Real money? |
|---|---|---|---|
| **Refer Subscription — Level 1** | Someone you referred buys a subscription | Virtual | Yes |
| **Refer Subscription — Level 2** | Their referral buys a subscription | Virtual | Yes |
| **Refer Subscription — Level 3** | One level further down | Virtual | Yes |
| **Refer Service & Product Sale** | Someone you referred buys a product or service | Virtual | Yes |
| **My Discount Saved** | Subscriber discount you received | — | **No — report only** |
| **My Sales Earning** | Your own product sales *(sellers only)* | Main | Yes |
| **Staff Share** | Staff commission *(staff/admin only)* | Main | Yes |

The list adapts to **whoever is logged in** — a customer never sees seller or staff
sources; a seller sees their sales earning; staff see the staff share.

Removed from the report: the old generic `Referral Earning`, `Seller Share`, `Bonus`.

---

## 3-level subscription commission is now live

The engine existed but was **never called**, so it never paid out. It now fires on
every subscription purchase — both **Pay from Wallet** and **Razorpay**.

Percentages come from **Admin → Commission Mgt → Refer & Earn**
(`ref_level_1_comm`, `ref_level_2_comm`, `ref_level_3_comm`).

Example on a ₹999 subscription with 10% / 5% / 2%:

| Who | Level | Gets |
|---|---|---|
| Direct referrer | 1 | ₹99.90 |
| Their referrer | 2 | ₹49.95 |
| One above that | 3 | ₹19.98 |
| Level 4 and beyond | — | nothing |

Money lands in the **virtual wallet** (spendable, not withdrawable). A circular or
self-referral chain is guarded against — the loop stops instead of paying twice.

---

## "My Discount Saved" is report-only

This is money the user **saved**, not money they received, so:

- It is stored with `wallet = 'report'` — it never touches a balance
- It is **excluded** from Total Earned, This Month, Last 7 Days, Today and the chart
- It is **excluded** from Wallet History and the wallet summary
- It gets its own emerald card, a dashed panel in the breakdown, and a
  **"Report only"** badge on every row

So the earning totals always reconcile with the actual wallet balances.

---

## Old data is safe

Even if you skip the migration, the report still reads correctly — `referral` folds
into *Refer Service & Product Sale* and `seller_share` into *My Sales Earning* at
read time. The migration just makes it permanent and splits the levels properly:

| Old | Becomes |
|---|---|
| `seller_share` | `sales_earning` |
| `referral` with "Level 1/2/3" in the description | `ref_sub_l1` / `l2` / `l3` |
| `referral` (anything else) | `ref_sale` |
| `product_share_ledger` subscription discounts | `own_discount` (report rows) |

---

## Post-deploy check

1. Run `earningsourcemigration.php` → the table at the bottom shows rows per source → delete it.
2. Open **Earning Report** → the Source dropdown should list the new options only.
3. A customer should **not** see "My Sales Earning" or "Staff Share"; a seller should see "My Sales Earning".
4. The **My Discount Saved** card should appear, and its amount must **not** be inside Total Earned.
5. Buy a subscription from a referred account → check the upline's virtual wallet and their Level 1/2/3 rows.
6. **Export CSV** → the Wallet column should read `Report only` for discount rows.
7. Open **Wallet → Wallet History** → no discount rows should appear there, and the balances should be unchanged.
