1#=====================================================================
2# LedgerSMB Small Medium Business Accounting
3# http://www.ledgersmb.org/
4#
5
6# Copyright (C) 2006
7# This work contains copyrighted information from a number of sources all used
8# with permission.
9#
10# This file contains source code included with or based on SQL-Ledger which
11# is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
12# under the GNU General Public License version 2 or, at your option, any later
13# version.  For a full list including contact information of contributors,
14# maintainers, and copyright holders, see the CONTRIBUTORS file.
15#
16# Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
17# Copyright (c) 2003
18#
19#  Author: DWS Systems Inc.
20#     Web: http://www.sql-ledger.org
21#
22#
23#
24# This program is free software; you can redistribute it and/or modify
25# it under the terms of the GNU General Public License as published by
26# the Free Software Foundation; either version 2 of the License, or
27# (at your option) any later version.
28#
29# This program is distributed in the hope that it will be useful,
30# but WITHOUT ANY WARRANTY; without even the implied warranty of
31# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32# GNU General Public License for more details.
33# You should have received a copy of the GNU General Public License
34# along with this program; if not, write to the Free Software
35# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36#======================================================================
37#
38# common routines for gl, ar, ap, is, ir, oe
39#
40
41use LedgerSMB::AA;
42
43# any custom scripts for this one
44if ( -f "bin/custom/arap.pl" ) {
45    eval { require "bin/custom/arap.pl"; };
46}
47if ( -f "bin/custom/$form->{login}_arap.pl" ) {
48    eval { require "bin/custom/$form->{login}_arap.pl"; };
49}
50
511;
52
53# end of main
54
55sub check_name {
56    my ($name) = @_;
57
58    my ( $new_name, $new_id ) = split /--/, $form->{$name};
59    my $rv = 0;
60
61    # if we use a selection
62    if ( $form->{"select$name"} ) {
63        if ( $form->{"old$name"} ne $form->{$name} ) {
64
65            # this is needed for is, ir and oe
66            for ( split / /, $form->{taxaccounts} ) {
67                delete $form->{"${_}_rate"};
68            }
69
70            # for credit calculations
71            $form->{oldinvtotal}  = 0;
72            $form->{oldtotalpaid} = 0;
73            $form->{calctax}      = 1;
74
75            $form->{"${name}_id"} = $new_id;
76            AA->get_name( \%myconfig, \%$form );
77
78            $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
79            $form->{currency} =~ s/ //g;
80
81            # put employee together if there is a new employee_id
82            $form->{employee} = "$form->{employee}--$form->{employee_id}"
83              if $form->{employee_id};
84
85            $rv = 1;
86        }
87    }
88    else {
89
90        # check name, combine name and id
91        if ( $form->{"old$name"} ne qq|$form->{$name}--$form->{"${name}_id"}| )
92        {
93
94            # this is needed for is, ir and oe
95            for ( split / /, $form->{taxaccounts} ) {
96                delete $form->{"${_}_rate"};
97            }
98
99            # for credit calculations
100            $form->{oldinvtotal}  = 0;
101            $form->{oldtotalpaid} = 0;
102            $form->{calctax}      = 1;
103
104            # return one name or a list of names in $form->{name_list}
105            if (
106                (
107                    $rv =
108                    $form->get_name( \%myconfig, $name, $form->{transdate} )
109                ) > 1
110              )
111            {
112                &select_name($name);
113                exit;
114            }
115
116            if ( $rv == 1 ) {
117
118                # we got one name
119                $form->{"${name}_id"} = $form->{name_list}[0]->{id};
120                $form->{$name} = $form->{name_list}[0]->{name};
121                $form->{"old$name"} = qq|$form->{$name}--$form->{"${name}_id"}|;
122
123                AA->get_name( \%myconfig, \%$form );
124
125                $form->{currency} =~ s/ //g;
126
127                # put employee together if there is a new employee_id
128                $form->{employee} = "$form->{employee}--$form->{employee_id}"
129                  if $form->{employee_id};
130
131            }
132            else {
133
134                # name is not on file
135                $msg = ucfirst $name . " not on file!";
136                $form->error( $locale->text($msg) );
137            }
138        }
139    }
140
141    $rv;
142
143}
144
145# $locale->text('Customer not on file!')
146# $locale->text('Vendor not on file!')
147
148sub select_name {
149    my ($table) = @_;
150
151    @column_index = qw(ndx name address);
152
153    $label = ucfirst $table;
154    $column_data{ndx} = qq|<th>&nbsp;</th>|;
155    $column_data{name} =
156      qq|<th class="listheading">| . $locale->text($label) . qq|</th>|;
157    $column_data{address} =
158        qq|<th class="listheading" colspan="5">|
159      . $locale->text('Address')
160      . qq|</th>|;
161
162    # list items with radio button on a form
163    $form->header;
164
165    $title = $locale->text('Select from one of the names below');
166
167    print qq|
168<body>
169
170<form method="post" action="$form->{script}">
171
172<table width="100%">
173  <tr>
174    <th class="listtop">$title</th>
175  </tr>
176  <tr space="5"></tr>
177  <tr>
178    <td>
179      <table width="100%">
180	<tr class="listheading">|;
181
182    for (@column_index) { print "\n$column_data{$_}" }
183
184    print qq|
185	</tr>
186|;
187
188    @column_index = qw(ndx name address city state zipcode country);
189
190    my $i = 0;
191    foreach $ref ( @{ $form->{name_list} } ) {
192        $checked = ( $i++ ) ? "" : "checked";
193
194        $ref->{name} = $form->quote( $ref->{name} );
195
196        $column_data{ndx} =
197qq|<td><input name="ndx" class="radio" type="radio" value="$i" $checked></td>|;
198        $column_data{name} =
199qq|<td><input name="new_name_$i" type="hidden" value="$ref->{name}">$ref->{name}</td>|;
200        $column_data{address} = qq|<td>$ref->{address1} $ref->{address2}</td>|;
201        for (qw(city state zipcode country)) {
202            $column_data{$_} = qq|<td>$ref->{$_}&nbsp;</td>|;
203        }
204
205        $j++;
206        $j %= 2;
207        print qq|
208	<tr class="listrow$j">|;
209
210        for (@column_index) { print "\n$column_data{$_}" }
211
212        print qq|
213	</tr>
214
215<input name="new_id_$i" type="hidden" value="$ref->{id}">
216
217|;
218
219    }
220
221    print qq|
222      </table>
223    </td>
224  </tr>
225  <tr>
226    <td><hr size="3" noshade></td>
227  </tr>
228</table>
229
230<input name="lastndx" type="hidden" value="$i">
231
232|;
233
234    # delete variables
235    for (qw(nextsub name_list)) { delete $form->{$_} }
236
237    $form->{action} = "name_selected";
238
239    $form->hide_form;
240
241    print qq|
242<input type="hidden" name="nextsub" value="name_selected">
243<input type="hidden" name="vc" value="$table">
244<br>
245<button class="submit" type="submit" name="action" value="continue">|
246      . $locale->text('Continue')
247      . qq|</button>
248</form>
249
250</body>
251</html>
252|;
253
254}
255
256sub name_selected {
257
258    # replace the variable with the one checked
259
260    # index for new item
261    $i = $form->{ndx};
262
263    $form->{ $form->{vc} } = $form->{"new_name_$i"};
264    $form->{"$form->{vc}_id"} = $form->{"new_id_$i"};
265    $form->{"old$form->{vc}"} =
266      qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
267
268    # delete all the new_ variables
269    for $i ( 1 .. $form->{lastndx} ) {
270        for (qw(id, name)) { delete $form->{"new_${_}_$i"} }
271    }
272
273    for (qw(ndx lastndx nextsub)) { delete $form->{$_} }
274
275    AA->get_name( \%myconfig, \%$form );
276
277    # put employee together if there is a new employee_id
278    $form->{employee} = "$form->{employee}--$form->{employee_id}"
279      if $form->{employee_id};
280
281    &update(1);
282
283}
284
285sub rebuild_vc {
286    my ( $vc, $ARAP, $transdate, $job ) = @_;
287
288    ( $null, $form->{employee_id} ) = split /--/, $form->{employee};
289    $form->all_vc( \%myconfig, $vc, $ARAP, undef, $transdate, $job );
290    $form->{"select$vc"} = "";
291    for ( @{ $form->{"all_$vc"} } ) {
292        $form->{"select$vc"} .=
293          qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
294    }
295
296    $form->{selectprojectnumber} = "";
297    if ( @{ $form->{all_project} } ) {
298        $form->{selectprojectnumber} = "<option>\n";
299        for ( @{ $form->{all_project} } ) {
300            $form->{selectprojectnumber} .=
301qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n|;
302        }
303    }
304
305    1;
306}
307
308sub add_transaction {
309    my ($module) = @_;
310
311    delete $form->{script};
312    $form->{action} = "add";
313    $form->{type} = "invoice" if $module =~ /(is|ir)/;
314
315    $form->{callback} = $form->escape( $form->{callback}, 1 );
316    $argv = "";
317    for ( keys %$form ) { $argv .= "$_=$form->{$_}&" if $_ ne 'dbh' }
318
319    $form->{callback} = "$module.pl?$argv";
320
321    $form->redirect;
322
323}
324
325sub check_project {
326
327    for $i ( 1 .. $form->{rowcount} ) {
328        $form->{"project_id_$i"} = "" unless $form->{"projectnumber_$i"};
329        if ( $form->{"projectnumber_$i"} ne $form->{"oldprojectnumber_$i"} ) {
330            if ( $form->{"projectnumber_$i"} ) {
331
332                # get new project
333                $form->{projectnumber} = $form->{"projectnumber_$i"};
334                if ( ( $rows = PE->projects( \%myconfig, $form ) ) > 1 ) {
335
336                    # check form->{project_list} how many there are
337                    $form->{rownumber} = $i;
338                    &select_project;
339                    exit;
340                }
341
342                if ( $rows == 1 ) {
343                    $form->{"project_id_$i"} = $form->{project_list}->[0]->{id};
344                    $form->{"projectnumber_$i"} =
345                      $form->{project_list}->[0]->{projectnumber};
346                    $form->{"oldprojectnumber_$i"} =
347                      $form->{project_list}->[0]->{projectnumber};
348                }
349                else {
350
351                    # not on file
352                    $form->error( $locale->text('Project not on file!') );
353                }
354            }
355            else {
356                $form->{"oldprojectnumber_$i"} = "";
357            }
358        }
359    }
360
361}
362
363sub select_project {
364
365    @column_index = qw(ndx projectnumber description);
366
367    $column_data{ndx} = qq|<th>&nbsp;</th>|;
368    $column_data{projectnumber} =
369      qq|<th>| . $locale->text('Number') . qq|</th>|;
370    $column_data{description} =
371      qq|<th>| . $locale->text('Description') . qq|</th>|;
372
373    # list items with radio button on a form
374    $form->header;
375
376    $title = $locale->text('Select from one of the projects below');
377
378    print qq|
379<body>
380
381<form method="post" action="$form->{script}">
382
383<input type=hidden name="rownumber" value="$form->{rownumber}">
384
385<table width="100%">
386  <tr>
387    <th class="listtop">$title</th>
388  </tr>
389  <tr space="5"></tr>
390  <tr>
391    <td>
392      <table width="100%">
393	<tr class="listheading">|;
394
395    for (@column_index) { print "\n$column_data{$_}" }
396
397    print qq|
398        </tr>
399|;
400
401    my $i = 0;
402    foreach $ref ( @{ $form->{project_list} } ) {
403        $checked = ( $i++ ) ? "" : "checked";
404
405        $ref->{name} = $form->quote( $ref->{name} );
406
407        $column_data{ndx} =
408qq|<td><input name="ndx" class="radio" type="radio" value="$i" $checked></td>|;
409        $column_data{projectnumber} =
410qq|<td><input name="new_projectnumber_$i" type="hidden" value="$ref->{projectnumber}">$ref->{projectnumber}</td>|;
411        $column_data{description} = qq|<td>$ref->{description}</td>|;
412
413        $j++;
414        $j %= 2;
415        print qq|
416        <tr class="listrow$j">|;
417
418        for (@column_index) { print "\n$column_data{$_}" }
419
420        print qq|
421        </tr>
422
423<input name="new_id_$i" type="hidden" value="$ref->{id}">
424
425|;
426
427    }
428
429    print qq|
430      </table>
431    </td>
432  </tr>
433  <tr>
434    <td><hr size="3" noshade></td>
435  </tr>
436</table>
437
438<input name="lastndx" type="hidden" value="$i">
439
440|;
441
442    # delete list variable
443    for (qw(nextsub project_list)) { delete $form->{$_} }
444
445    $form->{action} = "project_selected";
446
447    $form->hide_form;
448
449    print qq|
450<input type="hidden" name="nextsub" value="project_selected">
451<br>
452<button class="submit" type="submit" name="action" value="continue">|
453      . $locale->text('Continue')
454      . qq|</button>
455</form>
456
457</body>
458</html>
459|;
460
461}
462
463sub project_selected {
464
465    # replace the variable with the one checked
466
467    # index for new item
468    $i = $form->{ndx};
469
470    $form->{"projectnumber_$form->{rownumber}"} =
471      $form->{"new_projectnumber_$i"};
472    $form->{"oldprojectnumber_$form->{rownumber}"} =
473      $form->{"new_projectnumber_$i"};
474    $form->{"project_id_$form->{rownumber}"} = $form->{"new_id_$i"};
475
476    # delete all the new_ variables
477    for $i ( 1 .. $form->{lastndx} ) {
478        for (qw(id projectnumber description)) { delete $form->{"new_${_}_$i"} }
479    }
480
481    for (qw(ndx lastndx nextsub)) { delete $form->{$_} }
482
483    if ( $form->{update} ) {
484        &{ $form->{update} };
485    }
486    else {
487        &update;
488    }
489
490}
491
492sub post_as_new {
493
494    for (qw(id printed emailed queued)) { delete $form->{$_} }
495    &post;
496
497}
498
499sub print_and_post_as_new {
500
501    for (qw(id printed emailed queued)) { delete $form->{$_} }
502    &print_and_post;
503
504}
505
506sub repost {
507
508    if ( $form->{type} =~ /_order/ ) {
509        if ( $form->{print_and_save} ) {
510            $form->{nextsub} = "print_and_save";
511            $msg =
512              $locale->text('You are printing and saving an existing order');
513        }
514        else {
515            $form->{nextsub} = "save";
516            $msg = $locale->text('You are saving an existing order');
517        }
518    }
519    elsif ( $form->{type} =~ /_quotation/ ) {
520        if ( $form->{print_and_save} ) {
521            $form->{nextsub} = "print_and_save";
522            $msg =
523              $locale->text(
524                'You are printing and saving an existing quotation');
525        }
526        else {
527            $form->{nextsub} = "save";
528            $msg = $locale->text('You are saving an existing quotation');
529        }
530    }
531    else {
532        if ( $form->{print_and_post} ) {
533            $form->{nextsub} = "print_and_post";
534            $msg =
535              $locale->text(
536                'You are printing and posting an existing transaction!');
537        }
538        else {
539            $form->{nextsub} = "post";
540            $msg = $locale->text('You are posting an existing transaction!');
541        }
542    }
543
544    delete $form->{action};
545    $form->{repost} = 1;
546
547    $form->header;
548
549    print qq|
550<body>
551
552<form method="post" action="$form->{script}">
553|;
554
555    $form->hide_form;
556
557    print qq|
558<h2 class="confirm">| . $locale->text('Warning!') . qq|</h2>
559
560<h4>$msg</h4>
561
562<button name="action" class="submit" type="submit" value="continue">|
563      . $locale->text('Continue')
564      . qq|</button>
565</form>
566
567</body>
568</html>
569|;
570
571}
572
573sub schedule {
574
575    (
576        $form->{recurringreference}, $form->{recurringstartdate},
577        $form->{recurringrepeat},    $form->{recurringunit},
578        $form->{recurringhowmany},   $form->{recurringpayment},
579        $form->{recurringprint},     $form->{recurringemail},
580        $form->{recurringmessage}
581    ) = split /,/, $form->{recurring};
582
583    $form->{recurringreference} =
584      $form->quote( $form->unescape( $form->{recurringreference} ) );
585    $form->{recurringmessage} =
586      $form->quote( $form->unescape( $form->{recurringmessage} ) );
587
588    $form->{recurringstartdate} ||= $form->{transdate};
589    $recurringpayment = "checked" if $form->{recurringpayment};
590
591    if ( $form->{paidaccounts} ) {
592        $postpayment = qq|
593 	<tr>
594	  <th align="right" nowrap>| . $locale->text('Include Payment') . qq|</th>
595	  <td><input name="recurringpayment" type="checkbox" class="checkbox" value="1" $recurringpayment></td>
596	</tr>
597|;
598    }
599
600    if ( $form->{recurringnextdate} ) {
601        $nextdate = qq|
602	      <tr>
603		<th align="right" nowrap>| . $locale->text('Next Date') . qq|</th>
604		<td><input name="recurringnextdate" size="11" title="($myconfig{'dateformat'})" value="$form->{recurringnextdate}"></td>
605	      </tr>
606|;
607    }
608
609    @a = split /<option/, $form->unescape( $form->{selectformname} );
610    %formname = ();
611
612    for ( $i = 1 ; $i <= $#a ; $i++ ) {
613        $a[$i] =~ /"(.*)"/;
614        $v = $1;
615        $a[$i] =~ />(.*)/;
616        $formname{$v} = $1;
617    }
618    for (qw(check receipt)) { delete $formname{$_} }
619
620    $selectformat = $form->unescape( $form->{selectformat} );
621
622    if ( $form->{type} !~ /transaction/ && %formname ) {
623        $email = qq|
624	<table>
625	  <tr>
626	    <th colspan="2" class="listheading">| . $locale->text('E-mail') . qq|</th>
627	  </tr>
628
629	  <tr>
630	    <td>
631	      <table>
632|;
633
634        # formname:format
635        @p = split /:/, $form->{recurringemail};
636        %p = ();
637        for ( $i = 0 ; $i <= $#p ; $i += 2 ) {
638            $p{ $p[$i] }{format} = $p[ $i + 1 ];
639        }
640
641        foreach $item ( keys %formname ) {
642
643            $checked = ( $p{$item}{format} ) ? "checked" : "";
644            $selectformat =~ s/ selected//;
645            $p{$item}{format} ||= "pdf";
646            $selectformat =~
647              s/(<option value="\Q$p{$item}{format}\E")/$1 selected/;
648
649            $email .= qq|
650		<tr>
651		  <td><input name="email$item" type="checkbox" class="checkbox" value="1" $checked></td>
652		  <th align=left>$formname{$item}</th>
653		  <td><select name="emailformat$item">$selectformat</select></td>
654		</tr>
655|;
656        }
657
658        $email .= qq|
659	      </table>
660	    </td>
661	  </tr>
662	</table>
663|;
664
665        $message = qq|
666	<table>
667	  <tr>
668	    <th class="listheading">| . $locale->text('E-mail message') . qq|</th>
669	  </tr>
670
671	  <tr>
672	    <td><textarea name="recurringmessage" rows="10" cols="60" wrap="soft">$form->{recurringmessage}</textarea></td>
673	  </tr>
674	</table>
675|;
676
677    }
678
679    if (   %{LedgerSMB::Sysconfig::printer}
680        && ${LedgerSMB::Sysconfig::latex}
681        && %formname )
682    {
683        $selectprinter = qq|<option>\n|;
684        for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
685            $selectprinter .= qq|<option value="$_">$_\n|;
686        }
687
688        # formname:format:printer
689        @p = split /:/, $form->{recurringprint};
690
691        %p = ();
692        for ( $i = 0 ; $i <= $#p ; $i += 3 ) {
693            $p{ $p[$i] }{formname} = $p[$i];
694            $p{ $p[$i] }{format}   = $p[ $i + 1 ];
695            $p{ $p[$i] }{printer}  = $p[ $i + 2 ];
696        }
697
698        $print = qq|
699	<table>
700	  <tr>
701	    <th colspan="2" class="listheading">| . $locale->text('Print') . qq|</th>
702	  </tr>
703
704	  <tr>
705	    <td>
706	      <table>
707|;
708
709        $selectformat =~ s/<option.*html//;
710        foreach $item ( keys %formname ) {
711
712            $selectprinter =~ s/ selected//;
713            $selectprinter =~
714              s/(<option value="\Q$p{$item}{printer}\E")/$1 selected/;
715
716            $checked = ( $p{$item}{formname} ) ? "checked" : "";
717
718            $selectformat =~ s/ selected//;
719            $p{$item}{format} ||= "postscript";
720            $selectformat =~
721              s/(<option value="\Q$p{$item}{format}\E")/$1 selected/;
722
723            $print .= qq|
724		<tr>
725		  <td><input name="print$item" type="checkbox" class="checkbox" value=1 $checked></td>
726		  <th align="left">$formname{$item}</th>
727		  <td><select name="printprinter$item">$selectprinter</select></td>
728		  <td><select name="printformat$item">$selectformat</select></td>
729		</tr>
730|;
731        }
732
733        $print .= qq|
734	      </table>
735	    </td>
736	  </tr>
737	</table>
738|;
739
740    }
741
742    $selectrepeat = "";
743    for ( 1 .. 31 ) { $selectrepeat .= qq|<option value="$_">$_\n| }
744    $selectrepeat =~ s/(<option value="$form->{recurringrepeat}")/$1 selected/;
745
746    $selectunit = qq|<option value="days">| . $locale->text('Day(s)') . qq|
747  <option value="weeks">| . $locale->text('Week(s)') . qq|
748  <option value="months">| . $locale->text('Month(s)') . qq|
749  <option value="years">| . $locale->text('Year(s)');
750
751    if ( $form->{recurringunit} ) {
752        $selectunit =~ s/(<option value="$form->{recurringunit}")/$1 selected/;
753    }
754
755    if ( $form->{ $form->{vc} } ) {
756        $description = $form->{ $form->{vc} };
757    }
758    else {
759        $description = $form->{description};
760    }
761
762    $repeat = qq|
763	    <table>
764	      <tr>
765		<th colspan="3" class="listheading">| . $locale->text('Repeat') . qq|</th>
766	      </tr>
767
768	      <tr>
769		<th align="right" nowrap>| . $locale->text('Every') . qq|</th>
770		<td><select name="recurringrepeat">$selectrepeat</td>
771		<td><select name="recurringunit">$selectunit</td>
772	      </tr>
773	      <tr>
774		<th align="right" nowrap>| . $locale->text('For') . qq|</th>
775		<td><input name="recurringhowmany" size="3" value="$form->{recurringhowmany}"></td>
776		<th align="left" nowrap>| . $locale->text('time(s)') . qq|</th>
777	      </tr>
778	    </table>
779|;
780
781    $title = $locale->text( 'Recurring Transaction for [_1]', $description );
782
783    $form->header;
784
785    print qq|
786<body>
787
788<form method="post" action="$form->{script}">
789
790<table width="100%">
791  <tr class="listtop">
792    <th class="listtop">$title</th>
793  </tr>
794  <tr space="5"></tr>
795  <tr>
796    <td>
797      <table>
798        <tr>
799	  <td>
800	    <table>
801	      <tr>
802		<th align="right" nowrap>| . $locale->text('Reference') . qq|</th>
803		<td><input name="recurringreference" size="20" value="$form->{recurringreference}"></td>
804	      </tr>
805	      <tr>
806		<th align="right" nowrap>| . $locale->text('Startdate') . qq|</th>
807		<td><input name="recurringstartdate" size="11" title="($myconfig{'dateformat'})" value="$form->{recurringstartdate}"></td>
808	      </tr>
809	      $nextdate
810	    </table>
811	  </td>
812	</tr>
813      </table>
814    </td>
815  </tr>
816
817  <tr>
818    <td>
819      <table>
820	$postpayment
821      </table>
822    </td>
823  </tr>
824
825  <tr>
826    <td>
827      <table>
828	<tr valign="top">
829	  <td>$repeat</td>
830	  <td>$print</td>
831	</tr>
832	<tr valign="top">
833	  <td>$email</td>
834	  <td>$message</td>
835	</tr>
836      </table>
837    </td>
838  </tr>
839  <tr>
840    <td><hr size="3" noshade></td>
841  </tr>
842</table>
843
844<br>
845|;
846
847    # type=submit $locale->text('Save Schedule')
848    # type=submit $locale->text('Delete Schedule')
849
850    %button = (
851        'save_schedule' =>
852          { ndx => 1, key => 'S', value => $locale->text('Save Schedule') },
853        'delete_schedule' =>
854          { ndx => 16, key => 'D', value => $locale->text('Delete Schedule') },
855    );
856
857    $form->print_button( \%button, 'save_schedule' );
858
859    if ( $form->{recurring} ) {
860        $form->print_button( \%button, 'delete_schedule' );
861    }
862
863    # delete variables
864    for (qw(action recurring)) { delete $form->{$_} }
865    for (
866        qw(reference startdate nextdate enddate repeat unit howmany payment print email message)
867      )
868    {
869        delete $form->{"recurring$_"};
870    }
871
872    $form->hide_form;
873
874    print qq|
875
876</form>
877
878</body>
879</html>
880|;
881
882}
883
884sub save_schedule {
885
886    $form->{recurring} = "";
887
888    $form->{recurringreference} =
889      $form->escape( $form->{recurringreference}, 1 );
890    $form->{recurringmessage} = $form->escape( $form->{recurringmessage}, 1 );
891    if ( $form->{recurringstartdate} ) {
892        for (qw(reference startdate repeat unit howmany payment)) {
893            $form->{recurring} .= qq|$form->{"recurring$_"},|;
894        }
895    }
896
897    @a = split /<option/, $form->unescape( $form->{selectformname} );
898    @p = ();
899
900    for ( $i = 1 ; $i <= $#a ; $i++ ) {
901        $a[$i] =~ /"(.*)"/;
902        push @p, $1;
903    }
904
905    $recurringemail = "";
906    for (@p) {
907        $recurringemail .= qq|$_:$form->{"emailformat$_"}:|
908          if $form->{"email$_"};
909    }
910    chop $recurringemail;
911
912    $recurringprint = "";
913    for (@p) {
914        $recurringprint .=
915          qq|$_:$form->{"printformat$_"}:$form->{"printprinter$_"}:|
916          if $form->{"print$_"};
917    }
918    chop $recurringprint;
919
920    $form->{recurring} .=
921      qq|$recurringprint,$recurringemail,$form->{recurringmessage}|
922      if $recurringemail || $recurringprint;
923
924    $form->save_recurring( undef, \%myconfig ) if $form->{id};
925
926    if ( $form->{recurringid} ) {
927        $form->redirect;
928    }
929    else {
930        &update;
931    }
932
933}
934
935sub delete_schedule {
936
937    $form->{recurring} = "";
938
939    $form->save_recurring( undef, \%myconfig ) if $form->{id};
940
941    if ( $form->{recurringid} ) {
942        $form->redirect;
943    }
944    else {
945        &update;
946    }
947
948}
949
950sub reprint {
951
952    $myconfig{vclimit} = 0;
953    $pf = "print_form";
954
955    for (qw(format formname media message)) { $temp{$_} = $form->{$_} }
956
957    if ( $form->{module} eq 'oe' ) {
958        &order_links;
959        &prepare_order;
960        delete $form->{order_details};
961        for ( keys %$form ) { $form->{$_} = $form->unquote( $form->{$_} ) }
962    }
963    else {
964        if ( $form->{type} eq 'invoice' ) {
965            &invoice_links;
966            &prepare_invoice;
967            for ( keys %$form ) { $form->{$_} = $form->unquote( $form->{$_} ) }
968        }
969        else {
970            &create_links;
971            $form->{rowcount}--;
972            for ( 1 .. $form->{rowcount} ) {
973                $form->{"amount_$_"} =
974                  $form->format_amount( \%myconfig, $form->{"amount_$_"}, 2 );
975            }
976            for ( split / /, $form->{taxaccounts} ) {
977                $form->{"tax_$_"} =
978                  $form->format_amount( \%myconfig, $form->{"tax_$_"}, 2 );
979            }
980            $pf = "print_transaction";
981        }
982        for (qw(acc_trans invoice_details)) { delete $form->{$_} }
983    }
984
985    for (qw(department employee language month partsgroup project years)) {
986        delete $form->{"all_$_"};
987    }
988
989    for ( keys %temp ) { $form->{$_} = $temp{$_} }
990
991    $form->{rowcount}++;
992    $form->{paidaccounts}++;
993
994    delete $form->{paid};
995
996    for ( 1 .. $form->{paidaccounts} ) {
997        $form->{"paid_$_"} =
998          $form->format_amount( \%myconfig, $form->{"paid_$_"}, 2 );
999    }
1000
1001    $form->{copies} = 1;
1002
1003    &$pf;
1004
1005    if ( $form->{media} eq 'email' ) {
1006
1007        # add email message
1008        $now = scalar localtime;
1009        $cc  = $locale->text( 'Cc: [_1]', $form->{cc} ) . qq|\n| if $form->{cc};
1010        $bcc = $locale->text( 'Bcc: [_1]', $form->{bcc} ) . qq|\n|
1011          if $form->{bcc};
1012
1013        $form->{intnotes} .= qq|\n\n| if $form->{intnotes};
1014        $form->{intnotes} .=
1015            qq|[email]\n|
1016          . $locale->text( 'Date: [_1]', $now ) . qq|\n|
1017          . $locale->text( 'To: [_1]',   $form->{email} )
1018          . qq|\n${cc}${bcc}|
1019          . $locale->text( 'Subject: [_1]', $form->{subject} )
1020          . qq|\n\n|
1021          . $locale->text('Message: ');
1022
1023        $form->{intnotes} .=
1024          ( $form->{message} ) ? $form->{message} : $locale->text('sent');
1025
1026        $form->save_intnotes( \%myconfig, $form->{module} );
1027
1028        0; #SC: Reversing the expected value to work with its caller
1029    }
1030
1031}
1032
1033sub continue        { &{ $form->{nextsub} } }
1034sub gl_transaction  { &add }
1035sub ar_transaction  { &add_transaction(ar) }
1036sub ap_transaction  { &add_transaction(ap) }
1037sub sales_invoice_  { &add_transaction(is) }
1038sub vendor_invoice_ { &add_transaction(ir) }
1039
1040