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')
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!')
49 use POSIX qw(strftime);
53 use SL::ReportGenerator;
55 require "bin/mozilla/common.pl";
56 require "bin/mozilla/reportgenerator.pl";
63 $lxdebug->enter_sub();
65 $auth->assert('customer_vendor_edit');
67 $form->{title} = "Add";
70 "$form->{script}?action=add&db=$form->{db}"
71 unless $form->{callback};
73 CT->populate_drop_down_boxes(\%myconfig, \%$form);
78 $lxdebug->leave_sub();
82 $lxdebug->enter_sub();
84 $auth->assert('customer_vendor_edit');
86 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
88 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
89 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
91 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
92 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
93 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
94 'include_prefix' => 'l_',
95 'include_value' => 'Y');
97 $form->{jsscript} = 1;
98 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
99 $form->{fokus} = 'Form.name';
102 print $form->parse_html_template('ct/search');
104 $lxdebug->leave_sub();
108 $lxdebug->enter_sub();
110 $auth->assert('customer_vendor_edit');
112 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
114 CT->search(\%myconfig, \%$form);
116 my $cvar_configs = CVar->get_configs('module' => 'CT');
119 if ($form->{status} eq 'all') {
120 push @options, $locale->text('All');
121 } elsif ($form->{status} eq 'orphaned') {
122 push @options, $locale->text('Orphaned');
125 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
126 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
127 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
128 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
131 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
132 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
133 'ordnumber', 'quonumber'
136 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
137 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
139 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
142 'id' => { 'text' => $locale->text('ID'), },
143 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
144 'name' => { 'text' => $locale->text('Name'), },
145 'address' => { 'text' => $locale->text('Address'), },
146 'contact' => { 'text' => $locale->text('Contact'), },
147 'phone' => { 'text' => $locale->text('Phone'), },
148 'fax' => { 'text' => $locale->text('Fax'), },
149 'email' => { 'text' => $locale->text('E-mail'), },
150 'cc' => { 'text' => $locale->text('Cc'), },
151 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
152 'sic_code' => { 'text' => $locale->text('SIC'), },
153 'business' => { 'text' => $locale->text('Type of Business'), },
154 'invnumber' => { 'text' => $locale->text('Invoice'), },
155 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
156 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
160 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
162 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
163 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
164 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
165 $form->{callback} = "$callback&sort=" . E($form->{sort});
167 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
169 my ($ordertype, $quotationtype, $attachment_basename);
170 if ($form->{IS_CUSTOMER}) {
171 $form->{title} = $locale->text('Customers');
172 $ordertype = 'sales_order';
173 $quotationtype = 'sales_quotation';
174 $attachment_basename = $locale->text('customer_list');
177 $form->{title} = $locale->text('Vendors');
178 $ordertype = 'purchase_order';
179 $quotationtype = 'request_quotation';
180 $attachment_basename = $locale->text('vendor_list');
183 my $report = SL::ReportGenerator->new(\%myconfig, $form);
185 $report->set_options('top_info_text' => join("\n", @options),
186 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
187 'output_format' => 'HTML',
188 'title' => $form->{title},
189 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
191 $report->set_options_from_form();
193 $report->set_columns(%column_defs);
194 $report->set_column_order(@columns);
196 $report->set_export_options('list_names', @hidden_variables);
198 $report->set_sort_indicator($form->{sort}, 1);
200 CVar->add_custom_variables_to_report('module' => 'CT',
201 'trans_id_field' => 'id',
202 'configs' => $cvar_configs,
203 'column_defs' => \%column_defs,
204 'data' => $form->{CT});
208 foreach my $ref (@{ $form->{CT} }) {
209 my $row = { map { $_ => { 'data' => '' } } @columns };
211 if ($ref->{id} ne $previous_id) {
212 $previous_id = $ref->{id};
213 map { $row->{$_}->{data} = $ref->{$_} } @columns;
215 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
216 $row->{email}->{link} = 'mailto:' . E($ref->{email});
219 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
220 $row->{invnumber}->{link} = $base_url;
221 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
222 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
223 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
224 $row->{$column}->{data} = $ref->{$column};
226 $report->add_data($row);
229 $report->generate_with_headers();
231 $lxdebug->leave_sub();
235 $lxdebug->enter_sub();
237 $auth->assert('customer_vendor_edit');
239 # show history button
240 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
241 #/show hhistory button
243 CT->get_tuple(\%myconfig, \%$form);
244 CT->populate_drop_down_boxes(\%myconfig, \%$form);
246 # format " into "
247 map { $form->{$_} =~ s/\"/"/g } keys %$form;
249 $form->{title} = "Edit";
252 $form->{discount} *= 100;
257 $lxdebug->leave_sub();
261 $lxdebug->enter_sub();
263 $auth->assert('customer_vendor_edit');
265 $form->get_lists("employees" => "ALL_EMPLOYEES",
266 "taxzones" => "ALL_TAXZONES");
267 $form->get_pricegroup(\%myconfig, { all => 1 });
269 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
270 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
271 $form->{is_admin} = $myconfig{role} eq 'admin';
272 $form->{is_customer} = $form->{db} eq 'customer';
273 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
274 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
275 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
276 $form->{taxzone_id} = 0 if !$form->{id};
277 $form->{jsscript} = 1;
278 $form->{fokus} = "ct.greeting";
279 $form->{AJAX} = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
281 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
282 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
284 $form->{title} = $form->{title_save}
285 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
287 CT->query_titles_and_greetings(\%myconfig, \%$form);
288 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 $msg = ucfirst $form->{db};
460 $form->isblank("name", $locale->text("Name missing!"));
461 if ($form->{"db"} eq "customer") {
462 $rc = CT->save_customer(\%myconfig, \%$form);
464 $rc = CT->save_vendor(\%myconfig, \%$form);
467 $form->error($locale->text('customernumber not unique!'));
470 if(!exists $form->{addition}) {
471 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
472 $form->{addition} = "SAVED";
473 $form->save_history($form->dbconnect(\%myconfig));
475 # /saving the history
476 $form->redirect($locale->text($msg));
478 $lxdebug->leave_sub();
482 $lxdebug->enter_sub();
484 $auth->assert('customer_vendor_edit');
486 $msg = ucfirst $form->{db};
489 $form->isblank("name", $locale->text("Name missing!"));
492 if ($form->{"db"} eq "customer") {
493 $res = CT->save_customer(\%myconfig, \%$form);
495 $res = CT->save_vendor(\%myconfig, \%$form);
499 if ($form->{"db"} eq "customer") {
500 $form->error($locale->text('This customer number is already in use.'));
502 $form->error($locale->text('This vendor number is already in use.'));
506 if(!exists $form->{addition}) {
507 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
508 $form->{addition} = "SAVED";
509 $form->save_history($form->dbconnect(\%myconfig));
511 # /saving the history
514 $lxdebug->leave_sub();
518 $lxdebug->enter_sub();
520 $auth->assert('customer_vendor_edit');
522 CT->delete(\%myconfig, \%$form);
524 $msg = ucfirst $form->{db};
527 if(!exists $form->{addition}) {
528 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
529 $form->{addition} = "DELETED";
530 $form->save_history($form->dbconnect(\%myconfig));
532 # /saving the history
533 $form->redirect($locale->text($msg));
535 $msg = "Cannot delete $form->{db}";
536 $form->error($locale->text($msg));
538 $lxdebug->leave_sub();
542 $lxdebug->enter_sub();
544 $auth->assert('customer_vendor_edit');
549 $lxdebug->leave_sub();
553 $lxdebug->enter_sub();
555 $auth->assert('customer_vendor_edit');
558 $lxdebug->leave_sub();
562 $lxdebug->enter_sub();
564 $auth->assert('customer_vendor_edit');
566 CT->get_contact(\%myconfig, \%$form);
567 print $cgi->header(), join '__pjx__', map $form->{"cp_$_"},
568 qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
569 $lxdebug->leave_sub();
574 $lxdebug->enter_sub();
576 $auth->assert('customer_vendor_edit');
578 CT->get_shipto(\%myconfig, \%$form);
579 print $cgi->header(), join '__pjx__', map $form->{"shipto$_"},
580 qw(name department_1 department_2 street zipcode city country contact phone fax email used);
581 $lxdebug->leave_sub();
586 $lxdebug->enter_sub();
588 $auth->assert('customer_vendor_edit');
590 CT->get_delivery(\%myconfig, \%$form );
592 print $cgi->header(), $form->parse_html_template('ct/get_delivery');
594 $lxdebug->leave_sub();
598 $main::lxdebug->enter_sub();
600 $auth->assert('customer_vendor_edit');
602 CT->get_shipto(\%myconfig, \%$form);
604 unless ($form->{shiptoused}) {
605 CT->delete_shipto($form->{shipto_id});
606 @$form{ grep /^shipto/, keys %$form } = undef;
611 $main::lxdebug->leave_sub();
615 $main::lxdebug->enter_sub();
617 $auth->assert('customer_vendor_edit');
619 CT->get_contact(\%myconfig, \%$form);
621 unless ($form->{cp_used}) {
622 CT->delete_shipto($form->{cp_id});
623 @$form{ grep /^cp_/, keys %$form } = undef;
628 $main::lxdebug->leave_sub();
631 sub continue { call_sub($form->{nextsub}); }