summaryrefslogtreecommitdiff
path: root/bin/cp.pl
blob: 0facb738adaf384e94fe097e967b614474502e0f (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. $totalpaid += $form->{"paid_$i"};
  306. } else {
  307. $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. $column_data{paid} = qq|<td align=right><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
  320. if ($same_id eq $form->{"$form->{vc}_id_$i"}) {
  321. for (qw(name memo source language)) { $column_data{$_} = qq|<td>&nbsp;</td>| }
  322. } else {
  323. $column_data{name} = qq|<td>$form->{"name_$i"}</td>|;
  324. $column_data{memo} = qq|<td align=right><input name="memo_$i" size=10 value="$form->{"memo_$i"}"></td>|;
  325. $column_data{source} = qq|<td align=right><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
  326. if ($form->{selectlanguage}) {
  327. $selectlanguage = $form->{selectlanguage};
  328. $selectlanguage =~ s/(<option value="\Q$form->{"language_code_$i"}\E")/$1 selected/;
  329. $column_data{language} = qq|<td><select name="language_code_$i">$selectlanguage</select></td>|;
  330. }
  331. }
  332. $column_data{name} .= qq|
  333. <input type=hidden name="name_$i" value="$form->{"name_$i"}">
  334. <input type=hidden name="$form->{vc}_id_$i" value="$form->{"$form->{vc}_id_$i"}">|;
  335. $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
  336. $column_data{checked} = qq|<td align=center><input name="checked_$i" type=checkbox class=checkbox $form->{"checked_$i"}></td>|;
  337. $j++; $j %= 2;
  338. print qq|
  339. <tr class=listrow$j>
  340. |;
  341. for (@column_index) { print "$column_data{$_}\n" }
  342. print qq|
  343. </tr>
  344. |;
  345. $same_id = $form->{"$form->{vc}_id_$i"};
  346. }
  347. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  348. $column_data{amount} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;").qq|</th>|;
  349. $column_data{due} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;").qq|</th>|;
  350. $column_data{paid} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;").qq|</th>|;
  351. print qq|
  352. <tr class=listtotal>
  353. |;
  354. for (@column_index) { print "$column_data{$_}\n" }
  355. print qq|
  356. </tr>
  357. </table>
  358. </td>
  359. </tr>
  360. <input type=hidden name=selectlanguage value="|.$form->escape($form->{selectlanguage},1).qq|">
  361. |;
  362. }
  363. sub payments_footer {
  364. $form->{DF}{$form->{format}} = "selected";
  365. $transdate = $form->datetonum(\%myconfig, $form->{datepaid});
  366. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  367. if (${LedgerSMB::Sysconfig::latex}) {
  368. $media = qq|<select name=media>
  369. <option value=screen>|.$locale->text('Screen');
  370. if (%{LedgerSMB::Sysconfig::printer}) {
  371. for (sort keys %{LedgerSMB::Sysconfig::printer}) { $media .= qq|
  372. <option value="$_">$_| }
  373. }
  374. $media .= qq|</select>|;
  375. $format = qq|<select name=format>
  376. <option value=postscript $form->{DF}{postscript}>|.$locale->text('Postscript').qq|
  377. <option value=pdf $form->{DF}{pdf}>|.$locale->text('PDF').qq|</select>|;
  378. }
  379. print qq|
  380. <tr>
  381. <td><hr size=3 noshade></td>
  382. </tr>
  383. </table>
  384. |;
  385. # type=submit $locale->text('Update')
  386. # type=submit $locale->text('Post')
  387. # type=submit $locale->text('Print')
  388. # type=submit $locale->text('Select all')
  389. %button = ('update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  390. 'select_all' => { ndx => 2, key => 'A', value => $locale->text('Select all') },
  391. 'print' => { ndx => 3, key => 'P', value => $locale->text('Print') },
  392. 'post' => { ndx => 4, key => 'O', value => $locale->text('Post') },
  393. );
  394. if (! ${LedgerSMB::Sysconfig::latex}) {
  395. delete $button{'print'};
  396. }
  397. if ($transdate <= $closedto) {
  398. for ('post', 'print') { delete $button{$_} }
  399. $media = $format = "";
  400. }
  401. for (sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button) { $form->print_button(\%button, $_) }
  402. $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
  403. print qq|
  404. $format
  405. $media
  406. |;
  407. $form->hide_form(qw(callback rowcount path login sessionid));
  408. if ($form->{lynx}) {
  409. require "bin/menu.pl";
  410. &menubar;
  411. }
  412. print qq|
  413. </form>
  414. </body>
  415. </html>
  416. |;
  417. }
  418. sub select_all {
  419. for (1 .. $form->{rowcount}) { $form->{"checked_$_"} = 1 }
  420. &{"update_$form->{payment}"}
  421. }
  422. sub update {
  423. my ($new_name_selected) = @_;
  424. &{"update_$form->{payment}"};
  425. }
  426. sub update_payments {
  427. if ($form->{ARAP} eq 'AR') {
  428. $buysell = "buy";
  429. } else {
  430. $buysell = "sell";
  431. }
  432. if (($form->{oldduedatefrom} ne $form->{duedatefrom}) || ($form->{oldduedateto} ne $form->{duedateto}) || ($form->{department} ne $form->{olddepartment})) {
  433. CP->get_openinvoices(\%myconfig, \%$form);
  434. $form->{redo} = 1;
  435. }
  436. if ($form->{currency} ne $form->{oldcurrency}) {
  437. $form->{oldcurrency} = $form->{currency};
  438. if (!$form->{redo}) {
  439. CP->get_openinvoices(\%myconfig, \%$form);
  440. $form->{redo} = 1;
  441. }
  442. }
  443. for (qw(duedatefrom duedateto department)) { $form->{"old$_"} = $form->{$_} }
  444. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid}, $buysell)));
  445. if ($form->{redo}) {
  446. $form->{rowcount} = 0;
  447. $i = 0;
  448. foreach $ref (@{ $form->{PR} }) {
  449. $i++;
  450. for (qw(id name invnumber transdate)) { $form->{"${_}_$i"} = $ref->{$_} }
  451. $form->{"$form->{vc}_id_$i"} = $ref->{"$form->{vc}_id"};
  452. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  453. $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
  454. $form->{"due_$i"} = ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
  455. $form->{"checked_$i"} = "";
  456. $form->{"paid_$i"} = "";
  457. # need to format
  458. for (qw(amount due)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) }
  459. }
  460. $form->{rowcount} = $i;
  461. }
  462. $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
  463. # recalculate
  464. $amount = 0;
  465. for $i (1 .. $form->{rowcount}) {
  466. for (qw(amount due paid)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  467. if ($form->{"checked_$i"}) {
  468. # calculate paid_$i
  469. if (!$form->{"paid_$i"}) {
  470. $form->{"paid_$i"} = $form->{"due_$i"};
  471. }
  472. $amount += $form->{"paid_$i"};
  473. $form->{redo} = 1;
  474. } else {
  475. $form->{"paid_$i"} = "";
  476. }
  477. for (qw(amount due paid)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) }
  478. }
  479. $form->{amount} += ($amount - $form->{oldamount}) if $form->{redo};
  480. &payments_header;
  481. &invoices_due;
  482. &payments_footer;
  483. }
  484. sub update_payment {
  485. if ($form->{vc} eq 'customer') {
  486. $buysell = "buy";
  487. } else {
  488. $buysell = "sell";
  489. }
  490. $department = $form->{department};
  491. # get customer/vendor
  492. &check_openvc;
  493. $form->{department} = $department;
  494. if ($form->{datepaid} ne $form->{olddatepaid}) {
  495. $form->{olddatepaid} = $form->{datepaid};
  496. $form->{oldall_vc} = !$form->{oldall_vc} if $form->{all_vc};
  497. }
  498. if ($form->{department} ne $form->{olddepartment}) {
  499. $form->{olddepartment} = $form->{department};
  500. $form->{redo} = 1;
  501. }
  502. # if we switched to all_vc
  503. if ($form->{all_vc} ne $form->{oldall_vc}) {
  504. if ($form->{"select$form->{vc}"}) {
  505. $form->{redo} = ($form->{"old$name"} ne $form->{$name});
  506. } else {
  507. $form->{redo} = ($form->{"old$name"} ne qq|$form->{$name}--$form->{"${name}_id"}|);
  508. }
  509. $form->{"select$form->{vc}"} = "";
  510. if ($form->{all_vc}) {
  511. $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP}, undef, $form->{datepaid});
  512. if (@{ $form->{"all_$form->{vc}"} }) {
  513. for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| }
  514. }
  515. } else {
  516. if (($myconfig{vclimit} * 1) > 0) {
  517. $form->{$form->{vc}} = "";
  518. }
  519. CP->get_openvc(\%myconfig, \%$form);
  520. if (($myconfig{vclimit} * 1) > 0) {
  521. $form->{"all_$form->{vc}"} = $form->{name_list};
  522. }
  523. if (@{ $form->{"all_$form->{vc}"} }) {
  524. $newvc = qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
  525. for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|<option value="$_->{name}--$_->{id}">$_->{name}\n| }
  526. # if the name is not the same
  527. if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
  528. $form->{$form->{vc}} = $newvc;
  529. &check_openvc;
  530. }
  531. }
  532. }
  533. if (@{ $form->{all_language} }) {
  534. $form->{selectlanguage} = "<option>\n";
  535. for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|<option value="$_->{code}">$_->{description}\n| }
  536. }
  537. }
  538. if ($new_name_selected || $form->{redo}) {
  539. CP->get_openinvoices(\%myconfig, \%$form);
  540. ($newvc) = split /--/, $form->{$form->{vc}};
  541. $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;;
  542. $form->{redo} = 1;
  543. }
  544. if ($form->{currency} ne $form->{oldcurrency}) {
  545. $form->{oldcurrency} = $form->{currency};
  546. if (!$form->{redo}) {
  547. CP->get_openinvoices(\%myconfig, \%$form);
  548. $form->{redo} = 1;
  549. }
  550. }
  551. $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{datepaid}, $buysell)));
  552. if ($form->{redo}) {
  553. $form->{rowcount} = 0;
  554. $i = 0;
  555. foreach $ref (@{ $form->{PR} }) {
  556. $i++;
  557. $form->{"id_$i"} = $ref->{id};
  558. $form->{"invnumber_$i"} = $ref->{invnumber};
  559. $form->{"transdate_$i"} = $ref->{transdate};
  560. $ref->{exchangerate} = 1 unless $ref->{exchangerate};
  561. $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
  562. $form->{"due_$i"} = ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
  563. $form->{"checked_$i"} = "";
  564. $form->{"paid_$i"} = "";
  565. # need to format
  566. for (qw(amount due)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) }
  567. }
  568. $form->{rowcount} = $i;
  569. }
  570. $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
  571. # recalculate
  572. $amount = 0;
  573. for $i (1 .. $form->{rowcount}) {
  574. for (qw(amount due paid)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  575. if ($form->{"checked_$i"}) {
  576. # calculate paid_$i
  577. if (!$form->{"paid_$i"}) {
  578. $form->{"paid_$i"} = $form->{"due_$i"};
  579. }
  580. $amount += $form->{"paid_$i"};
  581. $form->{redo} = 1;
  582. } else {
  583. $form->{"paid_$i"} = "";
  584. }
  585. for (qw(amount due paid)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) }
  586. }
  587. $form->{amount} += ($amount - $form->{oldamount}) if $form->{redo};
  588. &payment_header;
  589. &list_invoices;
  590. &payment_footer;
  591. }
  592. sub payment_header {
  593. $vclabel = ucfirst $form->{vc};
  594. $vclabel = $locale->text($vclabel);
  595. if ($form->{type} eq 'receipt') {
  596. $form->{title} = $locale->text('Receipt');
  597. }
  598. if ($form->{type} eq 'check') {
  599. $form->{title} = $locale->text('Payment');
  600. }
  601. # $locale->text('Customer')
  602. # $locale->text('Vendor')
  603. if ($form->{$form->{vc}} eq "") {
  604. for (qw(address1 address2 city zipcode state country)) { $form->{$_} = "" }
  605. }
  606. for ("$form->{vc}", "department") {
  607. $form->{"select$_"} = $form->unescape($form->{"select$_"});
  608. $form->{"select$_"} =~ s/ selected//;
  609. $form->{"select$_"} =~ s/(<option value="\Q$form->{$_}\E")/$1 selected/;
  610. }
  611. for ("account", "currency", "$form->{ARAP}") {
  612. $form->{"select$_"} =~ s/ selected//;
  613. $form->{"select$_"} =~ s/option>\Q$form->{$_}\E/option selected>$form->{$_}/;
  614. }
  615. if ($form->{defaultcurrency}) {
  616. $exchangerate = qq|
  617. <tr>
  618. <th align=right nowrap>|.$locale->text('Currency').qq|</th>
  619. <td><select name=currency>$form->{selectcurrency}</select></td>
  620. <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
  621. <input type=hidden name=oldcurrency value=$form->{oldcurrency}>
  622. </tr>
  623. |;
  624. }
  625. if ($form->{currency} ne $form->{defaultcurrency}) {
  626. $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
  627. if ($form->{forex}) {
  628. $exchangerate .= qq|
  629. <tr>
  630. <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
  631. <td colspan=3><input type=hidden name=exchangerate size=10 value=$form->{exchangerate}>$form->{exchangerate}</td>
  632. </tr>
  633. |;
  634. } else {
  635. $exchangerate .= qq|
  636. <tr>
  637. <th align=right nowrap>|.$locale->text('Exchange Rate').qq|</th>
  638. <td colspan=3><input name=exchangerate size=10 value=$form->{exchangerate}></td>
  639. </tr>
  640. |;
  641. }
  642. }
  643. $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}}">|;
  644. if ($form->{all_vc}) {
  645. $allvc = "checked";
  646. } else {
  647. $allvc = "";
  648. }
  649. # $locale->text('AR')
  650. # $locale->text('AP')
  651. $department = qq|
  652. <tr>
  653. <th align="right" nowrap>|.$locale->text('Department').qq|</th>
  654. <td><select name=department>$form->{selectdepartment}</select>
  655. <input type=hidden name=selectdepartment value="|.$form->escape($form->{selectdepartment},1).qq|">
  656. </td>
  657. </tr>
  658. | if $form->{selectdepartment};
  659. $form->header;
  660. print qq|
  661. <body>
  662. <form method=post action=$form->{script}>
  663. |;
  664. $form->hide_form(qw(defaultcurrency closedto vc type ARAP arap title formname payment olddepartment));
  665. print qq|
  666. <table width=100%>
  667. <tr>
  668. <th class=listtop>$form->{title}</th>
  669. </tr>
  670. <tr height="5"></tr>
  671. <tr>
  672. <td>
  673. <table width=100%>
  674. <tr valign=top>
  675. <td>
  676. <table>
  677. <tr>
  678. <td align=right>
  679. <input name=all_vc type=checkbox class=checkbox value=Y $allvc>
  680. <input type=hidden name="oldall_vc" value="$form->{all_vc}"></td>
  681. <th align=left>|.$locale->text('All').qq|</th>
  682. </tr>
  683. <tr>
  684. <th align=right>$vclabel</th>
  685. <td>$vc</td>
  686. <input type=hidden name="select$form->{vc}" value="|.$form->escape($form->{"select$form->{vc}"},1).qq|">
  687. <input type=hidden name="$form->{vc}_id" value=$form->{"$form->{vc}_id"}>
  688. <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
  689. </tr>
  690. <tr valign=top>
  691. <th align=right nowrap>|.$locale->text('Address').qq|</th>
  692. <td colspan=2>
  693. <table>
  694. <tr>
  695. <td>$form->{address1}</td>
  696. </tr>
  697. <tr>
  698. <td>$form->{address2}</td>
  699. </tr>
  700. <td>$form->{city}</td>
  701. </tr>
  702. </tr>
  703. <td>$form->{state}</td>
  704. </tr>
  705. </tr>
  706. <td>$form->{zipcode}</td>
  707. </tr>
  708. <tr>
  709. <td>$form->{country}</td>
  710. </tr>
  711. </table>
  712. </td>
  713. <input type=hidden name=address1 value="$form->{address1}">
  714. <input type=hidden name=address2 value="$form->{address2}">
  715. <input type=hidden name=city value="$form->{city}">
  716. <input type=hidden name=state value="$form->{state}">
  717. <input type=hidden name=zipcode value="$form->{zipcode}">
  718. <input type=hidden name=country value="$form->{country}">
  719. </tr>
  720. <tr>
  721. <th align=right>|.$locale->text('Memo').qq|</th>
  722. <td colspan=2><input name="memo" size=30 value="$form->{memo}"></td>
  723. </tr>
  724. </table>
  725. </td>
  726. <td align=right>
  727. <table>
  728. $department
  729. <tr>
  730. <th align=right nowrap>|.$locale->text($form->{ARAP}).qq|</th>
  731. <td colspan=3><select name=$form->{ARAP}>$form->{"select$form->{ARAP}"}</select>
  732. </td>
  733. <input type=hidden name="select$form->{ARAP}" value="$form->{"select$form->{ARAP}"}">
  734. </tr>
  735. <tr>
  736. <th align=right nowrap>|.$locale->text('Account').qq|</th>
  737. <td colspan=3><select name=account>$form->{selectaccount}</select>
  738. <input type=hidden name=selectaccount value="$form->{selectaccount}">
  739. </td>
  740. </tr>
  741. <tr>
  742. <th align=right nowrap>|.$locale->text('Date').qq|</th>
  743. <td><input name=datepaid value="$form->{datepaid}" title="$myconfig{dateformat}" size=11></td>
  744. <input type=hidden name=olddatepaid value=$form->{olddatepaid}>
  745. </tr>
  746. $exchangerate
  747. <tr>
  748. <th align=right nowrap>|.$locale->text('Source').qq|</th>
  749. <td colspan=3><input name=source value="$form->{source}" size=10></td>
  750. </tr>
  751. <tr>
  752. <th align=right nowrap>|.$locale->text('Amount').qq|</th>
  753. <td colspan=3><input name=amount size=10 value=|.$form->format_amount(\%myconfig, $form->{amount}, 2).qq|></td>
  754. <input type=hidden name=oldamount value=$form->{amount}>
  755. </tr>
  756. </table>
  757. </td>
  758. </tr>
  759. </table>
  760. </td>
  761. </tr>
  762. |;
  763. }
  764. sub list_invoices {
  765. @column_index = qw(invnumber transdate amount due checked paid);
  766. $colspan = $#column_index + 1;
  767. $invoice = $locale->text('Invoices');
  768. print qq|
  769. <input type=hidden name=column_index value="id @column_index">
  770. <tr>
  771. <td>
  772. <table width=100%>
  773. <tr>
  774. <th class=listheading colspan=$colspan>$invoice</th>
  775. </tr>
  776. |;
  777. $column_data{invnumber} = qq|<th nowrap>|.$locale->text('Invoice')."</th>";
  778. $column_data{transdate} = qq|<th nowrap>|.$locale->text('Date')."</th>";
  779. $column_data{amount} = qq|<th nowrap>|.$locale->text('Amount')."</th>";
  780. $column_data{due} = qq|<th nowrap>|.$locale->text('Amount Due')."</th>";
  781. $column_data{paid} = qq|<th nowrap>|.$locale->text('Amount')."</th>";
  782. $column_data{checked} = qq|<th nowrap>|.$locale->text('Select')."</th>";
  783. print qq|
  784. <tr>
  785. |;
  786. for (@column_index) { print "$column_data{$_}\n" }
  787. print qq|
  788. </tr>
  789. |;
  790. for $i (1 .. $form->{rowcount}) {
  791. for (qw(amount due paid)) { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
  792. $totalamount += $form->{"amount_$i"};
  793. $totaldue += $form->{"due_$i"};
  794. if ($form->{"paid_$i"} =~ /NaN/){
  795. $form->{"paid_$i"} = '';
  796. } else {
  797. $totalpaid += $form->{"paid_$i"};
  798. }
  799. for (qw(amount due paid)) { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) }
  800. $column_data{invnumber} = qq|<td>$form->{"invnumber_$i"}</td>
  801. <input type=hidden name="invnumber_$i" value="$form->{"invnumber_$i"}">
  802. <input type=hidden name="id_$i" value=$form->{"id_$i"}>|;
  803. $column_data{transdate} = qq|<td width=15%>$form->{"transdate_$i"}</td>
  804. <input type=hidden name="transdate_$i" value=$form->{"transdate_$i"}>|;
  805. $column_data{amount} = qq|<td align=right width=15%>$form->{"amount_$i"}</td>
  806. <input type=hidden name="amount_$i" value=$form->{"amount_$i"}>|;
  807. $column_data{due} = qq|<td align=right width=15%>$form->{"due_$i"}</td>
  808. <input type=hidden name="due_$i" value=$form->{"due_$i"}>|;
  809. $column_data{paid} = qq|<td align=right width=15%><input name="paid_$i" size=10 value=$form->{"paid_$i"}></td>|;
  810. $form->{"checked_$i"} = ($form->{"checked_$i"}) ? "checked" : "";
  811. $column_data{checked} = qq|<td align=center width=10%><input name="checked_$i" type=checkbox class=checkbox $form->{"checked_$i"}></td>|;
  812. $j++; $j %= 2;
  813. print qq|
  814. <tr class=listrow$j>
  815. |;
  816. for (@column_index) { print "$column_data{$_}\n" }
  817. print qq|
  818. </tr>
  819. |;
  820. }
  821. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  822. $column_data{amount} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;").qq|</th>|;
  823. $column_data{due} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totaldue, 2, "&nbsp;").qq|</th>|;
  824. $column_data{paid} = qq|<th class=listtotal align=right>|.$form->format_amount(\%myconfig, $totalpaid, 2, "&nbsp;").qq|</th>|;
  825. print qq|
  826. <tr class=listtotal>
  827. |;
  828. for (@column_index) { print "$column_data{$_}\n" }
  829. print qq|
  830. </tr>
  831. </table>
  832. </td>
  833. </tr>
  834. |;
  835. }
  836. sub payment_footer {
  837. $form->{DF}{$form->{format}} = "selected";
  838. $transdate = $form->datetonum(\%myconfig, $form->{datepaid});
  839. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  840. if (${LedgerSMB::Sysconfig::latex}) {
  841. if ($form->{selectlanguage}) {
  842. $form->{"selectlanguage"} = $form->unescape($form->{"selectlanguage"});
  843. $form->{"selectlanguage"} =~ s/ selected//;
  844. $form->{"selectlanguage"} =~ s/(<option value="\Q$form->{language_code}\E")/$1 selected/;
  845. $lang = qq|<select name=language_code>$form->{selectlanguage}</select>
  846. <input type=hidden name=selectlanguage value="|.
  847. $form->escape($form->{selectlanguage},1).qq|">|;
  848. }
  849. $media = qq|<select name=media>
  850. <option value=screen>|.$locale->text('Screen');
  851. if (%{LedgerSMB::Sysconfig::printer}) {
  852. for (sort keys %{LedgerSMB::Sysconfig::printer}) { $media .= qq|
  853. <option value="$_">$_| }
  854. }
  855. $media .= qq|</select>|;
  856. $format = qq|<select name=format>
  857. <option value=postscript $form->{DF}{postscript}>|.$locale->text('Postscript').qq|
  858. <option value=pdf $form->{DF}{pdf}>|.$locale->text('PDF').qq|</select>|;
  859. }
  860. print qq|
  861. <tr>
  862. <td><hr size=3 noshade></td>
  863. </tr>
  864. </table>
  865. |;
  866. %button = ('update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
  867. 'select_all' => { ndx => 2, key => 'A', value => $locale->text('Select all') },
  868. 'print' => { ndx => 3, key => 'P', value => $locale->text('Print') },
  869. 'post' => { ndx => 4, key => 'O', value => $locale->text('Post') },
  870. );
  871. if (! ${LedgerSMB::Sysconfig::latex}) {
  872. delete $button{'print'};
  873. }
  874. if ($transdate <= $closedto) {
  875. for ('post', 'print') { delete $button{$_} }
  876. $media = $format = "";
  877. }
  878. for (sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button) { $form->print_button(\%button, $_) }
  879. $media =~ s/(<option value="\Q$form->{media}\E")/$1 selected/;
  880. print qq|
  881. $lang
  882. $format
  883. $media
  884. |;
  885. $form->hide_form(qw(callback rowcount path login sessionid));
  886. if ($form->{lynx}) {
  887. require "bin/menu.pl";
  888. &menubar;
  889. }
  890. print qq|
  891. </form>
  892. </body>
  893. </html>
  894. |;
  895. }
  896. sub post { &{"post_$form->{payment}"} }
  897. sub post_payments {
  898. if ($form->{currency} ne $form->{defaultcurrency}) {
  899. $form->error($locale->text('Exchange rate missing!')) unless $form->{exchangerate};
  900. }
  901. if (CP->post_payments(\%myconfig, \%$form)) {
  902. $form->redirect($locale->text('Payments posted!'));
  903. } else {
  904. $form->error($locale->text('Posting failed!'));
  905. }
  906. }
  907. sub post_payment {
  908. &check_form;
  909. if ($form->{currency} ne $form->{defaultcurrency}) {
  910. $form->error($locale->text('Exchange rate missing!')) unless $form->{exchangerate};
  911. }
  912. $msg1 = "$form->{title} posted!";
  913. $msg2 = "Cannot post $form->{title}!";
  914. # $locale->text('Payment posted!')
  915. # $locale->text('Receipt posted!')
  916. # $locale->text('Cannot post Payment!')
  917. # $locale->text('Cannot post Receipt!')
  918. $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
  919. $source = $form->{source};
  920. $source =~ s/(\d+)/$1 + 1/e;
  921. if ($form->{callback}) {
  922. $form->{callback} .= "&source=$source";
  923. }
  924. if (CP->post_payment(\%myconfig, \%$form)) {
  925. $form->redirect($locale->text($msg1));
  926. } else {
  927. $form->error($locale->text($msg2));
  928. }
  929. }
  930. sub print {
  931. &{ "print_$form->{payment}" };
  932. &update if $form->{media} ne 'screen';
  933. }
  934. sub print_payments {
  935. $form->error($locale->text('Select postscript or PDF!')) if ($form->{format} !~ /(postscript|pdf)/);
  936. $SIG{INT} = 'IGNORE';
  937. for (qw(company address)) { $form->{$_} = $myconfig{$_} }
  938. $form->{address} =~ s/\\n/\n/g;
  939. %oldform = ();
  940. for (keys %$form) { $oldform{$_} = $form->{$_} };
  941. @a = qw(name company address text_amount text_decimal address1 address2 city state zipcode country memo);
  942. for (@a) { $temp{$_} = $form->{$_} }
  943. $form->format_string(@a);
  944. $ok = 0;
  945. $j = 0;
  946. $temp{rowcount} = $form->{rowcount};
  947. for $i (1 .. $temp{rowcount}) {
  948. if ($form->{"$form->{vc}_id_$i"} ne $form->{"$form->{vc}_id"}) {
  949. $form->{rowcount} = $j;
  950. for (1 .. $j) { $form->{"id_$_"} = $temp{"id_$_"} }
  951. &print_form if $ok;
  952. $ok = 0;
  953. $j = 0;
  954. $form->{amount} = 0;
  955. for (qw(invnumber invdate due paid)) { @{ $form->{$_} } = () }
  956. for (qw(language_code source memo)) { $form->{$_} = $form->{"${_}_$i"} }
  957. }
  958. if ($form->{"checked_$i"}) {
  959. $j++;
  960. $ok = 1;
  961. $temp{"id_$j"} = $form->{"id_$i"};
  962. $form->{"invdate_$i"} = $form->{"transdate_$i"};
  963. for (qw(invnumber invdate due paid)) { push @{ $form->{$_} }, $form->{"${_}_$i"} }
  964. $form->{amount} += $form->parse_amount(\%myconfig, $form->{"paid_$i"});
  965. $form->{"$form->{vc}_id"} = $form->{"$form->{vc}_id_$i"};
  966. }
  967. }
  968. $form->{rowcount} = $j;
  969. for (1 .. $j) { $form->{"id_$_"} = $temp{"id_$_"} }
  970. &print_form if $ok;
  971. for (keys %oldform) { $form->{$_} = $oldform{$_} }
  972. }
  973. sub print_form {
  974. $c = CP->new(($form->{language_code}) ? $form->{language_code} : $myconfig{countrycode});
  975. $c->init;
  976. ($whole, $form->{decimal}) = split /\./, $form->{amount};
  977. $form->{amount} = $form->format_amount(\%myconfig, $form->{amount}, 2);
  978. $form->{decimal} .= "00";
  979. $form->{decimal} = substr($form->{decimal}, 0, 2);
  980. $form->{text_decimal} = $c->num2text($form->{decimal} * 1);
  981. $form->{text_amount} = $c->num2text($whole);
  982. $form->{integer_amount} = $form->format_amount($myconfig, $whole);
  983. $datepaid = $form->datetonum(\%myconfig, $form->{datepaid});
  984. ($form->{yyyy}, $form->{mm}, $form->{dd}) = $datepaid =~ /(....)(..)(..)/;
  985. &{ "$form->{vc}_details" };
  986. $form->{templates} = "$myconfig{templates}";
  987. $form->{IN} = "$form->{formname}.tex";
  988. if ($form->{media} ne 'screen') {
  989. $form->{OUT} = "| ${LedgerSMB::Sysconfig::printer}{$form->{media}}";
  990. }
  991. $form->parse_template(\%myconfig, ${LedgerSMB::Sysconfig::userspath});
  992. }
  993. sub print_payment {
  994. &check_form;
  995. for (qw(company address)) { $form->{$_} = $myconfig{$_} }
  996. $form->{address} =~ s/\\n/\n/g;
  997. @a = qw(rowcount name company address text_amount text_decimal address1 address2 city state zipcode country memo);
  998. %temp = ();
  999. for (@a) { $temp{$_} = $form->{$_} }
  1000. if (scalar @{$form->{invnumber}} > ${LedgerSMB::Sysconfig::check_max_invoices}) {
  1001. $#{$form->{invnumber}} = ${LedgerSMB::Sysconfig::check_max_invoices} - 1;
  1002. $form->{invnumbers_maxed} = 1;
  1003. $form->{message} = $locale->text("Please see attatched report for list of invoices paid.");
  1004. }
  1005. $form->format_string(@a);
  1006. &print_form;
  1007. for (keys %temp) { $form->{$_} = $temp{$_} }
  1008. }
  1009. sub customer_details { IS->customer_details(\%myconfig, \%$form) };
  1010. sub vendor_details { IR->vendor_details(\%myconfig, \%$form) };
  1011. sub check_form {
  1012. &check_openvc;
  1013. if ($form->{currency} ne $form->{oldcurrency}) {
  1014. &update;
  1015. exit;
  1016. }
  1017. $form->error($locale->text('Date missing!')) unless $form->{datepaid};
  1018. $closedto = $form->datetonum(\%myconfig, $form->{closedto});
  1019. $datepaid = $form->datetonum(\%myconfig, $form->{datepaid});
  1020. $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
  1021. # this is just to format the year
  1022. $form->{datepaid} = $locale->date(\%myconfig, $form->{datepaid});
  1023. $amount = $form->parse_amount(\%myconfig, $form->{amount});
  1024. $form->{amount} = $amount;
  1025. for $i (1 .. $form->{rowcount}) {
  1026. if ($form->{"paid_$i"}) {
  1027. $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
  1028. push(@{ $form->{paid} }, $form->{"paid_$i"});
  1029. push(@{ $form->{due} }, $form->{"due_$i"});
  1030. push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
  1031. push(@{ $form->{invdate} }, $form->{"transdate_$i"});
  1032. }
  1033. }
  1034. if ($form->round_amount($amount, 2) != 0) {
  1035. push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
  1036. push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
  1037. push(@{ $form->{invnumber} }, ($form->{ARAP} eq 'AR') ? $locale->text('Deposit') : $locale->text('Prepayment'));
  1038. push(@{ $form->{invdate} }, $form->{datepaid});
  1039. }
  1040. }
  1041. sub check_openvc {
  1042. $name = $form->{vc};
  1043. ($new_name, $new_id) = split /--/, $form->{$name};
  1044. if ($form->{all_vc}) {
  1045. if ($form->{"select$name"}) {
  1046. $ok = ($form->{"old$name"} ne $form->{$name});
  1047. } else {
  1048. $ok = ($form->{"old$name"} ne qq|$form->{$name}--$form->{"${name}_id"}|);
  1049. }
  1050. if ($ok) {
  1051. $form->{redo} = 1;
  1052. if ($form->{"select$name"}) {
  1053. $form->{"${name}_id"} = $new_id;
  1054. AA->get_name(\%myconfig, \%$form);
  1055. $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
  1056. } else {
  1057. &check_name($form->{vc});
  1058. }
  1059. }
  1060. } else {
  1061. # if we use a selection
  1062. if ($form->{"select$name"}) {
  1063. if ($form->{"old$name"} ne $form->{$name}) {
  1064. $form->{"${name}_id"} = $new_id;
  1065. AA->get_name(\%myconfig, \%$form);
  1066. $form->{$name} = $form->{"old$name"} = "$new_name--$new_id";
  1067. $form->{redo} = 1;
  1068. }
  1069. } else {
  1070. # check name, combine name and id
  1071. if ($form->{"old$name"} ne qq|$form->{$name}--$form->{"${name}_id"}|) {
  1072. # return one name or a list of names in $form->{name_list}
  1073. if (($rv = CP->get_openvc(\%myconfig, \%$form)) > 1) {
  1074. $form->{redo} = 1;
  1075. &select_name($name);
  1076. exit;
  1077. }
  1078. if ($rv == 1) {
  1079. # we got one name
  1080. $form->{"${name}_id"} = $form->{name_list}[0]->{id};
  1081. $form->{$name} = $form->{name_list}[0]->{name};
  1082. $form->{"old$name"} = qq|$form->{$name}--$form->{"${name}_id"}|;
  1083. AA->get_name(\%myconfig, \%$form);
  1084. } else {
  1085. # nothing open
  1086. $form->error($locale->text('Nothing open!'));
  1087. }
  1088. $form->{redo} = 1;
  1089. }
  1090. }
  1091. }
  1092. }