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