# Optivita POS bridge — instalare / telepítés

Program mic pentru PC-ul casei din magazin. Face un singur lucru: când casa
web (c.optivita.ro/pos) tipărește un bon, el scrie fișierul bonului în folderul
`cash-register-data`, de unde FiscalWire îl tipărește pe Datecs DP25 — exact
ce făcea programul vechi `DrLenkeiSzamlazo`. Nu are ferestre: stă ca o
iconiță lângă ceas.

Kis program az üzlet kasszagépére. Egyetlen dolga van: amikor a webes kassza
(c.optivita.ro/pos) blokkot nyomtat, beírja a blokk fájlját a
`cash-register-data` mappába, ahonnan a FiscalWire kinyomtatja a Datecs DP25-ön
— pontosan úgy, ahogy a régi `DrLenkeiSzamlazo` program tette. Nincs ablaka:
ikonként ül az óra mellett.

Descărcare / Letöltés: **https://dev.optivita.ro/bridge/**

---

## RO — Instalare (5 minute, o singură dată)

1. Descarcă `optivita-pos-bridge-<versiune>.zip` și dezarhivează-l.
2. Deschide folderul programului vechi: click dreapta pe scurtătura
   `DrLenkeiSzamlazo` de pe desktop → **Open file location** (Deschide locația
   fișierului). Acolo se văd `DrLenkeiSzamlazo.exe` și folderul
   `cash-register-data`.
3. Copiază `optivita-pos-bridge.exe` în **acel** folder, lângă
   `DrLenkeiSzamlazo.exe`. (Nu-l porni din Downloads: acolo nu este folderul
   casei de marcat.)
4. Dublu-click pe `optivita-pos-bridge.exe`.
   - Dacă Windows arată „Windows protected your PC": apasă **More info** →
     **Run anyway**.
   - Dacă întreabă de firewall: **Allow access**.
   - Dacă apare „not a valid Win32 application", PC-ul e pe 32 de biți:
     folosește `optivita-pos-bridge-32bit.exe` în loc.
5. Lângă ceas apare o iconiță **verde** = totul e în regulă (folderul a fost
   găsit). Iconiță **portocalie** = folderul nu a fost găsit (vezi mai jos).
6. Deschide casa web în Chrome/Edge: https://c.optivita.ro/pos. Sus, în
   antet, trebuie să scrie **nyomtató ✓** (nyomtató = imprimantă). Dacă
   Chrome întreabă dacă pagina poate accesa „devices on your local network",
   apasă **Allow**.
7. Gata. Programul pornește singur la fiecare pornire a Windows-ului.
   FiscalWire rămâne pornit ca până acum; programul vechi poate rămâne
   instalat, dar nu mai trebuie pornit.

### Iconița
- click dreapta → **Deschide casa (POS)**, **Deschide folderul bonurilor**,
  **Deschide jurnalul**, **Setări (fișier)**, **Pornire cu Windows** (bifat),
  **Caută actualizări**, **Ieșire**.
- Prima linie a meniului arată versiunea și folderul folosit.

### Dacă iconița e portocalie (folderul nu a fost găsit)
Programul caută singur `cash-register-data` (lângă el, lângă
`DrLenkeiSzamlazo.exe`, pe Desktop, în C:\ …). Dacă nu l-a găsit:
- în pagina casei → ⚙ → „Nyomtató (bridge)" → scrie calea completă a
  folderului (ex. `C:\DrLenkei\cash-register-data`) → **Mappa beállítása**; sau
- click dreapta pe iconiță → **Setări (fișier)** → completează `"dir"` și
  salvează, apoi **Ieșire** și pornește din nou programul.

### Probleme
- **nyomtató ✗** în pagina casei: programul nu rulează (iconița lipsește).
  Pornește-l din folderul programului vechi.
- Bonul nu iese, dar pagina spune „a kassza átvette": problema e la
  FiscalWire sau la casa de marcat (hârtie, cablu, FiscalWire oprit) — la fel
  ca pe vremea programului vechi.
- Jurnalul: click dreapta pe iconiță → **Deschide jurnalul**
  (`optivita-pos-bridge.log`, lângă program). Trimite-l lui Flaviu la nevoie.
- Actualizările se fac singure (verifică la pornire și la 6 ore, se
  instalează când casa stă câteva minute). Versiunea se vede în meniu.

### Dezinstalare
Click dreapta pe iconiță → debifează **Pornire cu Windows** → **Ieșire** →
șterge `optivita-pos-bridge.exe` (și `optivita-pos-bridge.json`, `.log`).

---

## HU — Telepítés (5 perc, egyszer)

1. Töltsd le az `optivita-pos-bridge-<verzió>.zip` fájlt és csomagold ki.
2. Nyisd meg a régi program mappáját: jobb klikk az asztali
   `DrLenkeiSzamlazo` ikonra → **Open file location** (Fájl helyének
   megnyitása). Ott van a `DrLenkeiSzamlazo.exe` és a `cash-register-data`
   mappa.
3. Másold az `optivita-pos-bridge.exe`-t **abba** a mappába, a
   `DrLenkeiSzamlazo.exe` mellé. (Ne a Downloads mappából indítsd: ott nincs
   a pénztárgép mappája.)
4. Dupla klikk az `optivita-pos-bridge.exe`-re.
   - Ha a Windows azt írja „Windows protected your PC": **More info** →
     **Run anyway**.
   - Ha tűzfalat kérdez: **Allow access**.
   - Ha „not a valid Win32 application" jelenik meg, a gép 32 bites:
     használd az `optivita-pos-bridge-32bit.exe`-t.
5. Az óra mellett megjelenik egy **zöld** ikon = minden rendben (a mappa
   megvan). **Narancs** ikon = a mappa nem található (lásd lent).
6. Nyisd meg a webes kasszát Chrome/Edge-ben: https://c.optivita.ro/pos. Fent
   a fejlécben **nyomtató ✓** kell legyen. Ha a Chrome megkérdezi, hogy az
   oldal elérheti-e a „devices on your local network"-öt: **Allow**.
7. Kész. A program minden Windows-indításkor magától elindul. A FiscalWire
   marad, mint eddig; a régi program maradhat telepítve, de már nem kell
   elindítani.

### Az ikon
- jobb klikk → **Kassza megnyitása**, **Blokk mappa**, **Napló**,
  **Beállítások**, **Indítás a Windows-zal** (pipálva), **Frissítés
  keresése**, **Kilépés**.
- A menü első sora a verziót és a használt mappát mutatja.

### Ha az ikon narancs (a mappa nem található)
A program magától keresi a `cash-register-data` mappát (maga mellett, a
`DrLenkeiSzamlazo.exe` mellett, az Asztalon, a C:\ meghajtón…). Ha nem
találta:
- a kassza oldalon → ⚙ → „Nyomtató (bridge)" → írd be a mappa teljes
  útvonalát (pl. `C:\DrLenkei\cash-register-data`) → **Mappa beállítása**; vagy
- jobb klikk az ikonra → **Beállítások** → töltsd ki a `"dir"` sort, mentsd,
  majd **Kilépés** és indítsd újra a programot.

### Hibák
- **nyomtató ✗** a kassza oldalon: a program nem fut (nincs ikon). Indítsd
  el a régi program mappájából.
- Nem jön ki a blokk, de az oldal azt írja „a kassza átvette": a hiba a
  FiscalWire-nél vagy a pénztárgépnél van (papír, kábel, FiscalWire leállt)
  — ugyanúgy, mint a régi programnál.
- Napló: jobb klikk az ikonra → **Napló** (`optivita-pos-bridge.log`, a
  program mellett). Szükség esetén küldd el Flaviunak.
- A frissítés automatikus (induláskor és 6 óránként ellenőriz, akkor
  telepíti, amikor a kassza pár percig áll). A verzió a menüben látszik.

### Eltávolítás
Jobb klikk az ikonra → vedd ki a pipát **Indítás a Windows-zal** → **Kilépés**
→ töröld az `optivita-pos-bridge.exe`-t (és a `.json`, `.log` fájlokat).

---

## Technical notes (for the developer)

- Listens on `http://127.0.0.1:7788` (config `port`). Endpoints: `GET /health`,
  `GET /version`, `POST /print {lines:[…], id?}` (writes `<id>.txt` atomically:
  `<id>.tmp` + rename; UTF-8 no BOM; CRLF; refuses to print when the folder
  does not exist; `duplicate:true` when `<id>.txt` was written in the last 15
  minutes and is still in the folder or in `executate/` — an older file with
  the same random id is another receipt), `GET /status/<id>?wait=ms` (pending /
  executed / `raspuns/<id>.txt`), `GET|POST /config` (`{dir}` and / or
  `{cardTerminal}`), `GET|POST /update/check`.
- From 1.1.0 — FiscalWire's answer and the bank terminal: `/status/<id>` also
  returns `nrb` (text of FiscalWire's `.nrb` answer file), `result`
  (`state: ok | canceled | unknown`, `z`, `nr`, `bf`, `payments[]` with the
  payment code, amount and the terminal's `authCode` / `rrn` / masked card
  number; the card holder's name is dropped), `errors[]` (lines FiscalWire
  marked `Er`: the receipt was not printed) and `answered`. `&answer=1` makes
  `wait` last until FiscalWire has answered; `&since=<unix ms>` (when the page
  printed) ignores answer files older than that. The `.nrb` is looked up in
  `raspuns/` by exact name first, then by any file name containing the id (the
  name pattern is a FiscalWire setting). `cardTerminal` in `/health` and in the
  config (`card_terminal`) says that FiscalWire on this PC drives the bank
  terminal ("POS Bancar" gateway): the POS page then books a card receipt only
  after `result.state == "ok"`. Design: `docs/08-plata-card-pos.md`.
- `sim/fiscalwire-sim.mjs` (Node, local tests only) stands in for FiscalWire +
  the bank terminal: it consumes the command files, shows card payments on
  `http://127.0.0.1:7789` with buttons (approved / declined / cash / partial /
  cancelled / file refused) and writes the answer files.
- CORS: `https://c.optivita.ro`, `https://c-optivita.me-fe6.workers.dev`,
  `http://localhost:*`, `http://127.0.0.1:*`, plus `origins` from the config;
  `file://` pages (test.html) only with `-dev`. Answers Chrome's
  Private Network Access preflight.
- Config `optivita-pos-bridge.json` and log `optivita-pos-bridge.log` (1 MB,
  rotated ×2) next to the exe, or in `%LOCALAPPDATA%\optivita-pos-bridge` when
  the exe folder is read-only.
- Folder detection order: `cash-register-data` next to the exe →
  `config.ini` (`CASH_REGISTER_PATH`) of a `DrLenkeiSzamlazo.exe` next to the
  exe → bounded search (Desktop, Downloads, Documents, profile, C:\, Program
  Files, D:\, E:\; depth 6, 40 s) for `DrLenkeiSzamlazo.exe` / a
  `cash-register-data` folder, most recently used wins. The result is saved in
  the config; delete `"dir"` to re-detect.
- Autostart: `HKCU\Software\Microsoft\Windows\CurrentVersion\Run\OptivitaPosBridge`
  (no admin rights); `-no-autostart` or the tray checkbox turns it off. Where Windows
  refuses the registry write ("Access is denied"), the bridge writes
  `Optivita POS bridge.cmd` into the user's Startup folder instead (`shell:startup`;
  a shortcut made there by hand is left alone). `/health` reports the way in place
  (`autostart`: `registry`, `startup-folder` or empty) and the POS page warns when
  there is none.
- Self-update: `latest.json` at `update_url` (default
  `https://dev.optivita.ro/bridge/latest.json`), checked 30 s after start and
  every 6 h; sha256-verified download to `<exe>.new`, swapped in (`<exe>.old`)
  and restarted once no receipt was printed for 3 min.
- Flags: `-dir`, `-port`, `-config`, `-dev`, `-no-tray`, `-no-autostart`, `-autostart`
  (started at login: no message when a bridge already runs), `-version`.
- Build: `build.sh` on the server (see the comment in it); `build-mac.sh`
  builds a Mac binary for `-no-tray -dev` tests of the HTTP logic.
