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