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