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 $form->{title} = "Add";
60 "$form->{script}?action=add&db=$form->{db}&login=$form->{login}&password=$form->{password}"
61 unless $form->{callback};
63 CT->populate_drop_down_boxes(\%myconfig, \%$form);
68 $lxdebug->leave_sub();
72 $lxdebug->enter_sub();
74 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
76 $form->get_lists("business_types" => "ALL_BUSINESS_TYPES");
77 $form->{SHOW_BUSINESS_TYPES} = scalar @{ $form->{ALL_BUSINESS_TYPES} } > 0;
79 $form->{title} = $form->{IS_CUSTOMER} ? $locale->text('Customers') : $locale->text('Vendors');
80 $form->{fokus} = 'Form.name';
83 print $form->parse_html_template('ct/search');
85 $lxdebug->leave_sub();
89 $lxdebug->enter_sub();
91 $form->{IS_CUSTOMER} = $form->{db} eq 'customer';
93 CT->search(\%myconfig, \%$form);
96 if ($form->{status} eq 'all') {
97 push @options, $locale->text('All');
99 } elsif ($form->{status} eq 'orphaned') {
100 push @options, $locale->text('Orphaned');
104 push @options, $locale->text('Name') . " : $form->{name}";
106 if ($form->{contact}) {
107 push @options, $locale->text('Contact') . " : $form->{contact}";
109 if ($form->{"$form->{db}number"}) {
110 push @options, $locale->text('Number') . qq| : $form->{"$form->{db}number"}|;
112 if ($form->{email}) {
113 push @options, $locale->text('E-mail') . " : $form->{email}";
117 'id', 'name', "$form->{db}number", 'address', 'contact', 'phone',
118 'fax', 'email', 'taxnumber', 'sic_code', 'business', 'invnumber',
119 'ordnumber', 'quonumber'
123 'id' => { 'text' => $locale->text('ID'), },
124 "$form->{db}number" => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Customer Number') : $locale->text('Vendor Number'), },
125 'name' => { 'text' => $locale->text('Name'), },
126 'address' => { 'text' => $locale->text('Address'), },
127 'contact' => { 'text' => $locale->text('Contact'), },
128 'phone' => { 'text' => $locale->text('Phone'), },
129 'fax' => { 'text' => $locale->text('Fax'), },
130 'email' => { 'text' => $locale->text('E-mail'), },
131 'cc' => { 'text' => $locale->text('Cc'), },
132 'taxnumber' => { 'text' => $locale->text('Tax Number'), },
133 'sic_code' => { 'text' => $locale->text('SIC'), },
134 'business' => { 'text' => $locale->text('Type of Business'), },
135 'invnumber' => { 'text' => $locale->text('Invoice'), },
136 'ordnumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Sales Order') : $locale->text('Purchase Order'), },
137 'quonumber' => { 'text' => $form->{IS_CUSTOMER} ? $locale->text('Quotation') : $locale->text('Request for Quotation'), },
140 map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
142 my @hidden_variables = (qw(db status obsolete), map { "l_$_" } @columns);
143 my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
144 my $callback = build_std_url('action=list_names', grep { $form->{$_} } @hidden_variables);
145 $form->{callback} = "$callback&sort=" . E($form->{sort});
147 map { $column_defs{$_}->{link} = "${callback}&sort=${_}" } @columns;
149 my ($ordertype, $quotationtype, $attachment_basename);
150 if ($form->{IS_CUSTOMER}) {
151 $form->{title} = $locale->text('Customers');
152 $ordertype = 'sales_order';
153 $quotationtype = 'sales_quotation';
154 $attachment_basename = $locale->text('customer_list');
157 $form->{title} = $locale->text('Vendors');
158 $ordertype = 'purchase_order';
159 $quotationtype = 'request_quotation';
160 $attachment_basename = $locale->text('vendor_list');
163 my $report = SL::ReportGenerator->new(\%myconfig, $form);
165 $report->set_options('top_info_text' => join("\n", @options),
166 'raw_bottom_info_text' => $form->parse_html_template('ct/list_names_bottom'),
167 'output_format' => 'HTML',
168 'title' => $form->{title},
169 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
171 $report->set_options_from_form();
173 $report->set_columns(%column_defs);
174 $report->set_column_order(@columns);
176 $report->set_export_options('list_names', @hidden_variables);
178 $report->set_sort_indicator($form->{sort}, 1);
182 foreach my $ref (@{ $form->{CT} }) {
183 my $row = { map { $_ => { 'data' => '' } } @columns };
185 if ($ref->{id} ne $previous_id) {
186 $previous_id = $ref->{id};
187 map { $row->{$_}->{data} = $ref->{$_} } @columns;
189 $row->{name}->{link} = build_std_url('action=edit', 'id=' . E($ref->{id}), 'callback', @hidden_nondefault);
190 $row->{email}->{link} = 'mailto:' . E($ref->{email});
193 my $base_url = build_std_url("script=$ref->{module}.pl", 'action=edit', 'id=' . E($ref->{invid}), 'callback', @hidden_nondefault);
194 $row->{invnumber}->{link} = $base_url;
195 $row->{ordnumber}->{link} = $base_url . "&type=${ordertype}";
196 $row->{quonumber}->{link} = $base_url . "&type=${quotationtype}";
197 my $column = $ref->{formtype} eq 'invoice' ? 'invnumber' : $ref->{formtype} eq 'order' ? 'ordnumber' : 'quonumber';
198 $row->{$column}->{data} = $ref->{$column};
200 $report->add_data($row);
203 $report->generate_with_headers();
205 $lxdebug->leave_sub();
209 $lxdebug->enter_sub();
211 # show history button
212 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
213 #/show hhistory button
215 # $locale->text('Edit Customer')
216 # $locale->text('Edit Vendor')
218 CT->get_tuple(\%myconfig, \%$form);
219 CT->populate_drop_down_boxes(\%myconfig, \%$form);
221 # format " into "
222 map { $form->{$_} =~ s/\"/"/g } keys %$form;
224 $form->{title} = "Edit";
227 $form->{discount} *= 100;
232 $lxdebug->leave_sub();
236 $lxdebug->enter_sub();
238 $form->get_lists("employees" => "ALL_SALESMEN",
239 "taxzones" => "ALL_TAXZONES");
240 $form->get_pricegroup(\%myconfig, { all => 1 });
242 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
243 $form->{is_admin} = $myconfig{role} eq 'admin';
244 $form->{is_customer} = $form->{db} eq 'customer';
245 $form->{salesman_label} = sub { $_[0]->{name} ne "" ? $_[0]->{name} : $_[0]->{login} };
246 $form->{shipto_label} = sub { "$_[0]->{shiptoname} $_[0]->{shiptodepartment_1}" };
247 $form->{taxzone_id} = 0 if !$form->{id};
248 $form->{jsscript} = 1;
249 $form->{fokus} = "ct.greeting";
251 unshift @{ $form->{SHIPTO} }, +{ shipto_id => '0', shiptoname => '' }, +{ shipto_id => '0', shiptoname => 'Alle' };
252 unshift @{ $form->{CONTACTS} }, +{ cp_id => '0', cp_name => $locale->text('New contact') };
254 push @{ $form->{AJAX} }, map {
255 new CGI::Ajax( "get_$_" => "$form->{script}?login=$form->{login}&password=$form->{password}&action=get_$_" )
256 } qw(shipto contact delivery);
258 $form->{title} = $form->{title_save}
259 || $locale->text("$form->{title} " . ucfirst $form->{db}) . ($form->{title} eq "Edit" ? " $form->{name}" : '');
261 ## LINET: Create a drop-down box with all prior titles and greetings.
262 CT->query_titles_and_greetings(\%myconfig, \%$form);
263 map { $form->{"MB_$_"} = [ map +{ id => $_, description => $_ }, @{ $form->{$_} } ] } qw(TITLES GREETINGS COMPANY_GREETINGS DEPARTMENT);
267 print $form->parse_html_template('ct/form_header');
269 $lxdebug->leave_sub();
273 $lxdebug->enter_sub();
275 print $form->parse_html_template('ct/form_footer', { is_orphaned => $form->{status} eq 'orphaned',
276 is_customer => $form->{db} eq 'customer' });
277 $lxdebug->leave_sub();
280 sub add_transaction {
281 $lxdebug->enter_sub();
283 # # saving the history
284 # if(!exists $form->{addition}) {
285 # $form->{addition} = "ADD TRANSACTION";
286 # $form->save_history($form->dbconnect(\%myconfig));
288 # # /saving the history
290 $form->isblank("name", $locale->text("Name missing!"));
291 if ($form->{"db"} eq "customer") {
292 CT->save_customer(\%myconfig, \%$form);
294 CT->save_vendor(\%myconfig, \%$form);
297 $form->{callback} = $form->escape($form->{callback}, 1);
298 $name = $form->escape("$form->{name}", 1);
301 "$form->{script}?login=$form->{login}&password=$form->{password}&action=add&vc=$form->{db}&$form->{db}_id=$form->{id}&$form->{db}=$name&type=$form->{type}&callback=$form->{callback}";
304 $lxdebug->leave_sub();
307 sub save_and_ap_transaction {
308 $lxdebug->enter_sub();
310 $form->{script} = "ap.pl";
312 if(!exists $form->{addition}) {
313 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
314 $form->{addition} = "SAVED";
315 $form->save_history($form->dbconnect(\%myconfig));
317 # /saving the history
319 $lxdebug->leave_sub();
322 sub save_and_ar_transaction {
323 $lxdebug->enter_sub();
325 $form->{script} = "ar.pl";
327 if(!exists $form->{addition}) {
328 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
329 $form->{addition} = "SAVED";
330 $form->save_history($form->dbconnect(\%myconfig));
332 # /saving the history
334 $lxdebug->leave_sub();
337 sub save_and_invoice {
338 $lxdebug->enter_sub();
340 $form->{script} = ($form->{db} eq 'customer') ? "is.pl" : "ir.pl";
341 $form->{type} = "invoice";
343 if(!exists $form->{addition}) {
344 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
345 $form->{addition} = "SAVED";
346 $form->save_history($form->dbconnect(\%myconfig));
348 # /saving the history
350 $lxdebug->leave_sub();
354 $lxdebug->enter_sub();
356 $form->{script} = "oe.pl";
357 $form->{type} = "request_quotation";
359 if(!exists $form->{addition}) {
360 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
361 $form->{addition} = "SAVED";
362 $form->save_history($form->dbconnect(\%myconfig));
364 # /saving the history
366 $lxdebug->leave_sub();
369 sub save_and_quotation {
370 $lxdebug->enter_sub();
372 $form->{script} = "oe.pl";
373 $form->{type} = "sales_quotation";
375 if(!exists $form->{addition}) {
376 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
377 $form->{addition} = "SAVED";
378 $form->save_history($form->dbconnect(\%myconfig));
380 # /saving the history
382 $lxdebug->leave_sub();
386 $lxdebug->enter_sub();
388 $form->{script} = "oe.pl";
390 ($form->{db} eq 'customer') ? "sales_order" : "purchase_order";
392 if(!exists $form->{addition}) {
393 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
394 $form->{addition} = "SAVED";
395 $form->save_history($form->dbconnect(\%myconfig));
397 # /saving the history
399 $lxdebug->leave_sub();
403 $lxdebug->enter_sub();
405 # $locale->text('Customer saved!')
406 # $locale->text('Vendor saved!')
408 $msg = ucfirst $form->{db};
411 $form->isblank("name", $locale->text("Name missing!"));
412 if ($form->{"db"} eq "customer") {
413 $rc = CT->save_customer(\%myconfig, \%$form);
415 $rc = CT->save_vendor(\%myconfig, \%$form);
418 $form->error($locale->text('customernumber not unique!'));
421 if(!exists $form->{addition}) {
422 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
423 $form->{addition} = "SAVED";
424 $form->save_history($form->dbconnect(\%myconfig));
426 # /saving the history
427 $form->redirect($locale->text($msg));
429 $lxdebug->leave_sub();
433 $lxdebug->enter_sub();
435 # $locale->text('Customer saved!')
436 # $locale->text('Vendor saved!')
438 $msg = ucfirst $form->{db};
441 $form->isblank("name", $locale->text("Name missing!"));
444 if ($form->{"db"} eq "customer") {
445 $res = CT->save_customer(\%myconfig, \%$form);
447 $res = CT->save_vendor(\%myconfig, \%$form);
451 if ($form->{"db"} eq "customer") {
452 $form->error($locale->text('This customer number is already in use.'));
454 $form->error($locale->text('This vendor number is already in use.'));
458 if(!exists $form->{addition}) {
459 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
460 $form->{addition} = "SAVED";
461 $form->save_history($form->dbconnect(\%myconfig));
463 # /saving the history
466 $lxdebug->leave_sub();
470 $lxdebug->enter_sub();
472 # $locale->text('Customer deleted!')
473 # $locale->text('Cannot delete customer!')
474 # $locale->text('Vendor deleted!')
475 # $locale->text('Cannot delete vendor!')
477 CT->delete(\%myconfig, \%$form);
479 $msg = ucfirst $form->{db};
482 if(!exists $form->{addition}) {
483 $form->{snumbers} = ($form->{"db"} eq "customer" ? qq|customernumber_| . $form->{customernumber} : qq|vendornumber_| . $form->{vendornumber});
484 $form->{addition} = "DELETED";
485 $form->save_history($form->dbconnect(\%myconfig));
487 # /saving the history
488 $form->redirect($locale->text($msg));
490 $msg = "Cannot delete $form->{db}";
491 $form->error($locale->text($msg));
493 $lxdebug->leave_sub();
497 $lxdebug->enter_sub();
502 $lxdebug->leave_sub();
506 $lxdebug->enter_sub();
509 $lxdebug->leave_sub();
513 $lxdebug->enter_sub();
515 CT->get_contact(\%myconfig, \%$form);
518 $result = "$form->{cp_name}";
519 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);
522 $lxdebug->leave_sub();
527 $lxdebug->enter_sub();
529 CT->get_shipto(\%myconfig, \%$form);
532 $result = "$form->{shiptoname}";
533 map { $result .= "__pjx__" . $form->{$_} } qw(shiptodepartment_1 shiptodepartment_2 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax shiptoemail);
536 $lxdebug->leave_sub();
541 $lxdebug->enter_sub();
543 CT->get_delivery(\%myconfig, \%$form );
546 $form->sort_columns(shiptoname,
557 $column_header{shiptoname} =
558 qq|<th class=listheading>| . $locale->text('Shipping Address') . qq|</th>|;
559 $column_header{invnumber} =
560 qq|<th class=listheading>|. $locale->text('Invoice'). qq|</th>|;
561 $column_header{ordnumber} =
562 qq|<th class=listheading>|. $locale->text('Order'). qq|</th>|;
563 $column_header{transdate} =
564 qq|<th class=listheading>| . $locale->text('Invdate') . qq|</th>|;
565 $column_header{description} =
566 qq|<th class=listheading>| . $locale->text('Description') . qq|</th>|;
567 $column_header{qty} =
568 qq|<th class=listheading>| . $locale->text('Qty') . qq|</th>|;
569 $column_header{unit} =
570 qq|<th class=listheading>| . $locale->text('Unit') . qq|</th>|;
571 $column_header{sellprice} =
572 qq|<th class=listheading>| . $locale->text('Sell Price') . qq|</th>|;
579 <tr class=listheading>
582 map { $result .= "$column_header{$_}\n" } @column_index;
589 foreach $ref (@{ $form->{DELIVERY} }) {
591 if ($ref->{shiptoname} eq $sameshiptoname) {
592 map { $column_data{$_} = "<td>$ref->{$_} </td>" } @column_index;
593 $column_data{shiptoname} = "<td> </td>";
595 map { $column_data{$_} = "<td>$ref->{$_} </td>" } @column_index;
597 $column_data{sellprice} = "<td>". $form->format_amount(\%myconfig,$ref->{sellprice},2)." </td>";
604 map { $result .= "$column_data{$_}\n" } @column_index;
610 $sameshiptoname = $ref->{shiptoname};
622 $lxdebug->leave_sub();
626 sub continue { call_sub($form->{nextsub}); }