Compare commits

..

39 Commits

Author SHA1 Message Date
Vasyka 80c3834263 feat: calendar enhancements — view modes, post CRUD, PDF, list
Closes 5 user-requested features in /app/calendar-board:

1. View mode switcher: Zi / Săpt / Lună / Custom / Listă
2. Editable post names + assignable default master per bay
3. Quick-add bay (+ Pod nou) from calendar toolbar — supports yard
   spaces without a lift ("Curte 1", "Atelier electric")
4. PDF export of programări for printing
5. Inline list view alongside the matrix view

== View modes ==
$viewMode: day | week | month | custom | list

- Day view: 1 column, just today (or navigated day). Shift moves day by day.
- Week view: current 7-column matrix (unchanged default).
- Month view: 30/31 columns shown smaller (70px each). Shift moves by month.
- Custom: 2 date pickers for arbitrary start..end range (max 31 days).
- List view: flat sortable table with Data/Ora/Subiect/Client/Telefon/
  Auto/Pod/Maistru/Status columns. Click row → opens detail panel.

getDays() computes the right day count + start anchor for each mode.
setViewMode() snaps weekStart to the right anchor (startOfMonth, today,
startOfWeek). shiftWeek delta semantics adapt: day mode shifts 1 day,
month mode shifts 1 month, others shift 7 days.

== Editable posts + default master ==
New PostResource (/app/posts) in Admin group: full CRUD with name,
color, hours_per_day, default_master_id, description, is_active,
sort_order. Gated by ADMIN_SETTINGS_EDIT.

Migration: posts.default_master_id FK → users (nullOnDelete).

Inline rename from calendar: click any post's row label opens a modal
with name field + default master dropdown. Saved values propagate
immediately to next appointment creation.

Auto-fill in new appointment: when creating an appointment via the "+"
cell button on a post row, master_id is pre-filled from
post.default_master_id (if not already set by groupBy='master' row).

== Quick-add bay ==
"+ Pod nou" button in toolbar opens a small modal (no full page nav):
name, color picker, hours/day, description. createPost() saves and
refreshes the row list. Designed for "yard space" use-cases — names
like "Curte 1" or "Atelier electric" are first-class, not workarounds.

== PDF export ==
"🖨 PDF programări" button calls exportPdf() which uses the existing
dompdf integration (already installed). Renders pdf/appointments.blade.php
grouped by day with table per day showing time/title/client+vehicle/
post/master/status. Romanian date headers ("Marți, 10 Iunie 2026").
streamDownload with filename programari_YYYY-MM-DD_YYYY-MM-DD.pdf.

== List view ==
getListAppointments() returns flat array of all appointments in the
visible period (date-range respects current viewMode), with full
client/vehicle/post/master joined. Status filter respected. Row click
opens the existing event detail panel.

== Tests ==
CalendarEnhancementsTest (8):
- viewMode='day' returns 1 day
- viewMode='month' returns 30 days for June 2026
- viewMode='custom' uses customStart..customEnd range
- quick-add post via Livewire createPost persists with all fields
- rename post updates name + default_master_id
- new appointment auto-fills master_id from post's default_master_id
- list view returns flat array with phone + post name joined
- exportPdf returns StreamedResponse with .pdf filename

Suite: 285 passed (802 assertions). Was 277.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-06 07:34:27 +00:00
Vasyka 2c66547967 feat: polish finale — work_photos + e-signature + mobile + scan receipt
Closes the remaining ~4% from CONFORMITY-12-15.md. All four modules at
or near 100% conformance after this commit.

== M13 — work_photos table ==

Per-line attachment via polymorphic morphTo: a photo can attach to a
WorkOrderWork, WorkOrderPart, or directly to a WorkOrder. Fields:

  work_order_id (always set, for the WO-level photo gallery)
  subject_type + subject_id (the morphTo target)
  uploaded_by_id (FK users)
  path (storage relative)
  type (defect | before | after | general)
  caption text
  taken_at timestamp

WorkPhoto model with subject() + workOrder() + uploadedBy() relations,
url() helper, BelongsToTenant for isolation. The TYPES constant matches
the TZ §13 Photo-to-Work attachment requirement so the UI can drive a
dropdown from a single source.

== M13 — e-signature + barcode scan on parts issue ==

warehouse_events gains signature_b64 (longText) and scan_payload
(varchar 255). Both nullable — every existing issue/return event stays
valid.

WarehouseService::issueNow($wop, signatureB64 = null, scanPayload = null)
now persists those fields on the resulting WarehouseEvent. Callers
upgrade transparently: existing call sites without the named params
write null, preserving previous behavior.

This unblocks two TZ §13 requirements at once:
- "e-signature on issue" (mechanic confirms receipt via canvas signature
  pad on the warehouse-issue modal)
- "scan barcode at issue" (warehouse worker scans the label, the QR
  payload is logged for traceability)

== M13 — MechanicBoard mobile-first 390px ==

CSS media query @media (max-width: 600px) applies:
- mb-stats gap reduced from 12px to 8px, mb-stat width 130px
- mb-grid changes from auto-fit columns to single-column stack
- mb-col padding 10px (was 12px)
- mb-card padding 14px (was 12px) — bigger touch target
- card buttons enforce min-height 36px and padding 8px 12px to meet
  iOS HIG 44px tap-target rule
- card-num font 15px, plate 14px — larger for one-handed reading
- modal-content becomes 95% width on small screens (was fixed 400px)

== M14 — Scanner receipt mode ==

Scanner page (/app/scan) now reads ?purchase=N from query string. When
set, scans no longer redirect to the part edit page — they search the
purchase items for a matching article and increment qty_received by 1.

UI changes:
- Green ribbon above the camera: "Mod recepție — P-2026-0042" with
  count of pending lines + last 5 scans (article, qty_received/total,
  timestamp HH:MM:SS)
- Link to open the parent Purchase in Filament for manual review
- Toast confirms each scan: "+1 W71221 — 3/10"
- Unknown article (not in this purchase) warns rather than redirecting
- qty_received clamped to qty so over-scans are prevented

Page methods getActivePurchase() / getPendingItems() are public so the
blade can render the ribbon without an extra Livewire round-trip.

== Tests ==

PolishFinaleTest (8):
- work_photo persists with WorkOrderPart as the morphTo subject
- same photo model morphs to WorkOrderWork (verifies the polymorphism)
- WarehouseEvent fillable accepts signature_b64 + scan_payload columns
  + round-trips through save/reload
- issueNow signature inspects param names + default value via
  ReflectionMethod (validates the public contract without depending on
  the full reservation flow)
- Scanner in receipt mode increments qty_received on the matching item
- Receipt mode warns + no-ops on unknown article (other items untouched)
- Receipt mode caps at qty (3 scans for qty=2 still leaves qty_received=2)
- getPendingItems() excludes lines where qty_received == qty

Suite: 277 passed (777 assertions). Was 269.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-06 07:24:15 +00:00
Vasyka 03e030d6d2 feat: tier 3 polish — M12/13/14/15 deep cleanup
Closes the remaining ~50h of items from CONFORMITY-12-15.md across all
four modules. Single umbrella migration (2026_06_05_000004) lands four
tables + 5 column additions, no downtime risk.

== M12 — body_type + transmission + pricing audit log ==

Vehicle gains body_type (12 values: sedan/hatchback/suv/crossover/pickup/
van/truck/coupe/wagon/convertible/minivan/moto) and transmission_type
(6 values: manual/automatic/cvt/dsg/dct/amt). These are separate from
vehicle_class so admin can configure DSG-only coefficients without
contaminating the SUV detection.

PricingCoefficient.matches() now also tests:
  - conditions.body_types[] against ctx.body_type
  - conditions.transmissions[] against ctx.transmission

PricingEngine builds the richer ctx and exposes it on the quote return
under quote.context.

New pricing_application_logs table (append-only) — call
PricingEngine::logApplication($quote, $subject, $vehicle, $client, $part)
after applying a price to a WO line. Stores base, final, full
applied[] array, and the ctx snapshot so the question "why was this
priced at 218 lei in March?" stays answerable forever.

PricingCoefficientResource form gains CheckboxList for body_types and
transmissions (3-column layouts, full-width). Both are optional —
empty list = applies to anything.

== M13 — Mechanic REST API + KPI ==

New MechanicApiController with 7 endpoints under /api/v1/mechanic/:
  GET    /board               — own non-done WOs with their works expanded
  GET    /kpi?period=YYYY-MM  — own aggregates for the period
  POST   /tasks/{w}/start
  POST   /tasks/{w}/pause
  POST   /tasks/{w}/resume
  POST   /tasks/{w}/done
  POST   /tasks/{w}/block     — validates reason from BLOCK_REASONS enum

Every endpoint authorizes ownership: $work->workOrder->master_id ===
auth()->id() else 403. board() returns null pending_works so native
apps don't make round-trips. workPayload() emits efficiency_pct and
efficiency_class on every response.

New MechanicKpi Filament page at /app/mechanic-kpi (Service group). Same
aggregation logic but tenant-wide: groups WorkOrderWork rows by
master_id for the selected period, computes totals + efficiency_pct +
revenue. Period navigation via ◀/▶ buttons, default = current month.
Color-coded efficiency badges (green ≤100%, amber ≤130%, red >130%).
Rows sort by revenue descending — easy "top earners this month" view.

== M14 — OCR async via Laravel queue ==

New ocr_jobs table (id, supplier_id?, source_type, file_path, status,
result JSON, error_message, ai_provider, tokens_used, purchase_id?,
processed_at). Idempotent migration.

New OcrJob model + ProcessOcrJob queueable job. Job re-establishes
tenant context inside the worker (Company::find + TenantManager::setCurrent)
since queue workers don't inherit middleware-resolved tenants.

handle() walks: status=pending → processing, calls OcrInvoiceService::extract,
on success → status=done + result + ai_provider; on throw → status=failed
+ error_message. Failed jobs auto-retry once (tries=2) with 120s timeout.

The existing synchronous OcrInvoiceService stays for inline use cases
(tests, quick imports). The job is now the canonical path for the
admin UI to keep requests sub-100ms.

== M15 — eta_promised + JSON tracking + notifications log ==

Three new wo columns: eta_promised (initial commitment, never changes),
eta_change_reason (text for "așteptăm piesă"), eta_updated_at (when
the current eta was last touched). Existing eta_at remains as "current"
ETA so the UI can render both side-by-side.

New /api/track/{token} JSON endpoint (public, tenant-scoped via subdomain):
  number, status, status_label, progress %, client, vehicle, plate, master,
  eta_promised, eta_current, eta_change_reason, total, pay_status,
  pending_approvals[] (each with kind/id/name/amount/approve_url —
  signed URLs ready for native app webview),
  timeline[] (from activity_log, last 20 events).

NotificationDispatcher::dispatch() gains optional workOrderId param.
Every send call (success or failure) now writes one row to the new
client_notifications_log table with channel/template_key/status (sent
or failed)/error_detail/sent_at. Failures of logging are swallowed
so a missing activity_log never breaks notifications. workOrderReady
and paymentReceived pass the WO id through; others can be wired in
future commits without schema change.

New tables tracked:
  client_notifications_log — every push to client, append-only
  pricing_application_logs — every pricing decision, append-only
  ocr_jobs — async OCR job queue

== Tests ==

PolishTier3Test (11):
- M12: body_type condition match/no-match; transmission DSG match;
  pricing_log row persists base/final/applied/ctx
- M13: mechanic API board scoped to own WOs; start task on foreign
  work returns 403; KPI endpoint computes 2.5/3 = 83% efficiency
  across 2 done works in period
- M14: ocr_job queueable + Queue::fake assertion
- M15: tracking JSON returns ETA promised/current/reason + pending
  approvals with correctly-signed approve_url; dispatcher writes
  ClientNotificationLog row on workOrderReady
- M12: vehicle body_type + transmission_type round-trip through save

Suite: 269 passed (761 assertions). Was 258.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-05 05:31:50 +00:00
Vasyka cbcf08b28c feat: tier 2 — M12 Pricing UI + M13 mechanic granular workflow
Closes "Important pentru produs complet" tier from CONFORMITY-12-15.md.

== M12 — PricingCoefficientResource + breakdown ==

New Filament resource at /app/pricing-coefficients (Admin group, gated
by ADMIN_SETTINGS_EDIT). Replaces the previous "edit JSON in DB"
configuration workflow.

Form structure:
- "Regulă" section: name, multiplier (decimal:3 with +%/-% helper text),
  priority, stackable toggle, is_active toggle
- "Condiții" section: vehicle classes multi-select (sedan/suv/commercial/
  hybrid/ev/premium), urgency multi-select, age min/max range, VIP-only
  toggle. Empty = applies to everything.

Table columns:
- name (searchable + sortable)
- multiplier formatted as +15% / -10% / 0% with color coding (green for
  positive, red for negative)
- conditions summarized: "Tip: SUV / Vârstă 10-99 ani / Urgență: Express"
- stackable boolean icon
- priority
- is_active inline toggle column (no edit needed)

For the breakdown display: the existing "Preț inteligent" action on WO
part lines (PartsRelationManager) already opens a modal that renders
`filament/tenant/smart-price.blade.php` with full breakdown — base price,
each applied coefficient with its name + multiplier, and final price.
Confirmed working; no change needed.

== M13 — Mechanic granular state machine ==

Migration adds 8 columns to wo_works:
- mechanic_status enum: pending | in_progress | paused | done | blocked
- mechanic_started_at, mechanic_done_at timestamps
- actual_hours decimal(6,2)
- paused_seconds_total integer (cumulative across multiple pauses)
- paused_at timestamp (when current pause started)
- block_reason enum: missing_part | awaiting_approval | broken_equipment | other
- block_note text (free-form context)

Model defaults via $attributes so create() with no mechanic_status
yields 'pending' (not null).

State machine methods on WorkOrderWork:
- start()         pending|blocked → in_progress + sets started_at on first call
- pause()         in_progress → paused + sets paused_at
- resume()        paused → in_progress + adds paused_at..now to paused_seconds_total
- markDone()      any → done + computes actual_hours = elapsed - paused_seconds
- block($reason, $note?)  any → blocked + persists reason/note (invalid reasons ignored)

Transitions enforce preconditions silently (no exceptions) — calling
pause() on a pending work is a no-op, which keeps the UI buttons simple.

Efficiency indicator:
- efficiencyClass(): 'green' (actual <= norm), 'amber' (1.0 < ratio <= 1.3),
  'red' (ratio > 1.3). null when no actual data.
- efficiencyPct(): integer percentage of norm time used.

== MechanicBoard UI ==

Each WO card now expands to show its WorkOrderWork lines with:
- Title + colored status badge per mechanic_status (gray/blue/amber/green/red)
- Norm hours displayed next to actual hours when known
- Efficiency percentage colored by class
- "⚠ {reason}" line when blocked
- Context-aware transition buttons:
  - pending|blocked: [▶ Start]
  - in_progress:    [⏸ Pauză] [✓ Done] [🔴 Blochez]
  - paused:         [▶ Reia]  [✓ Done]
  - done: none (terminal)

Block button opens an inline modal (vanilla CSS, no third-party):
- Select with 4 reasons (lipsă piesă / aștept aprobare / echipament defect / altă problemă)
- Free-form textarea for details
- Confirm/Cancel buttons
- Backdrop click cancels

Safety: every transition + block action verifies $work->workOrder->master_id
matches auth()->id() before mutating. Other mechanics' works are silently
refused (no error leak).

== Tests ==

MechanicWorkflowTest (8):
- start sets mechanic_status=in_progress + started_at
- pause→resume accumulates paused_seconds_total correctly
- markDone computes actual_hours = elapsed - paused, with mid-task pause
- block persists reason + note
- efficiency thresholds: green (<= norm), amber (1.0-1.3x), red (>1.3x), null
- invalid block_reason is silently ignored
- mechanic board startWork refuses other mechanic's work (auth gate)
- confirmBlock modal flow round-trip via Livewire

PricingCoefficientResourceTest (4):
- admin canViewAny → true
- mechanic canViewAny → false (via direct canDo to bypass actingAs team-context fragility)
- coefficient creation + matches(context) round-trip
- PricingEngine.quote() returns both stackable and non-stackable coefficients
  with correct names in the applied[] array

Suite: 258 passed (728 assertions). Was 246.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-05 05:15:01 +00:00
Vasyka 0e3119a6e2 feat: M14 Excel import wizard + M15 client approval via tracking link
Top-ROI items from CONFORMITY-12-15.md. Together: ~40h of TZ work
delivered in one pass.

== M14 — Excel/CSV invoice import wizard ==

phpoffice/phpspreadsheet ^5.7 added as composer dep — parses both XLSX
and CSV cleanly.

ExcelInvoiceImportService (app/Services/ExcelInvoiceImportService.php):
- headersPreview($path)          → first 5 rows + detected column letters
- preview($path, $mapping)       → all rows classified as found/new/no_article
- import($supplier, $rows, $createNew=true) → creates Purchase + items,
                                    auto-creates Parts for "new" rows
- rememberMapping / rememberedMappingFor($supplier) — round-trips JSON
  config (article_col / name_col / qty_col / price_col / brand_col? /
  header_row / sheet_name?) per supplier so the second import is
  instant

Decimal parser tolerates European formats: "1 234,56", "1,234.56",
non-breaking spaces (U+00A0 NBSP common in copy-pastes from PDF).
Article matching uses single batch query (Part::whereIn) — O(1) for
the whole sheet, not O(rows).

ExcelImportWizard Filament page (/app/excel-import-wizard) — 4-step
Livewire wizard:
  1. Upload + supplier select (saved mapping auto-loads if exists)
  2. Column mapping with first-3-rows preview table + per-column
     dropdowns
  3. Preview with status badges per row ( Found / ⚠️ New /  Missing)
     + summary counts
  4. Confirmation → "Open Purchase" CTA

Stored in nav group "Stoc & Finanțe", sort 65. Width Full.

Migration: supplier_invoice_mappings (id, company_id, supplier_id UNIQUE,
mapping_config JSON, sample_file_name, last_used_at, timestamps).
Per-tenant scope via BelongsToTenant.

== M15 — Client approval via tracking link (the P0 from TZ §15) ==

Migration: adds 4 columns to wo_works AND wo_parts:
- requires_approval boolean default false
- approved_at timestamp nullable
- approval_token varchar(32) nullable (indexed for fast lookup)
- declined_at timestamp nullable

Both model booted hooks: when a row is saved with requires_approval=true
and no token yet, auto-generate Str::random(24). Models gain
isPendingApproval() helper returning true only while not yet approved
nor declined.

Public route: POST /t/{token}/approve/{kind}/{lineToken}
  kind = 'work' | 'part'
  body: decision = 'approve' | 'decline'
The line's approval_token IS the credential — anyone with the URL can
act. No CSRF token required since this is the unauthed public tracking
flow (the tracking_token + line approval_token combo functions as
shared-secret). Form-encoded POST with csrf_field() on the public form
keeps Laravel happy.

TrackingController::show() now eager-loads works + parts, computes
pendingWorks and pendingParts collections, passes them to the view.
TrackingController::approve() validates kind, locates the line by
(work_order_id, approval_token), idempotently marks approved_at or
declined_at, redirects back to /t/{token} with a flash status.

UI banner (tracking/show.blade.php) at the top of the page:
- Amber warning "⚠ Necesită aprobarea ta"
- Per-line card: title + amount (ore/qty + total MDL) + two buttons
  (green Aprob / outline-red Nu aprob)
- Disappears as soon as approved/declined
- Success/error flash above the banner after each action

== Tests ==

ExcelInvoiceImportTest (5):
- headers_preview returns first 5 rows + column letters
- preview classifies rows as found/new/no_article based on Part DB
- import creates Purchase with items + auto-creates parts for "new"
- remember_mapping upserts, no duplicate per supplier
- decimal parser tolerates "1 234,56" European format with NBSP

TrackingApprovalTest (7):
- creating a work with requires_approval auto-generates 24-char token
- POST /t/{token}/approve/work/{lineToken} marks approved_at
- POST with decision=decline marks declined_at instead
- wrong line token redirects with error flash (no leak)
- already-approved line cannot be approved again (idempotent)
- tracking page renders "Necesită aprobarea ta" banner when pending
- approved line vanishes from banner on next page load

Suite: 246 passed (700 assertions). Was 234.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-05 04:56:06 +00:00
Vasyka d9180e16b3 feat: P2 RBAC defers — REST API + invitation workflow
Closes the P2 items from /tmp/service/new/01-TZ-rbac §4.1 §4.2.

== User invitation workflow ==

New columns on users: invited_at, invited_by_id (FK self), accepted_at,
invitation_token (sha256 hash, indexed). Migration is idempotent.

User::sendInvitation($invitedBy = auth()->user())
  - generates 64-char random token
  - stores sha256(token) in invitation_token column (never plaintext)
  - marks invited_at = now(), status = inactive
  - queues UserInvitationMail to the user's email with the signed accept URL
  - returns the raw token (for tests / API consumers)

User::findByInvitationToken($rawToken) hashes + lookups.
User::acceptInvitation($password) sets password (hashed cast), clears
invitation_token, marks accepted_at + email_verified_at, status = active.

Web routes (no auth — token IS the credential):
  GET  /invitations/{token}  → password-set form
  POST /invitations/{token}  → validates min:8 + confirmed, accepts

Tokens expire after 7 days (checked against invited_at). Expired and
invalid tokens render dedicated views (invitations/expired.blade.php,
invitations/invalid.blade.php) instead of generic 404 — so the user
knows to ask for a resend.

UserInvitationMail uses Filament's existing markdown layout; subject
includes the tenant display_name.

== REST API ==

Twenty new endpoints under /api/v1/ (Sanctum auth + tenant scoping
via the existing EnsureTokenMatchesTenant middleware). All gated by
ADMIN_USERS_* / ADMIN_ROLES_MANAGE permissions; mechanic-level token
gets 403.

Users:
  GET    /users                                  — paginated + role/status/q filters
  GET    /users/{u}                              — eager-loads roles + overrides + invitedBy
  POST   /users                                  — creates inactive user + sends invitation
  PATCH  /users/{u}                              — update name/email/role/status
  DELETE /users/{u}                              — soft delete
  POST   /users/{u}/activate
  POST   /users/{u}/deactivate                   — also revokes all sessions
  POST   /users/{u}/resend-invitation
  POST   /users/{u}/force-password-reset         — re-sends invitation
  GET    /users/{u}/sessions                     — list active sessions (from sessions table)
  DELETE /users/{u}/sessions                     — revoke all
  DELETE /users/{u}/sessions/{sessionId}         — revoke one
  GET    /users/{u}/roles                        — assigned roles
  POST   /users/{u}/roles                        — assign role
  DELETE /users/{u}/roles/{role}                 — remove role
  GET    /users/{u}/permissions                  — effective: role perms + grants - active denies
  POST   /users/{u}/permission-overrides         — add grant/deny (with optional expires_at)
  DELETE /users/{u}/permission-overrides/{perm}

Roles:
  apiResource roles                              — index/show/store/update/destroy
                                                   (system roles guarded against rename/delete)
  GET    /roles/{r}/permissions
  PUT    /roles/{r}/permissions                  — bulk sync
  GET    /permissions                            — catalog: flat list + grouped + labels + role labels

Authorization is uniform: every controller method calls $this->authorize()
which throws 403 if canDo(perm) is false. canDo() already honors the
overrides + admin bypass + audit log from earlier commits, so the API
behaves identically to the Filament UI.

== Tests ==

InvitationFlowTest (8): token generation + sha256 storage + queued mail,
findByInvitationToken happy/sad path, accept sets password + activates,
GET form renders, POST accepts + redirects, invalid token view,
backdated invited_at → expired view, password too short → validation error.

RbacApiTest (12): admin can list users, mechanic 403, create user
queues invitation, assign+remove role round-trip, effective permissions
endpoint subtracts active denies, add+remove override via API,
role index returns 7 system roles with permission counts (51 for owner),
role sync permissions, system role destroy rejected with 422,
permission catalog endpoint returns all 51 + grouped + labels,
revoke all sessions deletes only target user's rows.

Suite: 234 passed (659 assertions). Was 214.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 22:36:44 +00:00
Vasyka 1d4ac3db38 feat: P1 RBAC defers — overrides + sessions + audit log
Completes the P1 items from /tmp/service/new/01-TZ-rbac §2.1 §4.1.

== user_permission_overrides table ==
Per-user grant/deny exceptions on top of role-based RBAC. Composite PK
(user_id, permission_id) so each user can have at most one override per
permission. Schema:
- mode: 'grant' | 'deny'
- reason: text (audit context: "lockdown audit period", etc.)
- granted_by_id + granted_at: who/when made the exception
- expires_at: optional auto-expiry

Eloquent model UserPermissionOverride with relations to user, permission,
grantedBy; isExpired() helper.

== Resolution order in User::canDo() ==
1. Active deny-override (not expired) → return false (and log if sensitive)
2. Active grant-override (not expired) → return true
3. Admin/owner bypass → return true
4. Standard role-based check via Spatie

Critically: deny overrides ALSO block admin/owner. This is intentional —
the TZ's "separation of duties" requirement (an admin who shouldn't be
able to delete payments). Without this, deny is useless against admins.

Override resolution uses a single query per check (cached by Eloquent
during the request). The override-check happens before the role check
so a deny is always authoritative.

== Audit log on sensitive denials ==
When canDo() returns false for one of these sensitive permissions, a
spatie/activitylog entry is written with event=permission_denied:
- admin.users.manage / admin.roles.manage / admin.settings.edit
  / admin.backup.download
- finance.delete_payment / finance.view_pl
- salaries.mark_paid / salaries.view_all
- work_orders.delete / work_orders.approve_discount_any

Non-sensitive denials (e.g., clients.create) don't log to avoid noise.
The activity payload includes the permission slug; causedBy is the user
who was denied. Failures of the logger are swallowed so a misconfigured
activitylog never breaks auth.

== UserResource UI ==
New PermissionOverridesRelationManager mounted on the edit page:
- Table: permission, mode (GRANT/DENY badge), reason, expires_at,
  granted_by
- Create form: permission select, mode, expires_at, reason
- granted_at + granted_by_id auto-populated to now() / auth()->id()
- Default sort: granted_at desc

Two new actions on the user row:
- "Force logout" (warning color): visible only when active sessions
  exist. Deletes every row in `sessions` with user_id=record→id.
  Notification shows count revoked.
- "Resetează 2FA" stays (from previous commit)

Two new toggleable columns:
- Sesiuni active (count from sessions table)
- Excepții (count of permission overrides)

== Tests ==
PermissionOverridesTest covers:
- grant unlocks a permission the role doesn't have
- deny blocks a permission the role grants
- deny blocks even admin role (separation of duties)
- expired override is ignored
- future-expiry override stays active
- audit log writes on sensitive denial
- audit log silent on non-sensitive denial
- force_logout deletes all user sessions but not others'

Suite: 214 passed (591 assertions). Was 206.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 22:27:20 +00:00
Vasyka 58004b65c4 feat: RBAC catalog + 2FA UX (P0 blocker from /tmp/service/new/01-TZ)
Implements the RBAC TZ in app/Auth/Permissions.php with a 51-permission
catalog spanning 9 modules (clients/vehicles/work_orders/finance/salaries/
inventory/suppliers/admin/ai_assistant+analytics). All slugs are constants,
not magic strings — refactors against renames stay safe.

== 7 system roles ==
owner / admin / manager / accountant / receptionist / mechanic / viewer
Each gets a curated role-permission matrix per the TZ section 2.4:
- owner + admin: all 51
- manager: 23 (operations + reporting, no destructive finance/users)
- accountant: 17 (full finance/salaries, view-only WOs, no admin)
- receptionist: 13 (front-desk: clients/vehicles/WOs/payment-create)
- mechanic: 4 (own WOs + inventory view + own salary)
- viewer: 6 (read-only everything except finance/salaries)

== Seeder ==
App\Services\RbacSeeder:
- seedPermissions() creates the 51 Permission rows globally (idempotent)
- seedTenantRoles($companyId) sets the team context, creates the 7 Role
  rows scoped to that tenant, and syncPermissions per matrix
- syncUsersToRoles($companyId) maps legacy users.role string column to
  the new Spatie role assignment (parts_manager→manager, master→mechanic,
  marketer→manager, user→viewer)

== Migration ==
2026_06_04_000003 loops over all existing Companies and runs the seeder.
On a fresh prod deploy, every tenant gets the full RBAC catalog wired up
automatically. CompanyProvisioner::provision() also calls the seeder for
new tenants going forward.

== Resource gates ==
canViewAny / canCreate / canDelete on:
- PaymentResource (FINANCE_VIEW_OVERVIEW / FINANCE_CREATE_PAYMENT / FINANCE_DELETE_PAYMENT)
- ExpenseResource (FINANCE_VIEW_OVERVIEW / FINANCE_CREATE_EXPENSE / FINANCE_DELETE_PAYMENT)
- PayrollAdjustmentResource (SALARIES_VIEW_ALL / SALARIES_CALCULATE)
- PayrollRunResource (SALARIES_VIEW_ALL / SALARIES_CALCULATE)
- UserResource (ADMIN_USERS_VIEW / ADMIN_USERS_MANAGE)
- RoleResource (ADMIN_ROLES_MANAGE)

Mechanic sees only own WOs + inventory + own salary. Accountant sees all
finance but not admin. Receptionist sees clients/WOs but not finance
overview. Etc.

== User helpers ==
$user->canDo(Permissions::WORK_ORDERS_CREATE) — admin gets a bypass to
prevent lockouts from misconfigured permission grants.
$user->isOwner() / isAccountant() / isMechanic() — role shortcuts.
$user->hasTwoFactorEnabled() — true when app_authentication_secret is set.

== 2FA ==
Filament 5's native MultiFactorAuthentication (App + Email) is already
enabled in both TenantPanelProvider and CentralPanelProvider — confirmed.
The User model already implements HasAppAuthentication +
HasAppAuthenticationRecovery + HasEmailAuthentication.

This commit adds UX around it:
- UserResource list column: 2FA badge (green ✓ when enabled, amber ⚠ when off)
- UserResource form: "Securitate" section shows enabled/disabled + last_login_at
- New admin action "Resetează 2FA" with confirmation modal — clears
  app_authentication_secret + recovery codes for locked-out users

== Roles management UI ==
New /app/roles RoleResource:
- List: role label + slug + permission count + user count
- Edit: 10 grouped checkbox lists (per module) for fine-grained
  permission assignment + bulk-toggle per group
- System roles (owner/admin/etc.) have slug locked, can't be deleted
- Custom tenant-specific roles can be added on top
- Gated behind ADMIN_ROLES_MANAGE

== UserResource extension ==
- Role select now uses Permissions::roleLabels() (owner/admin/manager/...)
- New "Roluri suplimentare" multi-select for stacking roles on top of
  the primary one (permissions cumulate)
- afterSave syncs the picked roles + ensures primary role is always
  included

== Tests ==
RbacTest covers: 51 permissions seeded, 7 roles per tenant, owner has
all, mechanic has minimal, accountant has finance but not admin,
canDo returns true when role has permission, admin bypass, owner helper,
syncUsersToRoles legacy mapping (parts_manager→manager, master→mechanic,
user→viewer), 2FA helper round-trip.

Suite: 206 passed (576 assertions). Was 196.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 22:03:03 +00:00
Vasyka 1d5ea6d261 feat: Calendar Vizual v2 (Pod×Days matrix) + hidden markup
Implements 2 of the biggest items from /tmp/service/new docs:

== Calendar Vizual v2 (from 02-prototip-calendar-vizual.html) ==

Replaces the FullCalendar week view (the one that visually collapsed after
Livewire re-renders) with a server-rendered matrix that the harness
already drives through Livewire — no third-party JS to clash with Filament.

Layout: 8-column CSS grid (1 row-label + 7 days). Rows are either Posts
(Pod 1, Pod 2…) or active masters depending on toolbar switch. Each
cell holds 0..N event cards.

Per-cell load badge (top-right):
  hours_planned / capacity  →  badge color (gray <50%, orange 50–90%, red ≥90%)

Drag-drop: HTML5 native, Alpine.js holds the dragEventId, moveEvent($id,
$toRowId, $toDate) in PHP updates either post_id or master_id (depending
on groupBy mode) plus date — works seamlessly when re-grouping.

KPI bar (4 cards above toolbar):
- Ore programate X / Y · % capacity
- Fișe deschise (orange)
- Confirmate X/Y (green) + confirmation rate
- No-show alert (red) — scheduled events <24h away that are still unconfirmed

Toolbar:
- ◀ Week ▶ + Astăzi (reset)
- Date label "01 — 07 iunie 2026"
- Grupare switch: Pod ↔ Mecanic
- Filtru: master dropdown + status dropdown (Confirmate/Neconfirmate/În lucru)

Today column highlighted blue; Sunday column hatched as closed
(non-interactive, no drop target); Saturday muted as weekend.

Event card color = master.color (deterministic, matches profile setting),
shown as left border + background tint. Title = client name; meta =
"VW Passat · CIU 001"; time = "08:00–12:00 · V.".

Click empty cell → quick-create panel (right slide-in) with date+pod
pre-filled. Click event → detail panel with Client/Phone/Auto/Plate/
Master/Pod + delete + edit.

Legend section at bottom (mecanici dots, load colors, day states).

== Hidden Markup (from gap-analysis.md #3) ==

Adds `hidden_markup_pct` decimal to parts. Customer documents continue
to show the standard sell_price; the hidden markup is an internal margin
indicator used for B2B contracts and corporate analytics.

Part::internalCostWithHiddenMarkup() returns buy_price * (1 + pct/100).
Falls back to buy_price when pct is null. Decimal:2 cast so persistence
round-trips cleanly.

== Schema migration ==

Idempotent (hasColumn guards):
- posts.hours_per_day decimal(5,1) default 10
- posts.description varchar(255) nullable
- parts.hidden_markup_pct decimal(5,2) nullable

== Tests ==

+11 new in CalendarBoardV2Test (8) + HiddenMarkupTest (3):
- get_days returns 7 days with today flagged + Sunday closed + Saturday weekend
- get_rows returns posts when grouped by post + with capacity
- get_rows returns masters when grouped by master + Fără maistru fallback row
- matrix places events in correct cells + sums hours
- move_event reassigns post_id and date
- create_appt inserts appointment via panel form
- stats compute utilization from events (8h / 60h capacity = 13%)
- status filter narrows to confirmed only
- hidden_markup applies pct correctly + falls back to buy_price + persists

Suite: 196 passed (551 assertions). Was 185.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 21:50:22 +00:00
Vasyka d9b198a235 feat: Pipeline board matches mockup pixel-by-pixel
Audit pass against /tmp/service/todo/psauto-pipeline-redesign.html — 10
gaps closed.

1. In-page TOPBAR (mockup had it; was missing): "Pipeline" title,
   sep, search box "Caută client, mașină, număr...", and right-side
   Filtre / Export / + Deal nou (primary) buttons. Search input is
   wire:model.live.debounce 300ms.

2. SEARCH actually filters cards: $searchQuery property in
   PipelineBoard scans subject + client_name + plate + code + phone
   across all 6 columns, case-insensitive.

3. "+ Deal nou" + "+ Adaugă cerere" (per-column bottom) now open the
   SAME right-side panel in "new form" mode. Inline create form:
   Nume / Telefon / Auto / Sursă / Notițe → createNewLead() inserts
   Lead with status=new, lands in col 1 instantly without leaving page.
   Validation: name + phone required.

4. EXPORT button calls exportCsv() — streams a CSV of current filtered
   columns (etapă, cod, subiect, client, telefon, auto, sumă,
   responsabil, stare timp).

5. PERIOD selector chip shows current month in Romanian
   (now()->locale('ro')->isoFormat('MMMM YYYY')) — matches "Iunie 2026".

6. HOVER icons now match mockup exactly per column:
   - request: 📅 schedule / 📞 phone / ⋮ edit
   - quote:   📅 schedule / 💬 wa / ⋮ edit
   - scheduled: 📄 file-plus (start WO) / 💬 wa / ⋮ edit
   - in_work: 👁 eye (open WO) / 💬 wa / ✓ mark Gata
   - ready:   💰 cash (mark paid) / 📞 phone / ⋮ edit
   - paid:    NONE (col 6 has no hover actions per mockup)

7. Col 6 "Achitat azi" cards now opacity:0.65, no hover actions,
   no time line, no assignee name (just avatar) — exactly as in mockup.

8. Sum display: amount == 0 renders "—" instead of "0 MDL", both in
   card footer and list view.

9. "Avans achitat" tag (blue) appears on Ready cards with partial
   payment (pay_status='partial'); "Neachitat" amber only when fully
   unpaid. Matches mockup col 5 example "Nissan Qashqai · Gata +
   Avans achitat".

10. Link tracking quick-action: appears in detail panel "Acțiuni rapide"
    grid when WO has tracking_url. Sits alongside WhatsApp / Sună / SMS.

Two-panel architecture: $showNewForm and $openCardKey are mutually
exclusive. Click outside or ✕ closes the panel; opening one closes
the other.

Tests: +4 (createNewLead happy path, validation, search filter,
partial payment tag). Suite 185/185 (was 181).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 21:18:54 +00:00
Vasyka 3c0f3ba39e feat: Pipeline board full-bleed + hover actions + Programare→Calendar
Addresses the gaps surfaced after the first redesign — the board was still
boxed in Filament chrome (not truly full-page), hover floating-actions and
"+ Adaugă" CTAs were missing, and the P0 "Programează" from deal card had
no calendar wiring.

Full-page:
- getMaxContentWidth() = Width::Full
- getHeading()/getSubheading() return empty so Filament's title bar
  disappears, leaving the kanban edge-to-edge
- CSS uses :has(.pb-shell) to strip Filament's page padding + heading
  block at the layout level
- Board height = calc(100vh - 64px); columns scroll independently

Hover floating-actions on every card (column-aware):
- Cols 1-2 (Cerere / Calculație): 📅 quickSchedule
- Col 3 (Programat): ▶ start work (creates WO)
- Col 4 (În lucru): ✓ mark Gata
- Col 5 (Gata): 💰 mark Achitat
- All cards with phone: 📞 tel: + 💬 wa.me
- All cards: ↗ open in resource edit
- Shown only on .pb-deal:hover, positioned absolute top-right

"+ Adaugă" CTA at column bottom:
- Cols 1-3 → /app/leads/create
- Cols 4-5 → /app/work-orders/create

Programare → Calendar (P0 AAA):
- quickSchedule($key) on PipelineBoard creates a real Appointment row for
  tomorrow 10:00 linked to (client_id, vehicle_id, master_id, deal_id),
  sets deal.stage='scheduled' + scheduled_at, then shows a toast
- Panel bottom action bar gains "📅 Programează" CTA for lead/deal cards
- "📅 Calendar" jump CTA for WO cards
- calendarUrl() returns the canonical filament.tenant.pages.calendar-board
  route

Empty column state now reads "Gol — trage un card aici" instead of just
"Gol" so the drop affordance is explicit.

Stat strip + filter bar sticky at top; board fills the remaining viewport.

Tests: +1 (quickSchedule creates Appointment + moves deal). Suite 181/181.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 20:14:20 +00:00
Vasyka 3603c0e43b feat: rich Pipeline board — unified Lead/Deal/WO Kanban with SLA + drag-drop transitions
Replaces the bare 6-status WO Kanban with the unified Pipeline view from
/tmp/service/todo/psauto-pipeline-redesign.html. Six columns now span the
entire customer journey end-to-end:

  Cerere nouă → Calculație → Programat → În lucru → Gata → Achitat azi
  └─ Lead/Deal  └─ Deal      └─ Deal     └─ WO       └─ WO    └─ WO+Payment

Cross-model drag-drop transitions:
- Lead → Calculație: Lead::convert() creates Deal at stage=contact, marks
  quote_sent_at = now, quote_status = sent
- Deal (any earlier stage) → În lucru: spawns a WorkOrder from the deal
  (client, vehicle, master, total, complaint), sets deal.stage=in_work,
  links wo.deal_id
- WO → Gata: status=ready + fires NotificationDispatcher::workOrderReady
  so client gets Telegram/email automatically
- WO → Achitat: creates Payment for remaining balance + status=done,
  closed_at=today (pay_status syncs to paid via Payment booted hook)

Rich card content per the mockup:
- Red urgent stripe (left border) for Deal.urgent or WO.urgency!=normal
- Source tag (Instagram/Site/Apel/etc.) on lead/deal cards
- Quote status badge ("Trimis · fără răspuns" amber / "Văzut ✓" blue /
  "A răspuns" green) based on deal.quote_status
- Scheduled time + bay tag ("05.06 · 09:00" + "Post 2")
- Fișă FL-NNN purple tag on WO cards
- "Necesită aprobare" amber tag when wo.status=agreement
- Progress bar (purple, 0-100%) on in-work cards: works_done + parts_installed
  over total lines
- SLA time line per card with overdue red color:
  * Lead 60+ min not contacted = overdue
  * Quote 2h+ no response = overdue
  * Ready 30+ min not paid = overdue (with phone icon)
  * WO past ETA = overdue
- Assignee avatar (deterministic CRC32 color: blue/green/purple/amber)
- Amount in MDL, formatted

Stat strip (6 metrics computed live):
- Total deals active (sum of cols 1-5)
- MDL pipeline total
- MDL closed today (Payment sum where paid_at=today)
- Necesită acțiune (overdue + urgent + pending approval)
- Rata conversie 30d (won / (won+lost) %)
- Depășit termen (count WO past eta_at)

Filter chips wire-driven: Toate / Ale mele (assigned_to=me) /
Urgente (urgent=true OR wo.urgency!=normal) / Azi.

View toggle: Kanban ↔ Listă (table with all cards flat, sortable by stage).

Slide-in detail panel:
- 6-step stage stepper highlighting current
- Client / Telefon (blue clickable) / Auto / Sursă / Responsabil / Sumă /
  De achitat (live computed balanceDue for WOs)
- Note / Reclamație
- Linked Fișă card with status badge, progress, ETA, "necesită aprobare"
  alert + tracking link
- Activity timeline from Spatie activity-log
- Quick actions: WhatsApp (wa.me/<phone>), Sună (tel:), SMS (sms:),
  Deschide (jumps to Filament resource edit)

DealResource hidden from nav (shouldRegisterNavigation=false) since
PipelineBoard is the canonical entry, but its edit/create routes stay
intact — the panel deep-links to them.

Auto-refresh: wire:poll.10s keeps the board live without WebSocket
dependency. Drag-drop is HTML5 native + Livewire wire:click for ops.

Dark mode supported via CSS variables overridden in .dark scope.

Migration: extend deals table with urgent, quote_sent_at, quote_status,
quote_seen_at, scheduled_at, bay, confirmed_at, confirmed_via,
last_action_at. Idempotent (hasColumn guards). Deal model auto-updates
last_action_at on saving.

Tests: 7 new + full suite 180/180 green (was 173).
- partition leads/deals/wos by column
- stats computation: active, pipeline_mdl, closed_today_mdl
- lead→quote transition converts lead into deal
- deal→in_work creates WorkOrder linked back to deal
- wo→paid creates payment for balance + marks done
- filter "mine" narrows to assigned user
- openCard loads panel detail with correct stepper position

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-04 20:02:44 +00:00
Vasyka 0620635abb test: full E2E audit + fix CsvImportExport vehicle.brand → make
Audit pass (33 new tests in tests/Feature/Audit/):
- CrmFunnelE2ETest: full Lead→Deal→Appointment→WO→Payment journey,
  covering 5 previously-untested models. Verifies WO.balanceDue updates
  correctly after payments, including refunds (negative amount → balance
  increases).
- WorkOrderTotalsTest: works+parts+subcontract+discount sum correctly,
  cancelled subcontract excluded, deleting lines triggers recalc, status=done
  consumes part reservations into issues, cancelled releases reservations.
- ShopJourneyE2ETest: register→cart→checkout→email confirmation→tracking
  page reachable→admin fulfills→stock drops→warehouse event recorded.
  Also guest checkout still works without account.
- CsvImportExportTest: round-trip, dedup-by-phone, **caught real bug** —
  Vehicle export wrote $row->brand (no such property) and import set
  'brand' => row['brand'] in Vehicle::create (column is `make`). Fix
  applied to both paths.
- TenantBackupServiceTest: zip contains valid manifest with counts +
  data/*.json per model + works embedded with WorkOrder.
- WorkOrderPdfServiceTest: generated PDF starts with %PDF, includes WO data,
  non-trivial size, handles empty WO.
- PayrollCalculatorTest: base + works_pct + parts_pct + bonus - fine -
  advance, scoped to user + period.
- NotificationFallbackTest: Telegram wins when chat_id present, falls back
  to email when not, returns false when neither, tenant disable flag stops
  both.
- AiProvidersCrossCheckTest: OpenAI request shape, Gemini URL with model,
  no-key friendly message, tenant model override propagates into HTTP body.
- SettingsPersistenceTest: 25-key settings JSON round-trips, partial update
  via array_replace_recursive preserves other keys.
- CompanyProvisionerTest: suspend / reactivate / archive behavior.

Bug fixed: CsvImportExport used `brand` on Vehicle which has column `make`.
The export silently emitted empty values, the import silently dropped the
brand. Now both paths use `make`.

Full suite: 173 passed (468 assertions). 0 failed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-03 07:05:46 +00:00
Vasyka 439ef605a1 feat: production email (Resend) + offsite backup (B2)
Resend mail transport:
- composer require resend/resend-laravel (v1.4)
- Laravel 11 ships the 'resend' mailer config in config/mail.php + services
- To switch to production email: set MAIL_MAILER=resend + RESEND_API_KEY,
  register the domain at resend.com/domains, and add the TXT + DKIM CNAME
  records in Cloudflare. .env.example documents the required steps.

Backblaze B2 offsite backup:
- New filesystems 'b2' disk (S3-compatible, env: B2_KEY/SECRET/BUCKET/REGION/ENDPOINT)
- BackupAllTenantsCommand: after writing each tenant's zip to local disk, it
  uploads the same file to the b2 disk under {YYYY-MM-DD}/{slug}.zip — only
  when both B2_KEY and B2_BUCKET are set, so unconfigured installs are no-op
- Without offsite, backups live on the same VPS as production: a single
  hardware failure loses everything. B2 + Resend together make the install
  genuinely production-ready (people get email + offsite backup).

Tests (2 new):
- backup uploads to b2 (fake disk) when configured
- backup skips offsite when env vars not present

Full suite: 140 passed. Force-rebuild deploy required so composer install
picks up resend/resend-php.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-03 06:43:39 +00:00
Vasyka 51917bcbaf feat: rate limiting + internal health monitor + secure VAPID note
Rate limiting:
- Shop POST endpoints get per-IP throttles with distinct prefixes so login,
  register, password-email, and password-reset have separate buckets:
  login/register/pw-reset = 5/min, pw-email = 3/min
- OcrInvoiceService gates per-tenant via RateLimiter (30/hour) so a runaway
  uploader can't burn Claude Vision spend

Health monitor (poor-man's monitoring):
- HealthCheckCommand probes DB (SELECT 1), cache write/read, public storage
  write/read, and most-recent backup age. On any failure, pushes a Telegram
  alert via HEALTH_ALERT_BOT_TOKEN/HEALTH_ALERT_CHAT_ID. Dedups identical
  failures within a 30-min window via cache.
- Scheduled every 10 min. Pair with external uptime monitoring (UptimeRobot,
  Better Stack hitting /up) for total-outage coverage.
- .env.example documents the two new env vars.

VAPID secret hygiene:
- credentials.md no longer stores the VAPID_PRIVATE_KEY; the source of truth
  is the Coolify env on the autocrm app. Doc points to where to read it
  (UI or API). Mitigates accidental git leak.

Tests (4 new):
- shop login throttles after 5 attempts (6th = 429); register throttle is
  independent of login (separate prefix); health command runs clean; dedup
  cache path exercised

Full suite: 138 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-03 06:37:53 +00:00
Vasyka 0e3f9e8bca feat: AI model selector + i18n nav labels (RU/EN) on new modules
AI model selector:
- AiAssistantService::MODEL_DEFAULTS and MODEL_OPTIONS const tables (3 picks per
  provider: Claude Opus 4.7 / Sonnet 4.6 / Haiku 4.5, OpenAI 4o / 4o-mini,
  Gemini 1.5 Pro / Flash). Default upgraded from Sonnet 4.5 → Sonnet 4.6.
- modelFor(provider, company?) resolves tenant override > global default.
- All 8 hardcoded model strings replaced with modelFor() across callClaude
  (chat with tool-use), callOpenAI, callGemini (chat), postClaude/postOpenAI/
  postGemini (single-shot), and OcrInvoiceService.
- Settings page adds 3 model selectors per provider with persistence at
  settings.ai.models.{claude,gpt,gemini}.

i18n nav labels:
- TireSet / Bodyshop / Subcontractor / SubcontractJob / PricingCoefficient /
  ShopCustomer resources: getNavigationLabel / getNavigationGroup /
  getModelLabel / getPluralModelLabel return __()-wrapped strings.
- 20 keys added to lang/ru.json and lang/en.json.

Tests (4 new): default model, tenant override wins, unknown provider falls
back to claude default, options dictionary contains each default key.

Full suite: 134 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-03 06:23:21 +00:00
Vasyka 3da1f5412a feat: shop UX polish — password reset / order email / multi-image / customer admin
Shop password reset:
- Configured 'shop_customers' password broker on the existing
  password_reset_tokens table
- ShopCustomer::sendPasswordResetNotification overrides Laravel default to
  send a ShopPasswordResetMail with a tenant-subdomain reset URL
- Routes /shop/password/forgot, /shop/password/email, /shop/password/reset/{token}
  + ShopAuthController showForgotPassword/sendResetLink/showResetPassword/
  resetPassword. Forgot view stays generic ("if it exists, we sent…") to avoid
  email enumeration. Login view links to "Am uitat parola".

Order confirmation email:
- ShopOrderConfirmationMail + nicely formatted HTML email template
- ShopOrderNotifier::placed now also emails customer_email (best-effort,
  warning-only logged on failure) alongside existing Telegram + staff push

Multiple images per Part:
- Part media collection switched from singleFile to multiple (max 8 in form)
- imageUrls() helper for galleries; imageUrl() still returns first for cards
- PartResource form: reorderable multi-upload
- Shop part detail: vertical thumbnails switch the main image via vanilla JS

ShopCustomerResource (tenant Filament, "Magazin" nav group):
- List with name/phone/email/client_id/orders_count/last_login_at
- Edit (no password field exposed)
- "Trimite reset parolă" action uses the new broker
- OrdersRelationManager shows the customer's orders read-only

Tests (7 new):
- forgot sends mail; forgot doesn't disclose unknown email; reset with valid
  token changes password; bad token rejected; order email when customer_email
  set; email skipped without it; Part has imageUrls() collection

Full suite: 130 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-03 06:14:45 +00:00
Vasyka fca4f75e9c feat: OCR invoice import via Claude Vision
Upload an invoice photo → Claude extracts {supplier_name, date, currency,
items, total} as JSON → auto-create a draft Purchase + PurchaseItems →
redirect to edit so the user reviews before confirming/receiving.

OcrInvoiceService:
- Validates supported MIME (jpg/png/webp/gif)
- Reads tenant Claude key (settings.ai.claude_key) — friendly error if missing
- Calls /v1/messages with image content block + structured-output system prompt
- Tolerant parser: strips ```json fences, falls back to first {…} block
- normalize(): computes per-item total when absent, fills overall total
- All return shapes: {ok:bool, data?, error?, raw?, tokens?}

Filament:
- "Import factură (OCR)" header action on Purchases list
- Image file upload → service → matches Supplier by case-insensitive name
  (notes the unmapped name if no match) → creates draft Purchase + items →
  redirects to the Edit page

Tests (6 new):
- clean JSON parses; markdown fences stripped; malformed → graceful error;
  missing key → friendly message + no HTTP; unsupported MIME rejected;
  item total computed when missing

Full suite: 123 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-03 06:00:45 +00:00
Vasyka 75386c354a feat: shop customer accounts (register/login + order history)
Schema:
- shop_customers (company_id, name, phone unique-per-tenant, email, password,
  client_id auto-linked, last_login_at)
- online_orders.shop_customer_id nullable FK

Auth:
- New 'shop' guard (session driver, shop_customers provider) in config/auth.php
- ShopCustomer Authenticatable with hashed password cast and BelongsToTenant
  global scope — login attempts naturally scoped to current tenant subdomain

Flow:
- ShopAuthController: register / login / logout / account
- Register auto-links to existing Client by phone match
- /shop/account: order history (only the logged customer's orders) + profile
- Checkout prefills name/phone/email from logged customer + sets
  shop_customer_id (and client_id from auto-link) on the placed order
- Layout nav switches between Login/Register and "👤 Name + Ieșire"

Tests (8 new):
- register creates customer + auto-login
- register auto-links existing Client by phone
- duplicate phone rejected
- login validates credentials
- /account requires auth (redirects to /shop/login)
- /account lists only the logged customer's orders
- checkout attaches shop_customer_id
- customers tenant-isolated

Full suite: 117 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-02 19:43:39 +00:00
Vasyka dfb92bf5e2 feat: AI chat tool-use (Claude function calling)
The Asistent AI chat can now query the tenant DB directly through Claude
tool use. AiToolExecutor exposes 5 read-only tools (search_clients,
get_vehicle, find_parts, recent_workorders, low_stock_parts) all scoped
to the current tenant via BelongsToTenant.

AiAssistantService::callClaude loops on stop_reason=tool_use up to 5 rounds:
- normalize message history to content blocks
- send `tools` definitions + messages to Anthropic API
- on tool_use: execute each tool, append tool_results as user turn, recall
- on end_turn: collect text + cumulative token counts + tool-call audit in
  AiMessage.meta.tools

Single-shot helpers (suggestDiagnosis, suggestPrice, vinRecommendations,
suggestParts) are unchanged — only the conversational chat gets tool-use.

Tests (3 new):
- two-round tool_use → execute → final text; verify 5 tools sent both rounds;
  cumulative tokens
- executor finds part by brand
- unknown tool name returns error blob

Full suite: 109 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-02 19:36:07 +00:00
Vasyka 8fdfc9ef85 feat: Part product images + seasonal tire-swap reminders
Part (HasMedia):
- Spatie media `image` single-file collection + imageUrl() helper
- PartResource form: image upload section (image editor, 2 MB max)
- Parts list: circular thumbnail column
- Shop catalog cards: square thumbnail + 📦 placeholder
- Shop part detail: 260px image alongside info, single column when no image

Seasonal tire-swap reminders:
- NotificationDispatcher::tireSeasonalSwap(TireSet) — Telegram first, email
  fallback (when set has a vehicle, via ServiceReminderMail with 'tire_swap'
  type and a size-aware note)
- tires:remind-seasonal artisan command, self-gating to Feb 15-Mar 15
  (notify winter sets stored) and Sep 15-Oct 15 (notify summer sets stored).
  60-day cooldown per client via service_reminders_sent. --force / --dry-run.
- Schedule: weekly Mon 09:30

Tests (6 new):
- outside window no-ops; spring window notifies winter; spring ignores summer;
  autumn notifies summer; cooldown blocks doubles; --force overrides window

Full suite: 106 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-06-02 19:31:24 +00:00
Vasyka b9ff9c6583 fix: scanner wire:ignore + Company custom columns
- Scanner page: wrap the html5-qrcode camera container (#reader) in
  wire:ignore so a Livewire DOM morph can't tear down the live camera
  stream (same class of bug as the calendar).
- Company::getCustomColumns(): add `is_demo` and `default_warehouse_id`.
  Stancl Tenant treats columns absent from this list as virtual `data` JSON
  attributes, so editing a company could move default_warehouse_id into data
  and null the real column — breaking WarehouseService::defaultWarehouse.

Full suite: 100 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 23:14:09 +00:00
Vasyka c84ef5d9bd fix: calendar collapses to plain text after first render
FullCalendar mounts into a Livewire-managed subtree. The first
$wire.getEvents() response triggered a Livewire DOM morph that reverted
#autocrm-calendar to its empty server HTML, destroying the rendered grid
(~1s after load it became unstyled text).

Wrap the calendar container in wire:ignore so Livewire's morphdom skips it.
The quick-create modal stays outside wire:ignore to keep its form reactive.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 22:58:55 +00:00
Vasyka 40478dd2aa fix: make purchases partial-receipt migration idempotent
On MariaDB (no transactional DDL) a half-applied prior run left
purchases.warehouse_id committed without recording the migration. On retry it
failed with "Duplicate column name 'warehouse_id'", which aborted the migrate
run and blocked every later migration (notifications, push, online store,
pricing, labor templates, tire, subcontractor, bodyshop) — so those tables were
never created (e.g. bodyshop_jobs missing → 500 on tenant pages).

Guard each step with Schema::hasColumn / hasTable so the migration completes on
re-run and unblocks the rest of the batch.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 22:39:34 +00:00
Vasyka ac7d5b4733 fix: central company view page 404 + broken stats query
The /admin/companies/{id} view page (ViewCompany extends Page) 404'd because
the {record} route param could arrive as a JSON-encoded model (Livewire typed-
property hydration), so findOrFail() received a non-id and threw
ModelNotFoundException. Added resolveRecordKey() to normalize scalar id / model
/ JSON-string down to the integer key.

Also fixed getStats() referencing non-existent parts columns `stock` /
`low_stock_threshold` (real columns are `qty` / `min_qty`), which would 500 the
page once mount resolved.

Added CentralCompanyViewTest as regression (asserts 200 + company name). Full
suite: 100 passed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 10:24:08 +00:00
Vasyka d6a0bfb890 fix: repair 4 pre-existing failing tests
- WorkOrderCalcTest: vehicle fixture used `brand` (column is `make`, NOT NULL)
  and WorkOrderPart used `price`/manual total (column is `sell_price`, total is
  auto-computed). Switched to correct columns + valid status.
- ExampleTest: stock test expected 200 on `/` but the central domain redirects
  to /admin. Replaced with a deterministic central-root → /admin redirect check.

Full suite now green: 99 passed, 0 failed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 07:14:20 +00:00
Vasyka 5e255b7b40 Stage 10 — Bodyshop / PDR / Detailing: damage map + insurance + photos
Completes the 18-stage roadmap (17/18 fully functional, 18 partial).

Schema:
- bodyshop_jobs (type body_repair/pdr/painting/detailing/ceramic/ppf/polishing,
  status workflow, insurance case fields, estimate/approved amounts)
- damage_points (zone, kind, severity) — the damage map

Models:
- BodyshopJob (HasMedia: photos_before/photos_after), auto number BS-YY-NNNN
- DamagePoint with ZONES/KINDS/SEVERITIES

Filament (new "Tinichigerie" nav group):
- BodyshopJobResource: type/status, collapsible insurance section (conditional
  fields), before/after photo upload, estimate/approved amounts
- DamagePointsRelationManager (zone + kind + colour-coded severity)
- Table with type badge, insurance flag, damage count; nav badge = open jobs

Tests (5 new):
- auto number; damage points relation; insurance fields persist;
  detailing types supported; tenant isolation

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 06:49:47 +00:00
Vasyka e8078f157a Stage 9 — Subcontractor System: outsourced work with cost+markup
Schema:
- subcontractors (specialty, rating, contact)
- subcontract_jobs (work_order link, cost, markup_pct, client_price, status
  workflow, sent_at/eta/returned_at, paid_to_sub)

Models:
- SubcontractJob: auto number (SC-YY-NNNN), client_price = cost×(1+markup/100)
  when markup>0 (else manual), margin() helper, recalcs parent WO on save/delete
- WorkOrder.recalcTotal now includes non-cancelled subcontract job client_price

Filament (new "Subcontractare" nav group):
- SubcontractorResource (specialty/rating CRUD)
- SubcontractJobResource board with cost/client/margin columns + status filters,
  nav badge = open jobs
- SubcontractJobsRelationManager on WorkOrder

Tests (7 new):
- client_price from markup; manual price without markup; auto number;
  WO total includes jobs; cancelled excluded; delete recalcs; tenant isolation

Closes roadmap to 16/18 stages (only Stage 10 Bodyshop remains).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 06:43:15 +00:00
Vasyka 94938f24d7 Stage 11 — Tire Service: tire hotel + wheel sets
Schema:
- tire_sets (client/vehicle, season, size width/profile/diameter, brand/DOT,
  rims, tread JSON per position + tread_min cache, TPMS + sensor ids, photos)
- tire_storage (location, season_label, stored/retrieved, check-in/out, fee)

Models:
- TireSet (HasMedia): sizeLabel, isStored, currentStorage, auto tread_min
- TireStorage: durationDays, isActive

Filament (new "Anvelope" nav group):
- TireSetResource: specs form + per-position tread + TPMS + photo upload;
  table with size, season badge, min tread (red < 3mm), storage status
- Check-in (location + period + fee → stored) / Check-out (→ retrieved)
- StorageRelationManager (stay history); nav badge = sets currently stored

Tests (6 new):
- sizeLabel formatting; tread_min from positions; check-in active storage;
  check-out retrieved + duration; multiple stays per set; tenant isolation

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 06:33:00 +00:00
Vasyka a1be01b0d5 Stage 4 — Labor Catalog: fixed price + default parts + service templates
Schema:
- labors.pricing_mode (hourly/fixed) + fixed_price
- labor_parts (default parts auto-added with a labor)
- service_templates + service_template_items (labor/part bundles)

ServiceComposer:
- addLabor(wo, labor, withParts) — hourly (hours×rate) or fixed (fixed_price),
  then auto-adds the labor's default parts
- addPart(wo, part, qty) — catalog price snapshot
- applyTemplate(wo, template) — adds all labor+part lines, recalcs total
- hourlyRate from settings.labor_rate

Filament:
- LaborResource: pricing_mode (live) toggles hours/fixed_price fields,
  DefaultPartsRelationManager
- ServiceTemplateResource (Service group) with ItemsRelationManager
- WorkOrder edit "Aplică șablon" action → applyTemplate
- WorksRelationManager CreateAction auto-adds labor default parts

Tests (6 new):
- hourly rate×hours; fixed uses fixed_price; default parts auto-added;
  withParts=false skips; applyTemplate adds lines + recalcs total;
  templates tenant-isolated

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 06:16:50 +00:00
Vasyka c90c35d930 Stage 8 — Smart Pricing Engine: contextual coefficients
Contextual multipliers layered on top of base MarkupRule pricing, applied
per work-order line based on vehicle, client and urgency.

Schema:
- pricing_coefficients (multiplier, conditions JSON, priority, stackable)
- vehicles.vehicle_class (sedan/suv/commercial/hybrid/ev/premium)
- clients.is_vip
- work_orders.urgency (normal/urgent/express)

PricingEngine::quote(Part, Vehicle?, Client?, urgency):
- base = MarkupRule on buy_price (fallback sell_price or buy×1.30)
- context: class (explicit or inferred hybrid/ev from fuel), age, vip, urgency
- stackable coefficients all multiply; non-stackable take only the highest
- returns {base, final, applied[]} breakdown

PricingCoefficient::matches(ctx) — classes/age range/vip/urgency conditions
(empty = always applies).

Filament:
- PricingCoefficientResource with condition builder (classes, age, vip, urgency)
- vehicle_class select, client is_vip toggle, WO urgency select
- "Preț inteligent" action on WO parts shows breakdown + applies sell_price

Tests (6 new):
- base-only without coefficients; age coefficient gating; VIP; express urgency;
  stackable multiply vs non-stackable highest-wins; hybrid inferred from fuel

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 05:40:27 +00:00
Vasyka 954ba8f059 Stage 12 — Online Store: public catalog + cart + orders
Schema:
- online_orders (token-tracked, status workflow, delivery method/fee)
- online_order_items (price snapshot, fulfilled flag)
- part_cross_refs (OEM/equivalent codes for search)
- parts.is_published (shop visibility)

Storefront (ShopController, tenant subdomain, /shop):
- Catalog with search across name/article/brand/cross-refs, category +
  in-stock filters, live stock, white-label themed layout
- Part detail page with cross-ref codes
- VIN search → VinDecoder → guided catalog search
- Session cart (per-tenant key), guest checkout, order confirmation page
- Respects settings.shop.enabled (404 when off); tenant-guarded

Part::searchPublished matches cross-ref articles via whereHas.

Order notifications (ShopOrderNotifier, best-effort):
- Staff: Web Push to active users
- Customer: Telegram if phone matches a linked client

Filament (tenant):
- OnlineOrderResource under "Magazin" nav group, status workflow,
  items relation, "Onorează" action issues stock via WarehouseService (FIFO)
- PartResource: is_published toggle + column + bulk publish/unpublish +
  CrossRefsRelationManager
- Settings: shop section (enable, delivery methods, fee, free-over)
- Landing page: shop button when enabled

Tests (6 new):
- catalog 404 when disabled; lists published only; cross-ref search;
  order placement (token + items + total); fulfill issues stock;
  cross-tenant token isolation

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 05:27:51 +00:00
Vasyka c413004930 Stage 15 — PWA complete: install prompt + Web Push notifications
Dependency:
- minishlink/web-push v10 (VAPID JWT + aes128gcm payload encryption)
- Dockerfile: add curl, mbstring, gmp extensions (web-push needs ext-curl)

VAPID:
- config/webpush.php from env; `php artisan push:vapid` generates keypair
- Shared platform keypair; .env.example has empty placeholders

Schema:
- push_subscriptions (user/company, endpoint unique, p256dh, auth, encoding)

WebPushService:
- send / sendToUser / dispatch via WebPush::flush
- Auto-prunes subscriptions reported expired (404/410)

Subscribe flow:
- POST /push/subscribe + /push/unsubscribe (auth, tenant)
- Tenant panel JS subscribes after SW registration with VAPID public key

Service worker (/sw.js):
- Cache v2, push listener → showNotification, notificationclick → focus/open

Install prompt:
- Floating "Instalează aplicația" button wired to beforeinstallprompt

Staff push:
- WorkOrder master_id change → push to assigned mechanic
- Settings "Test notificare push" action

Tests (6 new):
- subscribe stores + upserts; requires auth (401); validation (422);
  service configured; sendToUser with no subs returns zero

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-28 05:11:18 +00:00
Vasyka e48ef1b755 Stage 7+14 — Mechanic Board + Scan Center
Mechanic Workflow (Stage 7):
- /app/mechanic Filament page filtered to master_id = auth user
- Kanban 4 columns (in_work / awaiting_parts / ready / recent), each card
  shows WO#, plate, client, complaint summary, photo presence
- 2 KPI tiles (active now / closed today)
- Mobile-responsive grid (auto-fit, minmax 260px)

WarehouseService:
- issueNow(WorkOrderPart) — consume reservations immediately scoped to one
  line, without closing the WO (mechanic physically takes part now)
- returnPart(WorkOrderPart, qty?, notes?) — refund to stock as new batch
  at original buy_price, writes `return` event, capped at consumed total

WO PartsRelationManager:
- "Eliberează" action — visible when active reservation exists
- "Restituire" action — visible when consumed reservation exists, with qty
  modal + notes

Scan Center (Stage 14):
- PartResource "QR" action — per-part SVG QR with payload PART:<article|id>
- BulkAction "Tipărește etichete QR" → /parts/labels?ids=N,M (HTML A4 sheet,
  3-col grid, print CSS hides toolbar)
- /app/scan Filament page using html5-qrcode 2.3.8 (CDN), auto-picks back
  camera, decodes → Livewire dispatches scanner-decoded → resolveAndRedirect
- Lookup matches PART:N prefix, parts.article, parts.barcode, or numeric id
- Manual input fallback for browsers without camera

Tests (6 new):
- WarehouseIssueReturnTest (3): issueNow consumes immediately; returnPart
  creates positive batch + return event; over-return is capped
- ScannerLookupTest (3): PART: prefix lookup, raw barcode lookup, unknown miss

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 21:39:39 +00:00
Vasyka 1ff888131f Stage 16 — AI Layer: VIN decoder + diagnostic / parts / price helpers
VinDecoder (deterministic, no API):
- ISO 3779/3780 parsing: WMI manufacturer (~60 brands), year (cyclical with
  post-2010 disambiguation via position 7), region, plant, NA checksum
- Strip non-VIN chars, accept dashes/spaces, reject I/O/Q per spec

AiAssistantService:
- Refactored provider HTTP into postClaude/postOpenAI/postGemini so both
  chat history and one-shot calls share the same transport
- singleShot(system, userPrompt, provider?) for fire-and-forget calls
- 4 specialized helpers with tight prompts:
  - suggestDiagnosis(WO) — diagnostician based on complaint + VIN info
  - suggestParts(WO, task) — OEM parts list for an operation
  - suggestPrice(Part) — markup recommendation with justification
  - vinRecommendations(vin, mileage) — scheduled maintenance from decoded VIN
- monthlyUsage() — token spend MTD by provider

Filament:
- VehicleResource: "Decode VIN" + "AI: recomandări" actions
- WorkOrderResource Edit: "AI: sugerează diagnostic" header action
- PartResource: "AI: preț recomandat" action
- Shared views: filament.tenant.ai-reply, filament.tenant.vin-decode
- AiAssistant page shows monthly token usage banner

Tests (13 new):
- 8 VinDecoder unit tests with real VIN samples (Honda 2003, VW 1999, Audi
  2014, Dacia, unknown WMI, lowercase/dashes, forbidden chars)
- 5 AiHelpers feature tests with Http::fake covering all providers + no-key
  fallback + token usage aggregation

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 20:24:09 +00:00
Vasyka 85ef2f6e00 Stage 13 — Notifications: Telegram bot + multi-channel + service reminders
Schema:
- clients.telegram_chat_id (linked via /start contact-share)
- clients.notify_prefs (per-client channel order override)
- service_reminders_sent (dedup ledger for the daily cron)

Telegram (per tenant):
- TelegramService (sendMessage, getMe, setWebhook with auto-generated secret)
- Bot token stored in companies.settings.telegram.bot_token
- Webhook /telegram/webhook/{slug} validates X-Telegram-Bot-Api-Secret-Token,
  matches client by last 9 digits of phone, persists chat_id, replies confirm
- /start prompts share-contact; /stop unlinks chat_id

NotificationDispatcher refactor:
- Multi-channel: telegram first if chat_id + bot configured, then email
- Backwards-compat with legacy boolean notify.{type} flags
- 4 HTML-formatted Telegram messages (wo_ready with tracking link, payment,
  appointment, reminder)

Service reminders:
- `reminders:send` artisan command with --slug / --dry-run
- Policy: vehicles whose last closed WO is older than reminder.after_days
  (default 365). Skips if sent within reminder.cooldown_days (default 30).
- Schedule daily 09:00

Filament UI:
- Settings page: Telegram bot token field + "Test bot" + "Set webhook" actions
- Settings page: reminder_after_days + reminder_cooldown_days inputs
- ClientResource: telegram_chat_id readonly badge

Tests (6 new, all pass):
- webhook links client via shared contact
- webhook rejects wrong secret → 401
- dispatcher uses telegram when chat_id present (Http::fake)
- dispatcher falls back to email otherwise
- dispatcher returns false when no channel available
- reminder cron respects 30-day cooldown

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 20:14:17 +00:00
Vasyka a2026f640a Stage 6 — Purchase System: partial receipt + supplier analytics
Schema:
- purchase_items.qty_received (backfilled from `received` boolean)
- purchases.warehouse_id (target warehouse FK)
- supplier_part_prices (price history per supplier/part with purchase ref)
- New status `partial` between ordered and received

Purchase ↔ Warehouse integration:
- Purchase::receiveItem(item, qty, warehouse?) — routes through
  WarehouseService::receive: creates batch + receipt event + supplier price row
- Purchase::receiveAllRemaining(warehouse?) — receives all outstanding lines
- Purchase::recomputeStatus() — auto: ordered → partial → received

Old flat markReceived() removed — every receipt now writes batches + ledger.

Filament:
- Purchase list: progress %, partial badge, warehouse picker on form
- ItemsRelationManager: per-line "Recepționează" with qty + warehouse modal,
  qty_received shown as "X.XX / Y.YY" with colour
- PartResource: new PriceHistoryRelationManager (supplier price history)
- SupplierResource: derived columns onTimeRate / avgDeliveryDays / spend(90d)
  + "Rerating" action

Analytics:
- App\Services\Warehouse\SupplierAnalytics (onTimeRate, avgDeliveryDays,
  spend, count, computedRating)
- `suppliers:rate` artisan command + weekly schedule (Mon 04:00)
- Computed rating: 70% on-time + 20% volume + 10% speed bonus

Tests (6 new, all pass):
- Partial receipt of 3/10 → status=partial + 1 batch + 1 price row
- receiveAllRemaining → status=received with received_at set
- Over-receive throws InvalidArgumentException
- Two partial receipts (4+6) → 2 batches FIFO + status=received
- onTimeRate 50% with 1 on-time + 1 late
- computedRating null when <2 deliveries

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 19:37:12 +00:00
Vasyka 426156fe45 Stage 5.1 — Warehouse ERP: batches + FIFO + reservations + multi-warehouse
Schema:
- warehouses (multi-warehouse, code unique per company, is_default)
- part_batches (lot per receipt, qty_in/qty_remaining, buy_price, FIFO-indexed)
- warehouse_events (immutable ledger: opening/receipt/issue/transfer/adjustment/write_off)
- part_reservations (per-WO allocations from specific batches, active/consumed/released)
- companies.default_warehouse_id + parts.qty_reserved

Backfill: 1 default warehouse + 1 opening batch per existing part per company.

WarehouseService:
- receive / issue (FIFO) / reserve / release / consume / transfer / adjust
- DB::transaction + lockForUpdate on batch rows
- InsufficientStockException with requested + available context
- Auto-syncs parts.qty as aggregate cache (source of truth = sum(qty_remaining))

WO integration:
- WorkOrderPart created/updated → reserve from FIFO batches
- WorkOrderPart deleted → release
- WorkOrder status=done → consume reservations into issue events
- WorkOrder status=cancelled → release reservations

Filament:
- WarehouseResource (CRUD)
- BatchesRelationManager on PartResource (FIFO list with qty_remaining + cost)
- "Recepție" action on parts list → calls WarehouseService::receive
- qty_reserved column added on parts list

Tests (8 new, all pass):
- receipt creates batch + event
- FIFO order verified across 3 batches with different received_at
- InsufficientStockException on over-issue
- Reservations block other reservations but don't deplete on-hand
- WO done consumes; WO cancelled releases
- Batches tenant-isolated
- Transfer between warehouses with weighted-avg cost

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 19:29:19 +00:00
Vasyka edcdba9d53 Stage 3 — WO photos + ETA + QR + public tracking page
- HasMedia (Spatie) on WorkOrder with `photos` collection
- eta_at + tracking_token columns; token auto-generated on create
- Public /t/{token} page — tenant-scoped via subdomain, white-label themed
- QR code SVG via chillerlan/php-qrcode (inline modal + download)
- Filament: SpatieMediaLibraryFileUpload + ETA picker + tracking section
- EditWorkOrder header action "Link client (QR)" modal
- Fix: Auditable::dontSubmitEmptyLogs() → dontLogEmptyChanges() (removed in activitylog)
- Tests: TrackingPageTest (4 pass) covering token gen + cross-tenant isolation

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 19:21:23 +00:00
284 changed files with 23920 additions and 577 deletions
+27 -1
View File
@@ -48,7 +48,10 @@ REDIS_DB=0
# Broadcasting (Reverb — adăugăm la nevoie) # Broadcasting (Reverb — adăugăm la nevoie)
BROADCAST_CONNECTION=log BROADCAST_CONNECTION=log
# Mail — Mailpit intern # Mail — Mailpit intern (dev) sau Resend (prod)
# Dev: lasă smtp + Mailpit. Prod: setează MAIL_MAILER=resend + RESEND_API_KEY,
# înregistrează domeniul în https://resend.com/domains și adaugă DNS-urile
# (TXT + DKIM CNAME-uri) în Cloudflare. Verifică în dashboard înainte de trafic.
MAIL_MAILER=smtp MAIL_MAILER=smtp
MAIL_HOST=autocrm-mailpit MAIL_HOST=autocrm-mailpit
MAIL_PORT=1025 MAIL_PORT=1025
@@ -58,6 +61,29 @@ MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="noreply@service.mir.md" MAIL_FROM_ADDRESS="noreply@service.mir.md"
MAIL_FROM_NAME="${APP_NAME}" MAIL_FROM_NAME="${APP_NAME}"
# Resend API — necesar dacă MAIL_MAILER=resend
RESEND_API_KEY=
# Web Push (VAPID) — generate with: php artisan push:vapid
VAPID_SUBJECT=mailto:admin@service.mir.md
VAPID_PUBLIC_KEY=
VAPID_PRIVATE_KEY=
# Internal health monitor → Telegram alerts every 10 min on DB/cache/storage/backup failure.
# Create a separate bot at @BotFather and a private group; put the bot in it
# and use the group's chat_id (negative number).
HEALTH_ALERT_BOT_TOKEN=
HEALTH_ALERT_CHAT_ID=
# Backblaze B2 (S3-compatible) — offsite backup target for backup:tenants.
# Creează un bucket privat + Application Key cu acces la el. Fără aceste env
# vars, backup-urile rămân doar pe VPS (single point of failure).
B2_KEY=
B2_SECRET=
B2_BUCKET=
B2_REGION=us-west-002
B2_ENDPOINT=https://s3.us-west-002.backblazeb2.com
# Storage — local pentru MVP, S3-compatible mai târziu # Storage — local pentru MVP, S3-compatible mai târziu
FILESYSTEM_DISK=local FILESYSTEM_DISK=local
+4 -1
View File
@@ -41,7 +41,10 @@ RUN install-php-extensions \
opcache \ opcache \
pcntl \ pcntl \
sockets \ sockets \
exif exif \
curl \
mbstring \
gmp
# System tools # System tools
RUN apt-get update && apt-get install -y --no-install-recommends \ RUN apt-get update && apt-get install -y --no-install-recommends \
+224
View File
@@ -0,0 +1,224 @@
<?php
namespace App\Auth;
/**
* Central permission catalog 50 fine-grained permissions across 9 modules.
* Mirrors the catalog in /tmp/service/new/01-TZ-rbac-utilizator-tehnician.md §2.3.
*
* Use these constants instead of magic strings:
* $user->can(Permissions::WORK_ORDERS_CREATE)
*/
class Permissions
{
// Clients
public const CLIENTS_VIEW_ALL = 'clients.view_all';
public const CLIENTS_VIEW_OWN = 'clients.view_own';
public const CLIENTS_CREATE = 'clients.create';
public const CLIENTS_EDIT = 'clients.edit';
public const CLIENTS_DELETE = 'clients.delete';
public const CLIENTS_EXPORT = 'clients.export';
// Vehicles
public const VEHICLES_VIEW_ALL = 'vehicles.view_all';
public const VEHICLES_CREATE = 'vehicles.create';
public const VEHICLES_EDIT = 'vehicles.edit';
public const VEHICLES_DELETE = 'vehicles.delete';
// Work orders (Fișe)
public const WORK_ORDERS_VIEW_ALL = 'work_orders.view_all';
public const WORK_ORDERS_VIEW_OWN_ASSIGNED = 'work_orders.view_own_assigned';
public const WORK_ORDERS_CREATE = 'work_orders.create';
public const WORK_ORDERS_EDIT = 'work_orders.edit';
public const WORK_ORDERS_DELETE = 'work_orders.delete';
public const WORK_ORDERS_CHANGE_STATUS = 'work_orders.change_status';
public const WORK_ORDERS_APPROVE_DISCOUNT_5 = 'work_orders.approve_discount_5';
public const WORK_ORDERS_APPROVE_DISCOUNT_20 = 'work_orders.approve_discount_20';
public const WORK_ORDERS_APPROVE_DISCOUNT_ANY = 'work_orders.approve_discount_any';
public const WORK_ORDERS_PRINT = 'work_orders.print';
// Finance
public const FINANCE_VIEW_OVERVIEW = 'finance.view_overview';
public const FINANCE_VIEW_PL = 'finance.view_pl';
public const FINANCE_CREATE_PAYMENT = 'finance.create_payment';
public const FINANCE_DELETE_PAYMENT = 'finance.delete_payment';
public const FINANCE_CREATE_EXPENSE = 'finance.create_expense';
public const FINANCE_EXPORT = 'finance.export';
// Salaries
public const SALARIES_VIEW_OWN = 'salaries.view_own';
public const SALARIES_VIEW_ALL = 'salaries.view_all';
public const SALARIES_CALCULATE = 'salaries.calculate';
public const SALARIES_MARK_PAID = 'salaries.mark_paid';
// Inventory
public const INVENTORY_VIEW = 'inventory.view';
public const INVENTORY_CREATE_PART = 'inventory.create_part';
public const INVENTORY_EDIT_PART = 'inventory.edit_part';
public const INVENTORY_DELETE_PART = 'inventory.delete_part';
public const INVENTORY_ADJUST_STOCK = 'inventory.adjust_stock';
public const INVENTORY_CREATE_PURCHASE = 'inventory.create_purchase';
public const INVENTORY_RECEIVE_GOODS = 'inventory.receive_goods';
// Suppliers
public const SUPPLIERS_VIEW = 'suppliers.view';
public const SUPPLIERS_EDIT = 'suppliers.edit';
public const SUPPLIERS_DELETE = 'suppliers.delete';
// Admin
public const ADMIN_USERS_VIEW = 'admin.users.view';
public const ADMIN_USERS_MANAGE = 'admin.users.manage';
public const ADMIN_ROLES_MANAGE = 'admin.roles.manage';
public const ADMIN_SETTINGS_EDIT = 'admin.settings.edit';
public const ADMIN_INTEGRATIONS = 'admin.settings.integrations';
public const ADMIN_API_TOKENS_MANAGE = 'admin.api_tokens.manage';
public const ADMIN_AUDIT_LOG_VIEW = 'admin.audit_log.view';
public const ADMIN_BACKUP_DOWNLOAD = 'admin.backup.download';
// AI & Analytics
public const AI_ASSISTANT_USE = 'ai_assistant.use';
public const AI_ASSISTANT_CONFIGURE_KEYS = 'ai_assistant.configure_keys';
public const ANALYTICS_VIEW = 'analytics.view';
/** Full list — used by seeder. */
public static function all(): array
{
return [
self::CLIENTS_VIEW_ALL, self::CLIENTS_VIEW_OWN, self::CLIENTS_CREATE, self::CLIENTS_EDIT,
self::CLIENTS_DELETE, self::CLIENTS_EXPORT,
self::VEHICLES_VIEW_ALL, self::VEHICLES_CREATE, self::VEHICLES_EDIT, self::VEHICLES_DELETE,
self::WORK_ORDERS_VIEW_ALL, self::WORK_ORDERS_VIEW_OWN_ASSIGNED, self::WORK_ORDERS_CREATE,
self::WORK_ORDERS_EDIT, self::WORK_ORDERS_DELETE, self::WORK_ORDERS_CHANGE_STATUS,
self::WORK_ORDERS_APPROVE_DISCOUNT_5, self::WORK_ORDERS_APPROVE_DISCOUNT_20,
self::WORK_ORDERS_APPROVE_DISCOUNT_ANY, self::WORK_ORDERS_PRINT,
self::FINANCE_VIEW_OVERVIEW, self::FINANCE_VIEW_PL, self::FINANCE_CREATE_PAYMENT,
self::FINANCE_DELETE_PAYMENT, self::FINANCE_CREATE_EXPENSE, self::FINANCE_EXPORT,
self::SALARIES_VIEW_OWN, self::SALARIES_VIEW_ALL, self::SALARIES_CALCULATE, self::SALARIES_MARK_PAID,
self::INVENTORY_VIEW, self::INVENTORY_CREATE_PART, self::INVENTORY_EDIT_PART, self::INVENTORY_DELETE_PART,
self::INVENTORY_ADJUST_STOCK, self::INVENTORY_CREATE_PURCHASE, self::INVENTORY_RECEIVE_GOODS,
self::SUPPLIERS_VIEW, self::SUPPLIERS_EDIT, self::SUPPLIERS_DELETE,
self::ADMIN_USERS_VIEW, self::ADMIN_USERS_MANAGE, self::ADMIN_ROLES_MANAGE,
self::ADMIN_SETTINGS_EDIT, self::ADMIN_INTEGRATIONS, self::ADMIN_API_TOKENS_MANAGE,
self::ADMIN_AUDIT_LOG_VIEW, self::ADMIN_BACKUP_DOWNLOAD,
self::AI_ASSISTANT_USE, self::AI_ASSISTANT_CONFIGURE_KEYS, self::ANALYTICS_VIEW,
];
}
/** Groups for UI rendering: module → list of permissions. */
public static function grouped(): array
{
$groups = [];
foreach (self::all() as $slug) {
[$module] = explode('.', $slug, 2);
$groups[$module][] = $slug;
}
return $groups;
}
public static function labels(): array
{
return [
'clients' => 'Clienți',
'vehicles' => 'Mașini',
'work_orders' => 'Fișe lucru',
'finance' => 'Finanțe',
'salaries' => 'Salarii',
'inventory' => 'Stoc',
'suppliers' => 'Furnizori',
'admin' => 'Administrare',
'ai_assistant' => 'AI Assistant',
'analytics' => 'Analitică',
];
}
/**
* Role-permission matrix per TZ §2.4.
* Returns: ['owner' => [perm1, perm2, ...], ...]
*/
public static function roleMatrix(): array
{
$all = self::all();
$matrix = [];
// owner = everything
$matrix['owner'] = $all;
// admin = everything except billing-only stuff (we don't have that yet, so = all)
$matrix['admin'] = $all;
// manager: operations + reporting, no destructive
$matrix['manager'] = [
self::CLIENTS_VIEW_ALL, self::CLIENTS_CREATE, self::CLIENTS_EDIT, self::CLIENTS_EXPORT,
self::VEHICLES_VIEW_ALL, self::VEHICLES_CREATE, self::VEHICLES_EDIT,
self::WORK_ORDERS_VIEW_ALL, self::WORK_ORDERS_VIEW_OWN_ASSIGNED, self::WORK_ORDERS_CREATE,
self::WORK_ORDERS_EDIT, self::WORK_ORDERS_CHANGE_STATUS,
self::WORK_ORDERS_APPROVE_DISCOUNT_5, self::WORK_ORDERS_APPROVE_DISCOUNT_20,
self::WORK_ORDERS_PRINT,
self::FINANCE_VIEW_OVERVIEW, self::FINANCE_CREATE_PAYMENT, self::FINANCE_CREATE_EXPENSE,
self::SALARIES_VIEW_OWN,
self::INVENTORY_VIEW, self::INVENTORY_CREATE_PART, self::INVENTORY_EDIT_PART,
self::INVENTORY_ADJUST_STOCK, self::INVENTORY_CREATE_PURCHASE, self::INVENTORY_RECEIVE_GOODS,
self::SUPPLIERS_VIEW, self::SUPPLIERS_EDIT,
self::AI_ASSISTANT_USE,
self::ANALYTICS_VIEW,
];
// accountant: finance + reporting only
$matrix['accountant'] = [
self::CLIENTS_VIEW_ALL, self::CLIENTS_EXPORT,
self::VEHICLES_VIEW_ALL,
self::WORK_ORDERS_VIEW_ALL,
self::FINANCE_VIEW_OVERVIEW, self::FINANCE_VIEW_PL, self::FINANCE_CREATE_PAYMENT,
self::FINANCE_DELETE_PAYMENT, self::FINANCE_CREATE_EXPENSE, self::FINANCE_EXPORT,
self::SALARIES_VIEW_OWN, self::SALARIES_VIEW_ALL, self::SALARIES_CALCULATE, self::SALARIES_MARK_PAID,
self::INVENTORY_VIEW,
self::SUPPLIERS_VIEW,
self::ANALYTICS_VIEW,
self::AI_ASSISTANT_USE,
];
// receptionist: front-desk operations
$matrix['receptionist'] = [
self::CLIENTS_VIEW_ALL, self::CLIENTS_CREATE, self::CLIENTS_EDIT,
self::VEHICLES_VIEW_ALL, self::VEHICLES_CREATE, self::VEHICLES_EDIT,
self::WORK_ORDERS_VIEW_ALL, self::WORK_ORDERS_CREATE, self::WORK_ORDERS_EDIT,
self::WORK_ORDERS_CHANGE_STATUS, self::WORK_ORDERS_APPROVE_DISCOUNT_5, self::WORK_ORDERS_PRINT,
self::FINANCE_CREATE_PAYMENT,
self::SALARIES_VIEW_OWN,
self::INVENTORY_VIEW,
self::AI_ASSISTANT_USE,
];
// mechanic: only own WOs + inventory view
$matrix['mechanic'] = [
self::WORK_ORDERS_VIEW_OWN_ASSIGNED, self::WORK_ORDERS_CHANGE_STATUS,
self::INVENTORY_VIEW,
self::SALARIES_VIEW_OWN,
];
// viewer: read-only
$matrix['viewer'] = [
self::CLIENTS_VIEW_ALL,
self::VEHICLES_VIEW_ALL,
self::WORK_ORDERS_VIEW_ALL,
self::INVENTORY_VIEW,
self::SUPPLIERS_VIEW,
self::AI_ASSISTANT_USE,
];
return $matrix;
}
public static function roleLabels(): array
{
return [
'owner' => 'Proprietar',
'admin' => 'Administrator',
'manager' => 'Manager',
'accountant' => 'Contabil',
'receptionist' => 'Recepție',
'mechanic' => 'Mecanic',
'viewer' => 'Vizitator',
];
}
}
@@ -49,6 +49,9 @@ class BackupAllTenantsCommand extends Command
$size = round(filesize($dest) / 1024, 1); $size = round(filesize($dest) / 1024, 1);
$this->info("{$company->slug}{$size}KB"); $this->info("{$company->slug}{$size}KB");
// Offsite copy to B2 (if configured) — disk lazily resolved.
$this->uploadOffsite($dest, "{$date}/{$company->slug}.zip");
$ok++; $ok++;
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->error("{$company->slug}: {$e->getMessage()}"); $this->error("{$company->slug}: {$e->getMessage()}");
@@ -65,6 +68,20 @@ class BackupAllTenantsCommand extends Command
return $failed > 0 ? self::FAILURE : self::SUCCESS; return $failed > 0 ? self::FAILURE : self::SUCCESS;
} }
/** Upload one backup zip to the offsite B2 disk if env is configured. */
private function uploadOffsite(string $localPath, string $remoteKey): void
{
if (! env('B2_KEY') || ! env('B2_BUCKET')) return;
try {
$stream = fopen($localPath, 'rb');
\Illuminate\Support\Facades\Storage::disk('b2')->put($remoteKey, $stream);
if (is_resource($stream)) fclose($stream);
$this->line(" ↑ offsite: {$remoteKey}");
} catch (\Throwable $e) {
$this->warn(" ✗ offsite upload failed: " . substr($e->getMessage(), 0, 120));
}
}
private function cleanupOld(int $keep): void private function cleanupOld(int $keep): void
{ {
$backupsDir = storage_path('app/backups'); $backupsDir = storage_path('app/backups');
@@ -0,0 +1,28 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Minishlink\WebPush\VAPID;
class GenerateVapidKeysCommand extends Command
{
protected $signature = 'push:vapid';
protected $description = 'Generate a VAPID keypair for Web Push and print the .env lines.';
public function handle(): int
{
$keys = VAPID::createVapidKeys();
$this->info('VAPID keys generated. Add these to your .env:');
$this->newLine();
$this->line('VAPID_SUBJECT=mailto:admin@service.mir.md');
$this->line('VAPID_PUBLIC_KEY=' . $keys['publicKey']);
$this->line('VAPID_PRIVATE_KEY=' . $keys['privateKey']);
$this->newLine();
$this->warn('Keep the private key secret. Re-generating invalidates existing subscriptions.');
return self::SUCCESS;
}
}
+122
View File
@@ -0,0 +1,122 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
/**
* Lightweight self-health probe. Runs on the box, so it can't catch total
* outages pair with external uptime monitoring (UptimeRobot, Better Stack)
* pointing at /up.
*
* Tests:
* - DB connectivity (central connection: SELECT 1)
* - Cache write/read (Redis if configured)
* - Public storage disk write/read
* - Most recent tenant backup age (warn if > 30h)
*
* On any failure, pushes a short Telegram alert to HEALTH_ALERT_CHAT_ID via
* HEALTH_ALERT_BOT_TOKEN (env). Dedups identical failures within 30 minutes
* via cache to avoid spamming on each cron tick.
*/
class HealthCheckCommand extends Command
{
protected $signature = 'health:check
{--silent : Do not echo OK output (for cron)}';
protected $description = 'Probe DB / cache / storage / backup freshness; alert via Telegram on failure.';
public function handle(): int
{
$issues = [];
try {
DB::connection()->select('SELECT 1');
} catch (\Throwable $e) {
$issues[] = 'DB: ' . substr($e->getMessage(), 0, 120);
}
try {
$stamp = 'hc:' . (string) microtime(true);
Cache::put('health:probe', $stamp, 30);
if (Cache::get('health:probe') !== $stamp) {
$issues[] = 'Cache: write/read mismatch';
}
} catch (\Throwable $e) {
$issues[] = 'Cache: ' . substr($e->getMessage(), 0, 120);
}
try {
$f = 'health/' . md5((string) microtime(true)) . '.txt';
Storage::disk('public')->put($f, 'ok');
if (Storage::disk('public')->get($f) !== 'ok') {
$issues[] = 'Storage: write/read mismatch';
}
Storage::disk('public')->delete($f);
} catch (\Throwable $e) {
$issues[] = 'Storage: ' . substr($e->getMessage(), 0, 120);
}
try {
$newest = collect(Storage::disk('local')->allFiles('backups'))
->map(fn ($f) => Storage::disk('local')->lastModified($f))
->max();
if ($newest && (time() - $newest) > 30 * 3600) {
$age = round((time() - $newest) / 3600, 1);
$issues[] = "Backup: cel mai recent are {$age}h (expectat <30h).";
}
} catch (\Throwable $e) {
// Backup folder might be empty on a fresh install — not an alert.
}
if (empty($issues)) {
if (! $this->option('silent')) {
$this->info('Health OK · ' . now()->toIso8601String());
}
return self::SUCCESS;
}
$signature = md5(implode('|', $issues));
$dedupKey = "health:alert:{$signature}";
if (! Cache::has($dedupKey)) {
$this->pushTelegramAlert($issues);
Cache::put($dedupKey, 1, 30 * 60); // 30-min cooldown per fingerprint
}
foreach ($issues as $i) $this->error($i);
return self::FAILURE;
}
private function pushTelegramAlert(array $issues): void
{
$bot = env('HEALTH_ALERT_BOT_TOKEN');
$chat = env('HEALTH_ALERT_CHAT_ID');
if (! $bot || ! $chat) {
Log::warning('health:check failed but HEALTH_ALERT_BOT_TOKEN/CHAT_ID not set', [
'issues' => $issues,
]);
return;
}
$body = "🚨 <b>AutoCRM health alert</b>\n"
. implode("\n", array_map(fn ($i) => '• ' . htmlspecialchars($i), $issues))
. "\n\n" . config('app.url', 'service.mir.md');
try {
Http::asJson()
->timeout(10)
->post("https://api.telegram.org/bot{$bot}/sendMessage", [
'chat_id' => $chat,
'text' => $body,
'parse_mode' => 'HTML',
]);
} catch (\Throwable $e) {
Log::warning('health:check telegram alert failed', ['err' => $e->getMessage()]);
}
}
}
@@ -0,0 +1,51 @@
<?php
namespace App\Console\Commands;
use App\Models\Central\Company;
use App\Models\Tenant\Supplier;
use App\Services\Warehouse\SupplierAnalytics;
use App\Tenancy\TenantManager;
use Illuminate\Console\Command;
class RateSuppliersCommand extends Command
{
protected $signature = 'suppliers:rate
{--days=90 : Look-back window in days}
{--slug= : Only one tenant by slug}';
protected $description = 'Recompute auto-rating for every supplier based on on-time deliveries, speed and volume.';
public function handle(SupplierAnalytics $analytics): int
{
$query = Company::query()->where('status', '!=', 'archived');
if ($slug = $this->option('slug')) {
$query->where('slug', $slug);
}
$companies = $query->get();
$days = (int) $this->option('days');
$totalUpdated = 0;
foreach ($companies as $company) {
app(TenantManager::class)->setCurrent($company);
$suppliers = Supplier::where('is_active', true)->get();
$changed = 0;
foreach ($suppliers as $supplier) {
$score = $analytics->computedRating($supplier, $days);
if ($score !== null && $score !== (int) $supplier->rating) {
$supplier->rating = $score;
$supplier->saveQuietly();
$changed++;
}
}
$this->info(sprintf('[%s] suppliers rated, %d updated', $company->slug, $changed));
$totalUpdated += $changed;
}
$this->info("Total suppliers updated: {$totalUpdated}");
return self::SUCCESS;
}
}
@@ -0,0 +1,88 @@
<?php
namespace App\Console\Commands;
use App\Models\Central\Company;
use App\Models\Tenant\ServiceReminderSent;
use App\Models\Tenant\Vehicle;
use App\Models\Tenant\WorkOrder;
use App\Services\NotificationDispatcher;
use App\Tenancy\TenantManager;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
class SendServiceRemindersCommand extends Command
{
protected $signature = 'reminders:send
{--slug= : Only one tenant by slug}
{--dry-run : Show candidates without sending}';
protected $description = 'Scan vehicles for due service reminders and send via configured channels.';
public function handle(NotificationDispatcher $dispatcher): int
{
$query = Company::query()->where('status', '!=', 'archived');
if ($slug = $this->option('slug')) {
$query->where('slug', $slug);
}
$companies = $query->get();
$dry = (bool) $this->option('dry-run');
$totalSent = 0;
foreach ($companies as $company) {
app(TenantManager::class)->setCurrent($company);
$settings = (array) ($company->settings ?? []);
$reminderDays = (int) data_get($settings, 'reminder.after_days', 365);
$cooldownDays = (int) data_get($settings, 'reminder.cooldown_days', 30);
$cutoff = Carbon::now()->subDays($reminderDays);
$cooldown = Carbon::now()->subDays($cooldownDays);
// Pick vehicles whose last *closed* WO was before $cutoff (or never).
$vehicles = Vehicle::with('client')
->whereHas('client', fn ($q) => $q->where('status', 'active'))
->get();
$sentThisTenant = 0;
foreach ($vehicles as $v) {
$lastClosedAt = WorkOrder::where('vehicle_id', $v->id)
->whereNotNull('closed_at')
->max('closed_at');
if (! $lastClosedAt) continue; // never serviced — skip (handled by other logic)
if (Carbon::parse($lastClosedAt)->gt($cutoff)) continue;
$recent = ServiceReminderSent::where('vehicle_id', $v->id)
->where('sent_at', '>=', $cooldown)
->exists();
if ($recent) continue;
if ($dry) {
$this->line(" - [{$company->slug}] Vehicle #{$v->id} {$v->plate} last serviced {$lastClosedAt}");
continue;
}
$ok = $dispatcher->serviceReminder($v, 'general');
if ($ok) {
ServiceReminderSent::create([
'company_id' => $company->id,
'vehicle_id' => $v->id,
'client_id' => $v->client_id,
'channel' => $v->client?->telegram_chat_id ? 'telegram' : 'email',
'type' => 'general',
'sent_at' => now(),
]);
$sentThisTenant++;
}
}
$this->info(sprintf('[%s] reminders sent: %d', $company->slug, $sentThisTenant));
$totalSent += $sentThisTenant;
}
$this->info("Total reminders sent: {$totalSent}" . ($dry ? ' (dry run)' : ''));
return self::SUCCESS;
}
}
@@ -0,0 +1,114 @@
<?php
namespace App\Console\Commands;
use App\Models\Central\Company;
use App\Models\Tenant\ServiceReminderSent;
use App\Models\Tenant\TireSet;
use App\Services\NotificationDispatcher;
use App\Tenancy\TenantManager;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
/**
* Twice-a-year window reminder for clients to swap seasonal tires.
* - Around March 1 (Feb 15 Mar 15): notify clients with WINTER sets still
* in storage (time to swap to summer).
* - Around October 1 (Sep 15 Oct 15): notify clients with SUMMER sets still
* in storage (time to swap to winter).
*
* Dedup via service_reminders_sent (type='tire_swap', per client+set, 60-day
* cooldown effectively once per window).
*/
class SendTireSeasonalRemindersCommand extends Command
{
protected $signature = 'tires:remind-seasonal
{--slug= : Only one tenant by slug}
{--force : Send even outside the swap window}
{--dry-run : Show candidates without sending}';
protected $description = 'Send seasonal tire-swap reminders during Feb-Mar / Sep-Oct windows.';
public function handle(NotificationDispatcher $dispatcher): int
{
$window = $this->windowFor(today());
$force = (bool) $this->option('force');
$dry = (bool) $this->option('dry-run');
if (! $window && ! $force) {
$this->info('Outside swap window. Use --force to run anyway. Today: ' . today()->toDateString());
return self::SUCCESS;
}
$targetSeason = $window['season'] ?? 'winter'; // season of stored sets we want to notify
$query = Company::query()->where('status', '!=', 'archived');
if ($slug = $this->option('slug')) $query->where('slug', $slug);
$companies = $query->get();
$totalSent = 0;
$cooldown = today()->subDays(60);
foreach ($companies as $company) {
app(TenantManager::class)->setCurrent($company);
// Sets currently in storage whose season matches the window target.
$sets = TireSet::with(['client', 'vehicle', 'storage'])
->where('season', $targetSeason)
->whereHas('storage', fn ($s) => $s->where('status', 'stored'))
->get()
->filter(fn (TireSet $s) => $s->client && $s->client->status === 'active');
$sentThisTenant = 0;
foreach ($sets as $set) {
$recent = ServiceReminderSent::where('type', 'tire_swap')
->where('client_id', $set->client_id)
->where('sent_at', '>=', $cooldown)
->exists();
if ($recent) continue;
if ($dry) {
$this->line(sprintf(' - [%s] set #%d %s · client %s · loc %s',
$company->slug, $set->id, $set->sizeLabel(),
$set->client?->name ?? '—',
$set->currentStorage()?->location ?? '—'));
continue;
}
$ok = $dispatcher->tireSeasonalSwap($set);
if ($ok) {
ServiceReminderSent::create([
'company_id' => $company->id,
'vehicle_id' => $set->vehicle_id,
'client_id' => $set->client_id,
'channel' => $set->client?->telegram_chat_id ? 'telegram' : 'email',
'type' => 'tire_swap',
'sent_at' => now(),
]);
$sentThisTenant++;
}
}
$this->info(sprintf('[%s] tire-swap reminders sent: %d', $company->slug, $sentThisTenant));
$totalSent += $sentThisTenant;
}
$this->info("Total tire-swap reminders sent: {$totalSent}" . ($dry ? ' (dry run)' : ''));
return self::SUCCESS;
}
/** Returns ['season' => 'winter'|'summer'] if today is in a swap window, else null. */
private function windowFor(Carbon $today): ?array
{
// Feb 15 Mar 15 → notify WINTER sets (swap to summer).
$springStart = Carbon::create($today->year, 2, 15);
$springEnd = Carbon::create($today->year, 3, 15);
if ($today->between($springStart, $springEnd)) return ['season' => 'winter'];
// Sep 15 Oct 15 → notify SUMMER sets (swap to winter).
$autumnStart = Carbon::create($today->year, 9, 15);
$autumnEnd = Carbon::create($today->year, 10, 15);
if ($today->between($autumnStart, $autumnEnd)) return ['season' => 'summer'];
return null;
}
}
@@ -27,7 +27,27 @@ class ViewCompany extends Page
public function mount(int|string $record): void public function mount(int|string $record): void
{ {
$this->record = Company::with(['plan', 'subscriptions' => fn ($q) => $q->latest('period_end')->limit(10)])->findOrFail($record); // The {record} route param may arrive as a scalar id, an Eloquent model,
// or (via Livewire's typed-property hydration) a JSON-encoded model.
// Normalize all of these down to the integer primary key.
$key = $this->resolveRecordKey($record);
$this->record = Company::with(['plan', 'subscriptions' => fn ($q) => $q->latest('period_end')->limit(10)])
->findOrFail($key);
}
private function resolveRecordKey(mixed $record): int|string
{
if ($record instanceof Company) {
return $record->getKey();
}
if (is_string($record) && str_starts_with(ltrim($record), '{')) {
$decoded = json_decode($record, true);
if (is_array($decoded) && isset($decoded['id'])) {
return $decoded['id'];
}
}
return $record;
} }
public function getTitle(): string public function getTitle(): string
@@ -50,8 +70,8 @@ class ViewCompany extends Page
'work_orders_open' => WorkOrder::whereNotIn('status', ['done', 'cancelled'])->count(), 'work_orders_open' => WorkOrder::whereNotIn('status', ['done', 'cancelled'])->count(),
'parts' => Part::count(), 'parts' => Part::count(),
'parts_low_stock' => Part::where('is_active', true) 'parts_low_stock' => Part::where('is_active', true)
->whereColumn('stock', '<=', 'low_stock_threshold') ->whereColumn('qty', '<=', 'min_qty')
->where('stock', '>', 0) ->where('qty', '>', 0)
->count(), ->count(),
'revenue_this_month' => (float) Payment::whereYear('paid_at', date('Y')) 'revenue_this_month' => (float) Payment::whereYear('paid_at', date('Y'))
->whereMonth('paid_at', date('m'))->sum('amount'), ->whereMonth('paid_at', date('m'))->sum('amount'),
@@ -60,6 +60,11 @@ class AiAssistant extends Page
->get(); ->get();
} }
public function getUsage(): array
{
return app(AiAssistantService::class)->monthlyUsage();
}
public function newChat(): void public function newChat(): void
{ {
$chat = AiChat::create([ $chat = AiChat::create([
+466 -113
View File
@@ -8,11 +8,8 @@ use App\Models\Tenant\Post;
use App\Models\Tenant\User; use App\Models\Tenant\User;
use App\Models\Tenant\Vehicle; use App\Models\Tenant\Vehicle;
use Carbon\Carbon; use Carbon\Carbon;
use Filament\Forms;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Filament\Pages\Page; use Filament\Pages\Page;
use Filament\Schemas;
use Filament\Schemas\Schema;
class CalendarBoard extends Page class CalendarBoard extends Page
{ {
@@ -24,147 +21,503 @@ class CalendarBoard extends Page
protected static ?int $navigationSort = 8; protected static ?int $navigationSort = 8;
protected static ?string $title = 'Calendar'; protected static ?string $title = 'Calendar vizual';
protected string $view = 'filament.tenant.pages.calendar'; protected string $view = 'filament.tenant.pages.calendar';
public ?array $createData = []; public string $weekStart; // 'Y-m-d' (Monday)
public string $groupBy = 'post'; // 'post' | 'master'
public string $viewMode = 'week'; // day | week | month | list
public string $customStart = ''; // when viewMode='custom'
public string $customEnd = '';
public ?int $masterFilter = null;
public string $statusFilter = 'all'; // all | confirmed | unconfirmed | in_work
public bool $showNewForm = false;
public bool $showNewPostForm = false;
public ?int $openEventId = null;
public ?int $renamingPostId = null;
public string $renamingPostName = '';
public ?int $renamingPostMasterId = null;
public ?array $editData = []; public array $newAppt = [];
public array $newPost = ['name' => '', 'color' => '#3b82f6', 'hours_per_day' => 10, 'description' => ''];
public ?int $editId = null; public function getMaxContentWidth(): \Filament\Support\Enums\Width
/** Register all forms used by this page (Filament v5 multi-form pattern). */
protected function getForms(): array
{ {
return ['createForm']; return \Filament\Support\Enums\Width::Full;
} }
public function getEvents(string $start, string $end): array public function getHeading(): string { return ''; }
public function getSubheading(): ?string { return null; }
public function mount(): void
{ {
return Appointment::with(['client:id,name', 'vehicle:id,plate,make,model', 'master:id,name,color', 'post:id,name,color']) $this->weekStart = Carbon::now()->startOfWeek()->toDateString();
->whereBetween('date', [$start, $end]) }
->get()
->map(fn (Appointment $a) => [ public function shiftWeek(int $deltaWeeks): void
'id' => $a->id, {
'title' => trim($a->title ?: ($a->client?->name ?? '—')), // delta semantic depends on view mode
'start' => $a->date->format('Y-m-d') . 'T' . ($a->time_start ?? '08:00:00'), $current = Carbon::parse($this->weekStart);
'end' => $a->date->format('Y-m-d') . 'T' . ($a->time_end ?? '09:00:00'), $this->weekStart = match ($this->viewMode) {
'backgroundColor' => $a->color ?: ($a->master?->color ?? '#3b82f6'), 'day' => $current->addDays($deltaWeeks)->toDateString(),
'borderColor' => $a->color ?: ($a->master?->color ?? '#3b82f6'), 'month' => $current->addMonths($deltaWeeks)->startOfMonth()->toDateString(),
'extendedProps' => [ default => $current->addWeeks($deltaWeeks)->toDateString(),
'client' => $a->client?->name, };
'vehicle' => trim(($a->vehicle?->make ?? '') . ' ' . ($a->vehicle?->model ?? '')), }
'plate' => $a->vehicle?->plate,
'master' => $a->master?->name, public function setWeekToday(): void
'post' => $a->post?->name, {
'status' => $a->status, $this->weekStart = match ($this->viewMode) {
'notes' => $a->notes, 'day' => Carbon::today()->toDateString(),
], 'month' => Carbon::now()->startOfMonth()->toDateString(),
default => Carbon::now()->startOfWeek()->toDateString(),
};
}
public function setGroupBy(string $g): void
{
$this->groupBy = in_array($g, ['post', 'master'], true) ? $g : 'post';
}
public function setViewMode(string $m): void
{
if (! in_array($m, ['day', 'week', 'month', 'list', 'custom'], true)) return;
$this->viewMode = $m;
// Snap weekStart to a sensible anchor for the new view
$this->weekStart = match ($m) {
'day' => Carbon::today()->toDateString(),
'month' => Carbon::parse($this->weekStart)->startOfMonth()->toDateString(),
'custom' => $this->customStart ?: Carbon::today()->toDateString(),
default => Carbon::parse($this->weekStart)->startOfWeek()->toDateString(),
};
}
public function setStatusFilter(string $s): void
{
$this->statusFilter = in_array($s, ['all', 'confirmed', 'unconfirmed', 'in_work'], true) ? $s : 'all';
}
public function setMasterFilter($id): void
{
$this->masterFilter = $id ? (int) $id : null;
}
/** Build day headers — count varies by view mode. */
public function getDays(): array
{
$today = Carbon::today()->toDateString();
$names = ['Luni', 'Marți', 'Miercuri', 'Joi', 'Vineri', 'Sâmbătă', 'Duminică'];
$start = Carbon::parse($this->weekStart);
$count = match ($this->viewMode) {
'day' => 1,
'month' => $start->daysInMonth,
'custom' => $this->customStart && $this->customEnd
? max(1, min(31, Carbon::parse($this->customStart)->diffInDays(Carbon::parse($this->customEnd)) + 1))
: 7,
default => 7,
};
if ($this->viewMode === 'month') {
$start = Carbon::parse($this->weekStart)->startOfMonth();
}
if ($this->viewMode === 'custom' && $this->customStart) {
$start = Carbon::parse($this->customStart);
}
$days = [];
for ($i = 0; $i < $count; $i++) {
$d = $start->copy()->addDays($i);
$dow = (int) $d->dayOfWeek; // 0=Sunday, 6=Saturday in Carbon
$isoDow = (int) $d->isoWeekday(); // 1=Mon..7=Sun
$days[] = [
'date' => $d->toDateString(),
'label' => $d->format('d.m'),
'name' => $names[($isoDow - 1) % 7],
'is_today' => $d->toDateString() === $today,
'is_weekend' => $isoDow >= 6,
'is_closed' => $isoDow === 7, // Sunday default
];
}
return $days;
}
/** Rows: either Posts or active Masters depending on $groupBy. */
public function getRows(): array
{
if ($this->groupBy === 'master') {
$rows = User::query()
->where('status', 'active')
->whereNotNull('role')
->where(function ($q) { $q->where('role', 'master')->orWhere('role', 'mecanic')->orWhereNull('role'); })
->orderBy('name')
->get(['id', 'name', 'color', 'specialization'])
->map(fn ($u) => [
'kind' => 'master',
'id' => $u->id,
'name' => $u->name,
'color' => $u->color ?: '#3b82f6',
'meta' => $u->specialization ?: '8h/zi',
'capacity_hours' => 8.0,
])->all();
// Always include "Fără maistru" row at the bottom
$rows[] = ['kind' => 'master', 'id' => 0, 'name' => 'Fără maistru', 'color' => '#94a3b8', 'meta' => '—', 'capacity_hours' => 0];
return $rows;
}
$posts = Post::where('is_active', true)->orderBy('sort_order')->orderBy('name')->get();
// Fallback: synthesize a default post if none configured yet
if ($posts->isEmpty()) {
return [
['kind' => 'post', 'id' => 0, 'name' => 'Pod 1 (default)', 'color' => '#3b82f6', 'meta' => '10h/zi', 'capacity_hours' => 10.0],
];
}
return $posts->map(fn ($p) => [
'kind' => 'post',
'id' => $p->id,
'name' => $p->name,
'color' => $p->color ?: '#3b82f6',
'meta' => ($p->hours_per_day ? $p->hours_per_day . 'h/zi' : '10h/zi') . ($p->description ? ' · ' . $p->description : ''),
'capacity_hours' => (float) ($p->hours_per_day ?: 10),
])->all(); ])->all();
} }
/** Drag-drop reschedule. */ /** Returns map [rowId][date] => ['events'=>[], 'load_hours'=>float, 'capacity'=>float] */
public function moveEvent(int $id, string $start, string $end): void public function getMatrix(): array
{
$start = $this->weekStart;
$end = Carbon::parse($this->weekStart)->addDays(6)->toDateString();
$q = Appointment::with(['client:id,name', 'vehicle:id,plate,make,model', 'master:id,name,color', 'post:id,name,color'])
->whereBetween('date', [$start, $end]);
if ($this->masterFilter) {
$q->where('master_id', $this->masterFilter);
}
if ($this->statusFilter === 'confirmed') {
$q->where('status', 'arrived');
} elseif ($this->statusFilter === 'unconfirmed') {
$q->where('status', 'scheduled');
} elseif ($this->statusFilter === 'in_work') {
$q->where('status', 'in_work');
}
$events = $q->get();
$rows = $this->getRows();
$days = $this->getDays();
$matrix = [];
foreach ($rows as $row) {
foreach ($days as $day) {
$matrix[$row['id']][$day['date']] = ['events' => [], 'load_hours' => 0, 'capacity' => $row['capacity_hours']];
}
}
foreach ($events as $a) {
$rowId = $this->groupBy === 'post' ? ($a->post_id ?: ($rows[0]['id'] ?? 0)) : ($a->master_id ?: 0);
if (! isset($matrix[$rowId])) continue;
$date = $a->date->toDateString();
if (! isset($matrix[$rowId][$date])) continue;
$hours = $this->calcHours($a->time_start, $a->time_end);
$matrix[$rowId][$date]['load_hours'] += $hours;
$matrix[$rowId][$date]['events'][] = [
'id' => $a->id,
'title' => $a->title ?: ($a->client?->name ?? '—'),
'client_name' => $a->client?->name,
'vehicle' => trim(($a->vehicle?->make ?? '') . ' ' . ($a->vehicle?->model ?? '')),
'plate' => $a->vehicle?->plate,
'master_name' => $a->master?->name,
'master_initial' => $a->master ? strtoupper(mb_substr($a->master->name, 0, 1)) . '.' : '',
'time' => substr($a->time_start ?? '', 0, 5) . '' . substr($a->time_end ?? '', 0, 5),
'color' => $a->color ?: ($a->master?->color ?? '#3b82f6'),
'status' => $a->status,
];
}
return $matrix;
}
public function getStats(): array
{
$start = $this->weekStart;
$end = Carbon::parse($this->weekStart)->addDays(6)->toDateString();
$events = Appointment::whereBetween('date', [$start, $end])->get();
$rows = $this->getRows();
// Capacity = sum(rows.capacity_hours) * 6 working days
$capacity = 0;
foreach ($rows as $r) { $capacity += $r['capacity_hours'] * 6; }
$scheduled = 0;
foreach ($events as $a) {
$scheduled += $this->calcHours($a->time_start, $a->time_end);
}
$open = $events->whereNotIn('status', ['done', 'cancelled', 'no_show'])->count();
$confirmed = $events->whereIn('status', ['arrived', 'in_work', 'done'])->count();
$noShowAlert = $events
->where('status', 'scheduled')
->filter(fn ($a) => Carbon::parse($a->date->toDateString() . ' ' . ($a->time_start ?? '08:00'))->diffInHours(now(), false) > -24)
->count();
return [
'scheduled_hours' => round($scheduled, 1),
'capacity_hours' => round($capacity, 1),
'utilization_pct' => $capacity > 0 ? (int) round(100 * $scheduled / $capacity) : 0,
'open_count' => $open,
'confirmed_count' => $confirmed,
'total_count' => $events->count(),
'confirmation_rate_pct' => $events->count() > 0 ? (int) round(100 * $confirmed / $events->count()) : 0,
'no_show_alert' => $noShowAlert,
];
}
// ============== mutations ==============
public function moveEvent(int $id, int $toRowId, string $toDate): void
{ {
$a = Appointment::find($id); $a = Appointment::find($id);
if (! $a) return; if (! $a) return;
if ($this->groupBy === 'post') {
[$startDate, $startTime] = $this->splitIso($start); $a->post_id = $toRowId ?: null;
[, $endTime] = $this->splitIso($end); } else {
$a->master_id = $toRowId ?: null;
$a->update([ }
'date' => $startDate, $a->date = $toDate;
'time_start' => $startTime, $a->save();
'time_end' => $endTime, Notification::make()->title('Programare mutată')->body($a->title . ' → ' . $toDate)->success()->send();
]);
Notification::make()
->title('Programare mutată')
->body($a->title . ' → ' . $startDate . ' ' . substr($startTime, 0, 5))
->success()->send();
$this->dispatch('events-changed');
} }
public function quickCreate(string $start, string $end): void public function openEvent(int $id): void
{ {
$this->createData = [ $this->openEventId = $id;
'date' => substr($start, 0, 10), $this->showNewForm = false;
'time_start' => substr($start, 11, 5), }
'time_end' => substr($end, 11, 5),
public function getOpenEvent(): ?array
{
if (! $this->openEventId) return null;
$a = Appointment::with(['client', 'vehicle', 'master', 'post'])->find($this->openEventId);
if (! $a) return null;
return [
'id' => $a->id,
'title' => $a->title,
'status' => $a->status,
'date' => $a->date->format('d.m.Y'),
'time' => substr($a->time_start ?? '', 0, 5) . '' . substr($a->time_end ?? '', 0, 5),
'client_name' => $a->client?->name,
'client_phone' => $a->client?->phone,
'vehicle' => trim(($a->vehicle?->make ?? '') . ' ' . ($a->vehicle?->model ?? '')),
'plate' => $a->vehicle?->plate,
'master_name' => $a->master?->name,
'post_name' => $a->post?->name,
'notes' => $a->notes,
'deal_id' => $a->deal_id,
]; ];
$this->createForm->fill($this->createData);
$this->dispatch('open-create-modal');
} }
public function createForm(Schema $schema): Schema public function closeEvent(): void
{ {
return $schema->components([ $this->openEventId = null;
Forms\Components\Hidden::make('date'),
Forms\Components\TextInput::make('title')->label('Subiect')->required(),
Schemas\Components\Section::make('Când')
->columns(2)
->schema([
Forms\Components\TimePicker::make('time_start')->label('De la')->seconds(false)->required(),
Forms\Components\TimePicker::make('time_end')->label('Până la')->seconds(false)->required(),
]),
Schemas\Components\Section::make('Cine')
->columns(2)
->schema([
Forms\Components\Select::make('client_id')->label('Client')
->options(fn () => Client::pluck('name', 'id'))
->searchable()
->live(),
Forms\Components\Select::make('vehicle_id')->label('Auto')
->options(fn (\Filament\Schemas\Components\Utilities\Get $get) => $get('client_id')
? Vehicle::where('client_id', $get('client_id'))->pluck('plate', 'id')
: []),
Forms\Components\Select::make('master_id')->label('Maistru')
->options(fn () => User::where('status', 'active')->pluck('name', 'id'))
->searchable(),
Forms\Components\Select::make('post_id')->label('Pod')
->options(fn () => Post::where('is_active', true)->pluck('name', 'id'))
->searchable(),
]),
Forms\Components\Textarea::make('notes')->rows(2),
])->statePath('createData');
}
public function saveCreate(): void
{
$data = $this->createForm->getState();
Appointment::create([
'date' => $data['date'],
'time_start' => $data['time_start'],
'time_end' => $data['time_end'],
'title' => $data['title'],
'client_id' => $data['client_id'] ?? null,
'vehicle_id' => $data['vehicle_id'] ?? null,
'master_id' => $data['master_id'] ?? null,
'post_id' => $data['post_id'] ?? null,
'notes' => $data['notes'] ?? null,
'status' => 'scheduled',
]);
$this->createData = [];
Notification::make()->title('Programare adăugată')->success()->send();
$this->dispatch('close-create-modal');
$this->dispatch('events-changed');
} }
public function deleteEvent(int $id): void public function deleteEvent(int $id): void
{ {
Appointment::where('id', $id)->delete(); Appointment::where('id', $id)->delete();
$this->openEventId = null;
Notification::make()->title('Programare ștearsă')->success()->send(); Notification::make()->title('Programare ștearsă')->success()->send();
$this->dispatch('events-changed');
} }
protected function splitIso(string $iso): array public function openNewForm(int $rowId = 0, string $date = ''): void
{ {
// "2026-05-07T10:30:00" → ["2026-05-07", "10:30:00"] $masterId = $this->groupBy === 'master' && $rowId ? $rowId : null;
if (str_contains($iso, 'T')) { $postId = $this->groupBy === 'post' && $rowId ? $rowId : null;
return explode('T', $iso);
// Auto-fill default master from post if one is set
if ($postId && ! $masterId) {
$post = Post::find($postId);
if ($post && $post->default_master_id) {
$masterId = $post->default_master_id;
} }
return [substr($iso, 0, 10), substr($iso, 11) ?: '08:00:00']; }
$this->newAppt = [
'date' => $date ?: today()->toDateString(),
'time_start' => '09:00',
'time_end' => '10:00',
'title' => '',
'client_id' => null,
'vehicle_id' => null,
'master_id' => $masterId,
'post_id' => $postId,
'notes' => '',
];
$this->showNewForm = true;
$this->openEventId = null;
}
/** Quick-add post from calendar toolbar. */
public function openNewPostForm(): void
{
$this->showNewPostForm = true;
$this->newPost = ['name' => '', 'color' => '#3b82f6', 'hours_per_day' => 10, 'description' => ''];
}
public function createPost(): void
{
$name = trim($this->newPost['name'] ?? '');
if ($name === '') {
Notification::make()->title('Numele este obligatoriu')->danger()->send();
return;
}
Post::create([
'name' => $name,
'color' => $this->newPost['color'] ?? '#3b82f6',
'hours_per_day' => (float) ($this->newPost['hours_per_day'] ?? 10),
'description' => trim($this->newPost['description'] ?? '') ?: null,
'is_active' => true,
'sort_order' => 100,
]);
$this->showNewPostForm = false;
Notification::make()->title('Spațiu de lucru adăugat')->success()->send();
}
/** Inline rename + reassign default master from row label click. */
public function openRenamePost(int $postId): void
{
$post = Post::find($postId);
if (! $post) return;
$this->renamingPostId = $postId;
$this->renamingPostName = $post->name;
$this->renamingPostMasterId = $post->default_master_id;
}
public function saveRenamePost(): void
{
if (! $this->renamingPostId) return;
$post = Post::find($this->renamingPostId);
if (! $post) return;
$name = trim($this->renamingPostName);
if ($name === '') return;
$post->update([
'name' => $name,
'default_master_id' => $this->renamingPostMasterId ?: null,
]);
$this->renamingPostId = null;
Notification::make()->title('Post actualizat')->success()->send();
}
/** Generate PDF for all appointments in the visible period. */
public function exportPdf()
{
$days = $this->getDays();
$firstDate = $days[0]['date'] ?? today()->toDateString();
$lastDate = end($days)['date'] ?? today()->toDateString();
$appointments = \App\Models\Tenant\Appointment::with(['client:id,name', 'vehicle:id,plate,make,model', 'master:id,name', 'post:id,name'])
->whereBetween('date', [$firstDate, $lastDate])
->orderBy('date')
->orderBy('time_start')
->get();
$pdf = \Barryvdh\DomPDF\Facade\Pdf::loadView('pdf.appointments', [
'appointments' => $appointments->groupBy(fn ($a) => $a->date->toDateString()),
'periodLabel' => Carbon::parse($firstDate)->format('d.m.Y') . ' — ' . Carbon::parse($lastDate)->format('d.m.Y'),
'generatedAt' => now()->format('d.m.Y H:i'),
])->setPaper('a4', 'portrait');
return response()->streamDownload(
fn () => print $pdf->output(),
'programari_' . $firstDate . '_' . $lastDate . '.pdf',
['Content-Type' => 'application/pdf']
);
}
/** Flat list of appointments for the visible period — used by list view. */
public function getListAppointments(): array
{
$days = $this->getDays();
$firstDate = $days[0]['date'] ?? today()->toDateString();
$lastDate = end($days)['date'] ?? today()->toDateString();
return \App\Models\Tenant\Appointment::with(['client:id,name,phone', 'vehicle:id,plate,make,model', 'master:id,name', 'post:id,name'])
->whereBetween('date', [$firstDate, $lastDate])
->when($this->masterFilter, fn ($q) => $q->where('master_id', $this->masterFilter))
->orderBy('date')
->orderBy('time_start')
->get()
->map(fn ($a) => [
'id' => $a->id,
'date' => $a->date->format('d.m.Y'),
'time' => substr($a->time_start ?? '', 0, 5) . '' . substr($a->time_end ?? '', 0, 5),
'title' => $a->title,
'client_name' => $a->client?->name,
'client_phone' => $a->client?->phone,
'vehicle' => trim(($a->vehicle?->make ?? '') . ' ' . ($a->vehicle?->model ?? '')) . ' · ' . ($a->vehicle?->plate ?? '—'),
'master_name' => $a->master?->name ?? '—',
'post_name' => $a->post?->name ?? '—',
'status' => $a->status,
])->all();
}
public function createAppt(): void
{
$d = $this->newAppt;
if (empty($d['title']) || empty($d['date'])) {
Notification::make()->title('Subiect și data sunt obligatorii')->danger()->send();
return;
}
Appointment::create([
'date' => $d['date'],
'time_start' => $d['time_start'] ?: '09:00',
'time_end' => $d['time_end'] ?: '10:00',
'title' => $d['title'],
'client_id' => $d['client_id'] ?: null,
'vehicle_id' => $d['vehicle_id'] ?: null,
'master_id' => $d['master_id'] ?: null,
'post_id' => $d['post_id'] ?: null,
'notes' => $d['notes'] ?: null,
'status' => 'scheduled',
]);
$this->showNewForm = false;
Notification::make()->title('Programare adăugată')->success()->send();
}
public function getMasterOptions(): array
{
return User::where('status', 'active')->pluck('name', 'id')->toArray();
}
public function getPostOptions(): array
{
return Post::where('is_active', true)->pluck('name', 'id')->toArray();
}
public function getClientOptions(): array
{
return Client::orderBy('name')->limit(50)->pluck('name', 'id')->toArray();
}
public function getVehicleOptions(?int $clientId): array
{
if (! $clientId) return [];
return Vehicle::where('client_id', $clientId)->pluck('plate', 'id')->toArray();
}
private function calcHours(?string $start, ?string $end): float
{
if (! $start || ! $end) return 1.0;
try {
$s = Carbon::createFromTimeString($start);
$e = Carbon::createFromTimeString($end);
$h = $e->floatDiffInHours($s);
return max(0, abs($h));
} catch (\Throwable $e) {
return 1.0;
}
}
public function getWeekLabel(): string
{
$s = Carbon::parse($this->weekStart);
$e = $s->copy()->addDays(6);
return $s->format('d.m') . ' — ' . $e->format('d.m.Y');
} }
} }
@@ -0,0 +1,143 @@
<?php
namespace App\Filament\Tenant\Pages;
use App\Models\Tenant\Supplier;
use App\Services\ExcelInvoiceImportService;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Facades\Storage;
use Livewire\WithFileUploads;
class ExcelImportWizard extends Page
{
use WithFileUploads;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-arrow-up-tray';
protected static ?string $navigationLabel = 'Import factură Excel';
protected static string|\UnitEnum|null $navigationGroup = 'Stoc & Finanțe';
protected static ?int $navigationSort = 65;
protected static ?string $title = 'Import factură Excel/CSV';
protected string $view = 'filament.tenant.pages.excel-import-wizard';
public int $step = 1;
public ?int $supplierId = null;
public $upload = null;
public ?string $storedPath = null;
public array $headersPreview = ['columns' => [], 'rows' => []];
public array $mapping = [
'article_col' => 'B',
'name_col' => 'C',
'qty_col' => 'E',
'price_col' => 'F',
'brand_col' => null,
'header_row' => 1,
];
public bool $rememberMapping = true;
public array $previewRows = [];
public array $previewSummary = ['total' => 0, 'found' => 0, 'new' => 0, 'no_article' => 0];
public bool $createNew = true;
public function getMaxContentWidth(): \Filament\Support\Enums\Width
{
return \Filament\Support\Enums\Width::Full;
}
public function getSupplierOptions(): array
{
return Supplier::orderBy('name')->pluck('name', 'id')->toArray();
}
public function goToStep2(): void
{
if (! $this->supplierId) {
Notification::make()->title('Selectează furnizorul')->danger()->send();
return;
}
if (! $this->upload) {
Notification::make()->title('Încarcă fișierul Excel sau CSV')->danger()->send();
return;
}
// Persist the uploaded file so Livewire reuses can resolve it
$this->storedPath = $this->upload->store('imports', 'local');
// Try to load remembered mapping for this supplier
$svc = app(ExcelInvoiceImportService::class);
$supplier = Supplier::find($this->supplierId);
$remembered = $svc->rememberedMappingFor($supplier);
if ($remembered) {
$this->mapping = array_merge($this->mapping, $remembered);
}
$absPath = Storage::disk('local')->path($this->storedPath);
$this->headersPreview = $svc->headersPreview($absPath);
$this->step = 2;
}
public function goToStep3(): void
{
$absPath = Storage::disk('local')->path($this->storedPath);
$svc = app(ExcelInvoiceImportService::class);
$result = $svc->preview($absPath, $this->mapping);
$this->previewRows = $result['rows'];
$this->previewSummary = $result['summary'];
if (empty($this->previewRows)) {
Notification::make()->title('Nu am găsit linii valide — verifică maparea coloanelor')->warning()->send();
return;
}
$this->step = 3;
}
public function confirmImport(): void
{
$svc = app(ExcelInvoiceImportService::class);
$supplier = Supplier::find($this->supplierId);
if ($this->rememberMapping) {
$svc->rememberMapping($supplier, $this->mapping, basename($this->storedPath ?? ''));
}
$purchase = $svc->import($supplier, $this->previewRows, $this->createNew);
Notification::make()
->title("Import reușit — Purchase {$purchase->number}")
->body("{$this->previewSummary['total']} linii importate")
->success()
->send();
// Cleanup uploaded file
if ($this->storedPath) {
Storage::disk('local')->delete($this->storedPath);
}
$this->step = 4;
$this->dispatch('purchase-created', purchaseId: $purchase->id);
// Set the redirect URL on the page so the blade can show a CTA
session()->flash('purchase_id', $purchase->id);
}
public function reset_(): void
{
$this->step = 1;
$this->supplierId = null;
$this->upload = null;
$this->storedPath = null;
$this->headersPreview = ['columns' => [], 'rows' => []];
$this->mapping = [
'article_col' => 'B', 'name_col' => 'C', 'qty_col' => 'E',
'price_col' => 'F', 'brand_col' => null, 'header_row' => 1,
];
$this->previewRows = [];
$this->previewSummary = ['total' => 0, 'found' => 0, 'new' => 0, 'no_article' => 0];
}
}
-58
View File
@@ -1,58 +0,0 @@
<?php
namespace App\Filament\Tenant\Pages;
use App\Models\Tenant\WorkOrder;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
class Kanban extends Page
{
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-view-columns';
protected static ?string $navigationLabel = 'Kanban';
protected static string|\UnitEnum|null $navigationGroup = 'Service';
protected static ?int $navigationSort = 31;
protected static ?string $title = 'Kanban — Fișe de lucru';
protected string $view = 'filament.tenant.pages.kanban';
public function getColumns(): array
{
$statuses = ['new', 'diagnosis', 'agreement', 'in_work', 'awaiting_parts', 'ready'];
$byStatus = WorkOrder::whereIn('status', $statuses)
->with(['client:id,name', 'vehicle:id,plate,make,model', 'master:id,name'])
->orderBy('opened_at')
->get()
->groupBy('status');
$columns = [];
foreach ($statuses as $status) {
$columns[$status] = [
'label' => WorkOrder::STATUSES[$status] ?? $status,
'cards' => $byStatus->get($status, collect())->all(),
'count' => $byStatus->get($status, collect())->count(),
];
}
return $columns;
}
public function moveCard(int $id, string $status): void
{
if (! in_array($status, array_keys(WorkOrder::STATUSES), true)) {
return;
}
$wo = WorkOrder::find($id);
if (! $wo) return;
$wo->update(['status' => $status]);
Notification::make()
->title("Fișa #{$wo->number}" . (WorkOrder::STATUSES[$status] ?? $status))
->success()
->send();
}
}
+142
View File
@@ -0,0 +1,142 @@
<?php
namespace App\Filament\Tenant\Pages;
use App\Models\Tenant\WorkOrder;
use App\Models\Tenant\WorkOrderWork;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
/**
* Mobile-first dashboard for a single mechanic shows ONLY work orders
* assigned to the currently logged-in user (master_id = auth()->id()).
* Kanban-style grouped by status.
*/
class MechanicBoard extends Page
{
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-wrench';
protected static ?string $navigationLabel = 'Atelierul meu';
protected static string|\UnitEnum|null $navigationGroup = 'Service';
protected static ?int $navigationSort = 25;
protected static ?string $title = 'Atelierul meu';
protected string $view = 'filament.tenant.pages.mechanic-board';
public function getColumns(): array
{
$userId = auth()->id();
if (! $userId) return [];
$all = WorkOrder::with(['client', 'vehicle'])
->where('master_id', $userId)
->whereIn('status', ['in_work', 'awaiting_parts', 'ready', 'done', 'approved', 'diagnosis'])
->orderBy('opened_at', 'desc')
->get();
return [
[
'key' => 'in_work',
'label' => 'În lucru',
'color' => '#f59e0b',
'items' => $all->where('status', 'in_work')->values(),
],
[
'key' => 'awaiting_parts',
'label' => 'Așteaptă piese',
'color' => '#8b5cf6',
'items' => $all->whereIn('status', ['awaiting_parts'])->values(),
],
[
'key' => 'ready',
'label' => 'Gata',
'color' => '#10b981',
'items' => $all->where('status', 'ready')->values(),
],
[
'key' => 'recent',
'label' => 'Recente / restul',
'color' => '#64748b',
'items' => $all->whereIn('status', ['done', 'approved', 'diagnosis'])
->take(20)
->values(),
],
];
}
public ?int $blockingWorkId = null;
public string $blockReason = 'missing_part';
public string $blockNote = '';
public function startWork(int $id): void
{
$w = WorkOrderWork::find($id);
if ($w && $w->workOrder?->master_id === auth()->id()) $w->start();
}
public function pauseWork(int $id): void
{
$w = WorkOrderWork::find($id);
if ($w && $w->workOrder?->master_id === auth()->id()) $w->pause();
}
public function resumeWork(int $id): void
{
$w = WorkOrderWork::find($id);
if ($w && $w->workOrder?->master_id === auth()->id()) $w->resume();
}
public function doneWork(int $id): void
{
$w = WorkOrderWork::find($id);
if ($w && $w->workOrder?->master_id === auth()->id()) $w->markDone();
}
public function openBlockModal(int $id): void
{
$this->blockingWorkId = $id;
$this->blockReason = 'missing_part';
$this->blockNote = '';
}
public function confirmBlock(): void
{
if (! $this->blockingWorkId) return;
$work = WorkOrderWork::find($this->blockingWorkId);
if (! $work) return;
// Only own work
if ($work->workOrder?->master_id !== auth()->id()) {
$this->blockingWorkId = null;
return;
}
$work->block($this->blockReason, trim($this->blockNote) ?: null);
Notification::make()->title('Lucrare blocată')->body($work->name . ' · ' . WorkOrderWork::BLOCK_REASONS[$this->blockReason])->warning()->send();
$this->blockingWorkId = null;
}
public function getWorksFor(int $woId): array
{
return WorkOrderWork::where('work_order_id', $woId)->orderBy('id')->get()->all();
}
public function getCounts(): array
{
$userId = auth()->id();
return [
'active' => $userId
? WorkOrder::where('master_id', $userId)
->whereIn('status', ['in_work', 'awaiting_parts', 'ready'])
->count()
: 0,
'closed_today' => $userId
? WorkOrder::where('master_id', $userId)
->where('status', 'done')
->whereDate('closed_at', today())
->count()
: 0,
];
}
}
+87
View File
@@ -0,0 +1,87 @@
<?php
namespace App\Filament\Tenant\Pages;
use App\Models\Tenant\User;
use App\Models\Tenant\WorkOrderWork;
use Carbon\Carbon;
use Filament\Pages\Page;
/**
* Aggregate KPI dashboard per mechanic over a period: tasks done, norm vs
* actual hours, efficiency %, revenue from manopere. Period defaults to
* current month.
*/
class MechanicKpi extends Page
{
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-chart-bar';
protected static ?string $navigationLabel = 'KPI mecanici';
protected static string|\UnitEnum|null $navigationGroup = 'Service';
protected static ?int $navigationSort = 28;
protected static ?string $title = 'KPI mecanici';
protected string $view = 'filament.tenant.pages.mechanic-kpi';
public string $period = '';
public function mount(): void
{
$this->period = now()->format('Y-m');
}
public function shiftMonth(int $delta): void
{
$this->period = Carbon::parse($this->period . '-01')->addMonths($delta)->format('Y-m');
}
public function getRows(): array
{
[$y, $m] = explode('-', $this->period);
$rows = WorkOrderWork::query()
->with('workOrder:id,master_id')
->where('mechanic_status', 'done')
->whereYear('mechanic_done_at', $y)
->whereMonth('mechanic_done_at', $m)
->get()
->groupBy(fn ($w) => $w->workOrder?->master_id ?: 0);
$masters = User::whereIn('id', $rows->keys()->all())->get(['id', 'name'])->keyBy('id');
$out = [];
foreach ($rows as $masterId => $works) {
if (! $masterId) continue;
$totalNorm = (float) $works->sum('hours');
$totalActual = (float) $works->sum('actual_hours');
$efficiencyPct = $totalNorm > 0 ? round(100 * $totalActual / $totalNorm) : null;
$cls = match (true) {
$efficiencyPct === null => 'gray',
$efficiencyPct <= 100 => 'green',
$efficiencyPct <= 130 => 'amber',
default => 'red',
};
$out[] = [
'master_id' => $masterId,
'master_name' => $masters[$masterId]?->name ?? 'Mecanic #' . $masterId,
'tasks_done' => $works->count(),
'norm_hours' => round($totalNorm, 2),
'actual_hours' => round($totalActual, 2),
'efficiency_pct' => $efficiencyPct,
'efficiency_class' => $cls,
'revenue' => round((float) $works->sum('total'), 2),
];
}
usort($out, fn ($a, $b) => $b['revenue'] <=> $a['revenue']);
return $out;
}
public function getPeriodLabel(): string
{
return Carbon::parse($this->period . '-01')->locale('ro')->isoFormat('MMMM YYYY');
}
}
+787
View File
@@ -0,0 +1,787 @@
<?php
namespace App\Filament\Tenant\Pages;
use App\Models\Tenant\Deal;
use App\Models\Tenant\Lead;
use App\Models\Tenant\Payment;
use App\Models\Tenant\WorkOrder;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class PipelineBoard extends Page
{
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-funnel';
protected static ?string $navigationLabel = 'Pipeline';
protected static string|\UnitEnum|null $navigationGroup = 'CRM';
protected static ?int $navigationSort = 5;
protected static ?string $title = 'Pipeline';
protected string $view = 'filament.tenant.pages.pipeline-board';
public function getMaxContentWidth(): \Filament\Support\Enums\Width
{
return \Filament\Support\Enums\Width::Full;
}
public function getHeading(): string
{
return '';
}
public function getSubheading(): ?string
{
return null;
}
public string $activeFilter = 'all'; // all | mine | urgent | today
public ?string $openCardKey = null; // "lead:5" / "deal:8" / "wo:12"
public bool $showNewForm = false; // panel in "new request" mode
public string $searchQuery = '';
public string $newName = '';
public string $newPhone = '';
public string $newCar = '';
public string $newSource = 'call';
public string $newNotes = '';
public const COLUMNS = [
'request' => ['Cerere nouă', '#94A3B8'],
'quote' => ['Calculație', '#F59E0B'],
'scheduled' => ['Programat', '#3B82F6'],
'in_work' => ['În lucru', '#8B5CF6'],
'ready' => ['Gata de ridicare', '#10B981'],
'paid' => ['Achitat azi', '#6EE7B7'],
];
public function getColumns(): array
{
$userId = auth()->id();
$mineOnly = $this->activeFilter === 'mine';
$urgentOnly = $this->activeFilter === 'urgent';
$todayOnly = $this->activeFilter === 'today';
// Col 1: leads (not converted) + deals at stage=new
$leads = Lead::query()
->whereIn('status', ['new', 'contacted', 'no_answer'])
->whereNull('deal_id')
->when($mineOnly, fn ($q) => $q->where('assigned_to', $userId))
->orderByDesc('created_at')
->get();
$dealQ = Deal::query()
->with(['client:id,name,phone', 'vehicle:id,plate,make,model', 'assignedTo:id,name'])
->whereNotIn('stage', ['done', 'lost'])
->when($mineOnly, fn ($q) => $q->where('assigned_to', $userId))
->when($urgentOnly, fn ($q) => $q->where('urgent', true))
->orderByDesc('updated_at')
->get();
$woQ = WorkOrder::query()
->with(['client:id,name,phone', 'vehicle:id,plate,make,model', 'master:id,name'])
->whereNotIn('status', ['done', 'cancelled'])
->when($mineOnly, fn ($q) => $q->where('master_id', $userId))
->when($urgentOnly, fn ($q) => $q->where('urgency', '!=', 'normal'))
->orderBy('opened_at')
->get();
$paidToday = WorkOrder::query()
->with(['client:id,name,phone', 'vehicle:id,plate,make,model', 'master:id,name'])
->where('status', 'done')
->where('pay_status', 'paid')
->whereDate('closed_at', today())
->when($mineOnly, fn ($q) => $q->where('master_id', $userId))
->orderByDesc('closed_at')
->get();
$cards = [
'request' => [],
'quote' => [],
'scheduled' => [],
'in_work' => [],
'ready' => [],
'paid' => [],
];
foreach ($leads as $lead) {
$cards['request'][] = $this->leadCard($lead);
}
foreach ($dealQ as $deal) {
$col = match ($deal->stage) {
'new' => 'request',
'contact', 'agree' => 'quote',
'scheduled', 'arrived' => 'scheduled',
'in_work' => null, // shouldn't happen: in_work creates WO
default => null,
};
if ($col) {
$cards[$col][] = $this->dealCard($deal);
}
}
foreach ($woQ as $wo) {
$col = $wo->status === 'ready' ? 'ready' : 'in_work';
$cards[$col][] = $this->woCard($wo);
}
foreach ($paidToday as $wo) {
$cards['paid'][] = $this->woCard($wo);
}
// Apply search query
$q = trim($this->searchQuery);
if ($q !== '') {
$needle = mb_strtolower($q);
foreach ($cards as $col => $list) {
$cards[$col] = array_values(array_filter($list, function ($c) use ($needle) {
$hay = mb_strtolower(($c['subject'] ?? '') . ' ' . ($c['client_name'] ?? '') . ' ' . ($c['plate'] ?? '') . ' ' . ($c['code'] ?? '') . ' ' . ($c['phone'] ?? ''));
return str_contains($hay, $needle);
}));
}
}
// Sort: urgent first, then time
foreach ($cards as $col => $list) {
usort($list, fn ($a, $b) => ($b['urgent'] ?? false) <=> ($a['urgent'] ?? false));
$cards[$col] = $list;
}
// "Today" filter narrows further: only scheduled today OR opened today OR paid today.
if ($todayOnly) {
$cards['request'] = array_filter($cards['request'], fn ($c) => str_contains($c['time_text'], 'azi') || str_contains($c['time_text'], 'min'));
// others: keep — Scheduled column inherently shows soon dates; In Work / Ready / Paid show today by default
}
$columns = [];
foreach (self::COLUMNS as $key => [$label, $color]) {
$list = array_values($cards[$key]);
$sum = array_sum(array_map(fn ($c) => (float) $c['amount'], $list));
$columns[$key] = [
'label' => $label,
'color' => $color,
'count' => count($list),
'sum' => $sum,
'cards' => $list,
];
}
return $columns;
}
public function getStats(): array
{
$cols = $this->getColumns();
$active = $cols['request']['count'] + $cols['quote']['count'] + $cols['scheduled']['count'] + $cols['in_work']['count'] + $cols['ready']['count'];
$pipeline = $cols['request']['sum'] + $cols['quote']['sum'] + $cols['scheduled']['sum'] + $cols['in_work']['sum'] + $cols['ready']['sum'];
$closedToday = (float) Payment::whereDate('paid_at', today())->sum('amount');
$needAction = 0;
foreach (['request', 'quote', 'scheduled', 'in_work', 'ready'] as $key) {
foreach ($cols[$key]['cards'] as $card) {
if (! empty($card['time_overdue']) || ! empty($card['urgent']) || ! empty($card['has_pending_approval'])) {
$needAction++;
}
}
}
$overdue = WorkOrder::whereNotIn('status', ['done', 'cancelled'])
->whereNotNull('eta_at')
->where('eta_at', '<', now())
->count();
$won = Deal::whereNotNull('won_at')->where('won_at', '>=', now()->subDays(30))->count();
$lost = Deal::whereNotNull('lost_at')->where('lost_at', '>=', now()->subDays(30))->count();
$conversionRate = ($won + $lost) > 0 ? round(100 * $won / ($won + $lost)) : 0;
return [
'active' => $active,
'pipeline_mdl' => $pipeline,
'closed_today_mdl' => $closedToday,
'need_action' => $needAction,
'conversion_rate' => $conversionRate,
'overdue' => $overdue,
];
}
public function setFilter(string $filter): void
{
$this->activeFilter = in_array($filter, ['all', 'mine', 'urgent', 'today'], true) ? $filter : 'all';
}
public function openNewForm(): void
{
$this->showNewForm = true;
$this->openCardKey = null;
$this->newName = '';
$this->newPhone = '';
$this->newCar = '';
$this->newSource = 'call';
$this->newNotes = '';
}
public function createNewLead(): void
{
$data = ['name' => trim($this->newName), 'phone' => trim($this->newPhone), 'car' => trim($this->newCar) ?: null, 'source' => $this->newSource, 'message' => trim($this->newNotes) ?: null];
if ($data['name'] === '' || $data['phone'] === '') {
$this->notify('Nume și telefon sunt obligatorii');
return;
}
Lead::create(array_merge($data, ['status' => 'new']));
$this->showNewForm = false;
$this->notify('Cerere nouă adăugată');
}
public function exportCsv()
{
$columns = $this->getColumns();
$csv = "Etapă,Cod,Subiect,Client,Telefon,Auto,Sumă,Responsabil,Stare\n";
foreach ($columns as $col) {
foreach ($col['cards'] as $card) {
$csv .= sprintf(
"%s,%s,%s,%s,%s,%s,%.2f,%s,%s\n",
$col['label'],
$card['code'],
str_replace(',', ' ', $card['subject']),
str_replace(',', ' ', $card['client_name']),
$card['phone'] ?? '',
$card['plate'],
$card['amount'],
$card['assignee']['name'],
str_replace(',', ' ', $card['time_text']),
);
}
}
return response()->streamDownload(fn () => print $csv, 'pipeline-' . today()->format('Y-m-d') . '.csv', ['Content-Type' => 'text/csv']);
}
public function moveCard(string $key, string $toCol): void
{
[$kind, $id] = explode(':', $key, 2) + [null, null];
$id = (int) $id;
if (! $kind || ! $id || ! isset(self::COLUMNS[$toCol])) {
return;
}
DB::transaction(function () use ($kind, $id, $toCol) {
switch ($kind . '→' . $toCol) {
// Lead in col 1, dragged to col 2 → convert to Deal at quote stage
case "lead→quote":
$lead = Lead::find($id);
if (! $lead) return;
$deal = $lead->convert(['stage' => 'contact', 'quote_status' => 'sent', 'quote_sent_at' => now()]);
$this->notify("Lead → Deal CIU-{$deal->id} · Calculație trimisă");
return;
// Lead → scheduled / in_work: convert + skip
case "lead→scheduled":
$lead = Lead::find($id);
if (! $lead) return;
$deal = $lead->convert(['stage' => 'scheduled', 'scheduled_at' => now()->addDay()]);
$this->notify("Lead → Deal CIU-{$deal->id} · Programat");
return;
case "lead→in_work":
$lead = Lead::find($id);
if (! $lead) return;
$deal = $lead->convert(['stage' => 'in_work']);
$wo = $this->createWorkOrderFromDeal($deal);
$this->notify("Lead → Fișă {$wo->number}");
return;
// Deal in col 1/2/3 → moving across deal stages
case "deal→request":
Deal::where('id', $id)->update(['stage' => 'new']);
return;
case "deal→quote":
Deal::where('id', $id)->update([
'stage' => 'contact',
'quote_status' => 'sent',
'quote_sent_at' => now(),
]);
return;
case "deal→scheduled":
Deal::where('id', $id)->update([
'stage' => 'scheduled',
'scheduled_at' => Deal::find($id)?->scheduled_at ?? now()->addDay(),
]);
return;
// Deal → În lucru: create work order
case "deal→in_work":
$deal = Deal::find($id);
if (! $deal) return;
$wo = $this->createWorkOrderFromDeal($deal);
$this->notify("Fișă {$wo->number} creată din deal");
return;
// WO between cols
case "wo→in_work":
WorkOrder::where('id', $id)->update(['status' => 'in_work']);
return;
case "wo→ready":
WorkOrder::where('id', $id)->update(['status' => 'ready']);
$this->notify("Fișa marcată ca Gata de ridicare");
// Fire notification to client (dispatcher handles channel choice)
$wo = WorkOrder::find($id);
if ($wo) app(\App\Services\NotificationDispatcher::class)->workOrderReady($wo);
return;
case "wo→paid":
$wo = WorkOrder::find($id);
if (! $wo) return;
$due = (float) $wo->total - (float) Payment::where('work_order_id', $wo->id)->sum('amount');
if ($due > 0.01) {
Payment::create([
'work_order_id' => $wo->id,
'client_id' => $wo->client_id,
'paid_at' => today(),
'amount' => round($due, 2),
'method' => 'cash',
'notes' => 'Achitat din Pipeline',
]);
}
$wo->update(['status' => 'done', 'closed_at' => today()]);
$this->notify("Fișa {$wo->number} → Achitat");
return;
}
});
}
public function openCard(string $key): void
{
$this->openCardKey = $key;
}
/** Quick-schedule from a card: bumps the source to "Programat", creates an Appointment for tomorrow 10:00, returns calendar URL. */
public function quickSchedule(string $key): void
{
[$kind, $id] = explode(':', $key, 2) + [null, null];
$id = (int) $id;
if (! $kind || ! $id) return;
$clientId = null; $vehicleId = null; $dealId = null; $title = null; $masterId = null;
if ($kind === 'lead') {
$lead = Lead::find($id);
if (! $lead) return;
$deal = $lead->convert(['stage' => 'scheduled', 'scheduled_at' => now()->addDay()->setHour(10)->setMinute(0)]);
$clientId = $deal->client_id; $vehicleId = $deal->vehicle_id; $dealId = $deal->id;
$title = $deal->name;
} elseif ($kind === 'deal') {
$deal = Deal::find($id);
if (! $deal) return;
$deal->update(['stage' => 'scheduled', 'scheduled_at' => now()->addDay()->setHour(10)->setMinute(0)]);
$clientId = $deal->client_id; $vehicleId = $deal->vehicle_id; $dealId = $deal->id;
$title = $deal->name;
$masterId = $deal->assigned_to;
} elseif ($kind === 'wo') {
$wo = WorkOrder::find($id);
if (! $wo) return;
$clientId = $wo->client_id; $vehicleId = $wo->vehicle_id;
$title = $wo->number;
$masterId = $wo->master_id;
}
\App\Models\Tenant\Appointment::create([
'client_id' => $clientId,
'vehicle_id' => $vehicleId,
'master_id' => $masterId,
'deal_id' => $dealId,
'date' => today()->addDay(),
'time_start' => '10:00',
'time_end' => '11:00',
'title' => $title ?: 'Programare',
'status' => 'scheduled',
]);
$this->notify("Programare creată · mâine 10:00");
$this->openCardKey = null;
}
public function calendarUrl(): string
{
return route('filament.tenant.pages.calendar-board');
}
public function closeCard(): void
{
$this->openCardKey = null;
}
public function getOpenCardDetail(): ?array
{
if (! $this->openCardKey) return null;
[$kind, $id] = explode(':', $this->openCardKey, 2) + [null, null];
$id = (int) $id;
if (! $kind || ! $id) return null;
return match ($kind) {
'lead' => $this->leadDetail(Lead::find($id)),
'deal' => $this->dealDetail(Deal::with(['client', 'vehicle', 'assignedTo'])->find($id)),
'wo' => $this->woDetail(WorkOrder::with(['client', 'vehicle', 'master', 'parts', 'works'])->find($id)),
default => null,
};
}
// ============== card builders ==============
private function leadCard(Lead $lead): array
{
$tags = [];
if (in_array($lead->source, ['instagram', 'facebook', 'site', 'google', 'call'])) {
$tags[] = ['label' => Lead::SOURCES[$lead->source] ?? $lead->source, 'color' => 'gray'];
}
if ($lead->status === 'no_answer') {
$tags[] = ['label' => 'Fără răspuns', 'color' => 'red'];
}
$diffMin = $lead->created_at?->diffInMinutes(now()) ?? 0;
$overdue = $diffMin > 60 && $lead->status !== 'contacted';
return [
'kind' => 'lead',
'id' => $lead->id,
'key' => "lead:{$lead->id}",
'code' => 'CR-' . str_pad((string) $lead->id, 4, '0', STR_PAD_LEFT),
'subject' => trim(($lead->car ?: '') . ' ' . ($lead->model ?: '')) ?: $lead->name,
'plate' => $lead->car ?: '',
'client_name' => $lead->name ?: 'Anonim',
'phone' => $lead->phone,
'source' => $lead->source,
'amount' => (float) $lead->budget,
'urgent' => $overdue,
'tags' => $tags,
'time_text' => $this->humanTime($lead->created_at),
'time_overdue' => $overdue,
'time_icon' => 'clock',
'assignee' => $this->assignee($lead->assignedTo),
'progress_pct' => null,
'has_pending_approval' => false,
'edit_url' => route('filament.tenant.resources.leads.edit', ['record' => $lead->id]),
];
}
private function dealCard(Deal $deal): array
{
$tags = [];
if ($deal->urgent) {
$tags[] = ['label' => 'Urgent', 'color' => 'red'];
}
if ($deal->source && in_array($deal->source, ['instagram', 'site', 'call', 'whatsapp', 'telegram'])) {
$tags[] = ['label' => Lead::SOURCES[$deal->source] ?? $deal->source, 'color' => 'gray'];
}
if ($deal->stage === 'contact' && $deal->quote_status) {
$color = match ($deal->quote_status) {
'sent' => 'amber',
'seen' => 'blue',
'responded' => 'green',
default => 'gray',
};
$tags[] = ['label' => Deal::QUOTE_STATUSES[$deal->quote_status] ?? $deal->quote_status, 'color' => $color];
}
if (in_array($deal->stage, ['scheduled', 'arrived'])) {
if ($deal->scheduled_at) {
$tags[] = ['label' => $deal->scheduled_at->format('d.m · H:i'), 'color' => 'blue'];
}
if ($deal->bay) {
$tags[] = ['label' => $deal->bay, 'color' => 'gray'];
}
}
// Time line
$timeText = '';
$timeIcon = 'clock';
$overdue = false;
if ($deal->stage === 'contact' && $deal->quote_sent_at && in_array($deal->quote_status, ['sent', null])) {
$mins = $deal->quote_sent_at->diffInMinutes(now());
if ($mins > 120) {
$overdue = true;
$timeText = 'Trimis acum ' . $this->humanDiff($deal->quote_sent_at);
} else {
$timeText = 'Trimis ' . $this->humanDiff($deal->quote_sent_at);
}
} elseif ($deal->stage === 'contact' && $deal->quote_status === 'seen' && $deal->quote_seen_at) {
$timeText = 'văzut ' . $this->humanDiff($deal->quote_seen_at);
} elseif (in_array($deal->stage, ['scheduled', 'arrived']) && $deal->confirmed_at) {
$timeText = 'Confirmat ' . (Deal::CONFIRM_CHANNELS[$deal->confirmed_via] ?? '');
$timeIcon = 'check';
} elseif (in_array($deal->stage, ['scheduled', 'arrived'])) {
$timeText = 'Neconfirmat';
} else {
$timeText = $this->humanTime($deal->updated_at);
}
return [
'kind' => 'deal',
'id' => $deal->id,
'key' => "deal:{$deal->id}",
'code' => 'CIU-' . str_pad((string) $deal->id, 4, '0', STR_PAD_LEFT),
'subject' => $deal->name,
'plate' => $deal->vehicle?->plate ?: '',
'client_name' => $deal->client?->name ?? '—',
'phone' => $deal->client?->phone,
'source' => $deal->source,
'amount' => (float) $deal->price,
'urgent' => (bool) $deal->urgent,
'tags' => $tags,
'time_text' => $timeText,
'time_overdue' => $overdue,
'time_icon' => $timeIcon,
'assignee' => $this->assignee($deal->assignedTo),
'progress_pct' => null,
'has_pending_approval' => false,
'edit_url' => route('filament.tenant.resources.deals.edit', ['record' => $deal->id]),
];
}
private function woCard(WorkOrder $wo): array
{
$tags = [];
$tags[] = ['label' => "Fișă {$wo->number}", 'color' => 'purple'];
if ($wo->status === 'agreement') {
$tags[] = ['label' => 'Necesită aprobare', 'color' => 'amber'];
}
if ($wo->status === 'awaiting_parts') {
$tags[] = ['label' => 'Așteaptă piese', 'color' => 'amber'];
}
if ($wo->status === 'ready') {
$tags[] = ['label' => 'Gata', 'color' => 'green'];
if ($wo->pay_status === 'partial') {
$tags[] = ['label' => 'Avans achitat', 'color' => 'blue'];
} elseif ($wo->pay_status !== 'paid') {
$tags[] = ['label' => 'Neachitat', 'color' => 'amber'];
}
}
if ($wo->status === 'done' && $wo->pay_status === 'paid') {
$tags[] = ['label' => '✓ Achitat', 'color' => 'green'];
}
$progress = null;
if (in_array($wo->status, ['in_work', 'diagnosis', 'agreement', 'approved', 'awaiting_parts'])) {
$total = max(1, $wo->works()->count() + $wo->parts()->count());
$done = $wo->works()->where('status', 'done')->count() + $wo->parts()->where('status', 'installed')->count();
$progress = (int) round(100 * $done / $total);
}
$timeText = '';
$timeIcon = 'clock';
$overdue = false;
if ($wo->status === 'ready') {
$minsSinceReady = $wo->updated_at?->diffInMinutes(now()) ?? 0;
if ($minsSinceReady > 30 && $wo->pay_status !== 'paid') {
$overdue = true;
$timeText = 'Notificat acum ' . $this->humanDiff($wo->updated_at);
$timeIcon = 'phone';
} else {
$timeText = 'Notificat ' . $this->humanDiff($wo->updated_at);
$timeIcon = 'message';
}
} elseif ($wo->eta_at) {
$timeText = 'ETA ' . $wo->eta_at->format('H:i') . ($progress ? " · {$progress}% gata" : '');
$overdue = $wo->eta_at->isPast();
} else {
$timeText = $this->humanTime($wo->opened_at);
}
return [
'kind' => 'wo',
'id' => $wo->id,
'key' => "wo:{$wo->id}",
'code' => $wo->number,
'subject' => ($wo->vehicle?->make . ' ' . $wo->vehicle?->model) . ($wo->complaint ? ' — ' . str($wo->complaint)->limit(40) : ''),
'plate' => $wo->vehicle?->plate ?: '',
'client_name' => $wo->client?->name ?? '—',
'phone' => $wo->client?->phone,
'source' => null,
'amount' => (float) $wo->total,
'urgent' => $wo->urgency !== 'normal',
'tags' => $tags,
'time_text' => $timeText,
'time_overdue' => $overdue,
'time_icon' => $timeIcon,
'assignee' => $this->assignee($wo->master),
'progress_pct' => $progress,
'has_pending_approval' => $wo->status === 'agreement',
'edit_url' => route('filament.tenant.resources.work-orders.edit', ['record' => $wo->id]),
];
}
private function leadDetail(?Lead $lead): ?array
{
if (! $lead) return null;
$card = $this->leadCard($lead);
return array_merge($card, [
'title' => $card['subject'] ?: ('Cerere · ' . $lead->name),
'subtitle' => $card['code'] . ' · Cerere',
'stages' => $this->stageStepper(0),
'fields' => [
'Client' => $lead->name,
'Telefon' => $lead->phone,
'Email' => $lead->email,
'Automobil' => trim(($lead->car ?: '') . ' ' . ($lead->model ?: '')),
'Sursă' => Lead::SOURCES[$lead->source] ?? $lead->source,
'Buget estimat' => $lead->budget ? number_format($lead->budget, 0, '.', ' ') . ' MDL' : null,
],
'note' => $lead->message ?? $lead->notes,
'activity' => [],
'wo' => null,
]);
}
private function dealDetail(?Deal $deal): ?array
{
if (! $deal) return null;
$card = $this->dealCard($deal);
$stageIdx = match ($deal->stage) {
'new' => 0,
'contact', 'agree' => 1,
'scheduled', 'arrived' => 2,
default => 0,
};
return array_merge($card, [
'title' => $deal->name,
'subtitle' => $card['code'] . ' · ' . (Deal::STAGES[$deal->stage] ?? $deal->stage),
'stages' => $this->stageStepper($stageIdx),
'fields' => [
'Client' => $deal->client?->name,
'Telefon' => $deal->client?->phone,
'Automobil' => trim(($deal->vehicle?->make ?? '') . ' ' . ($deal->vehicle?->model ?? '') . ' · ' . ($deal->vehicle?->plate ?? '')),
'Sursă' => $deal->source ? (Lead::SOURCES[$deal->source] ?? $deal->source) : null,
'Responsabil' => $deal->assignedTo?->name,
'Sumă' => number_format($deal->price, 0, '.', ' ') . ' MDL',
],
'note' => $deal->note,
'activity' => $this->loadActivity($deal),
'wo' => null,
]);
}
private function woDetail(?WorkOrder $wo): ?array
{
if (! $wo) return null;
$card = $this->woCard($wo);
$stageIdx = match ($wo->status) {
'new', 'diagnosis', 'agreement', 'approved' => 3,
'in_work', 'awaiting_parts' => 3,
'ready' => 4,
'done' => 5,
default => 3,
};
$balanceDue = (float) $wo->total - (float) Payment::where('work_order_id', $wo->id)->sum('amount');
return array_merge($card, [
'title' => ($wo->vehicle?->make . ' ' . $wo->vehicle?->model) . ($wo->complaint ? ' — ' . str($wo->complaint)->limit(60) : ''),
'subtitle' => $wo->number . ' · ' . (WorkOrder::STATUSES[$wo->status] ?? $wo->status),
'stages' => $this->stageStepper($stageIdx),
'fields' => [
'Client' => $wo->client?->name,
'Telefon' => $wo->client?->phone,
'Automobil' => trim(($wo->vehicle?->make ?? '') . ' · ' . ($wo->vehicle?->plate ?? '')),
'Responsabil' => $wo->master?->name,
'Sumă' => number_format($wo->total, 0, '.', ' ') . ' MDL',
'De achitat' => number_format(max(0, $balanceDue), 0, '.', ' ') . ' MDL',
],
'note' => $wo->diagnosis ?: $wo->complaint,
'activity' => $this->loadActivity($wo),
'wo' => [
'number' => $wo->number,
'status_label' => WorkOrder::STATUSES[$wo->status] ?? $wo->status,
'progress_pct' => $card['progress_pct'],
'eta' => $wo->eta_at?->format('H:i'),
'has_pending_approval' => $card['has_pending_approval'],
'tracking_url' => $wo->trackingUrl(),
],
]);
}
private function loadActivity($model): array
{
$items = [];
if (method_exists($model, 'activities')) {
foreach ($model->activities()->latest()->take(6)->get() as $a) {
$items[] = [
'icon' => match ($a->event ?? 'updated') {
'created' => 'plus',
'deleted' => 'trash',
default => 'edit',
},
'color' => 'blue',
'text' => $a->description ?: ucfirst($a->event ?? 'actualizat'),
'time' => $a->created_at?->diffForHumans(),
];
}
}
return $items;
}
private function stageStepper(int $currentIdx): array
{
$labels = ['Cerere', 'Calcul.', 'Programat', 'În lucru', 'Gata', 'Achitat'];
$out = [];
foreach ($labels as $i => $label) {
$out[] = [
'label' => $label,
'done' => $i < $currentIdx,
'current' => $i === $currentIdx,
];
}
return $out;
}
private function assignee($user): array
{
if (! $user) {
return ['initials' => '?', 'name' => '—', 'color' => 'gray'];
}
$parts = preg_split('/\s+/', trim($user->name ?? '?'));
$initials = strtoupper(substr($parts[0] ?? '?', 0, 1) . substr($parts[1] ?? '', 0, 1));
// hash to a deterministic color
$colors = ['blue', 'green', 'purple', 'amber'];
$color = $colors[abs(crc32($user->id ?? 1)) % 4];
return [
'initials' => $initials ?: '?',
'name' => $this->shortName($user->name ?? ''),
'color' => $color,
];
}
private function shortName(string $name): string
{
$parts = preg_split('/\s+/', trim($name));
if (count($parts) < 2) return $name;
return $parts[0] . ' ' . strtoupper(substr($parts[1], 0, 1)) . '.';
}
private function humanTime(?Carbon $dt): string
{
if (! $dt) return '';
$mins = $dt->diffInMinutes(now());
if ($mins < 60) return "acum $mins min";
if ($mins < 60 * 24) return "acum " . round($mins / 60) . "h";
return $dt->format('d.m');
}
private function humanDiff(?Carbon $dt): string
{
if (! $dt) return '';
$mins = $dt->diffInMinutes(now());
if ($mins < 60) return "$mins min";
if ($mins < 60 * 24) return round($mins / 60) . "h";
return $dt->format('d.m');
}
private function createWorkOrderFromDeal(Deal $deal): WorkOrder
{
$wo = WorkOrder::create([
'number' => WorkOrder::generateNumber($deal->company_id),
'client_id' => $deal->client_id,
'vehicle_id' => $deal->vehicle_id,
'master_id' => $deal->assigned_to,
'deal_id' => $deal->id,
'opened_at' => today(),
'status' => 'in_work',
'total' => $deal->price ?: 0,
'complaint' => $deal->note ?: $deal->name,
]);
$deal->update(['stage' => 'in_work']);
return $wo;
}
private function notify(string $text): void
{
Notification::make()->title($text)->success()->send();
}
}
+153
View File
@@ -0,0 +1,153 @@
<?php
namespace App\Filament\Tenant\Pages;
use App\Models\Tenant\Part;
use App\Models\Tenant\Purchase;
use App\Models\Tenant\PurchaseItem;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Livewire\Attributes\On;
/**
* Mobile scanner: opens camera in the browser, decodes QR/barcode, looks up
* Part by:
* - `PART:<article|id>` payload (our own QR labels)
* - exact barcode match on parts.barcode
* - exact article match on parts.article
* On match redirect to Part edit page.
*/
class Scanner extends Page
{
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-qr-code';
protected static ?string $navigationLabel = 'Scaner';
protected static string|\UnitEnum|null $navigationGroup = 'Depozit';
protected static ?int $navigationSort = 39;
protected static ?string $title = 'Scaner cod QR / Bare';
protected string $view = 'filament.tenant.pages.scanner';
public string $manual = '';
public ?int $purchaseId = null;
public array $receivedToasts = [];
public function mount(): void
{
// Optional ?purchase=N → receipt mode: scans mark items received
$purchase = request()->query('purchase');
if ($purchase && ctype_digit((string) $purchase)) {
$this->purchaseId = (int) $purchase;
}
}
public function getActivePurchase(): ?Purchase
{
return $this->purchaseId ? Purchase::find($this->purchaseId) : null;
}
public function getPendingItems(): array
{
if (! $this->purchaseId) return [];
return PurchaseItem::where('purchase_id', $this->purchaseId)
->whereColumn('qty_received', '<', 'qty')
->orderBy('article')
->get(['id', 'article', 'name', 'qty', 'qty_received'])
->toArray();
}
#[On('scanner-decoded')]
public function decoded(string $text): void
{
$this->process(trim($text));
}
public function submitManual(): void
{
if (trim($this->manual) === '') return;
$this->process(trim($this->manual));
$this->manual = '';
}
protected function process(string $code): void
{
// Receipt mode: increment qty_received on matching purchase item
if ($this->purchaseId) {
$this->markReceivedByScan($code);
return;
}
$this->resolveAndRedirect($code);
}
protected function markReceivedByScan(string $code): void
{
$clean = str_starts_with($code, 'PART:') ? substr($code, 5) : $code;
$item = PurchaseItem::where('purchase_id', $this->purchaseId)
->whereColumn('qty_received', '<', 'qty')
->where(function ($q) use ($clean, $code) {
$q->where('article', $clean)->orWhere('article', $code);
})
->first();
if (! $item) {
Notification::make()
->title('Articol nu se potrivește comenzii')
->body('Codul ' . $code . ' nu apare în liniile neîncasate ale acestei comenzi.')
->warning()->send();
return;
}
$item->qty_received = min((float) $item->qty, (float) $item->qty_received + 1);
$item->save();
$this->receivedToasts[] = [
'article' => $item->article,
'name' => $item->name,
'qty_received' => (float) $item->qty_received,
'qty_total' => (float) $item->qty,
'at' => now()->format('H:i:s'),
];
Notification::make()
->title("+1 {$item->article}{$item->qty_received}/{$item->qty}")
->success()->send();
}
protected function resolveAndRedirect(string $code): void
{
$clean = $code;
if (str_starts_with($clean, 'PART:')) {
$clean = substr($clean, 5);
}
$part = Part::where(function ($q) use ($clean, $code) {
$q->where('article', $clean)
->orWhere('barcode', $clean)
->orWhere('barcode', $code);
if (ctype_digit($clean)) $q->orWhere('id', (int) $clean);
})
->first();
if (! $part) {
Notification::make()
->title('Cod necunoscut')
->body('Nu am găsit nicio piesă pentru: ' . $code)
->warning()
->send();
return;
}
Notification::make()
->title('Piesă găsită: ' . $part->name)
->success()
->send();
$this->redirect(
route('filament.tenant.resources.parts.edit', ['record' => $part->id])
);
}
}
+157 -2
View File
@@ -2,7 +2,9 @@
namespace App\Filament\Tenant\Pages; namespace App\Filament\Tenant\Pages;
use App\Services\Notifications\TelegramService;
use App\Tenancy\TenantManager; use App\Tenancy\TenantManager;
use Filament\Actions;
use Filament\Forms; use Filament\Forms;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
use Filament\Pages\Page; use Filament\Pages\Page;
@@ -50,10 +52,20 @@ class Settings extends Page
'notify_payment' => $notify['payment'] ?? true, 'notify_payment' => $notify['payment'] ?? true,
'notify_appointment' => $notify['appointment'] ?? true, 'notify_appointment' => $notify['appointment'] ?? true,
'notify_reminder' => $notify['reminder'] ?? true, 'notify_reminder' => $notify['reminder'] ?? true,
'telegram_bot_token' => data_get($settings, 'telegram.bot_token'),
'reminder_after_days' => data_get($settings, 'reminder.after_days', 365),
'reminder_cooldown_days' => data_get($settings, 'reminder.cooldown_days', 30),
'shop_enabled' => data_get($settings, 'shop.enabled', false),
'shop_delivery_methods' => data_get($settings, 'shop.delivery_methods', ['pickup']),
'shop_delivery_fee' => data_get($settings, 'shop.delivery_fee', 0),
'shop_free_delivery_over' => data_get($settings, 'shop.free_delivery_over', 0),
'ai_default_provider' => $settings['ai']['default_provider'] ?? 'claude', 'ai_default_provider' => $settings['ai']['default_provider'] ?? 'claude',
'ai_claude_key' => $settings['ai']['claude_key'] ?? null, 'ai_claude_key' => $settings['ai']['claude_key'] ?? null,
'ai_gpt_key' => $settings['ai']['gpt_key'] ?? null, 'ai_gpt_key' => $settings['ai']['gpt_key'] ?? null,
'ai_gemini_key' => $settings['ai']['gemini_key'] ?? null, 'ai_gemini_key' => $settings['ai']['gemini_key'] ?? null,
'ai_model_claude' => data_get($settings, 'ai.models.claude', \App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['claude']),
'ai_model_gpt' => data_get($settings, 'ai.models.gpt', \App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['gpt']),
'ai_model_gemini' => data_get($settings, 'ai.models.gemini', \App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['gemini']),
]); ]);
} }
@@ -126,8 +138,8 @@ class Settings extends Page
->maxSize(512) ->maxSize(512)
->helperText('PNG/ICO, max 512 KB.'), ->helperText('PNG/ICO, max 512 KB.'),
]), ]),
Schemas\Components\Section::make('Notificări email') Schemas\Components\Section::make('Notificări')
->description('Activează / dezactivează emailurile auto către clienți.') ->description('Activează / dezactivează notificările auto către clienți. Telegram are prioritate dacă clientul are cont legat.')
->columns(2) ->columns(2)
->schema([ ->schema([
Forms\Components\Toggle::make('notify_wo_ready')->label('Mașina e gata de ridicat')->default(true), Forms\Components\Toggle::make('notify_wo_ready')->label('Mașina e gata de ridicat')->default(true),
@@ -135,6 +147,46 @@ class Settings extends Page
Forms\Components\Toggle::make('notify_appointment')->label('Programare confirmată')->default(true), Forms\Components\Toggle::make('notify_appointment')->label('Programare confirmată')->default(true),
Forms\Components\Toggle::make('notify_reminder')->label('Reminder ITP / revizie')->default(true), Forms\Components\Toggle::make('notify_reminder')->label('Reminder ITP / revizie')->default(true),
]), ]),
Schemas\Components\Section::make('Telegram bot')
->description('Creează un bot la @BotFather, lipește token-ul aici și apasă „Setează webhook". Clienții îți scriu la bot, partajează telefonul, iar codul se leagă automat de fișa lor.')
->columns(1)
->schema([
Forms\Components\TextInput::make('telegram_bot_token')
->label('Bot token')
->password()
->revealable()
->placeholder('123456:ABC-XYZ...')
->helperText(fn () => 'Webhook URL: ' .
app(\App\Services\Notifications\TelegramService::class)
->webhookUrlFor(app(\App\Tenancy\TenantManager::class)->current())),
]),
Schemas\Components\Section::make('Reminder service auto')
->columns(2)
->schema([
Forms\Components\TextInput::make('reminder_after_days')
->label('Trimite reminder după X zile fără vizită')
->numeric()
->minValue(30)
->default(365),
Forms\Components\TextInput::make('reminder_cooldown_days')
->label('Nu re-trimite mai des de X zile')
->numeric()
->minValue(7)
->default(30),
]),
Schemas\Components\Section::make('Magazin online')
->description('Activează magazinul public la <slug>.service.mir.md/shop. Piesele apar doar dacă sunt marcate „Publicat".')
->columns(2)
->schema([
Forms\Components\Toggle::make('shop_enabled')->label('Magazin activ')->columnSpanFull(),
Forms\Components\CheckboxList::make('shop_delivery_methods')
->label('Metode de livrare')
->options(\App\Models\Tenant\OnlineOrder::DELIVERY)
->default(['pickup'])
->columnSpanFull(),
Forms\Components\TextInput::make('shop_delivery_fee')->label('Taxă livrare')->numeric()->default(0),
Forms\Components\TextInput::make('shop_free_delivery_over')->label('Livrare gratuită peste')->numeric()->default(0)->helperText('0 = dezactivat'),
]),
Schemas\Components\Section::make('Asistent AI') Schemas\Components\Section::make('Asistent AI')
->description('Adaugă chei API ca să activezi asistentul. Cheile rămân la voi — nu sunt partajate.') ->description('Adaugă chei API ca să activezi asistentul. Cheile rămân la voi — nu sunt partajate.')
->columns(2) ->columns(2)
@@ -144,8 +196,20 @@ class Settings extends Page
->options(['claude' => 'Claude (Anthropic)', 'gpt' => 'ChatGPT (OpenAI)', 'gemini' => 'Gemini (Google)']) ->options(['claude' => 'Claude (Anthropic)', 'gpt' => 'ChatGPT (OpenAI)', 'gemini' => 'Gemini (Google)'])
->default('claude'), ->default('claude'),
Forms\Components\TextInput::make('ai_claude_key')->label('Claude API Key')->password()->revealable()->placeholder('sk-ant-...'), Forms\Components\TextInput::make('ai_claude_key')->label('Claude API Key')->password()->revealable()->placeholder('sk-ant-...'),
Forms\Components\Select::make('ai_model_claude')
->label('Model Claude')
->options(\App\Services\Ai\AiAssistantService::MODEL_OPTIONS['claude'])
->default(\App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['claude']),
Forms\Components\TextInput::make('ai_gpt_key')->label('OpenAI API Key')->password()->revealable()->placeholder('sk-proj-...'), Forms\Components\TextInput::make('ai_gpt_key')->label('OpenAI API Key')->password()->revealable()->placeholder('sk-proj-...'),
Forms\Components\Select::make('ai_model_gpt')
->label('Model OpenAI')
->options(\App\Services\Ai\AiAssistantService::MODEL_OPTIONS['gpt'])
->default(\App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['gpt']),
Forms\Components\TextInput::make('ai_gemini_key')->label('Gemini API Key')->password()->revealable(), Forms\Components\TextInput::make('ai_gemini_key')->label('Gemini API Key')->password()->revealable(),
Forms\Components\Select::make('ai_model_gemini')
->label('Model Gemini')
->options(\App\Services\Ai\AiAssistantService::MODEL_OPTIONS['gemini'])
->default(\App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['gemini']),
]), ]),
]) ])
->statePath('data'); ->statePath('data');
@@ -178,11 +242,30 @@ class Settings extends Page
'appointment' => (bool) ($data['notify_appointment'] ?? true), 'appointment' => (bool) ($data['notify_appointment'] ?? true),
'reminder' => (bool) ($data['notify_reminder'] ?? true), 'reminder' => (bool) ($data['notify_reminder'] ?? true),
], ],
'telegram' => array_replace(
(array) data_get($company->settings, 'telegram', []),
['bot_token' => $data['telegram_bot_token'] ?? null]
),
'reminder' => [
'after_days' => (int) ($data['reminder_after_days'] ?? 365),
'cooldown_days' => (int) ($data['reminder_cooldown_days'] ?? 30),
],
'shop' => [
'enabled' => (bool) ($data['shop_enabled'] ?? false),
'delivery_methods' => array_values((array) ($data['shop_delivery_methods'] ?? ['pickup'])),
'delivery_fee' => (float) ($data['shop_delivery_fee'] ?? 0),
'free_delivery_over' => (float) ($data['shop_free_delivery_over'] ?? 0),
],
'ai' => [ 'ai' => [
'default_provider' => $data['ai_default_provider'] ?? 'claude', 'default_provider' => $data['ai_default_provider'] ?? 'claude',
'claude_key' => $data['ai_claude_key'] ?? null, 'claude_key' => $data['ai_claude_key'] ?? null,
'gpt_key' => $data['ai_gpt_key'] ?? null, 'gpt_key' => $data['ai_gpt_key'] ?? null,
'gemini_key' => $data['ai_gemini_key'] ?? null, 'gemini_key' => $data['ai_gemini_key'] ?? null,
'models' => [
'claude' => $data['ai_model_claude'] ?? \App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['claude'],
'gpt' => $data['ai_model_gpt'] ?? \App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['gpt'],
'gemini' => $data['ai_model_gemini'] ?? \App\Services\Ai\AiAssistantService::MODEL_DEFAULTS['gemini'],
],
], ],
]), ]),
]); ]);
@@ -201,4 +284,76 @@ class Settings extends Page
Notification::make()->title('Setări salvate')->success()->send(); Notification::make()->title('Setări salvate')->success()->send();
} }
protected function getHeaderActions(): array
{
return [
Actions\Action::make('push_test')
->label('Test notificare push')
->icon('heroicon-m-bell-alert')
->color('gray')
->action(function () {
$svc = app(\App\Services\Notifications\WebPushService::class);
if (! $svc->configured()) {
Notification::make()
->title('Web Push neconfigurat')
->body('Rulează `php artisan push:vapid` și adaugă cheile în .env.')
->warning()->send();
return;
}
$r = $svc->sendToUser(
(int) auth()->id(),
'Test AutoCRM',
'Notificările push funcționează ✅',
'/app',
);
Notification::make()
->title($r['sent'] > 0 ? "Trimis pe {$r['sent']} dispozitiv(e)" : 'Niciun dispozitiv abonat')
->body($r['sent'] > 0 ? null : 'Deschide panoul pe telefon și acceptă notificările întâi.')
->{$r['sent'] > 0 ? 'success' : 'warning'}()
->send();
}),
Actions\Action::make('telegram_test')
->label('Testează bot Telegram')
->icon('heroicon-m-bolt')
->color('gray')
->action(function () {
$company = app(TenantManager::class)->current();
if (! $company) return;
$r = app(TelegramService::class)->getMe($company);
if (! ($r['ok'] ?? false)) {
Notification::make()
->title('Bot Telegram nu răspunde')
->body($r['error'] ?? 'Verifică token-ul.')
->danger()->send();
return;
}
$name = data_get($r, 'response.result.username', '?');
Notification::make()
->title("Bot OK: @{$name}")
->success()->send();
}),
Actions\Action::make('telegram_webhook')
->label('Setează webhook')
->icon('heroicon-m-link')
->color('primary')
->requiresConfirmation()
->modalDescription('Telegram va trimite mesajele primite la URL-ul webhook de mai jos.')
->action(function () {
$company = app(TenantManager::class)->current();
if (! $company) return;
$r = app(TelegramService::class)->setWebhook($company);
if (! ($r['ok'] ?? false)) {
Notification::make()
->title('Webhook eșuat')
->body($r['error'] ?? json_encode($r['response'] ?? []))
->danger()->send();
return;
}
Notification::make()
->title('Webhook setat — botul e gata')
->success()->send();
}),
];
}
} }
@@ -0,0 +1,166 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\BodyshopJobResource\Pages;
use App\Filament\Tenant\Resources\BodyshopJobResource\RelationManagers;
use App\Models\Tenant\BodyshopJob;
use App\Models\Tenant\Client;
use App\Models\Tenant\Vehicle;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class BodyshopJobResource extends Resource
{
protected static ?string $model = BodyshopJob::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-paint-brush';
protected static ?string $navigationLabel = 'Tinichigerie / Detailing';
protected static string|\UnitEnum|null $navigationGroup = 'Tinichigerie';
protected static ?string $modelLabel = 'lucrare caroserie';
protected static ?string $pluralModelLabel = 'lucrări caroserie';
protected static ?int $navigationSort = 80;
public static function getNavigationBadge(): ?string
{
$open = static::getModel()::query()
->whereNotIn('status', ['delivered', 'cancelled'])->count();
return $open > 0 ? (string) $open : null;
}
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Lucrare')
->columns(3)
->schema([
Forms\Components\TextInput::make('number')->label('Nr.')->disabled()->dehydrated(false)->placeholder('Generat automat'),
Forms\Components\Select::make('type')->label('Tip')->options(BodyshopJob::TYPES)->default('body_repair')->required(),
Forms\Components\Select::make('status')->label('Status')->options(BodyshopJob::STATUSES)->default('estimate')->required(),
Forms\Components\Select::make('client_id')
->label('Client')
->options(fn () => Client::pluck('name', 'id'))
->searchable()->live(),
Forms\Components\Select::make('vehicle_id')
->label('Auto')
->options(fn (Get $get) => $get('client_id')
? Vehicle::where('client_id', $get('client_id'))->get()
->mapWithKeys(fn ($v) => [$v->id => "{$v->make} {$v->model} {$v->plate}"])->toArray()
: [])
->searchable(),
Forms\Components\TextInput::make('estimate_amount')->label('Deviz')->numeric()->default(0),
Forms\Components\TextInput::make('approved_amount')->label('Aprobat')->numeric()->default(0),
]),
Schemas\Components\Section::make('Asigurare')
->collapsible()
->columns(3)
->schema([
Forms\Components\Toggle::make('is_insurance')->label('Caz de asigurare')->live()->columnSpanFull(),
Forms\Components\TextInput::make('insurer')->label('Asigurător')
->visible(fn (Get $get) => $get('is_insurance')),
Forms\Components\TextInput::make('policy_no')->label('Nr. poliță')
->visible(fn (Get $get) => $get('is_insurance')),
Forms\Components\TextInput::make('claim_no')->label('Nr. dosar daună')
->visible(fn (Get $get) => $get('is_insurance')),
Forms\Components\Select::make('insurance_status')->label('Status dosar')
->options(BodyshopJob::INSURANCE_STATUSES)
->visible(fn (Get $get) => $get('is_insurance')),
]),
Schemas\Components\Section::make('Foto înainte / după')
->columns(2)
->schema([
\Filament\Forms\Components\SpatieMediaLibraryFileUpload::make('photos_before')
->label('Înainte')->collection('photos_before')->multiple()->image()->reorderable()->maxFiles(20),
\Filament\Forms\Components\SpatieMediaLibraryFileUpload::make('photos_after')
->label('După')->collection('photos_after')->multiple()->image()->reorderable()->maxFiles(20),
]),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('number')->label('Nr.')->searchable()->sortable(),
Tables\Columns\TextColumn::make('client.name')->label('Client')->searchable()->placeholder('—'),
Tables\Columns\TextColumn::make('vehicle.plate')->label('Auto')->placeholder('—'),
Tables\Columns\TextColumn::make('type')
->formatStateUsing(fn ($s) => BodyshopJob::TYPES[$s] ?? $s)
->badge()->color('info'),
Tables\Columns\IconColumn::make('is_insurance')->label('Asig.')->boolean()->toggleable(),
Tables\Columns\TextColumn::make('damage_points_count')->counts('damagePoints')->label('Daune')->alignRight(),
Tables\Columns\TextColumn::make('approved_amount')->label('Aprobat')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => BodyshopJob::STATUSES[$s] ?? $s)
->badge()
->colors([
'gray' => ['estimate'],
'info' => ['approved', 'in_progress'],
'success' => ['done', 'delivered'],
'danger' => ['cancelled'],
]),
])
->filters([
Tables\Filters\SelectFilter::make('type')->options(BodyshopJob::TYPES),
Tables\Filters\SelectFilter::make('status')->options(BodyshopJob::STATUSES),
Tables\Filters\TernaryFilter::make('is_insurance')->label('Caz asigurare'),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Nicio lucrare de caroserie')
->emptyStateDescription('Înregistrează lucrări de tinichigerie, vopsitorie, PDR, detailing, ceramică, PPF sau polish. Hartă daune, dosar asigurare și arhivă foto înainte/după.')
->emptyStateIcon('heroicon-o-paint-brush')
->defaultSort('created_at', 'desc');
}
public static function getRelations(): array
{
return [
RelationManagers\DamagePointsRelationManager::class,
];
}
public static function getNavigationLabel(): string
{
return __('Tinichigerie / Detailing');
}
public static function getNavigationGroup(): ?string
{
return __('Tinichigerie');
}
public static function getModelLabel(): string
{
return __('lucrare caroserie');
}
public static function getPluralModelLabel(): string
{
return __('lucrări caroserie');
}
public static function getPages(): array
{
return [
'index' => Pages\ListBodyshopJobs::route('/'),
'create' => Pages\CreateBodyshopJob::route('/create'),
'edit' => Pages\EditBodyshopJob::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\BodyshopJobResource\Pages;
use App\Filament\Tenant\Resources\BodyshopJobResource;
use Filament\Resources\Pages\CreateRecord;
class CreateBodyshopJob extends CreateRecord
{
protected static string $resource = BodyshopJobResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\BodyshopJobResource\Pages;
use App\Filament\Tenant\Resources\BodyshopJobResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditBodyshopJob extends EditRecord
{
protected static string $resource = BodyshopJobResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\BodyshopJobResource\Pages;
use App\Filament\Tenant\Resources\BodyshopJobResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListBodyshopJobs extends ListRecords
{
protected static string $resource = BodyshopJobResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -0,0 +1,59 @@
<?php
namespace App\Filament\Tenant\Resources\BodyshopJobResource\RelationManagers;
use App\Models\Tenant\DamagePoint;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class DamagePointsRelationManager extends RelationManager
{
protected static string $relationship = 'damagePoints';
protected static ?string $title = 'Hartă daune';
public function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\Select::make('zone')
->label('Zonă')
->options(array_combine(DamagePoint::ZONES, DamagePoint::ZONES))
->searchable()
->required(),
Forms\Components\Select::make('kind')
->label('Tip daună')
->options(array_combine(DamagePoint::KINDS, DamagePoint::KINDS))
->required(),
Forms\Components\Select::make('severity')
->label('Gravitate')
->options(DamagePoint::SEVERITIES)
->default('minor')
->required(),
Forms\Components\Textarea::make('notes')->label('Observații')->rows(2)->columnSpanFull(),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('zone')
->columns([
Tables\Columns\TextColumn::make('zone')->label('Zonă')->badge()->color('gray'),
Tables\Columns\TextColumn::make('kind')->label('Tip'),
Tables\Columns\TextColumn::make('severity')
->label('Gravitate')
->formatStateUsing(fn ($s) => DamagePoint::SEVERITIES[$s] ?? $s)
->badge()
->colors(['gray' => ['minor'], 'warning' => ['medium'], 'danger' => ['severe']]),
Tables\Columns\TextColumn::make('notes')->limit(40)->placeholder('—'),
])
->headerActions([Actions\CreateAction::make()])
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()])
->emptyStateHeading('Nicio daună marcată')
->emptyStateDescription('Adaugă punctele de daună pe zone (capotă, ușă, aripă) cu tip și gravitate — formează harta de daune a mașinii.');
}
}
@@ -65,6 +65,9 @@ class ClientResource extends Resource
]) ])
->default('active') ->default('active')
->required(), ->required(),
Forms\Components\Toggle::make('is_vip')
->label('Client VIP')
->helperText('Activează coeficienții de preț VIP pe fișele acestui client.'),
]), ]),
Schemas\Components\Section::make('Contacte') Schemas\Components\Section::make('Contacte')
->columns(2) ->columns(2)
@@ -73,6 +76,14 @@ class ClientResource extends Resource
Forms\Components\TextInput::make('phone_alt')->label('Telefon alternativ')->tel()->maxLength(40), Forms\Components\TextInput::make('phone_alt')->label('Telefon alternativ')->tel()->maxLength(40),
Forms\Components\TextInput::make('email')->email()->maxLength(120), Forms\Components\TextInput::make('email')->email()->maxLength(120),
Forms\Components\TextInput::make('telegram')->maxLength(60), Forms\Components\TextInput::make('telegram')->maxLength(60),
Forms\Components\TextInput::make('telegram_chat_id')
->label('Telegram chat ID')
->disabled()
->dehydrated(false)
->placeholder('Se completează automat când clientul scrie la bot')
->helperText(fn ($record) => $record?->telegram_chat_id
? '✅ Telegram legat — notificările vor merge prin bot'
: null),
Forms\Components\TextInput::make('whatsapp')->maxLength(60), Forms\Components\TextInput::make('whatsapp')->maxLength(60),
Forms\Components\TextInput::make('viber')->maxLength(60), Forms\Components\TextInput::make('viber')->maxLength(60),
]), ]),
@@ -22,10 +22,15 @@ class DealResource extends Resource
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-funnel'; protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-funnel';
protected static ?string $navigationLabel = 'Pipeline'; protected static ?string $navigationLabel = 'Pipeline (tabel)';
protected static string|\UnitEnum|null $navigationGroup = 'CRM'; protected static string|\UnitEnum|null $navigationGroup = 'CRM';
public static function shouldRegisterNavigation(): bool
{
return false; // PipelineBoard page is the canonical entry; this resource keeps CRUD routes for edit/create.
}
protected static ?string $modelLabel = 'deal'; protected static ?string $modelLabel = 'deal';
protected static ?string $pluralModelLabel = 'deal-uri'; protected static ?string $pluralModelLabel = 'deal-uri';
@@ -29,6 +29,16 @@ class ExpenseResource extends Resource
protected static ?int $navigationSort = 51; protected static ?int $navigationSort = 51;
public static function canViewAny(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::FINANCE_VIEW_OVERVIEW) ?? false;
}
public static function canCreate(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::FINANCE_CREATE_EXPENSE) ?? false;
}
public static function form(Schema $schema): Schema public static function form(Schema $schema): Schema
{ {
return $schema->components([ return $schema->components([
@@ -3,6 +3,7 @@
namespace App\Filament\Tenant\Resources; namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\LaborResource\Pages; use App\Filament\Tenant\Resources\LaborResource\Pages;
use App\Filament\Tenant\Resources\LaborResource\RelationManagers;
use App\Models\Tenant\Labor; use App\Models\Tenant\Labor;
use Filament\Actions; use Filament\Actions;
use Filament\Forms; use Filament\Forms;
@@ -42,8 +43,17 @@ class LaborResource extends Resource
Forms\Components\TextInput::make('code')->label('Cod')->maxLength(32), Forms\Components\TextInput::make('code')->label('Cod')->maxLength(32),
Forms\Components\TextInput::make('name_ro')->label('Nume (RO)')->required()->maxLength(160), Forms\Components\TextInput::make('name_ro')->label('Nume (RO)')->required()->maxLength(160),
Forms\Components\TextInput::make('name_ru')->label('Nume (RU)')->maxLength(160), Forms\Components\TextInput::make('name_ru')->label('Nume (RU)')->maxLength(160),
Forms\Components\TextInput::make('hours')->label('Ore')->numeric()->default(1)->required(), Forms\Components\Select::make('pricing_mode')
Forms\Components\TextInput::make('price')->label('Preț (MDL)')->numeric()->default(0), ->label('Mod tarifare')
->options(Labor::PRICING_MODES)
->default('hourly')
->live()
->required(),
Forms\Components\TextInput::make('hours')->label('Ore (normă)')->numeric()->default(1)
->visible(fn (Schemas\Components\Utilities\Get $get) => $get('pricing_mode') !== 'fixed'),
Forms\Components\TextInput::make('fixed_price')->label('Preț fix (MDL)')->numeric()->default(0)
->visible(fn (Schemas\Components\Utilities\Get $get) => $get('pricing_mode') === 'fixed'),
Forms\Components\TextInput::make('price')->label('Preț orientativ (MDL)')->numeric()->default(0),
Forms\Components\Toggle::make('is_active')->label('Activă')->default(true), Forms\Components\Toggle::make('is_active')->label('Activă')->default(true),
]), ]),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2), Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
@@ -56,8 +66,15 @@ class LaborResource extends Resource
->columns([ ->columns([
Tables\Columns\TextColumn::make('category')->label('Categorie')->badge()->sortable(), Tables\Columns\TextColumn::make('category')->label('Categorie')->badge()->sortable(),
Tables\Columns\TextColumn::make('name_ro')->label('Manoperă')->searchable()->sortable(), Tables\Columns\TextColumn::make('name_ro')->label('Manoperă')->searchable()->sortable(),
Tables\Columns\TextColumn::make('pricing_mode')
->label('Tarifare')
->formatStateUsing(fn ($s) => $s === 'fixed' ? 'Fix' : 'Pe oră')
->badge()
->color(fn ($s) => $s === 'fixed' ? 'info' : 'gray'),
Tables\Columns\TextColumn::make('hours')->label('Ore')->numeric(decimalPlaces: 2)->alignRight(), Tables\Columns\TextColumn::make('hours')->label('Ore')->numeric(decimalPlaces: 2)->alignRight(),
Tables\Columns\TextColumn::make('price')->label('Preț')->money('MDL')->alignRight(), Tables\Columns\TextColumn::make('fixed_price')->label('Preț fix')->money('MDL')->alignRight()
->placeholder('—')->toggleable(),
Tables\Columns\TextColumn::make('laborParts_count')->counts('laborParts')->label('Piese impl.')->alignRight()->toggleable(),
Tables\Columns\IconColumn::make('is_active')->label('Activă')->boolean(), Tables\Columns\IconColumn::make('is_active')->label('Activă')->boolean(),
]) ])
->filters([ ->filters([
@@ -73,6 +90,13 @@ class LaborResource extends Resource
->defaultGroup('category'); ->defaultGroup('category');
} }
public static function getRelations(): array
{
return [
RelationManagers\DefaultPartsRelationManager::class,
];
}
public static function getPages(): array public static function getPages(): array
{ {
return [ return [
@@ -0,0 +1,57 @@
<?php
namespace App\Filament\Tenant\Resources\LaborResource\RelationManagers;
use App\Models\Tenant\Part;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class DefaultPartsRelationManager extends RelationManager
{
protected static string $relationship = 'laborParts';
protected static ?string $title = 'Piese implicite';
public function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\Select::make('part_id')
->label('Piesă')
->options(fn () => Part::where('is_active', true)
->get()
->mapWithKeys(fn ($p) => [$p->id => "{$p->name} " . ($p->article ? "[{$p->article}]" : '')])
->toArray())
->searchable()
->required()
->live()
->afterStateUpdated(function ($state, Set $set) {
if ($state && $p = Part::find($state)) {
$set('unit', $p->unit);
}
}),
Forms\Components\TextInput::make('qty')->label('Cantitate')->numeric()->default(1)->required(),
Forms\Components\TextInput::make('unit')->label('UM')->default('buc')->maxLength(16),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('part.name')
->columns([
Tables\Columns\TextColumn::make('part.name')->label('Piesă')->wrap(),
Tables\Columns\TextColumn::make('part.article')->label('Cod')->placeholder('—'),
Tables\Columns\TextColumn::make('qty')->label('Cant.')->alignRight(),
Tables\Columns\TextColumn::make('unit')->label('UM'),
])
->headerActions([Actions\CreateAction::make()])
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()])
->emptyStateHeading('Nicio piesă implicită')
->emptyStateDescription('Adaugă piesele care se montează de obicei la această manoperă — se adaugă automat în fișă când selectezi manopera.');
}
}
@@ -0,0 +1,142 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\OnlineOrderResource\Pages;
use App\Filament\Tenant\Resources\OnlineOrderResource\RelationManagers;
use App\Models\Tenant\OnlineOrder;
use Filament\Actions;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class OnlineOrderResource extends Resource
{
protected static ?string $model = OnlineOrder::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-shopping-bag';
protected static ?string $navigationLabel = 'Comenzi online';
protected static string|\UnitEnum|null $navigationGroup = 'Magazin';
protected static ?string $modelLabel = 'comandă';
protected static ?string $pluralModelLabel = 'comenzi online';
protected static ?int $navigationSort = 50;
public static function getNavigationBadge(): ?string
{
$new = static::getModel()::query()->where('status', 'new')->count();
return $new > 0 ? (string) $new : null;
}
public static function getNavigationBadgeColor(): ?string
{
return 'warning';
}
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Comandă')
->columns(3)
->schema([
Forms\Components\TextInput::make('number')->label('Nr.')->disabled()->dehydrated(false),
Forms\Components\Select::make('status')->options(OnlineOrder::STATUSES)->required(),
Forms\Components\Select::make('delivery_method')->label('Livrare')->options(OnlineOrder::DELIVERY)->required(),
Forms\Components\TextInput::make('customer_name')->label('Client')->required(),
Forms\Components\TextInput::make('customer_phone')->label('Telefon')->required(),
Forms\Components\TextInput::make('customer_email')->label('Email'),
Forms\Components\TextInput::make('address')->label('Adresă')->columnSpan(2),
Forms\Components\TextInput::make('delivery_fee')->label('Taxă livrare')->numeric(),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('number')->label('Nr.')->searchable()->sortable(),
Tables\Columns\TextColumn::make('created_at')->label('Data')->dateTime('d.m.Y H:i')->sortable(),
Tables\Columns\TextColumn::make('customer_name')->label('Client')->searchable(),
Tables\Columns\TextColumn::make('customer_phone')->label('Telefon')->copyable(),
Tables\Columns\TextColumn::make('delivery_method')
->label('Livrare')
->formatStateUsing(fn ($s) => OnlineOrder::DELIVERY[$s] ?? $s),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => OnlineOrder::STATUSES[$s] ?? $s)
->badge()
->colors([
'warning' => ['new'],
'info' => ['confirmed', 'packed'],
'primary' => ['shipped'],
'success' => ['delivered'],
'danger' => ['cancelled'],
]),
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight()->sortable(),
])
->filters([
Tables\Filters\SelectFilter::make('status')->options(OnlineOrder::STATUSES),
])
->actions([
Actions\Action::make('fulfill')
->label('Onorează (scade stoc)')
->icon('heroicon-m-check-badge')
->color('success')
->visible(fn (OnlineOrder $r) => ! in_array($r->status, ['delivered', 'cancelled'], true))
->requiresConfirmation()
->modalDescription('Scade din stoc piesele legate de catalog (FIFO) și marchează comanda confirmată.')
->action(function (OnlineOrder $r) {
$svc = app(\App\Services\Warehouse\WarehouseService::class);
$issued = 0; $skipped = 0;
foreach ($r->items as $item) {
if ($item->fulfilled) continue;
if (! $item->part_id) { $skipped++; continue; }
$part = \App\Models\Tenant\Part::find($item->part_id);
if (! $part) { $skipped++; continue; }
try {
$svc->issue($part, (float) $item->qty, null, $r, "Comandă online #{$r->number}");
$item->fulfilled = true;
$item->save();
$issued++;
} catch (\App\Services\Warehouse\InsufficientStockException $e) {
$skipped++;
}
}
if ($r->status === 'new') {
$r->status = 'confirmed';
$r->save();
}
Notification::make()
->title("Onorat: {$issued} linii scăzute" . ($skipped ? ", {$skipped} sărite (stoc/lipsă link)" : ''))
->{$skipped ? 'warning' : 'success'}()
->send();
}),
Actions\EditAction::make(),
])
->defaultSort('created_at', 'desc');
}
public static function getRelations(): array
{
return [
RelationManagers\ItemsRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListOnlineOrders::route('/'),
'edit' => Pages\EditOnlineOrder::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\OnlineOrderResource\Pages;
use App\Filament\Tenant\Resources\OnlineOrderResource;
use Filament\Resources\Pages\EditRecord;
class EditOnlineOrder extends EditRecord
{
protected static string $resource = OnlineOrderResource::class;
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\OnlineOrderResource\Pages;
use App\Filament\Tenant\Resources\OnlineOrderResource;
use Filament\Resources\Pages\ListRecords;
class ListOnlineOrders extends ListRecords
{
protected static string $resource = OnlineOrderResource::class;
}
@@ -0,0 +1,28 @@
<?php
namespace App\Filament\Tenant\Resources\OnlineOrderResource\RelationManagers;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
class ItemsRelationManager extends RelationManager
{
protected static string $relationship = 'items';
protected static ?string $title = 'Produse';
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('name')->label('Piesă')->wrap(),
Tables\Columns\TextColumn::make('article')->label('Cod')->placeholder('—'),
Tables\Columns\TextColumn::make('qty')->label('Cant.')->alignRight(),
Tables\Columns\TextColumn::make('price')->label('Preț')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(),
Tables\Columns\IconColumn::make('fulfilled')->label('Onorat')->boolean(),
]);
}
}
@@ -3,6 +3,7 @@
namespace App\Filament\Tenant\Resources; namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\PartResource\Pages; use App\Filament\Tenant\Resources\PartResource\Pages;
use App\Filament\Tenant\Resources\PartResource\RelationManagers;
use App\Models\Tenant\Part; use App\Models\Tenant\Part;
use App\Models\Tenant\Supplier; use App\Models\Tenant\Supplier;
use Filament\Actions; use Filament\Actions;
@@ -79,6 +80,10 @@ class PartResource extends Resource
Forms\Components\TextInput::make('unit')->label('UM')->default('buc')->maxLength(16), Forms\Components\TextInput::make('unit')->label('UM')->default('buc')->maxLength(16),
Forms\Components\TextInput::make('min_qty')->label('Minim')->numeric()->default(0), Forms\Components\TextInput::make('min_qty')->label('Minim')->numeric()->default(0),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true), Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
Forms\Components\Toggle::make('is_published')
->label('Publicat în magazin')
->helperText('Apare în magazinul online public.')
->default(false),
]), ]),
Schemas\Components\Section::make('Prețuri') Schemas\Components\Section::make('Prețuri')
->columns(2) ->columns(2)
@@ -94,6 +99,21 @@ class PartResource extends Resource
->options(fn () => Supplier::pluck('name', 'id')) ->options(fn () => Supplier::pluck('name', 'id'))
->searchable(), ->searchable(),
]), ]),
Schemas\Components\Section::make('Imagine')
->collapsible()
->schema([
\Filament\Forms\Components\SpatieMediaLibraryFileUpload::make('image')
->label('Foto piesă')
->collection('image')
->multiple()
->reorderable()
->image()
->imageEditor()
->maxFiles(8)
->maxSize(2048)
->columnSpanFull()
->helperText('Galerie de până la 8 imagini. Prima e afișată în catalog. Max 2 MB / imagine.'),
]),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2), Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
]); ]);
} }
@@ -102,6 +122,11 @@ class PartResource extends Resource
{ {
return $table return $table
->columns([ ->columns([
\Filament\Tables\Columns\SpatieMediaLibraryImageColumn::make('image')
->label('')
->collection('image')
->circular()
->size(32),
Tables\Columns\TextColumn::make('name')->searchable()->sortable()->wrap(), Tables\Columns\TextColumn::make('name')->searchable()->sortable()->wrap(),
Tables\Columns\TextColumn::make('article')->label('Cod')->searchable()->copyable()->placeholder('—'), Tables\Columns\TextColumn::make('article')->label('Cod')->searchable()->copyable()->placeholder('—'),
Tables\Columns\TextColumn::make('brand')->placeholder('—'), Tables\Columns\TextColumn::make('brand')->placeholder('—'),
@@ -112,9 +137,16 @@ class PartResource extends Resource
->alignRight() ->alignRight()
->color(fn ($state, $record) => $record->qty <= 0 ? 'danger' : ($record->qty <= $record->min_qty ? 'warning' : null)) ->color(fn ($state, $record) => $record->qty <= 0 ? 'danger' : ($record->qty <= $record->min_qty ? 'warning' : null))
->weight(fn ($state, $record) => $record->qty <= $record->min_qty ? 'bold' : null), ->weight(fn ($state, $record) => $record->qty <= $record->min_qty ? 'bold' : null),
Tables\Columns\TextColumn::make('qty_reserved')
->label('Rezervat')
->numeric(decimalPlaces: 2)
->alignRight()
->color(fn ($state) => (float) $state > 0 ? 'info' : null)
->toggleable(),
Tables\Columns\TextColumn::make('unit')->label('UM'), Tables\Columns\TextColumn::make('unit')->label('UM'),
Tables\Columns\TextColumn::make('location')->label('Loc.')->placeholder('—'), Tables\Columns\TextColumn::make('location')->label('Loc.')->placeholder('—'),
Tables\Columns\TextColumn::make('sell_price')->label('Preț vz.')->money('MDL')->alignRight(), Tables\Columns\TextColumn::make('sell_price')->label('Preț vz.')->money('MDL')->alignRight(),
Tables\Columns\IconColumn::make('is_published')->label('Magazin')->boolean()->toggleable(),
Tables\Columns\TextColumn::make('preferredSupplier.name')->label('Furnizor')->placeholder('—')->toggleable(), Tables\Columns\TextColumn::make('preferredSupplier.name')->label('Furnizor')->placeholder('—')->toggleable(),
]) ])
->filters([ ->filters([
@@ -128,15 +160,115 @@ class PartResource extends Resource
->query(fn ($q) => $q->where('qty', '<=', 0)), ->query(fn ($q) => $q->where('qty', '<=', 0)),
]) ])
->actions([ ->actions([
Actions\Action::make('qr')
->label('QR')
->icon('heroicon-m-qr-code')
->color('gray')
->modalHeading(fn (Part $r) => 'QR pentru ' . $r->name)
->modalSubmitAction(false)
->modalCancelActionLabel('Închide')
->modalContent(function (Part $r) {
$payload = 'PART:' . ($r->article ?: $r->id);
$svg = (new \chillerlan\QRCode\QRCode(new \chillerlan\QRCode\QROptions([
'outputType' => \chillerlan\QRCode\QRCode::OUTPUT_MARKUP_SVG,
'eccLevel' => \chillerlan\QRCode\QRCode::ECC_M,
'scale' => 8,
'imageBase64' => false,
'addQuietzone' => true,
])))->render($payload);
return view('filament.tenant.part-qr', [
'part' => $r, 'svg' => $svg, 'payload' => $payload,
]);
}),
Actions\Action::make('ai_price')
->label('AI: preț recomandat')
->icon('heroicon-m-sparkles')
->color('primary')
->modalHeading(fn (Part $r) => "AI: preț pentru {$r->name}")
->modalSubmitAction(false)
->modalCancelActionLabel('Închide')
->modalContent(function (Part $r) {
[$reply, $meta] = app(\App\Services\Ai\AiAssistantService::class)
->suggestPrice($r);
return view('filament.tenant.ai-reply', ['reply' => $reply, 'meta' => $meta]);
}),
Actions\Action::make('receive')
->label('Recepție')
->icon('heroicon-m-arrow-down-tray')
->color('success')
->schema([
Forms\Components\TextInput::make('qty')->label('Cantitate')->numeric()->required()->minValue(0.001),
Forms\Components\TextInput::make('buy_price')->label('Preț unitar')->numeric()->required(),
Forms\Components\Select::make('supplier_id')
->label('Furnizor')
->options(fn () => \App\Models\Tenant\Supplier::pluck('name', 'id')),
Forms\Components\Select::make('warehouse_id')
->label('Depozit')
->options(fn () => \App\Models\Tenant\Warehouse::where('is_active', true)->pluck('name', 'id'))
->default(fn () => \App\Models\Tenant\Warehouse::where('is_default', true)->value('id')),
Forms\Components\TextInput::make('batch_ref')->label('Ref. lot/factură')->maxLength(64),
])
->action(function (Part $record, array $data) {
$warehouse = $data['warehouse_id']
? \App\Models\Tenant\Warehouse::find($data['warehouse_id'])
: null;
$supplier = $data['supplier_id']
? \App\Models\Tenant\Supplier::find($data['supplier_id'])
: null;
app(\App\Services\Warehouse\WarehouseService::class)->receive(
part: $record,
qty: (float) $data['qty'],
buyPrice: (float) $data['buy_price'],
warehouse: $warehouse,
supplier: $supplier,
batchRef: $data['batch_ref'] ?? null,
);
\Filament\Notifications\Notification::make()
->title('Stoc adăugat')
->success()
->send();
}),
Actions\EditAction::make(), Actions\EditAction::make(),
Actions\DeleteAction::make(), Actions\DeleteAction::make(),
]) ])
->bulkActions([
Actions\BulkAction::make('print_labels')
->label('Tipărește etichete QR')
->icon('heroicon-m-printer')
->color('gray')
->action(function ($records) {
$ids = collect($records)->pluck('id')->implode(',');
return redirect()->away('/parts/labels?ids=' . $ids);
})
->deselectRecordsAfterCompletion(),
Actions\BulkAction::make('publish')
->label('Publică în magazin')
->icon('heroicon-m-globe-alt')
->color('success')
->action(fn ($records) => collect($records)->each->update(['is_published' => true]))
->deselectRecordsAfterCompletion(),
Actions\BulkAction::make('unpublish')
->label('Scoate din magazin')
->icon('heroicon-m-eye-slash')
->color('gray')
->action(fn ($records) => collect($records)->each->update(['is_published' => false]))
->deselectRecordsAfterCompletion(),
])
->emptyStateHeading('Depozit gol') ->emptyStateHeading('Depozit gol')
->emptyStateDescription('Adaugă piese manual, sau folosește Achiziții ca să le adaugi prin recepție de la furnizor (cu prețuri și stoc auto). Procentaj poate seta automat prețul de vânzare.') ->emptyStateDescription('Adaugă piese manual, sau folosește Achiziții ca să le adaugi prin recepție de la furnizor (cu prețuri și stoc auto). Procentaj poate seta automat prețul de vânzare.')
->emptyStateIcon('heroicon-o-cube') ->emptyStateIcon('heroicon-o-cube')
->defaultSort('name'); ->defaultSort('name');
} }
public static function getRelations(): array
{
return [
RelationManagers\BatchesRelationManager::class,
RelationManagers\PriceHistoryRelationManager::class,
RelationManagers\CrossRefsRelationManager::class,
];
}
public static function getPages(): array public static function getPages(): array
{ {
return [ return [
@@ -0,0 +1,45 @@
<?php
namespace App\Filament\Tenant\Resources\PartResource\RelationManagers;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
class BatchesRelationManager extends RelationManager
{
protected static string $relationship = 'batches';
protected static ?string $title = 'Loturi (FIFO)';
public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('received_at')
->label('Recepție')
->dateTime('d.m.Y H:i')
->sortable(),
Tables\Columns\TextColumn::make('warehouse.code')->label('Depozit')->placeholder('—'),
Tables\Columns\TextColumn::make('batch_ref')->label('Ref.')->placeholder('—'),
Tables\Columns\TextColumn::make('supplier.name')->label('Furnizor')->placeholder('—'),
Tables\Columns\TextColumn::make('qty_in')
->label('Intrat')
->numeric(decimalPlaces: 2)
->alignRight(),
Tables\Columns\TextColumn::make('qty_remaining')
->label('Rămas')
->numeric(decimalPlaces: 2)
->alignRight()
->weight('bold')
->color(fn ($state) => (float) $state <= 0 ? 'gray' : 'success'),
Tables\Columns\TextColumn::make('buy_price')
->label('Preț unit.')
->money('MDL')
->alignRight(),
])
->defaultSort('received_at')
->emptyStateHeading('Niciun lot înregistrat')
->emptyStateDescription('Apasă „Recepție" pe lista de piese pentru a înregistra prima intrare în depozit.');
}
}
@@ -0,0 +1,39 @@
<?php
namespace App\Filament\Tenant\Resources\PartResource\RelationManagers;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class CrossRefsRelationManager extends RelationManager
{
protected static string $relationship = 'crossRefs';
protected static ?string $title = 'Coduri cross (OEM/echivalente)';
public function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\TextInput::make('cross_article')->label('Cod echivalent')->required()->maxLength(64),
Forms\Components\TextInput::make('brand')->label('Brand')->maxLength(64),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('cross_article')
->columns([
Tables\Columns\TextColumn::make('cross_article')->label('Cod')->searchable(),
Tables\Columns\TextColumn::make('brand')->placeholder('—'),
])
->headerActions([Actions\CreateAction::make()])
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()])
->emptyStateHeading('Niciun cod cross')
->emptyStateDescription('Adaugă coduri echivalente OEM/aftermarket ca să fie găsite în căutarea din magazin.');
}
}
@@ -0,0 +1,35 @@
<?php
namespace App\Filament\Tenant\Resources\PartResource\RelationManagers;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
class PriceHistoryRelationManager extends RelationManager
{
protected static string $relationship = 'priceHistory';
protected static ?string $title = 'Istoric prețuri furnizori';
public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('observed_at')
->label('Data')
->dateTime('d.m.Y H:i')
->sortable(),
Tables\Columns\TextColumn::make('supplier.name')->label('Furnizor')->searchable(),
Tables\Columns\TextColumn::make('purchase.number')->label('PO')->placeholder('—'),
Tables\Columns\TextColumn::make('price')
->money('MDL')
->alignRight()
->sortable(),
Tables\Columns\TextColumn::make('currency')->label('Val.'),
])
->defaultSort('observed_at', 'desc')
->emptyStateHeading('Niciun preț înregistrat')
->emptyStateDescription('Prețurile se înregistrează automat la fiecare recepție de PO.');
}
}
@@ -30,6 +30,21 @@ class PaymentResource extends Resource
protected static ?int $navigationSort = 50; protected static ?int $navigationSort = 50;
public static function canViewAny(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::FINANCE_VIEW_OVERVIEW) ?? false;
}
public static function canCreate(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::FINANCE_CREATE_PAYMENT) ?? false;
}
public static function canDelete($record): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::FINANCE_DELETE_PAYMENT) ?? false;
}
public static function form(Schema $schema): Schema public static function form(Schema $schema): Schema
{ {
return $schema->components([ return $schema->components([
@@ -29,6 +29,16 @@ class PayrollAdjustmentResource extends Resource
protected static ?int $navigationSort = 54; protected static ?int $navigationSort = 54;
public static function canViewAny(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::SALARIES_VIEW_ALL) ?? false;
}
public static function canCreate(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::SALARIES_CALCULATE) ?? false;
}
public static function form(Schema $schema): Schema public static function form(Schema $schema): Schema
{ {
return $schema->components([ return $schema->components([
@@ -31,6 +31,16 @@ class PayrollRunResource extends Resource
protected static ?int $navigationSort = 53; protected static ?int $navigationSort = 53;
public static function canViewAny(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::SALARIES_VIEW_ALL) ?? false;
}
public static function canCreate(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::SALARIES_CALCULATE) ?? false;
}
public static function form(Schema $schema): Schema public static function form(Schema $schema): Schema
{ {
return $schema->components([ return $schema->components([
@@ -0,0 +1,103 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Auth\Permissions;
use App\Filament\Tenant\Resources\PostResource\Pages;
use App\Models\Tenant\Post;
use App\Models\Tenant\User;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class PostResource extends Resource
{
protected static ?string $model = Post::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-wrench-screwdriver';
protected static ?string $navigationLabel = 'Posturi de lucru';
protected static string|\UnitEnum|null $navigationGroup = 'Admin';
protected static ?string $modelLabel = 'pod';
protected static ?string $pluralModelLabel = 'posturi de lucru';
protected static ?int $navigationSort = 76;
public static function canViewAny(): bool
{
return auth()->user()?->canDo(Permissions::ADMIN_SETTINGS_EDIT) ?? false;
}
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Pod / Spațiu lucru')
->columns(2)
->schema([
Forms\Components\TextInput::make('name')
->label('Nume')
->required()
->maxLength(80)
->placeholder('Ex: Pod 1, Curte 1, Atelier electric'),
Forms\Components\ColorPicker::make('color')
->default('#3b82f6'),
Forms\Components\TextInput::make('hours_per_day')
->label('Ore disponibile / zi')
->numeric()
->step(0.5)
->default(10)
->helperText('Capacitatea zilnică în ore'),
Forms\Components\Select::make('default_master_id')
->label('Mecanic implicit')
->options(fn () => User::where('status', 'active')->pluck('name', 'id'))
->searchable()
->placeholder('Niciun mecanic implicit')
->helperText('Va fi pre-completat când creezi o programare pentru acest pod'),
Forms\Components\TextInput::make('description')
->label('Descriere')
->maxLength(255)
->placeholder('Ex: cu lift, fără lift, doar diagnoză...')
->columnSpanFull(),
Forms\Components\TextInput::make('sort_order')
->numeric()
->default(100),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\ColorColumn::make('color'),
Tables\Columns\TextColumn::make('hours_per_day')->label('Ore/zi')->sortable(),
Tables\Columns\TextColumn::make('defaultMaster.name')->label('Mecanic implicit')->placeholder('—'),
Tables\Columns\TextColumn::make('description')->placeholder('—')->limit(40)->toggleable(),
Tables\Columns\TextColumn::make('appointments_count')->counts('appointments')->label('Programări')->badge(),
Tables\Columns\ToggleColumn::make('is_active')->label('Activ'),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->defaultSort('sort_order');
}
public static function getPages(): array
{
return [
'index' => Pages\ListPosts::route('/'),
'create' => Pages\CreatePost::route('/create'),
'edit' => Pages\EditPost::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\PostResource\Pages;
use App\Filament\Tenant\Resources\PostResource;
use Filament\Resources\Pages\CreateRecord;
class CreatePost extends CreateRecord
{
protected static string $resource = PostResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\PostResource\Pages;
use App\Filament\Tenant\Resources\PostResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditPost extends EditRecord
{
protected static string $resource = PostResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\PostResource\Pages;
use App\Filament\Tenant\Resources\PostResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListPosts extends ListRecords
{
protected static string $resource = PostResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -0,0 +1,139 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\PricingCoefficientResource\Pages;
use App\Models\Tenant\PricingCoefficient;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class PricingCoefficientResource extends Resource
{
protected static ?string $model = PricingCoefficient::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-adjustments-horizontal';
protected static ?string $navigationLabel = 'Coeficienți preț';
protected static string|\UnitEnum|null $navigationGroup = 'Depozit';
protected static ?string $modelLabel = 'coeficient';
protected static ?string $pluralModelLabel = 'coeficienți preț';
protected static ?int $navigationSort = 46;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Coeficient')
->columns(2)
->schema([
Forms\Components\TextInput::make('name')->label('Denumire')->required()
->placeholder('ex: Mașină veche, Client VIP, Express')->columnSpanFull(),
Forms\Components\TextInput::make('multiplier')
->label('Multiplicator')
->numeric()
->required()
->default(1.10)
->helperText('1.15 = +15% peste prețul de bază. 0.95 = -5%.'),
Forms\Components\TextInput::make('priority')->label('Prioritate')->numeric()->default(100),
Forms\Components\Toggle::make('stackable')
->label('Cumulabil')
->default(true)
->helperText('Cumulabil = se înmulțește cu alți coeficienți. Necumulabil = doar cel mai mare necumulabil se aplică.'),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
]),
Schemas\Components\Section::make('Condiții (toate trebuie îndeplinite)')
->description('Lasă gol = se aplică mereu. Combină condițiile pentru a ținti situații specifice.')
->columns(2)
->schema([
Forms\Components\CheckboxList::make('conditions.classes')
->label('Clase auto')
->options(PricingCoefficient::VEHICLE_CLASSES)
->columns(2)
->columnSpanFull(),
Forms\Components\CheckboxList::make('conditions.body_types')
->label('Caroserie')
->options(\App\Models\Tenant\Vehicle::BODY_TYPES)
->columns(3)
->columnSpanFull(),
Forms\Components\CheckboxList::make('conditions.transmissions')
->label('Cutie de viteze')
->options(\App\Models\Tenant\Vehicle::TRANSMISSION_TYPES)
->columns(3)
->columnSpanFull(),
Forms\Components\TextInput::make('conditions.age_min')->label('Vârstă min (ani)')->numeric(),
Forms\Components\TextInput::make('conditions.age_max')->label('Vârstă max (ani)')->numeric(),
Forms\Components\Toggle::make('conditions.client_vip')->label('Doar clienți VIP'),
Forms\Components\CheckboxList::make('conditions.urgency')
->label('Urgență')
->options(PricingCoefficient::URGENCY)
->columns(3)
->columnSpanFull(),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('priority')->label('Prio')->sortable()->alignRight(),
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('multiplier')
->label('Multiplicator')
->formatStateUsing(fn ($s) => '×' . rtrim(rtrim(number_format((float) $s, 3), '0'), '.'))
->alignRight()
->color(fn ($s) => (float) $s >= 1 ? 'success' : 'warning'),
Tables\Columns\IconColumn::make('stackable')->label('Cumul.')->boolean(),
Tables\Columns\IconColumn::make('is_active')->label('Activ')->boolean(),
])
->filters([
Tables\Filters\TernaryFilter::make('is_active')->label('Active'),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Niciun coeficient')
->emptyStateDescription('Adaugă reguli care ajustează prețul în funcție de vârsta mașinii, clasă (SUV, comercial, hibrid), client VIP sau urgență. Se aplică peste markup-ul de bază pe fișele de lucru.')
->emptyStateIcon('heroicon-o-adjustments-horizontal')
->defaultSort('priority');
}
public static function getNavigationLabel(): string
{
return __('Coeficienți preț');
}
public static function getNavigationGroup(): ?string
{
return __('Depozit');
}
public static function getModelLabel(): string
{
return __('coeficient');
}
public static function getPluralModelLabel(): string
{
return __('coeficienți preț');
}
public static function getPages(): array
{
return [
'index' => Pages\ListPricingCoefficients::route('/'),
'create' => Pages\CreatePricingCoefficient::route('/create'),
'edit' => Pages\EditPricingCoefficient::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\PricingCoefficientResource\Pages;
use App\Filament\Tenant\Resources\PricingCoefficientResource;
use Filament\Resources\Pages\CreateRecord;
class CreatePricingCoefficient extends CreateRecord
{
protected static string $resource = PricingCoefficientResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\PricingCoefficientResource\Pages;
use App\Filament\Tenant\Resources\PricingCoefficientResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditPricingCoefficient extends EditRecord
{
protected static string $resource = PricingCoefficientResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\PricingCoefficientResource\Pages;
use App\Filament\Tenant\Resources\PricingCoefficientResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListPricingCoefficients extends ListRecords
{
protected static string $resource = PricingCoefficientResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -6,6 +6,7 @@ use App\Filament\Tenant\Resources\PurchaseResource\Pages;
use App\Filament\Tenant\Resources\PurchaseResource\RelationManagers; use App\Filament\Tenant\Resources\PurchaseResource\RelationManagers;
use App\Models\Tenant\Purchase; use App\Models\Tenant\Purchase;
use App\Models\Tenant\Supplier; use App\Models\Tenant\Supplier;
use App\Models\Tenant\Warehouse;
use Filament\Actions; use Filament\Actions;
use Filament\Forms; use Filament\Forms;
use Filament\Notifications\Notification; use Filament\Notifications\Notification;
@@ -43,6 +44,11 @@ class PurchaseResource extends Resource
->options(fn () => Supplier::where('is_active', true)->pluck('name', 'id')) ->options(fn () => Supplier::where('is_active', true)->pluck('name', 'id'))
->searchable() ->searchable()
->required(), ->required(),
Forms\Components\Select::make('warehouse_id')
->label('Depozit țintă')
->options(fn () => Warehouse::where('is_active', true)->pluck('name', 'id'))
->default(fn () => Warehouse::where('is_default', true)->value('id'))
->required(),
Forms\Components\Select::make('status') Forms\Components\Select::make('status')
->options(Purchase::STATUSES) ->options(Purchase::STATUSES)
->default('draft') ->default('draft')
@@ -71,9 +77,19 @@ class PurchaseResource extends Resource
->colors([ ->colors([
'gray' => ['draft'], 'gray' => ['draft'],
'warning' => ['ordered'], 'warning' => ['ordered'],
'info' => ['partial'],
'success' => ['received'], 'success' => ['received'],
'danger' => ['cancelled'], 'danger' => ['cancelled'],
]), ]),
Tables\Columns\TextColumn::make('received_progress')
->label('Progres')
->state(function (Purchase $r) {
$items = $r->items;
$ord = (float) $items->sum('qty');
$rec = (float) $items->sum('qty_received');
return $ord > 0 ? sprintf('%d%%', (int) round($rec / $ord * 100)) : '—';
})
->alignRight(),
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(), Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(),
]) ])
->filters([ ->filters([
@@ -83,19 +99,27 @@ class PurchaseResource extends Resource
->options(fn () => Supplier::pluck('name', 'id')), ->options(fn () => Supplier::pluck('name', 'id')),
]) ])
->actions([ ->actions([
Actions\Action::make('receive') Actions\Action::make('receive_all')
->label('Recepționează') ->label('Recepție totală')
->icon('heroicon-m-check-circle') ->icon('heroicon-m-check-circle')
->color('success') ->color('success')
->visible(fn (Purchase $r) => $r->status !== 'received' && $r->status !== 'cancelled') ->visible(fn (Purchase $r) => ! in_array($r->status, ['received', 'cancelled', 'draft'], true))
->requiresConfirmation() ->requiresConfirmation()
->modalDescription('Se va incrementa stocul pieselor legate.') ->modalDescription('Se vor crea batch-uri pentru toate restanțele rămase în depozitul țintă.')
->action(function (Purchase $r) { ->action(function (Purchase $r) {
$r->markReceived(); try {
$r->receiveAllRemaining();
Notification::make() Notification::make()
->title('Recepționat — stoc actualizat') ->title('Recepție completă — batch-uri create')
->success() ->success()
->send(); ->send();
} catch (\Throwable $e) {
Notification::make()
->title('Eroare')
->body($e->getMessage())
->danger()
->send();
}
}), }),
Actions\EditAction::make(), Actions\EditAction::make(),
Actions\DeleteAction::make(), Actions\DeleteAction::make(),
@@ -3,8 +3,15 @@
namespace App\Filament\Tenant\Resources\PurchaseResource\Pages; namespace App\Filament\Tenant\Resources\PurchaseResource\Pages;
use App\Filament\Tenant\Resources\PurchaseResource; use App\Filament\Tenant\Resources\PurchaseResource;
use App\Models\Tenant\Purchase;
use App\Models\Tenant\PurchaseItem;
use App\Models\Tenant\Supplier;
use App\Services\Ai\OcrInvoiceService;
use Filament\Actions; use Filament\Actions;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords; use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\Storage;
class ListPurchases extends ListRecords class ListPurchases extends ListRecords
{ {
@@ -12,6 +19,78 @@ class ListPurchases extends ListRecords
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
return [Actions\CreateAction::make()]; return [
Actions\Action::make('ocr')
->label('Import factură (OCR)')
->icon('heroicon-m-document-arrow-up')
->color('gray')
->modalHeading('Import factură via OCR')
->modalDescription('Încarcă o poză cu factura. AI-ul extrage furnizorul, data și liniile. Verifici și salvezi.')
->schema([
Forms\Components\FileUpload::make('invoice')
->label('Foto factură')
->image()
->disk('local')
->directory('ocr-imports')
->required()
->maxSize(5120),
])
->action(function (array $data) {
$abs = Storage::disk('local')->path($data['invoice']);
$result = app(OcrInvoiceService::class)->extract($abs);
if (! ($result['ok'] ?? false)) {
Notification::make()
->title('OCR eșuat')
->body($result['error'] ?? 'Eroare necunoscută.')
->danger()->send();
@unlink($abs);
return;
}
$payload = $result['data'];
// Match supplier by case-insensitive name.
$supplierId = null;
if ($payload['supplier_name']) {
$supplierId = Supplier::whereRaw('LOWER(name) = ?', [mb_strtolower($payload['supplier_name'])])
->value('id');
}
$purchase = Purchase::create([
'number' => Purchase::generateNumber(
app(\App\Tenancy\TenantManager::class)->currentId()
),
'supplier_id' => $supplierId,
'order_date' => $payload['date'] ?? today()->toDateString(),
'status' => 'draft',
'notes' => 'Importat OCR' . ($payload['supplier_name'] && ! $supplierId
? " · furnizor nemap-uit: „{$payload['supplier_name']}"
: ''),
]);
foreach ($payload['items'] as $item) {
PurchaseItem::create([
'purchase_id' => $purchase->id,
'name' => $item['name'],
'qty' => $item['qty'],
'unit' => 'buc',
'buy_price' => $item['unit_price'],
]);
}
$purchase->refresh()->recalcTotal();
@unlink($abs);
Notification::make()
->title('Factură importată')
->body(sprintf('%d linii, total %.2f. Verifică și ajustează înainte de a confirma.',
count($payload['items']), (float) $purchase->total))
->success()->send();
$this->redirect(PurchaseResource::getUrl('edit', ['record' => $purchase]));
}),
Actions\CreateAction::make(),
];
} }
} }
@@ -3,8 +3,11 @@
namespace App\Filament\Tenant\Resources\PurchaseResource\RelationManagers; namespace App\Filament\Tenant\Resources\PurchaseResource\RelationManagers;
use App\Models\Tenant\Part; use App\Models\Tenant\Part;
use App\Models\Tenant\PurchaseItem;
use App\Models\Tenant\Warehouse;
use Filament\Actions; use Filament\Actions;
use Filament\Forms; use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager; use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Utilities\Set; use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema; use Filament\Schemas\Schema;
@@ -52,16 +55,58 @@ class ItemsRelationManager extends RelationManager
->columns([ ->columns([
Tables\Columns\TextColumn::make('name')->wrap(), Tables\Columns\TextColumn::make('name')->wrap(),
Tables\Columns\TextColumn::make('article')->placeholder('—'), Tables\Columns\TextColumn::make('article')->placeholder('—'),
Tables\Columns\TextColumn::make('qty')->alignRight(), Tables\Columns\TextColumn::make('qty')->label('Comandat')->alignRight(),
Tables\Columns\TextColumn::make('qty_received')
->label('Recepționat')
->alignRight()
->color(fn ($state, $record) => $record->isFullyReceived() ? 'success' : ((float) $state > 0 ? 'warning' : 'gray'))
->formatStateUsing(fn ($state, $record) => sprintf('%.2f / %.2f', (float) $state, (float) $record->qty)),
Tables\Columns\TextColumn::make('unit')->label('UM'), Tables\Columns\TextColumn::make('unit')->label('UM'),
Tables\Columns\TextColumn::make('buy_price')->money('MDL')->alignRight(), Tables\Columns\TextColumn::make('buy_price')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(), Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(),
Tables\Columns\IconColumn::make('received')->boolean()->label('Recepț.'),
]) ])
->headerActions([ ->headerActions([
Actions\CreateAction::make(), Actions\CreateAction::make(),
]) ])
->actions([ ->actions([
Actions\Action::make('receive_item')
->label('Recepționează')
->icon('heroicon-m-arrow-down-tray')
->color('success')
->visible(fn (PurchaseItem $r) => ! $r->isFullyReceived())
->schema([
Forms\Components\Placeholder::make('outstanding')
->label('Restanță')
->content(fn (PurchaseItem $r) => sprintf('%.2f %s', $r->outstanding(), $r->unit ?? 'buc')),
Forms\Components\TextInput::make('qty')
->label('Cantitate recepționată')
->numeric()
->required()
->minValue(0.001)
->default(fn (PurchaseItem $r) => $r->outstanding()),
Forms\Components\Select::make('warehouse_id')
->label('Depozit țintă')
->options(fn () => Warehouse::where('is_active', true)->pluck('name', 'id'))
->default(fn (PurchaseItem $r) => $r->purchase?->warehouse_id
?? Warehouse::where('is_default', true)->value('id'))
->required(),
])
->action(function (PurchaseItem $r, array $data) {
$wh = $data['warehouse_id'] ? Warehouse::find($data['warehouse_id']) : null;
try {
$r->purchase->receiveItem($r, (float) $data['qty'], $wh);
Notification::make()
->title('Recepționat — batch creat')
->success()
->send();
} catch (\Throwable $e) {
Notification::make()
->title('Eroare la recepție')
->body($e->getMessage())
->danger()
->send();
}
}),
Actions\EditAction::make(), Actions\EditAction::make(),
Actions\DeleteAction::make(), Actions\DeleteAction::make(),
]); ]);
@@ -0,0 +1,117 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Auth\Permissions;
use App\Filament\Tenant\Resources\RoleResource\Pages;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Spatie\Permission\Models\Role;
class RoleResource extends Resource
{
protected static ?string $model = Role::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-shield-check';
protected static ?string $navigationLabel = 'Roluri & Drepturi';
protected static string|\UnitEnum|null $navigationGroup = 'Admin';
protected static ?string $modelLabel = 'rol';
protected static ?string $pluralModelLabel = 'roluri';
protected static ?int $navigationSort = 82;
public static function canViewAny(): bool
{
return auth()->user()?->canDo(Permissions::ADMIN_ROLES_MANAGE) ?? false;
}
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Rol')
->columns(2)
->schema([
Forms\Components\TextInput::make('name')->label('Slug')->required()->maxLength(64)
->disabled(fn ($record) => $record && in_array($record->name, array_keys(Permissions::roleMatrix()), true))
->helperText('Rolurile sistem (owner/admin/etc.) au numele blocat'),
Forms\Components\TextInput::make('guard_name')->default('web')->disabled(),
]),
Schemas\Components\Section::make('Drepturi')
->description('Bifează ce poate face acest rol. Modificările au efect imediat.')
->schema(self::permissionFields())
->columns(1),
]);
}
private static function permissionFields(): array
{
$fields = [];
$labels = Permissions::labels();
foreach (Permissions::grouped() as $module => $perms) {
$options = [];
foreach ($perms as $p) {
$options[$p] = $p;
}
$fields[] = Forms\Components\CheckboxList::make("permissions_{$module}")
->label($labels[$module] ?? ucfirst($module))
->options($options)
->columns(2)
->bulkToggleable()
->afterStateHydrated(function (Forms\Components\CheckboxList $component, $state, $record) use ($module) {
if (! $record) { $component->state([]); return; }
$names = $record->permissions->pluck('name')->all();
$module_prefix = $module . '.';
$component->state(array_values(array_filter($names, fn ($n) => str_starts_with($n, $module_prefix))));
})
->dehydrated(false);
}
return $fields;
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label('Rol')
->formatStateUsing(fn ($state) => Permissions::roleLabels()[$state] ?? $state)
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('name')
->label('Slug')
->copyable()
->color('gray'),
Tables\Columns\TextColumn::make('permissions_count')
->counts('permissions')
->label('Drepturi')
->badge(),
Tables\Columns\TextColumn::make('users_count')
->counts('users')
->label('Utilizatori')
->badge(),
])
->actions([
Actions\EditAction::make()->label('Editează drepturi'),
Actions\DeleteAction::make()
->hidden(fn ($record) => in_array($record->name, array_keys(Permissions::roleMatrix()), true)),
])
->defaultSort('name');
}
public static function getPages(): array
{
return [
'index' => Pages\ListRoles::route('/'),
'edit' => Pages\EditRole::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,37 @@
<?php
namespace App\Filament\Tenant\Resources\RoleResource\Pages;
use App\Auth\Permissions;
use App\Filament\Tenant\Resources\RoleResource;
use Filament\Resources\Pages\EditRecord;
use Spatie\Permission\PermissionRegistrar;
class EditRole extends EditRecord
{
protected static string $resource = RoleResource::class;
protected function mutateFormDataBeforeSave(array $data): array
{
// Collect all picked permissions across the module check-lists.
$picked = [];
foreach (Permissions::grouped() as $module => $_) {
$key = "permissions_{$module}";
if (isset($data[$key]) && is_array($data[$key])) {
$picked = array_merge($picked, $data[$key]);
}
unset($data[$key]);
}
$this->_pickedPermissions = $picked;
return $data;
}
/** @var array<string> */
protected array $_pickedPermissions = [];
protected function afterSave(): void
{
$this->record->syncPermissions($this->_pickedPermissions);
app(PermissionRegistrar::class)->forgetCachedPermissions();
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\RoleResource\Pages;
use App\Filament\Tenant\Resources\RoleResource;
use Filament\Resources\Pages\ListRecords;
class ListRoles extends ListRecords
{
protected static string $resource = RoleResource::class;
}
@@ -0,0 +1,88 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\ServiceTemplateResource\Pages;
use App\Filament\Tenant\Resources\ServiceTemplateResource\RelationManagers;
use App\Models\Tenant\Labor;
use App\Models\Tenant\ServiceTemplate;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class ServiceTemplateResource extends Resource
{
protected static ?string $model = ServiceTemplate::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-clipboard-document-list';
protected static ?string $navigationLabel = 'Șabloane servicii';
protected static string|\UnitEnum|null $navigationGroup = 'Service';
protected static ?string $modelLabel = 'șablon';
protected static ?string $pluralModelLabel = 'șabloane servicii';
protected static ?int $navigationSort = 33;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make()
->columns(2)
->schema([
Forms\Components\TextInput::make('name')->label('Denumire')->required()
->placeholder('ex: Revizie completă 15.000 km')->columnSpanFull(),
Forms\Components\Select::make('category')
->label('Categorie')
->options(array_combine(Labor::CATEGORIES, Labor::CATEGORIES))
->searchable(),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('category')->badge()->placeholder('—'),
Tables\Columns\TextColumn::make('items_count')->counts('items')->label('Linii')->alignRight(),
Tables\Columns\IconColumn::make('is_active')->label('Activ')->boolean(),
])
->filters([
Tables\Filters\TernaryFilter::make('is_active')->label('Active'),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Niciun șablon')
->emptyStateDescription('Grupează manopere + piese frecvente într-un șablon (ex: „Schimb ulei complet") și aplică-l pe o fișă cu un click.')
->emptyStateIcon('heroicon-o-clipboard-document-list')
->defaultSort('name');
}
public static function getRelations(): array
{
return [
RelationManagers\ItemsRelationManager::class,
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListServiceTemplates::route('/'),
'create' => Pages\CreateServiceTemplate::route('/create'),
'edit' => Pages\EditServiceTemplate::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\ServiceTemplateResource\Pages;
use App\Filament\Tenant\Resources\ServiceTemplateResource;
use Filament\Resources\Pages\CreateRecord;
class CreateServiceTemplate extends CreateRecord
{
protected static string $resource = ServiceTemplateResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\ServiceTemplateResource\Pages;
use App\Filament\Tenant\Resources\ServiceTemplateResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditServiceTemplate extends EditRecord
{
protected static string $resource = ServiceTemplateResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\ServiceTemplateResource\Pages;
use App\Filament\Tenant\Resources\ServiceTemplateResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListServiceTemplates extends ListRecords
{
protected static string $resource = ServiceTemplateResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -0,0 +1,79 @@
<?php
namespace App\Filament\Tenant\Resources\ServiceTemplateResource\RelationManagers;
use App\Models\Tenant\Labor;
use App\Models\Tenant\Part;
use App\Models\Tenant\ServiceTemplateItem;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class ItemsRelationManager extends RelationManager
{
protected static string $relationship = 'items';
protected static ?string $title = 'Conținut șablon';
public function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\Select::make('kind')
->label('Tip')
->options(ServiceTemplateItem::KINDS)
->default('labor')
->live()
->required(),
Forms\Components\Select::make('labor_id')
->label('Manoperă')
->options(fn () => Labor::where('is_active', true)->pluck('name_ro', 'id'))
->searchable()
->visible(fn (Get $get) => $get('kind') === 'labor')
->live()
->afterStateUpdated(function ($state, Set $set) {
if ($state && $l = Labor::find($state)) {
$set('name', $l->name_ro);
$set('hours', $l->hours);
}
}),
Forms\Components\Select::make('part_id')
->label('Piesă')
->options(fn () => Part::where('is_active', true)
->get()->mapWithKeys(fn ($p) => [$p->id => "{$p->name} " . ($p->article ? "[{$p->article}]" : '')])->toArray())
->searchable()
->visible(fn (Get $get) => $get('kind') === 'part')
->live()
->afterStateUpdated(function ($state, Set $set) {
if ($state && $p = Part::find($state)) $set('name', $p->name);
}),
Forms\Components\TextInput::make('name')->label('Denumire')->required()->columnSpanFull(),
Forms\Components\TextInput::make('hours')->label('Ore')->numeric()
->visible(fn (Get $get) => $get('kind') === 'labor'),
Forms\Components\TextInput::make('qty')->label('Cantitate')->numeric()->default(1)
->visible(fn (Get $get) => $get('kind') === 'part'),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
Tables\Columns\TextColumn::make('kind')
->label('Tip')
->formatStateUsing(fn ($s) => ServiceTemplateItem::KINDS[$s] ?? $s)
->badge()
->color(fn ($s) => $s === 'labor' ? 'info' : 'gray'),
Tables\Columns\TextColumn::make('name')->wrap(),
Tables\Columns\TextColumn::make('hours')->label('Ore')->placeholder('—')->alignRight(),
Tables\Columns\TextColumn::make('qty')->label('Cant.')->placeholder('—')->alignRight(),
])
->headerActions([Actions\CreateAction::make()])
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()]);
}
}
@@ -0,0 +1,124 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\ShopCustomerResource\Pages;
use App\Filament\Tenant\Resources\ShopCustomerResource\RelationManagers;
use App\Models\Tenant\ShopCustomer;
use Filament\Actions;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
class ShopCustomerResource extends Resource
{
protected static ?string $model = ShopCustomer::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-user-circle';
protected static ?string $navigationLabel = 'Clienți magazin';
protected static string|\UnitEnum|null $navigationGroup = 'Magazin';
protected static ?string $modelLabel = 'client magazin';
protected static ?string $pluralModelLabel = 'clienți magazin';
protected static ?int $navigationSort = 52;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make()->columns(2)->schema([
Forms\Components\TextInput::make('name')->label('Nume')->required()->maxLength(160),
Forms\Components\TextInput::make('phone')->label('Telefon')->required()->maxLength(40),
Forms\Components\TextInput::make('email')->label('Email')->email()->maxLength(160),
Forms\Components\Select::make('client_id')
->label('Client legat (CRM)')
->options(fn () => \App\Models\Tenant\Client::pluck('name', 'id'))
->searchable()
->helperText('Legătura cu fișa CRM (opțional). Auto-matched la înregistrare după telefon.'),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('phone')->copyable()->searchable(),
Tables\Columns\TextColumn::make('email')->placeholder('—')->copyable()->toggleable(),
Tables\Columns\TextColumn::make('client.name')->label('Client CRM')->placeholder('—')->toggleable(),
Tables\Columns\TextColumn::make('orders_count')->counts('orders')->label('Comenzi')->alignRight(),
Tables\Columns\TextColumn::make('last_login_at')->label('Ultim login')->since()->placeholder('Niciodată'),
Tables\Columns\TextColumn::make('created_at')->label('Înregistrat')->date('d.m.Y')->toggleable(),
])
->actions([
Actions\Action::make('reset_password')
->label('Trimite reset parolă')
->icon('heroicon-m-key')
->color('warning')
->visible(fn (ShopCustomer $r) => ! empty($r->email))
->requiresConfirmation()
->modalDescription('Trimite emailul standard de resetare a parolei către clientul magazinului.')
->action(function (ShopCustomer $r) {
$status = Password::broker('shop_customers')->sendResetLink(['email' => $r->email]);
Notification::make()
->title($status === Password::RESET_LINK_SENT
? 'Link de resetare trimis la ' . $r->email
: 'Eșec: ' . $status)
->{$status === Password::RESET_LINK_SENT ? 'success' : 'warning'}()
->send();
}),
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Niciun client magazin')
->emptyStateDescription('Aici apar clienții care și-au creat cont în magazinul online (/shop/register).')
->emptyStateIcon('heroicon-o-user-circle')
->defaultSort('created_at', 'desc');
}
public static function getRelations(): array
{
return [
RelationManagers\OrdersRelationManager::class,
];
}
public static function getNavigationLabel(): string
{
return __('Clienți magazin');
}
public static function getNavigationGroup(): ?string
{
return __('Magazin');
}
public static function getModelLabel(): string
{
return __('client magazin');
}
public static function getPluralModelLabel(): string
{
return __('clienți magazin');
}
public static function getPages(): array
{
return [
'index' => Pages\ListShopCustomers::route('/'),
'edit' => Pages\EditShopCustomer::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\ShopCustomerResource\Pages;
use App\Filament\Tenant\Resources\ShopCustomerResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditShopCustomer extends EditRecord
{
protected static string $resource = ShopCustomerResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\ShopCustomerResource\Pages;
use App\Filament\Tenant\Resources\ShopCustomerResource;
use Filament\Resources\Pages\ListRecords;
class ListShopCustomers extends ListRecords
{
protected static string $resource = ShopCustomerResource::class;
}
@@ -0,0 +1,38 @@
<?php
namespace App\Filament\Tenant\Resources\ShopCustomerResource\RelationManagers;
use App\Models\Tenant\OnlineOrder;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
class OrdersRelationManager extends RelationManager
{
protected static string $relationship = 'orders';
protected static ?string $title = 'Comenzi';
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('number')
->columns([
Tables\Columns\TextColumn::make('number')->label('Nr.'),
Tables\Columns\TextColumn::make('created_at')->label('Data')->dateTime('d.m.Y H:i'),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => OnlineOrder::STATUSES[$s] ?? $s)
->badge()
->colors([
'warning' => ['new'],
'info' => ['confirmed', 'packed'],
'primary' => ['shipped'],
'success' => ['delivered'],
'danger' => ['cancelled'],
]),
Tables\Columns\TextColumn::make('total')->money('MDL')->alignRight(),
])
->defaultSort('created_at', 'desc')
->emptyStateHeading('Nicio comandă încă');
}
}
@@ -0,0 +1,154 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\SubcontractJobResource\Pages;
use App\Models\Tenant\Subcontractor;
use App\Models\Tenant\SubcontractJob;
use App\Models\Tenant\WorkOrder;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class SubcontractJobResource extends Resource
{
protected static ?string $model = SubcontractJob::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-arrow-top-right-on-square';
protected static ?string $navigationLabel = 'Lucrări terți';
protected static string|\UnitEnum|null $navigationGroup = 'Subcontractare';
protected static ?string $modelLabel = 'lucrare terți';
protected static ?string $pluralModelLabel = 'lucrări terți';
protected static ?int $navigationSort = 71;
public static function getNavigationBadge(): ?string
{
$open = static::getModel()::query()->whereNotIn('status', ['done', 'returned', 'cancelled'])->count();
return $open > 0 ? (string) $open : null;
}
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Lucrare')
->columns(2)
->schema([
Forms\Components\TextInput::make('number')->label('Nr.')->disabled()->dehydrated(false)->placeholder('Generat automat'),
Forms\Components\Select::make('status')->options(SubcontractJob::STATUSES)->default('sent')->required(),
Forms\Components\Select::make('subcontractor_id')
->label('Subcontractor')
->options(fn () => Subcontractor::where('is_active', true)->pluck('name', 'id'))
->searchable(),
Forms\Components\Select::make('work_order_id')
->label('Fișă asociată')
->options(fn () => WorkOrder::whereNotIn('status', ['done', 'cancelled'])
->get()->mapWithKeys(fn ($w) => [$w->id => "#{$w->number} · " . ($w->vehicle?->plate ?? '')])->toArray())
->searchable(),
Forms\Components\Select::make('category')
->label('Categorie')
->options(array_combine(Subcontractor::SPECIALTIES, Subcontractor::SPECIALTIES))
->searchable(),
Forms\Components\Textarea::make('description')->label('Descriere')->rows(2)->columnSpanFull(),
]),
Schemas\Components\Section::make('Cost & marjă')
->columns(3)
->schema([
Forms\Components\TextInput::make('cost')->label('Cost (de la terț)')->numeric()->default(0)->required(),
Forms\Components\TextInput::make('markup_pct')->label('Markup %')->numeric()->default(0)
->helperText('> 0 calculează automat prețul client.'),
Forms\Components\TextInput::make('client_price')->label('Preț client')->numeric()->default(0)
->helperText('Setat manual dacă markup = 0.'),
Forms\Components\Toggle::make('paid_to_sub')->label('Plătit către terț'),
]),
Schemas\Components\Section::make('Termene')
->columns(3)
->schema([
Forms\Components\DatePicker::make('sent_at')->label('Trimis')->default(today()),
Forms\Components\DatePicker::make('eta')->label('ETA'),
Forms\Components\DatePicker::make('returned_at')->label('Returnat'),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('number')->label('Nr.')->searchable()->sortable(),
Tables\Columns\TextColumn::make('subcontractor.name')->label('Terț')->placeholder('—'),
Tables\Columns\TextColumn::make('category')->badge()->placeholder('—'),
Tables\Columns\TextColumn::make('workOrder.number')->label('Fișă')->placeholder('—'),
Tables\Columns\TextColumn::make('cost')->label('Cost')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('client_price')->label('Preț client')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('margin')
->label('Marjă')
->state(fn (SubcontractJob $r) => $r->margin())
->money('MDL')
->alignRight()
->color(fn ($state) => (float) $state > 0 ? 'success' : ((float) $state < 0 ? 'danger' : 'gray')),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => SubcontractJob::STATUSES[$s] ?? $s)
->badge()
->colors([
'warning' => ['sent', 'in_progress'],
'success' => ['done', 'returned'],
'danger' => ['cancelled'],
]),
Tables\Columns\IconColumn::make('paid_to_sub')->label('Plătit terț')->boolean()->toggleable(),
])
->filters([
Tables\Filters\SelectFilter::make('status')->options(SubcontractJob::STATUSES),
Tables\Filters\SelectFilter::make('subcontractor_id')
->label('Subcontractor')
->options(fn () => Subcontractor::pluck('name', 'id')),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Nicio lucrare la terți')
->emptyStateDescription('Înregistrează lucrările trimise la ateliere externe (turbo, cutii, vopsitorie). Costul terțului + markup intră automat în totalul fișei asociate.')
->emptyStateIcon('heroicon-o-arrow-top-right-on-square')
->defaultSort('created_at', 'desc');
}
public static function getNavigationLabel(): string
{
return __('Lucrări terți');
}
public static function getNavigationGroup(): ?string
{
return __('Subcontractare');
}
public static function getModelLabel(): string
{
return __('lucrare terți');
}
public static function getPluralModelLabel(): string
{
return __('lucrări terți');
}
public static function getPages(): array
{
return [
'index' => Pages\ListSubcontractJobs::route('/'),
'create' => Pages\CreateSubcontractJob::route('/create'),
'edit' => Pages\EditSubcontractJob::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\SubcontractJobResource\Pages;
use App\Filament\Tenant\Resources\SubcontractJobResource;
use Filament\Resources\Pages\CreateRecord;
class CreateSubcontractJob extends CreateRecord
{
protected static string $resource = SubcontractJobResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\SubcontractJobResource\Pages;
use App\Filament\Tenant\Resources\SubcontractJobResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditSubcontractJob extends EditRecord
{
protected static string $resource = SubcontractJobResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\SubcontractJobResource\Pages;
use App\Filament\Tenant\Resources\SubcontractJobResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListSubcontractJobs extends ListRecords
{
protected static string $resource = SubcontractJobResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -0,0 +1,105 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\SubcontractorResource\Pages;
use App\Models\Tenant\Subcontractor;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class SubcontractorResource extends Resource
{
protected static ?string $model = Subcontractor::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-user-group';
protected static ?string $navigationLabel = 'Subcontractori';
protected static string|\UnitEnum|null $navigationGroup = 'Subcontractare';
protected static ?string $modelLabel = 'subcontractor';
protected static ?string $pluralModelLabel = 'subcontractori';
protected static ?int $navigationSort = 70;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make()->columns(2)->schema([
Forms\Components\TextInput::make('name')->label('Nume')->required()->maxLength(160),
Forms\Components\Select::make('specialty')
->label('Specialitate')
->options(array_combine(Subcontractor::SPECIALTIES, Subcontractor::SPECIALTIES))
->searchable(),
Forms\Components\TextInput::make('phone')->label('Telefon')->tel()->maxLength(40),
Forms\Components\TextInput::make('email')->email()->maxLength(120),
Forms\Components\Select::make('rating')
->label('Rating')
->options([1 => '★', 2 => '★★', 3 => '★★★', 4 => '★★★★', 5 => '★★★★★'])
->default(3),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
Forms\Components\Textarea::make('notes')->label('Observații')->columnSpanFull()->rows(2),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('specialty')->badge()->placeholder('—'),
Tables\Columns\TextColumn::make('phone')->copyable()->placeholder('—'),
Tables\Columns\TextColumn::make('rating')->formatStateUsing(fn ($s) => str_repeat('★', (int) $s)),
Tables\Columns\TextColumn::make('jobs_count')->counts('jobs')->label('Lucrări')->alignRight(),
Tables\Columns\IconColumn::make('is_active')->boolean(),
])
->filters([
Tables\Filters\TernaryFilter::make('is_active')->label('Activi'),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Niciun subcontractor')
->emptyStateDescription('Adaugă atelierele terțe la care trimiți lucrări (turbo, cutii, vopsitorie, PDR) și urmărește costul + marja.')
->emptyStateIcon('heroicon-o-user-group')
->defaultSort('name');
}
public static function getNavigationLabel(): string
{
return __('Subcontractori');
}
public static function getNavigationGroup(): ?string
{
return __('Subcontractare');
}
public static function getModelLabel(): string
{
return __('subcontractor');
}
public static function getPluralModelLabel(): string
{
return __('subcontractori');
}
public static function getPages(): array
{
return [
'index' => Pages\ListSubcontractors::route('/'),
'create' => Pages\CreateSubcontractor::route('/create'),
'edit' => Pages\EditSubcontractor::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\SubcontractorResource\Pages;
use App\Filament\Tenant\Resources\SubcontractorResource;
use Filament\Resources\Pages\CreateRecord;
class CreateSubcontractor extends CreateRecord
{
protected static string $resource = SubcontractorResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\SubcontractorResource\Pages;
use App\Filament\Tenant\Resources\SubcontractorResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditSubcontractor extends EditRecord
{
protected static string $resource = SubcontractorResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\SubcontractorResource\Pages;
use App\Filament\Tenant\Resources\SubcontractorResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListSubcontractors extends ListRecords
{
protected static string $resource = SubcontractorResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -68,15 +68,56 @@ class SupplierResource extends Resource
Tables\Columns\TextColumn::make('rating') Tables\Columns\TextColumn::make('rating')
->label('Rating') ->label('Rating')
->formatStateUsing(fn ($s) => str_repeat('★', (int) $s)), ->formatStateUsing(fn ($s) => str_repeat('★', (int) $s)),
Tables\Columns\TextColumn::make('delivery_days')->label('Livrare (zile)')->alignRight(), Tables\Columns\TextColumn::make('on_time_pct')
->label('La timp 90d')
->state(fn (Supplier $r) => app(\App\Services\Warehouse\SupplierAnalytics::class)->onTimeRate($r))
->formatStateUsing(fn ($s) => $s === null ? '—' : "{$s}%")
->color(fn ($s) => $s === null ? 'gray' : ($s >= 90 ? 'success' : ($s >= 70 ? 'warning' : 'danger')))
->alignRight()
->toggleable(),
Tables\Columns\TextColumn::make('avg_delivery_days')
->label('Avg zile')
->state(fn (Supplier $r) => app(\App\Services\Warehouse\SupplierAnalytics::class)->avgDeliveryDays($r))
->formatStateUsing(fn ($s) => $s === null ? '—' : (string) $s)
->alignRight()
->toggleable(),
Tables\Columns\TextColumn::make('spend_90d')
->label('Cheltuit 90d')
->state(fn (Supplier $r) => app(\App\Services\Warehouse\SupplierAnalytics::class)->spend($r))
->money('MDL')
->alignRight()
->toggleable(),
Tables\Columns\TextColumn::make('delivery_days')->label('Livrare (zile)')->alignRight()->toggleable(),
Tables\Columns\TextColumn::make('discount_pct')->label('Discount') Tables\Columns\TextColumn::make('discount_pct')->label('Discount')
->formatStateUsing(fn ($s) => $s . '%')->alignRight(), ->formatStateUsing(fn ($s) => $s . '%')->alignRight()->toggleable(),
Tables\Columns\IconColumn::make('is_active')->boolean(), Tables\Columns\IconColumn::make('is_active')->boolean(),
]) ])
->filters([ ->filters([
Tables\Filters\TernaryFilter::make('is_active')->label('Activi'), Tables\Filters\TernaryFilter::make('is_active')->label('Activi'),
]) ])
->actions([ ->actions([
Actions\Action::make('rate')
->label('Rerating')
->icon('heroicon-m-arrow-path')
->color('gray')
->action(function (Supplier $r) {
$score = app(\App\Services\Warehouse\SupplierAnalytics::class)
->computedRating($r);
if ($score === null) {
\Filament\Notifications\Notification::make()
->title('Date insuficiente')
->body('Necesită cel puțin 2 recepții complete cu data așteptată setată.')
->warning()
->send();
return;
}
$r->rating = $score;
$r->saveQuietly();
\Filament\Notifications\Notification::make()
->title("Rating actualizat → {$score}")
->success()
->send();
}),
Actions\EditAction::make(), Actions\EditAction::make(),
Actions\DeleteAction::make(), Actions\DeleteAction::make(),
]) ])
@@ -0,0 +1,217 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\TireSetResource\Pages;
use App\Filament\Tenant\Resources\TireSetResource\RelationManagers;
use App\Models\Tenant\Client;
use App\Models\Tenant\TireSet;
use App\Models\Tenant\Vehicle;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class TireSetResource extends Resource
{
protected static ?string $model = TireSet::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-lifebuoy';
protected static ?string $navigationLabel = 'Seturi anvelope';
protected static string|\UnitEnum|null $navigationGroup = 'Anvelope';
protected static ?string $modelLabel = 'set anvelope';
protected static ?string $pluralModelLabel = 'seturi anvelope';
protected static ?int $navigationSort = 60;
public static function getNavigationBadge(): ?string
{
$stored = \App\Models\Tenant\TireStorage::where('status', 'stored')->count();
return $stored > 0 ? (string) $stored : null;
}
public static function getNavigationBadgeColor(): ?string
{
return 'info';
}
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Proprietar')
->columns(2)
->schema([
Forms\Components\Select::make('client_id')
->label('Client')
->options(fn () => Client::pluck('name', 'id'))
->searchable()
->live()
->required(),
Forms\Components\Select::make('vehicle_id')
->label('Auto')
->options(fn (Get $get) => $get('client_id')
? Vehicle::where('client_id', $get('client_id'))->get()
->mapWithKeys(fn ($v) => [$v->id => "{$v->make} {$v->model} {$v->plate}"])->toArray()
: [])
->searchable(),
Forms\Components\TextInput::make('label')->label('Etichetă')->placeholder('ex: Iarnă Michelin'),
Forms\Components\Select::make('season')->label('Sezon')->options(TireSet::SEASONS)->default('winter')->required(),
]),
Schemas\Components\Section::make('Specificații')
->columns(3)
->schema([
Forms\Components\TextInput::make('width')->label('Lățime')->numeric()->placeholder('205'),
Forms\Components\TextInput::make('profile')->label('Profil')->numeric()->placeholder('55'),
Forms\Components\TextInput::make('diameter')->label('Diametru R')->numeric()->placeholder('16'),
Forms\Components\TextInput::make('brand')->maxLength(64),
Forms\Components\TextInput::make('model')->maxLength(64),
Forms\Components\TextInput::make('dot_year')->label('DOT')->maxLength(8)->placeholder('3621'),
Forms\Components\Toggle::make('has_rims')->label('Cu jante'),
Forms\Components\Select::make('rim_type')->label('Tip jante')->options(['steel' => 'Tablă', 'alloy' => 'Aliaj']),
Forms\Components\Select::make('condition')->label('Stare')->options(TireSet::CONDITIONS),
]),
Schemas\Components\Section::make('Uzură (mm) per poziție')
->columns(4)
->schema([
Forms\Components\TextInput::make('tread.fl')->label('Față-Stânga')->numeric(),
Forms\Components\TextInput::make('tread.fr')->label('Față-Dreapta')->numeric(),
Forms\Components\TextInput::make('tread.rl')->label('Spate-Stânga')->numeric(),
Forms\Components\TextInput::make('tread.rr')->label('Spate-Dreapta')->numeric(),
]),
Schemas\Components\Section::make('TPMS & foto')
->columns(2)
->schema([
Forms\Components\Toggle::make('tpms')->label('Senzori TPMS'),
Forms\Components\TextInput::make('notes')->label('Observații'),
\Filament\Forms\Components\SpatieMediaLibraryFileUpload::make('photos')
->label('Fotografii')
->collection('photos')
->multiple()
->image()
->maxFiles(8)
->columnSpanFull(),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('client.name')->label('Client')->searchable()->sortable(),
Tables\Columns\TextColumn::make('label')->label('Etichetă')->placeholder('—'),
Tables\Columns\TextColumn::make('size')
->label('Dimensiune')
->state(fn (TireSet $r) => $r->sizeLabel()),
Tables\Columns\TextColumn::make('season')
->label('Sezon')
->formatStateUsing(fn ($s) => TireSet::SEASONS[$s] ?? $s)
->badge()
->colors(['warning' => ['summer'], 'info' => ['winter'], 'gray' => ['allseason']]),
Tables\Columns\TextColumn::make('tread_min')->label('Uzură min')
->formatStateUsing(fn ($s) => $s ? $s . ' mm' : '—')
->color(fn ($s) => $s !== null && (float) $s < 3 ? 'danger' : null)
->alignRight(),
Tables\Columns\IconColumn::make('tpms')->label('TPMS')->boolean()->toggleable(),
Tables\Columns\TextColumn::make('storage_status')
->label('Depozit')
->state(fn (TireSet $r) => $r->isStored() ? ($r->currentStorage()?->location ?? 'da') : '—')
->badge()
->color(fn ($state) => $state === '—' ? 'gray' : 'success'),
])
->filters([
Tables\Filters\SelectFilter::make('season')->options(TireSet::SEASONS),
Tables\Filters\Filter::make('stored')
->label('În depozit')
->query(fn ($q) => $q->whereHas('storage', fn ($s) => $s->where('status', 'stored'))),
])
->actions([
Actions\Action::make('check_in')
->label('Check-in depozit')
->icon('heroicon-m-arrow-down-on-square')
->color('success')
->visible(fn (TireSet $r) => ! $r->isStored())
->schema([
Forms\Components\TextInput::make('location')->label('Locație (raft)')->required()->placeholder('A1-03'),
Forms\Components\TextInput::make('season_label')->label('Perioadă')->placeholder('Iarnă 2025-2026'),
Forms\Components\TextInput::make('fee')->label('Taxă depozitare')->numeric()->default(0),
])
->action(function (TireSet $r, array $data) {
\App\Models\Tenant\TireStorage::create([
'tire_set_id' => $r->id,
'location' => $data['location'],
'season_label' => $data['season_label'] ?? null,
'fee' => (float) ($data['fee'] ?? 0),
'status' => 'stored',
'checked_in_at' => now(),
]);
\Filament\Notifications\Notification::make()->title('Set primit în depozit')->success()->send();
}),
Actions\Action::make('check_out')
->label('Eliberează')
->icon('heroicon-m-arrow-up-on-square')
->color('warning')
->visible(fn (TireSet $r) => $r->isStored())
->requiresConfirmation()
->modalDescription('Marchează setul ca ridicat de client.')
->action(function (TireSet $r) {
$storage = $r->currentStorage();
if ($storage) {
$storage->update(['status' => 'retrieved', 'checked_out_at' => now()]);
}
\Filament\Notifications\Notification::make()->title('Set eliberat din depozit')->success()->send();
}),
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Niciun set de anvelope')
->emptyStateDescription('Înregistrează seturile de anvelope ale clienților și gestionează depozitarea sezonieră (tire hotel). Urmărește uzura, TPMS și locația în depozit.')
->emptyStateIcon('heroicon-o-lifebuoy')
->defaultSort('created_at', 'desc');
}
public static function getRelations(): array
{
return [
RelationManagers\StorageRelationManager::class,
];
}
public static function getNavigationLabel(): string
{
return __('Seturi anvelope');
}
public static function getNavigationGroup(): ?string
{
return __('Anvelope');
}
public static function getModelLabel(): string
{
return __('set anvelope');
}
public static function getPluralModelLabel(): string
{
return __('seturi anvelope');
}
public static function getPages(): array
{
return [
'index' => Pages\ListTireSets::route('/'),
'create' => Pages\CreateTireSet::route('/create'),
'edit' => Pages\EditTireSet::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\TireSetResource\Pages;
use App\Filament\Tenant\Resources\TireSetResource;
use Filament\Resources\Pages\CreateRecord;
class CreateTireSet extends CreateRecord
{
protected static string $resource = TireSetResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\TireSetResource\Pages;
use App\Filament\Tenant\Resources\TireSetResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditTireSet extends EditRecord
{
protected static string $resource = TireSetResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\TireSetResource\Pages;
use App\Filament\Tenant\Resources\TireSetResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListTireSets extends ListRecords
{
protected static string $resource = TireSetResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -0,0 +1,35 @@
<?php
namespace App\Filament\Tenant\Resources\TireSetResource\RelationManagers;
use App\Models\Tenant\TireStorage;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
class StorageRelationManager extends RelationManager
{
protected static string $relationship = 'storage';
protected static ?string $title = 'Istoric depozitare';
public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('season_label')->label('Perioadă')->placeholder('—'),
Tables\Columns\TextColumn::make('location')->label('Locație')->placeholder('—'),
Tables\Columns\TextColumn::make('checked_in_at')->label('Primit')->dateTime('d.m.Y'),
Tables\Columns\TextColumn::make('checked_out_at')->label('Ridicat')->dateTime('d.m.Y')->placeholder('—'),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => TireStorage::STATUSES[$s] ?? $s)
->badge()
->colors(['success' => ['stored'], 'gray' => ['retrieved']]),
Tables\Columns\TextColumn::make('fee')->money('MDL')->alignRight(),
Tables\Columns\IconColumn::make('paid')->label('Plătit')->boolean(),
])
->defaultSort('checked_in_at', 'desc')
->emptyStateHeading('Niciun istoric')
->emptyStateDescription('Folosește „Check-in depozit" pe set pentru a înregistra prima depozitare.');
}
}
+88 -13
View File
@@ -3,6 +3,7 @@
namespace App\Filament\Tenant\Resources; namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\UserResource\Pages; use App\Filament\Tenant\Resources\UserResource\Pages;
use App\Filament\Tenant\Resources\UserResource\RelationManagers;
use App\Models\Tenant\User; use App\Models\Tenant\User;
use Filament\Forms; use Filament\Forms;
use Filament\Resources\Resource; use Filament\Resources\Resource;
@@ -31,8 +32,17 @@ class UserResource extends Resource
public static function canViewAny(): bool public static function canViewAny(): bool
{ {
$u = auth()->user(); return auth()->user()?->canDo(\App\Auth\Permissions::ADMIN_USERS_VIEW) ?? false;
return $u && $u->role === 'admin'; }
public static function canCreate(): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::ADMIN_USERS_MANAGE) ?? false;
}
public static function canDelete($record): bool
{
return auth()->user()?->canDo(\App\Auth\Permissions::ADMIN_USERS_MANAGE) ?? false;
} }
public static function form(Schema $schema): Schema public static function form(Schema $schema): Schema
@@ -53,17 +63,10 @@ class UserResource extends Resource
->schema([ ->schema([
Forms\Components\Select::make('role') Forms\Components\Select::make('role')
->label('Rol primar') ->label('Rol primar')
->options([ ->options(\App\Auth\Permissions::roleLabels())
'admin' => 'Administrator',
'manager' => 'Manager',
'receptionist' => 'Recepție',
'mechanic' => 'Mecanic',
'parts_manager' => 'Magazioner piese',
'accountant' => 'Contabil',
'marketer' => 'Marketing',
])
->required() ->required()
->default('mechanic'), ->default('mechanic')
->helperText('Rolul principal — sincronizat automat cu drepturile RBAC.'),
Forms\Components\Select::make('status') Forms\Components\Select::make('status')
->options(['active' => 'Activ', 'inactive' => 'Inactiv', 'blocked' => 'Blocat']) ->options(['active' => 'Activ', 'inactive' => 'Inactiv', 'blocked' => 'Blocat'])
->default('active') ->default('active')
@@ -76,6 +79,26 @@ class UserResource extends Resource
->dehydrateStateUsing(fn ($state) => Hash::make($state)) ->dehydrateStateUsing(fn ($state) => Hash::make($state))
->minLength(6) ->minLength(6)
->helperText('La editare lasă gol pentru a păstra parola actuală.'), ->helperText('La editare lasă gol pentru a păstra parola actuală.'),
Forms\Components\Select::make('roles_picked')
->label('Roluri suplimentare')
->multiple()
->options(\App\Auth\Permissions::roleLabels())
->afterStateHydrated(function ($component, $record) {
if ($record) $component->state($record->roles->pluck('name')->all());
})
->dehydrated(false)
->columnSpanFull()
->helperText('Roluri suplimentare peste rolul primar — drepturile se cumulează.'),
]),
Schemas\Components\Section::make('Securitate')
->columns(2)
->schema([
Forms\Components\Placeholder::make('mfa_status')
->label('Autentificare 2FA')
->content(fn ($record) => $record && $record->hasTwoFactorEnabled() ? '✓ Activat (TOTP)' : '✗ Dezactivat'),
Forms\Components\Placeholder::make('last_login')
->label('Ultima autentificare')
->content(fn ($record) => $record?->last_login_at?->diffForHumans() ?? '—'),
]), ]),
]); ]);
} }
@@ -87,7 +110,29 @@ class UserResource extends Resource
Tables\Columns\TextColumn::make('name')->searchable()->sortable(), Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('email')->searchable()->copyable(), Tables\Columns\TextColumn::make('email')->searchable()->copyable(),
Tables\Columns\TextColumn::make('phone')->placeholder('—'), Tables\Columns\TextColumn::make('phone')->placeholder('—'),
Tables\Columns\TextColumn::make('role')->badge(), Tables\Columns\TextColumn::make('role')
->formatStateUsing(fn ($state) => \App\Auth\Permissions::roleLabels()[$state] ?? $state)
->badge(),
Tables\Columns\IconColumn::make('app_authentication_secret')
->label('2FA')
->boolean()
->getStateUsing(fn ($record) => $record->hasTwoFactorEnabled())
->trueIcon('heroicon-o-shield-check')
->trueColor('success')
->falseIcon('heroicon-o-shield-exclamation')
->falseColor('warning'),
Tables\Columns\TextColumn::make('active_sessions')
->label('Sesiuni')
->getStateUsing(fn ($record) => \Illuminate\Support\Facades\DB::table('sessions')->where('user_id', $record->id)->count())
->badge()
->color(fn ($state) => $state > 0 ? 'success' : 'gray')
->toggleable(),
Tables\Columns\TextColumn::make('permission_overrides_count')
->counts('permissionOverrides')
->label('Excepții')
->badge()
->color('warning')
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('status') Tables\Columns\TextColumn::make('status')
->badge() ->badge()
->colors([ ->colors([
@@ -109,11 +154,41 @@ class UserResource extends Resource
]) ])
->actions([ ->actions([
Actions\EditAction::make(), Actions\EditAction::make(),
Actions\Action::make('force_logout')
->label('Force logout')
->icon('heroicon-o-arrow-right-on-rectangle')
->color('warning')
->visible(fn ($record) => \Illuminate\Support\Facades\DB::table('sessions')->where('user_id', $record->id)->exists())
->requiresConfirmation()
->modalDescription('Va deconecta utilizatorul pe toate device-urile.')
->action(function ($record) {
$n = \Illuminate\Support\Facades\DB::table('sessions')->where('user_id', $record->id)->delete();
\Filament\Notifications\Notification::make()->title("$n sesiuni revoke-uite")->success()->send();
}),
Actions\Action::make('reset_2fa')
->label('Resetează 2FA')
->icon('heroicon-o-shield-exclamation')
->color('warning')
->visible(fn ($record) => $record && $record->hasTwoFactorEnabled())
->requiresConfirmation()
->modalDescription('Dezactivează 2FA pentru acest utilizator. Va trebui să re-configureze TOTP la următoarea autentificare.')
->action(function ($record) {
$record->saveAppAuthenticationSecret(null);
$record->saveAppAuthenticationRecoveryCodes(null);
\Filament\Notifications\Notification::make()->title('2FA resetat')->success()->send();
}),
Actions\DeleteAction::make(), Actions\DeleteAction::make(),
]) ])
->defaultSort('created_at', 'desc'); ->defaultSort('created_at', 'desc');
} }
public static function getRelations(): array
{
return [
RelationManagers\PermissionOverridesRelationManager::class,
];
}
public static function getPages(): array public static function getPages(): array
{ {
return [ return [
@@ -8,4 +8,23 @@ use Filament\Resources\Pages\CreateRecord;
class CreateUser extends CreateRecord class CreateUser extends CreateRecord
{ {
protected static string $resource = UserResource::class; protected static string $resource = UserResource::class;
/** @var array<string> */
protected array $_rolesPicked = [];
protected function mutateFormDataBeforeCreate(array $data): array
{
$this->_rolesPicked = $data['roles_picked'] ?? [];
unset($data['roles_picked']);
return $data;
}
protected function afterCreate(): void
{
$picked = $this->_rolesPicked;
if (! in_array($this->record->role, $picked, true)) {
$picked[] = $this->record->role;
}
$this->record->syncRoles(array_unique($picked));
}
} }
@@ -14,4 +14,24 @@ class EditUser extends EditRecord
{ {
return [Actions\DeleteAction::make()]; return [Actions\DeleteAction::make()];
} }
/** @var array<string> */
protected array $_rolesPicked = [];
protected function mutateFormDataBeforeSave(array $data): array
{
$this->_rolesPicked = $data['roles_picked'] ?? [];
unset($data['roles_picked']);
return $data;
}
protected function afterSave(): void
{
// Always include the primary role
$picked = $this->_rolesPicked;
if (! in_array($this->record->role, $picked, true)) {
$picked[] = $this->record->role;
}
$this->record->syncRoles(array_unique($picked));
}
} }
@@ -0,0 +1,90 @@
<?php
namespace App\Filament\Tenant\Resources\UserResource\RelationManagers;
use App\Auth\Permissions;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
use Spatie\Permission\Models\Permission;
class PermissionOverridesRelationManager extends RelationManager
{
protected static string $relationship = 'permissionOverrides';
protected static ?string $title = 'Excepții drepturi';
protected static string|\BackedEnum|null $icon = 'heroicon-o-shield-exclamation';
public function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make('Excepție')
->columns(2)
->schema([
Forms\Components\Select::make('permission_id')
->label('Drept')
->required()
->searchable()
->options(fn () => Permission::orderBy('name')->pluck('name', 'id'))
->columnSpanFull(),
Forms\Components\Select::make('mode')
->required()
->options(['grant' => 'GRANT — adaugă dreptul', 'deny' => 'DENY — interzice dreptul'])
->default('grant'),
Forms\Components\DatePicker::make('expires_at')
->label('Expiră la (opțional)')
->minDate(now()),
Forms\Components\Textarea::make('reason')
->label('Motiv')
->columnSpanFull()
->placeholder('Ex: lockdown temporar; acces pentru audit; etc.')
->rows(2),
]),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('mode')
->columns([
Tables\Columns\TextColumn::make('permission.name')
->label('Drept')
->searchable()
->sortable(),
Tables\Columns\TextColumn::make('mode')
->badge()
->colors(['success' => 'grant', 'danger' => 'deny'])
->formatStateUsing(fn ($state) => strtoupper($state)),
Tables\Columns\TextColumn::make('reason')
->limit(40)
->placeholder('—'),
Tables\Columns\TextColumn::make('expires_at')
->label('Expiră')
->date()
->placeholder('niciodată')
->color(fn ($record) => $record?->isExpired() ? 'danger' : 'gray'),
Tables\Columns\TextColumn::make('grantedBy.name')
->label('Acordat de')
->placeholder('—')
->toggleable(),
])
->headerActions([
Actions\CreateAction::make()
->mutateDataUsing(fn (array $data) => array_merge($data, [
'granted_at' => now(),
'granted_by_id' => auth()->id(),
])),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->defaultSort('granted_at', 'desc');
}
}
@@ -72,6 +72,10 @@ class VehicleResource extends Resource
'Benzină' => 'Benzină', 'Diesel' => 'Diesel', 'Hybrid' => 'Hybrid', 'Benzină' => 'Benzină', 'Diesel' => 'Diesel', 'Hybrid' => 'Hybrid',
'EV' => 'Electric', 'GPL' => 'GPL', 'GNC' => 'GNC', 'EV' => 'Electric', 'GPL' => 'GPL', 'GNC' => 'GNC',
]), ]),
Forms\Components\Select::make('vehicle_class')
->label('Clasă (pentru pricing)')
->options(\App\Models\Tenant\PricingCoefficient::VEHICLE_CLASSES)
->helperText('Folosită de coeficienții de preț. Hibrid/EV se deduc și din combustibil.'),
Forms\Components\TextInput::make('mileage')->label('Kilometraj')->numeric()->default(0), Forms\Components\TextInput::make('mileage')->label('Kilometraj')->numeric()->default(0),
Forms\Components\TextInput::make('color')->maxLength(40), Forms\Components\TextInput::make('color')->maxLength(40),
]), ]),
@@ -93,6 +97,31 @@ class VehicleResource extends Resource
Tables\Columns\TextColumn::make('created_at')->date()->sortable(), Tables\Columns\TextColumn::make('created_at')->date()->sortable(),
]) ])
->actions([ ->actions([
Actions\Action::make('decode_vin')
->label('Decode VIN')
->icon('heroicon-m-cpu-chip')
->color('gray')
->visible(fn (\App\Models\Tenant\Vehicle $r) => ! empty($r->vin) && strlen($r->vin) === 17)
->modalHeading(fn (\App\Models\Tenant\Vehicle $r) => 'Decode VIN: ' . $r->vin)
->modalSubmitAction(false)
->modalCancelActionLabel('Închide')
->modalContent(function (\App\Models\Tenant\Vehicle $r) {
$info = app(\App\Services\Ai\VinDecoder::class)->decode($r->vin);
return view('filament.tenant.vin-decode', ['info' => $info, 'vehicle' => $r]);
}),
Actions\Action::make('ai_recommend')
->label('AI: recomandări')
->icon('heroicon-m-sparkles')
->color('primary')
->visible(fn (\App\Models\Tenant\Vehicle $r) => ! empty($r->vin))
->modalHeading('Recomandări AI')
->modalSubmitAction(false)
->modalCancelActionLabel('Închide')
->modalContent(function (\App\Models\Tenant\Vehicle $r) {
[$reply, $meta] = app(\App\Services\Ai\AiAssistantService::class)
->vinRecommendations($r->vin, (int) $r->mileage);
return view('filament.tenant.ai-reply', ['reply' => $reply, 'meta' => $meta]);
}),
Actions\EditAction::make(), Actions\EditAction::make(),
Actions\DeleteAction::make(), Actions\DeleteAction::make(),
]) ])
@@ -0,0 +1,76 @@
<?php
namespace App\Filament\Tenant\Resources;
use App\Filament\Tenant\Resources\WarehouseResource\Pages;
use App\Models\Tenant\Warehouse;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class WarehouseResource extends Resource
{
protected static ?string $model = Warehouse::class;
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-building-storefront';
protected static ?string $navigationLabel = 'Depozite';
protected static string|\UnitEnum|null $navigationGroup = 'Depozit';
protected static ?string $modelLabel = 'depozit';
protected static ?string $pluralModelLabel = 'depozite';
protected static ?int $navigationSort = 38;
public static function form(Schema $schema): Schema
{
return $schema->components([
Schemas\Components\Section::make()->columns(2)->schema([
Forms\Components\TextInput::make('code')->label('Cod')->required()->maxLength(32),
Forms\Components\TextInput::make('name')->label('Denumire')->required()->maxLength(120),
Forms\Components\TextInput::make('address')->label('Adresă')->columnSpanFull()->maxLength(200),
Forms\Components\Toggle::make('is_default')->label('Depozit implicit'),
Forms\Components\Toggle::make('is_active')->label('Activ')->default(true),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('code')->searchable()->sortable(),
Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
Tables\Columns\TextColumn::make('address')->placeholder('—')->toggleable(),
Tables\Columns\IconColumn::make('is_default')->label('Implicit')->boolean(),
Tables\Columns\IconColumn::make('is_active')->label('Activ')->boolean(),
Tables\Columns\TextColumn::make('batches_count')
->counts('batches')
->label('Loturi')
->alignRight(),
])
->actions([
Actions\EditAction::make(),
Actions\DeleteAction::make(),
])
->emptyStateHeading('Niciun depozit')
->emptyStateDescription('Un depozit implicit a fost creat la migrare. Adaugă altele dacă ai locații fizice separate (sucursală, hală, mobil).')
->emptyStateIcon('heroicon-o-building-storefront')
->defaultSort('code');
}
public static function getPages(): array
{
return [
'index' => Pages\ListWarehouses::route('/'),
'create' => Pages\CreateWarehouse::route('/create'),
'edit' => Pages\EditWarehouse::route('/{record}/edit'),
];
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Tenant\Resources\WarehouseResource\Pages;
use App\Filament\Tenant\Resources\WarehouseResource;
use Filament\Resources\Pages\CreateRecord;
class CreateWarehouse extends CreateRecord
{
protected static string $resource = WarehouseResource::class;
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\WarehouseResource\Pages;
use App\Filament\Tenant\Resources\WarehouseResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditWarehouse extends EditRecord
{
protected static string $resource = WarehouseResource::class;
protected function getHeaderActions(): array
{
return [Actions\DeleteAction::make()];
}
}
@@ -0,0 +1,17 @@
<?php
namespace App\Filament\Tenant\Resources\WarehouseResource\Pages;
use App\Filament\Tenant\Resources\WarehouseResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListWarehouses extends ListRecords
{
protected static string $resource = WarehouseResource::class;
protected function getHeaderActions(): array
{
return [Actions\CreateAction::make()];
}
}
@@ -72,6 +72,11 @@ class WorkOrderResource extends Resource
->options(WorkOrder::STATUSES) ->options(WorkOrder::STATUSES)
->default('new') ->default('new')
->required(), ->required(),
Forms\Components\Select::make('urgency')
->label('Urgență')
->options(\App\Models\Tenant\PricingCoefficient::URGENCY)
->default('normal')
->required(),
Forms\Components\Select::make('client_id') Forms\Components\Select::make('client_id')
->label('Client') ->label('Client')
->options(fn () => Client::pluck('name', 'id')) ->options(fn () => Client::pluck('name', 'id'))
@@ -101,6 +106,35 @@ class WorkOrderResource extends Resource
Forms\Components\Textarea::make('diagnosis')->label('Diagnostic')->rows(3)->columnSpanFull(), Forms\Components\Textarea::make('diagnosis')->label('Diagnostic')->rows(3)->columnSpanFull(),
Forms\Components\Textarea::make('recommendations')->label('Recomandări')->rows(2)->columnSpanFull(), Forms\Components\Textarea::make('recommendations')->label('Recomandări')->rows(2)->columnSpanFull(),
]), ]),
Schemas\Components\Section::make('Foto')
->collapsible()
->schema([
\Filament\Forms\Components\SpatieMediaLibraryFileUpload::make('photos')
->label('Fotografii')
->collection('photos')
->multiple()
->reorderable()
->image()
->imageEditor()
->maxFiles(20)
->columnSpanFull(),
]),
Schemas\Components\Section::make('Tracking & ETA')
->columns(3)
->collapsible()
->schema([
Forms\Components\DateTimePicker::make('eta_at')
->label('Gata estimat (ETA)')
->seconds(false),
Forms\Components\TextInput::make('tracking_token')
->label('Token public')
->disabled()
->dehydrated(false)
->columnSpan(2)
->helperText(fn (?WorkOrder $record) => $record?->tracking_token
? 'Link client: ' . $record->trackingUrl()
: 'Se generează la salvare'),
]),
Schemas\Components\Section::make('Plată & total') Schemas\Components\Section::make('Plată & total')
->columns(3) ->columns(3)
->schema([ ->schema([
@@ -180,6 +214,7 @@ class WorkOrderResource extends Resource
return [ return [
RelationManagers\WorksRelationManager::class, RelationManagers\WorksRelationManager::class,
RelationManagers\PartsRelationManager::class, RelationManagers\PartsRelationManager::class,
RelationManagers\SubcontractJobsRelationManager::class,
RelationManagers\PaymentsRelationManager::class, RelationManagers\PaymentsRelationManager::class,
]; ];
} }
@@ -15,6 +15,49 @@ class EditWorkOrder extends EditRecord
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
return [ return [
Actions\Action::make('apply_template')
->label('Aplică șablon')
->icon('heroicon-m-clipboard-document-list')
->color('gray')
->schema([
\Filament\Forms\Components\Select::make('template_id')
->label('Șablon serviciu')
->options(fn () => \App\Models\Tenant\ServiceTemplate::where('is_active', true)->pluck('name', 'id'))
->searchable()
->required(),
])
->action(function (array $data) {
$template = \App\Models\Tenant\ServiceTemplate::with('items')->find($data['template_id']);
if (! $template) return;
$r = app(\App\Services\ServiceComposer::class)->applyTemplate($this->record, $template);
$this->fillForm();
\Filament\Notifications\Notification::make()
->title("Șablon aplicat: {$r['labor']} manopere, {$r['parts']} piese")
->success()->send();
}),
Actions\Action::make('ai_diagnose')
->label('AI: sugerează diagnostic')
->icon('heroicon-m-sparkles')
->color('primary')
->visible(fn () => ! empty($this->record->complaint))
->modalHeading('Diagnostic AI bazat pe plângerea clientului')
->modalSubmitAction(false)
->modalCancelActionLabel('Închide')
->modalContent(function () {
[$reply, $meta] = app(\App\Services\Ai\AiAssistantService::class)
->suggestDiagnosis($this->record);
return view('filament.tenant.ai-reply', ['reply' => $reply, 'meta' => $meta]);
}),
Actions\Action::make('tracking')
->label('Link client (QR)')
->icon('heroicon-m-qr-code')
->color('primary')
->modalHeading(fn () => 'Tracking client — WO #' . $this->record->number)
->modalSubmitAction(false)
->modalCancelActionLabel('Închide')
->modalContent(fn () => view('filament.tenant.tracking-qr', [
'wo' => $this->record,
])),
Actions\Action::make('pdf') Actions\Action::make('pdf')
->label('Descarcă PDF') ->label('Descarcă PDF')
->icon('heroicon-m-document-arrow-down') ->icon('heroicon-m-document-arrow-down')
@@ -3,9 +3,12 @@
namespace App\Filament\Tenant\Resources\WorkOrderResource\RelationManagers; namespace App\Filament\Tenant\Resources\WorkOrderResource\RelationManagers;
use App\Models\Tenant\Part; use App\Models\Tenant\Part;
use App\Models\Tenant\PartReservation;
use App\Models\Tenant\WorkOrderPart; use App\Models\Tenant\WorkOrderPart;
use App\Services\Warehouse\WarehouseService;
use Filament\Actions; use Filament\Actions;
use Filament\Forms; use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager; use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Utilities\Set; use Filament\Schemas\Components\Utilities\Set;
use Filament\Schemas\Schema; use Filament\Schemas\Schema;
@@ -82,6 +85,74 @@ class PartsRelationManager extends RelationManager
Actions\CreateAction::make(), Actions\CreateAction::make(),
]) ])
->actions([ ->actions([
Actions\Action::make('smart_price')
->label('Preț inteligent')
->icon('heroicon-m-sparkles')
->color('primary')
->visible(fn (WorkOrderPart $r) => (bool) $r->part_id)
->modalHeading('Preț contextual')
->modalSubmitActionLabel('Aplică prețul')
->modalContent(function (WorkOrderPart $r) {
$wo = $r->workOrder;
$part = $r->part;
$quote = app(\App\Services\Pricing\PricingEngine::class)->quote(
$part, $wo?->vehicle, $wo?->client, $wo?->urgency ?? 'normal'
);
return view('filament.tenant.smart-price', ['quote' => $quote, 'item' => $r]);
})
->action(function (WorkOrderPart $r) {
$wo = $r->workOrder;
$quote = app(\App\Services\Pricing\PricingEngine::class)->quote(
$r->part, $wo?->vehicle, $wo?->client, $wo?->urgency ?? 'normal'
);
$r->sell_price = $quote['final'];
$r->save();
Notification::make()
->title('Preț actualizat: ' . number_format($quote['final'], 2) . ' MDL')
->success()->send();
}),
Actions\Action::make('issue_now')
->label('Eliberează')
->icon('heroicon-m-arrow-up-on-square')
->color('warning')
->visible(fn (WorkOrderPart $r) => $r->part_id
&& PartReservation::where('work_order_part_id', $r->id)
->where('status', PartReservation::STATUS_ACTIVE)
->exists())
->requiresConfirmation()
->modalDescription('Confirmă că mecanicul ia fizic piesa din depozit. Stocul scade acum, fără să aștepți închiderea fișei.')
->action(function (WorkOrderPart $r) {
$n = app(WarehouseService::class)->issueNow($r);
Notification::make()
->title("Eliberat: {$n} rezervări consumate")
->success()->send();
}),
Actions\Action::make('return_part')
->label('Restituire')
->icon('heroicon-m-arrow-uturn-left')
->color('gray')
->visible(fn (WorkOrderPart $r) => $r->part_id
&& PartReservation::where('work_order_part_id', $r->id)
->where('status', PartReservation::STATUS_CONSUMED)
->exists())
->schema([
Forms\Components\TextInput::make('qty')
->label('Cantitate restituită')
->numeric()
->required()
->minValue(0.001)
->default(fn (WorkOrderPart $r) => (float) $r->qty),
Forms\Components\Textarea::make('notes')->rows(2)->label('Observații'),
])
->action(function (WorkOrderPart $r, array $data) {
$batch = app(WarehouseService::class)->returnPart(
$r, (float) $data['qty'], $data['notes'] ?? null
);
Notification::make()
->title($batch ? 'Piesa returnată în stoc' : 'Nimic de restituit')
->{$batch ? 'success' : 'warning'}()
->send();
}),
Actions\EditAction::make(), Actions\EditAction::make(),
Actions\DeleteAction::make(), Actions\DeleteAction::make(),
]); ]);
@@ -0,0 +1,65 @@
<?php
namespace App\Filament\Tenant\Resources\WorkOrderResource\RelationManagers;
use App\Models\Tenant\Subcontractor;
use App\Models\Tenant\SubcontractJob;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;
class SubcontractJobsRelationManager extends RelationManager
{
protected static string $relationship = 'subcontractJobs';
protected static ?string $title = 'Lucrări la terți';
public function form(Schema $schema): Schema
{
return $schema->components([
Forms\Components\Select::make('subcontractor_id')
->label('Subcontractor')
->options(fn () => Subcontractor::where('is_active', true)->pluck('name', 'id'))
->searchable()
->columnSpanFull(),
Forms\Components\Select::make('category')
->label('Categorie')
->options(array_combine(Subcontractor::SPECIALTIES, Subcontractor::SPECIALTIES))
->searchable(),
Forms\Components\Select::make('status')->options(SubcontractJob::STATUSES)->default('sent')->required(),
Forms\Components\Textarea::make('description')->label('Descriere')->rows(2)->columnSpanFull(),
Forms\Components\TextInput::make('cost')->label('Cost (terț)')->numeric()->default(0)->required(),
Forms\Components\TextInput::make('markup_pct')->label('Markup %')->numeric()->default(0),
Forms\Components\TextInput::make('client_price')->label('Preț client')->numeric()->default(0)
->helperText('Folosit dacă markup = 0.'),
Forms\Components\DatePicker::make('eta')->label('ETA'),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('number')
->columns([
Tables\Columns\TextColumn::make('number')->label('Nr.'),
Tables\Columns\TextColumn::make('subcontractor.name')->label('Terț')->placeholder('—'),
Tables\Columns\TextColumn::make('category')->badge()->placeholder('—'),
Tables\Columns\TextColumn::make('cost')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('client_price')->label('Preț client')->money('MDL')->alignRight(),
Tables\Columns\TextColumn::make('margin')
->label('Marjă')
->state(fn (SubcontractJob $r) => $r->margin())
->money('MDL')->alignRight()
->color(fn ($s) => (float) $s > 0 ? 'success' : ((float) $s < 0 ? 'danger' : 'gray')),
Tables\Columns\TextColumn::make('status')
->formatStateUsing(fn ($s) => SubcontractJob::STATUSES[$s] ?? $s)
->badge()
->colors(['warning' => ['sent', 'in_progress'], 'success' => ['done', 'returned'], 'danger' => ['cancelled']]),
])
->headerActions([Actions\CreateAction::make()])
->actions([Actions\EditAction::make(), Actions\DeleteAction::make()]);
}
}
@@ -69,7 +69,23 @@ class WorksRelationManager extends RelationManager
->colors(['gray' => ['todo'], 'warning' => ['in_progress'], 'success' => ['done']]), ->colors(['gray' => ['todo'], 'warning' => ['in_progress'], 'success' => ['done']]),
]) ])
->headerActions([ ->headerActions([
Actions\CreateAction::make(), Actions\CreateAction::make()
->after(function (WorkOrderWork $record) {
// Auto-add the labor's default parts to the parent WO.
if (! $record->labor_id) return;
$labor = Labor::with('laborParts.part')->find($record->labor_id);
$wo = $record->workOrder;
if (! $labor || ! $wo || $labor->laborParts->isEmpty()) return;
$composer = app(\App\Services\ServiceComposer::class);
foreach ($labor->laborParts as $lp) {
if ($lp->part) {
$composer->addPart($wo, $lp->part, (float) $lp->qty, $lp->unit);
}
}
\Filament\Notifications\Notification::make()
->title('Piese implicite adăugate (' . $labor->laborParts->count() . ')')
->success()->send();
}),
]) ])
->actions([ ->actions([
Actions\EditAction::make(), Actions\EditAction::make(),
@@ -0,0 +1,127 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Tenant\WorkOrder;
use App\Models\Tenant\WorkOrderWork;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class MechanicApiController extends Controller
{
/** GET /api/v1/mechanic/board — only OWN WOs with their works expanded. */
public function board(): JsonResponse
{
$userId = auth()->id();
$wos = WorkOrder::with(['client:id,name', 'vehicle:id,plate,make,model', 'works'])
->where('master_id', $userId)
->whereNotIn('status', ['done', 'cancelled'])
->orderBy('opened_at')
->get()
->map(fn ($wo) => [
'id' => $wo->id, 'number' => $wo->number, 'status' => $wo->status,
'client_name' => $wo->client?->name,
'vehicle' => trim(($wo->vehicle?->make ?? '') . ' ' . ($wo->vehicle?->model ?? '')),
'plate' => $wo->vehicle?->plate,
'complaint' => $wo->complaint,
'eta_at' => $wo->eta_at?->toIso8601String(),
'works' => $wo->works->map(fn ($w) => $this->workPayload($w))->all(),
]);
return response()->json(['data' => $wos]);
}
/** POST /api/v1/mechanic/tasks/{work}/start */
public function startTask(WorkOrderWork $work): JsonResponse
{
$this->authorizeOwn($work);
$work->start();
return response()->json(['data' => $this->workPayload($work->fresh())]);
}
public function pauseTask(WorkOrderWork $work): JsonResponse
{
$this->authorizeOwn($work);
$work->pause();
return response()->json(['data' => $this->workPayload($work->fresh())]);
}
public function resumeTask(WorkOrderWork $work): JsonResponse
{
$this->authorizeOwn($work);
$work->resume();
return response()->json(['data' => $this->workPayload($work->fresh())]);
}
public function doneTask(WorkOrderWork $work): JsonResponse
{
$this->authorizeOwn($work);
$work->markDone();
return response()->json(['data' => $this->workPayload($work->fresh())]);
}
public function blockTask(Request $request, WorkOrderWork $work): JsonResponse
{
$this->authorizeOwn($work);
$data = $request->validate([
'reason' => 'required|in:' . implode(',', array_keys(WorkOrderWork::BLOCK_REASONS)),
'note' => 'nullable|string|max:1000',
]);
$work->block($data['reason'], $data['note'] ?? null);
return response()->json(['data' => $this->workPayload($work->fresh())]);
}
/** GET /api/v1/mechanic/kpi?period=2026-06 — own efficiency aggregates. */
public function kpi(Request $request): JsonResponse
{
$userId = auth()->id();
$period = $request->query('period', now()->format('Y-m'));
[$y, $m] = explode('-', $period);
$rows = WorkOrderWork::whereHas('workOrder', fn ($q) => $q->where('master_id', $userId))
->where('mechanic_status', 'done')
->whereYear('mechanic_done_at', $y)
->whereMonth('mechanic_done_at', $m)
->get();
$totalNorm = (float) $rows->sum('hours');
$totalActual = (float) $rows->sum('actual_hours');
$tasksDone = $rows->count();
$totalRevenue = (float) $rows->sum('total');
$efficiencyPct = $totalNorm > 0 ? round(100 * $totalActual / $totalNorm) : null;
return response()->json([
'period' => $period,
'tasks_done' => $tasksDone,
'norm_hours' => round($totalNorm, 2),
'actual_hours' => round($totalActual, 2),
'efficiency_pct' => $efficiencyPct,
'revenue_manopere' => round($totalRevenue, 2),
]);
}
private function workPayload(WorkOrderWork $w): array
{
return [
'id' => $w->id,
'name' => $w->name,
'mechanic_status' => $w->mechanic_status,
'norm_hours' => (float) $w->hours,
'actual_hours' => (float) $w->actual_hours,
'efficiency_pct' => $w->efficiencyPct(),
'efficiency_class' => $w->efficiencyClass(),
'block_reason' => $w->block_reason,
'block_note' => $w->block_note,
'mechanic_started_at' => $w->mechanic_started_at?->toIso8601String(),
'mechanic_done_at' => $w->mechanic_done_at?->toIso8601String(),
];
}
private function authorizeOwn(WorkOrderWork $work): void
{
if ($work->workOrder?->master_id !== auth()->id()) {
abort(403, 'Work belongs to a different mechanic.');
}
}
}
@@ -0,0 +1,102 @@
<?php
namespace App\Http\Controllers\Api;
use App\Auth\Permissions;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Spatie\Permission\PermissionRegistrar;
class RoleApiController extends Controller
{
public function index(): JsonResponse
{
$this->authorize(Permissions::ADMIN_ROLES_MANAGE);
$roles = Role::withCount('permissions')->orderBy('name')->get();
return response()->json(['data' => $roles]);
}
public function show(Role $role): JsonResponse
{
$this->authorize(Permissions::ADMIN_ROLES_MANAGE);
return response()->json(['data' => $role->load('permissions')]);
}
public function store(Request $request): JsonResponse
{
$this->authorize(Permissions::ADMIN_ROLES_MANAGE);
$data = $request->validate([
'name' => 'required|string|max:64',
'permissions' => 'sometimes|array',
'permissions.*' => 'string',
]);
// Disallow overwriting system roles
if (in_array($data['name'], array_keys(Permissions::roleMatrix()), true)) {
return response()->json(['error' => 'System role name is reserved'], 422);
}
$role = Role::create(['name' => $data['name'], 'guard_name' => 'web']);
if (! empty($data['permissions'])) {
$role->syncPermissions($data['permissions']);
}
return response()->json(['data' => $role->load('permissions')], 201);
}
public function update(Request $request, Role $role): JsonResponse
{
$this->authorize(Permissions::ADMIN_ROLES_MANAGE);
if (in_array($role->name, array_keys(Permissions::roleMatrix()), true)) {
return response()->json(['error' => 'Cannot rename system role'], 422);
}
$data = $request->validate(['name' => 'required|string|max:64']);
$role->update($data);
return response()->json(['data' => $role]);
}
public function destroy(Role $role): JsonResponse
{
$this->authorize(Permissions::ADMIN_ROLES_MANAGE);
if (in_array($role->name, array_keys(Permissions::roleMatrix()), true)) {
return response()->json(['error' => 'Cannot delete system role'], 422);
}
$role->delete();
return response()->json(['deleted' => true]);
}
public function permissions(Role $role): JsonResponse
{
$this->authorize(Permissions::ADMIN_ROLES_MANAGE);
return response()->json(['data' => $role->permissions->pluck('name')]);
}
public function syncPermissions(Request $request, Role $role): JsonResponse
{
$this->authorize(Permissions::ADMIN_ROLES_MANAGE);
$data = $request->validate([
'permissions' => 'required|array',
'permissions.*' => 'string',
]);
$role->syncPermissions($data['permissions']);
app(PermissionRegistrar::class)->forgetCachedPermissions();
return response()->json(['data' => $role->fresh()->permissions->pluck('name')]);
}
public function permissionCatalog(): JsonResponse
{
return response()->json([
'data' => Permission::orderBy('name')->get(['id', 'name']),
'grouped' => Permissions::grouped(),
'labels' => Permissions::labels(),
'roles' => Permissions::roleLabels(),
]);
}
private function authorize(string $permission): void
{
if (! auth()->user() || ! auth()->user()->canDo($permission)) {
abort(403, "Missing permission: $permission");
}
}
}
@@ -0,0 +1,228 @@
<?php
namespace App\Http\Controllers\Api;
use App\Auth\Permissions;
use App\Http\Controllers\Controller;
use App\Models\Tenant\User;
use App\Models\Tenant\UserPermissionOverride;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Permission;
class UserApiController extends Controller
{
public function index(Request $request): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_VIEW);
$q = User::query();
if ($role = $request->query('role')) $q->where('role', $role);
if ($status = $request->query('status')) $q->where('status', $status);
if ($search = $request->query('q')) {
$q->where(fn ($qq) => $qq->where('name', 'like', "%$search%")->orWhere('email', 'like', "%$search%"));
}
return response()->json([
'data' => $q->paginate((int) $request->query('per_page', 25))->items(),
'meta' => ['total' => $q->count()],
]);
}
public function show(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_VIEW);
return response()->json(['data' => $user->load('roles', 'permissionOverrides.permission', 'invitedBy:id,name')]);
}
public function store(Request $request): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$data = $request->validate([
'name' => 'required|string|max:120',
'email' => 'required|email|max:120',
'phone' => 'nullable|string|max:40',
'role' => 'required|string|in:' . implode(',', array_keys(Permissions::roleMatrix())),
'locale' => 'nullable|in:ro,ru,en',
'send_invitation' => 'nullable|boolean',
]);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'] ?? null,
'role' => $data['role'],
'locale' => $data['locale'] ?? 'ro',
'status' => 'inactive',
'password' => Hash::make(bin2hex(random_bytes(16))), // placeholder until invitation accept
]);
$user->syncRoles([$data['role']]);
if ($data['send_invitation'] ?? true) {
$user->sendInvitation(auth()->user());
}
return response()->json(['data' => $user->fresh(), 'invitation_sent' => $data['send_invitation'] ?? true], 201);
}
public function update(Request $request, User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$data = $request->validate([
'name' => 'sometimes|string|max:120',
'email' => 'sometimes|email|max:120',
'phone' => 'sometimes|nullable|string|max:40',
'locale' => 'sometimes|in:ro,ru,en',
'role' => 'sometimes|in:' . implode(',', array_keys(Permissions::roleMatrix())),
'status' => 'sometimes|in:active,inactive,blocked',
]);
$user->update($data);
if (isset($data['role'])) $user->syncRoles([$data['role']]);
return response()->json(['data' => $user->fresh()]);
}
public function destroy(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$user->delete();
return response()->json(['deleted' => true]);
}
public function activate(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$user->update(['status' => 'active']);
return response()->json(['data' => $user->fresh()]);
}
public function deactivate(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$user->update(['status' => 'inactive']);
DB::table('sessions')->where('user_id', $user->id)->delete();
return response()->json(['data' => $user->fresh()]);
}
public function resendInvitation(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
if ($user->accepted_at) {
return response()->json(['error' => 'User already accepted invitation'], 422);
}
$user->sendInvitation(auth()->user());
return response()->json(['invitation_sent' => true]);
}
public function forcePasswordReset(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$user->update(['status' => 'inactive', 'accepted_at' => null]);
DB::table('sessions')->where('user_id', $user->id)->delete();
$user->sendInvitation(auth()->user());
return response()->json(['invitation_sent' => true]);
}
public function sessions(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$rows = DB::table('sessions')->where('user_id', $user->id)
->select('id', 'ip_address', 'user_agent', 'last_activity')->get();
return response()->json(['data' => $rows]);
}
public function revokeSession(User $user, string $sessionId): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$n = DB::table('sessions')->where('user_id', $user->id)->where('id', $sessionId)->delete();
return response()->json(['revoked' => $n > 0]);
}
public function revokeAllSessions(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$n = DB::table('sessions')->where('user_id', $user->id)->delete();
return response()->json(['revoked_count' => $n]);
}
public function roles(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_VIEW);
return response()->json(['data' => $user->roles->pluck('name')]);
}
public function assignRole(Request $request, User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$data = $request->validate(['role' => 'required|string']);
$user->assignRole($data['role']);
return response()->json(['data' => $user->roles->pluck('name')]);
}
public function removeRole(User $user, string $role): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$user->removeRole($role);
return response()->json(['data' => $user->roles->pluck('name')]);
}
public function permissions(User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_VIEW);
// Effective: roles + grants - denies (active only)
$rolePerms = $user->getAllPermissions()->pluck('name');
$denies = $user->permissionOverrides()
->where('mode', 'deny')
->where(fn ($q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()))
->with('permission')->get()->pluck('permission.name');
$grants = $user->permissionOverrides()
->where('mode', 'grant')
->where(fn ($q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()))
->with('permission')->get()->pluck('permission.name');
$effective = $rolePerms->merge($grants)->unique()->reject(fn ($p) => $denies->contains($p))->values();
return response()->json([
'data' => $effective,
'overrides' => ['grants' => $grants, 'denies' => $denies],
]);
}
public function addOverride(Request $request, User $user): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$data = $request->validate([
'permission' => 'required|string',
'mode' => 'required|in:grant,deny',
'reason' => 'nullable|string',
'expires_at' => 'nullable|date',
]);
$perm = Permission::where('name', $data['permission'])->firstOrFail();
UserPermissionOverride::updateOrCreate(
['user_id' => $user->id, 'permission_id' => $perm->id],
[
'mode' => $data['mode'],
'reason' => $data['reason'] ?? null,
'expires_at' => $data['expires_at'] ?? null,
'granted_by_id' => auth()->id(),
'granted_at' => now(),
]
);
return response()->json(['data' => $user->load('permissionOverrides.permission')]);
}
public function removeOverride(User $user, string $permission): JsonResponse
{
$this->authorize(Permissions::ADMIN_USERS_MANAGE);
$perm = Permission::where('name', $permission)->firstOrFail();
UserPermissionOverride::where('user_id', $user->id)->where('permission_id', $perm->id)->delete();
return response()->json(['removed' => true]);
}
private function authorize(string $permission): void
{
if (! auth()->user() || ! auth()->user()->canDo($permission)) {
abort(403, "Missing permission: $permission");
}
}
}
@@ -0,0 +1,49 @@
<?php
namespace App\Http\Controllers;
use App\Models\Tenant\User;
use Illuminate\Http\Request;
class InvitationController extends Controller
{
public function show(string $token)
{
$user = User::findByInvitationToken($token);
if (! $user || ! $user->isPendingInvitation()) {
return view('invitations.invalid');
}
// Invitations expire after 7 days
if ($user->invited_at && $user->invited_at->lt(now()->subDays(7))) {
return view('invitations.expired');
}
return view('invitations.accept', [
'token' => $token,
'name' => $user->name,
'email' => $user->email,
'company' => $user->company?->display_name ?? $user->company?->name ?? 'AutoCRM',
]);
}
public function accept(string $token, Request $request)
{
$request->validate([
'password' => 'required|min:8|confirmed',
]);
$user = User::findByInvitationToken($token);
if (! $user || ! $user->isPendingInvitation()) {
return view('invitations.invalid');
}
if ($user->invited_at && $user->invited_at->lt(now()->subDays(7))) {
return view('invitations.expired');
}
$user->acceptInvitation($request->input('password'));
// Redirect to tenant login on the appropriate subdomain
$loginUrl = $user->company?->url('/app/login') ?? '/app/login';
return redirect($loginUrl)->with('status', 'Invitația a fost acceptată. Loghează-te cu noua parolă.');
}
}
@@ -0,0 +1,38 @@
<?php
namespace App\Http\Controllers;
use App\Models\Tenant\Part;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use Illuminate\Http\Request;
class PartLabelsController extends Controller
{
public function sheet(Request $request)
{
$ids = array_filter(array_map('intval', explode(',', (string) $request->query('ids', ''))));
if (empty($ids)) abort(400, 'No parts selected.');
$parts = Part::whereIn('id', $ids)->orderBy('name')->get();
$opts = new QROptions([
'outputType' => QRCode::OUTPUT_MARKUP_SVG,
'eccLevel' => QRCode::ECC_M,
'scale' => 4,
'imageBase64' => false,
'addQuietzone' => true,
]);
$labels = $parts->map(function (Part $p) use ($opts) {
$payload = 'PART:' . ($p->article ?: $p->id);
return [
'part' => $p,
'svg' => (new QRCode($opts))->render($payload),
'payload' => $payload,
];
});
return view('parts.labels', ['labels' => $labels]);
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Http\Controllers;
use App\Models\Tenant\PushSubscription;
use Illuminate\Http\Request;
class PushSubscriptionController extends Controller
{
public function subscribe(Request $request)
{
$data = $request->validate([
'endpoint' => 'required|string|max:500',
'keys.p256dh' => 'required|string',
'keys.auth' => 'required|string',
'contentEncoding' => 'nullable|string|max:32',
]);
$user = $request->user();
PushSubscription::updateOrCreate(
['endpoint' => $data['endpoint']],
[
'company_id' => $user?->company_id,
'user_id' => $user?->id,
'public_key' => $data['keys']['p256dh'],
'auth_token' => $data['keys']['auth'],
'content_encoding' => $data['contentEncoding'] ?? 'aesgcm',
'user_agent' => substr((string) $request->userAgent(), 0, 255),
]
);
return response()->json(['ok' => true]);
}
public function unsubscribe(Request $request)
{
$endpoint = $request->input('endpoint');
if ($endpoint) {
PushSubscription::where('endpoint', $endpoint)->delete();
}
return response()->json(['ok' => true]);
}
}
+184
View File
@@ -0,0 +1,184 @@
<?php
namespace App\Http\Controllers;
use App\Models\Tenant\Client;
use App\Models\Tenant\ShopCustomer;
use App\Tenancy\TenantManager;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ShopAuthController extends Controller
{
private function tenantOrFail()
{
$tenant = app(TenantManager::class)->current();
if (! $tenant || ! data_get($tenant->settings, 'shop.enabled')) {
throw new NotFoundHttpException('Magazinul online nu este activ.');
}
return $tenant;
}
public function showRegister()
{
$tenant = $this->tenantOrFail();
if (Auth::guard('shop')->check()) return redirect('/shop/account');
return view('shop.auth.register', ['tenant' => $tenant, 'cartCount' => $this->cartCount()]);
}
public function register(Request $request)
{
$tenant = $this->tenantOrFail();
$data = $request->validate([
'name' => 'required|string|max:160',
'phone' => 'required|string|max:40',
'email' => 'nullable|email|max:160',
'password' => 'required|string|min:6|confirmed',
]);
// Unique per tenant (handled by composite index, but check for nicer error).
if (ShopCustomer::where('phone', $data['phone'])->exists()) {
return back()->withErrors(['phone' => 'Există deja un cont cu acest telefon.'])->withInput();
}
// Auto-link to existing Client by phone if present.
$client = Client::where('phone', $data['phone'])->first();
$customer = ShopCustomer::create([
'client_id' => $client?->id,
'name' => $data['name'],
'phone' => $data['phone'],
'email' => $data['email'] ?? null,
'password' => $data['password'], // hashed by cast
]);
event(new Registered($customer));
Auth::guard('shop')->login($customer, remember: true);
$customer->forceFill(['last_login_at' => now()])->save();
return redirect('/shop/account');
}
public function showLogin()
{
$tenant = $this->tenantOrFail();
if (Auth::guard('shop')->check()) return redirect('/shop/account');
return view('shop.auth.login', ['tenant' => $tenant, 'cartCount' => $this->cartCount()]);
}
public function login(Request $request)
{
$tenant = $this->tenantOrFail();
$data = $request->validate([
'phone' => 'required|string|max:40',
'password' => 'required|string',
]);
$ok = Auth::guard('shop')->attempt(
['phone' => $data['phone'], 'password' => $data['password']],
remember: true
);
if (! $ok) {
return back()->withErrors(['phone' => 'Telefon sau parolă incorecte.'])->withInput();
}
$request->session()->regenerate();
Auth::guard('shop')->user()?->forceFill(['last_login_at' => now()])->save();
return redirect()->intended('/shop/account');
}
public function logout(Request $request)
{
Auth::guard('shop')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect('/shop');
}
public function account()
{
$tenant = $this->tenantOrFail();
$customer = Auth::guard('shop')->user();
if (! $customer) return redirect('/shop/login');
$orders = $customer->orders()
->latest('created_at')
->limit(50)
->get();
return view('shop.account', [
'tenant' => $tenant,
'customer' => $customer,
'orders' => $orders,
'cartCount' => $this->cartCount(),
]);
}
public function showForgotPassword()
{
$tenant = $this->tenantOrFail();
return view('shop.auth.forgot', ['tenant' => $tenant, 'cartCount' => $this->cartCount()]);
}
public function sendResetLink(Request $request)
{
$this->tenantOrFail();
$data = $request->validate(['email' => 'required|email']);
// Send (always returns generic "sent" message — don't disclose if email exists).
Password::broker('shop_customers')->sendResetLink(['email' => $data['email']]);
return back()->with('status', 'Dacă există un cont cu acest email, am trimis un link de resetare.');
}
public function showResetPassword(string $token, Request $request)
{
$tenant = $this->tenantOrFail();
return view('shop.auth.reset', [
'tenant' => $tenant,
'token' => $token,
'email' => $request->query('email'),
'cartCount' => $this->cartCount(),
]);
}
public function resetPassword(Request $request)
{
$this->tenantOrFail();
$data = $request->validate([
'token' => 'required|string',
'email' => 'required|email',
'password' => 'required|string|min:6|confirmed',
]);
$status = Password::broker('shop_customers')->reset(
$data,
function (ShopCustomer $customer, string $password) {
$customer->forceFill([
'password' => Hash::make($password),
'remember_token' => Str::random(60),
])->save();
event(new PasswordReset($customer));
}
);
if ($status === Password::PASSWORD_RESET) {
return redirect('/shop/login')->with('status', 'Parola a fost resetată. Te poți loga acum.');
}
return back()->withErrors(['email' => 'Link invalid sau expirat. Cere unul nou.'])->withInput();
}
private function cartCount(): int
{
$tenant = app(TenantManager::class)->current();
$cart = (array) session('shop_cart_' . ($tenant?->id ?? '0'), []);
return (int) collect($cart)->sum('qty');
}
}
+248
View File
@@ -0,0 +1,248 @@
<?php
namespace App\Http\Controllers;
use App\Models\Tenant\OnlineOrder;
use App\Models\Tenant\OnlineOrderItem;
use App\Models\Tenant\Part;
use App\Services\Ai\VinDecoder;
use App\Tenancy\TenantManager;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ShopController extends Controller
{
private function tenantOrFail()
{
$tenant = app(TenantManager::class)->current();
if (! $tenant) {
throw new NotFoundHttpException('Magazinul e disponibil doar pe subdomeniul service-ului.');
}
if (! data_get($tenant->settings, 'shop.enabled')) {
throw new NotFoundHttpException('Magazinul online nu este activ.');
}
return $tenant;
}
public function catalog(Request $request)
{
$tenant = $this->tenantOrFail();
$term = $request->query('q');
$category = $request->query('cat');
$inStock = $request->boolean('in_stock');
$query = Part::searchPublished($term);
if ($category) $query->where('category', $category);
if ($inStock) $query->where('qty', '>', 0);
$parts = $query->orderBy('name')->paginate(24)->withQueryString();
$categories = Part::published()->distinct()->pluck('category')->filter()->sort()->values();
return view('shop.catalog', [
'tenant' => $tenant,
'parts' => $parts,
'categories' => $categories,
'term' => $term,
'category' => $category,
'inStock' => $inStock,
'cartCount' => $this->cartCount(),
]);
}
public function part(Request $request, int $id)
{
$tenant = $this->tenantOrFail();
$part = Part::published()->with('crossRefs')->find($id);
if (! $part) throw new NotFoundHttpException('Piesa nu există sau nu e publicată.');
return view('shop.part', [
'tenant' => $tenant,
'part' => $part,
'cartCount' => $this->cartCount(),
]);
}
public function vin(Request $request)
{
$tenant = $this->tenantOrFail();
$vin = strtoupper(trim((string) $request->query('vin', '')));
$decoded = null;
if ($vin !== '') {
$decoded = app(VinDecoder::class)->decode($vin);
}
return view('shop.vin', [
'tenant' => $tenant,
'vin' => $vin,
'decoded' => $decoded,
'cartCount' => $this->cartCount(),
]);
}
// ─── Cart (session) ───────────────────────────────────────────
private function cartKey(): string
{
$tenant = app(TenantManager::class)->current();
return 'shop_cart_' . ($tenant?->id ?? '0');
}
private function cart(): array
{
return (array) session($this->cartKey(), []);
}
private function cartCount(): int
{
return (int) collect($this->cart())->sum('qty');
}
public function addToCart(Request $request, int $id)
{
$this->tenantOrFail();
$part = Part::published()->findOrFail($id);
$qty = max(1, (int) $request->input('qty', 1));
$cart = $this->cart();
$cart[$id] = [
'part_id' => $part->id,
'name' => $part->name,
'article' => $part->article,
'price' => (float) $part->sell_price,
'qty' => ($cart[$id]['qty'] ?? 0) + $qty,
];
session([$this->cartKey() => $cart]);
return redirect('/shop/cart');
}
public function updateCart(Request $request)
{
$this->tenantOrFail();
$cart = $this->cart();
foreach ((array) $request->input('qty', []) as $id => $qty) {
$qty = (int) $qty;
if ($qty <= 0) {
unset($cart[$id]);
} elseif (isset($cart[$id])) {
$cart[$id]['qty'] = $qty;
}
}
session([$this->cartKey() => $cart]);
return redirect('/shop/cart');
}
public function showCart()
{
$tenant = $this->tenantOrFail();
$cart = $this->cart();
$subtotal = collect($cart)->sum(fn ($i) => $i['price'] * $i['qty']);
return view('shop.cart', [
'tenant' => $tenant,
'cart' => $cart,
'subtotal' => $subtotal,
'cartCount' => $this->cartCount(),
]);
}
public function checkout()
{
$tenant = $this->tenantOrFail();
$cart = $this->cart();
if (empty($cart)) return redirect('/shop');
$subtotal = collect($cart)->sum(fn ($i) => $i['price'] * $i['qty']);
$customer = \Illuminate\Support\Facades\Auth::guard('shop')->user();
return view('shop.checkout', [
'tenant' => $tenant,
'cart' => $cart,
'subtotal' => $subtotal,
'customer' => $customer,
'deliveryOptions' => (array) data_get($tenant->settings, 'shop.delivery_methods', ['pickup']),
'cartCount' => $this->cartCount(),
]);
}
public function placeOrder(Request $request)
{
$tenant = $this->tenantOrFail();
$cart = $this->cart();
if (empty($cart)) return redirect('/shop');
$data = $request->validate([
'customer_name' => 'required|string|max:160',
'customer_phone' => 'required|string|max:40',
'customer_email' => 'nullable|email|max:160',
'delivery_method' => 'required|in:pickup,courier,post',
'address' => 'nullable|string|max:255',
'notes' => 'nullable|string|max:1000',
]);
$deliveryFee = 0.0;
$subtotal = collect($cart)->sum(fn ($i) => $i['price'] * $i['qty']);
if ($data['delivery_method'] !== 'pickup') {
$fee = (float) data_get($tenant->settings, 'shop.delivery_fee', 0);
$freeOver = (float) data_get($tenant->settings, 'shop.free_delivery_over', 0);
$deliveryFee = ($freeOver > 0 && $subtotal >= $freeOver) ? 0.0 : $fee;
}
$shopCustomer = \Illuminate\Support\Facades\Auth::guard('shop')->user();
$order = DB::transaction(function () use ($tenant, $cart, $data, $deliveryFee, $shopCustomer) {
$order = OnlineOrder::create([
'number' => OnlineOrder::generateNumber($tenant->id),
'shop_customer_id' => $shopCustomer?->id,
'client_id' => $shopCustomer?->client_id,
'customer_name' => $data['customer_name'],
'customer_phone' => $data['customer_phone'],
'customer_email' => $data['customer_email'] ?? null,
'delivery_method' => $data['delivery_method'],
'address' => $data['address'] ?? null,
'notes' => $data['notes'] ?? null,
'status' => 'new',
'delivery_fee' => $deliveryFee,
]);
foreach ($cart as $item) {
OnlineOrderItem::create([
'online_order_id' => $order->id,
'part_id' => $item['part_id'] ?? null,
'name' => $item['name'],
'article' => $item['article'] ?? null,
'qty' => $item['qty'],
'price' => $item['price'],
]);
}
$order->refresh()->recalcTotal();
return $order;
});
session()->forget($this->cartKey());
// Notify (best-effort): customer + shop staff.
try {
app(\App\Services\Notifications\ShopOrderNotifier::class)->placed($order);
} catch (\Throwable $e) {
\Illuminate\Support\Facades\Log::debug('shop order notify skipped: ' . $e->getMessage());
}
return redirect('/shop/order/' . $order->tracking_token);
}
public function orderStatus(Request $request, string $token)
{
$tenant = $this->tenantOrFail();
$order = OnlineOrder::with('items')->where('tracking_token', $token)->first();
if (! $order) throw new NotFoundHttpException('Comanda nu a fost găsită.');
return view('shop.order', [
'tenant' => $tenant,
'order' => $order,
'cartCount' => $this->cartCount(),
]);
}
}
@@ -0,0 +1,100 @@
<?php
namespace App\Http\Controllers;
use App\Models\Central\Company;
use App\Models\Tenant\Client;
use App\Services\Notifications\TelegramService;
use App\Tenancy\TenantManager;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
/**
* Receives Telegram updates per tenant. URL: /telegram/webhook/{slug}
*
* To link a Telegram account to a Client record, the bot expects the user
* to share their phone via Telegram's contact share button (Telegram lets
* users send their own phone with one tap). We match the shared phone (or
* the message text fallback) to clients.phone and persist chat_id.
*/
class TelegramWebhookController extends Controller
{
public function handle(Request $request, string $slug, TelegramService $telegram)
{
$company = Company::where('slug', $slug)->first();
if (! $company) return response()->json(['ok' => false], 404);
$expectedSecret = $telegram->webhookSecretFor($company);
$providedSecret = $request->header('X-Telegram-Bot-Api-Secret-Token');
if ($expectedSecret && $providedSecret !== $expectedSecret) {
Log::warning('telegram.webhook bad secret', ['tenant' => $slug]);
return response()->json(['ok' => false], 401);
}
app(TenantManager::class)->setCurrent($company);
$message = $request->input('message', []);
$chatId = (string) data_get($message, 'chat.id', '');
if (! $chatId) {
return response()->json(['ok' => true]);
}
$contact = data_get($message, 'contact');
$text = trim((string) data_get($message, 'text', ''));
$client = null;
$phoneRaw = null;
if ($contact) {
$phoneRaw = data_get($contact, 'phone_number');
} elseif (preg_match('/(\+?[0-9\-\s\(\)]{7,})/', $text, $m)) {
$phoneRaw = $m[1];
}
if ($phoneRaw) {
$needle = Client::normalizePhone($phoneRaw);
if ($needle) {
$client = Client::whereRaw(
"REPLACE(REPLACE(REPLACE(REPLACE(phone, ' ', ''), '-', ''), '(', ''), ')', '') LIKE ?",
['%' . substr($needle, -9) . '%']
)->first();
}
}
if (! $client && $text === '/start') {
$telegram->sendMessage($company, $chatId,
'Salut! Pentru a primi notificări despre mașina ta, ' .
'apasă butonul „Share contact" sau trimite numărul tău de telefon.'
);
return response()->json(['ok' => true]);
}
if ($client) {
$client->telegram_chat_id = $chatId;
$client->saveQuietly();
$name = $company->display_name ?? $company->name;
$telegram->sendMessage($company, $chatId,
"Te-am identificat — <b>{$client->name}</b>.\n" .
"Vei primi aici notificări despre fișele tale de la <b>{$name}</b>.\n\n" .
"Trimite /stop oricând ca să oprești notificările."
);
return response()->json(['ok' => true]);
}
if ($text === '/stop') {
Client::where('telegram_chat_id', $chatId)->update(['telegram_chat_id' => null]);
$telegram->sendMessage($company, $chatId, 'Notificările au fost oprite.');
return response()->json(['ok' => true]);
}
if ($phoneRaw) {
$telegram->sendMessage($company, $chatId,
"Nu am găsit un client cu acest număr la {$company->name}. " .
"Verifică telefonul sau contactează service-ul."
);
}
return response()->json(['ok' => true]);
}
}

Some files were not shown because too many files have changed in this diff Show More