Testanwendung der CGI::Ajax Modifikation:
[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
69   $form->{callback} =
70     "$form->{script}?action=add&db=$form->{db}"
71     unless $form->{callback};
72
73   CT->populate_drop_down_boxes(\%myconfig, \%$form);
74
75   &form_header;
76   &form_footer;
77
78   $lxdebug->leave_sub();
79 }
80
81 sub search {
82   $lxdebug->enter_sub();
83
84   $auth->assert('customer_vendor_edit');
85
86   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
87
88   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
89   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
90
91   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
92   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
93    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
94                                                                            'include_prefix' => 'l_',
95                                                                            'include_value'  => 'Y');
96
97   $form->{jsscript} = 1;
98   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
99   $form->{fokus}    = 'Form.name';
100
101   $form->header();
102   print $form->parse_html_template('ct/search');
103
104   $lxdebug->leave_sub();
105 }
106
107 sub list_names {
108   $lxdebug->enter_sub();
109
110   $auth->assert('customer_vendor_edit');
111
112   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
113
114   CT->search(\%myconfig, \%$form);
115
116   my $cvar_configs = CVar->get_configs('module' => 'CT');
117
118   my @options;
119   if ($form->{status} eq 'all') {
120     push @options, $locale->text('All');
121   } elsif ($form->{status} eq 'orphaned') {
122     push @options, $locale->text('Orphaned');
123   }
124
125   push @options, $locale->text('Name') . " : $form->{name}"                    if $form->{name};
126   push @options, $locale->text('Contact') . " : $form->{contact}"              if $form->{contact};
127   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
128   push @options, $locale->text('E-mail') . " : $form->{email}"                 if $form->{email};
129
130   my @columns = (
131     'id',        'name',  "$form->{db}number", 'address',  'contact',  'phone',
132     'fax',       'email', 'taxnumber',         'sic_code', 'business', 'invnumber',
133     'ordnumber', 'quonumber'
134   );
135
136   my @includeable_custom_variables = grep { $_->{includeable} } @{ $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), map { "l_$_" } @columns);
163   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
164   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
165   $form->{callback}     = "$callback&sort=" . E($form->{sort});
166
167   map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
168
169   my ($ordertype, $quotationtype, $attachment_basename);
170   if ($form->{IS_CUSTOMER}) {
171     $form->{title}       = $locale->text('Customers');
172     $ordertype           = 'sales_order';
173     $quotationtype       = 'sales_quotation';
174     $attachment_basename = $locale->text('customer_list');
175
176   } else {
177     $form->{title}       = $locale->text('Vendors');
178     $ordertype           = 'purchase_order';
179     $quotationtype       = 'request_quotation';
180     $attachment_basename = $locale->text('vendor_list');
181   }
182
183   my $report = SL::ReportGenerator->new(\%myconfig, $form);
184
185   $report->set_options('top_info_text'         => join("\n", @options),
186                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
187                        'output_format'         => 'HTML',
188                        'title'                 => $form->{title},
189                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
190     );
191   $report->set_options_from_form();
192
193   $report->set_columns(%column_defs);
194   $report->set_column_order(@columns);
195
196   $report->set_export_options('list_names', @hidden_variables);
197
198   $report->set_sort_indicator($form->{sort}, 1);
199
200   CVar->add_custom_variables_to_report('module'         => 'CT',
201                                        'trans_id_field' => 'id',
202                                        'configs'        => $cvar_configs,
203                                        'column_defs'    => \%column_defs,
204                                        'data'           => $form->{CT});
205
206   my $previous_id;
207
208   foreach my $ref (@{ $form->{CT} }) {
209     my $row = { map { $_ => { 'data' => '' } } @columns };
210
211     if ($ref->{id} ne $previous_id) {
212       $previous_id = $ref->{id};
213       map { $row->{$_}->{data} = $ref->{$_} } @columns;
214
215       $row->{name}->{link}  = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
216       $row->{email}->{link} = 'mailto:' . E($ref->{email});
217     }
218
219     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
220     $row->{invnumber}->{link} = $base_url;
221     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
222     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
223     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
224     $row->{$column}->{data}   = $ref->{$column};
225
226     $report->add_data($row);
227   }
228
229   $report->generate_with_headers();
230
231   $lxdebug->leave_sub();
232 }
233
234 sub edit {
235   $lxdebug->enter_sub();
236
237   $auth->assert('customer_vendor_edit');
238
239   # show history button
240   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
241   #/show hhistory button
242   
243   CT->get_tuple(\%myconfig, \%$form);
244   CT->populate_drop_down_boxes(\%myconfig, \%$form);
245
246   # format " into &quot;
247   map { $form->{$_} =~ s/\"/&quot;/g } keys %$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 { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
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 & general_ledger');
316
317 #  # saving the history
318 #  if(!exists $form->{addition}) {
319 #       $form->{addition} = "ADD TRANSACTION";
320 #       $form->save_history($form->dbconnect(\%myconfig));
321 #  }
322 #  # /saving the history
323   
324   $form->isblank("name", $locale->text("Name missing!"));
325   if ($form->{"db"} eq "customer") {
326     CT->save_customer(\%myconfig, \%$form);
327   } else {
328     CT->save_vendor(\%myconfig, \%$form);
329   }
330
331   $form->{callback} = $form->escape($form->{callback}, 1);
332   $name = $form->escape("$form->{name}", 1);
333
334   $form->{callback} =
335     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
336   $form->redirect;
337
338   $lxdebug->leave_sub();
339 }
340
341 sub save_and_ap_transaction {
342   $lxdebug->enter_sub();
343
344   $auth->assert('customer_vendor_edit & general_ledger');
345
346   $form->{script} = "ap.pl";
347   # saving the history
348   if(!exists $form->{addition}) {
349     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
350         $form->{addition} = "SAVED";
351         $form->save_history($form->dbconnect(\%myconfig));
352   }
353   # /saving the history
354   &add_transaction;
355   $lxdebug->leave_sub();
356 }
357
358 sub save_and_ar_transaction {
359   $lxdebug->enter_sub();
360
361   $auth->assert('customer_vendor_edit & general_ledger');
362
363   $form->{script} = "ar.pl";
364   # saving the history
365   if(!exists $form->{addition}) {
366     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
367         $form->{addition} = "SAVED";
368         $form->save_history($form->dbconnect(\%myconfig));
369   }
370   # /saving the history
371   &add_transaction;
372   $lxdebug->leave_sub();
373 }
374
375 sub save_and_invoice {
376   $lxdebug->enter_sub();
377
378   if ($form->{db} eq 'customer') {
379     $auth->assert('customer_vendor_edit & invoice_edit');
380   } else {
381     $auth->assert('customer_vendor_edit & vendor_invoice_edit');
382   }
383
384   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
385   $form->{type} = "invoice";
386   # saving the history
387   if(!exists $form->{addition}) {
388     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
389         $form->{addition} = "SAVED";
390         $form->save_history($form->dbconnect(\%myconfig));
391   }
392   # /saving the history
393   &add_transaction;
394   $lxdebug->leave_sub();
395 }
396
397 sub save_and_rfq {
398   $lxdebug->enter_sub();
399
400   $auth->assert('customer_vendor_edit & request_quotation_edit');
401
402   $form->{script} = "oe.pl";
403   $form->{type}   = "request_quotation";
404   # saving the history
405   if(!exists $form->{addition}) {
406     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
407         $form->{addition} = "SAVED";
408         $form->save_history($form->dbconnect(\%myconfig));
409   }
410   # /saving the history
411   &add_transaction;
412   $lxdebug->leave_sub();
413 }
414
415 sub save_and_quotation {
416   $lxdebug->enter_sub();
417
418   $auth->assert('customer_vendor_edit & sales_quotation_edit');
419
420   $form->{script} = "oe.pl";
421   $form->{type}   = "sales_quotation";
422   # saving the history
423   if(!exists $form->{addition}) {
424     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
425         $form->{addition} = "SAVED";
426         $form->save_history($form->dbconnect(\%myconfig));
427   }
428   # /saving the history
429   &add_transaction;
430   $lxdebug->leave_sub();
431 }
432
433 sub save_and_order {
434   $lxdebug->enter_sub();
435
436   $auth->assert('customer_vendor_edit & sales_order_edit');
437
438   $form->{script} = "oe.pl";
439   $form->{type}   =
440     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
441   # saving the history
442   if(!exists $form->{addition}) {
443     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
444         $form->{addition} = "SAVED";
445         $form->save_history($form->dbconnect(\%myconfig));
446   }
447   # /saving the history
448   &add_transaction;
449   $lxdebug->leave_sub();
450 }
451
452 sub save_and_close {
453   $lxdebug->enter_sub();
454
455   $auth->assert('customer_vendor_edit');
456
457   $msg = ucfirst $form->{db};
458   $imsg .= " saved!";
459
460   $form->isblank("name", $locale->text("Name missing!"));
461   if ($form->{"db"} eq "customer") {
462     $rc = CT->save_customer(\%myconfig, \%$form);
463   } else {
464     $rc = CT->save_vendor(\%myconfig, \%$form);
465   }
466   if ($rc == 3) {
467     $form->error($locale->text('customernumber not unique!'));
468   }
469   # saving the history
470   if(!exists $form->{addition}) {
471     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
472     $form->{addition} = "SAVED";
473         $form->save_history($form->dbconnect(\%myconfig));
474   }
475   # /saving the history
476   $form->redirect($locale->text($msg));
477
478   $lxdebug->leave_sub();
479 }
480
481 sub save {
482   $lxdebug->enter_sub();
483
484   $auth->assert('customer_vendor_edit');
485
486   $msg = ucfirst $form->{db};
487   $imsg .= " saved!";
488
489   $form->isblank("name", $locale->text("Name missing!"));
490
491   my $res;
492   if ($form->{"db"} eq "customer") {
493     $res = CT->save_customer(\%myconfig, \%$form);
494   } else {
495     $res = CT->save_vendor(\%myconfig, \%$form);
496   }
497
498   if (3 == $res) {
499     if ($form->{"db"} eq "customer") {
500       $form->error($locale->text('This customer number is already in use.'));
501     } else {
502       $form->error($locale->text('This vendor number is already in use.'));
503     }
504   }
505   # saving the history
506   if(!exists $form->{addition}) {
507     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
508         $form->{addition} = "SAVED";
509         $form->save_history($form->dbconnect(\%myconfig));
510   }
511   # /saving the history
512   &edit;
513   exit;
514   $lxdebug->leave_sub();
515 }
516
517 sub delete {
518   $lxdebug->enter_sub();
519
520   $auth->assert('customer_vendor_edit');
521
522   CT->delete(\%myconfig, \%$form);
523
524   $msg = ucfirst $form->{db};
525   $msg .= " deleted!";
526   # saving the history
527   if(!exists $form->{addition}) {
528     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
529         $form->{addition} = "DELETED";
530         $form->save_history($form->dbconnect(\%myconfig));
531   }
532   # /saving the history 
533   $form->redirect($locale->text($msg));
534
535   $msg = "Cannot delete $form->{db}";
536   $form->error($locale->text($msg));
537
538   $lxdebug->leave_sub();
539 }
540
541 sub display {
542   $lxdebug->enter_sub();
543
544   $auth->assert('customer_vendor_edit');
545
546   &form_header();
547   &form_footer();
548
549   $lxdebug->leave_sub();
550 }
551
552 sub update {
553   $lxdebug->enter_sub();
554
555   $auth->assert('customer_vendor_edit');
556
557   &display();
558   $lxdebug->leave_sub();
559 }
560
561 sub get_contact {
562   $lxdebug->enter_sub();
563
564   $auth->assert('customer_vendor_edit');
565
566   CT->get_contact(\%myconfig, \%$form);
567   print $cgi->header(), join '__pjx__', map $form->{"cp_$_"}, 
568     qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday);
569   $lxdebug->leave_sub();
570
571 }
572
573 sub get_shipto {
574   $lxdebug->enter_sub();
575
576   $auth->assert('customer_vendor_edit');
577
578   CT->get_shipto(\%myconfig, \%$form);
579   print $cgi->header(),  join '__pjx__', map $form->{"shipto$_"},
580     qw(name department_1 department_2 street zipcode city country contact phone fax email used);
581   $lxdebug->leave_sub();
582
583 }
584
585 sub get_delivery {
586   $lxdebug->enter_sub();
587
588   $auth->assert('customer_vendor_edit');
589
590   CT->get_delivery(\%myconfig, \%$form );
591
592   print $cgi->header(), $form->parse_html_template('ct/get_delivery');
593
594   $lxdebug->leave_sub();
595 }
596
597 sub continue { call_sub($form->{nextsub}); }