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')
41 use POSIX qw(strftime);
45 use SL::ReportGenerator;
47 require "bin/mozilla/common.pl";
48 require "bin/mozilla/reportgenerator.pl";
55 $lxdebug->enter_sub();
57 $auth->assert('customer_vendor_edit');
59 $form->{title} = "Add";
62 "$form->{script}?action=add&db=$form->{db}"
63 unless $form->{callback};
65 CT->populate_drop_down_boxes(\%myconfig, \%$form);
70 $lxdebug->leave_sub();
74 $lxdebug->enter_sub();
76 $auth->assert('customer_vendor_edit');
78 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
80 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
81 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
83 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
84 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
85 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
86 'include_prefix' => 'l_',
87 'include_value' => 'Y');
89 $form->{jsscript} = 1;
90 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
91 $form->{fokus} = 'Form.name';
94 print $form->parse_html_template('ct/search');
96 $lxdebug->leave_sub();
100 $lxdebug->enter_sub();
102 $auth->assert('customer_vendor_edit');
104 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
106 CT->search(\%myconfig, \%$form);
108 my $cvar_configs = CVar->get_configs('module' => 'CT');
111 if ($form->{status} eq 'all') {
112 push @options, $locale->text('All');
113 } elsif ($form->{status} eq 'orphaned') {
114 push @options, $locale->text('Orphaned');
117 push @options, $locale->text('Name') . " : $form->{name}" if $form->{name};
118 push @options, $locale->text('Contact') . " : $form->{contact}" if $form->{contact};
119 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}| if $form->{"$form->{db}number"};
120 push @options, $locale->text('E-mail') . " : $form->{email}" if $form->{email};
123 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
124 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
125 'ordnumber', 'quonumber'
128 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
129 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
131 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
134 'id' => { 'text' => $locale->text('ID'), },
135 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
136 'name' => { 'text' => $locale->text('Name'), },
137 'address' => { 'text' => $locale->text('Address'), },
138 'contact' => { 'text' => $locale->text('Contact'), },
139 'phone' => { 'text' => $locale->text('Phone'), },
140 'fax' => { 'text' => $locale->text('Fax'), },
141 'email' => { 'text' => $locale->text('E-mail'), },
142 'cc' => { 'text' => $locale->text('Cc'), },
143 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
144 'sic_code' => { 'text' => $locale->text('SIC'), },
145 'business' => { 'text' => $locale->text('Type of Business'), },
146 'invnumber' => { 'text' => $locale->text('Invoice'), },
147 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
148 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
152 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
154 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
155 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
156 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
157 $form->{callback} = "$callback&sort=" . E($form->{sort});
159 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
161 my ($ordertype, $quotationtype, $attachment_basename);
162 if ($form->{IS_CUSTOMER}) {
163 $form->{title} = $locale->text('Customers');
164 $ordertype = 'sales_order';
165 $quotationtype = 'sales_quotation';
166 $attachment_basename = $locale->text('customer_list');
169 $form->{title} = $locale->text('Vendors');
170 $ordertype = 'purchase_order';
171 $quotationtype = 'request_quotation';
172 $attachment_basename = $locale->text('vendor_list');
175 my $report = SL::ReportGenerator->new(\%myconfig, $form);
177 $report->set_options('top_info_text' => join("\n", @options),
178 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
179 'output_format' => 'HTML',
180 'title' => $form->{title},
181 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
183 $report->set_options_from_form();
185 $report->set_columns(%column_defs);
186 $report->set_column_order(@columns);
188 $report->set_export_options('list_names', @hidden_variables);
190 $report->set_sort_indicator($form->{sort}, 1);
192 CVar->add_custom_variables_to_report('module' => 'CT',
193 'trans_id_field' => 'id',
194 'configs' => $cvar_configs,
195 'column_defs' => \%column_defs,
196 'data' => $form->{CT});
200 foreach my $ref (@{ $form->{CT} }) {
201 my $row = { map { $_ => { 'data' => '' } } @columns };
203 if ($ref->{id} ne $previous_id) {
204 $previous_id = $ref->{id};
205 map { $row->{$_}->{data} = $ref->{$_} } @columns;
207 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
208 $row->{email}->{link} = 'mailto:' . E($ref->{email});
211 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
212 $row->{invnumber}->{link} = $base_url;
213 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
214 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
215 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
216 $row->{$column}->{data} = $ref->{$column};
218 $report->add_data($row);
221 $report->generate_with_headers();
223 $lxdebug->leave_sub();
227 $lxdebug->enter_sub();
229 $auth->assert('customer_vendor_edit');
231 # show history button
232 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
233 #/show hhistory button
235 # $locale->text('Edit Customer')
236 # $locale->text('Edit Vendor')
238 CT->get_tuple(\%myconfig, \%$form);
239 CT->populate_drop_down_boxes(\%myconfig, \%$form);
241 # format " into "
242 map { $form->{$_} =~ s/\"/"/g } keys %$form;
244 $form->{title} = "Edit";
247 $form->{discount} *= 100;
252 $lxdebug->leave_sub();
256 $lxdebug->enter_sub();
258 $auth->assert('customer_vendor_edit');
260 $form->get_lists("employees" => "ALL_EMPLOYEES",
261 "taxzones" => "ALL_TAXZONES");
262 $form->get_pricegroup(\%myconfig, { all => 1 });
264 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
265 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
266 $form->{is_admin} = $myconfig{role} eq 'admin';
267 $form->{is_customer} = $form->{db} eq 'customer';
268 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
269 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
270 $form->{taxzone_id} = 0 if !$form->{id};
271 $form->{jsscript} = 1;
272 $form->{fokus} = "ct.greeting";
274 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
275 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
277 push @{ $form->{AJAX} }, map {
278 new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" )
279 } qw(shipto contact delivery);
281 $form->{title} = $form->{title_save}
282 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
284 ## LINET: Create a drop-down box with all prior titles and greetings.
285 CT->query_titles_and_greetings(\%myconfig, \%$form);
286 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
289 $form->{NOTES} ||= [ ];
291 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
293 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
296 print $form->parse_html_template('ct/form_header');
298 $lxdebug->leave_sub();
302 $lxdebug->enter_sub();
304 $auth->assert('customer_vendor_edit');
306 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
307 is_customer => $form->{db} eq 'customer' });
308 $lxdebug->leave_sub();
311 sub add_transaction {
312 $lxdebug->enter_sub();
314 $auth->assert('customer_vendor_edit & general_ledger');
316 # # saving the history
317 # if(!exists $form->{addition}) {
318 # $form->{addition} = "ADD TRANSACTION";
319 # $form->save_history($form->dbconnect(\%myconfig));
321 # # /saving the history
323 $form->isblank("name", $locale->text("Name missing!"));
324 if ($form->{"db"} eq "customer") {
325 CT->save_customer(\%myconfig, \%$form);
327 CT->save_vendor(\%myconfig, \%$form);
330 $form->{callback} = $form->escape($form->{callback}, 1);
331 $name = $form->escape("$form->{name}", 1);
334 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
337 $lxdebug->leave_sub();
340 sub save_and_ap_transaction {
341 $lxdebug->enter_sub();
343 $auth->assert('customer_vendor_edit & general_ledger');
345 $form->{script} = "ap.pl";
347 if(!exists $form->{addition}) {
348 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
349 $form->{addition} = "SAVED";
350 $form->save_history($form->dbconnect(\%myconfig));
352 # /saving the history
354 $lxdebug->leave_sub();
357 sub save_and_ar_transaction {
358 $lxdebug->enter_sub();
360 $auth->assert('customer_vendor_edit & general_ledger');
362 $form->{script} = "ar.pl";
364 if(!exists $form->{addition}) {
365 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
366 $form->{addition} = "SAVED";
367 $form->save_history($form->dbconnect(\%myconfig));
369 # /saving the history
371 $lxdebug->leave_sub();
374 sub save_and_invoice {
375 $lxdebug->enter_sub();
377 if ($form->{db} eq 'customer') {
378 $auth->assert('customer_vendor_edit & invoice_edit');
380 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
383 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
384 $form->{type} = "invoice";
386 if(!exists $form->{addition}) {
387 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
388 $form->{addition} = "SAVED";
389 $form->save_history($form->dbconnect(\%myconfig));
391 # /saving the history
393 $lxdebug->leave_sub();
397 $lxdebug->enter_sub();
399 $auth->assert('customer_vendor_edit & request_quotation_edit');
401 $form->{script} = "oe.pl";
402 $form->{type} = "request_quotation";
404 if(!exists $form->{addition}) {
405 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
406 $form->{addition} = "SAVED";
407 $form->save_history($form->dbconnect(\%myconfig));
409 # /saving the history
411 $lxdebug->leave_sub();
414 sub save_and_quotation {
415 $lxdebug->enter_sub();
417 $auth->assert('customer_vendor_edit & sales_quotation_edit');
419 $form->{script} = "oe.pl";
420 $form->{type} = "sales_quotation";
422 if(!exists $form->{addition}) {
423 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
424 $form->{addition} = "SAVED";
425 $form->save_history($form->dbconnect(\%myconfig));
427 # /saving the history
429 $lxdebug->leave_sub();
433 $lxdebug->enter_sub();
435 $auth->assert('customer_vendor_edit & sales_order_edit');
437 $form->{script} = "oe.pl";
439 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
441 if(!exists $form->{addition}) {
442 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
443 $form->{addition} = "SAVED";
444 $form->save_history($form->dbconnect(\%myconfig));
446 # /saving the history
448 $lxdebug->leave_sub();
452 $lxdebug->enter_sub();
454 $auth->assert('customer_vendor_edit');
456 # $locale->text('Customer saved!')
457 # $locale->text('Vendor saved!')
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 # $locale->text('Customer saved!')
489 # $locale->text('Vendor saved!')
491 $msg = ucfirst $form->{db};
494 $form->isblank("name", $locale->text("Name missing!"));
497 if ($form->{"db"} eq "customer") {
498 $res = CT->save_customer(\%myconfig, \%$form);
500 $res = CT->save_vendor(\%myconfig, \%$form);
504 if ($form->{"db"} eq "customer") {
505 $form->error($locale->text('This customer number is already in use.'));
507 $form->error($locale->text('This vendor number is already in use.'));
511 if(!exists $form->{addition}) {
512 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
513 $form->{addition} = "SAVED";
514 $form->save_history($form->dbconnect(\%myconfig));
516 # /saving the history
519 $lxdebug->leave_sub();
523 $lxdebug->enter_sub();
525 $auth->assert('customer_vendor_edit');
527 # $locale->text('Customer deleted!')
528 # $locale->text('Cannot delete customer!')
529 # $locale->text('Vendor deleted!')
530 # $locale->text('Cannot delete vendor!')
532 CT->delete(\%myconfig, \%$form);
534 $msg = ucfirst $form->{db};
537 if(!exists $form->{addition}) {
538 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
539 $form->{addition} = "DELETED";
540 $form->save_history($form->dbconnect(\%myconfig));
542 # /saving the history
543 $form->redirect($locale->text($msg));
545 $msg = "Cannot delete $form->{db}";
546 $form->error($locale->text($msg));
548 $lxdebug->leave_sub();
552 $lxdebug->enter_sub();
554 $auth->assert('customer_vendor_edit');
559 $lxdebug->leave_sub();
563 $lxdebug->enter_sub();
565 $auth->assert('customer_vendor_edit');
568 $lxdebug->leave_sub();
572 $lxdebug->enter_sub();
574 $auth->assert('customer_vendor_edit');
576 CT->get_contact(\%myconfig, \%$form);
578 $result = "$form->{cp_name}";
579 map { $result .= "__pjx__" . $form->{$_} } qw(cp_greeting cp_title cp_givenname cp_phone1 cp_phone2 cp_email cp_abteilung cp_fax cp_mobile1 cp_mobile2 cp_satphone cp_satfax cp_project cp_privatphone cp_privatemail cp_birthday);
580 print $cgi->header(), $result;
581 $lxdebug->leave_sub();
586 $lxdebug->enter_sub();
588 $auth->assert('customer_vendor_edit');
590 CT->get_shipto(\%myconfig, \%$form);
592 $result = "$form->{shiptoname}";
593 map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
594 print $cgi->header(), $result;
595 $lxdebug->leave_sub();
600 $lxdebug->enter_sub();
602 $auth->assert('customer_vendor_edit');
604 CT->get_delivery(\%myconfig, \%$form );
606 print $cgi->header(), $form->parse_html_template('ct/get_delivery');
608 $lxdebug->leave_sub();
611 sub continue { call_sub($form->{nextsub}); }