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