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')
41 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 $auth->assert('customer_vendor_edit');
59 $form->{title} = "Add";
62 "$form->{script}?action=add&db=$form->{db}"
63 unless $form->{callback};
65 CT->populate_drop_down_boxes(\%myconfig, \%$form);
70 $lxdebug->leave_sub();
74 $lxdebug->enter_sub();
76 $auth->assert('customer_vendor_edit');
78 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
80 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
81 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
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');
89 $form->{jsscript} = 1;
90 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
91 $form->{fokus} = 'Form.name';
94 print $form->parse_html_template('ct/search');
96 $lxdebug->leave_sub();
100 $lxdebug->enter_sub();
102 $auth->assert('customer_vendor_edit');
104 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
106 CT->search(\%myconfig, \%$form);
108 my $cvar_configs = CVar->get_configs('module' => 'CT');
111 if ($form->{status} eq 'all') {
112 push @options, $locale->text('All');
114 } elsif ($form->{status} eq 'orphaned') {
115 push @options, $locale->text('Orphaned');
119 push @options, $locale->text('Name') . " : $form->{name}";
121 if ($form->{contact}) {
122 push @options, $locale->text('Contact') . " : $form->{contact}";
124 if ($form->{"$form->{db}number"}) {
125 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|;
127 if ($form->{email}) {
128 push @options, $locale->text('E-mail') . " : $form->{email}";
132 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
133 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
134 'ordnumber', 'quonumber'
137 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
138 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
140 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
143 'id' => { 'text' => $locale->text('ID'), },
144 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
145 'name' => { 'text' => $locale->text('Name'), },
146 'address' => { 'text' => $locale->text('Address'), },
147 'contact' => { 'text' => $locale->text('Contact'), },
148 'phone' => { 'text' => $locale->text('Phone'), },
149 'fax' => { 'text' => $locale->text('Fax'), },
150 'email' => { 'text' => $locale->text('E-mail'), },
151 'cc' => { 'text' => $locale->text('Cc'), },
152 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
153 'sic_code' => { 'text' => $locale->text('SIC'), },
154 'business' => { 'text' => $locale->text('Type of Business'), },
155 'invnumber' => { 'text' => $locale->text('Invoice'), },
156 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
157 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
161 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
163 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
164 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
165 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
166 $form->{callback} = "$callback&sort=" . E($form->{sort});
168 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
170 my ($ordertype, $quotationtype, $attachment_basename);
171 if ($form->{IS_CUSTOMER}) {
172 $form->{title} = $locale->text('Customers');
173 $ordertype = 'sales_order';
174 $quotationtype = 'sales_quotation';
175 $attachment_basename = $locale->text('customer_list');
178 $form->{title} = $locale->text('Vendors');
179 $ordertype = 'purchase_order';
180 $quotationtype = 'request_quotation';
181 $attachment_basename = $locale->text('vendor_list');
184 my $report = SL::ReportGenerator->new(\%myconfig, $form);
186 $report->set_options('top_info_text' => join("\n", @options),
187 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
188 'output_format' => 'HTML',
189 'title' => $form->{title},
190 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
192 $report->set_options_from_form();
194 $report->set_columns(%column_defs);
195 $report->set_column_order(@columns);
197 $report->set_export_options('list_names', @hidden_variables);
199 $report->set_sort_indicator($form->{sort}, 1);
201 CVar->add_custom_variables_to_report('module' => 'CT',
202 'trans_id_field' => 'id',
203 'configs' => $cvar_configs,
204 'column_defs' => \%column_defs,
205 'data' => $form->{CT});
209 foreach my $ref (@{ $form->{CT} }) {
210 my $row = { map { $_ => { 'data' => '' } } @columns };
212 if ($ref->{id} ne $previous_id) {
213 $previous_id = $ref->{id};
214 map { $row->{$_}->{data} = $ref->{$_} } @columns;
216 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
217 $row->{email}->{link} = 'mailto:' . E($ref->{email});
220 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
221 $row->{invnumber}->{link} = $base_url;
222 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
223 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
224 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
225 $row->{$column}->{data} = $ref->{$column};
227 $report->add_data($row);
230 $report->generate_with_headers();
232 $lxdebug->leave_sub();
236 $lxdebug->enter_sub();
238 $auth->assert('customer_vendor_edit');
240 # show history button
241 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
242 #/show hhistory button
244 # $locale->text('Edit Customer')
245 # $locale->text('Edit Vendor')
247 CT->get_tuple(\%myconfig, \%$form);
248 CT->populate_drop_down_boxes(\%myconfig, \%$form);
250 # format " into "
251 map { $form->{$_} =~ s/\"/"/g } keys %$form;
253 $form->{title} = "Edit";
256 $form->{discount} *= 100;
261 $lxdebug->leave_sub();
265 $lxdebug->enter_sub();
267 $auth->assert('customer_vendor_edit');
269 $form->get_lists("employees" => "ALL_EMPLOYEES",
270 "taxzones" => "ALL_TAXZONES");
271 $form->get_pricegroup(\%myconfig, { all => 1 });
273 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
274 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
275 $form->{is_admin} = $myconfig{role} eq 'admin';
276 $form->{is_customer} = $form->{db} eq 'customer';
277 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
278 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
279 $form->{taxzone_id} = 0 if !$form->{id};
280 $form->{jsscript} = 1;
281 $form->{fokus} = "ct.greeting";
283 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
284 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
286 push @{ $form->{AJAX} }, map {
287 new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" )
288 } qw(shipto contact delivery);
290 $form->{title} = $form->{title_save}
291 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
293 ## LINET: Create a drop-down box with all prior titles and greetings.
294 CT->query_titles_and_greetings(\%myconfig, \%$form);
295 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
298 $form->{NOTES} ||= [ ];
300 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
302 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
305 print $form->parse_html_template('ct/form_header');
307 $lxdebug->leave_sub();
311 $lxdebug->enter_sub();
313 $auth->assert('customer_vendor_edit');
315 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
316 is_customer => $form->{db} eq 'customer' });
317 $lxdebug->leave_sub();
320 sub add_transaction {
321 $lxdebug->enter_sub();
323 $auth->assert('customer_vendor_edit & general_ledger');
325 # # saving the history
326 # if(!exists $form->{addition}) {
327 # $form->{addition} = "ADD TRANSACTION";
328 # $form->save_history($form->dbconnect(\%myconfig));
330 # # /saving the history
332 $form->isblank("name", $locale->text("Name missing!"));
333 if ($form->{"db"} eq "customer") {
334 CT->save_customer(\%myconfig, \%$form);
336 CT->save_vendor(\%myconfig, \%$form);
339 $form->{callback} = $form->escape($form->{callback}, 1);
340 $name = $form->escape("$form->{name}", 1);
343 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
346 $lxdebug->leave_sub();
349 sub save_and_ap_transaction {
350 $lxdebug->enter_sub();
352 $auth->assert('customer_vendor_edit & general_ledger');
354 $form->{script} = "ap.pl";
356 if(!exists $form->{addition}) {
357 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
358 $form->{addition} = "SAVED";
359 $form->save_history($form->dbconnect(\%myconfig));
361 # /saving the history
363 $lxdebug->leave_sub();
366 sub save_and_ar_transaction {
367 $lxdebug->enter_sub();
369 $auth->assert('customer_vendor_edit & general_ledger');
371 $form->{script} = "ar.pl";
373 if(!exists $form->{addition}) {
374 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
375 $form->{addition} = "SAVED";
376 $form->save_history($form->dbconnect(\%myconfig));
378 # /saving the history
380 $lxdebug->leave_sub();
383 sub save_and_invoice {
384 $lxdebug->enter_sub();
386 if ($form->{db} eq 'customer') {
387 $auth->assert('customer_vendor_edit & invoice_edit');
389 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
392 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
393 $form->{type} = "invoice";
395 if(!exists $form->{addition}) {
396 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
397 $form->{addition} = "SAVED";
398 $form->save_history($form->dbconnect(\%myconfig));
400 # /saving the history
402 $lxdebug->leave_sub();
406 $lxdebug->enter_sub();
408 $auth->assert('customer_vendor_edit & request_quotation_edit');
410 $form->{script} = "oe.pl";
411 $form->{type} = "request_quotation";
413 if(!exists $form->{addition}) {
414 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
415 $form->{addition} = "SAVED";
416 $form->save_history($form->dbconnect(\%myconfig));
418 # /saving the history
420 $lxdebug->leave_sub();
423 sub save_and_quotation {
424 $lxdebug->enter_sub();
426 $auth->assert('customer_vendor_edit & sales_quotation_edit');
428 $form->{script} = "oe.pl";
429 $form->{type} = "sales_quotation";
431 if(!exists $form->{addition}) {
432 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
433 $form->{addition} = "SAVED";
434 $form->save_history($form->dbconnect(\%myconfig));
436 # /saving the history
438 $lxdebug->leave_sub();
442 $lxdebug->enter_sub();
444 $auth->assert('customer_vendor_edit & sales_order_edit');
446 $form->{script} = "oe.pl";
448 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
450 if(!exists $form->{addition}) {
451 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
452 $form->{addition} = "SAVED";
453 $form->save_history($form->dbconnect(\%myconfig));
455 # /saving the history
457 $lxdebug->leave_sub();
461 $lxdebug->enter_sub();
463 $auth->assert('customer_vendor_edit');
465 # $locale->text('Customer saved!')
466 # $locale->text('Vendor saved!')
468 $msg = ucfirst $form->{db};
471 $form->isblank("name", $locale->text("Name missing!"));
472 if ($form->{"db"} eq "customer") {
473 $rc = CT->save_customer(\%myconfig, \%$form);
475 $rc = CT->save_vendor(\%myconfig, \%$form);
478 $form->error($locale->text('customernumber not unique!'));
481 if(!exists $form->{addition}) {
482 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
483 $form->{addition} = "SAVED";
484 $form->save_history($form->dbconnect(\%myconfig));
486 # /saving the history
487 $form->redirect($locale->text($msg));
489 $lxdebug->leave_sub();
493 $lxdebug->enter_sub();
495 $auth->assert('customer_vendor_edit');
497 # $locale->text('Customer saved!')
498 # $locale->text('Vendor saved!')
500 $msg = ucfirst $form->{db};
503 $form->isblank("name", $locale->text("Name missing!"));
506 if ($form->{"db"} eq "customer") {
507 $res = CT->save_customer(\%myconfig, \%$form);
509 $res = CT->save_vendor(\%myconfig, \%$form);
513 if ($form->{"db"} eq "customer") {
514 $form->error($locale->text('This customer number is already in use.'));
516 $form->error($locale->text('This vendor number is already in use.'));
520 if(!exists $form->{addition}) {
521 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
522 $form->{addition} = "SAVED";
523 $form->save_history($form->dbconnect(\%myconfig));
525 # /saving the history
528 $lxdebug->leave_sub();
532 $lxdebug->enter_sub();
534 $auth->assert('customer_vendor_edit');
536 # $locale->text('Customer deleted!')
537 # $locale->text('Cannot delete customer!')
538 # $locale->text('Vendor deleted!')
539 # $locale->text('Cannot delete vendor!')
541 CT->delete(\%myconfig, \%$form);
543 $msg = ucfirst $form->{db};
546 if(!exists $form->{addition}) {
547 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
548 $form->{addition} = "DELETED";
549 $form->save_history($form->dbconnect(\%myconfig));
551 # /saving the history
552 $form->redirect($locale->text($msg));
554 $msg = "Cannot delete $form->{db}";
555 $form->error($locale->text($msg));
557 $lxdebug->leave_sub();
561 $lxdebug->enter_sub();
563 $auth->assert('customer_vendor_edit');
568 $lxdebug->leave_sub();
572 $lxdebug->enter_sub();
574 $auth->assert('customer_vendor_edit');
577 $lxdebug->leave_sub();
581 $lxdebug->enter_sub();
583 $auth->assert('customer_vendor_edit');
585 CT->get_contact(\%myconfig, \%$form);
587 $result = "$form->{cp_name}";
588 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 print $cgi->header(), $result;
590 $lxdebug->leave_sub();
595 $lxdebug->enter_sub();
597 $auth->assert('customer_vendor_edit');
599 CT->get_shipto(\%myconfig, \%$form);
601 $result = "$form->{shiptoname}";
602 map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
603 print $cgi->header(), $result;
604 $lxdebug->leave_sub();
609 $lxdebug->enter_sub();
611 $auth->assert('customer_vendor_edit');
613 CT->get_delivery(\%myconfig, \%$form );
615 print $cgi->header(), $form->parse_html_template('ct/get_delivery');
617 $lxdebug->leave_sub();
620 sub continue { call_sub($form->{nextsub}); }