Sortierung in Berichten über Kunden und Lieferanten auch auf- und absteigbar sortierb...
[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 %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
137
138   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
139
140   my %column_defs = (
141     'id'                => { 'text' => $locale->text('ID'), },
142     "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
143     'name'              => { 'text' => $locale->text('Name'), },
144     'address'           => { 'text' => $locale->text('Address'), },
145     'contact'           => { 'text' => $locale->text('Contact'), },
146     'phone'             => { 'text' => $locale->text('Phone'), },
147     'fax'               => { 'text' => $locale->text('Fax'), },
148     'email'             => { 'text' => $locale->text('E-mail'), },
149     'cc'                => { 'text' => $locale->text('Cc'), },
150     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
151     'sic_code'          => { 'text' => $locale->text('SIC'), },
152     'business'          => { 'text' => $locale->text('Type of Business'), },
153     'invnumber'         => { 'text' => $locale->text('Invoice'), },
154     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
155     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
156     %column_defs_cvars,
157   );
158
159   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
160
161   my @hidden_variables  = (qw(db status obsolete), map { "l_$_" } @columns);
162   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
163   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
164   $form->{callback}     = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
165
166   foreach (@columns) {
167     my $sortdir              = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
168     $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
169   }
170
171   my ($ordertype, $quotationtype, $attachment_basename);
172   if ($form->{IS_CUSTOMER}) {
173     $form->{title}       = $locale->text('Customers');
174     $ordertype           = 'sales_order';
175     $quotationtype       = 'sales_quotation';
176     $attachment_basename = $locale->text('customer_list');
177
178   } else {
179     $form->{title}       = $locale->text('Vendors');
180     $ordertype           = 'purchase_order';
181     $quotationtype       = 'request_quotation';
182     $attachment_basename = $locale->text('vendor_list');
183   }
184
185   my $report = SL::ReportGenerator->new(\%myconfig, $form);
186
187   $report->set_options('top_info_text'         => join("\n", @options),
188                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
189                        'output_format'         => 'HTML',
190                        'title'                 => $form->{title},
191                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
192     );
193   $report->set_options_from_form();
194
195   $report->set_columns(%column_defs);
196   $report->set_column_order(@columns);
197
198   $report->set_export_options('list_names', @hidden_variables);
199
200   $report->set_sort_indicator($form->{sort}, $form->{sortdir});
201
202   CVar->add_custom_variables_to_report('module'         => 'CT',
203                                        'trans_id_field' => 'id',
204                                        'configs'        => $cvar_configs,
205                                        'column_defs'    => \%column_defs,
206                                        'data'           => $form->{CT});
207
208   my $previous_id;
209
210   foreach my $ref (@{ $form->{CT} }) {
211     my $row = { map { $_ => { 'data' => '' } } @columns };
212
213     if ($ref->{id} ne $previous_id) {
214       $previous_id = $ref->{id};
215       map { $row->{$_}->{data} = $ref->{$_} } @columns;
216
217       $row->{name}->{link}  = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
218       $row->{email}->{link} = 'mailto:' . E($ref->{email});
219     }
220
221     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
222     $row->{invnumber}->{link} = $base_url;
223     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
224     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
225     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
226     $row->{$column}->{data}   = $ref->{$column};
227
228     $report->add_data($row);
229   }
230
231   $report->generate_with_headers();
232
233   $lxdebug->leave_sub();
234 }
235
236 sub edit {
237   $lxdebug->enter_sub();
238
239   $auth->assert('customer_vendor_edit');
240
241   # show history button
242   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
243   #/show hhistory button
244   
245   CT->get_tuple(\%myconfig, \%$form);
246   CT->populate_drop_down_boxes(\%myconfig, \%$form);
247
248   # format " into &quot;
249   map { $form->{$_} =~ s/\"/&quot;/g } keys %$form;
250
251   $form->{title} = "Edit";
252
253   # format discount
254   $form->{discount} *= 100;
255
256   &form_header;
257   &form_footer;
258
259   $lxdebug->leave_sub();
260 }
261
262 sub form_header {
263   $lxdebug->enter_sub();
264
265   $auth->assert('customer_vendor_edit');
266
267   $form->get_lists("employees" => "ALL_EMPLOYEES",
268                    "taxzones"  => "ALL_TAXZONES");
269   $form->get_pricegroup(\%myconfig, { all => 1 });
270
271   $form->{ALL_SALESMEN}   = $form->{ALL_EMPLOYEES};
272   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
273   $form->{is_admin}       = $myconfig{role} eq 'admin';
274   $form->{is_customer}    = $form->{db}     eq 'customer';
275   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
276   $form->{shipto_label}   = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
277   $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
278   $form->{taxzone_id}     = 0                                                               if !$form->{id};
279   $form->{jsscript}       = 1;
280   $form->{fokus}          = "ct.greeting";
281   $form->{AJAX}           = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
282
283   unshift @{ $form->{SHIPTO} },   +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
284   unshift @{ $form->{CONTACTS} }, +{ cp_id     => '0', cp_name => $locale->text('New contact') };
285
286   $form->{title} = $form->{title_save} 
287                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
288
289   CT->query_titles_and_greetings(\%myconfig, \%$form);
290   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
291
292   $form->{NOTES} ||= [ ];
293
294   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
295
296   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
297
298   $form->header;
299   print $form->parse_html_template('ct/form_header');
300
301   $lxdebug->leave_sub();
302 }
303
304 sub form_footer {
305   $lxdebug->enter_sub();
306
307   $auth->assert('customer_vendor_edit');
308
309   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
310                                                        is_customer => $form->{db}     eq 'customer' });
311   $lxdebug->leave_sub();
312 }
313
314 sub add_transaction {
315   $lxdebug->enter_sub();
316
317   $auth->assert('customer_vendor_edit & general_ledger');
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   $auth->assert('customer_vendor_edit & sales_order_edit');
439
440   $form->{script} = "oe.pl";
441   $form->{type}   =
442     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
443   # saving the history
444   if(!exists $form->{addition}) {
445     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
446         $form->{addition} = "SAVED";
447         $form->save_history($form->dbconnect(\%myconfig));
448   }
449   # /saving the history
450   &add_transaction;
451   $lxdebug->leave_sub();
452 }
453
454 sub save_and_close {
455   $lxdebug->enter_sub();
456
457   $auth->assert('customer_vendor_edit');
458
459   $msg = ucfirst $form->{db};
460   $imsg .= " saved!";
461
462   $form->isblank("name", $locale->text("Name missing!"));
463   if ($form->{"db"} eq "customer") {
464     $rc = CT->save_customer(\%myconfig, \%$form);
465   } else {
466     $rc = CT->save_vendor(\%myconfig, \%$form);
467   }
468   if ($rc == 3) {
469     $form->error($locale->text('customernumber not unique!'));
470   }
471   # saving the history
472   if(!exists $form->{addition}) {
473     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
474     $form->{addition} = "SAVED";
475         $form->save_history($form->dbconnect(\%myconfig));
476   }
477   # /saving the history
478   $form->redirect($locale->text($msg));
479
480   $lxdebug->leave_sub();
481 }
482
483 sub save {
484   $lxdebug->enter_sub();
485
486   $auth->assert('customer_vendor_edit');
487
488   $msg = ucfirst $form->{db};
489   $imsg .= " saved!";
490
491   $form->isblank("name", $locale->text("Name missing!"));
492
493   my $res;
494   if ($form->{"db"} eq "customer") {
495     $res = CT->save_customer(\%myconfig, \%$form);
496   } else {
497     $res = CT->save_vendor(\%myconfig, \%$form);
498   }
499
500   if (3 == $res) {
501     if ($form->{"db"} eq "customer") {
502       $form->error($locale->text('This customer number is already in use.'));
503     } else {
504       $form->error($locale->text('This vendor number is already in use.'));
505     }
506   }
507   # saving the history
508   if(!exists $form->{addition}) {
509     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
510         $form->{addition} = "SAVED";
511         $form->save_history($form->dbconnect(\%myconfig));
512   }
513   # /saving the history
514   &edit;
515   exit;
516   $lxdebug->leave_sub();
517 }
518
519 sub delete {
520   $lxdebug->enter_sub();
521
522   $auth->assert('customer_vendor_edit');
523
524   CT->delete(\%myconfig, \%$form);
525
526   $msg = ucfirst $form->{db};
527   $msg .= " deleted!";
528   # saving the history
529   if(!exists $form->{addition}) {
530     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
531         $form->{addition} = "DELETED";
532         $form->save_history($form->dbconnect(\%myconfig));
533   }
534   # /saving the history 
535   $form->redirect($locale->text($msg));
536
537   $msg = "Cannot delete $form->{db}";
538   $form->error($locale->text($msg));
539
540   $lxdebug->leave_sub();
541 }
542
543 sub display {
544   $lxdebug->enter_sub();
545
546   $auth->assert('customer_vendor_edit');
547
548   &form_header();
549   &form_footer();
550
551   $lxdebug->leave_sub();
552 }
553
554 sub update {
555   $lxdebug->enter_sub();
556
557   $auth->assert('customer_vendor_edit');
558
559   &display();
560   $lxdebug->leave_sub();
561 }
562
563 sub get_contact {
564   $lxdebug->enter_sub();
565
566   $auth->assert('customer_vendor_edit');
567
568   CT->get_contact(\%myconfig, \%$form);
569   print $cgi->header(), join '__pjx__', map $form->{"cp_$_"}, 
570     qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
571   $lxdebug->leave_sub();
572
573 }
574
575 sub get_shipto {
576   $lxdebug->enter_sub();
577
578   $auth->assert('customer_vendor_edit');
579
580   CT->get_shipto(\%myconfig, \%$form);
581   print $cgi->header(),  join '__pjx__', map $form->{"shipto$_"},
582     qw(name department_1 department_2 street zipcode city country contact phone fax email used);
583   $lxdebug->leave_sub();
584
585 }
586
587 sub get_delivery {
588   $lxdebug->enter_sub();
589
590   $auth->assert('customer_vendor_edit');
591
592   CT->get_delivery(\%myconfig, \%$form );
593
594   print $cgi->header(), $form->parse_html_template('ct/get_delivery');
595
596   $lxdebug->leave_sub();
597 }
598
599 sub delete_shipto {
600   $main::lxdebug->enter_sub();
601
602   $auth->assert('customer_vendor_edit');
603
604   CT->get_shipto(\%myconfig, \%$form);
605
606   unless ($form->{shiptoused}) {
607     CT->delete_shipto($form->{shipto_id});
608     @$form{ grep /^shipto/, keys %$form } = undef;
609   }
610
611   edit();
612
613   $main::lxdebug->leave_sub();
614 }
615
616 sub delete_contact {
617   $main::lxdebug->enter_sub();
618
619   $auth->assert('customer_vendor_edit');
620
621   CT->get_contact(\%myconfig, \%$form);
622
623   unless ($form->{cp_used}) {
624     CT->delete_shipto($form->{cp_id});
625     @$form{ grep /^cp_/, keys %$form } = undef;
626   }
627
628   edit();
629
630   $main::lxdebug->leave_sub();
631 }
632
633 sub continue { call_sub($form->{nextsub}); }