# Document Manager Application

A document processing application that allows users to scan/upload images, save them with metadata, and retrieve them easily.

## Features

- Scan or upload document images
- Save documents with metadata:
  - Document name
  - Control number (auto-generated unique ID)
  - Category
  - Date
- Retrieve documents by:
  - Control number
  - Category
- List documents by:
  - Date range
  - Document name
- Print retrieved documents

## Installation

- **Python**: Python 3.9+ recommended
- **Clone** this repository
- **Install Python dependencies**:

### Additional system requirements (for new features)

- **Tesseract OCR (for OCR features)**
  - Required if you want to use OCR via `pytesseract`.
  - Install Windows binaries (64-bit):
    - Official releases: https://github.com/tesseract-ocr/tesseract
    - Common installer (Windows): https://github.com/UB-Mannheim/tesseract/wiki
  - Default install path: `C:\\Program Files\\Tesseract-OCR\\tesseract.exe`
  - Configure in app: open `Settings` and set the Tesseract executable path, or add it to your system `PATH`.
  - Note: Even when using the packaged EXE, Tesseract must be installed separately on the machine.

- **Microsoft Visual C++ Redistributable**
  - Some wheels (e.g., `cryptography`) may require the latest MSVC runtime.
  - Download: https://aka.ms/vs/17/release/vc_redist.x64.exe

- **Email/Network**
  - Email verification uses the `requests` stack (`requests`, `urllib3`, `certifi`). Ensure outbound HTTPS is allowed by your firewall/proxy.
## Usage

Run the application:

```
python main.py
```

Or run as a packaged EXE (if provided):

```
dist_v2/OptimalDocManager_package/OptimalDocManager/OptimalDocManager.exe
```

If the local web interface doesn't load from the EXE, use the Help menu item **Open Web Server Log** to view logs at:
`%LOCALAPPDATA%\OptimalDocManager\logs\web_server.log`

### Features and their requirements

- **OCR (Convert images to text)**
  - Requires: Tesseract OCR installed locally.
  - Python side uses: `pytesseract` (already bundled in source/EXE).

- **Email verification / network calls**
  - Requires: Internet access (HTTPS) and time sync.
  - Python side uses: `requests`, `urllib3`, `certifi`, `idna`, `charset_normalizer` (bundled).

- **PDF viewing/processing**
  - Uses: `PyMuPDF` (MuPDF) and `PyPDF2` (bundled). No extra system installs required.

- **Local web interface**
  - Runs on localhost (default 8080). If blocked, try 9090 or adjust firewall rules.
  - Assets (`templates/`, `static/`) are bundled with the EXE and resolved automatically.

### Scanning/Adding Documents

1. Go to the "Scan Documents" tab
2. Click "Scan Document" or "Browse..." to select an image file
3. Fill in document metadata (name, category, date)
4. Click "Save Document"

### Viewing Documents

1. Go to the "View Documents" tab
2. Search for documents by control number or category
3. Double-click on a document in the results table to view it
4. Use the "Print Document" button to print the selected document

### Searching Documents

1. Go to the "Search Documents" tab
2. Use the "Search by Name" tab to find documents by name
3. Use the "Search by Date Range" tab to find documents within a specific date period
4. Double-click on a document in the results table to view it
5. Use the "Print Document" button to print the selected document

## File Structure

- `main.py` - Main application entry point
- `document_scanner.py` - Document scanning and metadata entry
- `document_viewer.py` - Document viewing and retrieval by control number/category
- `document_search.py` - Search functionality by name and date range
- `document_db.py` - Database handling for document storage and retrieval
- `database/` - Directory for the SQLite database
- `images/` - Directory for stored document images
- `templates/` - Packaged HTML templates for the local web interface
- `static/` - Packaged JS/CSS/assets for the local web interface

## Local Web Interface

- Access via menu: Help → Open Web Interface, or visit `http://127.0.0.1:8080`.
- The app shows a status indicator:
  - Top strip: red/green dot + text
  - Window title suffix: `| Web: ON (port)` or `| Web: OFF`
- The packaged EXE includes `templates/` and `static/` automatically; if the server fails to start, see the server log.
