summaryrefslogtreecommitdiff
path: root/bin/rc.pl
blob: 655cb3e604d3651f9033e2b2f80a1fa38168db04 (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. $form->{accountselect} = {
  71. name => 'accno',
  72. options => [],
  73. };
  74. for ( @{ $form->{PR} } ) {
  75. push @{$form->{accountselect}{options}}, {
  76. value => "$_->{accno}--$_->{description}",
  77. text => "$_->{accno}--$_->{description}",
  78. };
  79. }
  80. $form->{title} = $locale->text('Reconciliation');
  81. if ( $form->{report} ) {
  82. $form->{title} = $locale->text('Reconciliation Report');
  83. $hiddens{report} = 1;
  84. }
  85. if ( @{ $form->{all_years} } ) {
  86. # accounting years
  87. $form->{selectaccountingyear} = {
  88. name => 'year',
  89. options => [{text => '', value => ''}],
  90. };
  91. for ( @{ $form->{all_years} } ) {
  92. push @{$form->{selectaccountingyear}{options}},
  93. {text => $_, value => $_};
  94. }
  95. $form->{selectaccountingmonth} = {
  96. name => 'month',
  97. options => [{text => '', value => ''}],
  98. };
  99. for ( sort keys %{ $form->{all_month} } ) {
  100. push @{$form->{selectaccountingmonth}{options}}, {
  101. value => $_,
  102. text => $locale->text($form->{all_month}{$_})
  103. };
  104. }
  105. }
  106. $hiddens{nextsub} = 'get_payments';
  107. $hiddens{$_} = $form->{$_} foreach qw(path login sessionid);
  108. my @buttons = ({
  109. name => 'action',
  110. value => 'get_payments',
  111. text => $locale->text('Continue'),
  112. });
  113. ##SC: Temporary removal
  114. ## if ( $form->{lynx} ) {
  115. ## require "bin/menu.pl";
  116. ## &menubar;
  117. ## }
  118. my $template = LedgerSMB::Template->new_UI(
  119. user => \%myconfig,
  120. locale => $locale,
  121. template => 'rc-reconciliation',
  122. );
  123. $template->render({
  124. form => $form,
  125. user => \%myconfig,
  126. hiddens => \%hiddens,
  127. buttons => \@buttons,
  128. });
  129. }
  130. sub continue { &{ $form->{nextsub} } }
  131. sub till_closing {
  132. my %hiddens;
  133. $form->{callback} =
  134. "$form->{script}?path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  135. # $locale->text("Source");
  136. # $locale->text("Actual");
  137. # $locale->text("Expected");
  138. # $locale->text("Error");
  139. @colheadings = qw(Source Actual Expected Error);
  140. my $curren = $pos_config{'curren'};
  141. $form->{title} = $locale->text( "Closing Till For [_1]", $form->{login} );
  142. require "pos.conf.pl";
  143. RC->getposlines( \%myconfig, \%$form );
  144. $hiddens{path} = $form->{path};
  145. $hiddens{login} = $form->{login};
  146. $hiddens{sessionid} = $form->{sessionid};
  147. $hiddens{callback} = $form->{callback};
  148. $hiddens{sum} = "$form->{sum}" * -1;
  149. my $j;
  150. my $source;
  151. my @sources;
  152. foreach $source ( sort keys %pos_sources ) {
  153. $amount = 0;
  154. foreach $ref ( @{ $form->{TB} } ) {
  155. if ( $ref->{memo} eq $source ) {
  156. $amount = $ref->{amount} * -1;
  157. last;
  158. }
  159. }
  160. ++$j;
  161. $j = $j % 2;
  162. push @sources, {i => $j,
  163. label => $pos_sources{$source},
  164. source => $source,
  165. currenamount => "${curren}${amount}",
  166. };
  167. $hiddens{"expected_$source"} = "$amount";
  168. }
  169. my @units;
  170. foreach my $unit ( @{ $pos_config{'breakdown'} } ) {
  171. # XXX Needs to take into account currencies that don't use 2 dp
  172. my $calcval = $form->parse_amount( \%pos_config, $unit );
  173. $calcval = sprintf( '%03d', $calcval * 100 ) if $calcval < 1;
  174. my $subval = 'sub_' . $calcval;
  175. my $unit_name = $calcval;
  176. $calcval = 'calc_' . $calcval;
  177. push @units, {
  178. unit => $unit,
  179. unit_name => "$unit_name",
  180. currenunit => "${curren}${unit}",
  181. quantity => {name => $calcval, value => $form->{$calcval}},
  182. value => {name => $subval, value => $form->{$subval}},
  183. };
  184. }
  185. my @buttons = ({
  186. name => 'calculate',
  187. value => 'Calculate',
  188. type => 'button',
  189. text => $locale->text('Calculate'),
  190. attributes => {onclick => 'custom_calc_total()'},
  191. }, {
  192. name => 'action',
  193. value => 'close_till',
  194. text => $locale->text('Close Till'),
  195. });
  196. my $template = LedgerSMB::Template->new_UI(
  197. user => \%myconfig,
  198. locale => $locale,
  199. template => 'rc-till-closing',
  200. );
  201. $template->render({
  202. form => $form,
  203. user => \%myconfig,
  204. 'pos' => \%pos_config,
  205. hiddens => \%hiddens,
  206. columns => \@colheadings,
  207. sources => \@sources,
  208. units => \@units,
  209. buttons => \@buttons,
  210. });
  211. }
  212. sub close_till {
  213. use LedgerSMB::GL;
  214. require 'pos.conf.pl';
  215. RC->clear_till( \%myconfig, \%$form );
  216. my $amount = 0;
  217. my $expected = 0;
  218. my $difference = 0;
  219. my $lines = '';
  220. $form->{rowcount} = 2;
  221. foreach $key ( keys %pos_sources ) {
  222. $amount = 0;
  223. $expected = 0;
  224. $amount = $form->parse_amount( \%myconfig, $form->{"amount_$key"} );
  225. $expected = $form->parse_amount( \%myconfig, $form->{"expected_$key"} );
  226. $gl_entry = "Closing Till $pos_config{till} source = $key";
  227. $accno1 = $pos_config{till_accno};
  228. if ( ${ $pos_config{'source_accno_override'}{$key} } ) {
  229. $accno2 = ${ $pos_config{'source_accno_override'}{$key} };
  230. }
  231. else {
  232. $accno2 = $pos_config{'close_cash_accno'};
  233. }
  234. $form->{reference} = $gl_entry;
  235. $form->{accno_1} = $accno1;
  236. $form->{credit_1} = $amount;
  237. $form->{accno_2} = $accno2;
  238. $form->{debit_2} = $amount;
  239. $form->{transdate} = $form->current_date( \%myconfig );
  240. GL->post_transaction( \%myconfig, \%$form );
  241. delete $form->{id};
  242. $error = $amount - $expected;
  243. $difference += $error;
  244. $lines .=
  245. "Source: $key, Amount: $amount\nExpected: $expected. Error= $error\n\n";
  246. }
  247. $gl_entry = "Closing Till: $pos_config{till} Over/Under";
  248. $amount = $difference * -1;
  249. $form->{reference} = $gl_entry;
  250. $form->{accno_1} = $accno1;
  251. $form->{credit_1} = $amount;
  252. $form->{accno_2} = $pos_config{coa_prefix};
  253. $form->{debit_2} = $amount;
  254. $form->{transdate} = $form->current_date( \%myconfig );
  255. GL->post_transaction( \%myconfig, \%$form );
  256. delete $form->{id};
  257. $lines .= "Cumulative Error: $amount";
  258. $form->{accno} = $form->{accno_1};
  259. RC->getbalance( \%myconfig, \%$form );
  260. $amount = $form->{balance} * -1;
  261. $gl_entry = "Resetting Till: $pos_config{till}";
  262. $form->{reference} = $gl_entry;
  263. $form->{accno_1} = $accno1;
  264. $form->{credit_1} = $amount;
  265. $form->{accno_2} = $pos_config{coa_prefix};
  266. $form->{debit_2} = $amount;
  267. $form->{transdate} = $form->current_date( \%myconfig );
  268. GL->post_transaction( \%myconfig, \%$form );
  269. delete $form->{id};
  270. $head =
  271. "Closing Till $pos_config{till} for $form->{login}\n"
  272. . "Date: $form->{transdate}\n\n\n";
  273. my @cashlines = [ $locale->text("Cash Breakdown:") ];
  274. foreach my $unit ( @{ $pos_config{'breakdown'} } ) {
  275. # XXX Needs to take into account currencies that don't use 2 dp
  276. my $parsed = $form->parse_amount( \%pos_config, $unit );
  277. my $calcval = $parsed;
  278. $calcval = sprintf( '%03d', $calcval * 100 ) if $calcval < 1;
  279. my $subval = 'sub_' . $calcval;
  280. $calcval = 'calc_' . $calcval;
  281. push @cashlines, "$form->{$calcval} x $parseval = $form->{$subval}";
  282. }
  283. push @cashlines,
  284. $locale->text( "Total Cash in Drawer: [_1]", $form->{sub_sub} );
  285. push @cashlines,
  286. $locale->text( "Less Cash in Till At Start: [_1]", $form->{till_cash} );
  287. push @cashlines, "\n";
  288. $cash = join( "\n", @cashlines );
  289. $foot = $locale->text( "Cumulative Error: [_1]", $difference ) . "\n";
  290. $foot .=
  291. $locale->text( 'Reset Till By [_1]', $amount ) . "\n\n\n\n\n\n\n\n\n\n";
  292. open( PRN, "|-", ${LedgerSMB::Sysconfig::printer}{Printer} );
  293. print PRN $head;
  294. print PRN $lines;
  295. print PRN $cash;
  296. print PRN $cash;
  297. print PRN $foot;
  298. close PRN;
  299. if ( $difference > 0 ) {
  300. $message = $locale->text( "You are over by [_1]", $difference );
  301. }
  302. elsif ( $difference < 0 ) {
  303. $message = $locale->text( "You are under by [_1]", $difference * -1 );
  304. }
  305. else {
  306. $message =
  307. $local->text("Congratulations! Your till is exactly balanced.");
  308. }
  309. $form->info($message);
  310. }
  311. sub get_payments {
  312. ( $form->{accno}, $form->{account} ) = split /--/, $form->{accno};
  313. if ( $form->{'pos'} ) {
  314. require "pos.conf.pl";
  315. $form->{fromdate} = $form->current_date( \%myconfig );
  316. unless ( $form->{source} ) {
  317. $form->{source} = ( sort keys(%pos_sources) )[0];
  318. }
  319. if ( $form->{source} eq 'cash' ) {
  320. $form->{summary} = "true";
  321. }
  322. else {
  323. $form->{summary} = "";
  324. }
  325. $form->{accno} = $pos_config{'coa_prefix'} . "." . $pos_config{'till'};
  326. $form->{account} = $form->{source};
  327. }
  328. RC->payment_transactions( \%myconfig, \%$form );
  329. $ml = ( $form->{category} eq 'A' ) ? -1 : 1;
  330. $form->{statementbalance} = $form->{endingbalance} * $ml;
  331. if ( !$form->{fx_transaction} ) {
  332. $form->{statementbalance} =
  333. ( $form->{endingbalance} - $form->{fx_endingbalance} ) * $ml;
  334. }
  335. $form->{statementbalance} =
  336. $form->format_amount( \%myconfig, $form->{statementbalance}, 2, 0 );
  337. &display_form;
  338. }
  339. sub display_form {
  340. my %hiddens;
  341. my @buttons;
  342. my @column_index;
  343. if ( $form->{report} ) {
  344. @column_index = qw(transdate source name cleared debit credit);
  345. }
  346. else {
  347. @column_index = qw(transdate source name cleared debit credit balance);
  348. }
  349. my %column_header;
  350. $column_header{cleared} = $locale->text('R');
  351. $column_header{source} = $locale->text('Source');
  352. $column_header{name} = $locale->text('Description');
  353. $column_header{transdate} = $locale->text('Date');
  354. $column_header{debit} = $locale->text('Debit');
  355. $column_header{credit} = $locale->text('Credit');
  356. $column_header{balance} = $locale->text('Balance');
  357. my @options;
  358. if ( $form->{fromdate} ) {
  359. push @options,
  360. $locale->text('From [_1]',
  361. $locale->date( \%myconfig, $form->{fromdate}, 1 ));
  362. }
  363. if ( $form->{todate} ) {
  364. push @options,
  365. $locale->text('To [_1]',
  366. $locale->date( \%myconfig, $form->{todate}, 1 ));
  367. }
  368. $form->{title} = "$form->{accno}--$form->{account}";
  369. $hiddens{source} = $form->{source};
  370. $hiddens{cumulative_error} = $form->{cumulative_error};
  371. my $ml = ( $form->{category} eq 'A' ) ? -1 : 1;
  372. $form->{beginningbalance} *= $ml;
  373. $form->{fx_balance} *= $ml;
  374. if ( !$form->{fx_transaction} ) {
  375. $form->{beginningbalance} -= $form->{fx_balance};
  376. }
  377. my $balance = $form->{beginningbalance};
  378. my $i = 0;
  379. my $j = 0;
  380. my @rows;
  381. if ( !$form->{report} ) {
  382. my %column_data;
  383. for (qw(cleared transdate source debit credit)) {
  384. $column_data{$_} = ' ';
  385. }
  386. $column_data{name} = $locale->text('Beginning Balance');
  387. $column_data{balance} = $form->format_amount(\%myconfig, $balance, 2, 0);
  388. $column_data{i} = $j;
  389. push @rows, \%column_data;
  390. }
  391. foreach my $ref ( @{ $form->{PR} } ) {
  392. $i++;
  393. if ( !$form->{fx_transaction} ) {
  394. next if $ref->{fx_transaction};
  395. }
  396. my %column_data;
  397. my $checked = ( $ref->{cleared} ) ? "checked" : undef;
  398. my %temp = ();
  399. if ( !$ref->{fx_transaction} ) {
  400. for (qw(name source transdate)) { $temp{$_} = $ref->{$_} }
  401. }
  402. $column_data{name}{delimiter} = "|";
  403. for ( @{ $temp{name} } ) { $column_data{name}{text} .= "$_|" }
  404. $column_data{source} = $temp{source};
  405. $column_data{debit} = ' ';
  406. $column_data{credit} = ' ';
  407. $balance += $ref->{amount} * $ml;
  408. $hiddens{"id_$i"} = $ref->{id};
  409. if ( $ref->{amount} < 0 ) {
  410. $totaldebits += $ref->{amount} * -1;
  411. $column_data{debit} =
  412. $form->format_amount(\%myconfig, $ref->{amount} * -1, 2, ' ');
  413. } else {
  414. $totalcredits += $ref->{amount};
  415. $column_data{credit} =
  416. $form->format_amount(\%myconfig, $ref->{amount}, 2, ' ');
  417. }
  418. $column_data{balance} = $form->format_amount(\%myconfig, $balance, 2, 0);
  419. if ( $ref->{fx_transaction} ) {
  420. $column_data{cleared} =
  421. ($clearfx) ? '*': ' ';
  422. $cleared += $ref->{amount} * $ml if $clearfx;
  423. } else {
  424. if ( $form->{report} ) {
  425. if ( $ref->{cleared} ) {
  426. $column_data{cleared} = '*';
  427. $clearfx = 1;
  428. } else {
  429. $column_data{cleared} = ' ';
  430. $clearfx = 0;
  431. }
  432. } else {
  433. if ( $ref->{oldcleared} ) {
  434. $cleared += $ref->{amount} * $ml;
  435. $clearfx = 1;
  436. $hiddens{"cleared_$i"} = $ref->{cleared};
  437. $hiddens{"oldcleared_$i"} = $ref->{oldcleared};
  438. $hiddens{"source_$i"} = $ref->{source};
  439. $hiddens{"amount_$i"} = $ref->{amount};
  440. $column_data{cleared} = '*';
  441. } else {
  442. $cleared += $ref->{amount} * $ml if $checked;
  443. $clearfx = ($checked) ? 1 : 0;
  444. $hiddens{"source_$i"} = $ref->{source};
  445. $hiddens{"amount_$i"} = $ref->{amount};
  446. $column_data{cleared} = {input => {
  447. type => 'checkbox',
  448. value => 1,
  449. name => "cleared_$i",
  450. $checked => $checked,
  451. }};
  452. }
  453. }
  454. }
  455. $hiddens{"transdate_$i"} = $ref->{transdate};
  456. $column_data{transdate} = $temp{transdate};
  457. $j++;
  458. $j %= 2;
  459. $column_data{i} = $j;
  460. push @rows, \%column_data;
  461. }
  462. $form->{rowcount} = $i;
  463. # print totals
  464. my %column_data;
  465. for (@column_index) { $column_data{$_} = ' ' }
  466. $column_data{debit} =
  467. $form->format_amount( \%myconfig, $totaldebits, 2, ' ' );
  468. $column_data{credit} =
  469. $form->format_amount( \%myconfig, $totalcredits, 2, ' ' );
  470. $form->{statementbalance} =
  471. $form->parse_amount( \%myconfig, $form->{statementbalance} );
  472. $difference =
  473. $form->format_amount( \%myconfig,
  474. $form->{beginningbalance} + $cleared - $form->{statementbalance},
  475. 2, 0 );
  476. if ( $form->{source} ) {
  477. $difference = 0;
  478. }
  479. $form->{statementbalance} =
  480. $form->format_amount( \%myconfig, $form->{statementbalance}, 2, 0 );
  481. if ( $form->{'pos'} ) {
  482. push @buttons, {
  483. name => 'action',
  484. value => 'close_next',
  485. text => $locale->text('Close Next')
  486. };
  487. } else {
  488. push @buttons, {
  489. name => 'action',
  490. value => 'done',
  491. text => $locale->text('Done')
  492. };
  493. }
  494. $hiddens{difference} = $difference;
  495. if ( $form->{'pos'} ) {
  496. $hiddens{'pos'} = 'true';
  497. }
  498. if (! $form->{report} ) {
  499. $hiddens{$_} = $form->{$_} foreach
  500. qw(fx_transaction summary rowcount accno account fromdate todate path login sessionid);
  501. unshift @buttons, {
  502. name => 'action',
  503. value => 'select_all',
  504. text => $locale->text('Select all'),
  505. };
  506. unshift @buttons, {
  507. name => 'action',
  508. value => 'update',
  509. text => $locale->text('Update'),
  510. };
  511. }
  512. ##SC: Temporary removal
  513. ## if ( $form->{lynx} ) {
  514. ## require "bin/menu.pl";
  515. ## &menubar;
  516. ## }
  517. my $template = LedgerSMB::Template->new_UI(
  518. user => \%myconfig,
  519. locale => $locale,
  520. template => 'rc-display-form',
  521. );
  522. $template->render({
  523. form => $form,
  524. hiddens => \%hiddens,
  525. options => \@options,
  526. rows => \@rows,
  527. totals => \%column_data,
  528. columns => \@column_index,
  529. heading => \%column_header,
  530. buttons => \@buttons,
  531. });
  532. }
  533. sub update {
  534. $form->{null2} = $form->{null};
  535. RC->payment_transactions( \%myconfig, \%$form );
  536. $i = 0;
  537. foreach $ref ( @{ $form->{PR} } ) {
  538. $i++;
  539. $ref->{cleared} = ( $form->{"cleared_$i"} ) ? 1 : 0;
  540. }
  541. &display_form;
  542. }
  543. sub select_all {
  544. RC->payment_transactions( \%myconfig, \%$form );
  545. for ( @{ $form->{PR} } ) { $_->{cleared} = 1 }
  546. &display_form;
  547. }
  548. sub done {
  549. $form->{callback} =
  550. "$form->{script}?path=$form->{path}&action=reconciliation&login=$form->{login}&sessionid=$form->{sessionid}";
  551. $form->error( $locale->text('Out of balance!') )
  552. if ( $form->{difference} *= 1 );
  553. RC->reconcile( \%myconfig, \%$form );
  554. $form->redirect;
  555. }