# 🧾 Scrap Module - Change Log

All notable changes to the **Scrap Management** custom module are documented here.  
This changelog follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)  
and adheres to [Semantic Versioning](https://semver.org/).

---

## [2.1.0] – 2025-10-22  
**Dolibarr Compatibility:** v17.x – v22.x  
**Author:** Marcin Flaumenhaft  
**Maintainer:** NÉOS Solutions Limited  

---

### ✨ Added
- **Search Functionality**
  - Added search box beside “Add Scrap” button.
  - Fully styled to match Dolibarr theme.
  - Includes “Reset Filter” for clearing search.

- **Pagination**
  - Displays only the last 20 scrap entries.
  - Pagination for older records using Dolibarr native style.

- **Total Cost Summary**
  - Added automatic total cost calculation under scrap list.
  - Synchronized with search and pagination.

- **Enhanced Deletion Confirmation**
  - Custom overlay confirmation box (Dolibarr-styled).
  - Displays full scrap details before deletion:
    - Product reference
    - Warehouse
    - Quantity
    - Cost
    - Reason
    - Date
    - Added by (initials)

- **Responsive Scrap Report Dashboard**
  - Added mobile-friendly layout using `scrap_report_responsive.css`.
  - Fully responsive for tablets and phones.
  - Adjusts filters, charts, and summary positions automatically.

- **PDF Report Enhancements**
  - Monthly Summary shows all 12 months and final total row.
  - Header and table colors unified with NEOS blue (#003399).
  - Added initials after date column in Detailed Scrap List.
  - Improved spacing, alignment, and typography consistency.

- **Visual Improvements**
  - Color scheme unified with company branding.
  - Adjusted header and separator line thickness.
  - Fixed overlapping header sections and centered report title.

---

### 🧩 Modified
- **`scrap_index.php`**
  - Added CSRF token validation for add/delete actions.
  - Integrated stock restoration using `MouvementStock::reception()`.
  - Added detailed log description on stock movement:  
    `ScrapDeletedAndStockRestored (ProductRef - Label - Qty - Cost - Reason)`
  - Fixed cost handling (total instead of unit).
  - Display quantity as integer.
  - Added “Added By” initials column.
  - Enhanced user feedback messages with `$langs->trans()` translations.
  - Polished layout and alignment.

- **`scrap_new.php`**
  - Displays detailed success message after creation.
  - Automatically reduces physical stock on new scrap.
  - Improved input validation for numeric and date fields.
  - Records precise timestamp using `dol_now()`.

- **`scrap.class.php`**
  - Confirmed `$this->db` transaction handling.
  - Fixed physical stock synchronization on create/delete.
  - Improved movement description and logging.
  - Cleaned up error handling and message propagation.

- **`scrap_report.php`**
  - Maintains original design (filters, charts, tables).
  - Added full responsive behavior for mobile.
  - Centered “Found Scrap Records” text.
  - Adjusted chart sizes and container proportions.
  - Auto year filter and responsive chart resizing.

- **`scrap_report.pdf.php`**
  - Enhanced Monthly Summary layout to mirror Detailed Scrap List.
  - Header blue color matched logo tone (#003399).
  - Improved alignment, spacing, and readability.
  - Added logo auto-scaling and repositioned company box.
  - Added “General Scrap Record” title in header.
  - Detailed Scrap List now includes user initials and correct date formatting.

---

### 🧱 Database Schema
*(Unchanged since 2.0.0)*

```sql
CREATE TABLE IF NOT EXISTS llx_scrap (
  rowid INT AUTO_INCREMENT PRIMARY KEY,
  fk_product INT NOT NULL,
  fk_warehouse INT NOT NULL,
  qty DOUBLE NOT NULL,
  cost DOUBLE NOT NULL DEFAULT 0,
  reason VARCHAR(255),
  scrap_date DATETIME NOT NULL,
  fk_user_scrap INT NULL,
  entity INT DEFAULT 1,
  INDEX idx_scrap_entity (entity),
  INDEX idx_scrap_product (fk_product),
  INDEX idx_scrap_warehouse (fk_warehouse),
  CONSTRAINT fk_scrap_product FOREIGN KEY (fk_product) REFERENCES llx_product(rowid),
  CONSTRAINT fk_scrap_warehouse FOREIGN KEY (fk_warehouse) REFERENCES llx_entrepot(rowid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```


### 💄 Visual / UX Improvements
  - All buttons styled consistently (**Add Scrap**, **Search**, **Generate PDF**).
  - Centered summaries and charts.
  - Improved spacing between logo and company header in PDF.
  - Unified color palette: **NEOS Blue (#003399)** with gray accents.
  - Tables horizontally and vertically aligned with headers.
  - Optimized for Dolibarr dark/light theme compatibility.

---

### 🔒 Security Enhancements
  - Implemented full CSRF protection (`$_SESSION['newtoken']`).
  - Sanitized all form inputs using `GETPOST()` filters.
  - Added validation and protection against invalid direct access.
  - Confirmed Dolibarr access rights check for write actions.

---

### ✅ Testing
  **Environment:**
  - Dolibarr v18.0.3, v22.x  
  - PHP 8.2, MySQL 8.0  

  **Verified:**
  - Scrap creation with correct stock deduction.  
  - Scrap deletion with restored stock.  
  - Accurate totals in reports.  
  - Responsive layout and chart scaling.  
  - PDF layout and header alignment.  
  - CSRF tokens validated.  

---

### 🔮 Next Planned Features
  - CSV and XLSX export for scrap reports.  
  - User-based performance and scrap statistics.  
  - Multi-warehouse and product-category filters.  
  - Notification system for scrap thresholds.  
  - Integration with manufacturing/BOM tracking.  

---

  **Maintainer:** Marcin Flaumenhaft  
  **Location:** `/htdocs/custom/scrap/`  
  **Last Updated:** 2025-10-22
