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');
113 } elsif ($form->{status} eq 'orphaned') {
114 push @options, $locale->text('Orphaned');
117 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
118 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
119 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
120 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
123 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
124 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
125 'ordnumber', 'quonumber'
128 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
129 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
131 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
134 'id' => { 'text' => $locale->text('ID'), },
135 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
136 'name' => { 'text' => $locale->text('Name'), },
137 'address' => { 'text' => $locale->text('Address'), },
138 'contact' => { 'text' => $locale->text('Contact'), },
139 'phone' => { 'text' => $locale->text('Phone'), },
140 'fax' => { 'text' => $locale->text('Fax'), },
141 'email' => { 'text' => $locale->text('E-mail'), },
142 'cc' => { 'text' => $locale->text('Cc'), },
143 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
144 'sic_code' => { 'text' => $locale->text('SIC'), },
145 'business' => { 'text' => $locale->text('Type of Business'), },
146 'invnumber' => { 'text' => $locale->text('Invoice'), },
147 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
148 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
152 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
154 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
155 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
156 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
157 $form->{callback} = "$callback&sort=" . E($form->{sort});
159 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
161 my ($ordertype, $quotationtype, $attachment_basename);
162 if ($form->{IS_CUSTOMER}) {
163 $form->{title} = $locale->text('Customers');
164 $ordertype = 'sales_order';
165 $quotationtype = 'sales_quotation';
166 $attachment_basename = $locale->text('customer_list');
169 $form->{title} = $locale->text('Vendors');
170 $ordertype = 'purchase_order';
171 $quotationtype = 'request_quotation';
172 $attachment_basename = $locale->text('vendor_list');
175 my $report = SL::ReportGenerator->new(\%myconfig, $form);
177 $report->set_options('top_info_text' => join("\n", @options),
178 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
179 'output_format' => 'HTML',
180 'title' => $form->{title},
181 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
183 $report->set_options_from_form();
185 $report->set_columns(%column_defs);
186 $report->set_column_order(@columns);
188 $report->set_export_options('list_names', @hidden_variables);
190 $report->set_sort_indicator($form->{sort}, 1);
192 CVar->add_custom_variables_to_report('module' => 'CT',
193 'trans_id_field' => 'id',
194 'configs' => $cvar_configs,
195 'column_defs' => \%column_defs,
196 'data' => $form->{CT});
200 foreach my $ref (@{ $form->{CT} }) {
201 my $row = { map { $_ => { 'data' => '' } } @columns };
203 if ($ref->{id} ne $previous_id) {
204 $previous_id = $ref->{id};
205 map { $row->{$_}->{data} = $ref->{$_} } @columns;
207 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
208 $row->{email}->{link} = 'mailto:' . E($ref->{email});
211 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
212 $row->{invnumber}->{link} = $base_url;
213 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
214 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
215 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
216 $row->{$column}->{data} = $ref->{$column};
218 $report->add_data($row);
221 $report->generate_with_headers();
223 $lxdebug->leave_sub();
227 $lxdebug->enter_sub();
229 $auth->assert('customer_vendor_edit');
231 # show history button
232 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
233 #/show hhistory button
235 # $locale->text('Edit Customer')
236 # $locale->text('Edit Vendor')
238 CT->get_tuple(\%myconfig, \%$form);
239 CT->populate_drop_down_boxes(\%myconfig, \%$form);
241 # format " into "
242 map { $form->{$_} =~ s/\"/"/g } keys %$form;
244 $form->{title} = "Edit";
247 $form->{discount} *= 100;
252 $lxdebug->leave_sub();
256 $lxdebug->enter_sub();
258 $auth->assert('customer_vendor_edit');
260 $form->get_lists("employees" => "ALL_EMPLOYEES",
261 "taxzones" => "ALL_TAXZONES");
262 $form->get_pricegroup(\%myconfig, { all => 1 });
264 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
265 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
266 $form->{is_admin} = $myconfig{role} eq 'admin';
267 $form->{is_customer} = $form->{db} eq 'customer';
268 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
269 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
270 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
271 $form->{taxzone_id} = 0 if !$form->{id};
272 $form->{jsscript} = 1;
273 $form->{fokus} = "ct.greeting";
275 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
276 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
278 push @{ $form->{AJAX} }, map {
279 new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" )
280 } qw(shipto contact delivery);
282 $form->{title} = $form->{title_save}
283 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
285 ## LINET: Create a drop-down box with all prior titles and greetings.
286 CT->query_titles_and_greetings(\%myconfig, \%$form);
287 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
290 $form->{NOTES} ||= [ ];
292 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
294 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
297 print $form->parse_html_template('ct/form_header');
299 $lxdebug->leave_sub();
303 $lxdebug->enter_sub();
305 $auth->assert('customer_vendor_edit');
307 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
308 is_customer => $form->{db} eq 'customer' });
309 $lxdebug->leave_sub();
312 sub add_transaction {
313 $lxdebug->enter_sub();
315 $auth->assert('customer_vendor_edit & general_ledger');
317 # # saving the history
318 # if(!exists $form->{addition}) {
319 # $form->{addition} = "ADD TRANSACTION";
320 # $form->save_history($form->dbconnect(\%myconfig));
322 # # /saving the history
324 $form->isblank("name", $locale->text("Name missing!"));
325 if ($form->{"db"} eq "customer") {
326 CT->save_customer(\%myconfig, \%$form);
328 CT->save_vendor(\%myconfig, \%$form);
331 $form->{callback} = $form->escape($form->{callback}, 1);
332 $name = $form->escape("$form->{name}", 1);
335 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
338 $lxdebug->leave_sub();
341 sub save_and_ap_transaction {
342 $lxdebug->enter_sub();
344 $auth->assert('customer_vendor_edit & general_ledger');
346 $form->{script} = "ap.pl";
348 if(!exists $form->{addition}) {
349 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
350 $form->{addition} = "SAVED";
351 $form->save_history($form->dbconnect(\%myconfig));
353 # /saving the history
355 $lxdebug->leave_sub();
358 sub save_and_ar_transaction {
359 $lxdebug->enter_sub();
361 $auth->assert('customer_vendor_edit & general_ledger');
363 $form->{script} = "ar.pl";
365 if(!exists $form->{addition}) {
366 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
367 $form->{addition} = "SAVED";
368 $form->save_history($form->dbconnect(\%myconfig));
370 # /saving the history
372 $lxdebug->leave_sub();
375 sub save_and_invoice {
376 $lxdebug->enter_sub();
378 if ($form->{db} eq 'customer') {
379 $auth->assert('customer_vendor_edit & invoice_edit');
381 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
384 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
385 $form->{type} = "invoice";
387 if(!exists $form->{addition}) {
388 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
389 $form->{addition} = "SAVED";
390 $form->save_history($form->dbconnect(\%myconfig));
392 # /saving the history
394 $lxdebug->leave_sub();
398 $lxdebug->enter_sub();
400 $auth->assert('customer_vendor_edit & request_quotation_edit');
402 $form->{script} = "oe.pl";
403 $form->{type} = "request_quotation";
405 if(!exists $form->{addition}) {
406 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
407 $form->{addition} = "SAVED";
408 $form->save_history($form->dbconnect(\%myconfig));
410 # /saving the history
412 $lxdebug->leave_sub();
415 sub save_and_quotation {
416 $lxdebug->enter_sub();
418 $auth->assert('customer_vendor_edit & sales_quotation_edit');
420 $form->{script} = "oe.pl";
421 $form->{type} = "sales_quotation";
423 if(!exists $form->{addition}) {
424 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
425 $form->{addition} = "SAVED";
426 $form->save_history($form->dbconnect(\%myconfig));
428 # /saving the history
430 $lxdebug->leave_sub();
434 $lxdebug->enter_sub();
436 $auth->assert('customer_vendor_edit & sales_order_edit');
438 $form->{script} = "oe.pl";
440 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
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 $auth->assert('customer_vendor_edit');
457 # $locale->text('Customer saved!')
458 # $locale->text('Vendor saved!')
460 $msg = ucfirst $form->{db};
463 $form->isblank("name", $locale->text("Name missing!"));
464 if ($form->{"db"} eq "customer") {
465 $rc = CT->save_customer(\%myconfig, \%$form);
467 $rc = CT->save_vendor(\%myconfig, \%$form);
470 $form->error($locale->text('customernumber not unique!'));
473 if(!exists $form->{addition}) {
474 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
475 $form->{addition} = "SAVED";
476 $form->save_history($form->dbconnect(\%myconfig));
478 # /saving the history
479 $form->redirect($locale->text($msg));
481 $lxdebug->leave_sub();
485 $lxdebug->enter_sub();
487 $auth->assert('customer_vendor_edit');
489 # $locale->text('Customer saved!')
490 # $locale->text('Vendor saved!')
492 $msg = ucfirst $form->{db};
495 $form->isblank("name", $locale->text("Name missing!"));
498 if ($form->{"db"} eq "customer") {
499 $res = CT->save_customer(\%myconfig, \%$form);
501 $res = CT->save_vendor(\%myconfig, \%$form);
505 if ($form->{"db"} eq "customer") {
506 $form->error($locale->text('This customer number is already in use.'));
508 $form->error($locale->text('This vendor number is already in use.'));
512 if(!exists $form->{addition}) {
513 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
514 $form->{addition} = "SAVED";
515 $form->save_history($form->dbconnect(\%myconfig));
517 # /saving the history
520 $lxdebug->leave_sub();
524 $lxdebug->enter_sub();
526 $auth->assert('customer_vendor_edit');
528 # $locale->text('Customer deleted!')
529 # $locale->text('Cannot delete customer!')
530 # $locale->text('Vendor deleted!')
531 # $locale->text('Cannot delete vendor!')
533 CT->delete(\%myconfig, \%$form);
535 $msg = ucfirst $form->{db};
538 if(!exists $form->{addition}) {
539 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
540 $form->{addition} = "DELETED";
541 $form->save_history($form->dbconnect(\%myconfig));
543 # /saving the history
544 $form->redirect($locale->text($msg));
546 $msg = "Cannot delete $form->{db}";
547 $form->error($locale->text($msg));
549 $lxdebug->leave_sub();
553 $lxdebug->enter_sub();
555 $auth->assert('customer_vendor_edit');
560 $lxdebug->leave_sub();
564 $lxdebug->enter_sub();
566 $auth->assert('customer_vendor_edit');
569 $lxdebug->leave_sub();
573 $lxdebug->enter_sub();
575 $auth->assert('customer_vendor_edit');
577 CT->get_contact(\%myconfig, \%$form);
579 $result = "$form->{cp_name}";
580 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);
581 print $cgi->header(), $result;
582 $lxdebug->leave_sub();
587 $lxdebug->enter_sub();
589 $auth->assert('customer_vendor_edit');
591 CT->get_shipto(\%myconfig, \%$form);
593 $result = "$form->{shiptoname}";
594 map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
595 print $cgi->header(), $result;
596 $lxdebug->leave_sub();
601 $lxdebug->enter_sub();
603 $auth->assert('customer_vendor_edit');
605 CT->get_delivery(\%myconfig, \%$form );
607 print $cgi->header(), $form->parse_html_template('ct/get_delivery');
609 $lxdebug->leave_sub();
612 sub continue { call_sub($form->{nextsub}); }