""" Main window. Deliberately thin: it wires the toolbar, tabs, and dialogs together, and delegates real work to app.services (fetching), app.workers (saving/loading/stats), and app.services.odoo_export (file export). JIRA import creates/updates ticket rows. "Pull Tracking Numbers" (ShipStation) doesn't create anything of its own - it merges tracking numbers onto the matching JIRA row by ticket number, since ShipStation here is purely a label-generation step for JIRA tickets. """ from __future__ import annotations from PyQt6.QtGui import QAction from PyQt6.QtWidgets import ( QMainWindow, QWidget, QVBoxLayout, QToolBar, QStatusBar, QMessageBox, QLabel, QTabWidget, QFileDialog, ) from app import config from app.services import SERVICE_REGISTRY from app.services.odoo_export import export_orders_to_csv from app.status_rules import parse_status_list, find_new_cancellations from app.ui.settings_dialog import SettingsDialog from app.ui.widgets.orders_table import OrdersTableView from app.ui.widgets.dashboard import DashboardWidget from app.ui.widgets.order_detail_dialog import OrderDetailDialog from app.workers import FetchOrdersWorker, load_all_orders, get_dashboard_stats class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Order Manager") self.resize(1150, 700) self._workers: dict[str, FetchOrdersWorker] = {} self._import_actions: dict[str, QAction] = {} self._build_ui() self._refresh_everything() # -- UI construction ------------------------------------------------- def _build_ui(self) -> None: central = QWidget() layout = QVBoxLayout(central) self.tabs = QTabWidget() self.dashboard = DashboardWidget() self.orders_table = OrdersTableView() self.orders_table.order_double_clicked.connect(self._on_order_double_clicked) self.tabs.addTab(self.dashboard, "Dashboard") self.tabs.addTab(self.orders_table, "All Orders") layout.addWidget(self.tabs) self.setCentralWidget(central) toolbar = QToolBar("Main") toolbar.setMovable(False) self.addToolBar(toolbar) jira_action = QAction("Import from JIRA", self) jira_action.triggered.connect(lambda: self._on_import_clicked("jira")) toolbar.addAction(jira_action) self._import_actions["jira"] = jira_action shipstation_action = QAction("Pull Tracking Numbers (ShipStation)", self) shipstation_action.triggered.connect(lambda: self._on_import_clicked("shipstation")) toolbar.addAction(shipstation_action) self._import_actions["shipstation"] = shipstation_action toolbar.addSeparator() export_action = QAction("Export Visible Orders to Odoo CSV", self) export_action.triggered.connect(self._on_export_clicked) toolbar.addAction(export_action) toolbar.addSeparator() settings_action = QAction("Settings", self) settings_action.triggered.connect(self._on_settings_clicked) toolbar.addAction(settings_action) refresh_action = QAction("Refresh from Local DB", self) refresh_action.triggered.connect(self._refresh_everything) toolbar.addAction(refresh_action) self.status_bar = QStatusBar() self.setStatusBar(self.status_bar) self.status_label = QLabel("Ready.") self.status_bar.addWidget(self.status_label) # -- actions ----------------------------------------------------------- def _on_settings_clicked(self) -> None: dialog = SettingsDialog(self) dialog.exec() def _on_order_double_clicked(self, order) -> None: dialog = OrderDetailDialog(order, self) dialog.exec() def _on_import_clicked(self, service_name: str) -> None: service_cls = SERVICE_REGISTRY[service_name] service = service_cls() if not service.is_configured(): QMessageBox.warning( self, f"{service_name.title()} not configured", f"Fill in the {service_name.title()} settings first, then try again.", ) return action = self._import_actions[service_name] action.setEnabled(False) if service_name == "jira": self.status_label.setText("Importing orders from JIRA...") else: self.status_label.setText("Pulling tracking numbers from ShipStation...") worker = FetchOrdersWorker(service) worker.finished_ok.connect( lambda result: self._on_import_finished(service_name, result) ) worker.failed.connect(lambda message: self._on_import_failed(service_name, message)) self._workers[service_name] = worker # keep a reference so it isn't garbage collected worker.start() def _on_import_finished(self, service_name: str, result: dict) -> None: self._import_actions[service_name].setEnabled(True) self._refresh_everything() if service_name == "jira": self.status_label.setText( f"JIRA import complete: {result['new_count']} new, " f"{result['updated_count']} updated." ) self._notify_new_cancellations(result["status_changes"]) else: self.status_label.setText( f"Tracking pull complete: {result['enriched_count']} ticket(s) updated with " f"tracking numbers." ) self._notify_unmatched_tracking(result["unmatched_tracking_tickets"]) def _notify_unmatched_tracking(self, unmatched_tickets: list) -> None: if not unmatched_tickets: return lines = unmatched_tickets[:15] if len(unmatched_tickets) > 15: lines.append(f"...and {len(unmatched_tickets) - 15} more") QMessageBox.information( self, "Some tracking numbers didn't match a known ticket", "ShipStation had tracking numbers for these ticket numbers, but no matching " "JIRA ticket was found locally (maybe it hasn't been imported yet):\n\n" + "\n".join(lines), ) def _notify_new_cancellations(self, status_changes: list) -> None: cancelled_statuses = parse_status_list( config.get("CANCELLED_STATUSES", config.DEFAULT_CANCELLED_STATUSES) ) newly_cancelled = find_new_cancellations(status_changes, cancelled_statuses) if not newly_cancelled: return lines = [ f"{c['ticket_number']}: {c['old_status']} -> {c['new_status']}" for c in newly_cancelled[:15] ] if len(newly_cancelled) > 15: lines.append(f"...and {len(newly_cancelled) - 15} more") QMessageBox.warning( self, "Ticket(s) cancelled", f"{len(newly_cancelled)} ticket(s) were just cancelled:\n\n" + "\n".join(lines), ) def _on_import_failed(self, service_name: str, message: str) -> None: self._import_actions[service_name].setEnabled(True) self.status_label.setText(f"{service_name.title()} import failed.") QMessageBox.critical(self, f"{service_name.title()} import failed", message) def _on_export_clicked(self) -> None: orders = self.orders_table.visible_orders() if not orders: QMessageBox.information( self, "Nothing to export", "No orders match the current filters." ) return filepath, _ = QFileDialog.getSaveFileName( self, "Export to Odoo CSV", "odoo_orders_export.csv", "CSV Files (*.csv)" ) if not filepath: return try: count = export_orders_to_csv(orders, filepath) except OSError as exc: QMessageBox.critical(self, "Export failed", str(exc)) return self.status_label.setText(f"Exported {count} order(s) to {filepath}") def _refresh_everything(self) -> None: orders = load_all_orders() self.orders_table.set_orders(orders) self.dashboard.update_stats(get_dashboard_stats()) if not hasattr(self, "_suppress_ready_status"): self.status_label.setText(f"{len(orders)} order(s) in local database.") # --------------------------------------------------------------------------- # Future growth notes: # # - Odoo push API (once ready): add app/services/odoo_service.py with a # push_orders(orders) method, wire a new toolbar action similarly to # _on_import_clicked, and it can eventually replace/augment odoo_export.py. # - Emailed-label SKU: these tickets won't get tracking numbers pulled via # the normal ShipStation flow - once its handling is defined, it likely # needs its own terminal status and its own "mark as sent" action here. # - Pipeline actions ("mark fulfilled manually", "void a label"): add # toolbar actions gated on table selection. # - If the window grows too much, split each tab's toolbar into its own # QToolBar shown only while that tab is active. # ---------------------------------------------------------------------------