cleanup Methode und Manager für CsvImportReport
[kivitendo-erp.git] / SL / DB / CsvImportReport.pm
1 # This file has been auto-generated only because it didn't exist.
2 # Feel free to modify it at will; it will not be overwritten automatically.
3
4 package SL::DB::CsvImportReport;
5
6 use strict;
7 use SL::DBUtils;
8
9 use SL::DB::MetaSetup::CsvImportReport;
10 use SL::DB::Manager::CsvImportReport;
11
12 __PACKAGE__->meta->add_relationships(
13   rows => {
14     type         => 'one to many',
15     class        => 'SL::DB::CsvImportReportRow',
16     column_map   => { id => 'csv_import_report_id' },
17   },
18   status => {
19     type         => 'one to many',
20     class        => 'SL::DB::CsvImportReportStatus',
21     column_map   => { id => 'csv_import_report_id' },
22   },
23 );
24
25 __PACKAGE__->meta->initialize;
26
27 sub folded_rows {
28   my ($self) = @_;
29
30   $self->_fold_rows unless $self->{folded_rows};
31
32   return $self->{folded_rows};
33 }
34
35 sub folded_status {
36   my ($self) = @_;
37
38   $self->_fold_status unless $self->{folded_status};
39
40   return $self->{folded_status};
41 }
42
43 sub _fold_rows {
44   my ($self) = @_;
45
46   $self->{folded_rows} = [];
47
48   for my $row_obj (@{ $self->rows }) {
49     $self->{folded_rows}->[ $row_obj->row ] ||= [];
50     $self->{folded_rows}->[ $row_obj->row ][ $row_obj->col ] = $row_obj->value;
51   }
52 }
53
54 sub _fold_status {
55   my ($self) = @_;
56
57   $self->{folded_status} = [];
58
59   for my $status_obj (@{ $self->status }) {
60     $self->{folded_status}->[ $status_obj->row ] ||= {};
61     $self->{folded_status}->[ $status_obj->row ]{information} ||= [];
62     $self->{folded_status}->[ $status_obj->row ]{errors} ||= [];
63     push @{ $self->{folded_status}->[ $status_obj->row ]{ $status_obj->type } }, $status_obj->value;
64   }
65 }
66
67 # implementes cascade delete as per documentation
68 sub destroy {
69   my ($self) = @_;
70
71   my $dbh = $self->db->dbh;
72
73   $dbh->begin_work;
74
75   do_query($::form, $dbh, 'DELETE FROM csv_import_report_status WHERE csv_import_report_id = ?', $self->id);
76   do_query($::form, $dbh, 'DELETE FROM csv_import_report_rows WHERE csv_import_report_id = ?', $self->id);
77   do_query($::form, $dbh, 'DELETE FROM csv_import_reports WHERE id = ?', $self->id);
78
79   if ($self->profile_id) {
80     do_query($::form, $dbh, 'DELETE FROM csv_import_profile_settings WHERE csv_import_profile_id = ?', $self->profile_id);
81     do_query($::form, $dbh, 'DELETE FROM csv_import_profiles WHERE id = ?', $self->profile_id);
82   }
83
84   $dbh->commit;
85 }
86
87 1;