33514c4a7e0b106e2ea6b672efd3e0186837e1b0
[kivitendo-erp.git] / SL / Controller / DownloadZip.pm
1 package SL::Controller::DownloadZip;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use List::Util qw(first max);
8
9 use utf8;
10 use Encode qw(decode encode);
11 use Archive::Zip;
12 use SL::File;
13 use SL::SessionFile::Random;
14
15 sub action_download_orderitems_files {
16   my ($self) = @_;
17
18   #
19   # special case for customer which want to have not all
20   # in kivitendo.conf some regex may be defined:
21   # For no values just let it commented out
22   # PA = Produktionsauftrag, L = Lieferschein, ML = Materialliste
23   # If you want several options, please separate the letter with '|'. Example: '^(PA|L).*'
24   #set_sales_documenttype_for_delivered_quantity = '^(LS).*'
25   #set_purchase_documenttype_for_delivered_quantity = '^(EL).*'
26   #
27   # enbale this perl code:
28   #  my $doctype = $::lx_office_conf{system}->{"set_documenttype_for_part_zip_download"};
29   #  if ( $doctype ) {
30   #    # eliminate first and last char (are quotes)
31   #    $doctype =~ s/^.//;
32   #    $doctype =~ s/.$//;
33   #  }
34
35   #$Archive::Zip::UNICODE = 1;
36
37   my $object_id    = $::form->{object_id};
38   my $object_type  = $::form->{object_type};
39   my $element_type = $::form->{element_type};
40   my $sfile = SL::SessionFile::Random->new(mode => "w");
41   my $zip = Archive::Zip->new();
42   #TODO Check client encoding !!
43   #my $name_encoding = 'cp850';
44   my $name_encoding = 'UTF-8';
45
46   if (   $object_id
47       && ($object_type =~ m{^(?:sales_order|purchase_order|sales_quotation|request_quotation)$})
48       && ($element_type eq 'part')) {
49     my $orderitems = SL::DB::Manager::OrderItem->get_all(query => ['order.id' => $object_id ],
50                                                          with_objects => [ 'order', 'part' ],
51                                                          sort_by => 'part.partnumber ASC');
52     foreach my $item ( @{$orderitems} ) {
53       my @files = SL::File->get_all(object_id   => $item->parts_id,
54                                     object_type => $element_type,
55                                   );
56       next unless @files;
57
58       $zip->addDirectory($item->part->partnumber);
59       $zip->addFile($_->get_file, Encode::encode($name_encoding, $item->part->partnumber . '/' . $_->db_file->file_name)) for @files;
60     }
61   }
62   unless ( $zip->writeToFileNamed($sfile->file_name) == Archive::Zip::AZ_OK ) {
63     die 'zipfile write error';
64   }
65   $sfile->fh->close;
66
67   return $self->send_file(
68     $sfile->file_name,
69     type => 'application/zip',
70     name => $::form->{zipname}.'.zip',
71   );
72 }
73
74 1;
75
76 __END__
77
78 =pod
79
80 =encoding utf-8
81
82 =head1 NAME
83
84 SL::Controller::DownloadZip - controller for download all files from parts of an order in one zip file
85
86 =head2  C<action_download_zip FORMPARAMS>
87
88 Some customer want all attached files for the parts of an sales order or sales delivery order in one zip to download.
89 This is a special method for one customer, so it is moved into an extra controller.
90 The $Archive::Zip::UNICODE = 1; doesnt work ok
91 So today the filenames in cp850/DOS format for legacy windows.
92 To ues it for Linux Clients an additinal effort must be done,
93 for ex. a link to the same file with an utf-8 name.
94
95 There is also a special javascript method necessary which calles this controller method.
96 THis method must be inserted into the customer branch:
97
98 =begin text
99
100   ns.downloadOrderitemsAtt = function(type,id) {
101     var rowcount  = $('input[name=rowcount]').val() - 1;
102     var data = {
103         action:     'FileManagement/download_zip',
104         type:       type,
105         object_id:  id,
106         rowcount:   rowcount
107     };
108     if ( rowcount == 0 ) {
109         kivi.display_flash('error', kivi.t8('No articles have been added yet.'));
110         return false;
111     }
112     for (var i = 1; i <= rowcount; i++) {
113         data['parts_id_'+i] =  $('#id_' + i).val();
114     };
115     $.download("controller.pl", data);
116     return false;
117   }
118
119 =end text
120
121 See also L<SL::Controller::FileManagement>
122
123 =head1 DISCUSSION
124
125 Is this method needed in the master branch ?
126
127 =head1 AUTHOR
128
129 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
130
131 =cut