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_EMPLOYEES",
271 "taxzones" => "ALL_TAXZONES");
272 $form->get_pricegroup(\%myconfig, { all => 1 });
274 $form->{ALL_SALESMEN} = $form->{ALL_EMPLOYEES};
275 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
276 $form->{is_admin} = $myconfig{role} eq 'admin';
277 $form->{is_customer} = $form->{db} eq 'customer';
278 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
279 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
280 $form->{taxzone_id} = 0 if !$form->{id};
281 $form->{jsscript} = 1;
282 $form->{fokus} = "ct.greeting";
284 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
285 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
287 push @{ $form->{AJAX} }, map {
288 new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" )
289 } qw(shipto contact delivery);
291 $form->{title} = $form->{title_save}
292 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
294 ## LINET: Create a drop-down box with all prior titles and greetings.
295 CT->query_titles_and_greetings(\%myconfig, \%$form);
296 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
299 $form->{NOTES} ||= [ ];
301 $form->{CUSTOM_VARIABLES} = CVar->get_custom_variables('module' => 'CT', 'trans_id' => $form->{id});
303 CVar->render_inputs('variables' => $form->{CUSTOM_VARIABLES}) if (scalar @{ $form->{CUSTOM_VARIABLES} });
306 print $form->parse_html_template('ct/form_header');
308 $lxdebug->leave_sub();
312 $lxdebug->enter_sub();
314 $auth->assert('customer_vendor_edit');
316 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
317 is_customer => $form->{db} eq 'customer' });
318 $lxdebug->leave_sub();
321 sub add_transaction {
322 $lxdebug->enter_sub();
324 $auth->assert('customer_vendor_edit & general_ledger');
326 # # saving the history
327 # if(!exists $form->{addition}) {
328 # $form->{addition} = "ADD TRANSACTION";
329 # $form->save_history($form->dbconnect(\%myconfig));
331 # # /saving the history
333 $form->isblank("name", $locale->text("Name missing!"));
334 if ($form->{"db"} eq "customer") {
335 CT->save_customer(\%myconfig, \%$form);
337 CT->save_vendor(\%myconfig, \%$form);
340 $form->{callback} = $form->escape($form->{callback}, 1);
341 $name = $form->escape("$form->{name}", 1);
344 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
347 $lxdebug->leave_sub();
350 sub save_and_ap_transaction {
351 $lxdebug->enter_sub();
353 $auth->assert('customer_vendor_edit & general_ledger');
355 $form->{script} = "ap.pl";
357 if(!exists $form->{addition}) {
358 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
359 $form->{addition} = "SAVED";
360 $form->save_history($form->dbconnect(\%myconfig));
362 # /saving the history
364 $lxdebug->leave_sub();
367 sub save_and_ar_transaction {
368 $lxdebug->enter_sub();
370 $auth->assert('customer_vendor_edit & general_ledger');
372 $form->{script} = "ar.pl";
374 if(!exists $form->{addition}) {
375 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
376 $form->{addition} = "SAVED";
377 $form->save_history($form->dbconnect(\%myconfig));
379 # /saving the history
381 $lxdebug->leave_sub();
384 sub save_and_invoice {
385 $lxdebug->enter_sub();
387 if ($form->{db} eq 'customer') {
388 $auth->assert('customer_vendor_edit & invoice_edit');
390 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
393 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
394 $form->{type} = "invoice";
396 if(!exists $form->{addition}) {
397 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
398 $form->{addition} = "SAVED";
399 $form->save_history($form->dbconnect(\%myconfig));
401 # /saving the history
403 $lxdebug->leave_sub();
407 $lxdebug->enter_sub();
409 $auth->assert('customer_vendor_edit & request_quotation_edit');
411 $form->{script} = "oe.pl";
412 $form->{type} = "request_quotation";
414 if(!exists $form->{addition}) {
415 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
416 $form->{addition} = "SAVED";
417 $form->save_history($form->dbconnect(\%myconfig));
419 # /saving the history
421 $lxdebug->leave_sub();
424 sub save_and_quotation {
425 $lxdebug->enter_sub();
427 $auth->assert('customer_vendor_edit & sales_quotation_edit');
429 $form->{script} = "oe.pl";
430 $form->{type} = "sales_quotation";
432 if(!exists $form->{addition}) {
433 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
434 $form->{addition} = "SAVED";
435 $form->save_history($form->dbconnect(\%myconfig));
437 # /saving the history
439 $lxdebug->leave_sub();
443 $lxdebug->enter_sub();
445 $auth->assert('customer_vendor_edit & sales_order_edit');
447 $form->{script} = "oe.pl";
449 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
451 if(!exists $form->{addition}) {
452 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
453 $form->{addition} = "SAVED";
454 $form->save_history($form->dbconnect(\%myconfig));
456 # /saving the history
458 $lxdebug->leave_sub();
462 $lxdebug->enter_sub();
464 $auth->assert('customer_vendor_edit');
466 # $locale->text('Customer saved!')
467 # $locale->text('Vendor saved!')
469 $msg = ucfirst $form->{db};
472 $form->isblank("name", $locale->text("Name missing!"));
473 if ($form->{"db"} eq "customer") {
474 $rc = CT->save_customer(\%myconfig, \%$form);
476 $rc = CT->save_vendor(\%myconfig, \%$form);
479 $form->error($locale->text('customernumber not unique!'));
482 if(!exists $form->{addition}) {
483 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
484 $form->{addition} = "SAVED";
485 $form->save_history($form->dbconnect(\%myconfig));
487 # /saving the history
488 $form->redirect($locale->text($msg));
490 $lxdebug->leave_sub();
494 $lxdebug->enter_sub();
496 $auth->assert('customer_vendor_edit');
498 # $locale->text('Customer saved!')
499 # $locale->text('Vendor saved!')
501 $msg = ucfirst $form->{db};
504 $form->isblank("name", $locale->text("Name missing!"));
507 if ($form->{"db"} eq "customer") {
508 $res = CT->save_customer(\%myconfig, \%$form);
510 $res = CT->save_vendor(\%myconfig, \%$form);
514 if ($form->{"db"} eq "customer") {
515 $form->error($locale->text('This customer number is already in use.'));
517 $form->error($locale->text('This vendor number is already in use.'));
521 if(!exists $form->{addition}) {
522 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
523 $form->{addition} = "SAVED";
524 $form->save_history($form->dbconnect(\%myconfig));
526 # /saving the history
529 $lxdebug->leave_sub();
533 $lxdebug->enter_sub();
535 $auth->assert('customer_vendor_edit');
537 # $locale->text('Customer deleted!')
538 # $locale->text('Cannot delete customer!')
539 # $locale->text('Vendor deleted!')
540 # $locale->text('Cannot delete vendor!')
542 CT->delete(\%myconfig, \%$form);
544 $msg = ucfirst $form->{db};
547 if(!exists $form->{addition}) {
548 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
549 $form->{addition} = "DELETED";
550 $form->save_history($form->dbconnect(\%myconfig));
552 # /saving the history
553 $form->redirect($locale->text($msg));
555 $msg = "Cannot delete $form->{db}";
556 $form->error($locale->text($msg));
558 $lxdebug->leave_sub();
562 $lxdebug->enter_sub();
564 $auth->assert('customer_vendor_edit');
569 $lxdebug->leave_sub();
573 $lxdebug->enter_sub();
575 $auth->assert('customer_vendor_edit');
578 $lxdebug->leave_sub();
582 $lxdebug->enter_sub();
584 $auth->assert('customer_vendor_edit');
586 CT->get_contact(\%myconfig, \%$form);
589 $result = "$form->{cp_name}";
590 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);
593 $lxdebug->leave_sub();
598 $lxdebug->enter_sub();
600 $auth->assert('customer_vendor_edit');
602 CT->get_shipto(\%myconfig, \%$form);
605 $result = "$form->{shiptoname}";
606 map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
609 $lxdebug->leave_sub();
614 $lxdebug->enter_sub();
616 $auth->assert('customer_vendor_edit');
618 CT->get_delivery(\%myconfig, \%$form );
620 print CGI->new->header();
621 print $form->parse_html_template('ct/get_delivery');
622 $lxdebug->leave_sub();
625 sub continue { call_sub($form->{nextsub}); }