2020namespace FacturaScripts \Plugins \AsientosPredefinidos \Extension \Controller ;
2121
2222use Closure ;
23+ use FacturaScripts \Core \Lib \Import \CSVImport ;
24+ use FacturaScripts \Core \Tools ;
25+ use FacturaScripts \Plugins \AsientosPredefinidos \Model \AsientoPredefinido ;
26+ use FacturaScripts \Plugins \AsientosPredefinidos \Model \AsientoPredefinidoLinea ;
27+ use FacturaScripts \Plugins \AsientosPredefinidos \Model \AsientoPredefinidoVariable ;
2328
2429class ListAsiento
2530{
@@ -41,6 +46,102 @@ protected function createViewsAsientosPredefinidos(): Closure
4146 ->addOrderBy (['id ' ], 'code ' , 1 )
4247 ->addOrderBy (['descripcion ' ], 'description ' )
4348 ->addSearchFields (['id ' , 'concepto ' , 'descripcion ' ]);
49+
50+ if ($ this ->user ->admin ) {
51+ $ this ->tab ($ viewName )->addButton ([
52+ 'action ' => 'restore-predefined ' ,
53+ 'color ' => 'warning ' ,
54+ 'confirm ' => true ,
55+ 'icon ' => 'fa-solid fa-trash-restore ' ,
56+ 'label ' => 'restore '
57+ ]);
58+ }
59+ };
60+ }
61+
62+ public function execPreviousAction (): Closure
63+ {
64+ return function ($ action ) {
65+ if ($ action !== 'restore-predefined ' ) {
66+ return ;
67+ }
68+
69+ if (false === $ this ->user ->admin || false === $ this ->permissions ->allowUpdate ) {
70+ Tools::log ()->warning ('not-allowed-modify ' );
71+ return ;
72+ } elseif (false === $ this ->validateFormToken ()) {
73+ return ;
74+ }
75+
76+ $ codpais = Tools::settings ('default ' , 'codpais ' , 'ESP ' );
77+ $ basePath = dirname (__DIR__ , 2 ) . '/Data/Codpais/ ' . $ codpais ;
78+ if (false === is_dir ($ basePath )) {
79+ $ basePath = dirname (__DIR__ , 2 ) . '/Data/Codpais/ESP ' ;
80+ }
81+
82+ $ preFile = $ basePath . '/asientospre.csv ' ;
83+ $ lineFile = $ basePath . '/asientospre_lineas.csv ' ;
84+ $ varFile = $ basePath . '/asientospre_variables.csv ' ;
85+ foreach ([$ preFile , $ lineFile , $ varFile ] as $ filePath ) {
86+ if (false === file_exists ($ filePath )) {
87+ Tools::log ()->warning ('file-not-found ' );
88+ return ;
89+ }
90+ }
91+
92+ $ templateIds = [];
93+ $ handle = fopen ($ preFile , 'rb ' );
94+ if (false === $ handle ) {
95+ Tools::log ()->error ('record-save-error ' );
96+ return ;
97+ }
98+
99+ $ headers = fgetcsv ($ handle );
100+ $ idColumn = false === is_array ($ headers ) ? false : array_search ('id ' , $ headers , true );
101+ while (false !== $ idColumn && ($ row = fgetcsv ($ handle )) !== false ) {
102+ if (empty ($ row [$ idColumn ])) {
103+ continue ;
104+ }
105+
106+ $ templateIds [] = (int )$ row [$ idColumn ];
107+ }
108+ fclose ($ handle );
109+
110+ $ templateIds = array_values (array_unique (array_filter ($ templateIds )));
111+ if (empty ($ templateIds )) {
112+ Tools::log ()->error ('record-save-error ' );
113+ return ;
114+ }
115+
116+ $ in = implode (', ' , array_map ('intval ' , $ templateIds ));
117+ $ mainSql = CSVImport::importFileSQL (AsientoPredefinido::tableName (), $ preFile , true );
118+ $ lineSql = CSVImport::importFileSQL (AsientoPredefinidoLinea::tableName (), $ lineFile );
119+ $ varSql = CSVImport::importFileSQL (AsientoPredefinidoVariable::tableName (), $ varFile );
120+ if (empty ($ mainSql ) || empty ($ lineSql ) || empty ($ varSql )) {
121+ Tools::log ()->error ('record-save-error ' );
122+ return ;
123+ }
124+
125+ $ this ->dataBase ->beginTransaction ();
126+
127+ if (
128+ false === $ this ->dataBase ->exec ($ mainSql )
129+ || false === $ this ->dataBase ->exec (
130+ 'DELETE FROM ' . AsientoPredefinidoLinea::tableName () . ' WHERE idasientopre IN ( ' . $ in . '); '
131+ )
132+ || false === $ this ->dataBase ->exec (
133+ 'DELETE FROM ' . AsientoPredefinidoVariable::tableName () . ' WHERE idasientopre IN ( ' . $ in . '); '
134+ )
135+ || false === $ this ->dataBase ->exec ($ lineSql )
136+ || false === $ this ->dataBase ->exec ($ varSql )
137+ ) {
138+ $ this ->dataBase ->rollback ();
139+ Tools::log ()->error ('record-save-error ' );
140+ return ;
141+ }
142+
143+ $ this ->dataBase ->commit ();
144+ Tools::log ()->notice ('record-updated-correctly ' );
44145 };
45146 }
46147}
0 commit comments