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