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