Beim Export der Kunden-/Lieferantenstammdatenberichte auch die Suchfelder mit übergeb...
[kivitendo-erp.git] / bin / mozilla / ct.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors: Reed White <alta@alta-research.com>
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #======================================================================
30 #
31 # customer/vendor module
32 #
33 #======================================================================
34
35 # $locale->text('Customers')
36 # $locale->text('Vendors')
37 # $locale->text('Add Customer')
38 # $locale->text('Add Vendor')
39 # $locale->text('Edit Customer')
40 # $locale->text('Edit Vendor')
41 # $locale->text('Customer saved!')
42 # $locale->text('Vendor saved!')
43 # $locale->text('Customer deleted!')
44 # $locale->text('Cannot delete customer!')
45 # $locale->text('Vendor deleted!')
46 # $locale->text('Cannot delete vendor!')
47
48 use CGI::Ajax;
49 use POSIX qw(strftime);
50
51 use SL::CT;
52 use SL::CVar;
53 use SL::ReportGenerator;
54
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/reportgenerator.pl";
57
58 1;
59
60 # end of main
61
62 sub add {
63   $lxdebug->enter_sub();
64
65   $auth->assert('customer_vendor_edit');
66
67   $form->{title}    = "Add";
68   $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
69
70   CT->populate_drop_down_boxes(\%myconfig, \%$form);
71
72   &form_header;
73   &form_footer;
74
75   $lxdebug->leave_sub();
76 }
77
78 sub search {
79   $lxdebug->enter_sub();
80
81   $auth->assert('customer_vendor_edit');
82
83   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
84
85   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
86   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
87
88   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
89   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
90    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
91                                                                            'include_prefix' => 'l_',
92                                                                            'include_value'  => 'Y');
93
94   $form->{jsscript} = 1;
95   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
96   $form->{fokus}    = 'Form.name';
97
98   $form->header();
99   print $form->parse_html_template('ct/search');
100
101   $lxdebug->leave_sub();
102 }
103
104 sub list_names {
105   $lxdebug->enter_sub();
106
107   $auth->assert('customer_vendor_edit');
108
109   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
110
111   report_generator_set_default_sort('name', 1);
112
113   CT->search(\%myconfig, \%$form);
114
115   my $cvar_configs = CVar->get_configs('module' => 'CT');
116
117   my @options;
118   if ($form->{status} eq 'all') {
119     push @options, $locale->text('All');
120   } elsif ($form->{status} eq 'orphaned') {
121     push @options, $locale->text('Orphaned');
122   }
123
124   push @options, $locale->text('Name') . " : $form->{name}"                    if $form->{name};
125   push @options, $locale->text('Contact') . " : $form->{contact}"              if $form->{contact};
126   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
127   push @options, $locale->text('E-mail') . " : $form->{email}"                 if $form->{email};
128
129   my @columns = (
130     'id',        'name',  "$form->{db}number", 'address',  'contact',  'phone',
131     'fax',       'email', 'taxnumber',         'sic_code', 'business', 'invnumber',
132     'ordnumber', 'quonumber'
133   );
134
135   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
136   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs };
137   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
138
139   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
140
141   my %column_defs = (
142     'id'                => { 'text' => $locale->text('ID'), },
143     "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
144     'name'              => { 'text' => $locale->text('Name'), },
145     'address'           => { 'text' => $locale->text('Address'), },
146     'contact'           => { 'text' => $locale->text('Contact'), },
147     'phone'             => { 'text' => $locale->text('Phone'), },
148     'fax'               => { 'text' => $locale->text('Fax'), },
149     'email'             => { 'text' => $locale->text('E-mail'), },
150     'cc'                => { 'text' => $locale->text('Cc'), },
151     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
152     'sic_code'          => { 'text' => $locale->text('SIC'), },
153     'business'          => { 'text' => $locale->text('Type of Business'), },
154     'invnumber'         => { 'text' => $locale->text('Invoice'), },
155     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
156     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
157     %column_defs_cvars,
158   );
159
160   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
161
162   my @hidden_variables  = (qw(db status obsolete name contact email cp_name addr_city), "$form->{db}number", @searchable_custom_variables, map { "l_$_" } @columns);
163   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
164   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
165   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
166
167   foreach (@columns) {
168     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
169     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
170   }
171
172   my ($ordertype, $quotationtype, $attachment_basename);
173   if ($form->{IS_CUSTOMER}) {
174     $form->{title}       = $locale->text('Customers');
175     $ordertype           = 'sales_order';
176     $quotationtype       = 'sales_quotation';
177     $attachment_basename = $locale->text('customer_list');
178
179   } else {
180     $form->{title}       = $locale->text('Vendors');
181     $ordertype           = 'purchase_order';
182     $quotationtype       = 'request_quotation';
183     $attachment_basename = $locale->text('vendor_list');
184   }
185
186   my $report = SL::ReportGenerator->new(\%myconfig, $form);
187
188   $report->set_options('top_info_text'         => join("\n", @options),
189                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
190                        'output_format'         => 'HTML',
191                        'title'                 => $form->{title},
192                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
193     );
194   $report->set_options_from_form();
195
196   $report->set_columns(%column_defs);
197   $report->set_column_order(@columns);
198
199   $report->set_export_options('list_names', @hidden_variables);
200
201   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
202
203   CVar->add_custom_variables_to_report('module'         => 'CT',
204                                        'trans_id_field' => 'id',
205                                        'configs'        => $cvar_configs,
206                                        'column_defs'    => \%column_defs,
207                                        'data'           => $form->{CT});
208
209   my $previous_id;
210
211   foreach my $ref (@{ $form->{CT} }) {
212     my $row = { map { $_ => { 'data' => '' } } @columns };
213
214     if ($ref->{id} ne $previous_id) {
215       $previous_id = $ref->{id};
216       map { $row->{$_}->{data} = $ref->{$_} } @columns;
217
218       $row->{name}->{link}  = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
219       $row->{email}->{link} = 'mailto:' . E($ref->{email});
220     }
221
222     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
223     $row->{invnumber}->{link} = $base_url;
224     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
225     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
226     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
227     $row->{$column}->{data}   = $ref->{$column};
228
229     $report->add_data($row);
230   }
231
232   $report->generate_with_headers();
233
234   $lxdebug->leave_sub();
235 }
236
237 sub edit {
238   $lxdebug->enter_sub();
239
240   $auth->assert('customer_vendor_edit');
241
242   # show history button
243   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
244   #/show hhistory button
245   
246   CT->get_tuple(\%myconfig, \%$form);
247   CT->populate_drop_down_boxes(\%myconfig, \%$form);
248
249   $form->{title} = "Edit";
250
251   # format discount
252   $form->{discount} *= 100;
253
254   &form_header;
255   &form_footer;
256
257   $lxdebug->leave_sub();
258 }
259
260 sub form_header {
261   $lxdebug->enter_sub();
262
263   $auth->assert('customer_vendor_edit');
264
265   $form->get_lists("employees" => "ALL_EMPLOYEES",
266                    "taxzones"  => "ALL_TAXZONES");
267   $form->get_pricegroup(\%myconfig, { all => 1 });
268
269   $form->{ALL_SALESMEN}   = $form->{ALL_EMPLOYEES};
270   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
271   $form->{is_admin}       = $myconfig{role} eq 'admin';
272   $form->{is_customer}    = $form->{db}     eq 'customer';
273   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
274   $form->{shipto_label}   = sub { my $s = shift(@_); join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' ' };
275   $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
276   $form->{taxzone_id}     = 0                                                               if !$form->{id};
277   $form->{jsscript}       = 1;
278   $form->{fokus}          = "ct.greeting";
279   $form->{AJAX}           = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
280
281   unshift @{ $form->{SHIPTO} },   +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
282   unshift @{ $form->{CONTACTS} }, +{ cp_id     => '0', cp_name => $locale->text('New contact') };
283
284   $form->{title} = $form->{title_save} 
285                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
286
287   CT->query_titles_and_greetings(\%myconfig, \%$form);
288   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
289
290   $form->{NOTES} ||= [ ];
291
292   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
293
294   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
295
296   $form->header;
297   print $form->parse_html_template('ct/form_header');
298
299   $lxdebug->leave_sub();
300 }
301
302 sub form_footer {
303   $lxdebug->enter_sub();
304
305   $auth->assert('customer_vendor_edit');
306
307   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
308                                                        is_customer => $form->{db}     eq 'customer' });
309   $lxdebug->leave_sub();
310 }
311
312 sub add_transaction {
313   $lxdebug->enter_sub();
314
315   $auth->assert('customer_vendor_edit & ' .
316                 '(general_ledger         | invoice_edit         | vendor_invoice_edit | ' .
317                 ' request_quotation_edit | sales_quotation_edit | sales_order_edit    | purchase_order_edit)');
318
319 #  # saving the history
320 #  if(!exists $form->{addition}) {
321 #       $form->{addition} = "ADD TRANSACTION";
322 #       $form->save_history($form->dbconnect(\%myconfig));
323 #  }
324 #  # /saving the history
325   
326   $form->isblank("name", $locale->text("Name missing!"));
327   if ($form->{"db"} eq "customer") {
328     CT->save_customer(\%myconfig, \%$form);
329   } else {
330     CT->save_vendor(\%myconfig, \%$form);
331   }
332
333   $form->{callback} = $form->escape($form->{callback}, 1);
334   $name = $form->escape("$form->{name}", 1);
335
336   $form->{callback} =
337     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
338   $form->redirect;
339
340   $lxdebug->leave_sub();
341 }
342
343 sub save_and_ap_transaction {
344   $lxdebug->enter_sub();
345
346   $auth->assert('customer_vendor_edit & general_ledger');
347
348   $form->{script} = "ap.pl";
349   # saving the history
350   if(!exists $form->{addition}) {
351     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
352         $form->{addition} = "SAVED";
353         $form->save_history($form->dbconnect(\%myconfig));
354   }
355   # /saving the history
356   &add_transaction;
357   $lxdebug->leave_sub();
358 }
359
360 sub save_and_ar_transaction {
361   $lxdebug->enter_sub();
362
363   $auth->assert('customer_vendor_edit & general_ledger');
364
365   $form->{script} = "ar.pl";
366   # saving the history
367   if(!exists $form->{addition}) {
368     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
369         $form->{addition} = "SAVED";
370         $form->save_history($form->dbconnect(\%myconfig));
371   }
372   # /saving the history
373   &add_transaction;
374   $lxdebug->leave_sub();
375 }
376
377 sub save_and_invoice {
378   $lxdebug->enter_sub();
379
380   if ($form->{db} eq 'customer') {
381     $auth->assert('customer_vendor_edit & invoice_edit');
382   } else {
383     $auth->assert('customer_vendor_edit & vendor_invoice_edit');
384   }
385
386   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
387   $form->{type} = "invoice";
388   # saving the history
389   if(!exists $form->{addition}) {
390     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
391         $form->{addition} = "SAVED";
392         $form->save_history($form->dbconnect(\%myconfig));
393   }
394   # /saving the history
395   &add_transaction;
396   $lxdebug->leave_sub();
397 }
398
399 sub save_and_rfq {
400   $lxdebug->enter_sub();
401
402   $auth->assert('customer_vendor_edit & request_quotation_edit');
403
404   $form->{script} = "oe.pl";
405   $form->{type}   = "request_quotation";
406   # saving the history
407   if(!exists $form->{addition}) {
408     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
409         $form->{addition} = "SAVED";
410         $form->save_history($form->dbconnect(\%myconfig));
411   }
412   # /saving the history
413   &add_transaction;
414   $lxdebug->leave_sub();
415 }
416
417 sub save_and_quotation {
418   $lxdebug->enter_sub();
419
420   $auth->assert('customer_vendor_edit & sales_quotation_edit');
421
422   $form->{script} = "oe.pl";
423   $form->{type}   = "sales_quotation";
424   # saving the history
425   if(!exists $form->{addition}) {
426     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
427         $form->{addition} = "SAVED";
428         $form->save_history($form->dbconnect(\%myconfig));
429   }
430   # /saving the history
431   &add_transaction;
432   $lxdebug->leave_sub();
433 }
434
435 sub save_and_order {
436   $lxdebug->enter_sub();
437
438   if ($form->{db} eq 'customer') {
439     $auth->assert('customer_vendor_edit & sales_order_edit');
440   } else {
441     $auth->assert('customer_vendor_edit & purchase_order_edit');
442   }
443
444   $form->{script} = "oe.pl";
445   $form->{type}   =
446     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
447   # saving the history
448   if(!exists $form->{addition}) {
449     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
450         $form->{addition} = "SAVED";
451         $form->save_history($form->dbconnect(\%myconfig));
452   }
453   # /saving the history
454   &add_transaction;
455   $lxdebug->leave_sub();
456 }
457
458 sub save_and_close {
459   $lxdebug->enter_sub();
460
461   $auth->assert('customer_vendor_edit');
462
463   $msg = ucfirst $form->{db};
464   $imsg .= " saved!";
465
466   $form->isblank("name", $locale->text("Name missing!"));
467   if ($form->{"db"} eq "customer") {
468     $rc = CT->save_customer(\%myconfig, \%$form);
469   } else {
470     $rc = CT->save_vendor(\%myconfig, \%$form);
471   }
472   if ($rc == 3) {
473     $form->error($locale->text('customernumber not unique!'));
474   }
475   # saving the history
476   if(!exists $form->{addition}) {
477     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
478     $form->{addition} = "SAVED";
479         $form->save_history($form->dbconnect(\%myconfig));
480   }
481   # /saving the history
482   $form->redirect($locale->text($msg));
483
484   $lxdebug->leave_sub();
485 }
486
487 sub save {
488   $lxdebug->enter_sub();
489
490   $auth->assert('customer_vendor_edit');
491
492   $msg = ucfirst $form->{db};
493   $imsg .= " saved!";
494
495   $form->isblank("name", $locale->text("Name missing!"));
496
497   my $res;
498   if ($form->{"db"} eq "customer") {
499     $res = CT->save_customer(\%myconfig, \%$form);
500   } else {
501     $res = CT->save_vendor(\%myconfig, \%$form);
502   }
503
504   if (3 == $res) {
505     if ($form->{"db"} eq "customer") {
506       $form->error($locale->text('This customer number is already in use.'));
507     } else {
508       $form->error($locale->text('This vendor number is already in use.'));
509     }
510   }
511   # saving the history
512   if(!exists $form->{addition}) {
513     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
514         $form->{addition} = "SAVED";
515         $form->save_history($form->dbconnect(\%myconfig));
516   }
517   # /saving the history
518   &edit;
519   exit;
520   $lxdebug->leave_sub();
521 }
522
523 sub delete {
524   $lxdebug->enter_sub();
525
526   $auth->assert('customer_vendor_edit');
527
528   CT->delete(\%myconfig, \%$form);
529
530   $msg = ucfirst $form->{db};
531   $msg .= " deleted!";
532   # saving the history
533   if(!exists $form->{addition}) {
534     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
535         $form->{addition} = "DELETED";
536         $form->save_history($form->dbconnect(\%myconfig));
537   }
538   # /saving the history 
539   $form->redirect($locale->text($msg));
540
541   $msg = "Cannot delete $form->{db}";
542   $form->error($locale->text($msg));
543
544   $lxdebug->leave_sub();
545 }
546
547 sub display {
548   $lxdebug->enter_sub();
549
550   $auth->assert('customer_vendor_edit');
551
552   &form_header();
553   &form_footer();
554
555   $lxdebug->leave_sub();
556 }
557
558 sub update {
559   $lxdebug->enter_sub();
560
561   $auth->assert('customer_vendor_edit');
562
563   &display();
564   $lxdebug->leave_sub();
565 }
566
567 sub get_contact {
568   $lxdebug->enter_sub();
569
570   $auth->assert('customer_vendor_edit');
571
572   CT->get_contact(\%myconfig, \%$form);
573   print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"}, 
574     qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
575   $lxdebug->leave_sub();
576
577 }
578
579 sub get_shipto {
580   $lxdebug->enter_sub();
581
582   $auth->assert('customer_vendor_edit');
583
584   CT->get_shipto(\%myconfig, \%$form);
585   print $form->ajax_response_header(),  join '__pjx__', map $form->{"shipto$_"},
586     qw(name department_1 department_2 street zipcode city country contact phone fax email used);
587   $lxdebug->leave_sub();
588
589 }
590
591 sub get_delivery {
592   $lxdebug->enter_sub();
593
594   $auth->assert('customer_vendor_edit');
595
596   CT->get_delivery(\%myconfig, \%$form );
597
598   print $form->ajax_response_header(), $form->parse_html_template('ct/get_delivery');
599
600   $lxdebug->leave_sub();
601 }
602
603 sub delete_shipto {
604   $main::lxdebug->enter_sub();
605
606   $auth->assert('customer_vendor_edit');
607
608   CT->get_shipto(\%myconfig, \%$form);
609
610   unless ($form->{shiptoused}) {
611     CT->delete_shipto($form->{shipto_id});
612     @$form{ grep /^shipto/, keys %$form } = undef;
613   }
614
615   edit();
616
617   $main::lxdebug->leave_sub();
618 }
619
620 sub delete_contact {
621   $main::lxdebug->enter_sub();
622
623   $auth->assert('customer_vendor_edit');
624
625   CT->get_contact(\%myconfig, \%$form);
626
627   unless ($form->{cp_used}) {
628     CT->delete_shipto($form->{cp_id});
629     @$form{ grep /^cp_/, keys %$form } = undef;
630   }
631
632   edit();
633
634   $main::lxdebug->leave_sub();
635 }
636
637 sub continue { call_sub($form->{nextsub}); }