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