Zwei neue Features:
[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;
41 use CGI::Ajax;
42 use POSIX qw(strftime);
43
44 use SL::CT;
45 use SL::CVar;
46 use SL::ReportGenerator;
47
48 require "bin/mozilla/common.pl";
49 require "bin/mozilla/reportgenerator.pl";
50
51 1;
52
53 # end of main
54
55 sub add {
56   $lxdebug->enter_sub();
57
58   $auth->assert('customer_vendor_edit');
59
60   $form->{title} = "Add";
61
62   $form->{callback} =
63     "$form->{script}?action=add&db=$form->{db}"
64     unless $form->{callback};
65
66   CT->populate_drop_down_boxes(\%myconfig, \%$form);
67
68   &form_header;
69   &form_footer;
70
71   $lxdebug->leave_sub();
72 }
73
74 sub search {
75   $lxdebug->enter_sub();
76
77   $auth->assert('customer_vendor_edit');
78
79   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
80
81   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
82   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
83
84   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
85   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
86    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
87                                                                            'include_prefix' => 'l_',
88                                                                            'include_value'  => 'Y');
89
90   $form->{jsscript} = 1;
91   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
92   $form->{fokus}    = 'Form.name';
93
94   $form->header();
95   print $form->parse_html_template('ct/search');
96
97   $lxdebug->leave_sub();
98 }
99
100 sub list_names {
101   $lxdebug->enter_sub();
102
103   $auth->assert('customer_vendor_edit');
104
105   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
106
107   CT->search(\%myconfig, \%$form);
108
109   my $cvar_configs = CVar->get_configs('module' => 'CT');
110
111   my @options;
112   if ($form->{status} eq 'all') {
113     push @options, $locale->text('All');
114
115   } elsif ($form->{status} eq 'orphaned') {
116     push @options, $locale->text('Orphaned');
117   }
118
119   if ($form->{name}) {
120     push @options, $locale->text('Name') . " : $form->{name}";
121   }
122   if ($form->{contact}) {
123     push @options, $locale->text('Contact') . " : $form->{contact}";
124   }
125   if ($form->{"$form->{db}number"}) {
126     push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|;
127   }
128   if ($form->{email}) {
129     push @options, $locale->text('E-mail') . " : $form->{email}";
130   }
131
132   my @columns = (
133     'id',        'name',  "$form->{db}number", 'address',  'contact',  'phone',
134     'fax',       'email', 'taxnumber',         'sic_code', 'business', 'invnumber',
135     'ordnumber', 'quonumber'
136   );
137
138   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
139   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
140
141   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
142
143   my %column_defs = (
144     'id'                => { 'text' => $locale->text('ID'), },
145     "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
146     'name'              => { 'text' => $locale->text('Name'), },
147     'address'           => { 'text' => $locale->text('Address'), },
148     'contact'           => { 'text' => $locale->text('Contact'), },
149     'phone'             => { 'text' => $locale->text('Phone'), },
150     'fax'               => { 'text' => $locale->text('Fax'), },
151     'email'             => { 'text' => $locale->text('E-mail'), },
152     'cc'                => { 'text' => $locale->text('Cc'), },
153     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
154     'sic_code'          => { 'text' => $locale->text('SIC'), },
155     'business'          => { 'text' => $locale->text('Type of Business'), },
156     'invnumber'         => { 'text' => $locale->text('Invoice'), },
157     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
158     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
159     %column_defs_cvars,
160   );
161
162   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
163
164   my @hidden_variables  = (qw(db status obsolete), map { "l_$_" } @columns);
165   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
166   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
167   $form->{callback}     = "$callback&sort=" . E($form->{sort});
168
169   map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
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}, 1);
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   # $locale->text('Edit Customer')
246   # $locale->text('Edit Vendor')
247
248   CT->get_tuple(\%myconfig, \%$form);
249   CT->populate_drop_down_boxes(\%myconfig, \%$form);
250
251   # format " into &quot;
252   map { $form->{$_} =~ s/\"/&quot;/g } keys %$form;
253
254   $form->{title} = "Edit";
255
256   # format discount
257   $form->{discount} *= 100;
258
259   &form_header;
260   &form_footer;
261
262   $lxdebug->leave_sub();
263 }
264
265 sub form_header {
266   $lxdebug->enter_sub();
267
268   $auth->assert('customer_vendor_edit');
269
270   $form->get_lists("employees" => "ALL_EMPLOYEES",
271                    "taxzones"  => "ALL_TAXZONES");
272   $form->get_pricegroup(\%myconfig, { all => 1 });
273
274   $form->{ALL_SALESMEN}   = $form->{ALL_EMPLOYEES};
275   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
276   $form->{is_admin}       = $myconfig{role} eq 'admin';
277   $form->{is_customer}    = $form->{db}     eq 'customer';
278   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
279   $form->{shipto_label}   = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
280   $form->{taxzone_id}     = 0                                                               if !$form->{id};
281   $form->{jsscript}       = 1;
282   $form->{fokus}          = "ct.greeting";
283
284   unshift @{ $form->{SHIPTO} },   +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
285   unshift @{ $form->{CONTACTS} }, +{ cp_id     => '0', cp_name => $locale->text('New contact') };
286
287   push @{ $form->{AJAX} }, map { 
288     new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" ) 
289   } qw(shipto contact delivery);
290
291   $form->{title} = $form->{title_save} 
292                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
293
294 ## LINET: Create a drop-down box with all prior titles and greetings.
295   CT->query_titles_and_greetings(\%myconfig, \%$form);
296   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
297 ## /LINET
298
299   $form->{NOTES} ||= [ ];
300
301   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
302
303   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
304
305   $form->header;
306   print $form->parse_html_template('ct/form_header');
307
308   $lxdebug->leave_sub();
309 }
310
311 sub form_footer {
312   $lxdebug->enter_sub();
313
314   $auth->assert('customer_vendor_edit');
315
316   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
317                                                        is_customer => $form->{db}     eq 'customer' });
318   $lxdebug->leave_sub();
319 }
320
321 sub add_transaction {
322   $lxdebug->enter_sub();
323
324   $auth->assert('customer_vendor_edit & general_ledger');
325
326 #  # saving the history
327 #  if(!exists $form->{addition}) {
328 #       $form->{addition} = "ADD TRANSACTION";
329 #       $form->save_history($form->dbconnect(\%myconfig));
330 #  }
331 #  # /saving the history
332   
333   $form->isblank("name", $locale->text("Name missing!"));
334   if ($form->{"db"} eq "customer") {
335     CT->save_customer(\%myconfig, \%$form);
336   } else {
337     CT->save_vendor(\%myconfig, \%$form);
338   }
339
340   $form->{callback} = $form->escape($form->{callback}, 1);
341   $name = $form->escape("$form->{name}", 1);
342
343   $form->{callback} =
344     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
345   $form->redirect;
346
347   $lxdebug->leave_sub();
348 }
349
350 sub save_and_ap_transaction {
351   $lxdebug->enter_sub();
352
353   $auth->assert('customer_vendor_edit & general_ledger');
354
355   $form->{script} = "ap.pl";
356   # saving the history
357   if(!exists $form->{addition}) {
358     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
359         $form->{addition} = "SAVED";
360         $form->save_history($form->dbconnect(\%myconfig));
361   }
362   # /saving the history
363   &add_transaction;
364   $lxdebug->leave_sub();
365 }
366
367 sub save_and_ar_transaction {
368   $lxdebug->enter_sub();
369
370   $auth->assert('customer_vendor_edit & general_ledger');
371
372   $form->{script} = "ar.pl";
373   # saving the history
374   if(!exists $form->{addition}) {
375     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
376         $form->{addition} = "SAVED";
377         $form->save_history($form->dbconnect(\%myconfig));
378   }
379   # /saving the history
380   &add_transaction;
381   $lxdebug->leave_sub();
382 }
383
384 sub save_and_invoice {
385   $lxdebug->enter_sub();
386
387   if ($form->{db} eq 'customer') {
388     $auth->assert('customer_vendor_edit & invoice_edit');
389   } else {
390     $auth->assert('customer_vendor_edit & vendor_invoice_edit');
391   }
392
393   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
394   $form->{type} = "invoice";
395   # saving the history
396   if(!exists $form->{addition}) {
397     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
398         $form->{addition} = "SAVED";
399         $form->save_history($form->dbconnect(\%myconfig));
400   }
401   # /saving the history
402   &add_transaction;
403   $lxdebug->leave_sub();
404 }
405
406 sub save_and_rfq {
407   $lxdebug->enter_sub();
408
409   $auth->assert('customer_vendor_edit & request_quotation_edit');
410
411   $form->{script} = "oe.pl";
412   $form->{type}   = "request_quotation";
413   # saving the history
414   if(!exists $form->{addition}) {
415     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
416         $form->{addition} = "SAVED";
417         $form->save_history($form->dbconnect(\%myconfig));
418   }
419   # /saving the history
420   &add_transaction;
421   $lxdebug->leave_sub();
422 }
423
424 sub save_and_quotation {
425   $lxdebug->enter_sub();
426
427   $auth->assert('customer_vendor_edit & sales_quotation_edit');
428
429   $form->{script} = "oe.pl";
430   $form->{type}   = "sales_quotation";
431   # saving the history
432   if(!exists $form->{addition}) {
433     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
434         $form->{addition} = "SAVED";
435         $form->save_history($form->dbconnect(\%myconfig));
436   }
437   # /saving the history
438   &add_transaction;
439   $lxdebug->leave_sub();
440 }
441
442 sub save_and_order {
443   $lxdebug->enter_sub();
444
445   $auth->assert('customer_vendor_edit & sales_order_edit');
446
447   $form->{script} = "oe.pl";
448   $form->{type}   =
449     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
450   # saving the history
451   if(!exists $form->{addition}) {
452     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
453         $form->{addition} = "SAVED";
454         $form->save_history($form->dbconnect(\%myconfig));
455   }
456   # /saving the history
457   &add_transaction;
458   $lxdebug->leave_sub();
459 }
460
461 sub save_and_close {
462   $lxdebug->enter_sub();
463
464   $auth->assert('customer_vendor_edit');
465
466   # $locale->text('Customer saved!')
467   # $locale->text('Vendor saved!')
468
469   $msg = ucfirst $form->{db};
470   $imsg .= " saved!";
471
472   $form->isblank("name", $locale->text("Name missing!"));
473   if ($form->{"db"} eq "customer") {
474     $rc = CT->save_customer(\%myconfig, \%$form);
475   } else {
476     $rc = CT->save_vendor(\%myconfig, \%$form);
477   }
478   if ($rc == 3) {
479     $form->error($locale->text('customernumber not unique!'));
480   }
481   # saving the history
482   if(!exists $form->{addition}) {
483     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
484     $form->{addition} = "SAVED";
485         $form->save_history($form->dbconnect(\%myconfig));
486   }
487   # /saving the history
488   $form->redirect($locale->text($msg));
489
490   $lxdebug->leave_sub();
491 }
492
493 sub save {
494   $lxdebug->enter_sub();
495
496   $auth->assert('customer_vendor_edit');
497
498   # $locale->text('Customer saved!')
499   # $locale->text('Vendor saved!')
500
501   $msg = ucfirst $form->{db};
502   $imsg .= " saved!";
503
504   $form->isblank("name", $locale->text("Name missing!"));
505
506   my $res;
507   if ($form->{"db"} eq "customer") {
508     $res = CT->save_customer(\%myconfig, \%$form);
509   } else {
510     $res = CT->save_vendor(\%myconfig, \%$form);
511   }
512
513   if (3 == $res) {
514     if ($form->{"db"} eq "customer") {
515       $form->error($locale->text('This customer number is already in use.'));
516     } else {
517       $form->error($locale->text('This vendor number is already in use.'));
518     }
519   }
520   # saving the history
521   if(!exists $form->{addition}) {
522     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
523         $form->{addition} = "SAVED";
524         $form->save_history($form->dbconnect(\%myconfig));
525   }
526   # /saving the history
527   &edit;
528   exit;
529   $lxdebug->leave_sub();
530 }
531
532 sub delete {
533   $lxdebug->enter_sub();
534
535   $auth->assert('customer_vendor_edit');
536
537   # $locale->text('Customer deleted!')
538   # $locale->text('Cannot delete customer!')
539   # $locale->text('Vendor deleted!')
540   # $locale->text('Cannot delete vendor!')
541
542   CT->delete(\%myconfig, \%$form);
543
544   $msg = ucfirst $form->{db};
545   $msg .= " deleted!";
546   # saving the history
547   if(!exists $form->{addition}) {
548     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
549         $form->{addition} = "DELETED";
550         $form->save_history($form->dbconnect(\%myconfig));
551   }
552   # /saving the history 
553   $form->redirect($locale->text($msg));
554
555   $msg = "Cannot delete $form->{db}";
556   $form->error($locale->text($msg));
557
558   $lxdebug->leave_sub();
559 }
560
561 sub display {
562   $lxdebug->enter_sub();
563
564   $auth->assert('customer_vendor_edit');
565
566   &form_header();
567   &form_footer();
568
569   $lxdebug->leave_sub();
570 }
571
572 sub update {
573   $lxdebug->enter_sub();
574
575   $auth->assert('customer_vendor_edit');
576
577   &display();
578   $lxdebug->leave_sub();
579 }
580
581 sub get_contact {
582   $lxdebug->enter_sub();
583
584   $auth->assert('customer_vendor_edit');
585
586   CT->get_contact(\%myconfig, \%$form);
587
588   my $q = new CGI;
589   $result = "$form->{cp_name}";
590   map { $result .= "__pjx__" . $form->{$_} } qw(cp_greeting cp_title cp_givenname cp_phone1 cp_phone2 cp_email cp_abteilung cp_fax cp_mobile1 cp_mobile2 cp_satphone cp_satfax cp_project cp_privatphone cp_privatemail cp_birthday);
591   print $q->header();
592   print $result;
593   $lxdebug->leave_sub();
594
595 }
596
597 sub get_shipto {
598   $lxdebug->enter_sub();
599
600   $auth->assert('customer_vendor_edit');
601
602   CT->get_shipto(\%myconfig, \%$form);
603
604   my $q = new CGI;
605   $result = "$form->{shiptoname}";
606   map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
607   print $q->header();
608   print $result;
609   $lxdebug->leave_sub();
610
611 }
612
613 sub get_delivery {
614   $lxdebug->enter_sub();
615
616   $auth->assert('customer_vendor_edit');
617
618   CT->get_delivery(\%myconfig, \%$form );
619
620   print CGI->new->header();
621   print $form->parse_html_template('ct/get_delivery');
622   $lxdebug->leave_sub();
623 }
624
625 sub continue { call_sub($form->{nextsub}); }