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);
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->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
84 $form->{fokus} = 'Form.name';
87 print $form->parse_html_template('ct/search');
89 $lxdebug->leave_sub();
93 $lxdebug->enter_sub();
95 $auth->assert('customer_vendor_edit');
97 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
99 CT->search(\%myconfig, \%$form);
102 if ($form->{status} eq 'all') {
103 push @options, $locale->text('All');
105 } elsif ($form->{status} eq 'orphaned') {
106 push @options, $locale->text('Orphaned');
110 push @options, $locale->text('Name') . " : $form->{name}";
112 if ($form->{contact}) {
113 push @options, $locale->text('Contact') . " : $form->{contact}";
115 if ($form->{"$form->{db}number"}) {
116 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|;
118 if ($form->{email}) {
119 push @options, $locale->text('E-mail') . " : $form->{email}";
123 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
124 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
125 'ordnumber', 'quonumber'
129 'id' => { 'text' => $locale->text('ID'), },
130 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
131 'name' => { 'text' => $locale->text('Name'), },
132 'address' => { 'text' => $locale->text('Address'), },
133 'contact' => { 'text' => $locale->text('Contact'), },
134 'phone' => { 'text' => $locale->text('Phone'), },
135 'fax' => { 'text' => $locale->text('Fax'), },
136 'email' => { 'text' => $locale->text('E-mail'), },
137 'cc' => { 'text' => $locale->text('Cc'), },
138 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
139 'sic_code' => { 'text' => $locale->text('SIC'), },
140 'business' => { 'text' => $locale->text('Type of Business'), },
141 'invnumber' => { 'text' => $locale->text('Invoice'), },
142 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
143 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
146 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
148 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
149 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
150 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
151 $form->{callback} = "$callback&sort=" . E($form->{sort});
153 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
155 my ($ordertype, $quotationtype, $attachment_basename);
156 if ($form->{IS_CUSTOMER}) {
157 $form->{title} = $locale->text('Customers');
158 $ordertype = 'sales_order';
159 $quotationtype = 'sales_quotation';
160 $attachment_basename = $locale->text('customer_list');
163 $form->{title} = $locale->text('Vendors');
164 $ordertype = 'purchase_order';
165 $quotationtype = 'request_quotation';
166 $attachment_basename = $locale->text('vendor_list');
169 my $report = SL::ReportGenerator->new(\%myconfig, $form);
171 $report->set_options('top_info_text' => join("\n", @options),
172 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
173 'output_format' => 'HTML',
174 'title' => $form->{title},
175 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
177 $report->set_options_from_form();
179 $report->set_columns(%column_defs);
180 $report->set_column_order(@columns);
182 $report->set_export_options('list_names', @hidden_variables);
184 $report->set_sort_indicator($form->{sort}, 1);
188 foreach my $ref (@{ $form->{CT} }) {
189 my $row = { map { $_ => { 'data' => '' } } @columns };
191 if ($ref->{id} ne $previous_id) {
192 $previous_id = $ref->{id};
193 map { $row->{$_}->{data} = $ref->{$_} } @columns;
195 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
196 $row->{email}->{link} = 'mailto:' . E($ref->{email});
199 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
200 $row->{invnumber}->{link} = $base_url;
201 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
202 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
203 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
204 $row->{$column}->{data} = $ref->{$column};
206 $report->add_data($row);
209 $report->generate_with_headers();
211 $lxdebug->leave_sub();
215 $lxdebug->enter_sub();
217 $auth->assert('customer_vendor_edit');
219 # show history button
220 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
221 #/show hhistory button
223 # $locale->text('Edit Customer')
224 # $locale->text('Edit Vendor')
226 CT->get_tuple(\%myconfig, \%$form);
227 CT->populate_drop_down_boxes(\%myconfig, \%$form);
229 # format " into "
230 map { $form->{$_} =~ s/\"/"/g } keys %$form;
232 $form->{title} = "Edit";
235 $form->{discount} *= 100;
240 $lxdebug->leave_sub();
244 $lxdebug->enter_sub();
246 $auth->assert('customer_vendor_edit');
248 $form->get_lists("employees" => "ALL_SALESMEN",
249 "taxzones" => "ALL_TAXZONES");
250 $form->get_pricegroup(\%myconfig, { all => 1 });
252 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
253 $form->{is_admin} = $myconfig{role} eq 'admin';
254 $form->{is_customer} = $form->{db} eq 'customer';
255 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
256 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
257 $form->{taxzone_id} = 0 if !$form->{id};
258 $form->{jsscript} = 1;
259 $form->{fokus} = "ct.greeting";
261 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
262 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
264 push @{ $form->{AJAX} }, map {
265 new CGI::Ajax( "get_$_" => "$form->{script}?action=get_$_" )
266 } qw(shipto contact delivery);
268 $form->{title} = $form->{title_save}
269 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
271 ## LINET: Create a drop-down box with all prior titles and greetings.
272 CT->query_titles_and_greetings(\%myconfig, \%$form);
273 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
277 print $form->parse_html_template('ct/form_header');
279 $lxdebug->leave_sub();
283 $lxdebug->enter_sub();
285 $auth->assert('customer_vendor_edit');
287 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
288 is_customer => $form->{db} eq 'customer' });
289 $lxdebug->leave_sub();
292 sub add_transaction {
293 $lxdebug->enter_sub();
295 $auth->assert('customer_vendor_edit & general_ledger');
297 # # saving the history
298 # if(!exists $form->{addition}) {
299 # $form->{addition} = "ADD TRANSACTION";
300 # $form->save_history($form->dbconnect(\%myconfig));
302 # # /saving the history
304 $form->isblank("name", $locale->text("Name missing!"));
305 if ($form->{"db"} eq "customer") {
306 CT->save_customer(\%myconfig, \%$form);
308 CT->save_vendor(\%myconfig, \%$form);
311 $form->{callback} = $form->escape($form->{callback}, 1);
312 $name = $form->escape("$form->{name}", 1);
315 "$form->{script}?action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
318 $lxdebug->leave_sub();
321 sub save_and_ap_transaction {
322 $lxdebug->enter_sub();
324 $auth->assert('customer_vendor_edit & general_ledger');
326 $form->{script} = "ap.pl";
328 if(!exists $form->{addition}) {
329 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
330 $form->{addition} = "SAVED";
331 $form->save_history($form->dbconnect(\%myconfig));
333 # /saving the history
335 $lxdebug->leave_sub();
338 sub save_and_ar_transaction {
339 $lxdebug->enter_sub();
341 $auth->assert('customer_vendor_edit & general_ledger');
343 $form->{script} = "ar.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_invoice {
356 $lxdebug->enter_sub();
358 if ($form->{db} eq 'customer') {
359 $auth->assert('customer_vendor_edit & invoice_edit');
361 $auth->assert('customer_vendor_edit & vendor_invoice_edit');
364 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
365 $form->{type} = "invoice";
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();
378 $lxdebug->enter_sub();
380 $auth->assert('customer_vendor_edit & request_quotation_edit');
382 $form->{script} = "oe.pl";
383 $form->{type} = "request_quotation";
385 if(!exists $form->{addition}) {
386 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
387 $form->{addition} = "SAVED";
388 $form->save_history($form->dbconnect(\%myconfig));
390 # /saving the history
392 $lxdebug->leave_sub();
395 sub save_and_quotation {
396 $lxdebug->enter_sub();
398 $auth->assert('customer_vendor_edit & sales_quotation_edit');
400 $form->{script} = "oe.pl";
401 $form->{type} = "sales_quotation";
403 if(!exists $form->{addition}) {
404 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
405 $form->{addition} = "SAVED";
406 $form->save_history($form->dbconnect(\%myconfig));
408 # /saving the history
410 $lxdebug->leave_sub();
414 $lxdebug->enter_sub();
416 $auth->assert('customer_vendor_edit & sales_order_edit');
418 $form->{script} = "oe.pl";
420 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
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');
437 # $locale->text('Customer saved!')
438 # $locale->text('Vendor saved!')
440 $msg = ucfirst $form->{db};
443 $form->isblank("name", $locale->text("Name missing!"));
444 if ($form->{"db"} eq "customer") {
445 $rc = CT->save_customer(\%myconfig, \%$form);
447 $rc = CT->save_vendor(\%myconfig, \%$form);
450 $form->error($locale->text('customernumber not unique!'));
453 if(!exists $form->{addition}) {
454 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
455 $form->{addition} = "SAVED";
456 $form->save_history($form->dbconnect(\%myconfig));
458 # /saving the history
459 $form->redirect($locale->text($msg));
461 $lxdebug->leave_sub();
465 $lxdebug->enter_sub();
467 $auth->assert('customer_vendor_edit');
469 # $locale->text('Customer saved!')
470 # $locale->text('Vendor saved!')
472 $msg = ucfirst $form->{db};
475 $form->isblank("name", $locale->text("Name missing!"));
478 if ($form->{"db"} eq "customer") {
479 $res = CT->save_customer(\%myconfig, \%$form);
481 $res = CT->save_vendor(\%myconfig, \%$form);
485 if ($form->{"db"} eq "customer") {
486 $form->error($locale->text('This customer number is already in use.'));
488 $form->error($locale->text('This vendor number is already in use.'));
492 if(!exists $form->{addition}) {
493 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
494 $form->{addition} = "SAVED";
495 $form->save_history($form->dbconnect(\%myconfig));
497 # /saving the history
500 $lxdebug->leave_sub();
504 $lxdebug->enter_sub();
506 $auth->assert('customer_vendor_edit');
508 # $locale->text('Customer deleted!')
509 # $locale->text('Cannot delete customer!')
510 # $locale->text('Vendor deleted!')
511 # $locale->text('Cannot delete vendor!')
513 CT->delete(\%myconfig, \%$form);
515 $msg = ucfirst $form->{db};
518 if(!exists $form->{addition}) {
519 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
520 $form->{addition} = "DELETED";
521 $form->save_history($form->dbconnect(\%myconfig));
523 # /saving the history
524 $form->redirect($locale->text($msg));
526 $msg = "Cannot delete $form->{db}";
527 $form->error($locale->text($msg));
529 $lxdebug->leave_sub();
533 $lxdebug->enter_sub();
535 $auth->assert('customer_vendor_edit');
540 $lxdebug->leave_sub();
544 $lxdebug->enter_sub();
546 $auth->assert('customer_vendor_edit');
549 $lxdebug->leave_sub();
553 $lxdebug->enter_sub();
555 $auth->assert('customer_vendor_edit');
557 CT->get_contact(\%myconfig, \%$form);
560 $result = "$form->{cp_name}";
561 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);
564 $lxdebug->leave_sub();
569 $lxdebug->enter_sub();
571 $auth->assert('customer_vendor_edit');
573 CT->get_shipto(\%myconfig, \%$form);
576 $result = "$form->{shiptoname}";
577 map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
580 $lxdebug->leave_sub();
585 $lxdebug->enter_sub();
587 $auth->assert('customer_vendor_edit');
589 CT->get_delivery(\%myconfig, \%$form );
591 print CGI->new->header();
592 print $form->parse_html_template('ct/get_delivery');
593 $lxdebug->leave_sub();
596 sub continue { call_sub($form->{nextsub}); }