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