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