d4c5230d3e7f3c03cf910689abfabf5f0c6ba70e
[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_SALESMEN",
271                    "taxzones"  => "ALL_TAXZONES");
272   $form->get_pricegroup(\%myconfig, { all => 1 });
273
274   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
275   $form->{is_admin}       = $myconfig{role} eq 'admin';
276   $form->{is_customer}    = $form->{db}     eq 'customer';
277   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
278   $form->{shipto_label}   = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
279   $form->{taxzone_id}     = 0                                                               if !$form->{id};
280   $form->{jsscript}       = 1;
281   $form->{fokus}          = "ct.greeting";
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   push @{ $form->{AJAX} }, map { 
287     new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" ) 
288   } qw(shipto contact delivery);
289
290   $form->{title} = $form->{title_save} 
291                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
292
293 ## LINET: Create a drop-down box with all prior titles and greetings.
294   CT->query_titles_and_greetings(\%myconfig, \%$form);
295   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
296 ## /LINET
297
298   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
299
300   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
301
302   $main::lxdebug->dump(0, "cvar", $form->{CUSTOM_VARIABLES});
303
304   $form->header;
305   print $form->parse_html_template('ct/form_header');
306
307   $lxdebug->leave_sub();
308 }
309
310 sub form_footer {
311   $lxdebug->enter_sub();
312
313   $auth->assert('customer_vendor_edit');
314
315   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
316                                                        is_customer => $form->{db}     eq 'customer' });
317   $lxdebug->leave_sub();
318 }
319
320 sub add_transaction {
321   $lxdebug->enter_sub();
322
323   $auth->assert('customer_vendor_edit & general_ledger');
324
325 #  # saving the history
326 #  if(!exists $form->{addition}) {
327 #       $form->{addition} = "ADD TRANSACTION";
328 #       $form->save_history($form->dbconnect(\%myconfig));
329 #  }
330 #  # /saving the history
331   
332   $form->isblank("name", $locale->text("Name missing!"));
333   if ($form->{"db"} eq "customer") {
334     CT->save_customer(\%myconfig, \%$form);
335   } else {
336     CT->save_vendor(\%myconfig, \%$form);
337   }
338
339   $form->{callback} = $form->escape($form->{callback}, 1);
340   $name = $form->escape("$form->{name}", 1);
341
342   $form->{callback} =
343     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
344   $form->redirect;
345
346   $lxdebug->leave_sub();
347 }
348
349 sub save_and_ap_transaction {
350   $lxdebug->enter_sub();
351
352   $auth->assert('customer_vendor_edit & general_ledger');
353
354   $form->{script} = "ap.pl";
355   # saving the history
356   if(!exists $form->{addition}) {
357     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
358         $form->{addition} = "SAVED";
359         $form->save_history($form->dbconnect(\%myconfig));
360   }
361   # /saving the history
362   &add_transaction;
363   $lxdebug->leave_sub();
364 }
365
366 sub save_and_ar_transaction {
367   $lxdebug->enter_sub();
368
369   $auth->assert('customer_vendor_edit & general_ledger');
370
371   $form->{script} = "ar.pl";
372   # saving the history
373   if(!exists $form->{addition}) {
374     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
375         $form->{addition} = "SAVED";
376         $form->save_history($form->dbconnect(\%myconfig));
377   }
378   # /saving the history
379   &add_transaction;
380   $lxdebug->leave_sub();
381 }
382
383 sub save_and_invoice {
384   $lxdebug->enter_sub();
385
386   if ($form->{db} eq 'customer') {
387     $auth->assert('customer_vendor_edit & invoice_edit');
388   } else {
389     $auth->assert('customer_vendor_edit & vendor_invoice_edit');
390   }
391
392   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
393   $form->{type} = "invoice";
394   # saving the history
395   if(!exists $form->{addition}) {
396     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
397         $form->{addition} = "SAVED";
398         $form->save_history($form->dbconnect(\%myconfig));
399   }
400   # /saving the history
401   &add_transaction;
402   $lxdebug->leave_sub();
403 }
404
405 sub save_and_rfq {
406   $lxdebug->enter_sub();
407
408   $auth->assert('customer_vendor_edit & request_quotation_edit');
409
410   $form->{script} = "oe.pl";
411   $form->{type}   = "request_quotation";
412   # saving the history
413   if(!exists $form->{addition}) {
414     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
415         $form->{addition} = "SAVED";
416         $form->save_history($form->dbconnect(\%myconfig));
417   }
418   # /saving the history
419   &add_transaction;
420   $lxdebug->leave_sub();
421 }
422
423 sub save_and_quotation {
424   $lxdebug->enter_sub();
425
426   $auth->assert('customer_vendor_edit & sales_quotation_edit');
427
428   $form->{script} = "oe.pl";
429   $form->{type}   = "sales_quotation";
430   # saving the history
431   if(!exists $form->{addition}) {
432     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
433         $form->{addition} = "SAVED";
434         $form->save_history($form->dbconnect(\%myconfig));
435   }
436   # /saving the history
437   &add_transaction;
438   $lxdebug->leave_sub();
439 }
440
441 sub save_and_order {
442   $lxdebug->enter_sub();
443
444   $auth->assert('customer_vendor_edit & sales_order_edit');
445
446   $form->{script} = "oe.pl";
447   $form->{type}   =
448     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
449   # saving the history
450   if(!exists $form->{addition}) {
451     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
452         $form->{addition} = "SAVED";
453         $form->save_history($form->dbconnect(\%myconfig));
454   }
455   # /saving the history
456   &add_transaction;
457   $lxdebug->leave_sub();
458 }
459
460 sub save_and_close {
461   $lxdebug->enter_sub();
462
463   $auth->assert('customer_vendor_edit');
464
465   # $locale->text('Customer saved!')
466   # $locale->text('Vendor saved!')
467
468   $msg = ucfirst $form->{db};
469   $imsg .= " saved!";
470
471   $form->isblank("name", $locale->text("Name missing!"));
472   if ($form->{"db"} eq "customer") {
473     $rc = CT->save_customer(\%myconfig, \%$form);
474   } else {
475     $rc = CT->save_vendor(\%myconfig, \%$form);
476   }
477   if ($rc == 3) {
478     $form->error($locale->text('customernumber not unique!'));
479   }
480   # saving the history
481   if(!exists $form->{addition}) {
482     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
483     $form->{addition} = "SAVED";
484         $form->save_history($form->dbconnect(\%myconfig));
485   }
486   # /saving the history
487   $form->redirect($locale->text($msg));
488
489   $lxdebug->leave_sub();
490 }
491
492 sub save {
493   $lxdebug->enter_sub();
494
495   $auth->assert('customer_vendor_edit');
496
497   # $locale->text('Customer saved!')
498   # $locale->text('Vendor saved!')
499
500   $msg = ucfirst $form->{db};
501   $imsg .= " saved!";
502
503   $form->isblank("name", $locale->text("Name missing!"));
504
505   my $res;
506   if ($form->{"db"} eq "customer") {
507     $res = CT->save_customer(\%myconfig, \%$form);
508   } else {
509     $res = CT->save_vendor(\%myconfig, \%$form);
510   }
511
512   if (3 == $res) {
513     if ($form->{"db"} eq "customer") {
514       $form->error($locale->text('This customer number is already in use.'));
515     } else {
516       $form->error($locale->text('This vendor number is already in use.'));
517     }
518   }
519   # saving the history
520   if(!exists $form->{addition}) {
521     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
522         $form->{addition} = "SAVED";
523         $form->save_history($form->dbconnect(\%myconfig));
524   }
525   # /saving the history
526   &edit;
527   exit;
528   $lxdebug->leave_sub();
529 }
530
531 sub delete {
532   $lxdebug->enter_sub();
533
534   $auth->assert('customer_vendor_edit');
535
536   # $locale->text('Customer deleted!')
537   # $locale->text('Cannot delete customer!')
538   # $locale->text('Vendor deleted!')
539   # $locale->text('Cannot delete vendor!')
540
541   CT->delete(\%myconfig, \%$form);
542
543   $msg = ucfirst $form->{db};
544   $msg .= " deleted!";
545   # saving the history
546   if(!exists $form->{addition}) {
547     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
548         $form->{addition} = "DELETED";
549         $form->save_history($form->dbconnect(\%myconfig));
550   }
551   # /saving the history 
552   $form->redirect($locale->text($msg));
553
554   $msg = "Cannot delete $form->{db}";
555   $form->error($locale->text($msg));
556
557   $lxdebug->leave_sub();
558 }
559
560 sub display {
561   $lxdebug->enter_sub();
562
563   $auth->assert('customer_vendor_edit');
564
565   &form_header();
566   &form_footer();
567
568   $lxdebug->leave_sub();
569 }
570
571 sub update {
572   $lxdebug->enter_sub();
573
574   $auth->assert('customer_vendor_edit');
575
576   &display();
577   $lxdebug->leave_sub();
578 }
579
580 sub get_contact {
581   $lxdebug->enter_sub();
582
583   $auth->assert('customer_vendor_edit');
584
585   CT->get_contact(\%myconfig, \%$form);
586
587   my $q = new CGI;
588   $result = "$form->{cp_name}";
589   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);
590   print $q->header();
591   print $result;
592   $lxdebug->leave_sub();
593
594 }
595
596 sub get_shipto {
597   $lxdebug->enter_sub();
598
599   $auth->assert('customer_vendor_edit');
600
601   CT->get_shipto(\%myconfig, \%$form);
602
603   my $q = new CGI;
604   $result = "$form->{shiptoname}";
605   map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
606   print $q->header();
607   print $result;
608   $lxdebug->leave_sub();
609
610 }
611
612 sub get_delivery {
613   $lxdebug->enter_sub();
614
615   $auth->assert('customer_vendor_edit');
616
617   CT->get_delivery(\%myconfig, \%$form );
618
619   print CGI->new->header();
620   print $form->parse_html_template('ct/get_delivery');
621   $lxdebug->leave_sub();
622 }
623
624 sub continue { call_sub($form->{nextsub}); }