5249410c8c41d5df4d592c6d5ef88a03ac6b6cb0
[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::DB;
8 use SL::DBUtils;
9
10 use SL::DB::MetaSetup::CsvImportReport;
11 use SL::DB::Manager::CsvImportReport;
12
13 __PACKAGE__->meta->add_relationships(
14   rows => {
15     type         => 'one to many',
16     class        => 'SL::DB::CsvImportReportRow',
17     column_map   => { id => 'csv_import_report_id' },
18   },
19   status => {
20     type         => 'one to many',
21     class        => 'SL::DB::CsvImportReportStatus',
22     column_map   => { id => 'csv_import_report_id' },
23   },
24 );
25
26 __PACKAGE__->meta->initialize;
27
28 sub folded_rows {
29   my ($self, %params) = @_;
30
31   my $folded_rows = {};
32
33   for my $row_obj (@{ $params{rows} || $self->rows }) {
34     $folded_rows->{ $row_obj->row } ||= [];
35     $folded_rows->{ $row_obj->row }[ $row_obj->col ] = $row_obj->value;
36   }
37
38   $folded_rows;
39 }
40
41 sub folded_status {
42   my ($self, %params) = @_;
43
44   my $folded_status = {};
45
46   for my $status_obj (@{ $params{status} || $self->status }) {
47     $folded_status->{ $status_obj->row } ||= {};
48     $folded_status->{ $status_obj->row }{information} ||= [];
49     $folded_status->{ $status_obj->row }{errors} ||= [];
50     push @{ $folded_status->{ $status_obj->row }{ $status_obj->type } }, $status_obj->value;
51   }
52
53   $folded_status;
54 }
55
56 # implementes cascade delete as per documentation
57 sub destroy {
58   my ($self) = @_;
59
60   SL::DB->client->with_transaction(sub {
61     my $dbh = SL::DB->client->dbh;
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       my ($is_profile_used_elsewhere) = selectfirst_array_query($::form, $dbh, <<SQL, $self->profile_id);
69         SELECT id
70         FROM csv_import_reports
71         WHERE profile_id = ?
72         LIMIT 1
73 SQL
74
75       if (!$is_profile_used_elsewhere) {
76         do_query($::form, $dbh, 'DELETE FROM csv_import_profile_settings WHERE csv_import_profile_id = ?', $self->profile_id);
77         do_query($::form, $dbh, 'DELETE FROM csv_import_profiles WHERE id = ?', $self->profile_id);
78       }
79     }
80     1;
81   }) or do { die SL::DB->client->error };
82 }
83
84 1;