summaryrefslogtreecommitdiff
path: root/bin/cp.pl
blob: c643b7d8e2aa5f906ba75587aae852aab258530a (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # http://www.ledgersmb.org/
  4. #
  5. # Copyright (C) 2006
  6. # This work contains copyrighted information from a number of sources all used
  7. # with permission.
  8. #
  9. # This file contains source code included with or based on SQL-Ledger which
  10. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  11. # under the GNU General Public License version 2 or, at your option, any later
  12. # version. For a full list including contact information of contributors,
  13. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  14. #
  15. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  16. # Copyright (c) 2002
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. # Contributors:
  22. #
  23. #
  24. # Author: DWS Systems Inc.
  25. # Web: http://www.ledgersmb.org/
  26. #
  27. # Contributors:
  28. #
  29. # This program is free software; you can redistribute it and/or modify
  30. # it under the terms of the GNU General Public License as published by
  31. # the Free Software Foundation; either version 2 of the License, or
  32. # (at your option) any later version.
  33. #
  34. # This program is distributed in the hope that it will be useful,
  35. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. # GNU General Public License for more details.
  38. # You should have received a copy of the GNU General Public License
  39. # along with this program; if not, write to the Free Software
  40. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  41. #======================================================================
  42. #
  43. # Payment module
  44. #
  45. #======================================================================
  46. use Error qw(:try);
  47. use LedgerSMB::Template;
  48. use LedgerSMB::CP;
  49. use LedgerSMB::OP;
  50. use LedgerSMB::IS;
  51. use LedgerSMB::IR;
  52. require "bin/arap.pl";
  53. 1;
  54. # end of main
  55. sub payment {
  56. if ( $form->{type} eq 'receipt' ) {
  57. $form->{ARAP} = "AR";
  58. $form->{arap} = "ar";
  59. $form->{vc} = "customer";
  60. $form->{formname} = "receipt";
  61. }
  62. if ( $form->{type} eq 'check' ) {
  63. $form->{ARAP} = "AP";
  64. $form->{arap} = "ap";
  65. $form->{vc} = "vendor";
  66. $form->{formname} = "check";
  67. }
  68. $form->{payment} = "payment";
  69. $form->{callback} =
  70. "$form->{script}?action=payment&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&all_vc=$form->{all_vc}&type=$form->{type}";
  71. # setup customer/vendor selection for open invoices
  72. if ( $form->{all_vc} ) {
  73. $form->all_vc( \%myconfig, $form->{vc}, $form->{ARAP}, undef,
  74. $form->{datepaid} );
  75. }
  76. else {
  77. CP->get_openvc( \%myconfig, \%$form );
  78. if ( $myconfig{vclimit} > 0 ) {
  79. $form->{"all_$form->{vc}"} = $form->{name_list};
  80. }
  81. }
  82. $form->{"select$form->{vc}"} = "";
  83. if ( @{ $form->{"all_$form->{vc}"} } ) {
  84. $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
  85. for ( @{ $form->{"all_$form->{vc}"} } ) {
  86. $form->{"select$form->{vc}"} .=
  87. qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
  88. }
  89. }
  90. # departments
  91. if ( @{ $form->{all_department} } ) {
  92. $form->{selectdepartment} = "<option>\n";
  93. $form->{department} = "$form->{department}--$form->{department_id}"
  94. if $form->{department};
  95. for ( @{ $form->{all_department} } ) {
  96. $form->{selectdepartment} .=
  97. qq|<option value="$_->{description}--$_->{id}">$_->{description}\n|;
  98. }
  99. }
  100. if ( @{ $form->{all_language} } ) {
  101. $form->{selectlanguage} = "<option>\n";
  102. for ( @{ $form->{all_language} } ) {
  103. $form->{selectlanguage} .=
  104. qq|<option value="$_->{code}">$_->{description}\n|;
  105. }
  106. }
  107. CP->paymentaccounts( \%myconfig, \%$form );
  108. $form->{selectaccount} = "";
  109. $form->{"select$form->{ARAP}"} = "";
  110. for ( @{ $form->{PR}{"$form->{ARAP}_paid"} } ) {
  111. $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n";
  112. }
  113. for ( @{ $form->{PR}{ $form->{ARAP} } } ) {
  114. $form->{"select$form->{ARAP}"} .=
  115. "<option>$_->{accno}--$_->{description}\n";
  116. }
  117. # currencies
  118. @curr = split /:/, $form->{currencies};
  119. $form->{defaultcurrency} = $curr[0];
  120. chomp $form->{defaultcurrency};
  121. $form->{selectcurrency} = "";
  122. for (@curr) { $form->{selectcurrency} .= "<option>$_\n" }
  123. $form->{currency} = $form->{defaultcurrency};
  124. $form->{oldcurrency} = $form->{currency};
  125. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  126. $form->{forex} = $form->{exchangerate} =
  127. $form->check_exchangerate( \%myconfig, $form->{currency},
  128. $form->{datepaid}, ( $form->{vc} eq 'customer' ) ? "buy" : "sell" );
  129. }
  130. $form->{olddatepaid} = $form->{datepaid};
  131. $form->{media} = $myconfig{printer};
  132. $form->{format} = "pdf" unless $myconfig{printer};
  133. &payment_header;
  134. &payment_footer;
  135. }
  136. sub payments {
  137. if ( $form->{type} eq 'receipt' ) {
  138. $form->{ARAP} = "AR";
  139. $form->{arap} = "ar";
  140. $form->{vc} = "customer";
  141. $form->{formname} = "receipt";
  142. }
  143. if ( $form->{type} eq 'check' ) {
  144. $form->{ARAP} = "AP";
  145. $form->{arap} = "ap";
  146. $form->{vc} = "vendor";
  147. $form->{formname} = "check";
  148. }
  149. $form->{payment} = "payments";
  150. $form->{callback} = "$form->{script}?action=$form->{action}";
  151. for (qw(path login sessionid type)) {
  152. $form->{callback} .= "&$_=$form->{$_}";
  153. }
  154. CP->paymentaccounts( \%myconfig, \%$form );
  155. if ( @{ $form->{all_language} } ) {
  156. $form->{selectlanguage} = "<option>\n";
  157. for ( @{ $form->{all_language} } ) {
  158. $form->{selectlanguage} .=
  159. qq|<option value="$_->{code}">$_->{description}\n|;
  160. }
  161. }
  162. # departments
  163. if ( @{ $form->{all_department} } ) {
  164. $form->{selectdepartment} = "<option>\n";
  165. $form->{department} = "$form->{department}--$form->{department_id}"
  166. if $form->{department};
  167. for ( @{ $form->{all_department} } ) {
  168. $form->{selectdepartment} .=
  169. qq|<option value="$_->{description}--$_->{id}">$_->{description}\n|;
  170. }
  171. }
  172. $form->{selectaccount} = "";
  173. $form->{"select$form->{ARAP}"} = "";
  174. for ( @{ $form->{PR}{"$form->{ARAP}_paid"} } ) {
  175. $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n";
  176. }
  177. for ( @{ $form->{PR}{ $form->{ARAP} } } ) {
  178. $form->{"select$form->{ARAP}"} .=
  179. "<option>$_->{accno}--$_->{description}\n";
  180. }
  181. # currencies
  182. @curr = split /:/, $form->{currencies};
  183. $form->{defaultcurrency} = $curr[0];
  184. chomp $form->{defaultcurrency};
  185. $form->{selectcurrency} = "";
  186. for (@curr) { $form->{selectcurrency} .= "<option>$_\n" }
  187. $form->{oldcurrency} = $form->{currency} = $form->{defaultcurrency};
  188. $form->{oldduedateto} = $form->{datepaid};
  189. $form->{media} = $myconfig{printer};
  190. $form->{format} = "pdf" unless $myconfig{printer};
  191. &payments_header;
  192. &invoices_due;
  193. &payments_footer;
  194. }
  195. sub payments_header {
  196. if ( $form->{type} eq 'receipt' ) {
  197. $form->{title} = $locale->text('Receipts');
  198. }
  199. if ( $form->{type} eq 'check' ) {
  200. $form->{title} = $locale->text('Payments');
  201. }
  202. for ("department") {
  203. $form->{"select$_"} = $form->unescape( $form->{"select$_"} );
  204. $form->{"select$_"} =~ s/ selected//;
  205. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  206. }
  207. for ( "account", "currency", "$form->{ARAP}" ) {
  208. $form->{"select$_"} =~ s/ selected//;
  209. $form->{"select$_"} =~
  210. s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  211. }
  212. if ( $form->{defaultcurrency} ) {
  213. $exchangerate = qq|
  214. <tr>
  215. <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
  216. <td><select name=currency>$form->{selectcurrency}</select></td>
  217. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  218. <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
  219. </tr>
  220. |;
  221. }
  222. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  223. $form->{exchangerate} =
  224. $form->format_amount( \%myconfig, $form->{exchangerate} );
  225. if ( $form->{forex} ) {
  226. $exchangerate .= qq|
  227. <tr>
  228. <th align=right nowrap>| . $locale->text('Exchange Rate') . qq|</th>
  229. <td colspan=3><input type=hidden name=exchangerate size=10 value=$form->{exchangerate}>$form->{exchangerate}</td>
  230. </tr>
  231. |;
  232. }
  233. else {
  234. $exchangerate .= qq|
  235. <tr>
  236. <th align=right nowrap>| . $locale->text('Exchange Rate') . qq|</th>
  237. <td colspan=3><input name=exchangerate size=10 value=$form->{exchangerate}></td>
  238. </tr>
  239. |;
  240. }
  241. }
  242. $department = qq|
  243. <tr>
  244. <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
  245. <td><select name=department>$form->{selectdepartment}</select>
  246. <input type=hidden name=selectdepartment value="|
  247. . $form->escape( $form->{selectdepartment}, 1 ) . qq|">
  248. </td>
  249. </tr>
  250. | if $form->{selectdepartment};
  251. $form->header;
  252. print qq|
  253. <body>
  254. <form method=post action=$form->{script}>
  255. |;
  256. $form->hide_form(
  257. qw(defaultcurrency closedto vc type formname arap ARAP title oldduedatefrom oldduedateto payment olddepartment)
  258. );
  259. print qq|
  260. <table width=100%>
  261. <tr>
  262. <th class=listtop>$form->{title}</th>
  263. </tr>
  264. <tr height="5"></tr>
  265. <tr>
  266. <td>
  267. <table width=100%>
  268. <tr valign=top>
  269. <td>
  270. <table>
  271. <tr>
  272. <th align=right>|
  273. . $locale->text('Due Date')
  274. . qq|&nbsp;|
  275. . $locale->text('From')
  276. . qq|</th>
  277. <td><input name=duedatefrom value="$form->{duedatefrom}" title="$myconfig{dateformat}" size=11></td>
  278. <th align=right>| . $locale->text('To') . qq|</th>
  279. <td><input name=duedateto value="$form->{duedateto}" title="$myconfig{dateformat}" size=11></td>
  280. </tr>
  281. </table>
  282. </td>
  283. </td>
  284. <td align=right>
  285. <table>
  286. $department
  287. <tr>
  288. <th align=right nowrap>| . $locale->text( $form->{ARAP} ) . qq|</th>
  289. <td colspan=3><select name=$form->{ARAP}>$form->{"select$form->{ARAP}"}</select>
  290. </td>
  291. <input type=hidden name="select$form->{ARAP}" value="$form->{"select$form->{ARAP}"}">
  292. </tr>
  293. <tr>
  294. <th align=right nowrap>| . $locale->text('Account') . qq|</th>
  295. <td colspan=3><select name=account>$form->{selectaccount}</select>
  296. <input type=hidden name=selectaccount value="$form->{selectaccount}">
  297. </td>
  298. </tr>
  299. <tr>
  300. <th align=right nowrap>| . $locale->text('Date') . qq|</th>
  301. <td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11></td>
  302. </tr>
  303. $exchangerate
  304. </table>
  305. </td>
  306. </tr>
  307. </table>
  308. </td>
  309. </tr>
  310. |;
  311. }
  312. sub invoices_due {
  313. @column_index =
  314. qw(name invnumber transdate amount due checked paid memo source);
  315. push @column_index, "language" if $form->{selectlanguage};
  316. $colspan = $#column_index + 1;
  317. $invoice = $locale->text('Invoices');
  318. $vclabel = ucfirst $form->{vc};
  319. $vclabel = $locale->text($vclabel);
  320. print qq|
  321. <input type=hidden name=column_index value="id @column_index">
  322. <tr>
  323. <td>
  324. <table width=100%>
  325. <tr>
  326. <th class=listheading colspan=$colspan>$invoice</th>
  327. </tr>
  328. |;
  329. $column_data{name} = qq|<th nowrap>$vclabel</th>|;
  330. $column_data{invnumber} =
  331. qq|<th nowrap>| . $locale->text('Invoice') . "</th>";
  332. $column_data{transdate} = qq|<th nowrap>| . $locale->text('Date') . "</th>";
  333. $column_data{amount} = qq|<th nowrap>| . $locale->text('Amount') . "</th>";
  334. $column_data{due} = qq|<th nowrap>| . $locale->text('Amount Due') . "</th>";
  335. $column_data{paid} = qq|<th nowrap>| . $locale->text('Amount') . "</th>";
  336. $column_data{checked} = qq|<th nowrap>| . $locale->text('Select') . "</th>";
  337. $column_data{memo} = qq|<th nowrap>| . $locale->text('Memo') . "</th>";
  338. $column_data{source} = qq|<th nowrap>| . $locale->text('Source') . "</th>";
  339. $column_data{language} =
  340. qq|<th nowrap>| . $locale->text('Language') . "</th>";
  341. print qq|
  342. <tr>
  343. |;
  344. for (@column_index) { print "$column_data{$_}\n" }
  345. print qq|
  346. </tr>
  347. |;
  348. $form->{selectlanguage} = $form->unescape( $form->{selectlanguage} );
  349. for $i ( 1 .. $form->{rowcount} ) {
  350. for (qw(amount due paid)) {
  351. $form->{"${_}_$i"} =
  352. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  353. }
  354. $totalamount += $form->{"amount_$i"};
  355. $totaldue += $form->{"due_$i"};
  356. if ( $form->{"paid_$i"} !~ /NaN/ ) {
  357. $totalpaid += $form->{"paid_$i"};
  358. }
  359. else {
  360. $form->{"paid_$i"} = '';
  361. }
  362. for (qw(amount due paid)) {
  363. $form->{"${_}_$i"} =
  364. $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 );
  365. }
  366. $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
  367. <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
  368. <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
  369. $column_data{transdate} = qq|<td>$form->{"transdate_$i"}</td>
  370. <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
  371. $column_data{amount} = qq|<td align=right>$form->{"amount_$i"}</td>
  372. <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
  373. $column_data{due} = qq|<td align=right>$form->{"due_$i"}</td>
  374. <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
  375. $column_data{paid} =
  376. qq|<td align=right><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
  377. if ( $same_id eq $form->{"$form->{vc}_id_$i"} ) {
  378. for (qw(name memo source language)) {
  379. $column_data{$_} = qq|<td>&nbsp;</td>|;
  380. }
  381. }
  382. else {
  383. $column_data{name} = qq|<td>$form->{"name_$i"}</td>|;
  384. $column_data{memo} =
  385. qq|<td align=right><input name="memo_$i" size=10 value="$form->{"memo_$i"}"></td>|;
  386. $column_data{source} =
  387. qq|<td align=right><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
  388. if ( $form->{selectlanguage} ) {
  389. $selectlanguage = $form->{selectlanguage};
  390. $selectlanguage =~
  391. s/(<option value="\Q$form->{"language_code_$i"}\E")/$1 selected/;
  392. $column_data{language} =
  393. qq|<td><select name="language_code_$i">$selectlanguage</select></td>|;
  394. }
  395. }
  396. $column_data{name} .= qq|
  397. <input type=hidden name="name_$i" value="$form->{"name_$i"}">
  398. <input type=hidden name="$form->{vc}_id_$i" value="$form->{"$form->{vc}_id_$i"}">|;
  399. $form->{"checked_$i"} = ( $form->{"checked_$i"} ) ? "checked" : "";
  400. $column_data{checked} =
  401. qq|<td align=center><input name="checked_$i" type=checkbox class=checkbox $form->{"checked_$i"}></td>|;
  402. $j++;
  403. $j %= 2;
  404. print qq|
  405. <tr class=listrow$j>
  406. |;
  407. for (@column_index) { print "$column_data{$_}\n" }
  408. print qq|
  409. </tr>
  410. |;
  411. $same_id = $form->{"$form->{vc}_id_$i"};
  412. }
  413. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  414. $column_data{amount} =
  415. qq|<th class=listtotal align=right>|
  416. . $form->format_amount( \%myconfig, $totalamount, 2, "&nbsp;" )
  417. . qq|</th>|;
  418. $column_data{due} =
  419. qq|<th class=listtotal align=right>|
  420. . $form->format_amount( \%myconfig, $totaldue, 2, "&nbsp;" )
  421. . qq|</th>|;
  422. $column_data{paid} =
  423. qq|<th class=listtotal align=right>|
  424. . $form->format_amount( \%myconfig, $totalpaid, 2, "&nbsp;" )
  425. . qq|</th>|;
  426. print qq|
  427. <tr class=listtotal>
  428. |;
  429. for (@column_index) { print "$column_data{$_}\n" }
  430. print qq|
  431. </tr>
  432. </table>
  433. </td>
  434. </tr>
  435. <input type=hidden name=selectlanguage value="|
  436. . $form->escape( $form->{selectlanguage}, 1 ) . qq|">
  437. |;
  438. }
  439. sub payments_footer {
  440. $form->{DF}{ $form->{format} } = "selected";
  441. $transdate = $form->datetonum( \%myconfig, $form->{datepaid} );
  442. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  443. if ( ${LedgerSMB::Sysconfig::latex} ) {
  444. $media = qq|<select name=media>
  445. <option value=screen>| . $locale->text('Screen');
  446. if ( %{LedgerSMB::Sysconfig::printer} ) {
  447. for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  448. $media .= qq|
  449. <option value="$_">$_|;
  450. }
  451. }
  452. $media .= qq|</select>|;
  453. $format = qq|<select name=format>
  454. <option value=postscript $form->{DF}{postscript}>|
  455. . $locale->text('Postscript') . qq|
  456. <option value=pdf $form->{DF}{pdf}>|
  457. . $locale->text('PDF')
  458. . qq|</select>|;
  459. }
  460. print qq|
  461. <tr>
  462. <td><hr size=3 noshade></td>
  463. </tr>
  464. </table>
  465. |;
  466. # type=submit $locale->text('Update')
  467. # type=submit $locale->text('Post')
  468. # type=submit $locale->text('Print')
  469. # type=submit $locale->text('Select all')
  470. %button = (
  471. 'update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  472. 'select_all' =>
  473. { ndx => 2, key => 'A', value => $locale->text('Select all') },
  474. 'print' => { ndx => 3, key => 'P', value => $locale->text('Print') },
  475. 'post' => { ndx => 4, key => 'O', value => $locale->text('Post') },
  476. );
  477. if ( !${LedgerSMB::Sysconfig::latex} ) {
  478. delete $button{'print'};
  479. }
  480. if ( $transdate <= $closedto ) {
  481. for ( 'post', 'print' ) { delete $button{$_} }
  482. $media = $format = "";
  483. }
  484. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button ) {
  485. $form->print_button( \%button, $_ );
  486. }
  487. $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
  488. print qq|
  489. $format
  490. $media
  491. |;
  492. $form->hide_form(qw(callback rowcount path login sessionid));
  493. if ( $form->{lynx} ) {
  494. require "bin/menu.pl";
  495. &menubar;
  496. }
  497. print qq|
  498. </form>
  499. </body>
  500. </html>
  501. |;
  502. }
  503. sub select_all {
  504. for ( 1 .. $form->{rowcount} ) { $form->{"checked_$_"} = 1 }
  505. &{"update_$form->{payment}"}
  506. }
  507. sub update {
  508. my ($new_name_selected) = @_;
  509. &{"update_$form->{payment}"};
  510. }
  511. sub update_payments {
  512. if ( $form->{ARAP} eq 'AR' ) {
  513. $buysell = "buy";
  514. }
  515. else {
  516. $buysell = "sell";
  517. }
  518. if ( ( $form->{oldduedatefrom} ne $form->{duedatefrom} )
  519. || ( $form->{oldduedateto} ne $form->{duedateto} )
  520. || ( $form->{department} ne $form->{olddepartment} ) )
  521. {
  522. CP->get_openinvoices( \%myconfig, \%$form );
  523. $form->{redo} = 1;
  524. }
  525. if ( $form->{currency} ne $form->{oldcurrency} ) {
  526. $form->{oldcurrency} = $form->{currency};
  527. if ( !$form->{redo} ) {
  528. CP->get_openinvoices( \%myconfig, \%$form );
  529. $form->{redo} = 1;
  530. }
  531. }
  532. for (qw(duedatefrom duedateto department)) {
  533. $form->{"old$_"} = $form->{$_};
  534. }
  535. $form->{exchangerate} = $exchangerate
  536. if (
  537. $form->{forex} = (
  538. $exchangerate = $form->check_exchangerate(
  539. \%myconfig, $form->{currency}, $form->{datepaid}, $buysell
  540. )
  541. )
  542. );
  543. if ( $form->{redo} ) {
  544. $form->{rowcount} = 0;
  545. $i = 0;
  546. foreach $ref ( @{ $form->{PR} } ) {
  547. $i++;
  548. for (qw(id name invnumber transdate)) {
  549. $form->{"${_}_$i"} = $ref->{$_};
  550. }
  551. $form->{"$form->{vc}_id_$i"} = $ref->{"$form->{vc}_id"};
  552. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  553. $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
  554. $form->{"due_$i"} =
  555. ( $ref->{amount} - $ref->{paid} ) / $ref->{exchangerate};
  556. $form->{"checked_$i"} = "";
  557. $form->{"paid_$i"} = "";
  558. # need to format
  559. for (qw(amount due)) {
  560. $form->{"${_}_$i"} =
  561. $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 );
  562. }
  563. }
  564. $form->{rowcount} = $i;
  565. }
  566. $form->{amount} = $form->parse_amount( \%myconfig, $form->{amount} );
  567. # recalculate
  568. $amount = 0;
  569. for $i ( 1 .. $form->{rowcount} ) {
  570. for (qw(amount due paid)) {
  571. $form->{"${_}_$i"} =
  572. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  573. }
  574. if ( $form->{"checked_$i"} ) {
  575. # calculate paid_$i
  576. if ( !$form->{"paid_$i"} ) {
  577. $form->{"paid_$i"} = $form->{"due_$i"};
  578. }
  579. $amount += $form->{"paid_$i"};
  580. $form->{redo} = 1;
  581. }
  582. else {
  583. $form->{"paid_$i"} = "";
  584. }
  585. for (qw(amount due paid)) {
  586. $form->{"${_}_$i"} =
  587. $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 );
  588. }
  589. }
  590. $form->{amount} += ( $amount - $form->{oldamount} ) if $form->{redo};
  591. &payments_header;
  592. &invoices_due;
  593. &payments_footer;
  594. }
  595. sub update_payment {
  596. if ( $form->{vc} eq 'customer' ) {
  597. $buysell = "buy";
  598. }
  599. else {
  600. $buysell = "sell";
  601. }
  602. $department = $form->{department};
  603. # get customer/vendor
  604. &check_openvc;
  605. $form->{department} = $department;
  606. if ( $form->{datepaid} ne $form->{olddatepaid} ) {
  607. $form->{olddatepaid} = $form->{datepaid};
  608. $form->{oldall_vc} = !$form->{oldall_vc} if $form->{all_vc};
  609. }
  610. if ( $form->{department} ne $form->{olddepartment} ) {
  611. $form->{olddepartment} = $form->{department};
  612. $form->{redo} = 1;
  613. }
  614. # if we switched to all_vc
  615. if ( $form->{all_vc} ne $form->{oldall_vc} ) {
  616. if ( $form->{"select$form->{vc}"} ) {
  617. $form->{redo} = ( $form->{"old$name"} ne $form->{$name} );
  618. }
  619. else {
  620. $form->{redo} =
  621. ( $form->{"old$name"} ne
  622. qq|$form->{$name}--$form->{"${name}_id"}| );
  623. }
  624. $form->{"select$form->{vc}"} = "";
  625. if ( $form->{all_vc} ) {
  626. $form->all_vc( \%myconfig, $form->{vc}, $form->{ARAP}, undef,
  627. $form->{datepaid} );
  628. if ( @{ $form->{"all_$form->{vc}"} } ) {
  629. for ( @{ $form->{"all_$form->{vc}"} } ) {
  630. $form->{"select$form->{vc}"} .=
  631. qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
  632. }
  633. }
  634. }
  635. else {
  636. if ( ( $myconfig{vclimit} * 1 ) > 0 ) {
  637. $form->{ $form->{vc} } = "";
  638. }
  639. CP->get_openvc( \%myconfig, \%$form );
  640. if ( ( $myconfig{vclimit} * 1 ) > 0 ) {
  641. $form->{"all_$form->{vc}"} = $form->{name_list};
  642. }
  643. if ( @{ $form->{"all_$form->{vc}"} } ) {
  644. $newvc =
  645. qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
  646. for ( @{ $form->{"all_$form->{vc}"} } ) {
  647. $form->{"select$form->{vc}"} .=
  648. qq|<option value="$_->{name}--$_->{id}">$_->{name}\n|;
  649. }
  650. # if the name is not the same
  651. if ( $form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/ ) {
  652. $form->{ $form->{vc} } = $newvc;
  653. &check_openvc;
  654. }
  655. }
  656. }
  657. if ( @{ $form->{all_language} } ) {
  658. $form->{selectlanguage} = "<option>\n";
  659. for ( @{ $form->{all_language} } ) {
  660. $form->{selectlanguage} .=
  661. qq|<option value="$_->{code}">$_->{description}\n|;
  662. }
  663. }
  664. }
  665. if ( $new_name_selected || $form->{redo} ) {
  666. CP->get_openinvoices( \%myconfig, \%$form );
  667. ($newvc) = split /--/, $form->{ $form->{vc} };
  668. $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
  669. $form->{redo} = 1;
  670. }
  671. if ( $form->{currency} ne $form->{oldcurrency} ) {
  672. $form->{oldcurrency} = $form->{currency};
  673. if ( !$form->{redo} ) {
  674. CP->get_openinvoices( \%myconfig, \%$form );
  675. $form->{redo} = 1;
  676. }
  677. }
  678. $form->{exchangerate} = $exchangerate
  679. if (
  680. $form->{forex} = (
  681. $exchangerate = $form->check_exchangerate(
  682. \%myconfig, $form->{currency}, $form->{datepaid}, $buysell
  683. )
  684. )
  685. );
  686. if ( $form->{redo} ) {
  687. $form->{rowcount} = 0;
  688. $i = 0;
  689. foreach $ref ( @{ $form->{PR} } ) {
  690. $i++;
  691. $form->{"id_$i"} = $ref->{id};
  692. $form->{"invnumber_$i"} = $ref->{invnumber};
  693. $form->{"transdate_$i"} = $ref->{transdate};
  694. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  695. $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
  696. $form->{"due_$i"} =
  697. ( $ref->{amount} - $ref->{paid} ) / $ref->{exchangerate};
  698. $form->{"checked_$i"} = "";
  699. $form->{"paid_$i"} = "";
  700. # need to format
  701. for (qw(amount due)) {
  702. $form->{"${_}_$i"} =
  703. $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 );
  704. }
  705. }
  706. $form->{rowcount} = $i;
  707. }
  708. $form->{amount} = $form->parse_amount( \%myconfig, $form->{amount} );
  709. # recalculate
  710. $amount = 0;
  711. for $i ( 1 .. $form->{rowcount} ) {
  712. for (qw(amount due paid)) {
  713. $form->{"${_}_$i"} =
  714. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  715. }
  716. if ( $form->{"checked_$i"} ) {
  717. # calculate paid_$i
  718. if ( !$form->{"paid_$i"} ) {
  719. $form->{"paid_$i"} = $form->{"due_$i"};
  720. }
  721. $amount += $form->{"paid_$i"};
  722. $form->{redo} = 1;
  723. }
  724. else {
  725. $form->{"paid_$i"} = "";
  726. }
  727. for (qw(amount due paid)) {
  728. $form->{"${_}_$i"} =
  729. $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 );
  730. }
  731. }
  732. $form->{amount} += ( $amount - $form->{oldamount} ) if $form->{redo};
  733. &payment_header;
  734. &list_invoices;
  735. &payment_footer;
  736. }
  737. sub payment_header {
  738. $vclabel = ucfirst $form->{vc};
  739. $vclabel = $locale->text($vclabel);
  740. if ( $form->{type} eq 'receipt' ) {
  741. $form->{title} = $locale->text('Receipt');
  742. }
  743. if ( $form->{type} eq 'check' ) {
  744. $form->{title} = $locale->text('Payment');
  745. }
  746. # $locale->text('Customer')
  747. # $locale->text('Vendor')
  748. if ( $form->{ $form->{vc} } eq "" ) {
  749. for (qw(address1 address2 city zipcode state country)) {
  750. $form->{$_} = "";
  751. }
  752. }
  753. for ( "$form->{vc}", "department" ) {
  754. $form->{"select$_"} = $form->unescape( $form->{"select$_"} );
  755. $form->{"select$_"} =~ s/ selected//;
  756. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  757. }
  758. for ( "account", "currency", "$form->{ARAP}" ) {
  759. $form->{"select$_"} =~ s/ selected//;
  760. $form->{"select$_"} =~
  761. s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  762. }
  763. if ( $form->{defaultcurrency} ) {
  764. $exchangerate = qq|
  765. <tr>
  766. <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
  767. <td><select name=currency>$form->{selectcurrency}</select></td>
  768. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  769. <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
  770. </tr>
  771. |;
  772. }
  773. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  774. $form->{exchangerate} =
  775. $form->format_amount( \%myconfig, $form->{exchangerate} );
  776. if ( $form->{forex} ) {
  777. $exchangerate .= qq|
  778. <tr>
  779. <th align=right nowrap>| . $locale->text('Exchange Rate') . qq|</th>
  780. <td colspan=3><input type=hidden name=exchangerate size=10 value=$form->{exchangerate}>$form->{exchangerate}</td>
  781. </tr>
  782. |;
  783. }
  784. else {
  785. $exchangerate .= qq|
  786. <tr>
  787. <th align=right nowrap>| . $locale->text('Exchange Rate') . qq|</th>
  788. <td colspan=3><input name=exchangerate size=10 value=$form->{exchangerate}></td>
  789. </tr>
  790. |;
  791. }
  792. }
  793. $vc =
  794. ( $form->{"select$form->{vc}"} )
  795. ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>|
  796. : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
  797. if ( $form->{all_vc} ) {
  798. $allvc = "checked";
  799. }
  800. else {
  801. $allvc = "";
  802. }
  803. # $locale->text('AR')
  804. # $locale->text('AP')
  805. $department = qq|
  806. <tr>
  807. <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
  808. <td><select name=department>$form->{selectdepartment}</select>
  809. <input type=hidden name=selectdepartment value="|
  810. . $form->escape( $form->{selectdepartment}, 1 ) . qq|">
  811. </td>
  812. </tr>
  813. | if $form->{selectdepartment};
  814. $form->header;
  815. print qq|
  816. <body>
  817. <form method=post action=$form->{script}>
  818. |;
  819. $form->hide_form(
  820. qw(defaultcurrency closedto vc type ARAP arap title formname payment olddepartment)
  821. );
  822. print qq|
  823. <table width=100%>
  824. <tr>
  825. <th class=listtop>$form->{title}</th>
  826. </tr>
  827. <tr height="5"></tr>
  828. <tr>
  829. <td>
  830. <table width=100%>
  831. <tr valign=top>
  832. <td>
  833. <table>
  834. <tr>
  835. <td align=right>
  836. <input name=all_vc type=checkbox class=checkbox value=Y $allvc>
  837. <input type=hidden name="oldall_vc" value="$form->{all_vc}"></td>
  838. <th align=left>| . $locale->text('All') . qq|</th>
  839. </tr>
  840. <tr>
  841. <th align=right>$vclabel</th>
  842. <td>$vc</td>
  843. <input type=hidden name="select$form->{vc}" value="|
  844. . $form->escape( $form->{"select$form->{vc}"}, 1 ) . qq|">
  845. <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
  846. <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
  847. </tr>
  848. <tr valign=top>
  849. <th align=right nowrap>| . $locale->text('Address') . qq|</th>
  850. <td colspan=2>
  851. <table>
  852. <tr>
  853. <td>$form->{address1}</td>
  854. </tr>
  855. <tr>
  856. <td>$form->{address2}</td>
  857. </tr>
  858. <td>$form->{city}</td>
  859. </tr>
  860. </tr>
  861. <td>$form->{state}</td>
  862. </tr>
  863. </tr>
  864. <td>$form->{zipcode}</td>
  865. </tr>
  866. <tr>
  867. <td>$form->{country}</td>
  868. </tr>
  869. </table>
  870. </td>
  871. <input type=hidden name=address1 value="$form->{address1}">
  872. <input type=hidden name=address2 value="$form->{address2}">
  873. <input type=hidden name=city value="$form->{city}">
  874. <input type=hidden name=state value="$form->{state}">
  875. <input type=hidden name=zipcode value="$form->{zipcode}">
  876. <input type=hidden name=country value="$form->{country}">
  877. </tr>
  878. <tr>
  879. <th align=right>| . $locale->text('Memo') . qq|</th>
  880. <td colspan=2><input name="memo" size=30 value="$form->{memo}"></td>
  881. </tr>
  882. </table>
  883. </td>
  884. <td align=right>
  885. <table>
  886. $department
  887. <tr>
  888. <th align=right nowrap>| . $locale->text( $form->{ARAP} ) . qq|</th>
  889. <td colspan=3><select name=$form->{ARAP}>$form->{"select$form->{ARAP}"}</select>
  890. </td>
  891. <input type=hidden name="select$form->{ARAP}" value="$form->{"select$form->{ARAP}"}">
  892. </tr>
  893. <tr>
  894. <th align=right nowrap>| . $locale->text('Account') . qq|</th>
  895. <td colspan=3><select name=account>$form->{selectaccount}</select>
  896. <input type=hidden name=selectaccount value="$form->{selectaccount}">
  897. </td>
  898. </tr>
  899. <tr>
  900. <th align=right nowrap>| . $locale->text('Date') . qq|</th>
  901. <td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11></td>
  902. <input type=hidden name=olddatepaid value=$form->{olddatepaid}>
  903. </tr>
  904. $exchangerate
  905. <tr>
  906. <th align=right nowrap>| . $locale->text('Source') . qq|</th>
  907. <td colspan=3><input name=source value="$form->{source}" size=10></td>
  908. </tr>
  909. <tr>
  910. <th align=right nowrap>| . $locale->text('Amount') . qq|</th>
  911. <td colspan=3><input name=amount size=10 value=|
  912. . $form->format_amount( \%myconfig, $form->{amount}, 2 )
  913. . qq|></td>
  914. <input type=hidden name=oldamount value=$form->{amount}>
  915. </tr>
  916. </table>
  917. </td>
  918. </tr>
  919. </table>
  920. </td>
  921. </tr>
  922. |;
  923. }
  924. sub list_invoices {
  925. @column_index = qw(invnumber transdate amount due checked paid);
  926. $colspan = $#column_index + 1;
  927. $invoice = $locale->text('Invoices');
  928. print qq|
  929. <input type=hidden name=column_index value="id @column_index">
  930. <tr>
  931. <td>
  932. <table width=100%>
  933. <tr>
  934. <th class=listheading colspan=$colspan>$invoice</th>
  935. </tr>
  936. |;
  937. $column_data{invnumber} =
  938. qq|<th nowrap>| . $locale->text('Invoice') . "</th>";
  939. $column_data{transdate} = qq|<th nowrap>| . $locale->text('Date') . "</th>";
  940. $column_data{amount} = qq|<th nowrap>| . $locale->text('Amount') . "</th>";
  941. $column_data{due} = qq|<th nowrap>| . $locale->text('Amount Due') . "</th>";
  942. $column_data{paid} = qq|<th nowrap>| . $locale->text('Amount') . "</th>";
  943. $column_data{checked} = qq|<th nowrap>| . $locale->text('Select') . "</th>";
  944. print qq|
  945. <tr>
  946. |;
  947. for (@column_index) { print "$column_data{$_}\n" }
  948. print qq|
  949. </tr>
  950. |;
  951. for $i ( 1 .. $form->{rowcount} ) {
  952. for (qw(amount due paid)) {
  953. $form->{"${_}_$i"} =
  954. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  955. }
  956. $totalamount += $form->{"amount_$i"};
  957. $totaldue += $form->{"due_$i"};
  958. if ( $form->{"paid_$i"} =~ /NaN/ ) {
  959. $form->{"paid_$i"} = '';
  960. }
  961. else {
  962. $totalpaid += $form->{"paid_$i"};
  963. }
  964. for (qw(amount due paid)) {
  965. $form->{"${_}_$i"} =
  966. $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 );
  967. }
  968. $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
  969. <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
  970. <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
  971. $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
  972. <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
  973. $column_data{amount} =
  974. qq|<td align=right width=15%>$form->{"amount_$i"}</td>
  975. <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
  976. $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
  977. <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
  978. $column_data{paid} =
  979. qq|<td align=right width=15%><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
  980. $form->{"checked_$i"} = ( $form->{"checked_$i"} ) ? "checked" : "";
  981. $column_data{checked} =
  982. qq|<td align=center width=10%><input name="checked_$i" type=checkbox class=checkbox $form->{"checked_$i"}></td>|;
  983. $j++;
  984. $j %= 2;
  985. print qq|
  986. <tr class=listrow$j>
  987. |;
  988. for (@column_index) { print "$column_data{$_}\n" }
  989. print qq|
  990. </tr>
  991. |;
  992. }
  993. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  994. $column_data{amount} =
  995. qq|<th class=listtotal align=right>|
  996. . $form->format_amount( \%myconfig, $totalamount, 2, "&nbsp;" )
  997. . qq|</th>|;
  998. $column_data{due} =
  999. qq|<th class=listtotal align=right>|
  1000. . $form->format_amount( \%myconfig, $totaldue, 2, "&nbsp;" )
  1001. . qq|</th>|;
  1002. $column_data{paid} =
  1003. qq|<th class=listtotal align=right>|
  1004. . $form->format_amount( \%myconfig, $totalpaid, 2, "&nbsp;" )
  1005. . qq|</th>|;
  1006. print qq|
  1007. <tr class=listtotal>
  1008. |;
  1009. for (@column_index) { print "$column_data{$_}\n" }
  1010. print qq|
  1011. </tr>
  1012. </table>
  1013. </td>
  1014. </tr>
  1015. |;
  1016. }
  1017. sub payment_footer {
  1018. $form->{DF}{ $form->{format} } = "selected";
  1019. $transdate = $form->datetonum( \%myconfig, $form->{datepaid} );
  1020. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  1021. if ( ${LedgerSMB::Sysconfig::latex} ) {
  1022. if ( $form->{selectlanguage} ) {
  1023. $form->{"selectlanguage"} =
  1024. $form->unescape( $form->{"selectlanguage"} );
  1025. $form->{"selectlanguage"} =~ s/ selected//;
  1026. $form->{"selectlanguage"} =~
  1027. s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  1028. $lang =
  1029. qq|<select name=language_code>$form->{selectlanguage}</select>
  1030. <input type=hidden name=selectlanguage value="|
  1031. . $form->escape( $form->{selectlanguage}, 1 ) . qq|">|;
  1032. }
  1033. $media = qq|<select name=media>
  1034. <option value=screen>| . $locale->text('Screen');
  1035. if ( %{LedgerSMB::Sysconfig::printer} ) {
  1036. for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  1037. $media .= qq|
  1038. <option value="$_">$_|;
  1039. }
  1040. }
  1041. $media .= qq|</select>|;
  1042. $format = qq|<select name=format>
  1043. <option value=postscript $form->{DF}{postscript}>|
  1044. . $locale->text('Postscript') . qq|
  1045. <option value=pdf $form->{DF}{pdf}>|
  1046. . $locale->text('PDF')
  1047. . qq|</select>|;
  1048. }
  1049. print qq|
  1050. <tr>
  1051. <td><hr size=3 noshade></td>
  1052. </tr>
  1053. </table>
  1054. |;
  1055. %button = (
  1056. 'update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  1057. 'select_all' =>
  1058. { ndx => 2, key => 'A', value => $locale->text('Select all') },
  1059. 'print' => { ndx => 3, key => 'P', value => $locale->text('Print') },
  1060. 'post' => { ndx => 4, key => 'O', value => $locale->text('Post') },
  1061. );
  1062. if ( !${LedgerSMB::Sysconfig::latex} ) {
  1063. delete $button{'print'};
  1064. }
  1065. if ( $transdate <= $closedto ) {
  1066. for ( 'post', 'print' ) { delete $button{$_} }
  1067. $media = $format = "";
  1068. }
  1069. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button ) {
  1070. $form->print_button( \%button, $_ );
  1071. }
  1072. $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
  1073. print qq|
  1074. $lang
  1075. $format
  1076. $media
  1077. |;
  1078. $form->hide_form(qw(callback rowcount path login sessionid));
  1079. if ( $form->{lynx} ) {
  1080. require "bin/menu.pl";
  1081. &menubar;
  1082. }
  1083. print qq|
  1084. </form>
  1085. </body>
  1086. </html>
  1087. |;
  1088. }
  1089. sub post { &{"post_$form->{payment}"} }
  1090. sub post_payments {
  1091. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  1092. $form->error( $locale->text('Exchange rate missing!') )
  1093. unless $form->{exchangerate};
  1094. }
  1095. if ( CP->post_payments( \%myconfig, \%$form ) ) {
  1096. $form->redirect( $locale->text('Payments posted!') );
  1097. }
  1098. else {
  1099. $form->error( $locale->text('Posting failed!') );
  1100. }
  1101. }
  1102. sub post_payment {
  1103. &check_form;
  1104. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  1105. $form->error( $locale->text('Exchange rate missing!') )
  1106. unless $form->{exchangerate};
  1107. }
  1108. $msg1 = "$form->{title} posted!";
  1109. $msg2 = "Cannot post $form->{title}!";
  1110. # $locale->text('Payment posted!')
  1111. # $locale->text('Receipt posted!')
  1112. # $locale->text('Cannot post Payment!')
  1113. # $locale->text('Cannot post Receipt!')
  1114. $form->{amount} = $form->format_amount( \%myconfig, $form->{amount}, 2 );
  1115. $source = $form->{source};
  1116. $source =~ s/(\d+)/$1 + 1/e;
  1117. if ( $form->{callback} ) {
  1118. $form->{callback} .= "&source=$source";
  1119. }
  1120. if ( CP->post_payment( \%myconfig, \%$form ) ) {
  1121. $form->redirect( $locale->text($msg1) );
  1122. }
  1123. else {
  1124. $form->error( $locale->text($msg2) );
  1125. }
  1126. }
  1127. sub print {
  1128. &{"print_$form->{payment}"};
  1129. &update if $form->{media} ne 'screen';
  1130. }
  1131. sub print_payments {
  1132. $form->error( $locale->text('Select postscript or PDF!') )
  1133. if ( $form->{format} !~ /(postscript|pdf)/ );
  1134. $SIG{INT} = 'IGNORE';
  1135. for (qw(company address)) { $form->{$_} = $myconfig{$_} }
  1136. $form->{address} =~ s/\\n/\n/g;
  1137. %oldform = ();
  1138. for ( keys %$form ) { $oldform{$_} = $form->{$_} }
  1139. @a =
  1140. qw(name company address text_amount text_decimal address1 address2 city state zipcode country memo);
  1141. for (@a) { $temp{$_} = $form->{$_} }
  1142. $form->format_string(@a);
  1143. $ok = 0;
  1144. $j = 0;
  1145. $temp{rowcount} = $form->{rowcount};
  1146. for $i ( 1 .. $temp{rowcount} ) {
  1147. if ( $form->{"$form->{vc}_id_$i"} ne $form->{"$form->{vc}_id"} ) {
  1148. $form->{rowcount} = $j;
  1149. for ( 1 .. $j ) { $form->{"id_$_"} = $temp{"id_$_"} }
  1150. &print_form if $ok;
  1151. $ok = 0;
  1152. $j = 0;
  1153. $form->{amount} = 0;
  1154. for (qw(invnumber invdate due paid)) { @{ $form->{$_} } = () }
  1155. for (qw(language_code source memo)) {
  1156. $form->{$_} = $form->{"${_}_$i"};
  1157. }
  1158. }
  1159. if ( $form->{"checked_$i"} ) {
  1160. $j++;
  1161. $ok = 1;
  1162. $temp{"id_$j"} = $form->{"id_$i"};
  1163. $form->{"invdate_$i"} = $form->{"transdate_$i"};
  1164. for (qw(invnumber invdate due paid)) {
  1165. push @{ $form->{$_} }, $form->{"${_}_$i"};
  1166. }
  1167. $form->{amount} +=
  1168. $form->parse_amount( \%myconfig, $form->{"paid_$i"} );
  1169. $form->{"$form->{vc}_id"} = $form->{"$form->{vc}_id_$i"};
  1170. }
  1171. }
  1172. $form->{rowcount} = $j;
  1173. for ( 1 .. $j ) { $form->{"id_$_"} = $temp{"id_$_"} }
  1174. &print_form if $ok;
  1175. for ( keys %oldform ) { $form->{$_} = $oldform{$_} }
  1176. }
  1177. sub print_form {
  1178. $c =
  1179. CP->new( ( $form->{language_code} )
  1180. ? $form->{language_code}
  1181. : $myconfig{countrycode} );
  1182. $c->init;
  1183. ( $whole, $form->{decimal} ) = split /\./, $form->{amount};
  1184. $form->{amount} = $form->format_amount( \%myconfig, $form->{amount}, 2 );
  1185. $form->{decimal} .= "00";
  1186. $form->{decimal} = substr( $form->{decimal}, 0, 2 );
  1187. $form->{text_decimal} = $c->num2text( $form->{decimal} * 1 );
  1188. $form->{text_amount} = $c->num2text($whole);
  1189. $form->{integer_amount} = $form->format_amount( $myconfig, $whole );
  1190. $datepaid = $form->datetonum( \%myconfig, $form->{datepaid} );
  1191. ( $form->{yyyy}, $form->{mm}, $form->{dd} ) = $datepaid =~ /(....)(..)(..)/;
  1192. &{"$form->{vc}_details"};
  1193. $form->{templates} = "$myconfig{templates}";
  1194. $form->{IN} = "$form->{formname}.tex";
  1195. if ( $form->{media} ne 'screen' ) {
  1196. $form->{OUT} = "${LedgerSMB::Sysconfig::printer}{$form->{media}}";
  1197. $form->{printmode} = '|-';
  1198. }
  1199. if ( ( $form->{'media'} eq 'screen' ) and ( $form->{'format'} eq 'html' ) )
  1200. {
  1201. my $template =
  1202. LedgerSMB::Template->new( \%myconfig, $form->{'formname'}, 'HTML' );
  1203. try {
  1204. $template->render($form);
  1205. $form->header;
  1206. print $template->{'output'};
  1207. exit;
  1208. }
  1209. catch Error::Simple with {
  1210. my $E = shift;
  1211. $form->error( $E->stacktrace );
  1212. };
  1213. }
  1214. $form->parse_template( \%myconfig, ${LedgerSMB::Sysconfig::userspath} );
  1215. }
  1216. sub print_payment {
  1217. &check_form;
  1218. for (qw(company address)) { $form->{$_} = $myconfig{$_} }
  1219. $form->{address} =~ s/\\n/\n/g;
  1220. @a =
  1221. qw(rowcount name company address text_amount text_decimal address1 address2 city state zipcode country memo);
  1222. %temp = ();
  1223. for (@a) { $temp{$_} = $form->{$_} }
  1224. if (
  1225. scalar @{ $form->{invnumber} } >
  1226. ${LedgerSMB::Sysconfig::check_max_invoices} )
  1227. {
  1228. $#{ $form->{invnumber} } =
  1229. ${LedgerSMB::Sysconfig::check_max_invoices} - 1;
  1230. $form->{invnumbers_maxed} = 1;
  1231. $form->{message} =
  1232. $locale->text(
  1233. "Please see attatched report for list of invoices paid.");
  1234. }
  1235. $form->format_string(@a);
  1236. &print_form;
  1237. for ( keys %temp ) { $form->{$_} = $temp{$_} }
  1238. }
  1239. sub customer_details { IS->customer_details( \%myconfig, \%$form ) }
  1240. sub vendor_details { IR->vendor_details( \%myconfig, \%$form ) }
  1241. sub check_form {
  1242. &check_openvc;
  1243. if ( $form->{currency} ne $form->{oldcurrency} ) {
  1244. &update;
  1245. exit;
  1246. }
  1247. $form->error( $locale->text('Date missing!') ) unless $form->{datepaid};
  1248. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  1249. $datepaid = $form->datetonum( \%myconfig, $form->{datepaid} );
  1250. $form->error( $locale->text('Cannot post payment for a closed period!') )
  1251. if ( $datepaid <= $closedto );
  1252. # this is just to format the year
  1253. $form->{datepaid} = $locale->date( \%myconfig, $form->{datepaid} );
  1254. $amount = $form->parse_amount( \%myconfig, $form->{amount} );
  1255. $form->{amount} = $amount;
  1256. for $i ( 1 .. $form->{rowcount} ) {
  1257. if ( $form->{"paid_$i"} ) {
  1258. $amount -= $form->parse_amount( \%myconfig, $form->{"paid_$i"} );
  1259. push( @{ $form->{paid} }, $form->{"paid_$i"} );
  1260. push( @{ $form->{due} }, $form->{"due_$i"} );
  1261. push( @{ $form->{invnumber} }, $form->{"invnumber_$i"} );
  1262. push( @{ $form->{invdate} }, $form->{"transdate_$i"} );
  1263. }
  1264. }
  1265. if ( $form->round_amount( $amount, 2 ) != 0 ) {
  1266. push(
  1267. @{ $form->{paid} },
  1268. $form->format_amount( \%myconfig, $amount, 2 )
  1269. );
  1270. push( @{ $form->{due} }, $form->format_amount( \%myconfig, 0, "0" ) );
  1271. push(
  1272. @{ $form->{invnumber} },
  1273. ( $form->{ARAP} eq 'AR' )
  1274. ? $locale->text('Deposit')
  1275. : $locale->text('Prepayment')
  1276. );
  1277. push( @{ $form->{invdate} }, $form->{datepaid} );
  1278. }
  1279. }
  1280. sub check_openvc {
  1281. $name = $form->{vc};
  1282. ( $new_name, $new_id ) = split /--/, $form->{$name};
  1283. if ( $form->{all_vc} ) {
  1284. if ( $form->{"select$name"} ) {
  1285. $ok = ( $form->{"old$name"} ne $form->{$name} );
  1286. }
  1287. else {
  1288. $ok =
  1289. ( $form->{"old$name"} ne
  1290. qq|$form->{$name}--$form->{"${name}_id"}| );
  1291. }
  1292. if ($ok) {
  1293. $form->{redo} = 1;
  1294. if ( $form->{"select$name"} ) {
  1295. $form->{"${name}_id"} = $new_id;
  1296. AA->get_name( \%myconfig, \%$form );
  1297. $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
  1298. }
  1299. else {
  1300. &check_name( $form->{vc} );
  1301. }
  1302. }
  1303. }
  1304. else {
  1305. # if we use a selection
  1306. if ( $form->{"select$name"} ) {
  1307. if ( $form->{"old$name"} ne $form->{$name} ) {
  1308. $form->{"${name}_id"} = $new_id;
  1309. AA->get_name( \%myconfig, \%$form );
  1310. $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
  1311. $form->{redo} = 1;
  1312. }
  1313. }
  1314. else {
  1315. # check name, combine name and id
  1316. if ( $form->{"old$name"} ne
  1317. qq|$form->{$name}--$form->{"${name}_id"}| )
  1318. {
  1319. # return one name or a list of names in $form->{name_list}
  1320. if ( ( $rv = CP->get_openvc( \%myconfig, \%$form ) ) > 1 ) {
  1321. $form->{redo} = 1;
  1322. &select_name($name);
  1323. exit;
  1324. }
  1325. if ( $rv == 1 ) {
  1326. # we got one name
  1327. $form->{"${name}_id"} = $form->{name_list}[0]->{id};
  1328. $form->{$name} = $form->{name_list}[0]->{name};
  1329. $form->{"old$name"} =
  1330. qq|$form->{$name}--$form->{"${name}_id"}|;
  1331. AA->get_name( \%myconfig, \%$form );
  1332. }
  1333. else {
  1334. # nothing open
  1335. $form->error( $locale->text('Nothing open!') );
  1336. }
  1337. $form->{redo} = 1;
  1338. }
  1339. }
  1340. }
  1341. }