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