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