Skip to content

Commit 7f4b581

Browse files
committed
fix: generazione stampe contabili definitive
1 parent 852bd76 commit 7f4b581

4 files changed

Lines changed: 94 additions & 15 deletions

File tree

modules/stampe_contabili/actions.php

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,67 @@
2323
switch (filter('op')) {
2424
case 'crea_definitiva':
2525
$year = date('Y', strtotime(post('date_start')));
26+
$id_print = post('id_print');
27+
$id_sezionale = post('id_sezionale');
28+
$dir = post('dir');
29+
$date_start = post('date_start');
30+
$date_end = post('date_end');
2631

27-
$first_page = $dbo->fetchOne('SELECT MAX(last_page) AS last_page FROM co_stampecontabili WHERE `id_print`='.prepare(post('id_print')).' AND `id_sezionale`='.prepare(post('id_sezionale')).' AND YEAR(`date_end`)='.prepare($year).' AND `dir`='.prepare(post('dir')))['last_page'] + 1;
32+
$where_conditions = [
33+
'`id_print`='.prepare($id_print),
34+
'YEAR(`date_end`)='.prepare($year)
35+
];
2836

29-
$print = Prints::render(post('id_print'), null, null, true, true, ['reset' => $first_page - 1, 'suppress' => 0]);
37+
if (!empty($id_sezionale)) {
38+
$where_conditions[] = '`id_sezionale`='.prepare($id_sezionale);
39+
} else {
40+
$where_conditions[] = '(`id_sezionale` IS NULL OR `id_sezionale` = "")';
41+
}
42+
43+
if (!empty($dir)) {
44+
$where_conditions[] = '`dir`='.prepare($dir);
45+
} else {
46+
$where_conditions[] = '(`dir` IS NULL OR `dir` = "")';
47+
}
48+
49+
$where_clause = implode(' AND ', $where_conditions);
50+
$first_page = $dbo->fetchOne('SELECT MAX(last_page) AS last_page FROM co_stampecontabili WHERE '.$where_clause)['last_page'] + 1;
51+
52+
$print = Prints::render($id_print, null, null, true, true, ['reset' => $first_page - 1, 'suppress' => 0]);
3053
$pages = count($print['pages']);
3154
$last_page = $first_page + $pages - 1;
3255

33-
$result = $dbo->table('co_stampecontabili')->insertGetId([
34-
'id_print' => post('id_print'),
35-
'id_sezionale' => post('id_sezionale') ?: '',
36-
'date_start' => post('date_start'),
37-
'date_end' => post('date_end'),
56+
$insert_data = [
57+
'id_print' => $id_print,
58+
'date_start' => $date_start,
59+
'date_end' => $date_end,
3860
'first_page' => $first_page,
3961
'last_page' => $last_page,
40-
'dir' => post('dir'),
41-
]);
62+
];
63+
64+
if (!empty($id_sezionale)) {
65+
$insert_data['id_sezionale'] = $id_sezionale;
66+
}
67+
68+
if (!empty($dir)) {
69+
$insert_data['dir'] = $dir;
70+
}
71+
72+
$result = $dbo->table('co_stampecontabili')->insertGetId($insert_data);
73+
74+
$print = Prints::render($id_print, null, null, true, true, ['reset' => $first_page - 1, 'suppress' => 0]);
75+
76+
$print_name = $print['name'];
77+
$print_name = str_replace('.pdf', '', $print_name);
78+
$date_formatted = date('Y-m-d', strtotime($date_start));
79+
80+
if (!empty($dir)) {
81+
$dir_text = ($dir == 'entrata') ? 'vendite' : 'acquisti';
82+
$name = $print_name.'_'.$dir_text.'_del_'.$date_formatted;
83+
} else {
84+
$name = $print_name.'_del_'.$date_formatted;
85+
}
4286

43-
$print = Prints::render(post('id_print'), null, null, true, true, ['reset' => $first_page - 1, 'suppress' => 0]);
44-
$name = 'Registro_iva_'.(post('dir') == 'entrata' ? 'vendite' : 'acquisti').'_del_'.post('date_start');
4587
$upload = Uploads::upload($print['pdf'], [
4688
'name' => $name,
4789
'original_name' => $name.'.pdf',

modules/stampe_contabili/ajax/complete.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,26 @@
2828
$id_sezionale = get('id_sezionale');
2929
$dir = get('dir');
3030

31-
$stampa_definitiva = $dbo->fetchOne('SELECT id FROM co_stampecontabili WHERE id_print='.prepare($id_print).' AND id_sezionale='.prepare($id_sezionale).' AND dir='.prepare($dir).' AND date_start='.prepare($date_start).' AND date_end='.prepare($date_end))['id'];
31+
$where_conditions = [
32+
'id_print='.prepare($id_print),
33+
'date_start='.prepare($date_start),
34+
'date_end='.prepare($date_end)
35+
];
36+
37+
if (!empty($id_sezionale)) {
38+
$where_conditions[] = 'id_sezionale='.prepare($id_sezionale);
39+
} else {
40+
$where_conditions[] = '(id_sezionale IS NULL OR id_sezionale = "")';
41+
}
42+
43+
if (!empty($dir)) {
44+
$where_conditions[] = 'dir='.prepare($dir);
45+
} else {
46+
$where_conditions[] = '(dir IS NULL OR dir = "")';
47+
}
48+
49+
$where_clause = implode(' AND ', $where_conditions);
50+
$stampa_definitiva = $dbo->fetchOne('SELECT id FROM co_stampecontabili WHERE '.$where_clause)['id'];
3251

3352
echo json_encode($stampa_definitiva ?: 0);
3453

modules/stampe_contabili/stampe_contabili.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,19 @@
146146
</div>
147147
</form>';
148148

149-
$elementi = $dbo->fetchArray('SELECT * FROM co_stampecontabili WHERE date_end BETWEEN '.prepare($_SESSION['period_start']).' AND '.prepare($_SESSION['period_end']).' AND id_print='.prepare($id_print).' AND dir='.prepare($dir));
149+
$where_conditions = [
150+
'date_end BETWEEN '.prepare($_SESSION['period_start']).' AND '.prepare($_SESSION['period_end']),
151+
'id_print='.prepare($id_print)
152+
];
153+
154+
if (!empty($dir)) {
155+
$where_conditions[] = 'dir='.prepare($dir);
156+
} else {
157+
$where_conditions[] = '(dir IS NULL OR dir = "")';
158+
}
159+
160+
$where_clause = implode(' AND ', $where_conditions);
161+
$elementi = $dbo->fetchArray('SELECT * FROM co_stampecontabili WHERE '.$where_clause);
150162
echo '
151163
<div class="card card-info card-outline mt-3 collapsed-card">
152164
<div class="card-header">
@@ -181,7 +193,7 @@
181193
foreach ($elementi as $elemento) {
182194
$sezionale_stampa = $dbo->fetchOne('SELECT `zz_segments_lang`.`title` FROM `zz_segments` LEFT JOIN `zz_segments_lang` ON (`zz_segments`.`id` = `zz_segments_lang`.`id_record` AND `zz_segments_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).') WHERE `zz_segments`.`id` = '.$elemento['id_sezionale'])['title'];
183195

184-
$file = $dbo->selectOne('zz_files', '*', [
196+
$file = $dbo->selectOne('zz_files', ['*'], [
185197
'id_module' => $id_module,
186198
'id_record' => $elemento['id'],
187199
]);

update/2_10.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,10 @@ ORDER BY
225225
UPDATE `zz_modules` SET `options` = 'SELECT |select| FROM `co_movimenti_modelli` WHERE 1=1 GROUP BY `idmastrino` HAVING 2=2 ORDER BY `co_movimenti_modelli`.`nome`' WHERE `zz_modules`.`name` = 'Modelli prima nota';
226226

227227
-- Ordinamento vista IVA
228-
UPDATE `zz_modules` SET `options` = '\nSELECT\n |select|\nFROM\n `co_iva`\n LEFT JOIN `co_iva_lang` ON (`co_iva`.`id` = `co_iva_lang`.`id_record` AND |lang|)\nWHERE\n 1=1 AND `deleted_at` IS NULL\nHAVING\n 2=2 \nORDER BY\n `co_iva_lang`.`title`' WHERE `zz_modules`.`name` = 'Iva';
228+
UPDATE `zz_modules` SET `options` = '\nSELECT\n |select|\nFROM\n `co_iva`\n LEFT JOIN `co_iva_lang` ON (`co_iva`.`id` = `co_iva_lang`.`id_record` AND |lang|)\nWHERE\n 1=1 AND `deleted_at` IS NULL\nHAVING\n 2=2 \nORDER BY\n `co_iva_lang`.`title`' WHERE `zz_modules`.`name` = 'Iva';
229+
230+
-- Fix per stampe contabili
231+
ALTER TABLE `co_stampecontabili` CHANGE `dir` `dir` VARCHAR(255) NULL;
232+
ALTER TABLE `co_stampecontabili` CHANGE `id_sezionale` `id_sezionale` INT NULL;
233+
UPDATE `co_stampecontabili` SET `dir` = NULL WHERE `dir` = '';
234+
UPDATE `co_stampecontabili` SET `id_sezionale` = NULL WHERE `id_sezionale` = 0;

0 commit comments

Comments
 (0)