# Documentation: Export to Excel App (Bitrix24)

### Overview
This is a local Bitrix24 application used to generate **Quotes in Excel format (.xls)**.
The app works in two distinct modes:
1.  **From a Quote:** Generates the spreadsheet and attaches it to the Quote Timeline.
2.  **From a Deal:** Automatically creates a new Quote entity, generates the spreadsheet, attaches it to the Deal Timeline, and creates an **Email Activity**.

### 📂 Project Structure
**Server Path:** `/app/export_to_excel_app/` (relative to domain root)
**Base URL:** `https://app.appliedphysicsinc.com/export_to_excel_app/`

| File | Description |
| :--- | :--- |
| **`app.php`** | **Entry Point (Quotes).** Loaded inside the slider when clicked from a Quote. Triggers `handler.php`. |
| **`handler.php`** | **Logic (Quotes).** Generates Excel from Quote data and attaches it to the timeline. |
| **`app_deal_create_quote.php`** | **Entry Point (Deals).** Loaded inside the slider when clicked from a Deal. Triggers `handler_deal_create_quote.php`. |
| **`handler_deal_create_quote.php`** | **Logic (Deals).** Creates a new Quote entity, generates Excel, attaches it, and **creates an Email Draft**. |
| **`index.php`** | **Control Panel.** Used to manually register (bind) buttons in Bitrix24 (Quotes, Deals, Invoices). |
| **`upload/`** | Temporary storage for generated files. **Must have write permissions (755 or 777).** |
| **`images/`** | Static assets: logo (`Applied-Physics_transparent icon.png`) and signature (`signature.png`). |

---

### ⚙️ Critical Configuration
> **⚠️ IMPORTANT:** Settings are hardcoded directly into the PHP files. You must edit the code in **both** handler files if details change.

#### 1. Domain / Handler URL
If the domain changes, you must update the AJAX calls in the interface files.
* **Files:** `app.php` AND `app_deal_create_quote.php`
* **Action:** Update `$.post("https://app.appliedphysicsinc.com/...")` to the new URL.

#### 2. Company Address & Bank Info
Organization details and Wire Transfer info are manually written in the code.
* **Files:** `handler.php` AND `handler_deal_create_quote.php`
* **Action:** Search for "Applied Physics, Inc." or "JP Morgan" in the code to update values.

#### 3. Email Sender ID (Deals Only)
When generating from a Deal, the script creates an email. It requires a valid **Contact ID** to assign the email sender (e.g., `sales@...`).
* **File:** `handler_deal_create_quote.php`
* **Code:** `"ENTITY_ID" => 6830`
* **Action:** If the script fails to create an email, check the logs. If "Could not find CONTACT" appears, change `6830` to a valid Contact ID from your CRM that has the correct email address.

---

### 🚀 How it Works (Workflow Logic)

#### Scenario A: From a Quote
1.  User clicks **"Export to Excel"** inside a Quote.
2.  `app.php` triggers `handler.php`.
3.  Script generates the `.xls` file and attaches it to the **Quote Timeline** as a comment.

#### Scenario B: From a Deal
1.  User clicks **"Create Quote"** inside a Deal.
2.  `app_deal_create_quote.php` triggers `handler_deal_create_quote.php`.
3.  **Steps performed:**
    * Creates a new **Quote** entity in CRM (copies products/client from Deal).
    * Generates the `.xls` file.
    * Attaches the file to the **Deal Timeline**.
    * Creates an **Email Activity** (Draft/Sent) with the file attached and a pre-written message.

---

### 🛠 Troubleshooting

**Problem: Buttons missing in Deals.**
* **Fix:** Open `https://.../export_to_excel_app/index.php` in your browser.
* Look for the section **DEAL NEW BUTTONS**.
* Click **"Add app's Deal 'create quote' buttons"**.

**Problem: File generated, but no Email created (Deals).**
* **Check Logs:** Look at `/export_to_excel_app/deal export_quote.log`.
* **Common Error:** `Could not find 'CONTACT' with ID: ...`.
* **Fix:** Update the `ENTITY_ID` in `handler_deal_create_quote.php` to a valid Contact ID.

**Problem: Infinite loading / File not generated.**
* Check permissions for the `upload/` folder.
* Ensure the images (`logo`, `signature`) exist in the `images/` folder.