1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 1998-2002
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
15 # Contributors: Reed White <alta@alta-research.com>
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.
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 #======================================================================
31 # customer/vendor module
33 #======================================================================
35 # $locale->text('Customers')
36 # $locale->text('Vendors')
37 # $locale->text('Add Customer')
38 # $locale->text('Add Vendor')
42 use POSIX qw(strftime);
45 use SL::ReportGenerator;
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
55 $lxdebug->enter_sub();
57 $form->{title} = "Add";
60 "$form->{script}?action=add&db=$form->{db}&login=$form->{login}&password=$form->{password}"
61 unless $form->{callback};
63 CT->populate_drop_down_boxes(\%myconfig, \%$form);
68 $lxdebug->leave_sub();
72 $lxdebug->enter_sub();
74 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
76 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
77 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
79 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
80 $form->{fokus} = 'Form.name';
83 print $form->parse_html_template('ct/search');
85 $lxdebug->leave_sub();
89 $lxdebug->enter_sub();
91 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
93 CT->search(\%myconfig, \%$form);
96 if ($form->{status} eq 'all') {
97 push @options, $locale->text('All');
99 } elsif ($form->{status} eq 'orphaned') {
100 push @options, $locale->text('Orphaned');
104 push @options, $locale->text('Name') . " : $form->{name}";
106 if ($form->{contact}) {
107 push @options, $locale->text('Contact') . " : $form->{contact}";
109 if ($form->{"$form->{db}number"}) {
110 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|;
112 if ($form->{email}) {
113 push @options, $locale->text('E-mail') . " : $form->{email}";
117 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
118 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
119 'ordnumber', 'quonumber'
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'), },
140 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
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});
147 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
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');
157 $form->{title} = $locale->text('Vendors');
158 $ordertype = 'purchase_order';
159 $quotationtype = 'request_quotation';
160 $attachment_basename = $locale->text('vendor_list');
163 my $report = SL::ReportGenerator->new(\%myconfig, $form);
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),
171 $report->set_options_from_form();
173 $report->set_columns(%column_defs);
174 $report->set_column_order(@columns);
176 $report->set_export_options('list_names', @hidden_variables);
178 $report->set_sort_indicator($form->{sort}, 1);
182 foreach my $ref (@{ $form->{CT} }) {
183 my $row = { map { $_ => { 'data' => '' } } @columns };
185 if ($ref->{id} ne $previous_id) {
186 $previous_id = $ref->{id};
187 map { $row->{$_}->{data} = $ref->{$_} } @columns;
189 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
190 $row->{email}->{link} = 'mailto:' . E($ref->{email});
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};
200 $report->add_data($row);
203 $report->generate_with_headers();
205 $lxdebug->leave_sub();
209 $lxdebug->enter_sub();
211 # show history button
212 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
213 #/show hhistory button
215 # $locale->text('Edit Customer')
216 # $locale->text('Edit Vendor')
218 CT->get_tuple(\%myconfig, \%$form);
219 CT->populate_drop_down_boxes(\%myconfig, \%$form);
221 # format " into "
222 map { $form->{$_} =~ s/\"/"/g } keys %$form;
224 $form->{title} = "Edit";
227 $form->{discount} *= 100;
232 $lxdebug->leave_sub();
236 $lxdebug->enter_sub();
238 $form->get_lists("employees" => "ALL_SALESMEN",
239 "taxzones" => "ALL_TAXZONES");
240 $form->get_pricegroup(\%myconfig, { all => 1 });
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";
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') };
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);
258 $form->{title} = $form->{title_save}
259 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
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);
267 print $form->parse_html_template('ct/form_header');
269 $lxdebug->leave_sub();
273 $lxdebug->enter_sub();
275 $label = ucfirst $form->{db};
277 ($form->{db} eq 'customer')
278 ? $locale->text('Save and Quotation')
279 : $locale->text('Save and RFQ');
281 ($form->{db} eq 'customer')
282 ? $locale->text('Save and AR Transaction')
283 : $locale->text('Save and AP Transaction');
285 ##<input class=submit type=submit name=action value="|.$locale->text("Save and Quotation").qq|">
286 ##<input class=submit type=submit name=action value="|.$locale->text("Save and RFQ").qq|">
287 ##<input class=submit type=submit name=action value="|.$locale->text("Save and AR Transaction").qq|">
288 ##<input class=submit type=submit name=action value="|.$locale->text("Save and AP Transaction").qq|">
291 <input name=id type=hidden id=cvid value=$form->{id}>
292 <input name=business_save type=hidden value="$form->{selectbusiness}">
293 <input name=title_save type=hidden value="$form->{title}">
295 <input type=hidden name=login value=$form->{login}>
296 <input type=hidden name=password value=$form->{password}>
298 <input type=hidden name=callback value="$form->{callback}">
299 <input type=hidden name=db id=db value=$form->{db}>
304 <input class=submit type=submit name=action accesskey="s" value="|
305 . $locale->text("Save") . qq|">
306 <input class=submit type=submit name=action accesskey="s" value="|
307 . $locale->text("Save and Close") . qq|">
308 <input class=submit type=submit name=action value="$arap">
309 <input class=submit type=submit name=action value="|
310 . $locale->text("Save and Invoice") . qq|">
311 <input class=submit type=submit name=action value="|
312 . $locale->text("Save and Order") . qq|">
313 <input class=submit type=submit name=action value="$quotation">
316 if ($form->{id} && $form->{status} eq 'orphaned') {
317 print qq|<input class=submit type=submit name=action value="|
318 . $locale->text('Delete')
322 # button for saving history
323 if($form->{id} ne "") {
325 <input type=button class=submit onclick=set_history_window(|
327 . qq|); name=history id=history value=|
328 . $locale->text('history')
331 # /button for saving history
336 <script type="text/javascript">
337 //Start Tab Content script for UL with id="maintab" Separate multiple ids each with a comma.
338 initializetabcontent("maintab")
344 $lxdebug->leave_sub();
347 sub add_transaction {
348 $lxdebug->enter_sub();
350 # # saving the history
351 # if(!exists $form->{addition}) {
352 # $form->{addition} = "ADD TRANSACTION";
353 # $form->save_history($form->dbconnect(\%myconfig));
355 # # /saving the history
357 $form->isblank("name", $locale->text("Name missing!"));
358 if ($form->{"db"} eq "customer") {
359 CT->save_customer(\%myconfig, \%$form);
361 CT->save_vendor(\%myconfig, \%$form);
364 $form->{callback} = $form->escape($form->{callback}, 1);
365 $name = $form->escape("$form->{name}", 1);
368 "$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}";
371 $lxdebug->leave_sub();
374 sub save_and_ap_transaction {
375 $lxdebug->enter_sub();
377 $form->{script} = "ap.pl";
379 if(!exists $form->{addition}) {
380 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
381 $form->{addition} = "SAVED";
382 $form->save_history($form->dbconnect(\%myconfig));
384 # /saving the history
386 $lxdebug->leave_sub();
389 sub save_and_ar_transaction {
390 $lxdebug->enter_sub();
392 $form->{script} = "ar.pl";
394 if(!exists $form->{addition}) {
395 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
396 $form->{addition} = "SAVED";
397 $form->save_history($form->dbconnect(\%myconfig));
399 # /saving the history
401 $lxdebug->leave_sub();
404 sub save_and_invoice {
405 $lxdebug->enter_sub();
407 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
408 $form->{type} = "invoice";
410 if(!exists $form->{addition}) {
411 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
412 $form->{addition} = "SAVED";
413 $form->save_history($form->dbconnect(\%myconfig));
415 # /saving the history
417 $lxdebug->leave_sub();
421 $lxdebug->enter_sub();
423 $form->{script} = "oe.pl";
424 $form->{type} = "request_quotation";
426 if(!exists $form->{addition}) {
427 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
428 $form->{addition} = "SAVED";
429 $form->save_history($form->dbconnect(\%myconfig));
431 # /saving the history
433 $lxdebug->leave_sub();
436 sub save_and_quotation {
437 $lxdebug->enter_sub();
439 $form->{script} = "oe.pl";
440 $form->{type} = "sales_quotation";
442 if(!exists $form->{addition}) {
443 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
444 $form->{addition} = "SAVED";
445 $form->save_history($form->dbconnect(\%myconfig));
447 # /saving the history
449 $lxdebug->leave_sub();
453 $lxdebug->enter_sub();
455 $form->{script} = "oe.pl";
457 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
459 if(!exists $form->{addition}) {
460 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
461 $form->{addition} = "SAVED";
462 $form->save_history($form->dbconnect(\%myconfig));
464 # /saving the history
466 $lxdebug->leave_sub();
470 $lxdebug->enter_sub();
472 # $locale->text('Customer saved!')
473 # $locale->text('Vendor saved!')
475 $msg = ucfirst $form->{db};
478 $form->isblank("name", $locale->text("Name missing!"));
479 if ($form->{"db"} eq "customer") {
480 $rc = CT->save_customer(\%myconfig, \%$form);
482 $rc = CT->save_vendor(\%myconfig, \%$form);
485 $form->error($locale->text('customernumber not unique!'));
488 if(!exists $form->{addition}) {
489 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
490 $form->{addition} = "SAVED";
491 $form->save_history($form->dbconnect(\%myconfig));
493 # /saving the history
494 $form->redirect($locale->text($msg));
496 $lxdebug->leave_sub();
500 $lxdebug->enter_sub();
502 # $locale->text('Customer saved!')
503 # $locale->text('Vendor saved!')
505 $msg = ucfirst $form->{db};
508 $form->isblank("name", $locale->text("Name missing!"));
511 if ($form->{"db"} eq "customer") {
512 $res = CT->save_customer(\%myconfig, \%$form);
514 $res = CT->save_vendor(\%myconfig, \%$form);
518 if ($form->{"db"} eq "customer") {
519 $form->error($locale->text('This customer number is already in use.'));
521 $form->error($locale->text('This vendor number is already in use.'));
525 if(!exists $form->{addition}) {
526 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
527 $form->{addition} = "SAVED";
528 $form->save_history($form->dbconnect(\%myconfig));
530 # /saving the history
533 $lxdebug->leave_sub();
537 $lxdebug->enter_sub();
539 # $locale->text('Customer deleted!')
540 # $locale->text('Cannot delete customer!')
541 # $locale->text('Vendor deleted!')
542 # $locale->text('Cannot delete vendor!')
544 CT->delete(\%myconfig, \%$form);
546 $msg = ucfirst $form->{db};
549 if(!exists $form->{addition}) {
550 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
551 $form->{addition} = "DELETED";
552 $form->save_history($form->dbconnect(\%myconfig));
554 # /saving the history
555 $form->redirect($locale->text($msg));
557 $msg = "Cannot delete $form->{db}";
558 $form->error($locale->text($msg));
560 $lxdebug->leave_sub();
564 $lxdebug->enter_sub();
569 $lxdebug->leave_sub();
573 $lxdebug->enter_sub();
576 $lxdebug->leave_sub();
580 $lxdebug->enter_sub();
582 CT->get_contact(\%myconfig, \%$form);
585 $result = "$form->{cp_name}";
586 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);
589 $lxdebug->leave_sub();
594 $lxdebug->enter_sub();
596 CT->get_shipto(\%myconfig, \%$form);
599 $result = "$form->{shiptoname}";
600 map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
603 $lxdebug->leave_sub();
608 $lxdebug->enter_sub();
610 CT->get_delivery(\%myconfig, \%$form );
613 $form->sort_columns(shiptoname,
624 $column_header{shiptoname} =
625 qq|<th class=listheading>| . $locale->text('Shipping Address') . qq|</th>|;
626 $column_header{invnumber} =
627 qq|<th class=listheading>|. $locale->text('Invoice'). qq|</th>|;
628 $column_header{ordnumber} =
629 qq|<th class=listheading>|. $locale->text('Order'). qq|</th>|;
630 $column_header{transdate} =
631 qq|<th class=listheading>| . $locale->text('Invdate') . qq|</th>|;
632 $column_header{description} =
633 qq|<th class=listheading>| . $locale->text('Description') . qq|</th>|;
634 $column_header{qty} =
635 qq|<th class=listheading>| . $locale->text('Qty') . qq|</th>|;
636 $column_header{unit} =
637 qq|<th class=listheading>| . $locale->text('Unit') . qq|</th>|;
638 $column_header{sellprice} =
639 qq|<th class=listheading>| . $locale->text('Sell Price') . qq|</th>|;
646 <tr class=listheading>
649 map { $result .= "$column_header{$_}\n" } @column_index;
656 foreach $ref (@{ $form->{DELIVERY} }) {
658 if ($ref->{shiptoname} eq $sameshiptoname) {
659 map { $column_data{$_} = "<td>$ref->{$_} </td>" } @column_index;
660 $column_data{shiptoname} = "<td> </td>";
662 map { $column_data{$_} = "<td>$ref->{$_} </td>" } @column_index;
664 $column_data{sellprice} = "<td>". $form->format_amount(\%myconfig,$ref->{sellprice},2)." </td>";
671 map { $result .= "$column_data{$_}\n" } @column_index;
677 $sameshiptoname = $ref->{shiptoname};
689 $lxdebug->leave_sub();
693 sub continue { call_sub($form->{nextsub}); }