|
|
@@ -310,6 +310,11 @@ class Report extends Component
|
|
|
'particolarita_strada.required' => 'Il campo particolarità strada è obbligatorio ai fini della validazione del verbale',
|
|
|
];
|
|
|
|
|
|
+ protected $listeners = [
|
|
|
+ 'process-offline-reports' => 'processOfflineReports'
|
|
|
+ ];
|
|
|
+
|
|
|
+
|
|
|
public function resetFields()
|
|
|
{
|
|
|
$this->name = '';
|
|
|
@@ -589,10 +594,7 @@ class Report extends Component
|
|
|
public function add()
|
|
|
{
|
|
|
try {
|
|
|
- $this->protocollo_num = DB::table('fcf_reports_reports')
|
|
|
- ->where('protocollo_anno', date('Y'))
|
|
|
- ->max('protocollo_num') + 1;
|
|
|
-
|
|
|
+ $this->protocollo_num = $this->getNextProtocolloNum();
|
|
|
$this->protocollo_anno = date('Y');
|
|
|
$this->name = $this->protocollo_num . "/" . $this->protocollo_anno;
|
|
|
|
|
|
@@ -624,18 +626,73 @@ class Report extends Component
|
|
|
'particolarita_strada' => $this->particolarita_strada,
|
|
|
];
|
|
|
|
|
|
- Log::info('Creating report with data:', $reportData);
|
|
|
-
|
|
|
- $record = \App\Models\Report::create($reportData);
|
|
|
-
|
|
|
- session()->flash('success', 'Record creato');
|
|
|
- $this->closeModal();
|
|
|
- $this->edit($record->id);
|
|
|
+ if ($this->isOnline()) {
|
|
|
+ Log::info('Creating report with data:', $reportData);
|
|
|
+ $record = \App\Models\Report::create($reportData);
|
|
|
+ session()->flash('success', 'Record creato');
|
|
|
+ $this->closeModal();
|
|
|
+ $this->edit($record->id);
|
|
|
+ } else {
|
|
|
+ $this->saveOffline($reportData);
|
|
|
+ session()->flash('success', 'Record salvato offline. Verrà sincronizzato quando sarai online.');
|
|
|
+ $this->closeModal();
|
|
|
+ }
|
|
|
} catch (\Exception $ex) {
|
|
|
session()->flash('error', 'Errore in fase di salvataggio (' . $ex->getMessage() . ')');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ protected function getNextProtocolloNum()
|
|
|
+ {
|
|
|
+ if ($this->isOnline()) {
|
|
|
+ return DB::table('fcf_reports_reports')
|
|
|
+ ->where('protocollo_anno', date('Y'))
|
|
|
+ ->max('protocollo_num') + 1;
|
|
|
+ } else {
|
|
|
+ return 9999;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function isOnline()
|
|
|
+ {
|
|
|
+ return @fsockopen("www.google.com", 80);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function saveOffline($reportData)
|
|
|
+ {
|
|
|
+ $this->dispatchBrowserEvent('save-offline-report', [
|
|
|
+ 'report' => $reportData
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function getOfflineReports()
|
|
|
+ {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function syncOfflineData()
|
|
|
+ {
|
|
|
+ $this->dispatchBrowserEvent('get-offline-reports');
|
|
|
+ }
|
|
|
+
|
|
|
+ public function processOfflineReports($reports)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ if (!is_array($reports) || empty($reports)) {
|
|
|
+ session()->flash('warning', 'Nessun record da sincronizzare');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach ($reports as $reportData) {
|
|
|
+ \App\Models\Report::create($reportData);
|
|
|
+ }
|
|
|
+
|
|
|
+ session()->flash('success', count($reports) . ' record(i) sincronizzati con successo');
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('Sync error: ' . $e->getMessage());
|
|
|
+ session()->flash('error', 'Errore durante la sincronizzazione');
|
|
|
+ }
|
|
|
+ }
|
|
|
public function store()
|
|
|
{
|
|
|
$this->validate();
|