Umstellung der Benutzerverwaltung von Dateien im Verzeichnis "users" auf die Verwendu...
[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::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->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
84   $form->{fokus} = 'Form.name';
85
86   $form->header();
87   print $form->parse_html_template('ct/search');
88
89   $lxdebug->leave_sub();
90 }
91
92 sub list_names {
93   $lxdebug->enter_sub();
94
95   $auth->assert('customer_vendor_edit');
96
97   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
98
99   CT->search(\%myconfig, \%$form);
100
101   my @options;
102   if ($form->{status} eq 'all') {
103     push @options, $locale->text('All');
104
105   } elsif ($form->{status} eq 'orphaned') {
106     push @options, $locale->text('Orphaned');
107   }
108
109   if ($form->{name}) {
110     push @options, $locale->text('Name') . " : $form->{name}";
111   }
112   if ($form->{contact}) {
113     push @options, $locale->text('Contact') . " : $form->{contact}";
114   }
115   if ($form->{"$form->{db}number"}) {
116     push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|;
117   }
118   if ($form->{email}) {
119     push @options, $locale->text('E-mail') . " : $form->{email}";
120   }
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 %column_defs = (
129     'id'                => { 'text' => $locale->text('ID'), },
130     "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
131     'name'              => { 'text' => $locale->text('Name'), },
132     'address'           => { 'text' => $locale->text('Address'), },
133     'contact'           => { 'text' => $locale->text('Contact'), },
134     'phone'             => { 'text' => $locale->text('Phone'), },
135     'fax'               => { 'text' => $locale->text('Fax'), },
136     'email'             => { 'text' => $locale->text('E-mail'), },
137     'cc'                => { 'text' => $locale->text('Cc'), },
138     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
139     'sic_code'          => { 'text' => $locale->text('SIC'), },
140     'business'          => { 'text' => $locale->text('Type of Business'), },
141     'invnumber'         => { 'text' => $locale->text('Invoice'), },
142     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
143     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
144   );
145
146   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
147
148   my @hidden_variables  = (qw(db status obsolete), map { "l_$_" } @columns);
149   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
150   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
151   $form->{callback}     = "$callback&sort=" . E($form->{sort});
152
153   map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
154
155   my ($ordertype, $quotationtype, $attachment_basename);
156   if ($form->{IS_CUSTOMER}) {
157     $form->{title}       = $locale->text('Customers');
158     $ordertype           = 'sales_order';
159     $quotationtype       = 'sales_quotation';
160     $attachment_basename = $locale->text('customer_list');
161
162   } else {
163     $form->{title}       = $locale->text('Vendors');
164     $ordertype           = 'purchase_order';
165     $quotationtype       = 'request_quotation';
166     $attachment_basename = $locale->text('vendor_list');
167   }
168
169   my $report = SL::ReportGenerator->new(\%myconfig, $form);
170
171   $report->set_options('top_info_text'         => join("\n", @options),
172                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
173                        'output_format'         => 'HTML',
174                        'title'                 => $form->{title},
175                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
176     );
177   $report->set_options_from_form();
178
179   $report->set_columns(%column_defs);
180   $report->set_column_order(@columns);
181
182   $report->set_export_options('list_names', @hidden_variables);
183
184   $report->set_sort_indicator($form->{sort}, 1);
185
186   my $previous_id;
187
188   foreach my $ref (@{ $form->{CT} }) {
189     my $row = { map { $_ => { 'data' => '' } } @columns };
190
191     if ($ref->{id} ne $previous_id) {
192       $previous_id = $ref->{id};
193       map { $row->{$_}->{data} = $ref->{$_} } @columns;
194
195       $row->{name}->{link}  = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
196       $row->{email}->{link} = 'mailto:' . E($ref->{email});
197     }
198
199     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
200     $row->{invnumber}->{link} = $base_url;
201     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
202     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
203     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
204     $row->{$column}->{data}   = $ref->{$column};
205
206     $report->add_data($row);
207   }
208
209   $report->generate_with_headers();
210
211   $lxdebug->leave_sub();
212 }
213
214 sub edit {
215   $lxdebug->enter_sub();
216
217   $auth->assert('customer_vendor_edit');
218
219   # show history button
220   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
221   #/show hhistory button
222   
223   # $locale->text('Edit Customer')
224   # $locale->text('Edit Vendor')
225
226   CT->get_tuple(\%myconfig, \%$form);
227   CT->populate_drop_down_boxes(\%myconfig, \%$form);
228
229   # format " into &quot;
230   map { $form->{$_} =~ s/\"/&quot;/g } keys %$form;
231
232   $form->{title} = "Edit";
233
234   # format discount
235   $form->{discount} *= 100;
236
237   &form_header;
238   &form_footer;
239
240   $lxdebug->leave_sub();
241 }
242
243 sub form_header {
244   $lxdebug->enter_sub();
245
246   $auth->assert('customer_vendor_edit');
247
248   $form->get_lists("employees" => "ALL_SALESMEN",
249                    "taxzones"  => "ALL_TAXZONES");
250   $form->get_pricegroup(\%myconfig, { all => 1 });
251
252   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
253   $form->{is_admin}       = $myconfig{role} eq 'admin';
254   $form->{is_customer}    = $form->{db}     eq 'customer';
255   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
256   $form->{shipto_label}   = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
257   $form->{taxzone_id}     = 0                                                               if !$form->{id};
258   $form->{jsscript}       = 1;
259   $form->{fokus}          = "ct.greeting";
260
261   unshift @{ $form->{SHIPTO} },   +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
262   unshift @{ $form->{CONTACTS} }, +{ cp_id     => '0', cp_name => $locale->text('New contact') };
263
264   push @{ $form->{AJAX} }, map { 
265     new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" ) 
266   } qw(shipto contact delivery);
267
268   $form->{title} = $form->{title_save} 
269                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
270
271 ## LINET: Create a drop-down box with all prior titles and greetings.
272   CT->query_titles_and_greetings(\%myconfig, \%$form);
273   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
274 ## /LINET
275
276   $form->header;
277   print $form->parse_html_template('ct/form_header');
278
279   $lxdebug->leave_sub();
280 }
281
282 sub form_footer {
283   $lxdebug->enter_sub();
284
285   $auth->assert('customer_vendor_edit');
286
287   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
288                                                        is_customer => $form->{db}     eq 'customer' });
289   $lxdebug->leave_sub();
290 }
291
292 sub add_transaction {
293   $lxdebug->enter_sub();
294
295   $auth->assert('customer_vendor_edit & general_ledger');
296
297 #  # saving the history
298 #  if(!exists $form->{addition}) {
299 #       $form->{addition} = "ADD TRANSACTION";
300 #       $form->save_history($form->dbconnect(\%myconfig));
301 #  }
302 #  # /saving the history
303   
304   $form->isblank("name", $locale->text("Name missing!"));
305   if ($form->{"db"} eq "customer") {
306     CT->save_customer(\%myconfig, \%$form);
307   } else {
308     CT->save_vendor(\%myconfig, \%$form);
309   }
310
311   $form->{callback} = $form->escape($form->{callback}, 1);
312   $name = $form->escape("$form->{name}", 1);
313
314   $form->{callback} =
315     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
316   $form->redirect;
317
318   $lxdebug->leave_sub();
319 }
320
321 sub save_and_ap_transaction {
322   $lxdebug->enter_sub();
323
324   $auth->assert('customer_vendor_edit & general_ledger');
325
326   $form->{script} = "ap.pl";
327   # saving the history
328   if(!exists $form->{addition}) {
329     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
330         $form->{addition} = "SAVED";
331         $form->save_history($form->dbconnect(\%myconfig));
332   }
333   # /saving the history
334   &add_transaction;
335   $lxdebug->leave_sub();
336 }
337
338 sub save_and_ar_transaction {
339   $lxdebug->enter_sub();
340
341   $auth->assert('customer_vendor_edit & general_ledger');
342
343   $form->{script} = "ar.pl";
344   # saving the history
345   if(!exists $form->{addition}) {
346     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
347         $form->{addition} = "SAVED";
348         $form->save_history($form->dbconnect(\%myconfig));
349   }
350   # /saving the history
351   &add_transaction;
352   $lxdebug->leave_sub();
353 }
354
355 sub save_and_invoice {
356   $lxdebug->enter_sub();
357
358   if ($form->{db} eq 'customer') {
359     $auth->assert('customer_vendor_edit & invoice_edit');
360   } else {
361     $auth->assert('customer_vendor_edit & vendor_invoice_edit');
362   }
363
364   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
365   $form->{type} = "invoice";
366   # saving the history
367   if(!exists $form->{addition}) {
368     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
369         $form->{addition} = "SAVED";
370         $form->save_history($form->dbconnect(\%myconfig));
371   }
372   # /saving the history
373   &add_transaction;
374   $lxdebug->leave_sub();
375 }
376
377 sub save_and_rfq {
378   $lxdebug->enter_sub();
379
380   $auth->assert('customer_vendor_edit & request_quotation_edit');
381
382   $form->{script} = "oe.pl";
383   $form->{type}   = "request_quotation";
384   # saving the history
385   if(!exists $form->{addition}) {
386     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
387         $form->{addition} = "SAVED";
388         $form->save_history($form->dbconnect(\%myconfig));
389   }
390   # /saving the history
391   &add_transaction;
392   $lxdebug->leave_sub();
393 }
394
395 sub save_and_quotation {
396   $lxdebug->enter_sub();
397
398   $auth->assert('customer_vendor_edit & sales_quotation_edit');
399
400   $form->{script} = "oe.pl";
401   $form->{type}   = "sales_quotation";
402   # saving the history
403   if(!exists $form->{addition}) {
404     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
405         $form->{addition} = "SAVED";
406         $form->save_history($form->dbconnect(\%myconfig));
407   }
408   # /saving the history
409   &add_transaction;
410   $lxdebug->leave_sub();
411 }
412
413 sub save_and_order {
414   $lxdebug->enter_sub();
415
416   $auth->assert('customer_vendor_edit & sales_order_edit');
417
418   $form->{script} = "oe.pl";
419   $form->{type}   =
420     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
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_close {
433   $lxdebug->enter_sub();
434
435   $auth->assert('customer_vendor_edit');
436
437   # $locale->text('Customer saved!')
438   # $locale->text('Vendor saved!')
439
440   $msg = ucfirst $form->{db};
441   $imsg .= " saved!";
442
443   $form->isblank("name", $locale->text("Name missing!"));
444   if ($form->{"db"} eq "customer") {
445     $rc = CT->save_customer(\%myconfig, \%$form);
446   } else {
447     $rc = CT->save_vendor(\%myconfig, \%$form);
448   }
449   if ($rc == 3) {
450     $form->error($locale->text('customernumber not unique!'));
451   }
452   # saving the history
453   if(!exists $form->{addition}) {
454     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
455     $form->{addition} = "SAVED";
456         $form->save_history($form->dbconnect(\%myconfig));
457   }
458   # /saving the history
459   $form->redirect($locale->text($msg));
460
461   $lxdebug->leave_sub();
462 }
463
464 sub save {
465   $lxdebug->enter_sub();
466
467   $auth->assert('customer_vendor_edit');
468
469   # $locale->text('Customer saved!')
470   # $locale->text('Vendor saved!')
471
472   $msg = ucfirst $form->{db};
473   $imsg .= " saved!";
474
475   $form->isblank("name", $locale->text("Name missing!"));
476
477   my $res;
478   if ($form->{"db"} eq "customer") {
479     $res = CT->save_customer(\%myconfig, \%$form);
480   } else {
481     $res = CT->save_vendor(\%myconfig, \%$form);
482   }
483
484   if (3 == $res) {
485     if ($form->{"db"} eq "customer") {
486       $form->error($locale->text('This customer number is already in use.'));
487     } else {
488       $form->error($locale->text('This vendor number is already in use.'));
489     }
490   }
491   # saving the history
492   if(!exists $form->{addition}) {
493     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
494         $form->{addition} = "SAVED";
495         $form->save_history($form->dbconnect(\%myconfig));
496   }
497   # /saving the history
498   &edit;
499   exit;
500   $lxdebug->leave_sub();
501 }
502
503 sub delete {
504   $lxdebug->enter_sub();
505
506   $auth->assert('customer_vendor_edit');
507
508   # $locale->text('Customer deleted!')
509   # $locale->text('Cannot delete customer!')
510   # $locale->text('Vendor deleted!')
511   # $locale->text('Cannot delete vendor!')
512
513   CT->delete(\%myconfig, \%$form);
514
515   $msg = ucfirst $form->{db};
516   $msg .= " deleted!";
517   # saving the history
518   if(!exists $form->{addition}) {
519     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
520         $form->{addition} = "DELETED";
521         $form->save_history($form->dbconnect(\%myconfig));
522   }
523   # /saving the history 
524   $form->redirect($locale->text($msg));
525
526   $msg = "Cannot delete $form->{db}";
527   $form->error($locale->text($msg));
528
529   $lxdebug->leave_sub();
530 }
531
532 sub display {
533   $lxdebug->enter_sub();
534
535   $auth->assert('customer_vendor_edit');
536
537   &form_header();
538   &form_footer();
539
540   $lxdebug->leave_sub();
541 }
542
543 sub update {
544   $lxdebug->enter_sub();
545
546   $auth->assert('customer_vendor_edit');
547
548   &display();
549   $lxdebug->leave_sub();
550 }
551
552 sub get_contact {
553   $lxdebug->enter_sub();
554
555   $auth->assert('customer_vendor_edit');
556
557   CT->get_contact(\%myconfig, \%$form);
558
559   my $q = new CGI;
560   $result = "$form->{cp_name}";
561   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);
562   print $q->header();
563   print $result;
564   $lxdebug->leave_sub();
565
566 }
567
568 sub get_shipto {
569   $lxdebug->enter_sub();
570
571   $auth->assert('customer_vendor_edit');
572
573   CT->get_shipto(\%myconfig, \%$form);
574
575   my $q = new CGI;
576   $result = "$form->{shiptoname}";
577   map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
578   print $q->header();
579   print $result;
580   $lxdebug->leave_sub();
581
582 }
583
584 sub get_delivery {
585   $lxdebug->enter_sub();
586
587   $auth->assert('customer_vendor_edit');
588
589   CT->get_delivery(\%myconfig, \%$form );
590
591   print CGI->new->header();
592   print $form->parse_html_template('ct/get_delivery');
593   $lxdebug->leave_sub();
594 }
595
596 sub continue { call_sub($form->{nextsub}); }