summaryrefslogtreecommitdiff
path: root/bin/gl.pl
blob: adc7bafecdf57410111b517175bddf28f5fda7e2 (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. $form->{sort} = "transdate" unless $form->{sort};
  375. $form->{amountfrom} = $form->parse_amount(\%myconfig, $form->{amountfrom});
  376. $form->{amountto} = $form->parse_amount(\%myconfig, $form->{amountto});
  377. GL->all_transactions( \%myconfig, \%$form );
  378. $href =
  379. "$form->{script}?action=generate_report&direction=$form->{direction}&oldsort=$form->{oldsort}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  380. $form->sort_order();
  381. $callback =
  382. "$form->{script}?action=generate_report&direction=$form->{direction}&oldsort=$form->{oldsort}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
  383. my %hiddens = (
  384. 'action' => 'generate_report',
  385. 'direction' => $form->{direction},
  386. 'oldsort' => $form->{oldsort},
  387. 'path' => $form->{path},
  388. 'login' => $form->{login},
  389. 'sessionid' => $form->{sessionid},
  390. );
  391. %acctype = (
  392. 'A' => $locale->text('Asset'),
  393. 'L' => $locale->text('Liability'),
  394. 'Q' => $locale->text('Equity'),
  395. 'I' => $locale->text('Income'),
  396. 'E' => $locale->text('Expense'),
  397. );
  398. my @options;
  399. $form->{title} = $locale->text('General Ledger');
  400. $ml = ( $form->{category} =~ /(A|E)/ ) ? -1 : 1;
  401. unless ( $form->{category} eq 'X' ) {
  402. $form->{title} .=
  403. " : " . $locale->text( $acctype{ $form->{category} } );
  404. }
  405. if ( $form->{accno} ) {
  406. $href .= "&accno=" . $form->escape( $form->{accno} );
  407. $callback .= "&accno=" . $form->escape( $form->{accno}, 1 );
  408. $hiddens{accno} = $form->{accno};
  409. push @options, $locale->text('Account')
  410. . " : $form->{accno} $form->{account_description}";
  411. }
  412. if ( $form->{gifi_accno} ) {
  413. $href .= "&gifi_accno=" . $form->escape( $form->{gifi_accno} );
  414. $callback .= "&gifi_accno=" . $form->escape( $form->{gifi_accno}, 1 );
  415. $hiddens{gifi_accno} = $form->{gifi_accno};
  416. push @options, $locale->text('GIFI')
  417. . " : $form->{gifi_accno} $form->{gifi_account_description}";
  418. }
  419. if ( $form->{source} ) {
  420. $href .= "&source=" . $form->escape( $form->{source} );
  421. $callback .= "&source=" . $form->escape( $form->{source}, 1 );
  422. $hiddens{source} = $form->{source};
  423. push @options, $locale->text('Source') . " : $form->{source}";
  424. }
  425. if ( $form->{memo} ) {
  426. $href .= "&memo=" . $form->escape( $form->{memo} );
  427. $callback .= "&memo=" . $form->escape( $form->{memo}, 1 );
  428. $hiddens{memo} = $form->{memo};
  429. push @options, $locale->text('Memo') . " : $form->{memo}";
  430. }
  431. if ( $form->{reference} ) {
  432. $href .= "&reference=" . $form->escape( $form->{reference} );
  433. $callback .= "&reference=" . $form->escape( $form->{reference}, 1 );
  434. $hiddens{reference} = $form->{reference};
  435. push @options, $locale->text('Reference') . " : $form->{reference}";
  436. }
  437. if ( $form->{department} ) {
  438. $href .= "&department=" . $form->escape( $form->{department} );
  439. $callback .= "&department=" . $form->escape( $form->{department}, 1 );
  440. $hiddens{department} = $form->{department};
  441. ($department) = split /--/, $form->{department};
  442. push @options, $locale->text('Department') . " : $department";
  443. }
  444. if ( $form->{description} ) {
  445. $href .= "&description=" . $form->escape( $form->{description} );
  446. $callback .= "&description=" . $form->escape( $form->{description}, 1 );
  447. $hiddens{description} = $form->{description};
  448. push @options, $locale->text('Description') . " : $form->{description}";
  449. }
  450. if ( $form->{notes} ) {
  451. $href .= "&notes=" . $form->escape( $form->{notes} );
  452. $callback .= "&notes=" . $form->escape( $form->{notes}, 1 );
  453. $hiddens{notes} = $form->{notes};
  454. push @options, $locale->text('Notes') . " : $form->{notes}";
  455. }
  456. if ( $form->{datefrom} ) {
  457. $href .= "&datefrom=$form->{datefrom}";
  458. $callback .= "&datefrom=$form->{datefrom}";
  459. $hiddens{datefrom} = $form->{datefrom};
  460. push @options, $locale->text('From') . " "
  461. . $locale->date( \%myconfig, $form->{datefrom}, 1 );
  462. }
  463. if ( $form->{dateto} ) {
  464. $href .= "&dateto=$form->{dateto}";
  465. $callback .= "&dateto=$form->{dateto}";
  466. $hiddens{dateto} = $form->{dateto};
  467. my $option = $locale->text('To') . " "
  468. . $locale->date( \%myconfig, $form->{dateto}, 1 );
  469. if ( $form->{datefrom} ) {
  470. $options[$#options] .= " $option";
  471. }
  472. else {
  473. push @options, $option;
  474. }
  475. }
  476. if ( $form->{amountfrom} ) {
  477. $href .= "&amountfrom=$form->{amountfrom}";
  478. $callback .= "&amountfrom=$form->{amountfrom}";
  479. $hiddens{amountfrom} = $form->{amountfrom};
  480. push @options, $locale->text('Amount') . " >= "
  481. . $form->format_amount( \%myconfig, $form->{amountfrom}, 2 );
  482. }
  483. if ( $form->{amountto} ) {
  484. $href .= "&amountto=$form->{amountto}";
  485. $callback .= "&amountto=$form->{amountto}";
  486. $hiddens{amountto} = $form->{amountto};
  487. my $option .= $form->format_amount( \%myconfig, $form->{amountto}, 2 );
  488. if ( $form->{amountfrom} ) {
  489. $options[$#options] .= " <= $option";
  490. }
  491. else {
  492. push @options, $locale->text('Amount') . " <= $option";
  493. }
  494. }
  495. @columns =
  496. $form->sort_columns(
  497. qw(transdate id reference description notes source memo debit credit accno gifi_accno department)
  498. );
  499. pop @columns if $form->{department};
  500. if ( $form->{link} =~ /_paid/ ) {
  501. @columns =
  502. $form->sort_columns(
  503. qw(transdate id reference description notes source memo cleared debit credit accno gifi_accno)
  504. );
  505. $form->{l_cleared} = "Y";
  506. }
  507. if ( $form->{accno} || $form->{gifi_accno} ) {
  508. @columns = grep !/(accno|gifi_accno)/, @columns;
  509. push @columns, "balance";
  510. $form->{l_balance} = "Y";
  511. }
  512. foreach $item (@columns) {
  513. if ( $form->{"l_$item"} eq "Y" ) {
  514. push @column_index, $item;
  515. # add column to href and callback
  516. $callback .= "&l_$item=Y";
  517. $href .= "&l_$item=Y";
  518. $hiddens{"l_$item"} = 'Y';
  519. }
  520. }
  521. if ( $form->{l_subtotal} eq 'Y' ) {
  522. $callback .= "&l_subtotal=Y";
  523. $href .= "&l_subtotal=Y";
  524. $hiddens{l_subtotal} = 'Y';
  525. }
  526. $callback .= "&category=$form->{category}";
  527. $href .= "&category=$form->{category}";
  528. $hiddens{category} = $form->{category};
  529. $column_header{id} =
  530. {text => $locale->text('ID'), href=> "$href&sort=id"};
  531. $column_header{transdate} =
  532. {text => $locale->text('Date'), href=> "$href&sort=transdate"};
  533. $column_header{reference} =
  534. {text => $locale->text('Reference'), href=> "$href&sort=reference"};
  535. $column_header{source} =
  536. {text => $locale->text('Source'), href=> "$href&sort=source"};
  537. $column_header{memo} =
  538. {text => $locale->text('Memo'), href=> "$href&sort=memo"};
  539. $column_header{description} =
  540. {text => $locale->text('Description'), href=> "$href&sort=description"};
  541. $column_header{department} =
  542. {text => $locale->text('Department'), href=> "$href&sort=department"};
  543. $column_header{notes} = $locale->text('Notes');
  544. $column_header{debit} = $locale->text('Debit');
  545. $column_header{credit} = $locale->text('Credit');
  546. $column_header{accno} =
  547. {text => $locale->text('Account'), href=> "$href&sort=accno"};
  548. $column_header{gifi_accno} =
  549. {text => $locale->text('GIFI'), href=> "$href&sort=gifi_accno"};
  550. $column_header{balance} = $locale->text('Balance');
  551. $column_header{cleared} = $locale->text('R');
  552. # add sort to callback
  553. $form->{callback} = "$callback&sort=$form->{sort}";
  554. $callback = $form->escape( $form->{callback} );
  555. $hiddens{sort} = $form->{sort};
  556. $hiddens{callback} = $form->{callback};
  557. $cml = 1;
  558. # initial item for subtotals
  559. if ( @{ $form->{GL} } ) {
  560. $sameitem = $form->{GL}->[0]->{ $form->{sort} };
  561. $cml = -1 if $form->{contra};
  562. }
  563. my @rows;
  564. if ( ( $form->{accno} || $form->{gifi_accno} ) && $form->{balance} ) {
  565. my %column_data;
  566. for (@column_index) { $column_data{$_} = " " }
  567. $column_data{balance} =
  568. $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml,
  569. 2, 0 );
  570. $column_data{i} = 1;
  571. push @rows, \%column_data;
  572. }
  573. # reverse href
  574. $direction = ( $form->{direction} eq 'ASC' ) ? "ASC" : "DESC";
  575. $form->sort_order();
  576. $href =~ s/direction=$form->{direction}/direction=$direction/;
  577. my $i = 0;
  578. foreach $ref ( @{ $form->{GL} } ) {
  579. my %column_data;
  580. # if item ne sort print subtotal
  581. if ( $form->{l_subtotal} eq 'Y' ) {
  582. if ( $sameitem ne $ref->{ $form->{sort} } ) {
  583. push @rows, &gl_subtotal_tt();
  584. }
  585. }
  586. $form->{balance} += $ref->{amount};
  587. $subtotaldebit += $ref->{debit};
  588. $subtotalcredit += $ref->{credit};
  589. $totaldebit += $ref->{debit};
  590. $totalcredit += $ref->{credit};
  591. $ref->{debit} =
  592. $form->format_amount( \%myconfig, $ref->{debit}, 2, " " );
  593. $ref->{credit} =
  594. $form->format_amount( \%myconfig, $ref->{credit}, 2, " " );
  595. for (qw(id transdate)) { $column_data{$_} = "$ref->{$_}" }
  596. $column_data{reference} =
  597. {href => "$ref->{module}.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback",
  598. text => $ref->{reference}};
  599. #$ref->{notes} =~ s/\r?\n/<br>/g;
  600. for (qw(description source memo notes department)) {
  601. $column_data{$_} = "$ref->{$_} ";
  602. }
  603. $column_data{debit} = "$ref->{debit}";
  604. $column_data{credit} = "$ref->{credit}";
  605. $column_data{accno} =
  606. {href => "$href&accno=$ref->{accno}&callback=$callback",
  607. text => "$ref->{accno} $ref->{accname}"};
  608. $column_data{gifi_accno} =
  609. {href => "$href&gifi_accno=$ref->{gifi_accno}&callback=$callback",
  610. text => $ref->{gifi_accno}};
  611. $column_data{balance} = $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml,
  612. 2, 0 );
  613. $column_data{cleared} =
  614. ( $ref->{cleared} ) ? "*" : " ";
  615. if ( $ref->{id} != $sameid ) {
  616. $i++;
  617. $i %= 2;
  618. }
  619. $column_data{'i'} = $i;
  620. push @rows, \%column_data;
  621. $sameid = $ref->{id};
  622. }
  623. push @rows, &gl_subtotal_tt() if ( $form->{l_subtotal} eq 'Y' );
  624. for (@column_index) { $column_data{$_} = " " }
  625. $column_data{debit} = $form->format_amount( \%myconfig, $totaldebit, 2, " " );
  626. $column_data{credit} = $form->format_amount( \%myconfig, $totalcredit, 2, " " );
  627. $column_data{balance} = $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, 2, 0 );
  628. $i = 1;
  629. my %button;
  630. if ( $myconfig{acs} !~ /General Ledger--General Ledger/ ) {
  631. $button{'General Ledger--Add Transaction'} = {
  632. name => 'action',
  633. value => 'gl_transaction',
  634. text => $locale->text('GL Transaction'),
  635. type => 'submit',
  636. class => 'submit',
  637. order => $i++};
  638. }
  639. if ( $myconfig{acs} !~ /AR--AR/ ) {
  640. $button{'AR--Add Transaction'} = {
  641. name => 'action',
  642. value => 'ar_transaction',
  643. text => $locale->text('AR Transaction'),
  644. type => 'submit',
  645. class => 'submit',
  646. order => $i++};
  647. $button{'AR--Sales Invoice'} = {
  648. name => 'action',
  649. value => 'sales_invoice_',
  650. text => $locale->text('Sales Invoice'),
  651. type => 'submit',
  652. class => 'submit',
  653. order => $i++};
  654. }
  655. if ( $myconfig{acs} !~ /AP--AP/ ) {
  656. $button{'AP--Add Transaction'} = {
  657. name => 'action',
  658. value => 'ap_transaction',
  659. text => $locale->text('AP Transaction'),
  660. type => 'submit',
  661. class => 'submit',
  662. order => $i++};
  663. $button{'AP--Vendor Invoice'} = {
  664. name => 'action',
  665. value => 'vendor_invoice_',
  666. text => $locale->text('Vendor Invoice'),
  667. type => 'submit',
  668. class => 'submit',
  669. order => $i++};
  670. }
  671. foreach $item ( split /;/, $myconfig{acs} ) {
  672. delete $button{$item};
  673. }
  674. my @buttons;
  675. foreach my $item ( sort { $a->{order} <=> $b->{order} } %button ) {
  676. push @buttons, $item if ref $item;
  677. }
  678. push @buttons, {
  679. name => 'action',
  680. value => 'csv_gl_report',
  681. text => $locale->text('CSV Report'),
  682. type => 'submit',
  683. class => 'submit',
  684. };
  685. ##SC: Taking this out for now...
  686. ## if ( $form->{lynx} ) {
  687. ## require "bin/menu.pl";
  688. ## &menubar;
  689. ## }
  690. my %row_alignment = (
  691. 'balance' => 'right',
  692. 'debit' => 'right',
  693. 'credit' => 'right'
  694. );
  695. my $template;
  696. my $format = uc substr($form->{action}, 0, 3);
  697. my $template = LedgerSMB::Template->new(
  698. user => \%myconfig,
  699. locale => $locale,
  700. path => 'UI',
  701. template => 'form-dynatable',
  702. format => ($format ne 'CSV')? 'HTML': 'CSV');
  703. $template->render({
  704. form => \%$form,
  705. buttons => \@buttons,
  706. hiddens => \%hiddens,
  707. options => \@options,
  708. columns => \@column_index,
  709. heading => \%column_header,
  710. rows => \@rows,
  711. row_alignment => \%row_alignment,
  712. totals => \%column_data,
  713. });
  714. }
  715. sub csv_gl_report { &generate_report }
  716. sub gl_subtotal_tt {
  717. my %column_data;
  718. $subtotaldebit =
  719. $form->format_amount( \%myconfig, $subtotaldebit, 2, " " );
  720. $subtotalcredit =
  721. $form->format_amount( \%myconfig, $subtotalcredit, 2, " " );
  722. for (@column_index) { $column_data{$_} = " " }
  723. $column_data{class} = 'subtotal';
  724. $column_data{debit} = $subtotaldebit;
  725. $column_data{credit} = $subtotalcredit;
  726. $subtotaldebit = 0;
  727. $subtotalcredit = 0;
  728. $sameitem = $ref->{ $form->{sort} };
  729. return \%column_data;
  730. }
  731. sub gl_subtotal {
  732. $subtotaldebit =
  733. $form->format_amount( \%myconfig, $subtotaldebit, 2, "&nbsp;" );
  734. $subtotalcredit =
  735. $form->format_amount( \%myconfig, $subtotalcredit, 2, "&nbsp;" );
  736. for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
  737. $column_data{debit} =
  738. "<th align=right class=listsubtotal>$subtotaldebit</td>";
  739. $column_data{credit} =
  740. "<th align=right class=listsubtotal>$subtotalcredit</td>";
  741. print "<tr class=listsubtotal>";
  742. for (@column_index) { print "$column_data{$_}\n" }
  743. print "</tr>";
  744. $subtotaldebit = 0;
  745. $subtotalcredit = 0;
  746. $sameitem = $ref->{ $form->{sort} };
  747. }
  748. sub update {
  749. if ( $form->{transdate} ne $form->{oldtransdate} ) {
  750. if ( $form->{selectprojectnumber} ) {
  751. $form->all_projects( \%myconfig, undef, $form->{transdate} );
  752. if ( @{ $form->{all_project} } ) {
  753. $form->{selectprojectnumber} = "<option>\n";
  754. for ( @{ $form->{all_project} } ) {
  755. $form->{selectprojectnumber} .=
  756. qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n|;
  757. }
  758. $form->{selectprojectnumber} =
  759. $form->escape( $form->{selectprojectnumber}, 1 );
  760. }
  761. }
  762. $form->{oldtransdate} = $form->{transdate};
  763. }
  764. @a = ();
  765. $count = 0;
  766. @flds = qw(accno debit credit projectnumber fx_transaction source memo);
  767. for $i ( 1 .. $form->{rowcount} ) {
  768. unless ( ( $form->{"debit_$i"} eq "" )
  769. && ( $form->{"credit_$i"} eq "" ) )
  770. {
  771. for (qw(debit credit)) {
  772. $form->{"${_}_$i"} =
  773. $form->parse_amount( \%myconfig, $form->{"${_}_$i"} );
  774. }
  775. push @a, {};
  776. $j = $#a;
  777. for (@flds) { $a[$j]->{$_} = $form->{"${_}_$i"} }
  778. $count++;
  779. }
  780. }
  781. for $i ( 1 .. $count ) {
  782. $j = $i - 1;
  783. for (@flds) { $form->{"${_}_$i"} = $a[$j]->{$_} }
  784. }
  785. for $i ( $count + 1 .. $form->{rowcount} ) {
  786. for (@flds) { delete $form->{"${_}_$i"} }
  787. }
  788. $form->{rowcount} = $count + 1;
  789. &display_form;
  790. }
  791. sub display_form {
  792. my ($init) = @_;
  793. &form_header;
  794. &display_rows($init);
  795. &form_footer;
  796. }
  797. sub display_rows {
  798. my ($init) = @_;
  799. $form->{selectprojectnumber} =
  800. $form->unescape( $form->{selectprojectnumber} )
  801. if $form->{selectprojectnumber};
  802. $form->{totaldebit} = 0;
  803. $form->{totalcredit} = 0;
  804. for $i ( 1 .. $form->{rowcount} ) {
  805. $source = qq|
  806. <td><input name="source_$i" size=10 value="$form->{"source_$i"}"></td>|;
  807. $memo = qq|
  808. <td><input name="memo_$i" value="$form->{"memo_$i"}"></td>|;
  809. if ($init) {
  810. $accno = qq|
  811. <td><select name="accno_$i">$form->{selectaccno}</select></td>|;
  812. if ( $form->{selectprojectnumber} ) {
  813. $project = qq|
  814. <td><select name="projectnumber_$i">$form->{selectprojectnumber}</select></td>|;
  815. }
  816. if ( $form->{transfer} ) {
  817. $fx_transaction = qq|
  818. <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
  819. |;
  820. }
  821. }
  822. else {
  823. $form->{totaldebit} += $form->{"debit_$i"};
  824. $form->{totalcredit} += $form->{"credit_$i"};
  825. for (qw(debit credit)) {
  826. $form->{"${_}_$i"} =
  827. ( $form->{"${_}_$i"} )
  828. ? $form->format_amount( \%myconfig, $form->{"${_}_$i"}, 2 )
  829. : "";
  830. }
  831. if ( $i < $form->{rowcount} ) {
  832. $accno = qq|<td>$form->{"accno_$i"}</td>|;
  833. if ( $form->{selectprojectnumber} ) {
  834. $form->{"projectnumber_$i"} = ""
  835. if $form->{selectprojectnumber} !~
  836. /$form->{"projectnumber_$i"}/;
  837. $project = $form->{"projectnumber_$i"};
  838. $project =~ s/--.*//;
  839. $project = qq|<td>$project</td>|;
  840. }
  841. if ( $form->{transfer} ) {
  842. $checked = ( $form->{"fx_transaction_$i"} ) ? "1" : "";
  843. $x = ($checked) ? "x" : "";
  844. $fx_transaction = qq|
  845. <td><input type=hidden name="fx_transaction_$i" value="$checked">$x</td>
  846. |;
  847. }
  848. $form->hide_form( "accno_$i", "projectnumber_$i" );
  849. }
  850. else {
  851. $accno = qq|
  852. <td><select name="accno_$i">$form->{selectaccno}</select></td>|;
  853. if ( $form->{selectprojectnumber} ) {
  854. $project = qq|
  855. <td><select name="projectnumber_$i">$form->{selectprojectnumber}</select></td>|;
  856. }
  857. if ( $form->{transfer} ) {
  858. $fx_transaction = qq|
  859. <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1></td>
  860. |;
  861. }
  862. }
  863. }
  864. print qq|<tr valign=top>
  865. $accno
  866. $fx_transaction
  867. <td><input name="debit_$i" size=12 value="$form->{"debit_$i"}" accesskey=$i></td>
  868. <td><input name="credit_$i" size=12 value=$form->{"credit_$i"}></td>
  869. $source
  870. $memo
  871. $project
  872. </tr>
  873. |;
  874. }
  875. $form->hide_form(qw(rowcount selectaccno pos_adjust));
  876. print qq|
  877. <input type=hidden name=selectprojectnumber value="|
  878. . $form->escape( $form->{selectprojectnumber}, 1 ) . qq|">|;
  879. }
  880. sub form_header {
  881. $title = $form->{title};
  882. if ( $form->{transfer} ) {
  883. $form->{title} = $locale->text("$title Cash Transfer Transaction");
  884. }
  885. else {
  886. $form->{title} = $locale->text("$title General Ledger Transaction");
  887. }
  888. # $locale->text('Add Cash Transfer Transaction')
  889. # $locale->text('Edit Cash Transfer Transaction')
  890. # $locale->text('Add General Ledger Transaction')
  891. # $locale->text('Edit General Ledger Transaction')
  892. $form->{selectdepartment} = $form->unescape( $form->{selectdepartment} );
  893. $form->{selectdepartment} =~ s/ selected//;
  894. $form->{selectdepartment} =~
  895. s/(<option value="\Q$form->{department}\E")/$1 selected/;
  896. for (qw(reference description notes)) {
  897. $form->{$_} = $form->quote( $form->{$_} );
  898. }
  899. if ( ( $rows = $form->numtextrows( $form->{description}, 50 ) ) > 1 ) {
  900. $description =
  901. qq|<textarea name=description rows=$rows cols=50 wrap=soft>$form->{description}</textarea>|;
  902. }
  903. else {
  904. $description =
  905. qq|<input name=description size=50 value="$form->{description}">|;
  906. }
  907. if ( ( $rows = $form->numtextrows( $form->{notes}, 50 ) ) > 1 ) {
  908. $notes =
  909. qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
  910. }
  911. else {
  912. $notes = qq|<input name=notes size=50 value="$form->{notes}">|;
  913. }
  914. $department = qq|
  915. <tr>
  916. <th align=right nowrap>| . $locale->text('Department') . qq|</th>
  917. <td><select name=department>$form->{selectdepartment}</select></td>
  918. <input type=hidden name=selectdepartment value="|
  919. . $form->escape( $form->{selectdepartment}, 1 ) . qq|">
  920. </tr>
  921. | if $form->{selectdepartment};
  922. $project = qq|
  923. <th class=listheading>| . $locale->text('Project') . qq|</th>
  924. | if $form->{selectprojectnumber};
  925. if ( $form->{transfer} ) {
  926. $fx_transaction = qq|
  927. <th class=listheading>| . $locale->text('FX') . qq|</th>
  928. |;
  929. }
  930. $focus = ( $form->{focus} ) ? $form->{focus} : "debit_$form->{rowcount}";
  931. $form->header;
  932. print qq|
  933. <body onload="document.forms[0].${focus}.focus()" />
  934. <form method=post action=$form->{script}>
  935. |;
  936. $form->hide_form(
  937. qw(id transfer selectaccno closedto locked oldtransdate recurring));
  938. print qq|
  939. <input type=hidden name=title value="$title">
  940. <table width=100%>
  941. <tr>
  942. <th class=listtop>$form->{title}</th>
  943. </tr>
  944. <tr height="5"></tr>
  945. <tr>
  946. <td>
  947. <table>
  948. <tr>
  949. <th align=right>| . $locale->text('Reference') . qq|</th>
  950. <td><input name=reference size=20 value="$form->{reference}"></td>
  951. <th align=right>| . $locale->text('Date') . qq|</th>
  952. <td><input class="date" name=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
  953. </tr>
  954. $department
  955. <tr>
  956. <th align=right>| . $locale->text('Description') . qq|</th>
  957. <td colspan=3>$description</td>
  958. </tr>
  959. <tr>
  960. <th align=right>| . $locale->text('Notes') . qq|</th>
  961. <td colspan=3>$notes</td>
  962. </tr>
  963. </table>
  964. </td>
  965. </tr>
  966. <tr>
  967. <td>
  968. <table width=100%>
  969. <tr class=listheading>
  970. <th class=listheading>| . $locale->text('Account') . qq|</th>
  971. $fx_transaction
  972. <th class=listheading>| . $locale->text('Debit') . qq|</th>
  973. <th class=listheading>| . $locale->text('Credit') . qq|</th>
  974. <th class=listheading>| . $locale->text('Source') . qq|</th>
  975. <th class=listheading>| . $locale->text('Memo') . qq|</th>
  976. $project
  977. </tr>
  978. |;
  979. }
  980. sub form_footer {
  981. for (qw(totaldebit totalcredit)) {
  982. $form->{$_} =
  983. $form->format_amount( \%myconfig, $form->{$_}, 2, "&nbsp;" );
  984. }
  985. $project = qq|
  986. <th>&nbsp;</th>
  987. | if $form->{selectprojectnumber};
  988. if ( $form->{transfer} ) {
  989. $fx_transaction = qq|
  990. <th>&nbsp;</th>
  991. |;
  992. }
  993. print qq|
  994. <tr class=listtotal>
  995. <th>&nbsp;</th>
  996. $fx_transaction
  997. <th class=listtotal align=right>$form->{totaldebit}</th>
  998. <th class=listtotal align=right>$form->{totalcredit}</th>
  999. <th>&nbsp;</th>
  1000. <th>&nbsp;</th>
  1001. $project
  1002. </tr>
  1003. </table>
  1004. </td>
  1005. </tr>
  1006. <tr>
  1007. <td><hr size=3 noshade></td>
  1008. </tr>
  1009. </table>
  1010. |;
  1011. $form->hide_form(qw(path login sessionid callback));
  1012. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  1013. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  1014. # type=submit $locale->text('Update')
  1015. # type=submit $locale->text('Post')
  1016. # type=submit $locale->text('Schedule')
  1017. # type=submit $locale->text('Post as new')
  1018. # type=submit $locale->text('Delete')
  1019. if ( !$form->{readonly} ) {
  1020. %button = (
  1021. 'update' =>
  1022. { ndx => 1, key => 'U', value => $locale->text('Update') },
  1023. 'post' => { ndx => 3, key => 'O', value => $locale->text('Post') },
  1024. 'post_as_new' =>
  1025. { ndx => 6, key => 'N', value => $locale->text('Post as new') },
  1026. 'schedule' =>
  1027. { ndx => 7, key => 'H', value => $locale->text('Schedule') },
  1028. 'delete' =>
  1029. { ndx => 8, key => 'D', value => $locale->text('Delete') },
  1030. );
  1031. %a = ();
  1032. if ( $form->{id} ) {
  1033. for ( 'update', 'post_as_new', 'schedule' ) { $a{$_} = 1 }
  1034. if ( !$form->{locked} ) {
  1035. if ( $transdate > $closedto ) {
  1036. for ( 'post', 'delete' ) { $a{$_} = 1 }
  1037. }
  1038. }
  1039. }
  1040. else {
  1041. if ( $transdate > $closedto ) {
  1042. for ( "update", "post", "schedule" ) { $a{$_} = 1 }
  1043. }
  1044. }
  1045. for ( keys %button ) { delete $button{$_} if !$a{$_} }
  1046. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  1047. {
  1048. $form->print_button( \%button, $_ );
  1049. }
  1050. }
  1051. if ( $form->{recurring} ) {
  1052. print qq|<div align=right>| . $locale->text('Scheduled') . qq|</div>|;
  1053. }
  1054. if ( $form->{lynx} ) {
  1055. require "bin/menu.pl";
  1056. &menubar;
  1057. }
  1058. print qq|
  1059. </form>
  1060. </body>
  1061. </html>
  1062. |;
  1063. }
  1064. sub delete {
  1065. my %hiddens;
  1066. delete $form->{action};
  1067. foreach (keys %$form) {
  1068. $hiddens{$_} = $form->{$_} unless ref $form->{$_};
  1069. }
  1070. $form->{title} = $locale->text('Confirm!');
  1071. my $query = $locale->text(
  1072. 'Are you sure you want to delete Transaction [_1]',
  1073. $form->{reference} );
  1074. my @buttons = ({
  1075. name => 'action',
  1076. value => 'delete_transaction',
  1077. text => $locale->text('Yes'),
  1078. });
  1079. my $template = LedgerSMB::Template->new_UI(
  1080. user => \%myconfig,
  1081. locale => $locale,
  1082. template => 'form-confirmation',
  1083. );
  1084. $template->render({
  1085. form => $form,
  1086. query => $query,
  1087. hiddens => \%hiddens,
  1088. buttons => \@buttons,
  1089. });
  1090. }
  1091. sub delete_transaction {
  1092. if ( GL->delete_transaction( \%myconfig, \%$form ) ) {
  1093. $form->redirect( $locale->text('Transaction deleted!') );
  1094. }
  1095. else {
  1096. $form->error( $locale->text('Cannot delete transaction!') );
  1097. }
  1098. }
  1099. sub post {
  1100. $form->isblank( "transdate", $locale->text('Transaction Date missing!') );
  1101. $transdate = $form->datetonum( \%myconfig, $form->{transdate} );
  1102. $closedto = $form->datetonum( \%myconfig, $form->{closedto} );
  1103. $form->error(
  1104. $locale->text('Cannot post transaction for a closed period!') )
  1105. if ( $transdate <= $closedto );
  1106. # add up debits and credits
  1107. for $i ( 1 .. $form->{rowcount} ) {
  1108. $dr = $form->parse_amount( \%myconfig, $form->{"debit_$i"} );
  1109. $cr = $form->parse_amount( \%myconfig, $form->{"credit_$i"} );
  1110. if ( $dr && $cr ) {
  1111. $form->error(
  1112. $locale->text(
  1113. 'Cannot post transaction with a debit and credit entry for the same account!'
  1114. )
  1115. );
  1116. }
  1117. $debit += $dr;
  1118. $credit += $cr;
  1119. }
  1120. if ( $form->round_amount( $debit, 2 ) != $form->round_amount( $credit, 2 ) )
  1121. {
  1122. $form->error( $locale->text('Out of balance transaction!') );
  1123. }
  1124. if ( !$form->{repost} ) {
  1125. if ( $form->{id} ) {
  1126. &repost;
  1127. exit;
  1128. }
  1129. }
  1130. if ( GL->post_transaction( \%myconfig, \%$form ) ) {
  1131. $form->redirect( $locale->text('Transaction posted!') );
  1132. }
  1133. else {
  1134. $form->error( $locale->text('Cannot post transaction!') );
  1135. }
  1136. }