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