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