@@ -400,6 +400,33 @@ public function getSubtotaleAttribute()
400400 return $ this ->imponibile ;
401401 }
402402
403+ /**
404+ * Imposta l'identificatore del conto solo se la colonna esiste.
405+ *
406+ * @param int $value
407+ */
408+ public function setIdContoAttribute ($ value )
409+ {
410+ if ($ this ->hasColumn ('idconto ' )) {
411+ $ this ->attributes ['idconto ' ] = $ value ;
412+ }
413+ }
414+
415+ /**
416+ * Restituisce l'identificatore del conto se la colonna esiste.
417+ *
418+ * @return int|null
419+ */
420+ public function getIdContoAttribute ()
421+ {
422+ if ($ this ->hasColumn ('idconto ' )) {
423+ return $ this ->attributes ['idconto ' ] ?? null ;
424+ }
425+ return null ;
426+ }
427+
428+
429+
403430 /**
404431 * Salva la riga, impostando i campi dipendenti dai singoli parametri.
405432 *
@@ -413,6 +440,7 @@ public function save(array $options = [])
413440 $ this ->fixProvvigione ();
414441
415442 $ this ->fixIva ();
443+ $ this ->fixIdConto ();
416444
417445 return parent ::save ($ options );
418446 }
@@ -448,6 +476,78 @@ protected function fixIvaIndetraibile()
448476 $ this ->attributes ['iva_indetraibile ' ] = $ this ->iva_indetraibile ;
449477 }
450478
479+ /**
480+ * Imposta il campo idconto solo se esiste nella tabella.
481+ */
482+ protected function fixIdConto ()
483+ {
484+ // Verifica se la colonna idconto esiste nella tabella
485+ if ($ this ->hasColumn ('idconto ' )) {
486+ // Se il campo idconto non è già impostato, prova a copiarlo dal documento padre
487+ if (empty ($ this ->attributes ['idconto ' ]) && !empty ($ this ->getDocument ()->idconto )) {
488+ $ this ->attributes ['idconto ' ] = $ this ->getDocument ()->idconto ;
489+ }
490+ }
491+ }
492+
493+ /**
494+ * Verifica se una colonna esiste nella tabella del modello.
495+ *
496+ * @param string $column
497+ * @return bool
498+ */
499+ protected function hasColumn ($ column )
500+ {
501+ static $ columns = [];
502+
503+ $ table = $ this ->getTable ();
504+
505+ if (!isset ($ columns [$ table ])) {
506+ try {
507+ // Usa una query diretta per verificare l'esistenza della colonna
508+ $ result = database ()->fetchArray ("SHOW COLUMNS FROM ` {$ table }` LIKE ' {$ column }' " );
509+ $ columns [$ table ][$ column ] = !empty ($ result );
510+ } catch (\Exception $ e ) {
511+ $ columns [$ table ][$ column ] = false ;
512+ }
513+ }
514+
515+ return isset ($ columns [$ table ][$ column ]) ? $ columns [$ table ][$ column ] : false ;
516+ }
517+
518+ /**
519+ * Gestisce l'impostazione di proprietà con nomi alternativi.
520+ *
521+ * @param string $key
522+ * @param mixed $value
523+ */
524+ public function __set ($ key , $ value )
525+ {
526+ // Gestisce id_conto come alias per idconto
527+ if ($ key === 'id_conto ' ) {
528+ $ this ->setIdContoAttribute ($ value );
529+ return ;
530+ }
531+
532+ parent ::__set ($ key , $ value );
533+ }
534+
535+ /**
536+ * Gestisce l'accesso a proprietà con nomi alternativi.
537+ *
538+ * @param string $key
539+ * @return mixed
540+ */
541+ public function __get ($ key )
542+ {
543+ // Gestisce id_conto come alias per idconto
544+ if ($ key === 'id_conto ' ) {
545+ return $ this ->getIdContoAttribute ();
546+ }
547+
548+ return parent ::__get ($ key );
549+ }
550+
451551 /**
452552 * Effettua i conti per lo sconto totale.
453553 */
0 commit comments