summaryrefslogtreecommitdiff
path: root/bin/gl.pl
blob: 6a3a3caa2c8ee1f9416f9b0aca6d85adab7359a6 (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) 2001
  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. # Genereal Ledger
  44. #
  45. #======================================================================
  46. use LedgerSMB::GL;
  47. use LedgerSMB::PE;
  48. use LedgerSMB::Template;
  49. require "bin/arap.pl";
  50. $form->{login} = 'test';
  51. 1;
  52. # end of main
  53. # this is for our long dates
  54. # $locale->text('January')
  55. # $locale->text('February')
  56. # $locale->text('March')
  57. # $locale->text('April')
  58. # $locale->text('May ')
  59. # $locale->text('June')
  60. # $locale->text('July')
  61. # $locale->text('August')
  62. # $locale->text('September')
  63. # $locale->text('October')
  64. # $locale->text('November')
  65. # $locale->text('December')
  66. # this is for our short month
  67. # $locale->text('Jan')
  68. # $locale->text('Feb')
  69. # $locale->text('Mar')
  70. # $locale->text('Apr')
  71. # $locale->text('May')
  72. # $locale->text('Jun')
  73. # $locale->text('Jul')
  74. # $locale->text('Aug')
  75. # $locale->text('Sep')
  76. # $locale->text('Oct')
  77. # $locale->text('Nov')
  78. # $locale->text('Dec')
  79. sub pos_adjust {
  80. $form->{rowcount} = 3;
  81. require "pos.conf.pl";
  82. $form->{accno_1} = $pos_config{'close_cash_accno'};
  83. $form->{accno_2} = $pos_config{'coa_prefix'};
  84. $form->{accno_3} = $pos_config{'coa_prefix'};
  85. }
  86. sub add_pos_adjust {
  87. $form->{pos_adjust} = 1;
  88. $form->{reference} =
  89. $locale->text("Adjusting Till: (till) Source: (source)");
  90. $form->{description} =
  91. $locale->text("Adjusting till due to data entry error.");
  92. $form->{callback} =
  93. "$form->{script}?action=add_pos_adjust&transfer=$form->{transfer}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}"
  94. unless $form->{callback};
  95. &add;
  96. }
  97. sub add {
  98. $form->{title} = "Add";
  99. $form->{callback} =
  100. "$form->{script}?action=add&transfer=$form->{transfer}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}"
  101. unless $form->{callback};
  102. &create_links;
  103. $form->{reference} = $form->update_defaults(\%myconfig, 'glnumber');
  104. $form->{rowcount} = ( $form->{transfer} ) ? 3 : 9;
  105. if ( $form->{pos_adjust} ) {
  106. &pos_adjust;
  107. }
  108. $form->{oldtransdate} = $form->{transdate};
  109. $form->{focus} = "reference";
  110. # departments
  111. if ( @{ $form->{all_department} } ) {
  112. $form->{selectdepartment} = "<option>\n";
  113. for ( @{ $form->{all_department} } ) {
  114. $form->{selectdepartment} .=
  115. qq|<option value="$_->{description}--$_->{id}">$_->{description}\n|;
  116. }
  117. }
  118. &display_form(1);
  119. }
  120. sub edit {
  121. &create_links;
  122. $form->{locked} =
  123. ( $form->{revtrans} )
  124. ? '1'
  125. : ( $form->datetonum( \%myconfig, $form->{transdate} ) <=
  126. $form->datetonum( \%myconfig, $form->{closedto} ) );
  127. # readonly
  128. if ( !$form->{readonly} ) {
  129. $form->{readonly} = 1
  130. if $myconfig{acs} =~ /General Ledger--Add Transaction/;
  131. }
  132. $form->{title} = "Edit";
  133. $i = 1;
  134. foreach $ref ( @{ $form->{GL} } ) {
  135. $form->{"accno_$i"} = "$ref->{accno}--$ref->{description}";
  136. $form->{"projectnumber_$i"} =
  137. "$ref->{projectnumber}--$ref->{project_id}";
  138. for (qw(fx_transaction source memo)) { $form->{"${_}_$i"} = $ref->{$_} }
  139. if ( $ref->{amount} < 0 ) {
  140. $form->{totaldebit} -= $ref->{amount};
  141. $form->{"debit_$i"} = $ref->{amount} * -1;
  142. }
  143. else {
  144. $form->{totalcredit} += $ref->{amount};
  145. $form->{"credit_$i"} = $ref->{amount};
  146. }
  147. $i++;
  148. }
  149. $form->{rowcount} = $i;
  150. $form->{focus} = "debit_$i";
  151. &form_header;
  152. &display_rows;
  153. &form_footer;
  154. }
  155. sub create_links {
  156. GL->transaction( \%myconfig, \%$form );
  157. for ( @{ $form->{all_accno} } ) {
  158. $form->{selectaccno} .= "<option>$_->{accno}--$_->{description}\n";
  159. }
  160. # projects
  161. if ( @{ $form->{all_project} } ) {
  162. $form->{selectprojectnumber} = "<option>\n";
  163. for ( @{ $form->{all_project} } ) {
  164. $form->{selectprojectnumber} .=
  165. qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n|;
  166. }
  167. }
  168. # departments
  169. if ( @{ $form->{all_department} } ) {
  170. $form->{department} = "$form->{department}--$form->{department_id}";
  171. $form->{selectdepartment} = "<option>\n";
  172. for ( @{ $form->{all_department} } ) {
  173. $form->{selectdepartment} .=
  174. qq|<option value="$_->{description}--$_->{id}">$_->{description}\n|;
  175. }
  176. }
  177. }
  178. sub search {
  179. $form->{title} = $locale->text('General Ledger Reports');
  180. $colspan = 5;
  181. $form->all_departments( \%myconfig );
  182. # departments
  183. if ( @{ $form->{all_department} } ) {
  184. $form->{selectdepartment} = "<option>\n";
  185. for ( @{ $form->{all_department} } ) {
  186. $form->{selectdepartment} .=
  187. qq|<option value="$_->{description}--$_->{id}">$_->{description}\n|;
  188. }
  189. $l_department =
  190. qq|<input name="l_department" class=checkbox type=checkbox value=Y> |
  191. . $locale->text('Department');
  192. $department = qq|
  193. <tr>
  194. <th align=right nowrap>| . $locale->text('Department') . qq|</th>
  195. <td colspan=$colspan><select name=department>$form->{selectdepartment}</select></td>
  196. </tr>
  197. |;
  198. }
  199. if ( @{ $form->{all_years} } ) {
  200. # accounting years
  201. $form->{selectaccountingyear} = "<option>\n";
  202. for ( @{ $form->{all_years} } ) {
  203. $form->{selectaccountingyear} .= qq|<option>$_\n|;
  204. }
  205. $form->{selectaccountingmonth} = "<option>\n";
  206. for ( sort keys %{ $form->{all_month} } ) {
  207. $form->{selectaccountingmonth} .=
  208. qq|<option value=$_>|
  209. . $locale->text( $form->{all_month}{$_} ) . qq|\n|;
  210. }
  211. $selectfrom = qq|
  212. <tr>
  213. <th align=right>| . $locale->text('Period') . qq|</th>
  214. <td colspan=$colspan>
  215. <select name=month>$form->{selectaccountingmonth}</select>
  216. <select name=year>$form->{selectaccountingyear}</select>
  217. <input name=interval class=radio type=radio value=0 checked>&nbsp;|
  218. . $locale->text('Current') . qq|
  219. <input name=interval class=radio type=radio value=1>&nbsp;|
  220. . $locale->text('Month') . qq|
  221. <input name=interval class=radio type=radio value=3>&nbsp;|
  222. . $locale->text('Quarter') . qq|
  223. <input name=interval class=radio type=radio value=12>&nbsp;|
  224. . $locale->text('Year') . qq|
  225. </td>
  226. </tr>
  227. |;
  228. }
  229. @a = ();
  230. push @a, qq|<input name="l_id" class=checkbox type=checkbox value=Y> |
  231. . $locale->text('ID');
  232. push @a,
  233. qq|<input name="l_transdate" class=checkbox type=checkbox value=Y checked> |
  234. . $locale->text('Date');
  235. push @a,
  236. qq|<input name="l_reference" class=checkbox type=checkbox value=Y checked> |
  237. . $locale->text('Reference');
  238. push @a,
  239. qq|<input name="l_description" class=checkbox type=checkbox value=Y checked> |
  240. . $locale->text('Description');
  241. push @a, qq|<input name="l_notes" class=checkbox type=checkbox value=Y> |
  242. . $locale->text('Notes');
  243. push @a, $l_department if $l_department;
  244. push @a,
  245. qq|<input name="l_debit" class=checkbox type=checkbox value=Y checked> |
  246. . $locale->text('Debit');
  247. push @a,
  248. qq|<input name="l_credit" class=checkbox type=checkbox value=Y checked> |
  249. . $locale->text('Credit');
  250. push @a,
  251. qq|<input name="l_source" class=checkbox type=checkbox value=Y checked> |
  252. . $locale->text('Source');
  253. push @a, qq|<input name="l_memo" class=checkbox type=checkbox value=Y> |
  254. . $locale->text('Memo');
  255. push @a,
  256. qq|<input name="l_accno" class=checkbox type=checkbox value=Y checked> |
  257. . $locale->text('Account');
  258. push @a,
  259. qq|<input name="l_gifi_accno" class=checkbox type=checkbox value=Y> |
  260. . $locale->text('GIFI');
  261. $form->header;
  262. print qq|
  263. <body>
  264. <form method=post action=$form->{script}>
  265. <input type=hidden name=sort value=transdate>
  266. <table width=100%>
  267. <tr>
  268. <th class=listtop>$form->{title}</th>
  269. </tr>
  270. <tr height="5"></tr>
  271. <tr>
  272. <td>
  273. <table>
  274. <tr>
  275. <th align=right>| . $locale->text('Reference') . qq|</th>
  276. <td><input name=reference size=20></td>
  277. </tr>
  278. <tr>
  279. <th align=right>| . $locale->text('Source') . qq|</th>
  280. <td><input name=source size=20></td>
  281. <th align=right>| . $locale->text('Memo') . qq|</th>
  282. <td><input name=memo size=20></td>
  283. </tr>
  284. $department
  285. <tr>
  286. <th align=right>| . $locale->text('Description') . qq|</th>
  287. <td colspan=$colspan><input name=description size=60></td>
  288. </tr>
  289. <tr>
  290. <th align=right>| . $locale->text('Notes') . qq|</th>
  291. <td colspan=$colspan><input name=notes size=60></td>
  292. </tr>
  293. <tr>
  294. <th align=right>| . $locale->text('From') . qq|</th>
  295. <td><input class="date" name=datefrom size=11 title="$myconfig{dateformat}"></td>
  296. <th align=right>| . $locale->text('To') . qq|</th>
  297. <td><input class="date" name=dateto size=11 title="$myconfig{dateformat}"></td>
  298. </tr>
  299. $selectfrom
  300. <tr>
  301. <th align=right>| . $locale->text('Amount') . qq| >=</th>
  302. <td><input name=amountfrom size=11></td>
  303. <th align=right>| . $locale->text('Amount') . qq| <=</th>
  304. <td><input name=amountto size=11></td>
  305. </tr>
  306. <tr>
  307. <th align=right>| . $locale->text('Include in Report') . qq|</th>
  308. <td colspan=$colspan>
  309. <table>
  310. <tr>
  311. <td>
  312. <input name="category" class=radio type=radio value=X checked>&nbsp;|
  313. . $locale->text('All') . qq|
  314. <input name="category" class=radio type=radio value=A>&nbsp;|
  315. . $locale->text('Asset') . qq|
  316. <input name="category" class=radio type=radio value=L>&nbsp;|
  317. . $locale->text('Liability') . qq|
  318. <input name="category" class=radio type=radio value=Q>&nbsp;|
  319. . $locale->text('Equity') . qq|
  320. <input name="category" class=radio type=radio value=I>&nbsp;|
  321. . $locale->text('Income') . qq|
  322. <input name="category" class=radio type=radio value=E>&nbsp;|
  323. . $locale->text('Expense') . qq|
  324. </td>
  325. </tr>
  326. <tr>
  327. <table>
  328. |;
  329. while (@a) {
  330. print qq|<tr>\n|;
  331. for ( 1 .. 5 ) {
  332. print qq|<td nowrap>| . shift @a;
  333. print qq|</td>\n|;
  334. }
  335. print qq|</tr>\n|;
  336. }
  337. print qq|
  338. <tr>
  339. <td nowrap><input name="l_subtotal" class=checkbox type=checkbox value=Y> |
  340. . $locale->text('Subtotal')
  341. . qq|</td>
  342. </tr>
  343. </table>
  344. </tr>
  345. </table>
  346. </tr>
  347. </table>
  348. </td>
  349. </tr>
  350. <tr>
  351. <td><hr size=3 noshade></td>
  352. </tr>
  353. </table>
  354. <input type=hidden name=nextsub value=generate_report>
  355. |;
  356. $form->hide_form(qw(path login sessionid));
  357. print qq|
  358. <br>
  359. <button class="submit" type="submit" name="action" value="continue">|
  360. . $locale->text('Continue')
  361. . qq|</button>
  362. </form>
  363. |;
  364. if ( $form->{lynx} ) {
  365. require "bin/menu.pl";
  366. &menubar;
  367. }
  368. print qq|
  369. </body>
  370. </html>
  371. |;
  372. }
  373. sub generate_report {
  374. my $output_options = shift;
  375. $form->{sort} = "transdate" unless $form->{sort};
  376. $form->{amountfrom} = $form->parse_amount(\%myconfig, $form->{amountfrom});
  377. $form->{amountto} = $form->parse_amount(\%myconfig, $form->{amountto});
  378. GL->all_transactions( \%myconfig, \%$form );
  379. $href =
  380. "$form->{script}?action=generate_report&direction=$form->{direction}&oldsort=$form->{oldsort}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  381. $form->sort_order();
  382. $callback =
  383. "$form->{script}?action=generate_report&direction=$form->{direction}&oldsort=$form->{oldsort}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  384. my %hiddens = (
  385. 'action' => 'generate_report',
  386. 'direction' => $form->{direction},
  387. 'oldsort' => $form->{oldsort},
  388. 'path' => $form->{path},
  389. 'login' => $form->{login},
  390. 'sessionid' => $form->{sessionid},
  391. );
  392. %acctype = (
  393. 'A' => $locale->text('Asset'),
  394. 'L' => $locale->text('Liability'),
  395. 'Q' => $locale->text('Equity'),
  396. 'I' => $locale->text('Income'),
  397. 'E' => $locale->text('Expense'),
  398. );
  399. my @options;
  400. $form->{title} = $locale->text('General Ledger');
  401. $ml = ( $form->{category} =~ /(A|E)/ ) ? -1 : 1;
  402. unless ( $form->{category} eq 'X' ) {
  403. $form->{title} .=
  404. " : " . $locale->text( $acctype{ $form->{category} } );
  405. }
  406. if ( $form->{accno} ) {
  407. $href .= "&accno=" . $form->escape( $form->{accno} );
  408. $callback .= "&accno=" . $form->escape( $form->{accno}, 1 );
  409. $hiddens{accno} = $form->{accno};
  410. push @options, $locale->text('Account')
  411. . " : $form->{accno} $form->{account_description}";
  412. }
  413. if ( $form->{gifi_accno} ) {
  414. $href .= "&gifi_accno=" . $form->escape( $form->{gifi_accno} );
  415. $callback .= "&gifi_accno=" . $form->escape( $form->{gifi_accno}, 1 );
  416. $hiddens{gifi_accno} = $form->{gifi_accno};
  417. push @options, $locale->text('GIFI')
  418. . " : $form->{gifi_accno} $form->{gifi_account_description}";
  419. }
  420. if ( $form->{source} ) {
  421. $href .= "&source=" . $form->escape( $form->{source} );
  422. $callback .= "&source=" . $form->escape( $form->{source}, 1 );
  423. $hiddens{source} = $form->{source};
  424. push @options, $locale->text('Source') . " : $form->{source}";
  425. }
  426. if ( $form->{memo} ) {
  427. $href .= "&memo=" . $form->escape( $form->{memo} );
  428. $callback .= "&memo=" . $form->escape( $form->{memo}, 1 );
  429. $hiddens{memo} = $form->{memo};
  430. push @options, $locale->text('Memo') . " : $form->{memo}";
  431. }
  432. if ( $form->{reference} ) {
  433. $href .= "&reference=" . $form->escape( $form->{reference} );
  434. $callback .= "&reference=" . $form->escape( $form->{reference}, 1 );
  435. $hiddens{reference} = $form->{reference};
  436. push @options, $locale->text('Reference') . " : $form->{reference}";
  437. }
  438. if ( $form->{department} ) {
  439. $href .= "&department=" . $form->escape( $form->{department} );
  440. $callback .= "&department=" . $form->escape( $form->{department}, 1 );
  441. $hiddens{department} = $form->{department};
  442. ($department) = split /--/, $form->{department};
  443. push @options, $locale->text('Department') . " : $department";
  444. }
  445. if ( $form->{description} ) {
  446. $href .= "&description=" . $form->escape( $form->{description} );
  447. $callback .= "&description=" . $form->escape( $form->{description}, 1 );
  448. $hiddens{description} = $form->{description};
  449. push @options, $locale->text('Description') . " : $form->{description}";
  450. }
  451. if ( $form->{notes} ) {
  452. $href .= "&notes=" . $form->escape( $form->{notes} );
  453. $callback .= "&notes=" . $form->escape( $form->{notes}, 1 );
  454. $hiddens{notes} = $form->{notes};
  455. push @options, $locale->text('Notes') . " : $form->{notes}";
  456. }
  457. if ( $form->{datefrom} ) {
  458. $href .= "&datefrom=$form->{datefrom}";
  459. $callback .= "&datefrom=$form->{datefrom}";
  460. $hiddens{datefrom} = $form->{datefrom};
  461. push @options, $locale->text('From') . " "
  462. . $locale->date( \%myconfig, $form->{datefrom}, 1 );
  463. }
  464. if ( $form->{dateto} ) {
  465. $href .= "&dateto=$form->{dateto}";
  466. $callback .= "&dateto=$form->{dateto}";
  467. $hiddens{dateto} = $form->{dateto};
  468. my $option = $locale->text('To') . " "
  469. . $locale->date( \%myconfig, $form->{dateto}, 1 );
  470. if ( $form->{datefrom} ) {
  471. $options[$#options] .= " $option";
  472. }
  473. else {
  474. push @options, $option;
  475. }
  476. }
  477. if ( $form->{amountfrom} ) {
  478. $href .= "&amountfrom=$form->{amountfrom}";
  479. $callback .= "&amountfrom=$form->{amountfrom}";
  480. $hiddens{amountfrom} = $form->{amountfrom};
  481. push @options, $locale->text('Amount') . " >= "
  482. . $form->format_amount( \%myconfig, $form->{amountfrom}, 2 );
  483. }
  484. if ( $form->{amountto} ) {
  485. $href .= "&amountto=$form->{amountto}";
  486. $callback .= "&amountto=$form->{amountto}";
  487. $hiddens{amountto} = $form->{amountto};
  488. my $option .= $form->format_amount( \%myconfig, $form->{amountto}, 2 );
  489. if ( $form->{amountfrom} ) {
  490. $options[$#options] .= " <= $option";
  491. }
  492. else {
  493. push @options, $locale->text('Amount') . " <= $option";
  494. }
  495. }
  496. @columns =
  497. $form->sort_columns(
  498. qw(transdate id reference description notes source memo debit credit accno gifi_accno department)
  499. );
  500. pop @columns if $form->{department};
  501. if ( $form->{link} =~ /_paid/ ) {
  502. @columns =
  503. $form->sort_columns(
  504. qw(transdate id reference description notes source memo cleared debit credit accno gifi_accno)
  505. );
  506. $form->{l_cleared} = "Y";
  507. }
  508. if ( $form->{accno} || $form->{gifi_accno} ) {
  509. @columns = grep !/(accno|gifi_accno)/, @columns;
  510. push @columns, "balance";
  511. $form->{l_balance} = "Y";
  512. }
  513. foreach $item (@columns) {
  514. if ( $form->{"l_$item"} eq "Y" ) {
  515. push @column_index, $item;
  516. # add column to href and callback
  517. $callback .= "&l_$item=Y";
  518. $href .= "&l_$item=Y";
  519. $hiddens{"l_$item"} = 'Y';
  520. }
  521. }
  522. if ( $form->{l_subtotal} eq 'Y' ) {
  523. $callback .= "&l_subtotal=Y";
  524. $href .= "&l_subtotal=Y";
  525. $hiddens{l_subtotal} = 'Y';
  526. }
  527. $callback .= "&category=$form->{category}";
  528. $href .= "&category=$form->{category}";
  529. $hiddens{category} = $form->{category};
  530. $column_header{id} =
  531. {text => $locale->text('ID'), href=> "$href&sort=id"};
  532. $column_header{transdate} =
  533. {text => $locale->text('Date'), href=> "$href&sort=transdate"};
  534. $column_header{reference} =
  535. {text => $locale->text('Reference'), href=> "$href&sort=reference"};
  536. $column_header{source} =
  537. {text => $locale->text('Source'), href=> "$href&sort=source"};
  538. $column_header{memo} =
  539. {text => $locale->text('Memo'), href=> "$href&sort=memo"};
  540. $column_header{description} =
  541. {text => $locale->text('Description'), href=> "$href&sort=description"};
  542. $column_header{department} =
  543. {text => $locale->text('Department'), href=> "$href&sort=department"};
  544. $column_header{notes} = $locale->text('Notes');
  545. $column_header{debit} = $locale->text('Debit');
  546. $column_header{credit} = $locale->text('Credit');
  547. $column_header{accno} =
  548. {text => $locale->text('Account'), href=> "$href&sort=accno"};
  549. $column_header{gifi_accno} =
  550. {text => $locale->text('GIFI'), href=> "$href&sort=gifi_accno"};
  551. $column_header{balance} = $locale->text('Balance');
  552. $column_header{cleared} = $locale->text('R');
  553. # add sort to callback
  554. $form->{callback} = "$callback&sort=$form->{sort}";
  555. $callback = $form->escape( $form->{callback} );
  556. $hiddens{sort} = $form->{sort};
  557. $hiddens{callback} = $form->{callback};
  558. $cml = 1;
  559. # initial item for subtotals
  560. if ( @{ $form->{GL} } ) {
  561. $sameitem = $form->{GL}->[0]->{ $form->{sort} };
  562. $cml = -1 if $form->{contra};
  563. }
  564. my @rows;
  565. if ( ( $form->{accno} || $form->{gifi_accno} ) && $form->{balance} ) {
  566. my %column_data;
  567. for (@column_index) { $column_data{$_} = " " }
  568. $column_data{balance} =
  569. $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml,
  570. 2, 0 );
  571. $column_data{i} = 1;
  572. push @rows, \%column_data;
  573. }
  574. # reverse href
  575. $direction = ( $form->{direction} eq 'ASC' ) ? "ASC" : "DESC";
  576. $form->sort_order();
  577. $href =~ s/direction=$form->{direction}/direction=$direction/;
  578. my $i = 0;
  579. foreach $ref ( @{ $form->{GL} } ) {
  580. my %column_data;
  581. # if item ne sort print subtotal
  582. if ( $form->{l_subtotal} eq 'Y' ) {
  583. if ( $sameitem ne $ref->{ $form->{sort} } ) {
  584. push @rows, &gl_subtotal_tt();
  585. }
  586. }
  587. $form->{balance} += $ref->{amount};
  588. $subtotaldebit += $ref->{debit};
  589. $subtotalcredit += $ref->{credit};
  590. $totaldebit += $ref->{debit};
  591. $totalcredit += $ref->{credit};
  592. $ref->{debit} =
  593. $form->format_amount( \%myconfig, $ref->{debit}, 2, " " );
  594. $ref->{credit} =
  595. $form->format_amount( \%myconfig, $ref->{credit}, 2, " " );
  596. for (qw(id transdate)) { $column_data{$_} = "$ref->{$_}" }
  597. $column_data{reference} =
  598. {href => "$ref->{module}.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback",
  599. text => $ref->{reference}};
  600. #$ref->{notes} =~ s/\r?\n/<br>/g;
  601. for (qw(description source memo notes department)) {
  602. $column_data{$_} = "$ref->{$_} ";
  603. }
  604. $column_data{debit} = "$ref->{debit}";
  605. $column_data{credit} = "$ref->{credit}";
  606. $column_data{accno} =
  607. {href => "$href&accno=$ref->{accno}&callback=$callback",
  608. text => "$ref->{accno} $ref->{accname}"};
  609. $column_data{gifi_accno} =
  610. {href => "$href&gifi_accno=$ref->{gifi_accno}&callback=$callback",
  611. text => $ref->{gifi_accno}};
  612. $column_data{balance} = $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml,
  613. 2, 0 );
  614. $column_data{cleared} =
  615. ( $ref->{cleared} ) ? "*" : " ";
  616. if ( $ref->{id} != $sameid ) {
  617. $i++;
  618. $i %= 2;
  619. }
  620. $column_data{'i'} = $i;
  621. push @rows, \%column_data;
  622. $sameid = $ref->{id};
  623. }
  624. push @rows, &gl_subtotal_tt() if ( $form->{l_subtotal} eq 'Y' );
  625. for (@column_index) { $column_data{$_} = " " }
  626. $column_data{debit} = $form->format_amount( \%myconfig, $totaldebit, 2, " " );
  627. $column_data{credit} = $form->format_amount( \%myconfig, $totalcredit, 2, " " );
  628. $column_data{balance} = $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, 2, 0 );
  629. $i = 1;
  630. my %button;
  631. if ( $myconfig{acs} !~ /General Ledger--General Ledger/ ) {
  632. $button{'General Ledger--Add Transaction'} = {
  633. name => 'action',
  634. value => 'gl_transaction',
  635. text => $locale->text('GL Transaction'),
  636. type => 'submit',
  637. class => 'submit',
  638. order => $i++};
  639. }
  640. if ( $myconfig{acs} !~ /AR--AR/ ) {
  641. $button{'AR--Add Transaction'} = {
  642. name => 'action',
  643. value => 'ar_transaction',
  644. text => $locale->text('AR Transaction'),
  645. type => 'submit',
  646. class => 'submit',
  647. order => $i++};
  648. $button{'AR--Sales Invoice'} = {
  649. name => 'action',
  650. value => 'sales_invoice_',
  651. text => $locale->text('Sales Invoice'),
  652. type => 'submit',
  653. class => 'submit',
  654. order => $i++};
  655. }
  656. if ( $myconfig{acs} !~ /AP--AP/ ) {
  657. $button{'AP--Add Transaction'} = {
  658. name => 'action',
  659. value => 'ap_transaction',
  660. text => $locale->text('AP Transaction'),
  661. type => 'submit',
  662. class => 'submit',
  663. order => $i++};
  664. $button{'AP--Vendor Invoice'} = {
  665. name => 'action',
  666. value => 'vendor_invoice_',
  667. text => $locale->text('Vendor Invoice'),
  668. type => 'submit',
  669. class => 'submit',
  670. order => $i++};
  671. }
  672. foreach $item ( split /;/, $myconfig{acs} ) {
  673. delete $button{$item};
  674. }
  675. my @buttons;
  676. foreach my $item ( sort { $a->{order} <=> $b->{order} } %button ) {
  677. push @buttons, $item if ref $item;
  678. }
  679. push @buttons, {
  680. name => 'action',
  681. value => 'csv_gl_report',
  682. text => $locale->text('CSV Report'),
  683. type => 'submit',
  684. class => 'submit',
  685. };
  686. push @buttons, {
  687. name => 'action',
  688. value => 'csv_email_gl_report',
  689. text => $locale->text('Email CSV Report'),
  690. type => 'submit',
  691. class => 'submit',
  692. };
  693. ##SC: Taking this out for now...
  694. ## if ( $form->{lynx} ) {
  695. ## require "bin/menu.pl";
  696. ## &menubar;
  697. ## }
  698. my %row_alignment = (
  699. 'balance' => 'right',
  700. 'debit' => 'right',
  701. 'credit' => 'right'
  702. );
  703. my $template;
  704. my $format = uc substr($form->{action}, 0, 3);
  705. my $template = LedgerSMB::Template->new(
  706. user => \%myconfig,
  707. locale => $locale,
  708. path => 'UI',
  709. template => 'form-dynatable',
  710. format => ($format ne 'CSV')? 'HTML': 'CSV',
  711. output_options => $output_options,
  712. );
  713. $template->{method} = 'email' if $output_options;
  714. $template->render({
  715. form => \%$form,
  716. buttons => \@buttons,
  717. hiddens => \%hiddens,
  718. options => \@options,
  719. columns => \@column_index,
  720. heading => \%column_header,
  721. rows => \@rows,
  722. row_alignment => \%row_alignment,
  723. totals => \%column_data,
  724. });
  725. $form->info($locale->text('GL report sent to [_1]', $form->{login}));
  726. }
  727. sub csv_gl_report { &generate_report }
  728. sub csv_email_gl_report {
  729. ##SC: XXX hardcoded test values
  730. &generate_report({
  731. to => 'seneca@localhost',
  732. from => 'seneca@localhost',
  733. subject => 'CSV GL report',
  734. });
  735. }
  736. sub gl_subtotal_tt {
  737. my %column_data;
  738. $subtotaldebit =
  739. $form->format_amount( \%myconfig, $subtotaldebit, 2, " " );
  740. $subtotalcredit =
  741. $form->format_amount( \%myconfig, $subtotalcredit, 2, " " );
  742. for (@column_index) { $column_data{$_} = " " }
  743. $column_data{class} = 'subtotal';
  744. $column_data{debit} = $subtotaldebit;
  745. $column_data{credit} = $subtotalcredit;
  746. $subtotaldebit = 0;
  747. $subtotalcredit = 0;
  748. $sameitem = $ref->{ $form->{sort} };
  749. return \%column_data;
  750. }
  751. sub gl_subtotal {
  752. $subtotaldebit =
  753. $form->format_amount( \%myconfig, $subtotaldebit, 2, "&nbsp;" );
  754. $subtotalcredit =
  755. $form->format_amount( \%myconfig, $subtotalcredit, 2, "&nbsp;" );
  756. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  757. $column_data{debit} =
  758. "<th align=right class=listsubtotal>$subtotaldebit</td>";
  759. $column_data{credit} =
  760. "<th align=right class=listsubtotal>$subtotalcredit</td>";
  761. print "<tr class=listsubtotal>";
  762. for (@column_index) { print "$column_data{$_}\n" }
  763. print "</tr>";
  764. $subtotaldebit = 0;
  765. $subtotalcredit = 0;
  766. $sameitem = $ref->{ $form->{sort} };
  767. }
  768. sub update {
  769. if ( $form->{transdate} ne $form->{oldtransdate} ) {
  770. if ( $form->{selectprojectnumber} ) {
  771. $form->all_projects( \%myconfig, undef, $form->{transdate} );
  772. if ( @{ $form->{all_project} } ) {
  773. $form->{selectprojectnumber} = "<option>\n";
  774. for ( @{ $form->{all_project} } ) {
  775. $form->{selectprojectnumber} .=
  776. qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n|;
  777. }
  778. $form->{selectprojectnumber} =
  779. $form->escape( $form->{selectprojectnumber}, 1 );
  780. }
  781. }
  782. $form->{oldtransdate} = $form->{transdate};
  783. }
  784. @a = ();
  785. $count = 0;
  786. @flds = qw(accno debit credit projectnumber fx_transaction source memo);
  787. for $i ( 1 .. $form->{rowcount} ) {
  788. unless ( ( $form->{"debit_$i"} eq "" )
  789. && ( $form->{"credit_$i"} eq "" ) )
  790. {
  791. for (qw(debit credit)) {
  792. $form->{"${_}_$i"} =
  793. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  794. }
  795. push @a, {};
  796. $j = $#a;
  797. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  798. $count++;
  799. }
  800. }
  801. for $i ( 1 .. $count ) {
  802. $j = $i - 1;
  803. for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
  804. }
  805. for $i ( $count + 1 .. $form->{rowcount} ) {
  806. for (@flds) { delete $form->{"${_}_$i"} }
  807. }
  808. $form->{rowcount} = $count + 1;
  809. &display_form;
  810. }
  811. sub display_form {
  812. my ($init) = @_;
  813. &form_header;
  814. &display_rows($init);
  815. &form_footer;
  816. }
  817. sub display_rows {
  818. my ($init) = @_;
  819. $form->{selectprojectnumber} =
  820. $form->unescape( $form->{selectprojectnumber} )
  821. if $form->{selectprojectnumber};
  822. $form->{totaldebit} = 0;
  823. $form->{totalcredit} = 0;
  824. for $i ( 1 .. $form->{rowcount} ) {
  825. $source = qq|
  826. <td><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
  827. $memo = qq|
  828. <td><input name="memo_$i" value="$form->{"memo_$i"}"></td>|;
  829. if ($init) {
  830. $accno = qq|
  831. <td><select name="accno_$i">$form->{selectaccno}</select></td>|;
  832. if ( $form->{selectprojectnumber} ) {
  833. $project = qq|
  834. <td><select name="projectnumber_$i">$form->{selectprojectnumber}</select></td>|;
  835. }
  836. if ( $form->{transfer} ) {
  837. $fx_transaction = qq|
  838. <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
  839. |;
  840. }
  841. }
  842. else {
  843. $form->{totaldebit} += $form->{"debit_$i"};
  844. $form->{totalcredit} += $form->{"credit_$i"};
  845. for (qw(debit credit)) {
  846. $form->{"${_}_$i"} =
  847. ( $form->{"${_}_$i"} )
  848. ? $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 )
  849. : "";
  850. }
  851. if ( $i < $form->{rowcount} ) {
  852. $accno = qq|<td>$form->{"accno_$i"}</td>|;
  853. if ( $form->{selectprojectnumber} ) {
  854. $form->{"projectnumber_$i"} = ""
  855. if $form->{selectprojectnumber} !~
  856. /$form->{"projectnumber_$i"}/;
  857. $project = $form->{"projectnumber_$i"};
  858. $project =~ s/--.*//;
  859. $project = qq|<td>$project</td>|;
  860. }
  861. if ( $form->{transfer} ) {
  862. $checked = ( $form->{"fx_transaction_$i"} ) ? "1" : "";
  863. $x = ($checked) ? "x" : "";
  864. $fx_transaction = qq|
  865. <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
  866. |;
  867. }
  868. $form->hide_form( "accno_$i", "projectnumber_$i" );
  869. }
  870. else {
  871. $accno = qq|
  872. <td><select name="accno_$i">$form->{selectaccno}</select></td>|;
  873. if ( $form->{selectprojectnumber} ) {
  874. $project = qq|
  875. <td><select name="projectnumber_$i">$form->{selectprojectnumber}</select></td>|;
  876. }
  877. if ( $form->{transfer} ) {
  878. $fx_transaction = qq|
  879. <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
  880. |;
  881. }
  882. }
  883. }
  884. print qq|<tr valign=top>
  885. $accno
  886. $fx_transaction
  887. <td><input name="debit_$i" size=12 value="$form->{"debit_$i"}" accesskey=$i></td>
  888. <td><input name="credit_$i" size=12 value=$form->{"credit_$i"}></td>
  889. $source
  890. $memo
  891. $project
  892. </tr>
  893. |;
  894. }
  895. $form->hide_form(qw(rowcount selectaccno pos_adjust));
  896. print qq|
  897. <input type=hidden name=selectprojectnumber value="|
  898. . $form->escape( $form->{selectprojectnumber}, 1 ) . qq|">|;
  899. }
  900. sub form_header {
  901. $title = $form->{title};
  902. if ( $form->{transfer} ) {
  903. $form->{title} = $locale->text("$title Cash Transfer Transaction");
  904. }
  905. else {
  906. $form->{title} = $locale->text("$title General Ledger Transaction");
  907. }
  908. # $locale->text('Add Cash Transfer Transaction')
  909. # $locale->text('Edit Cash Transfer Transaction')
  910. # $locale->text('Add General Ledger Transaction')
  911. # $locale->text('Edit General Ledger Transaction')
  912. $form->{selectdepartment} = $form->unescape( $form->{selectdepartment} );
  913. $form->{selectdepartment} =~ s/ selected//;
  914. $form->{selectdepartment} =~
  915. s/(<option value="\Q$form->{department}\E")/$1 selected/;
  916. for (qw(reference description notes)) {
  917. $form->{$_} = $form->quote( $form->{$_} );
  918. }
  919. if ( ( $rows = $form->numtextrows( $form->{description}, 50 ) ) > 1 ) {
  920. $description =
  921. qq|<textarea name=description rows=$rows cols=50 wrap=soft>$form->{description}</textarea>|;
  922. }
  923. else {
  924. $description =
  925. qq|<input name=description size=50 value="$form->{description}">|;
  926. }
  927. if ( ( $rows = $form->numtextrows( $form->{notes}, 50 ) ) > 1 ) {
  928. $notes =
  929. qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
  930. }
  931. else {
  932. $notes = qq|<input name=notes size=50 value="$form->{notes}">|;
  933. }
  934. if (!defined $form->{approved}){
  935. $form->{approved} = '1';
  936. }
  937. $department = qq|
  938. <tr>
  939. <th align=right nowrap>| . $locale->text('Department') . qq|</th>
  940. <td><select name=department>$form->{selectdepartment}</select></td>
  941. <input type=hidden name=selectdepartment value="|
  942. . $form->escape( $form->{selectdepartment}, 1 ) . qq|">
  943. </tr>
  944. | if $form->{selectdepartment};
  945. $project = qq|
  946. <th class=listheading>| . $locale->text('Project') . qq|</th>
  947. | if $form->{selectprojectnumber};
  948. if ( $form->{transfer} ) {
  949. $fx_transaction = qq|
  950. <th class=listheading>| . $locale->text('FX') . qq|</th>
  951. |;
  952. }
  953. $focus = ( $form->{focus} ) ? $form->{focus} : "debit_$form->{rowcount}";
  954. $form->header;
  955. print qq|
  956. <body onload="document.forms[0].${focus}.focus()" />
  957. <form method=post action=$form->{script}>
  958. <input type="hidden" name="approved" value="$form->{approved}" />
  959. |;
  960. $form->hide_form(
  961. qw(batch_id id transfer selectaccno closedto locked oldtransdate recurring));
  962. print qq|
  963. <input type=hidden name=title value="$title">
  964. <table width=100%>
  965. <tr>
  966. <th class=listtop>$form->{title}</th>
  967. </tr>
  968. <tr height="5"></tr>
  969. <tr>
  970. <td>
  971. <table>
  972. <tr>
  973. <th align=right>| . $locale->text('Reference') . qq|</th>
  974. <td><input name=reference size=20 value="$form->{reference}"></td>
  975. <th align=right>| . $locale->text('Date') . qq|</th>
  976. <td><input class="date" name=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
  977. </tr>
  978. $department
  979. <tr>
  980. <th align=right>| . $locale->text('Description') . qq|</th>
  981. <td colspan=3>$description</td>
  982. </tr>
  983. <tr>
  984. <th align=right>| . $locale->text('Notes') . qq|</th>
  985. <td colspan=3>$notes</td>
  986. </tr>
  987. </table>
  988. </td>
  989. </tr>
  990. <tr>
  991. <td>
  992. <table width=100%>
  993. <tr class=listheading>
  994. <th class=listheading>| . $locale->text('Account') . qq|</th>
  995. $fx_transaction
  996. <th class=listheading>| . $locale->text('Debit') . qq|</th>
  997. <th class=listheading>| . $locale->text('Credit') . qq|</th>
  998. <th class=listheading>| . $locale->text('Source') . qq|</th>
  999. <th class=listheading>| . $locale->text('Memo') . qq|</th>
  1000. $project
  1001. </tr>
  1002. |;
  1003. }
  1004. sub form_footer {
  1005. for (qw(totaldebit totalcredit)) {
  1006. $form->{$_} =
  1007. $form->format_amount( \%myconfig, $form->{$_}, 2, "&nbsp;" );
  1008. }
  1009. $project = qq|
  1010. <th>&nbsp;</th>
  1011. | if $form->{selectprojectnumber};
  1012. if ( $form->{transfer} ) {
  1013. $fx_transaction = qq|
  1014. <th>&nbsp;</th>
  1015. |;
  1016. }
  1017. print qq|
  1018. <tr class=listtotal>
  1019. <th>&nbsp;</th>
  1020. $fx_transaction
  1021. <th class=listtotal align=right>$form->{totaldebit}</th>
  1022. <th class=listtotal align=right>$form->{totalcredit}</th>
  1023. <th>&nbsp;</th>
  1024. <th>&nbsp;</th>
  1025. $project
  1026. </tr>
  1027. </table>
  1028. </td>
  1029. </tr>
  1030. <tr>
  1031. <td><hr size=3 noshade></td>
  1032. </tr>
  1033. </table>
  1034. |;
  1035. $form->hide_form(qw(path login sessionid callback));
  1036. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  1037. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  1038. # type=submit $locale->text('Update')
  1039. # type=submit $locale->text('Post')
  1040. # type=submit $locale->text('Schedule')
  1041. # type=submit $locale->text('Post as new')
  1042. # type=submit $locale->text('Delete')
  1043. if ( !$form->{readonly} ) {
  1044. %button = (
  1045. 'update' =>
  1046. { ndx => 1, key => 'U', value => $locale->text('Update') },
  1047. 'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  1048. 'post_as_new' =>
  1049. { ndx => 6, key => 'N', value => $locale->text('Post as new') },
  1050. 'schedule' =>
  1051. { ndx => 7, key => 'H', value => $locale->text('Schedule') },
  1052. 'delete' =>
  1053. { ndx => 8, key => 'D', value => $locale->text('Delete') },
  1054. );
  1055. %a = ();
  1056. if ( $form->{id} ) {
  1057. for ( 'update', 'post_as_new', 'schedule' ) { $a{$_} = 1 }
  1058. if ( !$form->{locked} ) {
  1059. if ( $transdate > $closedto ) {
  1060. for ( 'post', 'delete' ) { $a{$_} = 1 }
  1061. }
  1062. }
  1063. }
  1064. else {
  1065. if ( $transdate > $closedto ) {
  1066. for ( "update", "post", "schedule" ) { $a{$_} = 1 }
  1067. }
  1068. }
  1069. for ( keys %button ) { delete $button{$_} if !$a{$_} }
  1070. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  1071. {
  1072. $form->print_button( \%button, $_ );
  1073. }
  1074. }
  1075. if ( $form->{recurring} ) {
  1076. print qq|<div align=right>| . $locale->text('Scheduled') . qq|</div>|;
  1077. }
  1078. if ( $form->{lynx} ) {
  1079. require "bin/menu.pl";
  1080. &menubar;
  1081. }
  1082. print qq|
  1083. </form>
  1084. </body>
  1085. </html>
  1086. |;
  1087. }
  1088. sub delete {
  1089. my %hiddens;
  1090. delete $form->{action};
  1091. foreach (keys %$form) {
  1092. $hiddens{$_} = $form->{$_} unless ref $form->{$_};
  1093. }
  1094. $form->{title} = $locale->text('Confirm!');
  1095. my $query = $locale->text(
  1096. 'Are you sure you want to delete Transaction [_1]',
  1097. $form->{reference} );
  1098. my @buttons = ({
  1099. name => 'action',
  1100. value => 'delete_transaction',
  1101. text => $locale->text('Yes'),
  1102. });
  1103. my $template = LedgerSMB::Template->new_UI(
  1104. user => \%myconfig,
  1105. locale => $locale,
  1106. template => 'form-confirmation',
  1107. );
  1108. $template->render({
  1109. form => $form,
  1110. query => $query,
  1111. hiddens => \%hiddens,
  1112. buttons => \@buttons,
  1113. });
  1114. }
  1115. sub delete_transaction {
  1116. if ( GL->delete_transaction( \%myconfig, \%$form ) ) {
  1117. $form->redirect( $locale->text('Transaction deleted!') );
  1118. }
  1119. else {
  1120. $form->error( $locale->text('Cannot delete transaction!') );
  1121. }
  1122. }
  1123. sub post {
  1124. $form->isblank( "transdate", $locale->text('Transaction Date missing!') );
  1125. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  1126. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  1127. $form->error(
  1128. $locale->text('Cannot post transaction for a closed period!') )
  1129. if ( $transdate <= $closedto );
  1130. # add up debits and credits
  1131. for $i ( 1 .. $form->{rowcount} ) {
  1132. $dr = $form->parse_amount( \%myconfig, $form->{"debit_$i"} );
  1133. $cr = $form->parse_amount( \%myconfig, $form->{"credit_$i"} );
  1134. if ( $dr && $cr ) {
  1135. $form->error(
  1136. $locale->text(
  1137. 'Cannot post transaction with a debit and credit entry for the same account!'
  1138. )
  1139. );
  1140. }
  1141. $debit += $dr;
  1142. $credit += $cr;
  1143. }
  1144. if ( $form->round_amount( $debit, 2 ) != $form->round_amount( $credit, 2 ) )
  1145. {
  1146. $form->error( $locale->text('Out of balance transaction!') );
  1147. }
  1148. if ( !$form->{repost} ) {
  1149. if ( $form->{id} ) {
  1150. &repost;
  1151. exit;
  1152. }
  1153. }
  1154. if ( GL->post_transaction( \%myconfig, \%$form ) ) {
  1155. $form->redirect( $locale->text('Transaction posted!') );
  1156. }
  1157. else {
  1158. $form->error( $locale->text('Cannot post transaction!') );
  1159. }
  1160. }