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