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 # $locale->text('Edit Customer')
40 # $locale->text('Edit Vendor')
41 # $locale->text('Customer saved!')
42 # $locale->text('Vendor saved!')
43 # $locale->text('Customer deleted!')
44 # $locale->text('Cannot delete customer!')
45 # $locale->text('Vendor deleted!')
46 # $locale->text('Cannot delete vendor!')
47
48 use CGI::Ajax;
49 use POSIX qw(strftime);
50
51 use SL::CT;
52 use SL::CVar;
53 use SL::ReportGenerator;
54
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/reportgenerator.pl";
57
58 1;
59
60 # end of main
61
62 sub add {
63   $lxdebug->enter_sub();
64
65   $auth->assert('customer_vendor_edit');
66
67   $form->{title}    = "Add";
68   $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
69
70   CT->populate_drop_down_boxes(\%myconfig, \%$form);
71
72   &form_header;
73   &form_footer;
74
75   $lxdebug->leave_sub();
76 }
77
78 sub search {
79   $lxdebug->enter_sub();
80
81   $auth->assert('customer_vendor_edit');
82
83   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
84
85   $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
86   $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
87
88   $form->{CUSTOM_VARIABLES}                  = CVar->get_configs('module' => 'CT');
89   ($form->{CUSTOM_VARIABLES_FILTER_CODE},
90    $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables'      => $form->{CUSTOM_VARIABLES},
91                                                                            'include_prefix' => 'l_',
92                                                                            'include_value'  => 'Y');
93
94   $form->{jsscript} = 1;
95   $form->{title}    = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
96   $form->{fokus}    = 'Form.name';
97
98   $form->header();
99   print $form->parse_html_template('ct/search');
100
101   $lxdebug->leave_sub();
102 }
103
104 sub list_names {
105   $lxdebug->enter_sub();
106
107   $auth->assert('customer_vendor_edit');
108
109   $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
110
111   CT->search(\%myconfig, \%$form);
112
113   my $cvar_configs = CVar->get_configs('module' => 'CT');
114
115   my @options;
116   if ($form->{status} eq 'all') {
117     push @options, $locale->text('All');
118   } elsif ($form->{status} eq 'orphaned') {
119     push @options, $locale->text('Orphaned');
120   }
121
122   push @options, $locale->text('Name') . " : $form->{name}"                    if $form->{name};
123   push @options, $locale->text('Contact') . " : $form->{contact}"              if $form->{contact};
124   push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
125   push @options, $locale->text('E-mail') . " : $form->{email}"                 if $form->{email};
126
127   my @columns = (
128     'id',        'name',  "$form->{db}number", 'address',  'contact',  'phone',
129     'fax',       'email', 'taxnumber',         'sic_code', 'business', 'invnumber',
130     'ordnumber', 'quonumber'
131   );
132
133   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
134   my %column_defs_cvars            = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
135
136   push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
137
138   my %column_defs = (
139     'id'                => { 'text' => $locale->text('ID'), },
140     "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
141     'name'              => { 'text' => $locale->text('Name'), },
142     'address'           => { 'text' => $locale->text('Address'), },
143     'contact'           => { 'text' => $locale->text('Contact'), },
144     'phone'             => { 'text' => $locale->text('Phone'), },
145     'fax'               => { 'text' => $locale->text('Fax'), },
146     'email'             => { 'text' => $locale->text('E-mail'), },
147     'cc'                => { 'text' => $locale->text('Cc'), },
148     'taxnumber'         => { 'text' => $locale->text('Tax Number'), },
149     'sic_code'          => { 'text' => $locale->text('SIC'), },
150     'business'          => { 'text' => $locale->text('Type of Business'), },
151     'invnumber'         => { 'text' => $locale->text('Invoice'), },
152     'ordnumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
153     'quonumber'         => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation')   : $locale->text('Request for Quotation'), },
154     %column_defs_cvars,
155   );
156
157   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
158
159   my @hidden_variables  = (qw(db status obsolete), map { "l_$_" } @columns);
160   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
161   my $callback          = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
162   $form->{callback}     = "$callback&sort=" . E($form->{sort});
163
164   map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
165
166   my ($ordertype, $quotationtype, $attachment_basename);
167   if ($form->{IS_CUSTOMER}) {
168     $form->{title}       = $locale->text('Customers');
169     $ordertype           = 'sales_order';
170     $quotationtype       = 'sales_quotation';
171     $attachment_basename = $locale->text('customer_list');
172
173   } else {
174     $form->{title}       = $locale->text('Vendors');
175     $ordertype           = 'purchase_order';
176     $quotationtype       = 'request_quotation';
177     $attachment_basename = $locale->text('vendor_list');
178   }
179
180   my $report = SL::ReportGenerator->new(\%myconfig, $form);
181
182   $report->set_options('top_info_text'         => join("\n", @options),
183                        'raw_bottom_info_text'  => $form->parse_html_template('ct/list_names_bottom'),
184                        'output_format'         => 'HTML',
185                        'title'                 => $form->{title},
186                        'attachment_basename'   => $attachment_basename . strftime('_%Y%m%d', localtime time),
187     );
188   $report->set_options_from_form();
189
190   $report->set_columns(%column_defs);
191   $report->set_column_order(@columns);
192
193   $report->set_export_options('list_names', @hidden_variables);
194
195   $report->set_sort_indicator($form->{sort}, 1);
196
197   CVar->add_custom_variables_to_report('module'         => 'CT',
198                                        'trans_id_field' => 'id',
199                                        'configs'        => $cvar_configs,
200                                        'column_defs'    => \%column_defs,
201                                        'data'           => $form->{CT});
202
203   my $previous_id;
204
205   foreach my $ref (@{ $form->{CT} }) {
206     my $row = { map { $_ => { 'data' => '' } } @columns };
207
208     if ($ref->{id} ne $previous_id) {
209       $previous_id = $ref->{id};
210       map { $row->{$_}->{data} = $ref->{$_} } @columns;
211
212       $row->{name}->{link}  = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
213       $row->{email}->{link} = 'mailto:' . E($ref->{email});
214     }
215
216     my $base_url              = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
217     $row->{invnumber}->{link} = $base_url;
218     $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
219     $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
220     my $column                = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
221     $row->{$column}->{data}   = $ref->{$column};
222
223     $report->add_data($row);
224   }
225
226   $report->generate_with_headers();
227
228   $lxdebug->leave_sub();
229 }
230
231 sub edit {
232   $lxdebug->enter_sub();
233
234   $auth->assert('customer_vendor_edit');
235
236   # show history button
237   $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
238   #/show hhistory button
239   
240   CT->get_tuple(\%myconfig, \%$form);
241   CT->populate_drop_down_boxes(\%myconfig, \%$form);
242
243   # format " into &quot;
244   map { $form->{$_} =~ s/\"/&quot;/g } keys %$form;
245
246   $form->{title} = "Edit";
247
248   # format discount
249   $form->{discount} *= 100;
250
251   &form_header;
252   &form_footer;
253
254   $lxdebug->leave_sub();
255 }
256
257 sub form_header {
258   $lxdebug->enter_sub();
259
260   $auth->assert('customer_vendor_edit');
261
262   $form->get_lists("employees" => "ALL_EMPLOYEES",
263                    "taxzones"  => "ALL_TAXZONES");
264   $form->get_pricegroup(\%myconfig, { all => 1 });
265
266   $form->{ALL_SALESMEN}   = $form->{ALL_EMPLOYEES};
267   $form->{taxincluded}    = ($form->{taxincluded}) ? "checked" : "";
268   $form->{is_admin}       = $myconfig{role} eq 'admin';
269   $form->{is_customer}    = $form->{db}     eq 'customer';
270   $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
271   $form->{shipto_label}   = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
272   $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
273   $form->{taxzone_id}     = 0                                                               if !$form->{id};
274   $form->{jsscript}       = 1;
275   $form->{fokus}          = "ct.greeting";
276   $form->{AJAX}           = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
277
278   unshift @{ $form->{SHIPTO} },   +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
279   unshift @{ $form->{CONTACTS} }, +{ cp_id     => '0', cp_name => $locale->text('New contact') };
280
281   $form->{title} = $form->{title_save} 
282                 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
283
284   CT->query_titles_and_greetings(\%myconfig, \%$form);
285   map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
286
287   $form->{NOTES} ||= [ ];
288
289   $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
290
291   CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
292
293   $form->header;
294   print $form->parse_html_template('ct/form_header');
295
296   $lxdebug->leave_sub();
297 }
298
299 sub form_footer {
300   $lxdebug->enter_sub();
301
302   $auth->assert('customer_vendor_edit');
303
304   print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
305                                                        is_customer => $form->{db}     eq 'customer' });
306   $lxdebug->leave_sub();
307 }
308
309 sub add_transaction {
310   $lxdebug->enter_sub();
311
312   $auth->assert('customer_vendor_edit & general_ledger');
313
314 #  # saving the history
315 #  if(!exists $form->{addition}) {
316 #       $form->{addition} = "ADD TRANSACTION";
317 #       $form->save_history($form->dbconnect(\%myconfig));
318 #  }
319 #  # /saving the history
320   
321   $form->isblank("name", $locale->text("Name missing!"));
322   if ($form->{"db"} eq "customer") {
323     CT->save_customer(\%myconfig, \%$form);
324   } else {
325     CT->save_vendor(\%myconfig, \%$form);
326   }
327
328   $form->{callback} = $form->escape($form->{callback}, 1);
329   $name = $form->escape("$form->{name}", 1);
330
331   $form->{callback} =
332     "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
333   $form->redirect;
334
335   $lxdebug->leave_sub();
336 }
337
338 sub save_and_ap_transaction {
339   $lxdebug->enter_sub();
340
341   $auth->assert('customer_vendor_edit & general_ledger');
342
343   $form->{script} = "ap.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_ar_transaction {
356   $lxdebug->enter_sub();
357
358   $auth->assert('customer_vendor_edit & general_ledger');
359
360   $form->{script} = "ar.pl";
361   # saving the history
362   if(!exists $form->{addition}) {
363     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
364         $form->{addition} = "SAVED";
365         $form->save_history($form->dbconnect(\%myconfig));
366   }
367   # /saving the history
368   &add_transaction;
369   $lxdebug->leave_sub();
370 }
371
372 sub save_and_invoice {
373   $lxdebug->enter_sub();
374
375   if ($form->{db} eq 'customer') {
376     $auth->assert('customer_vendor_edit & invoice_edit');
377   } else {
378     $auth->assert('customer_vendor_edit & vendor_invoice_edit');
379   }
380
381   $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
382   $form->{type} = "invoice";
383   # saving the history
384   if(!exists $form->{addition}) {
385     $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
386         $form->{addition} = "SAVED";
387         $form->save_history($form->dbconnect(\%myconfig));
388   }
389   # /saving the history
390   &add_transaction;
391   $lxdebug->leave_sub();
392 }
393
394 sub save_and_rfq {
395   $lxdebug->enter_sub();
396
397   $auth->assert('customer_vendor_edit & request_quotation_edit');
398
399   $form->{script} = "oe.pl";
400   $form->{type}   = "request_quotation";
401   # saving the history
402   if(!exists $form->{addition}) {
403     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
404         $form->{addition} = "SAVED";
405         $form->save_history($form->dbconnect(\%myconfig));
406   }
407   # /saving the history
408   &add_transaction;
409   $lxdebug->leave_sub();
410 }
411
412 sub save_and_quotation {
413   $lxdebug->enter_sub();
414
415   $auth->assert('customer_vendor_edit & sales_quotation_edit');
416
417   $form->{script} = "oe.pl";
418   $form->{type}   = "sales_quotation";
419   # saving the history
420   if(!exists $form->{addition}) {
421     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
422         $form->{addition} = "SAVED";
423         $form->save_history($form->dbconnect(\%myconfig));
424   }
425   # /saving the history
426   &add_transaction;
427   $lxdebug->leave_sub();
428 }
429
430 sub save_and_order {
431   $lxdebug->enter_sub();
432
433   $auth->assert('customer_vendor_edit & sales_order_edit');
434
435   $form->{script} = "oe.pl";
436   $form->{type}   =
437     ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
438   # saving the history
439   if(!exists $form->{addition}) {
440     $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
441         $form->{addition} = "SAVED";
442         $form->save_history($form->dbconnect(\%myconfig));
443   }
444   # /saving the history
445   &add_transaction;
446   $lxdebug->leave_sub();
447 }
448
449 sub save_and_close {
450   $lxdebug->enter_sub();
451
452   $auth->assert('customer_vendor_edit');
453
454   $msg = ucfirst $form->{db};
455   $imsg .= " saved!";
456
457   $form->isblank("name", $locale->text("Name missing!"));
458   if ($form->{"db"} eq "customer") {
459     $rc = CT->save_customer(\%myconfig, \%$form);
460   } else {
461     $rc = CT->save_vendor(\%myconfig, \%$form);
462   }
463   if ($rc == 3) {
464     $form->error($locale->text('customernumber not unique!'));
465   }
466   # saving the history
467   if(!exists $form->{addition}) {
468     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
469     $form->{addition} = "SAVED";
470         $form->save_history($form->dbconnect(\%myconfig));
471   }
472   # /saving the history
473   $form->redirect($locale->text($msg));
474
475   $lxdebug->leave_sub();
476 }
477
478 sub save {
479   $lxdebug->enter_sub();
480
481   $auth->assert('customer_vendor_edit');
482
483   $msg = ucfirst $form->{db};
484   $imsg .= " saved!";
485
486   $form->isblank("name", $locale->text("Name missing!"));
487
488   my $res;
489   if ($form->{"db"} eq "customer") {
490     $res = CT->save_customer(\%myconfig, \%$form);
491   } else {
492     $res = CT->save_vendor(\%myconfig, \%$form);
493   }
494
495   if (3 == $res) {
496     if ($form->{"db"} eq "customer") {
497       $form->error($locale->text('This customer number is already in use.'));
498     } else {
499       $form->error($locale->text('This vendor number is already in use.'));
500     }
501   }
502   # saving the history
503   if(!exists $form->{addition}) {
504     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
505         $form->{addition} = "SAVED";
506         $form->save_history($form->dbconnect(\%myconfig));
507   }
508   # /saving the history
509   &edit;
510   exit;
511   $lxdebug->leave_sub();
512 }
513
514 sub delete {
515   $lxdebug->enter_sub();
516
517   $auth->assert('customer_vendor_edit');
518
519   CT->delete(\%myconfig, \%$form);
520
521   $msg = ucfirst $form->{db};
522   $msg .= " deleted!";
523   # saving the history
524   if(!exists $form->{addition}) {
525     $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
526         $form->{addition} = "DELETED";
527         $form->save_history($form->dbconnect(\%myconfig));
528   }
529   # /saving the history 
530   $form->redirect($locale->text($msg));
531
532   $msg = "Cannot delete $form->{db}";
533   $form->error($locale->text($msg));
534
535   $lxdebug->leave_sub();
536 }
537
538 sub display {
539   $lxdebug->enter_sub();
540
541   $auth->assert('customer_vendor_edit');
542
543   &form_header();
544   &form_footer();
545
546   $lxdebug->leave_sub();
547 }
548
549 sub update {
550   $lxdebug->enter_sub();
551
552   $auth->assert('customer_vendor_edit');
553
554   &display();
555   $lxdebug->leave_sub();
556 }
557
558 sub get_contact {
559   $lxdebug->enter_sub();
560
561   $auth->assert('customer_vendor_edit');
562
563   CT->get_contact(\%myconfig, \%$form);
564   print $cgi->header(), join '__pjx__', map $form->{"cp_$_"}, 
565     qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
566   $lxdebug->leave_sub();
567
568 }
569
570 sub get_shipto {
571   $lxdebug->enter_sub();
572
573   $auth->assert('customer_vendor_edit');
574
575   CT->get_shipto(\%myconfig, \%$form);
576   print $cgi->header(),  join '__pjx__', map $form->{"shipto$_"},
577     qw(name department_1 department_2 street zipcode city country contact phone fax email used);
578   $lxdebug->leave_sub();
579
580 }
581
582 sub get_delivery {
583   $lxdebug->enter_sub();
584
585   $auth->assert('customer_vendor_edit');
586
587   CT->get_delivery(\%myconfig, \%$form );
588
589   print $cgi->header(), $form->parse_html_template('ct/get_delivery');
590
591   $lxdebug->leave_sub();
592 }
593
594 sub delete_shipto {
595   $main::lxdebug->enter_sub();
596
597   $auth->assert('customer_vendor_edit');
598
599   CT->get_shipto(\%myconfig, \%$form);
600
601   unless ($form->{shiptoused}) {
602     CT->delete_shipto($form->{shipto_id});
603     @$form{ grep /^shipto/, keys %$form } = undef;
604   }
605
606   edit();
607
608   $main::lxdebug->leave_sub();
609 }
610
611 sub delete_contact {
612   $main::lxdebug->enter_sub();
613
614   $auth->assert('customer_vendor_edit');
615
616   CT->get_contact(\%myconfig, \%$form);
617
618   unless ($form->{cp_used}) {
619     CT->delete_shipto($form->{cp_id});
620     @$form{ grep /^cp_/, keys %$form } = undef;
621   }
622
623   edit();
624
625   $main::lxdebug->leave_sub();
626 }
627
628 sub continue { call_sub($form->{nextsub}); }