summaryrefslogtreecommitdiff
path: root/bin/rc.pl
blob: 618ff2dcba8f5a8e4619a10a3143cfe040021cbb (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) 2003
  17. #
  18. # Author: DWS Systems Inc.
  19. # Web: http://www.sql-ledger.org
  20. #
  21. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation; either version 2 of the License, or
  25. # (at your option) any later version.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. # You should have received a copy of the GNU General Public License
  32. # along with this program; if not, write to the Free Software
  33. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. #======================================================================
  35. #
  36. # Account reconciliation module
  37. #
  38. #======================================================================
  39. use LedgerSMB::RC;
  40. 1;
  41. # end of main
  42. # this is for our long dates
  43. # $locale->text('January')
  44. # $locale->text('February')
  45. # $locale->text('March')
  46. # $locale->text('April')
  47. # $locale->text('May ')
  48. # $locale->text('June')
  49. # $locale->text('July')
  50. # $locale->text('August')
  51. # $locale->text('September')
  52. # $locale->text('October')
  53. # $locale->text('November')
  54. # $locale->text('December')
  55. # this is for our short month
  56. # $locale->text('Jan')
  57. # $locale->text('Feb')
  58. # $locale->text('Mar')
  59. # $locale->text('Apr')
  60. # $locale->text('May')
  61. # $locale->text('Jun')
  62. # $locale->text('Jul')
  63. # $locale->text('Aug')
  64. # $locale->text('Sep')
  65. # $locale->text('Oct')
  66. # $locale->text('Nov')
  67. # $locale->text('Dec')
  68. sub reconciliation {
  69. RC->paymentaccounts( \%myconfig, \%$form );
  70. $selection = "";
  71. for ( @{ $form->{PR} } ) {
  72. $selection .= "<option>$_->{accno}--$_->{description}\n";
  73. }
  74. $form->{title} = $locale->text('Reconciliation');
  75. if ( $form->{report} ) {
  76. $form->{title} = $locale->text('Reconciliation Report');
  77. $cleared = qq|
  78. <input type=hidden name=report value=1>
  79. <tr>
  80. <td align=right><input type=checkbox class=checkbox name=outstanding value=1 checked></td>
  81. <td>| . $locale->text('Outstanding') . qq|</td>
  82. <td align=right><input type=checkbox class=checkbox name=cleared value=1></td>
  83. <td>| . $locale->text('Cleared') . qq|</td>
  84. </tr>
  85. |;
  86. }
  87. if ( @{ $form->{all_years} } ) {
  88. # accounting years
  89. $form->{selectaccountingyear} = "<option>\n";
  90. for ( @{ $form->{all_years} } ) {
  91. $form->{selectaccountingyear} .= qq|<option>$_\n|;
  92. }
  93. $form->{selectaccountingmonth} = "<option>\n";
  94. for ( sort keys %{ $form->{all_month} } ) {
  95. $form->{selectaccountingmonth} .=
  96. qq|<option value=$_>|
  97. . $locale->text( $form->{all_month}{$_} ) . qq|\n|;
  98. }
  99. $selectfrom = qq|
  100. <tr>
  101. <th align=right>| . $locale->text('Period') . qq|</th>
  102. <td colspan=3>
  103. <select name=month>$form->{selectaccountingmonth}</select>
  104. <select name=year>$form->{selectaccountingyear}</select>
  105. <input name=interval class=radio type=radio value=0 checked>&nbsp;|
  106. . $locale->text('Current') . qq|
  107. <input name=interval class=radio type=radio value=1>&nbsp;|
  108. . $locale->text('Month') . qq|
  109. <input name=interval class=radio type=radio value=3>&nbsp;|
  110. . $locale->text('Quarter') . qq|
  111. <input name=interval class=radio type=radio value=12>&nbsp;|
  112. . $locale->text('Year') . qq|
  113. </td>
  114. </tr>
  115. |;
  116. }
  117. $form->header;
  118. print qq|
  119. <body>
  120. <form method=post action=$form->{script}>
  121. <table width=100%>
  122. <tr>
  123. <th class=listtop>$form->{title}</th>
  124. </tr>
  125. <tr height="5"></tr>
  126. <tr>
  127. <td>
  128. <table>
  129. <tr>
  130. <th align=right nowrap>| . $locale->text('Account') . qq|</th>
  131. <td colspan=3><select name=accno>$selection</select></td>
  132. </tr>
  133. <tr>
  134. <th align=right>| . $locale->text('From') . qq|</th>
  135. <td colspan=3><input class="date" name=fromdate size=11 title="$myconfig{dateformat}"> <b>|
  136. . $locale->text('To')
  137. . qq|</b> <input class="date" name=todate size=11 title="$myconfig{dateformat}"></td>
  138. </tr>
  139. $selectfrom
  140. $cleared
  141. <tr>
  142. <td></td>
  143. <td colspan=3><input type=radio style=radio name=summary value=1 checked> |
  144. . $locale->text('Summary') . qq|
  145. <input type=radio style=radio name=summary value=0> |
  146. . $locale->text('Detail')
  147. . qq|</td>
  148. </tr>
  149. <tr>
  150. <td></td>
  151. <td colspan=3><input type=checkbox class=checkbox name=fx_transaction value=1 checked> |
  152. . $locale->text('Include Exchange Rate Difference')
  153. . qq|</td>
  154. </tr>
  155. </table>
  156. </td>
  157. </tr>
  158. <tr>
  159. <td><hr size=3 noshade></td>
  160. </tr>
  161. </table>
  162. <br>
  163. <input type=hidden name=nextsub value=get_payments>
  164. |;
  165. $form->hide_form(qw(path login sessionid));
  166. print qq|
  167. <button type="submit" class="submit" name="action" value="continue">|
  168. . $locale->text('Continue')
  169. . qq|</button>
  170. </form>
  171. |;
  172. if ( $form->{lynx} ) {
  173. require "bin/menu.pl";
  174. &menubar;
  175. }
  176. print qq|
  177. </body>
  178. </html>
  179. |;
  180. }
  181. sub continue { &{ $form->{nextsub} } }
  182. sub till_closing {
  183. my %hiddens;
  184. $form->{callback} =
  185. "$form->{script}?path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  186. # $locale->text("Source");
  187. # $locale->text("Actual");
  188. # $locale->text("Expected");
  189. # $locale->text("Error");
  190. @colheadings = qw(Source Actual Expected Error);
  191. my $curren = $pos_config{'curren'};
  192. $form->{title} = $locale->text( "Closing Till For [_1]", $form->{login} );
  193. require "pos.conf.pl";
  194. RC->getposlines( \%myconfig, \%$form );
  195. $hiddens{path} = $form->{path};
  196. $hiddens{login} = $form->{login};
  197. $hiddens{sessionid} = $form->{sessionid};
  198. $hiddens{callback} = $form->{callback};
  199. $hiddens{sum} = "$form->{sum}" * -1;
  200. my $j;
  201. my $source;
  202. my @sources;
  203. foreach $source ( sort keys %pos_sources ) {
  204. $amount = 0;
  205. foreach $ref ( @{ $form->{TB} } ) {
  206. if ( $ref->{memo} eq $source ) {
  207. $amount = $ref->{amount} * -1;
  208. last;
  209. }
  210. }
  211. ++$j;
  212. $j = $j % 2;
  213. push @sources, {i => $j,
  214. label => $pos_sources{$source},
  215. source => $source,
  216. currenamount => "${curren}${amount}",
  217. };
  218. $hiddens{"expected_$source"} = "$amount";
  219. }
  220. my @units;
  221. foreach my $unit ( @{ $pos_config{'breakdown'} } ) {
  222. # XXX Needs to take into account currencies that don't use 2 dp
  223. my $calcval = $form->parse_amount( \%pos_config, $unit );
  224. $calcval = sprintf( '%03d', $calcval * 100 ) if $calcval < 1;
  225. my $subval = 'sub_' . $calcval;
  226. my $unit_name = $calcval;
  227. $calcval = 'calc_' . $calcval;
  228. push @units, {
  229. unit => $unit,
  230. unit_name => "$unit_name",
  231. currenunit => "${curren}${unit}",
  232. quantity => {name => $calcval, value => $form->{$calcval}},
  233. value => {name => $subval, value => $form->{$subval}},
  234. };
  235. }
  236. my @buttons = ({
  237. name => 'calculate',
  238. value => 'Calculate',
  239. type => 'button',
  240. text => $locale->text('Calculate'),
  241. attributes => {onclick => 'custom_calc_total()'},
  242. }, {
  243. name => 'action',
  244. value => 'close_till',
  245. text => $locale->text('Close Till'),
  246. });
  247. my $template = LedgerSMB::Template->new_UI(
  248. user => \%myconfig,
  249. locale => $locale,
  250. template => 'rc-till-closing',
  251. );
  252. $template->render({
  253. form => $form,
  254. user => \%myconfig,
  255. 'pos' => \%pos_config,
  256. hiddens => \%hiddens,
  257. columns => \@colheadings,
  258. sources => \@sources,
  259. units => \@units,
  260. buttons => \@buttons,
  261. });
  262. }
  263. sub close_till {
  264. use LedgerSMB::GL;
  265. require 'pos.conf.pl';
  266. RC->clear_till( \%myconfig, \%$form );
  267. my $amount = 0;
  268. my $expected = 0;
  269. my $difference = 0;
  270. my $lines = '';
  271. $form->{rowcount} = 2;
  272. foreach $key ( keys %pos_sources ) {
  273. $amount = 0;
  274. $expected = 0;
  275. $amount = $form->parse_amount( \%myconfig, $form->{"amount_$key"} );
  276. $expected = $form->parse_amount( \%myconfig, $form->{"expected_$key"} );
  277. $gl_entry = "Closing Till $pos_config{till} source = $key";
  278. $accno1 = $pos_config{till_accno};
  279. if ( ${ $pos_config{'source_accno_override'}{$key} } ) {
  280. $accno2 = ${ $pos_config{'source_accno_override'}{$key} };
  281. }
  282. else {
  283. $accno2 = $pos_config{'close_cash_accno'};
  284. }
  285. $form->{reference} = $gl_entry;
  286. $form->{accno_1} = $accno1;
  287. $form->{credit_1} = $amount;
  288. $form->{accno_2} = $accno2;
  289. $form->{debit_2} = $amount;
  290. $form->{transdate} = $form->current_date( \%myconfig );
  291. GL->post_transaction( \%myconfig, \%$form );
  292. delete $form->{id};
  293. $error = $amount - $expected;
  294. $difference += $error;
  295. $lines .=
  296. "Source: $key, Amount: $amount\nExpected: $expected. Error= $error\n\n";
  297. }
  298. $gl_entry = "Closing Till: $pos_config{till} Over/Under";
  299. $amount = $difference * -1;
  300. $form->{reference} = $gl_entry;
  301. $form->{accno_1} = $accno1;
  302. $form->{credit_1} = $amount;
  303. $form->{accno_2} = $pos_config{coa_prefix};
  304. $form->{debit_2} = $amount;
  305. $form->{transdate} = $form->current_date( \%myconfig );
  306. GL->post_transaction( \%myconfig, \%$form );
  307. delete $form->{id};
  308. $lines .= "Cumulative Error: $amount";
  309. $form->{accno} = $form->{accno_1};
  310. RC->getbalance( \%myconfig, \%$form );
  311. $amount = $form->{balance} * -1;
  312. $gl_entry = "Resetting Till: $pos_config{till}";
  313. $form->{reference} = $gl_entry;
  314. $form->{accno_1} = $accno1;
  315. $form->{credit_1} = $amount;
  316. $form->{accno_2} = $pos_config{coa_prefix};
  317. $form->{debit_2} = $amount;
  318. $form->{transdate} = $form->current_date( \%myconfig );
  319. GL->post_transaction( \%myconfig, \%$form );
  320. delete $form->{id};
  321. $head =
  322. "Closing Till $pos_config{till} for $form->{login}\n"
  323. . "Date: $form->{transdate}\n\n\n";
  324. my @cashlines = [ $locale->text("Cash Breakdown:") ];
  325. foreach my $unit ( @{ $pos_config{'breakdown'} } ) {
  326. # XXX Needs to take into account currencies that don't use 2 dp
  327. my $parsed = $form->parse_amount( \%pos_config, $unit );
  328. my $calcval = $parsed;
  329. $calcval = sprintf( '%03d', $calcval * 100 ) if $calcval < 1;
  330. my $subval = 'sub_' . $calcval;
  331. $calcval = 'calc_' . $calcval;
  332. push @cashlines, "$form->{$calcval} x $parseval = $form->{$subval}";
  333. }
  334. push @cashlines,
  335. $locale->text( "Total Cash in Drawer: [_1]", $form->{sub_sub} );
  336. push @cashlines,
  337. $locale->text( "Less Cash in Till At Start: [_1]", $form->{till_cash} );
  338. push @cashlines, "\n";
  339. $cash = join( "\n", @cashlines );
  340. $foot = $locale->text( "Cumulative Error: [_1]", $difference ) . "\n";
  341. $foot .=
  342. $locale->text( 'Reset Till By [_1]', $amount ) . "\n\n\n\n\n\n\n\n\n\n";
  343. open( PRN, "|-", ${LedgerSMB::Sysconfig::printer}{Printer} );
  344. print PRN $head;
  345. print PRN $lines;
  346. print PRN $cash;
  347. print PRN $cash;
  348. print PRN $foot;
  349. close PRN;
  350. if ( $difference > 0 ) {
  351. $message = $locale->text( "You are over by [_1]", $difference );
  352. }
  353. elsif ( $difference < 0 ) {
  354. $message = $locale->text( "You are under by [_1]", $difference * -1 );
  355. }
  356. else {
  357. $message =
  358. $local->text("Congratulations! Your till is exactly balanced.");
  359. }
  360. $form->info($message);
  361. }
  362. sub get_payments {
  363. ( $form->{accno}, $form->{account} ) = split /--/, $form->{accno};
  364. if ( $form->{'pos'} ) {
  365. require "pos.conf.pl";
  366. $form->{fromdate} = $form->current_date( \%myconfig );
  367. unless ( $form->{source} ) {
  368. $form->{source} = ( sort keys(%pos_sources) )[0];
  369. }
  370. if ( $form->{source} eq 'cash' ) {
  371. $form->{summary} = "true";
  372. }
  373. else {
  374. $form->{summary} = "";
  375. }
  376. $form->{accno} = $pos_config{'coa_prefix'} . "." . $pos_config{'till'};
  377. $form->{account} = $form->{source};
  378. }
  379. RC->payment_transactions( \%myconfig, \%$form );
  380. $ml = ( $form->{category} eq 'A' ) ? -1 : 1;
  381. $form->{statementbalance} = $form->{endingbalance} * $ml;
  382. if ( !$form->{fx_transaction} ) {
  383. $form->{statementbalance} =
  384. ( $form->{endingbalance} - $form->{fx_endingbalance} ) * $ml;
  385. }
  386. $form->{statementbalance} =
  387. $form->format_amount( \%myconfig, $form->{statementbalance}, 2, 0 );
  388. &display_form;
  389. }
  390. sub display_form {
  391. my %hiddens;
  392. my @buttons;
  393. my @column_index;
  394. if ( $form->{report} ) {
  395. @column_index = qw(transdate source name cleared debit credit);
  396. }
  397. else {
  398. @column_index = qw(transdate source name cleared debit credit balance);
  399. }
  400. my %column_header;
  401. $column_header{cleared} = $locale->text('R');
  402. $column_header{source} = $locale->text('Source');
  403. $column_header{name} = $locale->text('Description');
  404. $column_header{transdate} = $locale->text('Date');
  405. $column_header{debit} = $locale->text('Debit');
  406. $column_header{credit} = $locale->text('Credit');
  407. $column_header{balance} = $locale->text('Balance');
  408. my @options;
  409. if ( $form->{fromdate} ) {
  410. push @options,
  411. $locale->text('From [_1]',
  412. $locale->date( \%myconfig, $form->{fromdate}, 1 ));
  413. }
  414. if ( $form->{todate} ) {
  415. push @options,
  416. $locale->text('To [_1]',
  417. $locale->date( \%myconfig, $form->{todate}, 1 ));
  418. }
  419. $form->{title} = "$form->{accno}--$form->{account}";
  420. $hiddens{source} = $form->{source};
  421. $hiddens{cumulative_error} = $form->{cumulative_error};
  422. my $ml = ( $form->{category} eq 'A' ) ? -1 : 1;
  423. $form->{beginningbalance} *= $ml;
  424. $form->{fx_balance} *= $ml;
  425. if ( !$form->{fx_transaction} ) {
  426. $form->{beginningbalance} -= $form->{fx_balance};
  427. }
  428. my $balance = $form->{beginningbalance};
  429. my $i = 0;
  430. my $j = 0;
  431. my @rows;
  432. if ( !$form->{report} ) {
  433. my %column_data;
  434. for (qw(cleared transdate source debit credit)) {
  435. $column_data{$_} = ' ';
  436. }
  437. $column_data{name} = $locale->text('Beginning Balance');
  438. $column_data{balance} = $form->format_amount(\%myconfig, $balance, 2, 0);
  439. $column_data{i} = $j;
  440. push @rows, \%column_data;
  441. }
  442. foreach my $ref ( @{ $form->{PR} } ) {
  443. $i++;
  444. if ( !$form->{fx_transaction} ) {
  445. next if $ref->{fx_transaction};
  446. }
  447. my %column_data;
  448. my $checked = ( $ref->{cleared} ) ? "checked" : undef;
  449. my %temp = ();
  450. if ( !$ref->{fx_transaction} ) {
  451. for (qw(name source transdate)) { $temp{$_} = $ref->{$_} }
  452. }
  453. $column_data{name}{delimiter} = "|";
  454. for ( @{ $temp{name} } ) { $column_data{name}{text} .= "$_|" }
  455. $column_data{source} = $temp{source};
  456. $column_data{debit} = ' ';
  457. $column_data{credit} = ' ';
  458. $balance += $ref->{amount} * $ml;
  459. $hiddens{"id_$i"} = $ref->{id};
  460. if ( $ref->{amount} < 0 ) {
  461. $totaldebits += $ref->{amount} * -1;
  462. $column_data{debit} =
  463. $form->format_amount(\%myconfig, $ref->{amount} * -1, 2, ' ');
  464. } else {
  465. $totalcredits += $ref->{amount};
  466. $column_data{credit} =
  467. $form->format_amount(\%myconfig, $ref->{amount}, 2, ' ');
  468. }
  469. $column_data{balance} = $form->format_amount(\%myconfig, $balance, 2, 0);
  470. if ( $ref->{fx_transaction} ) {
  471. $column_data{cleared} =
  472. ($clearfx) ? '*': ' ';
  473. $cleared += $ref->{amount} * $ml if $clearfx;
  474. } else {
  475. if ( $form->{report} ) {
  476. if ( $ref->{cleared} ) {
  477. $column_data{cleared} = '*';
  478. $clearfx = 1;
  479. } else {
  480. $column_data{cleared} = ' ';
  481. $clearfx = 0;
  482. }
  483. } else {
  484. if ( $ref->{oldcleared} ) {
  485. $cleared += $ref->{amount} * $ml;
  486. $clearfx = 1;
  487. $hiddens{"cleared_$i"} = $ref->{cleared};
  488. $hiddens{"oldcleared_$i"} = $ref->{oldcleared};
  489. $hiddens{"source_$i"} = $ref->{source};
  490. $hiddens{"amount_$i"} = $ref->{amount};
  491. $column_data{cleared} = '*';
  492. } else {
  493. $cleared += $ref->{amount} * $ml if $checked;
  494. $clearfx = ($checked) ? 1 : 0;
  495. $hiddens{"source_$i"} = $ref->{source};
  496. $hiddens{"amount_$i"} = $ref->{amount};
  497. $column_data{cleared} = {input => {
  498. type => 'checkbox',
  499. value => 1,
  500. name => "cleared_$i",
  501. $checked => $checked,
  502. }};
  503. }
  504. }
  505. }
  506. $hiddens{"transdate_$i"} = $ref->{transdate};
  507. $column_data{transdate} = $temp{transdate};
  508. $j++;
  509. $j %= 2;
  510. $column_data{i} = $j;
  511. push @rows, \%column_data;
  512. }
  513. $form->{rowcount} = $i;
  514. # print totals
  515. my %column_data;
  516. for (@column_index) { $column_data{$_} = ' ' }
  517. $column_data{debit} =
  518. $form->format_amount( \%myconfig, $totaldebits, 2, ' ' );
  519. $column_data{credit} =
  520. $form->format_amount( \%myconfig, $totalcredits, 2, ' ' );
  521. $form->{statementbalance} =
  522. $form->parse_amount( \%myconfig, $form->{statementbalance} );
  523. $difference =
  524. $form->format_amount( \%myconfig,
  525. $form->{beginningbalance} + $cleared - $form->{statementbalance},
  526. 2, 0 );
  527. if ( $form->{source} ) {
  528. $difference = 0;
  529. }
  530. $form->{statementbalance} =
  531. $form->format_amount( \%myconfig, $form->{statementbalance}, 2, 0 );
  532. if ( $form->{'pos'} ) {
  533. push @buttons, {
  534. name => 'action',
  535. value => 'close_next',
  536. text => $locale->text('Close Next')
  537. };
  538. } else {
  539. push @buttons, {
  540. name => 'action',
  541. value => 'done',
  542. text => $locale->text('Done')
  543. };
  544. }
  545. $hiddens{difference} = $difference;
  546. if ( $form->{'pos'} ) {
  547. $hiddens{'pos'} = 'true';
  548. }
  549. if (! $form->{report} ) {
  550. $hiddens{$_} = $form->{$_} foreach
  551. qw(fx_transaction summary rowcount accno account fromdate todate path login sessionid);
  552. unshift @buttons, {
  553. name => 'action',
  554. value => 'select_all',
  555. text => $locale->text('Select all'),
  556. };
  557. unshift @buttons, {
  558. name => 'action',
  559. value => 'update',
  560. text => $locale->text('Update'),
  561. };
  562. }
  563. ##SC: Temporary removal
  564. ## if ( $form->{lynx} ) {
  565. ## require "bin/menu.pl";
  566. ## &menubar;
  567. ## }
  568. my $template = LedgerSMB::Template->new_UI(
  569. user => \%myconfig,
  570. locale => $locale,
  571. template => 'rc-display-form',
  572. );
  573. $template->render({
  574. form => $form,
  575. hiddens => \%hiddens,
  576. options => \@options,
  577. rows => \@rows,
  578. totals => \%column_data,
  579. columns => \@column_index,
  580. heading => \%column_header,
  581. buttons => \@buttons,
  582. });
  583. }
  584. sub update {
  585. $form->{null2} = $form->{null};
  586. RC->payment_transactions( \%myconfig, \%$form );
  587. $i = 0;
  588. foreach $ref ( @{ $form->{PR} } ) {
  589. $i++;
  590. $ref->{cleared} = ( $form->{"cleared_$i"} ) ? 1 : 0;
  591. }
  592. &display_form;
  593. }
  594. sub select_all {
  595. RC->payment_transactions( \%myconfig, \%$form );
  596. for ( @{ $form->{PR} } ) { $_->{cleared} = 1 }
  597. &display_form;
  598. }
  599. sub done {
  600. $form->{callback} =
  601. "$form->{script}?path=$form->{path}&action=reconciliation&login=$form->{login}&sessionid=$form->{sessionid}";
  602. $form->error( $locale->text('Out of balance!') )
  603. if ( $form->{difference} *= 1 );
  604. RC->reconcile( \%myconfig, \%$form );
  605. $form->redirect;
  606. }