einfachere ajax methoden
[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
40 use CGI::Ajax;
41 use POSIX qw(strftime);
42
43 use SL::CT;
44 use SL::CVar;
45 use SL::ReportGenerator;
46
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
49
50 1;
51
52 # end of main
53
54 sub add {
55   $lxdebug->enter_sub();
56
57   $auth->assert('customer_vendor_edit');
58
59   $form->{title} = "Add";
60
61   $form->{callback} =
62     "$form->{script}?action=add&db=$form->{db}"
63     unless $form->{callback};
64
65   CT->populate_drop_down_boxes(\%myconfig, \%$form);
66
67   &form_header;
68   &form_footer;
69
70   $lxdebug->leave_sub();
71 }
72
73 sub search {
74   $lxdebug->enter_sub();
75
76   $auth->assert('customer_vendor_edit');
77
78   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
79
80   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
81   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
82
83   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
84   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
85    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
86                                                                            'include_prefix' => 'l_',
87                                                                            'include_value'  => 'Y');
88
89   $form->{jsscript} = 1;
90   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
91   $form->{fokus}    = 'Form.name';
92
93   $form->header();
94   print $form->parse_html_template('ct/search');
95
96   $lxdebug->leave_sub();
97 }
98
99 sub list_names {
100   $lxdebug->enter_sub();
101
102   $auth->assert('customer_vendor_edit');
103
104   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
105
106   CT->search(\%myconfig, \%$form);
107
108   my $cvar_configs = CVar->get_configs('module' => 'CT');
109
110   my @options;
111   if ($form->{status} eq 'all') {
112     push @options, $locale->text('All');
113   } elsif ($form->{status} eq 'orphaned') {
114     push @options, $locale->text('Orphaned');
115   }
116
117   push @options, $locale->text('Name') . " : $form->{name}"                    if $form->{name};
118   push @options, $locale->text('Contact') . " : $form->{contact}"              if $form->{contact};
119   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
120   push @options, $locale->text('E-mail') . " : $form->{email}"                 if $form->{email};
121
122   my @columns = (
123     'id',        'name',  "$form->{db}number", 'address',  'contact',  'phone',
124     'fax',       'email', 'taxnumber',         'sic_code', 'business', 'invnumber',
125     'ordnumber', 'quonumber'
126   );
127
128   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
129   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
130
131   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
132
133   my %column_defs = (
134     'id'                => { 'text' => $locale->text('ID'), },
135     "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
136     'name'              => { 'text' => $locale->text('Name'), },
137     'address'           => { 'text' => $locale->text('Address'), },
138     'contact'           => { 'text' => $locale->text('Contact'), },
139     'phone'             => { 'text' => $locale->text('Phone'), },
140     'fax'               => { 'text' => $locale->text('Fax'), },
141     'email'             => { 'text' => $locale->text('E-mail'), },
142     'cc'                => { 'text' => $locale->text('Cc'), },
143     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
144     'sic_code'          => { 'text' => $locale->text('SIC'), },
145     'business'          => { 'text' => $locale->text('Type of Business'), },
146     'invnumber'         => { 'text' => $locale->text('Invoice'), },
147     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
148     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
149     %column_defs_cvars,
150   );
151
152   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
153
154   my @hidden_variables  = (qw(db status obsolete), map { "l_$_" } @columns);
155   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
156   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
157   $form->{callback}     = "$callback&sort=" . E($form->{sort});
158
159   map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
160
161   my ($ordertype, $quotationtype, $attachment_basename);
162   if ($form->{IS_CUSTOMER}) {
163     $form->{title}       = $locale->text('Customers');
164     $ordertype           = 'sales_order';
165     $quotationtype       = 'sales_quotation';
166     $attachment_basename = $locale->text('customer_list');
167
168   } else {
169     $form->{title}       = $locale->text('Vendors');
170     $ordertype           = 'purchase_order';
171     $quotationtype       = 'request_quotation';
172     $attachment_basename = $locale->text('vendor_list');
173   }
174
175   my $report = SL::ReportGenerator->new(\%myconfig, $form);
176
177   $report->set_options('top_info_text'         => join("\n", @options),
178                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
179                        'output_format'         => 'HTML',
180                        'title'                 => $form->{title},
181                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
182     );
183   $report->set_options_from_form();
184
185   $report->set_columns(%column_defs);
186   $report->set_column_order(@columns);
187
188   $report->set_export_options('list_names', @hidden_variables);
189
190   $report->set_sort_indicator($form->{sort}, 1);
191
192   CVar->add_custom_variables_to_report('module'         => 'CT',
193                                        'trans_id_field' => 'id',
194                                        'configs'        => $cvar_configs,
195                                        'column_defs'    => \%column_defs,
196                                        'data'           => $form->{CT});
197
198   my $previous_id;
199
200   foreach my $ref (@{ $form->{CT} }) {
201     my $row = { map { $_ => { 'data' => '' } } @columns };
202
203     if ($ref->{id} ne $previous_id) {
204       $previous_id = $ref->{id};
205       map { $row->{$_}->{data} = $ref->{$_} } @columns;
206
207       $row->{name}->{link}  = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
208       $row->{email}->{link} = 'mailto:' . E($ref->{email});
209     }
210
211     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
212     $row->{invnumber}->{link} = $base_url;
213     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
214     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
215     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
216     $row->{$column}->{data}   = $ref->{$column};
217
218     $report->add_data($row);
219   }
220
221   $report->generate_with_headers();
222
223   $lxdebug->leave_sub();
224 }
225
226 sub edit {
227   $lxdebug->enter_sub();
228
229   $auth->assert('customer_vendor_edit');
230
231   # show history button
232   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
233   #/show hhistory button
234   
235   # $locale->text('Edit Customer')
236   # $locale->text('Edit Vendor')
237
238   CT->get_tuple(\%myconfig, \%$form);
239   CT->populate_drop_down_boxes(\%myconfig, \%$form);
240
241   # format " into &quot;
242   map { $form->{$_} =~ s/\"/&quot;/g } keys %$form;
243
244   $form->{title} = "Edit";
245
246   # format discount
247   $form->{discount} *= 100;
248
249   &form_header;
250   &form_footer;
251
252   $lxdebug->leave_sub();
253 }
254
255 sub form_header {
256   $lxdebug->enter_sub();
257
258   $auth->assert('customer_vendor_edit');
259
260   $form->get_lists("employees" => "ALL_EMPLOYEES",
261                    "taxzones"  => "ALL_TAXZONES");
262   $form->get_pricegroup(\%myconfig, { all => 1 });
263
264   $form->{ALL_SALESMEN}   = $form->{ALL_EMPLOYEES};
265   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
266   $form->{is_admin}       = $myconfig{role} eq 'admin';
267   $form->{is_customer}    = $form->{db}     eq 'customer';
268   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
269   $form->{shipto_label}   = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
270   $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
271   $form->{taxzone_id}     = 0                                                               if !$form->{id};
272   $form->{jsscript}       = 1;
273   $form->{fokus}          = "ct.greeting";
274
275   unshift @{ $form->{SHIPTO} },   +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
276   unshift @{ $form->{CONTACTS} }, +{ cp_id     => '0', cp_name => $locale->text('New contact') };
277
278   push @{ $form->{AJAX} }, map { 
279     new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" ) 
280   } qw(shipto contact delivery);
281
282   $form->{title} = $form->{title_save} 
283                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
284
285 ## LINET: Create a drop-down box with all prior titles and greetings.
286   CT->query_titles_and_greetings(\%myconfig, \%$form);
287   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
288 ## /LINET
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   # $locale->text('Customer saved!')
458   # $locale->text('Vendor saved!')
459
460   $msg = ucfirst $form->{db};
461   $imsg .= " saved!";
462
463   $form->isblank("name", $locale->text("Name missing!"));
464   if ($form->{"db"} eq "customer") {
465     $rc = CT->save_customer(\%myconfig, \%$form);
466   } else {
467     $rc = CT->save_vendor(\%myconfig, \%$form);
468   }
469   if ($rc == 3) {
470     $form->error($locale->text('customernumber not unique!'));
471   }
472   # saving the history
473   if(!exists $form->{addition}) {
474     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
475     $form->{addition} = "SAVED";
476         $form->save_history($form->dbconnect(\%myconfig));
477   }
478   # /saving the history
479   $form->redirect($locale->text($msg));
480
481   $lxdebug->leave_sub();
482 }
483
484 sub save {
485   $lxdebug->enter_sub();
486
487   $auth->assert('customer_vendor_edit');
488
489   # $locale->text('Customer saved!')
490   # $locale->text('Vendor saved!')
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   # $locale->text('Customer deleted!')
529   # $locale->text('Cannot delete customer!')
530   # $locale->text('Vendor deleted!')
531   # $locale->text('Cannot delete vendor!')
532
533   CT->delete(\%myconfig, \%$form);
534
535   $msg = ucfirst $form->{db};
536   $msg .= " deleted!";
537   # saving the history
538   if(!exists $form->{addition}) {
539     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
540         $form->{addition} = "DELETED";
541         $form->save_history($form->dbconnect(\%myconfig));
542   }
543   # /saving the history 
544   $form->redirect($locale->text($msg));
545
546   $msg = "Cannot delete $form->{db}";
547   $form->error($locale->text($msg));
548
549   $lxdebug->leave_sub();
550 }
551
552 sub display {
553   $lxdebug->enter_sub();
554
555   $auth->assert('customer_vendor_edit');
556
557   &form_header();
558   &form_footer();
559
560   $lxdebug->leave_sub();
561 }
562
563 sub update {
564   $lxdebug->enter_sub();
565
566   $auth->assert('customer_vendor_edit');
567
568   &display();
569   $lxdebug->leave_sub();
570 }
571
572 sub get_contact {
573   $lxdebug->enter_sub();
574
575   $auth->assert('customer_vendor_edit');
576
577   CT->get_contact(\%myconfig, \%$form);
578
579   print $cgi->header(), join '__pjx__', map $form->{"cp_$_"}, 
580     qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday);
581   $lxdebug->leave_sub();
582
583 }
584
585 sub get_shipto {
586   $lxdebug->enter_sub();
587
588   $auth->assert('customer_vendor_edit');
589
590   CT->get_shipto(\%myconfig, \%$form);
591
592   print $cgi->header(),  join '__pjx__', map $form->{"shipto$_"},
593     qw(name department_1 department_2 street zipcode city country contact phone fax email);
594   $lxdebug->leave_sub();
595
596 }
597
598 sub get_delivery {
599   $lxdebug->enter_sub();
600
601   $auth->assert('customer_vendor_edit');
602
603   CT->get_delivery(\%myconfig, \%$form );
604
605   print $cgi->header(), $form->parse_html_template('ct/get_delivery');
606
607   $lxdebug->leave_sub();
608 }
609
610 sub continue { call_sub($form->{nextsub}); }