b3cdf3bf348f6fdd57696df0e2eda65e1c7afd40
[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, %params) = @_;
29
30   my $folded_rows = {};
31
32   for my $row_obj (@{ $params{rows} || $self->rows }) {
33     $folded_rows->{ $row_obj->row } ||= [];
34     $folded_rows->{ $row_obj->row }[ $row_obj->col ] = $row_obj->value;
35   }
36
37   $folded_rows;
38 }
39
40 sub folded_status {
41   my ($self, %params) = @_;
42
43   my $folded_status = {};
44
45   for my $status_obj (@{ $params{status} || $self->status }) {
46     $folded_status->{ $status_obj->row } ||= {};
47     $folded_status->{ $status_obj->row }{information} ||= [];
48     $folded_status->{ $status_obj->row }{errors} ||= [];
49     push @{ $folded_status->{ $status_obj->row }{ $status_obj->type } }, $status_obj->value;
50   }
51
52   $folded_status;
53 }
54
55 # implementes cascade delete as per documentation
56 sub destroy {
57   my ($self) = @_;
58
59   my $dbh = $self->db->dbh;
60
61   $dbh->begin_work;
62
63   do_query($::form, $dbh, 'DELETE FROM csv_import_report_status WHERE csv_import_report_id = ?', $self->id);
64   do_query($::form, $dbh, 'DELETE FROM csv_import_report_rows WHERE csv_import_report_id = ?', $self->id);
65   do_query($::form, $dbh, 'DELETE FROM csv_import_reports WHERE id = ?', $self->id);
66
67   if ($self->profile_id) {
68     do_query($::form, $dbh, 'DELETE FROM csv_import_profile_settings WHERE csv_import_profile_id = ?', $self->profile_id);
69     do_query($::form, $dbh, 'DELETE FROM csv_import_profiles WHERE id = ?', $self->profile_id);
70   }
71
72   $dbh->commit;
73 }
74
75 1;