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";
68 $form->{callback} = "$form->{script}?action=add&db=$form->{db}" unless $form->{callback};
70 CT->populate_drop_down_boxes(\%myconfig, \%$form);
75 $lxdebug->leave_sub();
79 $lxdebug->enter_sub();
81 $auth->assert('customer_vendor_edit');
83 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
85 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
86 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
88 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
89 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
90 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
91 'include_prefix' => 'l_',
92 'include_value' => 'Y');
94 $form->{jsscript} = 1;
95 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
96 $form->{fokus} = 'Form.name';
99 print $form->parse_html_template('ct/search');
101 $lxdebug->leave_sub();
105 $lxdebug->enter_sub();
107 $auth->assert('customer_vendor_edit');
109 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
111 report_generator_set_default_sort('name', 1);
113 CT->search(\%myconfig, \%$form);
115 my $cvar_configs = CVar->get_configs('module' => 'CT');
118 if ($form->{status} eq 'all') {
119 push @options, $locale->text('All');
120 } elsif ($form->{status} eq 'orphaned') {
121 push @options, $locale->text('Orphaned');
124 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
125 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
126 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
127 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
128 push @options, $locale->text('Contact person (surname)') . " : $form->{cp_name}" if $form->{cp_name};
129 push @options, $locale->text('Billing/shipping address (city)') . " : $form->{addr_city}" if $form->{addr_city};
130 push @options, $locale->text('Billing/shipping address (zipcode)') . " : $form->{zipcode}" if $form->{addr_zipcode};
131 push @options, $locale->text('Billing/shipping address (street)') . " : $form->{street}" if $form->{addr_street};
134 'id', 'name', "$form->{db}number", 'contact', 'phone',
135 'fax', 'email', 'taxnumber', 'street', 'zipcode' , 'city',
136 'business', 'invnumber', 'ordnumber', 'quonumber'
139 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
140 my @searchable_custom_variables = grep { $_->{searchable} } @{ $cvar_configs };
141 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
143 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
146 'id' => { 'text' => $locale->text('ID'), },
147 "$form->{db}number" => { 'text' => $locale->text('Number'), },
148 'name' => { 'text' => $locale->text('Name'), },
149 'contact' => { 'text' => $locale->text('Contact'), },
150 'phone' => { 'text' => $locale->text('Phone'), },
151 'fax' => { 'text' => $locale->text('Fax'), },
152 'email' => { 'text' => $locale->text('E-mail'), },
153 'cc' => { 'text' => $locale->text('Cc'), },
154 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
155 'business' => { 'text' => $locale->text('Type of Business'), },
156 'invnumber' => { 'text' => $locale->text('Invoice'), },
157 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
158 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
159 'street' => { 'text' => $locale->text('Street'), },
160 'zipcode' => { 'text' => $locale->text('Zipcode'), },
161 'city' => { 'text' => $locale->text('City'), },
165 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
167 my @hidden_variables = (qw(db status obsolete name contact email cp_name addr_city), "$form->{db}number", @searchable_custom_variables, map { "l_$_" } @columns);
168 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
169 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_nondefault);
170 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
173 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
174 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
177 my ($ordertype, $quotationtype, $attachment_basename);
178 if ($form->{IS_CUSTOMER}) {
179 $form->{title} = $locale->text('Customers');
180 $ordertype = 'sales_order';
181 $quotationtype = 'sales_quotation';
182 $attachment_basename = $locale->text('customer_list');
185 $form->{title} = $locale->text('Vendors');
186 $ordertype = 'purchase_order';
187 $quotationtype = 'request_quotation';
188 $attachment_basename = $locale->text('vendor_list');
191 my $report = SL::ReportGenerator->new(\%myconfig, $form);
193 $report->set_options('top_info_text' => join("\n", @options),
194 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
195 'output_format' => 'HTML',
196 'title' => $form->{title},
197 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
199 $report->set_options_from_form();
201 $report->set_columns(%column_defs);
202 $report->set_column_order(@columns);
204 $report->set_export_options('list_names', @hidden_variables, qw(sort sortdir));
206 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
208 CVar->add_custom_variables_to_report('module' => 'CT',
209 'trans_id_field' => 'id',
210 'configs' => $cvar_configs,
211 'column_defs' => \%column_defs,
212 'data' => $form->{CT});
216 foreach my $ref (@{ $form->{CT} }) {
217 my $row = { map { $_ => { 'data' => '' } } @columns };
219 if ($ref->{id} ne $previous_id) {
220 $previous_id = $ref->{id};
221 map { $row->{$_}->{data} = $ref->{$_} } @columns;
223 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
224 $row->{email}->{link} = 'mailto:' . E($ref->{email});
227 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
228 $row->{invnumber}->{link} = $base_url;
229 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
230 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
231 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
232 $row->{$column}->{data} = $ref->{$column};
234 $report->add_data($row);
237 $report->generate_with_headers();
239 $lxdebug->leave_sub();
243 $lxdebug->enter_sub();
245 $auth->assert('customer_vendor_edit');
247 # show history button
248 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
249 #/show hhistory button
251 CT->get_tuple(\%myconfig, \%$form);
252 CT->populate_drop_down_boxes(\%myconfig, \%$form);
254 $form->{title} = "Edit";
257 $form->{discount} *= 100;
262 $lxdebug->leave_sub();
266 $lxdebug->enter_sub();
268 $auth->assert('customer_vendor_edit');
270 $form->get_lists("employees" => "ALL_EMPLOYEES",
271 "taxzones" => "ALL_TAXZONES");
272 $form->get_pricegroup(\%myconfig, { all => 1 });
274 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
275 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
276 $form->{is_admin} = $myconfig{role} eq 'admin';
277 $form->{is_customer} = $form->{db} eq 'customer';
278 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
279 $form->{shipto_label} = sub { my $s = shift(@_); join('; ', grep { $_ } map { $s->{"shipto$_"} } qw(name department_1 street city)) || ' ' };
280 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
281 $form->{taxzone_id} = 0 if !$form->{id};
282 $form->{jsscript} = 1;
283 $form->{fokus} = "ct.greeting";
284 $form->{AJAX} = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
286 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
287 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
289 $form->{title} = $form->{title_save}
290 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
292 CT->query_titles_and_greetings(\%myconfig, \%$form);
293 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
295 $form->{NOTES} ||= [ ];
297 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
299 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
302 print $form->parse_html_template('ct/form_header');
304 $lxdebug->leave_sub();
308 $lxdebug->enter_sub();
310 $auth->assert('customer_vendor_edit');
312 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
313 is_customer => $form->{db} eq 'customer' });
314 $lxdebug->leave_sub();
317 sub add_transaction {
318 $lxdebug->enter_sub();
320 $auth->assert('customer_vendor_edit & ' .
321 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
322 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
324 # # saving the history
325 # if(!exists $form->{addition}) {
326 # $form->{addition} = "ADD TRANSACTION";
327 # $form->save_history($form->dbconnect(\%myconfig));
329 # # /saving the history
331 $form->isblank("name", $locale->text("Name missing!"));
332 if ($form->{"db"} eq "customer") {
333 CT->save_customer(\%myconfig, \%$form);
335 CT->save_vendor(\%myconfig, \%$form);
338 $form->{callback} = $form->escape($form->{callback}, 1);
339 $name = $form->escape("$form->{name}", 1);
342 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
345 $lxdebug->leave_sub();
348 sub save_and_ap_transaction {
349 $lxdebug->enter_sub();
351 $auth->assert('customer_vendor_edit & general_ledger');
353 $form->{script} = "ap.pl";
355 if(!exists $form->{addition}) {
356 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
357 $form->{addition} = "SAVED";
358 $form->save_history($form->dbconnect(\%myconfig));
360 # /saving the history
362 $lxdebug->leave_sub();
365 sub save_and_ar_transaction {
366 $lxdebug->enter_sub();
368 $auth->assert('customer_vendor_edit & general_ledger');
370 $form->{script} = "ar.pl";
372 if(!exists $form->{addition}) {
373 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
374 $form->{addition} = "SAVED";
375 $form->save_history($form->dbconnect(\%myconfig));
377 # /saving the history
379 $lxdebug->leave_sub();
382 sub save_and_invoice {
383 $lxdebug->enter_sub();
385 if ($form->{db} eq 'customer') {
386 $auth->assert('customer_vendor_edit & invoice_edit');
388 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
391 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
392 $form->{type} = "invoice";
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();
405 $lxdebug->enter_sub();
407 $auth->assert('customer_vendor_edit & request_quotation_edit');
409 $form->{script} = "oe.pl";
410 $form->{type} = "request_quotation";
412 if(!exists $form->{addition}) {
413 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
414 $form->{addition} = "SAVED";
415 $form->save_history($form->dbconnect(\%myconfig));
417 # /saving the history
419 $lxdebug->leave_sub();
422 sub save_and_quotation {
423 $lxdebug->enter_sub();
425 $auth->assert('customer_vendor_edit & sales_quotation_edit');
427 $form->{script} = "oe.pl";
428 $form->{type} = "sales_quotation";
430 if(!exists $form->{addition}) {
431 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
432 $form->{addition} = "SAVED";
433 $form->save_history($form->dbconnect(\%myconfig));
435 # /saving the history
437 $lxdebug->leave_sub();
441 $lxdebug->enter_sub();
443 if ($form->{db} eq 'customer') {
444 $auth->assert('customer_vendor_edit & sales_order_edit');
446 $auth->assert('customer_vendor_edit & purchase_order_edit');
449 $form->{script} = "oe.pl";
451 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
453 if(!exists $form->{addition}) {
454 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
455 $form->{addition} = "SAVED";
456 $form->save_history($form->dbconnect(\%myconfig));
458 # /saving the history
460 $lxdebug->leave_sub();
464 $lxdebug->enter_sub();
466 $auth->assert('customer_vendor_edit');
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 $msg = ucfirst $form->{db};
500 $form->isblank("name", $locale->text("Name missing!"));
503 if ($form->{"db"} eq "customer") {
504 $res = CT->save_customer(\%myconfig, \%$form);
506 $res = CT->save_vendor(\%myconfig, \%$form);
510 if ($form->{"db"} eq "customer") {
511 $form->error($locale->text('This customer number is already in use.'));
513 $form->error($locale->text('This vendor number is already in use.'));
517 if(!exists $form->{addition}) {
518 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
519 $form->{addition} = "SAVED";
520 $form->save_history($form->dbconnect(\%myconfig));
522 # /saving the history
525 $lxdebug->leave_sub();
529 $lxdebug->enter_sub();
531 $auth->assert('customer_vendor_edit');
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);
578 print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"},
579 qw(name title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used gender);
580 $lxdebug->leave_sub();
585 $lxdebug->enter_sub();
587 $auth->assert('customer_vendor_edit');
589 CT->get_shipto(\%myconfig, \%$form);
590 print $form->ajax_response_header(), join '__pjx__', map $form->{"shipto$_"},
591 qw(name department_1 department_2 street zipcode city country contact phone fax email used);
592 $lxdebug->leave_sub();
597 $lxdebug->enter_sub();
599 $auth->assert('customer_vendor_edit');
601 CT->get_delivery(\%myconfig, \%$form );
602 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
604 print $form->ajax_response_header(), $form->parse_html_template('ct/get_delivery');
606 $lxdebug->leave_sub();
610 $main::lxdebug->enter_sub();
612 $auth->assert('customer_vendor_edit');
614 CT->get_shipto(\%myconfig, \%$form);
616 unless ($form->{shiptoused}) {
617 CT->delete_shipto($form->{shipto_id});
618 @$form{ grep /^shipto/, keys %$form } = undef;
623 $main::lxdebug->leave_sub();
627 $main::lxdebug->enter_sub();
629 $auth->assert('customer_vendor_edit');
631 CT->get_contact(\%myconfig, \%$form);
633 unless ($form->{cp_used}) {
634 CT->delete_shipto($form->{cp_id});
635 @$form{ grep /^cp_/, keys %$form } = undef;
640 $main::lxdebug->leave_sub();
643 sub continue { call_sub($form->{nextsub}); }