summaryrefslogtreecommitdiff
path: root/bin/cp.pl
blob: d9316feac5f8d9e0c0dd6cc002da21b2406d8367 (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 name=duedatefrom value="$form->{duedatefrom}" title="$myconfig{dateformat}" size=11></td>
  282. <th align=right>| . $locale->text('To') . qq|</th>
  283. <td><input 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 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. }
  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. }
  738. }
  739. $form->{amount} += ( $amount - $form->{oldamount} ) if $form->{redo};
  740. &payment_header;
  741. &list_invoices;
  742. &payment_footer;
  743. }
  744. sub payment_header {
  745. $vclabel = ucfirst $form->{vc};
  746. $vclabel = $locale->text($vclabel);
  747. if ( $form->{type} eq 'receipt' ) {
  748. $form->{title} = $locale->text('Receipt');
  749. }
  750. if ( $form->{type} eq 'check' ) {
  751. $form->{title} = $locale->text('Payment');
  752. }
  753. # $locale->text('Customer')
  754. # $locale->text('Vendor')
  755. if ( $form->{ $form->{vc} } eq "" ) {
  756. for (qw(address1 address2 city zipcode state country)) {
  757. $form->{$_} = "";
  758. }
  759. }
  760. for ( "$form->{vc}", "department" ) {
  761. $form->{"select$_"} = $form->unescape( $form->{"select$_"} );
  762. $form->{"select$_"} =~ s/ selected//;
  763. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  764. }
  765. for ( "account", "currency", "$form->{ARAP}" ) {
  766. $form->{"select$_"} =~ s/ selected//;
  767. $form->{"select$_"} =~
  768. s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  769. }
  770. if ( $form->{defaultcurrency} ) {
  771. $exchangerate = qq|
  772. <tr>
  773. <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
  774. <td><select name=currency>$form->{selectcurrency}</select></td>
  775. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  776. <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
  777. </tr>
  778. |;
  779. }
  780. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  781. $form->{exchangerate} =
  782. $form->format_amount( \%myconfig, $form->{exchangerate} );
  783. if ( $form->{forex} ) {
  784. $exchangerate .= qq|
  785. <tr>
  786. <th align=right nowrap>| . $locale->text('Exchange Rate') . qq|</th>
  787. <td colspan=3><input type=hidden name=exchangerate size=10 value=$form->{exchangerate}>$form->{exchangerate}</td>
  788. </tr>
  789. |;
  790. }
  791. else {
  792. $exchangerate .= qq|
  793. <tr>
  794. <th align=right nowrap>| . $locale->text('Exchange Rate') . qq|</th>
  795. <td colspan=3><input name=exchangerate size=10 value=$form->{exchangerate}></td>
  796. </tr>
  797. |;
  798. }
  799. }
  800. $vc =
  801. ( $form->{"select$form->{vc}"} )
  802. ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>|
  803. : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
  804. if ( $form->{all_vc} ) {
  805. $allvc = "checked";
  806. }
  807. else {
  808. $allvc = "";
  809. }
  810. # $locale->text('AR')
  811. # $locale->text('AP')
  812. $department = qq|
  813. <tr>
  814. <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
  815. <td><select name=department>$form->{selectdepartment}</select>
  816. <input type=hidden name=selectdepartment value="|
  817. . $form->escape( $form->{selectdepartment}, 1 ) . qq|">
  818. </td>
  819. </tr>
  820. | if $form->{selectdepartment};
  821. $form->header;
  822. print qq|
  823. <body>
  824. <form method=post action=$form->{script}>
  825. |;
  826. $form->hide_form(
  827. qw(defaultcurrency closedto vc type ARAP arap title formname payment olddepartment)
  828. );
  829. print qq|
  830. <table width=100%>
  831. <tr>
  832. <th class=listtop>$form->{title}</th>
  833. </tr>
  834. <tr height="5"></tr>
  835. <tr>
  836. <td>
  837. <table width=100%>
  838. <tr valign=top>
  839. <td>
  840. <table>
  841. <tr>
  842. <td align=right>
  843. <input name=all_vc type=checkbox class=checkbox value=Y $allvc>
  844. <input type=hidden name="oldall_vc" value="$form->{all_vc}"></td>
  845. <th align=left>| . $locale->text('All') . qq|</th>
  846. </tr>
  847. <tr>
  848. <th align=right>$vclabel</th>
  849. <td>$vc</td>
  850. <input type=hidden name="select$form->{vc}" value="|
  851. . $form->escape( $form->{"select$form->{vc}"}, 1 ) . qq|">
  852. <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
  853. <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
  854. </tr>
  855. <tr valign=top>
  856. <th align=right nowrap>| . $locale->text('Address') . qq|</th>
  857. <td colspan=2>
  858. <table>
  859. <tr>
  860. <td>$form->{address1}</td>
  861. </tr>
  862. <tr>
  863. <td>$form->{address2}</td>
  864. </tr>
  865. <td>$form->{city}</td>
  866. </tr>
  867. </tr>
  868. <td>$form->{state}</td>
  869. </tr>
  870. </tr>
  871. <td>$form->{zipcode}</td>
  872. </tr>
  873. <tr>
  874. <td>$form->{country}</td>
  875. </tr>
  876. </table>
  877. </td>
  878. <input type=hidden name=address1 value="$form->{address1}">
  879. <input type=hidden name=address2 value="$form->{address2}">
  880. <input type=hidden name=city value="$form->{city}">
  881. <input type=hidden name=state value="$form->{state}">
  882. <input type=hidden name=zipcode value="$form->{zipcode}">
  883. <input type=hidden name=country value="$form->{country}">
  884. </tr>
  885. <tr>
  886. <th align=right>| . $locale->text('Memo') . qq|</th>
  887. <td colspan=2><input name="memo" size=30 value="$form->{memo}"></td>
  888. </tr>
  889. </table>
  890. </td>
  891. <td align=right>
  892. <table>
  893. $department
  894. <tr>
  895. <th align=right nowrap>| . $locale->text( $form->{ARAP} ) . qq|</th>
  896. <td colspan=3><select name=$form->{ARAP}>$form->{"select$form->{ARAP}"}</select>
  897. </td>
  898. <input type=hidden name="select$form->{ARAP}" value="$form->{"select$form->{ARAP}"}">
  899. </tr>
  900. <tr>
  901. <th align=right nowrap>| . $locale->text('Account') . qq|</th>
  902. <td colspan=3><select name=account>$form->{selectaccount}</select>
  903. <input type=hidden name=selectaccount value="$form->{selectaccount}">
  904. </td>
  905. </tr>
  906. <tr>
  907. <th align=right nowrap>| . $locale->text('Date') . qq|</th>
  908. <td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11></td>
  909. <input type=hidden name=olddatepaid value=$form->{olddatepaid}>
  910. </tr>
  911. $exchangerate
  912. <tr>
  913. <th align=right nowrap>| . $locale->text('Source') . qq|</th>
  914. <td colspan=3><input name=source value="$form->{source}" size=10></td>
  915. </tr>
  916. <tr>
  917. <th align=right nowrap>| . $locale->text('Amount') . qq|</th>
  918. <td colspan=3><input name=amount size=10 value=|
  919. . $form->format_amount( \%myconfig, $form->{amount}, 2 )
  920. . qq|></td>
  921. <input type=hidden name=oldamount value=$form->{amount}>
  922. </tr>
  923. </table>
  924. </td>
  925. </tr>
  926. </table>
  927. </td>
  928. </tr>
  929. |;
  930. }
  931. sub list_invoices {
  932. @column_index = qw(invnumber transdate amount due checked paid);
  933. $colspan = $#column_index + 1;
  934. $invoice = $locale->text('Invoices');
  935. print qq|
  936. <input type=hidden name=column_index value="id @column_index">
  937. <tr>
  938. <td>
  939. <table width=100%>
  940. <tr>
  941. <th class=listheading colspan=$colspan>$invoice</th>
  942. </tr>
  943. |;
  944. $column_data{invnumber} =
  945. qq|<th nowrap>| . $locale->text('Invoice') . "</th>";
  946. $column_data{transdate} = qq|<th nowrap>| . $locale->text('Date') . "</th>";
  947. $column_data{amount} = qq|<th nowrap>| . $locale->text('Amount') . "</th>";
  948. $column_data{due} = qq|<th nowrap>| . $locale->text('Amount Due') . "</th>";
  949. $column_data{paid} = qq|<th nowrap>| . $locale->text('Amount') . "</th>";
  950. $column_data{checked} = qq|<th nowrap>| . $locale->text('Select') . "</th>";
  951. print qq|
  952. <tr>
  953. |;
  954. for (@column_index) { print "$column_data{$_}\n" }
  955. print qq|
  956. </tr>
  957. |;
  958. for $i ( 1 .. $form->{rowcount} ) {
  959. for (qw(amount due paid)) {
  960. $form->{"${_}_$i"} =
  961. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  962. }
  963. $totalamount += $form->{"amount_$i"};
  964. $totaldue += $form->{"due_$i"};
  965. if ( $form->{"paid_$i"} !~ /NaN/ ) {
  966. $totalpaid += $form->{"paid_$i"};
  967. }
  968. for (qw(amount due paid)) {
  969. $form->{"${_}_$i"} =
  970. $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 );
  971. }
  972. $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
  973. <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
  974. <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
  975. $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
  976. <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
  977. $column_data{amount} =
  978. qq|<td align=right width=15%>$form->{"amount_$i"}</td>
  979. <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
  980. $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
  981. <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
  982. if ( $form->{"paid_$i"} =~ /NaN/ ) {
  983. $form->{"paid_$i"} = '';
  984. }
  985. $column_data{paid} =
  986. qq|<td align=right width=15%><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
  987. $form->{"checked_$i"} = ( $form->{"checked_$i"} ) ? "checked" : "";
  988. $column_data{checked} =
  989. qq|<td align=center width=10%><input name="checked_$i" type=checkbox class=checkbox $form->{"checked_$i"}></td>|;
  990. $j++;
  991. $j %= 2;
  992. print qq|
  993. <tr class=listrow$j>
  994. |;
  995. for (@column_index) { print "$column_data{$_}\n" }
  996. print qq|
  997. </tr>
  998. |;
  999. }
  1000. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  1001. $column_data{amount} =
  1002. qq|<th class=listtotal align=right>|
  1003. . $form->format_amount( \%myconfig, $totalamount, 2, "&nbsp;" )
  1004. . qq|</th>|;
  1005. $column_data{due} =
  1006. qq|<th class=listtotal align=right>|
  1007. . $form->format_amount( \%myconfig, $totaldue, 2, "&nbsp;" )
  1008. . qq|</th>|;
  1009. $column_data{paid} =
  1010. qq|<th class=listtotal align=right>|
  1011. . $form->format_amount( \%myconfig, $totalpaid, 2, "&nbsp;" )
  1012. . qq|</th>|;
  1013. print qq|
  1014. <tr class=listtotal>
  1015. |;
  1016. for (@column_index) { print "$column_data{$_}\n" }
  1017. print qq|
  1018. </tr>
  1019. </table>
  1020. </td>
  1021. </tr>
  1022. |;
  1023. }
  1024. sub payment_footer {
  1025. $form->{DF}{ $form->{format} } = "selected";
  1026. $transdate = $form->datetonum( \%myconfig, $form->{datepaid} );
  1027. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  1028. if ( ${LedgerSMB::Sysconfig::latex} ) {
  1029. if ( $form->{selectlanguage} ) {
  1030. $form->{"selectlanguage"} =
  1031. $form->unescape( $form->{"selectlanguage"} );
  1032. $form->{"selectlanguage"} =~ s/ selected//;
  1033. $form->{"selectlanguage"} =~
  1034. s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  1035. $lang =
  1036. qq|<select name=language_code>$form->{selectlanguage}</select>
  1037. <input type=hidden name=selectlanguage value="|
  1038. . $form->escape( $form->{selectlanguage}, 1 ) . qq|">|;
  1039. }
  1040. $media = qq|<select name=media>
  1041. <option value=screen>| . $locale->text('Screen');
  1042. if ( %{LedgerSMB::Sysconfig::printer} ) {
  1043. for ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  1044. $media .= qq|
  1045. <option value="$_">$_|;
  1046. }
  1047. }
  1048. $media .= qq|</select>|;
  1049. $format = qq|<select name=format>
  1050. <option value=postscript $form->{DF}{postscript}>|
  1051. . $locale->text('Postscript') . qq|
  1052. <option value=pdf $form->{DF}{pdf}>|
  1053. . $locale->text('PDF')
  1054. . qq|</select>|;
  1055. }
  1056. print qq|
  1057. <tr>
  1058. <td><hr size=3 noshade></td>
  1059. </tr>
  1060. </table>
  1061. |;
  1062. %button = (
  1063. 'update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  1064. 'select_all' =>
  1065. { ndx => 2, key => 'A', value => $locale->text('Select all') },
  1066. 'print' => { ndx => 3, key => 'P', value => $locale->text('Print') },
  1067. 'post' => { ndx => 4, key => 'O', value => $locale->text('Post') },
  1068. );
  1069. if ( !${LedgerSMB::Sysconfig::latex} ) {
  1070. delete $button{'print'};
  1071. }
  1072. if ( $transdate <= $closedto ) {
  1073. for ( 'post', 'print' ) { delete $button{$_} }
  1074. $media = $format = "";
  1075. }
  1076. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button ) {
  1077. $form->print_button( \%button, $_ );
  1078. }
  1079. $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
  1080. print qq|
  1081. $lang
  1082. $format
  1083. $media
  1084. |;
  1085. $form->hide_form(qw(callback rowcount path login sessionid));
  1086. if ( $form->{lynx} ) {
  1087. require "bin/menu.pl";
  1088. &menubar;
  1089. }
  1090. print qq|
  1091. </form>
  1092. </body>
  1093. </html>
  1094. |;
  1095. }
  1096. sub post {
  1097. &default_date;
  1098. &{"post_$form->{payment}"};
  1099. }
  1100. sub post_payments {
  1101. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  1102. $form->error( $locale->text('Exchange rate missing!') )
  1103. unless $form->{exchangerate};
  1104. }
  1105. if ( CP->post_payments( \%myconfig, \%$form ) ) {
  1106. $form->redirect( $locale->text('Payments posted!') );
  1107. }
  1108. else {
  1109. $form->error( $locale->text('Posting failed!') );
  1110. }
  1111. }
  1112. sub post_payment {
  1113. &check_form;
  1114. if ( $form->{currency} ne $form->{defaultcurrency} ) {
  1115. $form->error( $locale->text('Exchange rate missing!') )
  1116. unless $form->{exchangerate};
  1117. }
  1118. $msg1 = "$form->{title} posted!";
  1119. $msg2 = "Cannot post $form->{title}!";
  1120. # $locale->text('Payment posted!')
  1121. # $locale->text('Receipt posted!')
  1122. # $locale->text('Cannot post Payment!')
  1123. # $locale->text('Cannot post Receipt!')
  1124. $form->{amount} = $form->format_amount( \%myconfig, $form->{amount}, 2 );
  1125. $source = $form->{source};
  1126. $source =~ s/(\d+)/$1 + 1/e;
  1127. if ( $form->{callback} ) {
  1128. $form->{callback} .= "&source=$source";
  1129. }
  1130. if ( CP->post_payment( \%myconfig, \%$form ) ) {
  1131. $form->redirect( $locale->text($msg1) );
  1132. }
  1133. else {
  1134. $form->error( $locale->text($msg2) );
  1135. }
  1136. }
  1137. sub print {
  1138. &{"print_$form->{payment}"};
  1139. &update if $form->{media} ne 'screen';
  1140. }
  1141. sub print_payments {
  1142. $form->error( $locale->text('Select postscript or PDF!') )
  1143. if ( $form->{format} !~ /(postscript|pdf)/ );
  1144. $SIG{INT} = 'IGNORE';
  1145. for (qw(company address)) { $form->{$_} = $myconfig{$_} }
  1146. $form->{address} =~ s/\\n/\n/g;
  1147. %oldform = ();
  1148. for ( keys %$form ) { $oldform{$_} = $form->{$_} }
  1149. @a =
  1150. qw(name company address text_amount text_decimal address1 address2 city state zipcode country memo);
  1151. for (@a) { $temp{$_} = $form->{$_} }
  1152. $form->format_string(@a);
  1153. $ok = 0;
  1154. $j = 0;
  1155. $temp{rowcount} = $form->{rowcount};
  1156. for $i ( 1 .. $temp{rowcount} ) {
  1157. if ( $form->{"$form->{vc}_id_$i"} ne $form->{"$form->{vc}_id"} ) {
  1158. $form->{rowcount} = $j;
  1159. for ( 1 .. $j ) { $form->{"id_$_"} = $temp{"id_$_"} }
  1160. &print_form if $ok;
  1161. $ok = 0;
  1162. $j = 0;
  1163. $form->{amount} = 0;
  1164. for (qw(invnumber invdate due paid)) { @{ $form->{$_} } = () }
  1165. for (qw(language_code source memo)) {
  1166. $form->{$_} = $form->{"${_}_$i"};
  1167. }
  1168. }
  1169. if ( $form->{"checked_$i"} ) {
  1170. $j++;
  1171. $ok = 1;
  1172. $temp{"id_$j"} = $form->{"id_$i"};
  1173. $form->{"invdate_$i"} = $form->{"transdate_$i"};
  1174. for (qw(invnumber invdate due paid)) {
  1175. push @{ $form->{$_} }, $form->{"${_}_$i"};
  1176. }
  1177. $form->{amount} +=
  1178. $form->parse_amount( \%myconfig, $form->{"paid_$i"} );
  1179. $form->{"$form->{vc}_id"} = $form->{"$form->{vc}_id_$i"};
  1180. }
  1181. }
  1182. $form->{rowcount} = $j;
  1183. for ( 1 .. $j ) { $form->{"id_$_"} = $temp{"id_$_"} }
  1184. &print_form if $ok;
  1185. for ( keys %oldform ) { $form->{$_} = $oldform{$_} }
  1186. }
  1187. sub print_form {
  1188. $c =
  1189. CP->new( ( $form->{language_code} )
  1190. ? $form->{language_code}
  1191. : $myconfig{countrycode} );
  1192. $c->init;
  1193. ( $whole, $form->{decimal} ) = split /\./, $form->{amount};
  1194. $form->{amount} = $form->format_amount( \%myconfig, $form->{amount}, 2 );
  1195. $form->{decimal} .= "00";
  1196. $form->{decimal} = substr( $form->{decimal}, 0, 2 );
  1197. $form->{text_decimal} = $c->num2text( $form->{decimal} * 1 );
  1198. $form->{text_amount} = $c->num2text($whole);
  1199. $form->{integer_amount} = $form->format_amount( $myconfig, $whole );
  1200. $datepaid = $form->datetonum( \%myconfig, $form->{datepaid} );
  1201. ( $form->{yyyy}, $form->{mm}, $form->{dd} ) = $datepaid =~ /(....)(..)(..)/;
  1202. &{"$form->{vc}_details"};
  1203. $form->{templates} = "$myconfig{templates}";
  1204. $form->{IN} = "$form->{formname}.tex";
  1205. if ( $form->{media} ne 'screen' ) {
  1206. $form->{OUT} = "${LedgerSMB::Sysconfig::printer}{$form->{media}}";
  1207. $form->{printmode} = '|-';
  1208. }
  1209. my $template = LedgerSMB::Template->new( user => \%myconfig,
  1210. template => $form->{'formname'}, format => uc $form->{format} );
  1211. try {
  1212. $template->render($form);
  1213. $template->output($form->{media});
  1214. }
  1215. catch Error::Simple with {
  1216. my $E = shift;
  1217. $form->error( $E->stacktrace );
  1218. };
  1219. }
  1220. sub print_payment {
  1221. &check_form;
  1222. for (qw(company address)) { $form->{$_} = $myconfig{$_} }
  1223. $form->{address} =~ s/\\n/\n/g;
  1224. @a =
  1225. qw(rowcount name company address text_amount text_decimal address1 address2 city state zipcode country memo);
  1226. %temp = ();
  1227. for (@a) { $temp{$_} = $form->{$_} }
  1228. if (
  1229. scalar @{ $form->{invnumber} } >
  1230. ${LedgerSMB::Sysconfig::check_max_invoices} )
  1231. {
  1232. $#{ $form->{invnumber} } =
  1233. ${LedgerSMB::Sysconfig::check_max_invoices} - 1;
  1234. $form->{invnumbers_maxed} = 1;
  1235. $form->{message} =
  1236. $locale->text(
  1237. "Please see attatched report for list of invoices paid.");
  1238. }
  1239. $form->format_string(@a);
  1240. &print_form;
  1241. for ( keys %temp ) { $form->{$_} = $temp{$_} }
  1242. }
  1243. sub customer_details { IS->customer_details( \%myconfig, \%$form ) }
  1244. sub vendor_details { IR->vendor_details( \%myconfig, \%$form ) }
  1245. sub check_form {
  1246. &check_openvc;
  1247. if ( $form->{currency} ne $form->{oldcurrency} ) {
  1248. &update;
  1249. exit;
  1250. }
  1251. $form->error( $locale->text('Date missing!') ) unless $form->{datepaid};
  1252. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  1253. $datepaid = $form->datetonum( \%myconfig, $form->{datepaid} );
  1254. $form->error( $locale->text('Cannot post payment for a closed period!') )
  1255. if ( $datepaid <= $closedto );
  1256. $form->{datepaid} = $locale->date( \%myconfig, $form->{datepaid} );
  1257. $amount = $form->parse_amount( \%myconfig, $form->{amount} );
  1258. $form->{amount} = $amount;
  1259. for $i ( 1 .. $form->{rowcount} ) {
  1260. if ( $form->{"paid_$i"} ) {
  1261. $amount -= $form->parse_amount( \%myconfig, $form->{"paid_$i"} );
  1262. push( @{ $form->{paid} }, $form->{"paid_$i"} );
  1263. push( @{ $form->{due} }, $form->{"due_$i"} );
  1264. push( @{ $form->{invnumber} }, $form->{"invnumber_$i"} );
  1265. push( @{ $form->{invdate} }, $form->{"transdate_$i"} );
  1266. }
  1267. }
  1268. if ( $form->round_amount( $amount, 2 ) != 0 ) {
  1269. push(
  1270. @{ $form->{paid} },
  1271. $form->format_amount( \%myconfig, $amount, 2 )
  1272. );
  1273. push( @{ $form->{due} }, $form->format_amount( \%myconfig, 0, "0" ) );
  1274. push(
  1275. @{ $form->{invnumber} },
  1276. ( $form->{ARAP} eq 'AR' )
  1277. ? $locale->text('Deposit')
  1278. : $locale->text('Prepayment')
  1279. );
  1280. push( @{ $form->{invdate} }, $form->{datepaid} );
  1281. }
  1282. }
  1283. sub check_openvc {
  1284. $name = $form->{vc};
  1285. ( $new_name, $new_id ) = split /--/, $form->{$name};
  1286. if ( $form->{all_vc} ) {
  1287. if ( $form->{"select$name"} ) {
  1288. $ok = ( $form->{"old$name"} ne $form->{$name} );
  1289. }
  1290. else {
  1291. $ok =
  1292. ( $form->{"old$name"} ne
  1293. qq|$form->{$name}--$form->{"${name}_id"}| );
  1294. }
  1295. if ($ok) {
  1296. $form->{redo} = 1;
  1297. if ( $form->{"select$name"} ) {
  1298. $form->{"${name}_id"} = $new_id;
  1299. AA->get_name( \%myconfig, \%$form );
  1300. $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
  1301. }
  1302. else {
  1303. &check_name( $form->{vc} );
  1304. }
  1305. }
  1306. }
  1307. else {
  1308. # if we use a selection
  1309. if ( $form->{"select$name"} ) {
  1310. if ( $form->{"old$name"} ne $form->{$name} ) {
  1311. $form->{"${name}_id"} = $new_id;
  1312. AA->get_name( \%myconfig, \%$form );
  1313. $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
  1314. $form->{redo} = 1;
  1315. }
  1316. }
  1317. else {
  1318. # check name, combine name and id
  1319. if ( $form->{"old$name"} ne
  1320. qq|$form->{$name}--$form->{"${name}_id"}| )
  1321. {
  1322. # return one name or a list of names in $form->{name_list}
  1323. if ( ( $rv = CP->get_openvc( \%myconfig, \%$form ) ) > 1 ) {
  1324. $form->{redo} = 1;
  1325. &select_name($name);
  1326. exit;
  1327. }
  1328. if ( $rv == 1 ) {
  1329. # we got one name
  1330. $form->{"${name}_id"} = $form->{name_list}[0]->{id};
  1331. $form->{$name} = $form->{name_list}[0]->{name};
  1332. $form->{"old$name"} =
  1333. qq|$form->{$name}--$form->{"${name}_id"}|;
  1334. AA->get_name( \%myconfig, \%$form );
  1335. }
  1336. else {
  1337. # nothing open
  1338. $form->error( $locale->text('Nothing open!') );
  1339. }
  1340. $form->{redo} = 1;
  1341. }
  1342. }
  1343. }
  1344. }