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')
42 use POSIX qw(strftime);
46 use SL::ReportGenerator;
48 require "bin/mozilla/common.pl";
49 require "bin/mozilla/reportgenerator.pl";
56 $lxdebug->enter_sub();
58 $auth->assert('customer_vendor_edit');
60 $form->{title} = "Add";
63 "$form->{script}?action=add&db=$form->{db}"
64 unless $form->{callback};
66 CT->populate_drop_down_boxes(\%myconfig, \%$form);
71 $lxdebug->leave_sub();
75 $lxdebug->enter_sub();
77 $auth->assert('customer_vendor_edit');
79 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
81 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
82 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
84 $form->{CUSTOM_VARIABLES} = CVar->get_configs('module' => 'CT');
85 ($form->{CUSTOM_VARIABLES_FILTER_CODE},
86 $form->{CUSTOM_VARIABLES_INCLUSION_CODE}) = CVar->render_search_options('variables' => $form->{CUSTOM_VARIABLES},
87 'include_prefix' => 'l_',
88 'include_value' => 'Y');
90 $form->{jsscript} = 1;
91 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
92 $form->{fokus} = 'Form.name';
95 print $form->parse_html_template('ct/search');
97 $lxdebug->leave_sub();
101 $lxdebug->enter_sub();
103 $auth->assert('customer_vendor_edit');
105 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
107 CT->search(\%myconfig, \%$form);
109 my $cvar_configs = CVar->get_configs('module' => 'CT');
112 if ($form->{status} eq 'all') {
113 push @options, $locale->text('All');
115 } elsif ($form->{status} eq 'orphaned') {
116 push @options, $locale->text('Orphaned');
120 push @options, $locale->text('Name') . " : $form->{name}";
122 if ($form->{contact}) {
123 push @options, $locale->text('Contact') . " : $form->{contact}";
125 if ($form->{"$form->{db}number"}) {
126 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|;
128 if ($form->{email}) {
129 push @options, $locale->text('E-mail') . " : $form->{email}";
133 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
134 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
135 'ordnumber', 'quonumber'
138 my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs };
139 my %column_defs_cvars = map { +"cvar_$_->{name}" => { 'text' => $_->{description} } } @includeable_custom_variables;
141 push @columns, map { "cvar_$_->{name}" } @includeable_custom_variables;
144 'id' => { 'text' => $locale->text('ID'), },
145 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
146 'name' => { 'text' => $locale->text('Name'), },
147 'address' => { 'text' => $locale->text('Address'), },
148 'contact' => { 'text' => $locale->text('Contact'), },
149 'phone' => { 'text' => $locale->text('Phone'), },
150 'fax' => { 'text' => $locale->text('Fax'), },
151 'email' => { 'text' => $locale->text('E-mail'), },
152 'cc' => { 'text' => $locale->text('Cc'), },
153 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
154 'sic_code' => { 'text' => $locale->text('SIC'), },
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'), },
162 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
164 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
165 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
166 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
167 $form->{callback} = "$callback&sort=" . E($form->{sort});
169 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
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}, 1);
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 # $locale->text('Edit Customer')
246 # $locale->text('Edit Vendor')
248 CT->get_tuple(\%myconfig, \%$form);
249 CT->populate_drop_down_boxes(\%myconfig, \%$form);
251 # format " into "
252 map { $form->{$_} =~ s/\"/"/g } keys %$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_SALESMEN",
271 "taxzones" => "ALL_TAXZONES");
272 $form->get_pricegroup(\%myconfig, { all => 1 });
274 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
275 $form->{is_admin} = $myconfig{role} eq 'admin';
276 $form->{is_customer} = $form->{db} eq 'customer';
277 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
278 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
279 $form->{taxzone_id} = 0 if !$form->{id};
280 $form->{jsscript} = 1;
281 $form->{fokus} = "ct.greeting";
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 push @{ $form->{AJAX} }, map {
287 new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" )
288 } qw(shipto contact delivery);
290 $form->{title} = $form->{title_save}
291 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
293 ## LINET: Create a drop-down box with all prior titles and greetings.
294 CT->query_titles_and_greetings(\%myconfig, \%$form);
295 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
298 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
300 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
302 $main::lxdebug->dump(0, "cvar", $form->{CUSTOM_VARIABLES});
305 print $form->parse_html_template('ct/form_header');
307 $lxdebug->leave_sub();
311 $lxdebug->enter_sub();
313 $auth->assert('customer_vendor_edit');
315 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
316 is_customer => $form->{db} eq 'customer' });
317 $lxdebug->leave_sub();
320 sub add_transaction {
321 $lxdebug->enter_sub();
323 $auth->assert('customer_vendor_edit & general_ledger');
325 # # saving the history
326 # if(!exists $form->{addition}) {
327 # $form->{addition} = "ADD TRANSACTION";
328 # $form->save_history($form->dbconnect(\%myconfig));
330 # # /saving the history
332 $form->isblank("name", $locale->text("Name missing!"));
333 if ($form->{"db"} eq "customer") {
334 CT->save_customer(\%myconfig, \%$form);
336 CT->save_vendor(\%myconfig, \%$form);
339 $form->{callback} = $form->escape($form->{callback}, 1);
340 $name = $form->escape("$form->{name}", 1);
343 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
346 $lxdebug->leave_sub();
349 sub save_and_ap_transaction {
350 $lxdebug->enter_sub();
352 $auth->assert('customer_vendor_edit & general_ledger');
354 $form->{script} = "ap.pl";
356 if(!exists $form->{addition}) {
357 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
358 $form->{addition} = "SAVED";
359 $form->save_history($form->dbconnect(\%myconfig));
361 # /saving the history
363 $lxdebug->leave_sub();
366 sub save_and_ar_transaction {
367 $lxdebug->enter_sub();
369 $auth->assert('customer_vendor_edit & general_ledger');
371 $form->{script} = "ar.pl";
373 if(!exists $form->{addition}) {
374 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
375 $form->{addition} = "SAVED";
376 $form->save_history($form->dbconnect(\%myconfig));
378 # /saving the history
380 $lxdebug->leave_sub();
383 sub save_and_invoice {
384 $lxdebug->enter_sub();
386 if ($form->{db} eq 'customer') {
387 $auth->assert('customer_vendor_edit & invoice_edit');
389 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
392 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
393 $form->{type} = "invoice";
395 if(!exists $form->{addition}) {
396 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
397 $form->{addition} = "SAVED";
398 $form->save_history($form->dbconnect(\%myconfig));
400 # /saving the history
402 $lxdebug->leave_sub();
406 $lxdebug->enter_sub();
408 $auth->assert('customer_vendor_edit & request_quotation_edit');
410 $form->{script} = "oe.pl";
411 $form->{type} = "request_quotation";
413 if(!exists $form->{addition}) {
414 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
415 $form->{addition} = "SAVED";
416 $form->save_history($form->dbconnect(\%myconfig));
418 # /saving the history
420 $lxdebug->leave_sub();
423 sub save_and_quotation {
424 $lxdebug->enter_sub();
426 $auth->assert('customer_vendor_edit & sales_quotation_edit');
428 $form->{script} = "oe.pl";
429 $form->{type} = "sales_quotation";
431 if(!exists $form->{addition}) {
432 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
433 $form->{addition} = "SAVED";
434 $form->save_history($form->dbconnect(\%myconfig));
436 # /saving the history
438 $lxdebug->leave_sub();
442 $lxdebug->enter_sub();
444 $auth->assert('customer_vendor_edit & sales_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 # $locale->text('Customer saved!')
466 # $locale->text('Vendor saved!')
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 # $locale->text('Customer saved!')
498 # $locale->text('Vendor saved!')
500 $msg = ucfirst $form->{db};
503 $form->isblank("name", $locale->text("Name missing!"));
506 if ($form->{"db"} eq "customer") {
507 $res = CT->save_customer(\%myconfig, \%$form);
509 $res = CT->save_vendor(\%myconfig, \%$form);
513 if ($form->{"db"} eq "customer") {
514 $form->error($locale->text('This customer number is already in use.'));
516 $form->error($locale->text('This vendor number is already in use.'));
520 if(!exists $form->{addition}) {
521 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
522 $form->{addition} = "SAVED";
523 $form->save_history($form->dbconnect(\%myconfig));
525 # /saving the history
528 $lxdebug->leave_sub();
532 $lxdebug->enter_sub();
534 $auth->assert('customer_vendor_edit');
536 # $locale->text('Customer deleted!')
537 # $locale->text('Cannot delete customer!')
538 # $locale->text('Vendor deleted!')
539 # $locale->text('Cannot delete vendor!')
541 CT->delete(\%myconfig, \%$form);
543 $msg = ucfirst $form->{db};
546 if(!exists $form->{addition}) {
547 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
548 $form->{addition} = "DELETED";
549 $form->save_history($form->dbconnect(\%myconfig));
551 # /saving the history
552 $form->redirect($locale->text($msg));
554 $msg = "Cannot delete $form->{db}";
555 $form->error($locale->text($msg));
557 $lxdebug->leave_sub();
561 $lxdebug->enter_sub();
563 $auth->assert('customer_vendor_edit');
568 $lxdebug->leave_sub();
572 $lxdebug->enter_sub();
574 $auth->assert('customer_vendor_edit');
577 $lxdebug->leave_sub();
581 $lxdebug->enter_sub();
583 $auth->assert('customer_vendor_edit');
585 CT->get_contact(\%myconfig, \%$form);
588 $result = "$form->{cp_name}";
589 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);
592 $lxdebug->leave_sub();
597 $lxdebug->enter_sub();
599 $auth->assert('customer_vendor_edit');
601 CT->get_shipto(\%myconfig, \%$form);
604 $result = "$form->{shiptoname}";
605 map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
608 $lxdebug->leave_sub();
613 $lxdebug->enter_sub();
615 $auth->assert('customer_vendor_edit');
617 CT->get_delivery(\%myconfig, \%$form );
619 print CGI->new->header();
620 print $form->parse_html_template('ct/get_delivery');
621 $lxdebug->leave_sub();
624 sub continue { call_sub($form->{nextsub}); }