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 & general_ledger');
319 # # saving the history
320 # if(!exists $form->{addition}) {
321 # $form->{addition} = "ADD TRANSACTION";
322 # $form->save_history($form->dbconnect(\%myconfig));
324 # # /saving the history
326 $form->isblank("name", $locale->text("Name missing!"));
327 if ($form->{"db"} eq "customer") {
328 CT->save_customer(\%myconfig, \%$form);
330 CT->save_vendor(\%myconfig, \%$form);
333 $form->{callback} = $form->escape($form->{callback}, 1);
334 $name = $form->escape("$form->{name}", 1);
337 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
340 $lxdebug->leave_sub();
343 sub save_and_ap_transaction {
344 $lxdebug->enter_sub();
346 $auth->assert('customer_vendor_edit & general_ledger');
348 $form->{script} = "ap.pl";
350 if(!exists $form->{addition}) {
351 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
352 $form->{addition} = "SAVED";
353 $form->save_history($form->dbconnect(\%myconfig));
355 # /saving the history
357 $lxdebug->leave_sub();
360 sub save_and_ar_transaction {
361 $lxdebug->enter_sub();
363 $auth->assert('customer_vendor_edit & general_ledger');
365 $form->{script} = "ar.pl";
367 if(!exists $form->{addition}) {
368 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
369 $form->{addition} = "SAVED";
370 $form->save_history($form->dbconnect(\%myconfig));
372 # /saving the history
374 $lxdebug->leave_sub();
377 sub save_and_invoice {
378 $lxdebug->enter_sub();
380 if ($form->{db} eq 'customer') {
381 $auth->assert('customer_vendor_edit & invoice_edit');
383 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
386 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
387 $form->{type} = "invoice";
389 if(!exists $form->{addition}) {
390 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
391 $form->{addition} = "SAVED";
392 $form->save_history($form->dbconnect(\%myconfig));
394 # /saving the history
396 $lxdebug->leave_sub();
400 $lxdebug->enter_sub();
402 $auth->assert('customer_vendor_edit & request_quotation_edit');
404 $form->{script} = "oe.pl";
405 $form->{type} = "request_quotation";
407 if(!exists $form->{addition}) {
408 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
409 $form->{addition} = "SAVED";
410 $form->save_history($form->dbconnect(\%myconfig));
412 # /saving the history
414 $lxdebug->leave_sub();
417 sub save_and_quotation {
418 $lxdebug->enter_sub();
420 $auth->assert('customer_vendor_edit & sales_quotation_edit');
422 $form->{script} = "oe.pl";
423 $form->{type} = "sales_quotation";
425 if(!exists $form->{addition}) {
426 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
427 $form->{addition} = "SAVED";
428 $form->save_history($form->dbconnect(\%myconfig));
430 # /saving the history
432 $lxdebug->leave_sub();
436 $lxdebug->enter_sub();
438 $auth->assert('customer_vendor_edit & sales_order_edit');
440 $form->{script} = "oe.pl";
442 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
444 if(!exists $form->{addition}) {
445 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
446 $form->{addition} = "SAVED";
447 $form->save_history($form->dbconnect(\%myconfig));
449 # /saving the history
451 $lxdebug->leave_sub();
455 $lxdebug->enter_sub();
457 $auth->assert('customer_vendor_edit');
459 $msg = ucfirst $form->{db};
462 $form->isblank("name", $locale->text("Name missing!"));
463 if ($form->{"db"} eq "customer") {
464 $rc = CT->save_customer(\%myconfig, \%$form);
466 $rc = CT->save_vendor(\%myconfig, \%$form);
469 $form->error($locale->text('customernumber not unique!'));
472 if(!exists $form->{addition}) {
473 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
474 $form->{addition} = "SAVED";
475 $form->save_history($form->dbconnect(\%myconfig));
477 # /saving the history
478 $form->redirect($locale->text($msg));
480 $lxdebug->leave_sub();
484 $lxdebug->enter_sub();
486 $auth->assert('customer_vendor_edit');
488 $msg = ucfirst $form->{db};
491 $form->isblank("name", $locale->text("Name missing!"));
494 if ($form->{"db"} eq "customer") {
495 $res = CT->save_customer(\%myconfig, \%$form);
497 $res = CT->save_vendor(\%myconfig, \%$form);
501 if ($form->{"db"} eq "customer") {
502 $form->error($locale->text('This customer number is already in use.'));
504 $form->error($locale->text('This vendor number is already in use.'));
508 if(!exists $form->{addition}) {
509 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
510 $form->{addition} = "SAVED";
511 $form->save_history($form->dbconnect(\%myconfig));
513 # /saving the history
516 $lxdebug->leave_sub();
520 $lxdebug->enter_sub();
522 $auth->assert('customer_vendor_edit');
524 CT->delete(\%myconfig, \%$form);
526 $msg = ucfirst $form->{db};
529 if(!exists $form->{addition}) {
530 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
531 $form->{addition} = "DELETED";
532 $form->save_history($form->dbconnect(\%myconfig));
534 # /saving the history
535 $form->redirect($locale->text($msg));
537 $msg = "Cannot delete $form->{db}";
538 $form->error($locale->text($msg));
540 $lxdebug->leave_sub();
544 $lxdebug->enter_sub();
546 $auth->assert('customer_vendor_edit');
551 $lxdebug->leave_sub();
555 $lxdebug->enter_sub();
557 $auth->assert('customer_vendor_edit');
560 $lxdebug->leave_sub();
564 $lxdebug->enter_sub();
566 $auth->assert('customer_vendor_edit');
568 CT->get_contact(\%myconfig, \%$form);
569 print $cgi->header(), join '__pjx__', map $form->{"cp_$_"},
570 qw(name greeting title givenname phone1 phone2 email abteilung fax mobile1 mobile2 satphone satfax project privatphone privatemail birthday used);
571 $lxdebug->leave_sub();
576 $lxdebug->enter_sub();
578 $auth->assert('customer_vendor_edit');
580 CT->get_shipto(\%myconfig, \%$form);
581 print $cgi->header(), join '__pjx__', map $form->{"shipto$_"},
582 qw(name department_1 department_2 street zipcode city country contact phone fax email used);
583 $lxdebug->leave_sub();
588 $lxdebug->enter_sub();
590 $auth->assert('customer_vendor_edit');
592 CT->get_delivery(\%myconfig, \%$form );
594 print $cgi->header(), $form->parse_html_template('ct/get_delivery');
596 $lxdebug->leave_sub();
600 $main::lxdebug->enter_sub();
602 $auth->assert('customer_vendor_edit');
604 CT->get_shipto(\%myconfig, \%$form);
606 unless ($form->{shiptoused}) {
607 CT->delete_shipto($form->{shipto_id});
608 @$form{ grep /^shipto/, keys %$form } = undef;
613 $main::lxdebug->leave_sub();
617 $main::lxdebug->enter_sub();
619 $auth->assert('customer_vendor_edit');
621 CT->get_contact(\%myconfig, \%$form);
623 unless ($form->{cp_used}) {
624 CT->delete_shipto($form->{cp_id});
625 @$form{ grep /^cp_/, keys %$form } = undef;
630 $main::lxdebug->leave_sub();
633 sub continue { call_sub($form->{nextsub}); }