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 CT->search(\%myconfig, \%$form);
113 my $cvar_configs = CVar->get_configs('module' => 'CT');
116 if ($form->{status} eq 'all') {
117 push @options, $locale->text('All');
118 } elsif ($form->{status} eq 'orphaned') {
119 push @options, $locale->text('Orphaned');
122 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
123 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
124 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
125 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
128 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
129 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
130 'ordnumber', 'quonumber'
133 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
134 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
136 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
139 'id' => { 'text' => $locale->text('ID'), },
140 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
141 'name' => { 'text' => $locale->text('Name'), },
142 'address' => { 'text' => $locale->text('Address'), },
143 'contact' => { 'text' => $locale->text('Contact'), },
144 'phone' => { 'text' => $locale->text('Phone'), },
145 'fax' => { 'text' => $locale->text('Fax'), },
146 'email' => { 'text' => $locale->text('E-mail'), },
147 'cc' => { 'text' => $locale->text('Cc'), },
148 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
149 'sic_code' => { 'text' => $locale->text('SIC'), },
150 'business' => { 'text' => $locale->text('Type of Business'), },
151 'invnumber' => { 'text' => $locale->text('Invoice'), },
152 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
153 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
157 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
159 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
160 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
161 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
162 $form->{callback} = "$callback&sort=" . E($form->{sort});
164 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
166 my ($ordertype, $quotationtype, $attachment_basename);
167 if ($form->{IS_CUSTOMER}) {
168 $form->{title} = $locale->text('Customers');
169 $ordertype = 'sales_order';
170 $quotationtype = 'sales_quotation';
171 $attachment_basename = $locale->text('customer_list');
174 $form->{title} = $locale->text('Vendors');
175 $ordertype = 'purchase_order';
176 $quotationtype = 'request_quotation';
177 $attachment_basename = $locale->text('vendor_list');
180 my $report = SL::ReportGenerator->new(\%myconfig, $form);
182 $report->set_options('top_info_text' => join("\n", @options),
183 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
184 'output_format' => 'HTML',
185 'title' => $form->{title},
186 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
188 $report->set_options_from_form();
190 $report->set_columns(%column_defs);
191 $report->set_column_order(@columns);
193 $report->set_export_options('list_names', @hidden_variables);
195 $report->set_sort_indicator($form->{sort}, 1);
197 CVar->add_custom_variables_to_report('module' => 'CT',
198 'trans_id_field' => 'id',
199 'configs' => $cvar_configs,
200 'column_defs' => \%column_defs,
201 'data' => $form->{CT});
205 foreach my $ref (@{ $form->{CT} }) {
206 my $row = { map { $_ => { 'data' => '' } } @columns };
208 if ($ref->{id} ne $previous_id) {
209 $previous_id = $ref->{id};
210 map { $row->{$_}->{data} = $ref->{$_} } @columns;
212 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
213 $row->{email}->{link} = 'mailto:' . E($ref->{email});
216 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
217 $row->{invnumber}->{link} = $base_url;
218 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
219 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
220 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
221 $row->{$column}->{data} = $ref->{$column};
223 $report->add_data($row);
226 $report->generate_with_headers();
228 $lxdebug->leave_sub();
232 $lxdebug->enter_sub();
234 $auth->assert('customer_vendor_edit');
236 # show history button
237 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
238 #/show hhistory button
240 CT->get_tuple(\%myconfig, \%$form);
241 CT->populate_drop_down_boxes(\%myconfig, \%$form);
243 # format " into "
244 map { $form->{$_} =~ s/\"/"/g } keys %$form;
246 $form->{title} = "Edit";
249 $form->{discount} *= 100;
254 $lxdebug->leave_sub();
258 $lxdebug->enter_sub();
260 $auth->assert('customer_vendor_edit');
262 $form->get_lists("employees" => "ALL_EMPLOYEES",
263 "taxzones" => "ALL_TAXZONES");
264 $form->get_pricegroup(\%myconfig, { all => 1 });
266 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
267 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
268 $form->{is_admin} = $myconfig{role} eq 'admin';
269 $form->{is_customer} = $form->{db} eq 'customer';
270 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
271 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
272 $form->{contacts_label} = sub { join ", ", grep { $_ } $_[0]->{cp_name}, $_[0]->{cp_givenname} };
273 $form->{taxzone_id} = 0 if !$form->{id};
274 $form->{jsscript} = 1;
275 $form->{fokus} = "ct.greeting";
276 $form->{AJAX} = [ new CGI::Ajax( map {; "get_$_" => "$form->{script}?action=get_$_" } qw(shipto contact delivery) ) ];
278 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
279 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
281 $form->{title} = $form->{title_save}
282 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
284 CT->query_titles_and_greetings(\%myconfig, \%$form);
285 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
287 $form->{NOTES} ||= [ ];
289 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
291 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
294 print $form->parse_html_template('ct/form_header');
296 $lxdebug->leave_sub();
300 $lxdebug->enter_sub();
302 $auth->assert('customer_vendor_edit');
304 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
305 is_customer => $form->{db} eq 'customer' });
306 $lxdebug->leave_sub();
309 sub add_transaction {
310 $lxdebug->enter_sub();
312 $auth->assert('customer_vendor_edit & general_ledger');
314 # # saving the history
315 # if(!exists $form->{addition}) {
316 # $form->{addition} = "ADD TRANSACTION";
317 # $form->save_history($form->dbconnect(\%myconfig));
319 # # /saving the history
321 $form->isblank("name", $locale->text("Name missing!"));
322 if ($form->{"db"} eq "customer") {
323 CT->save_customer(\%myconfig, \%$form);
325 CT->save_vendor(\%myconfig, \%$form);
328 $form->{callback} = $form->escape($form->{callback}, 1);
329 $name = $form->escape("$form->{name}", 1);
332 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
335 $lxdebug->leave_sub();
338 sub save_and_ap_transaction {
339 $lxdebug->enter_sub();
341 $auth->assert('customer_vendor_edit & general_ledger');
343 $form->{script} = "ap.pl";
345 if(!exists $form->{addition}) {
346 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
347 $form->{addition} = "SAVED";
348 $form->save_history($form->dbconnect(\%myconfig));
350 # /saving the history
352 $lxdebug->leave_sub();
355 sub save_and_ar_transaction {
356 $lxdebug->enter_sub();
358 $auth->assert('customer_vendor_edit & general_ledger');
360 $form->{script} = "ar.pl";
362 if(!exists $form->{addition}) {
363 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
364 $form->{addition} = "SAVED";
365 $form->save_history($form->dbconnect(\%myconfig));
367 # /saving the history
369 $lxdebug->leave_sub();
372 sub save_and_invoice {
373 $lxdebug->enter_sub();
375 if ($form->{db} eq 'customer') {
376 $auth->assert('customer_vendor_edit & invoice_edit');
378 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
381 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
382 $form->{type} = "invoice";
384 if(!exists $form->{addition}) {
385 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
386 $form->{addition} = "SAVED";
387 $form->save_history($form->dbconnect(\%myconfig));
389 # /saving the history
391 $lxdebug->leave_sub();
395 $lxdebug->enter_sub();
397 $auth->assert('customer_vendor_edit & request_quotation_edit');
399 $form->{script} = "oe.pl";
400 $form->{type} = "request_quotation";
402 if(!exists $form->{addition}) {
403 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
404 $form->{addition} = "SAVED";
405 $form->save_history($form->dbconnect(\%myconfig));
407 # /saving the history
409 $lxdebug->leave_sub();
412 sub save_and_quotation {
413 $lxdebug->enter_sub();
415 $auth->assert('customer_vendor_edit & sales_quotation_edit');
417 $form->{script} = "oe.pl";
418 $form->{type} = "sales_quotation";
420 if(!exists $form->{addition}) {
421 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
422 $form->{addition} = "SAVED";
423 $form->save_history($form->dbconnect(\%myconfig));
425 # /saving the history
427 $lxdebug->leave_sub();
431 $lxdebug->enter_sub();
433 $auth->assert('customer_vendor_edit & sales_order_edit');
435 $form->{script} = "oe.pl";
437 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
439 if(!exists $form->{addition}) {
440 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
441 $form->{addition} = "SAVED";
442 $form->save_history($form->dbconnect(\%myconfig));
444 # /saving the history
446 $lxdebug->leave_sub();
450 $lxdebug->enter_sub();
452 $auth->assert('customer_vendor_edit');
454 $msg = ucfirst $form->{db};
457 $form->isblank("name", $locale->text("Name missing!"));
458 if ($form->{"db"} eq "customer") {
459 $rc = CT->save_customer(\%myconfig, \%$form);
461 $rc = CT->save_vendor(\%myconfig, \%$form);
464 $form->error($locale->text('customernumber not unique!'));
467 if(!exists $form->{addition}) {
468 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
469 $form->{addition} = "SAVED";
470 $form->save_history($form->dbconnect(\%myconfig));
472 # /saving the history
473 $form->redirect($locale->text($msg));
475 $lxdebug->leave_sub();
479 $lxdebug->enter_sub();
481 $auth->assert('customer_vendor_edit');
483 $msg = ucfirst $form->{db};
486 $form->isblank("name", $locale->text("Name missing!"));
489 if ($form->{"db"} eq "customer") {
490 $res = CT->save_customer(\%myconfig, \%$form);
492 $res = CT->save_vendor(\%myconfig, \%$form);
496 if ($form->{"db"} eq "customer") {
497 $form->error($locale->text('This customer number is already in use.'));
499 $form->error($locale->text('This vendor number is already in use.'));
503 if(!exists $form->{addition}) {
504 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
505 $form->{addition} = "SAVED";
506 $form->save_history($form->dbconnect(\%myconfig));
508 # /saving the history
511 $lxdebug->leave_sub();
515 $lxdebug->enter_sub();
517 $auth->assert('customer_vendor_edit');
519 CT->delete(\%myconfig, \%$form);
521 $msg = ucfirst $form->{db};
524 if(!exists $form->{addition}) {
525 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
526 $form->{addition} = "DELETED";
527 $form->save_history($form->dbconnect(\%myconfig));
529 # /saving the history
530 $form->redirect($locale->text($msg));
532 $msg = "Cannot delete $form->{db}";
533 $form->error($locale->text($msg));
535 $lxdebug->leave_sub();
539 $lxdebug->enter_sub();
541 $auth->assert('customer_vendor_edit');
546 $lxdebug->leave_sub();
550 $lxdebug->enter_sub();
552 $auth->assert('customer_vendor_edit');
555 $lxdebug->leave_sub();
559 $lxdebug->enter_sub();
561 $auth->assert('customer_vendor_edit');
563 CT->get_contact(\%myconfig, \%$form);
564 print $cgi->header(), join '__pjx__', map $form->{"cp_$_"},
565 qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
566 $lxdebug->leave_sub();
571 $lxdebug->enter_sub();
573 $auth->assert('customer_vendor_edit');
575 CT->get_shipto(\%myconfig, \%$form);
576 print $cgi->header(), join '__pjx__', map $form->{"shipto$_"},
577 qw(name department_1 department_2 street zipcode city country contact phone fax email used);
578 $lxdebug->leave_sub();
583 $lxdebug->enter_sub();
585 $auth->assert('customer_vendor_edit');
587 CT->get_delivery(\%myconfig, \%$form );
589 print $cgi->header(), $form->parse_html_template('ct/get_delivery');
591 $lxdebug->leave_sub();
595 $main::lxdebug->enter_sub();
597 $auth->assert('customer_vendor_edit');
599 CT->get_shipto(\%myconfig, \%$form);
601 unless ($form->{shiptoused}) {
602 CT->delete_shipto($form->{shipto_id});
603 @$form{ grep /^shipto/, keys %$form } = undef;
608 $main::lxdebug->leave_sub();
612 $main::lxdebug->enter_sub();
614 $auth->assert('customer_vendor_edit');
616 CT->get_contact(\%myconfig, \%$form);
618 unless ($form->{cp_used}) {
619 CT->delete_shipto($form->{cp_id});
620 @$form{ grep /^cp_/, keys %$form } = undef;
625 $main::lxdebug->leave_sub();
628 sub continue { call_sub($form->{nextsub}); }