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};
130 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
131 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
132 'ordnumber', 'quonumber'
135 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
136 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
138 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
141 'id' => { 'text' => $locale->text('ID'), },
142 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
143 'name' => { 'text' => $locale->text('Name'), },
144 'address' => { 'text' => $locale->text('Address'), },
145 'contact' => { 'text' => $locale->text('Contact'), },
146 'phone' => { 'text' => $locale->text('Phone'), },
147 'fax' => { 'text' => $locale->text('Fax'), },
148 'email' => { 'text' => $locale->text('E-mail'), },
149 'cc' => { 'text' => $locale->text('Cc'), },
150 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
151 'sic_code' => { 'text' => $locale->text('SIC'), },
152 'business' => { 'text' => $locale->text('Type of Business'), },
153 'invnumber' => { 'text' => $locale->text('Invoice'), },
154 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
155 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
159 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
161 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
162 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
163 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
164 $form->{callback} = "$callback&sort=" . E($form->{sort}) . "&sortdir=" . E($form->{sortdir});
167 my $sortdir = $form->{sort} eq $_ ? 1 - $form->{sortdir} : $form->{sortdir};
168 $column_defs{$_}->{link} = "${callback}&sort=${_}&sortdir=${sortdir}";
171 my ($ordertype, $quotationtype, $attachment_basename);
172 if ($form->{IS_CUSTOMER}) {
173 $form->{title} = $locale->text('Customers');
174 $ordertype = 'sales_order';
175 $quotationtype = 'sales_quotation';
176 $attachment_basename = $locale->text('customer_list');
179 $form->{title} = $locale->text('Vendors');
180 $ordertype = 'purchase_order';
181 $quotationtype = 'request_quotation';
182 $attachment_basename = $locale->text('vendor_list');
185 my $report = SL::ReportGenerator->new(\%myconfig, $form);
187 $report->set_options('top_info_text' => join("\n", @options),
188 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
189 'output_format' => 'HTML',
190 'title' => $form->{title},
191 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
193 $report->set_options_from_form();
195 $report->set_columns(%column_defs);
196 $report->set_column_order(@columns);
198 $report->set_export_options('list_names', @hidden_variables);
200 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
202 CVar->add_custom_variables_to_report('module' => 'CT',
203 'trans_id_field' => 'id',
204 'configs' => $cvar_configs,
205 'column_defs' => \%column_defs,
206 'data' => $form->{CT});
210 foreach my $ref (@{ $form->{CT} }) {
211 my $row = { map { $_ => { 'data' => '' } } @columns };
213 if ($ref->{id} ne $previous_id) {
214 $previous_id = $ref->{id};
215 map { $row->{$_}->{data} = $ref->{$_} } @columns;
217 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
218 $row->{email}->{link} = 'mailto:' . E($ref->{email});
221 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
222 $row->{invnumber}->{link} = $base_url;
223 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
224 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
225 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
226 $row->{$column}->{data} = $ref->{$column};
228 $report->add_data($row);
231 $report->generate_with_headers();
233 $lxdebug->leave_sub();
237 $lxdebug->enter_sub();
239 $auth->assert('customer_vendor_edit');
241 # show history button
242 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
243 #/show hhistory button
245 CT->get_tuple(\%myconfig, \%$form);
246 CT->populate_drop_down_boxes(\%myconfig, \%$form);
248 # format " into "
249 map { $form->{$_} =~ s/\"/"/g } keys %$form;
251 $form->{title} = "Edit";
254 $form->{discount} *= 100;
259 $lxdebug->leave_sub();
263 $lxdebug->enter_sub();
265 $auth->assert('customer_vendor_edit');
267 $form->get_lists("employees" => "ALL_EMPLOYEES",
268 "taxzones" => "ALL_TAXZONES");
269 $form->get_pricegroup(\%myconfig, { all => 1 });
271 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
272 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
273 $form->{is_admin} = $myconfig{role} eq 'admin';
274 $form->{is_customer} = $form->{db} eq 'customer';
275 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
276 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
277 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
278 $form->{taxzone_id} = 0 if !$form->{id};
279 $form->{jsscript} = 1;
280 $form->{fokus} = "ct.greeting";
281 $form->{AJAX} = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
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 $form->{title} = $form->{title_save}
287 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
289 CT->query_titles_and_greetings(\%myconfig, \%$form);
290 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
292 $form->{NOTES} ||= [ ];
294 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
296 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
299 print $form->parse_html_template('ct/form_header');
301 $lxdebug->leave_sub();
305 $lxdebug->enter_sub();
307 $auth->assert('customer_vendor_edit');
309 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
310 is_customer => $form->{db} eq 'customer' });
311 $lxdebug->leave_sub();
314 sub add_transaction {
315 $lxdebug->enter_sub();
317 $auth->assert('customer_vendor_edit & ' .
318 '(general_ledger | invoice_edit | vendor_invoice_edit | ' .
319 ' request_quotation_edit | sales_quotation_edit | sales_order_edit | purchase_order_edit)');
321 # # saving the history
322 # if(!exists $form->{addition}) {
323 # $form->{addition} = "ADD TRANSACTION";
324 # $form->save_history($form->dbconnect(\%myconfig));
326 # # /saving the history
328 $form->isblank("name", $locale->text("Name missing!"));
329 if ($form->{"db"} eq "customer") {
330 CT->save_customer(\%myconfig, \%$form);
332 CT->save_vendor(\%myconfig, \%$form);
335 $form->{callback} = $form->escape($form->{callback}, 1);
336 $name = $form->escape("$form->{name}", 1);
339 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
342 $lxdebug->leave_sub();
345 sub save_and_ap_transaction {
346 $lxdebug->enter_sub();
348 $auth->assert('customer_vendor_edit & general_ledger');
350 $form->{script} = "ap.pl";
352 if(!exists $form->{addition}) {
353 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
354 $form->{addition} = "SAVED";
355 $form->save_history($form->dbconnect(\%myconfig));
357 # /saving the history
359 $lxdebug->leave_sub();
362 sub save_and_ar_transaction {
363 $lxdebug->enter_sub();
365 $auth->assert('customer_vendor_edit & general_ledger');
367 $form->{script} = "ar.pl";
369 if(!exists $form->{addition}) {
370 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
371 $form->{addition} = "SAVED";
372 $form->save_history($form->dbconnect(\%myconfig));
374 # /saving the history
376 $lxdebug->leave_sub();
379 sub save_and_invoice {
380 $lxdebug->enter_sub();
382 if ($form->{db} eq 'customer') {
383 $auth->assert('customer_vendor_edit & invoice_edit');
385 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
388 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
389 $form->{type} = "invoice";
391 if(!exists $form->{addition}) {
392 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
393 $form->{addition} = "SAVED";
394 $form->save_history($form->dbconnect(\%myconfig));
396 # /saving the history
398 $lxdebug->leave_sub();
402 $lxdebug->enter_sub();
404 $auth->assert('customer_vendor_edit & request_quotation_edit');
406 $form->{script} = "oe.pl";
407 $form->{type} = "request_quotation";
409 if(!exists $form->{addition}) {
410 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
411 $form->{addition} = "SAVED";
412 $form->save_history($form->dbconnect(\%myconfig));
414 # /saving the history
416 $lxdebug->leave_sub();
419 sub save_and_quotation {
420 $lxdebug->enter_sub();
422 $auth->assert('customer_vendor_edit & sales_quotation_edit');
424 $form->{script} = "oe.pl";
425 $form->{type} = "sales_quotation";
427 if(!exists $form->{addition}) {
428 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
429 $form->{addition} = "SAVED";
430 $form->save_history($form->dbconnect(\%myconfig));
432 # /saving the history
434 $lxdebug->leave_sub();
438 $lxdebug->enter_sub();
440 if ($form->{db} eq 'customer') {
441 $auth->assert('customer_vendor_edit & sales_order_edit');
443 $auth->assert('customer_vendor_edit & purchase_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 $msg = ucfirst $form->{db};
468 $form->isblank("name", $locale->text("Name missing!"));
469 if ($form->{"db"} eq "customer") {
470 $rc = CT->save_customer(\%myconfig, \%$form);
472 $rc = CT->save_vendor(\%myconfig, \%$form);
475 $form->error($locale->text('customernumber not unique!'));
478 if(!exists $form->{addition}) {
479 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
480 $form->{addition} = "SAVED";
481 $form->save_history($form->dbconnect(\%myconfig));
483 # /saving the history
484 $form->redirect($locale->text($msg));
486 $lxdebug->leave_sub();
490 $lxdebug->enter_sub();
492 $auth->assert('customer_vendor_edit');
494 $msg = ucfirst $form->{db};
497 $form->isblank("name", $locale->text("Name missing!"));
500 if ($form->{"db"} eq "customer") {
501 $res = CT->save_customer(\%myconfig, \%$form);
503 $res = CT->save_vendor(\%myconfig, \%$form);
507 if ($form->{"db"} eq "customer") {
508 $form->error($locale->text('This customer number is already in use.'));
510 $form->error($locale->text('This vendor number is already in use.'));
514 if(!exists $form->{addition}) {
515 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
516 $form->{addition} = "SAVED";
517 $form->save_history($form->dbconnect(\%myconfig));
519 # /saving the history
522 $lxdebug->leave_sub();
526 $lxdebug->enter_sub();
528 $auth->assert('customer_vendor_edit');
530 CT->delete(\%myconfig, \%$form);
532 $msg = ucfirst $form->{db};
535 if(!exists $form->{addition}) {
536 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
537 $form->{addition} = "DELETED";
538 $form->save_history($form->dbconnect(\%myconfig));
540 # /saving the history
541 $form->redirect($locale->text($msg));
543 $msg = "Cannot delete $form->{db}";
544 $form->error($locale->text($msg));
546 $lxdebug->leave_sub();
550 $lxdebug->enter_sub();
552 $auth->assert('customer_vendor_edit');
557 $lxdebug->leave_sub();
561 $lxdebug->enter_sub();
563 $auth->assert('customer_vendor_edit');
566 $lxdebug->leave_sub();
570 $lxdebug->enter_sub();
572 $auth->assert('customer_vendor_edit');
574 CT->get_contact(\%myconfig, \%$form);
575 print $form->ajax_response_header(), join '__pjx__', map $form->{"cp_$_"},
576 qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
577 $lxdebug->leave_sub();
582 $lxdebug->enter_sub();
584 $auth->assert('customer_vendor_edit');
586 CT->get_shipto(\%myconfig, \%$form);
587 print $form->ajax_response_header(), join '__pjx__', map $form->{"shipto$_"},
588 qw(name department_1 department_2 street zipcode city country contact phone fax email used);
589 $lxdebug->leave_sub();
594 $lxdebug->enter_sub();
596 $auth->assert('customer_vendor_edit');
598 CT->get_delivery(\%myconfig, \%$form );
600 print $form->ajax_response_header(), $form->parse_html_template('ct/get_delivery');
602 $lxdebug->leave_sub();
606 $main::lxdebug->enter_sub();
608 $auth->assert('customer_vendor_edit');
610 CT->get_shipto(\%myconfig, \%$form);
612 unless ($form->{shiptoused}) {
613 CT->delete_shipto($form->{shipto_id});
614 @$form{ grep /^shipto/, keys %$form } = undef;
619 $main::lxdebug->leave_sub();
623 $main::lxdebug->enter_sub();
625 $auth->assert('customer_vendor_edit');
627 CT->get_contact(\%myconfig, \%$form);
629 unless ($form->{cp_used}) {
630 CT->delete_shipto($form->{cp_id});
631 @$form{ grep /^cp_/, keys %$form } = undef;
636 $main::lxdebug->leave_sub();
639 sub continue { call_sub($form->{nextsub}); }