PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/chronique/GLAccountCSV.php

http://chronique.googlecode.com/
PHP | 265 lines | 218 code | 39 blank | 8 comment | 40 complexity | 955b98582034bc8f2ebf34570ade8511 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /* $Id: GLAccountCSV.php 4492 2011-02-18 09:56:52Z daintree $ */
  3. include ('includes/session.inc');
  4. $title = _('General Ledger Account Report');
  5. include('includes/header.inc');
  6. include('includes/GLPostings.inc');
  7. if (isset($_POST['Period'])){
  8. $SelectedPeriod = $_POST['Period'];
  9. } elseif (isset($_GET['Period'])){
  10. $SelectedPeriod = $_GET['Period'];
  11. }
  12. echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/transactions.png" title="' . _('General Ledger Account Inquiry') . '" alt="" />' . ' ' . _('General Ledger Account Report') . '</p>';
  13. echo '<div class="page_help_text">' . _('Use the keyboard Shift key to select multiple accounts and periods') . '</div><br />';
  14. echo '<form method="POST" action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '">';
  15. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  16. /*Dates in SQL format for the last day of last month*/
  17. $DefaultPeriodDate = Date ('Y-m-d', Mktime(0,0,0,Date('m'),0,Date('Y')));
  18. /*Show a form to allow input of criteria for the report */
  19. echo '<table>
  20. <tr>
  21. <td>'._('Selected Accounts') . ':</td>
  22. <td><select name="Account[]" multiple>';
  23. $sql = "SELECT accountcode, accountname FROM chartmaster ORDER BY accountcode";
  24. $AccountsResult = DB_query($sql,$db);
  25. $i=0;
  26. while ($myrow=DB_fetch_array($AccountsResult,$db)){
  27. if(isset($_POST['Account'][$i]) AND $myrow['accountcode'] == $_POST['Account'][$i]){
  28. echo '<option selected value="' . $myrow['accountcode'] . '">' . $myrow['accountcode'] . ' ' . $myrow['accountname'] . '</option>';
  29. $i++;
  30. } else {
  31. echo '<option value="' . $myrow['accountcode'] . '">' . $myrow['accountcode'] . ' ' . $myrow['accountname'] . '</option>';
  32. }
  33. }
  34. echo '</select></td>';
  35. echo '<td>'._('For Period range').':</td>
  36. <td><select name="Period[]" multiple>';
  37. $sql = "SELECT periodno, lastdate_in_period FROM periods ORDER BY periodno DESC";
  38. $Periods = DB_query($sql,$db);
  39. $id=0;
  40. while ($myrow=DB_fetch_array($Periods,$db)){
  41. if (isset($SelectedPeriod[$id]) and $myrow['periodno'] == $SelectedPeriod[$id]){
  42. echo '<option selected value="' . $myrow['periodno'] . '">' . _(MonthAndYearFromSQLDate($myrow['lastdate_in_period'])) . '</option>';
  43. $id++;
  44. } else {
  45. echo '<option value="' . $myrow['periodno'] . '">' . _(MonthAndYearFromSQLDate($myrow['lastdate_in_period'])) . '</option>';
  46. }
  47. }
  48. echo '</select></td></tr>';
  49. //Select the tag
  50. echo '<tr><td>' . _('Select Tag') . ':</td><td><select name="tag">';
  51. $SQL = "SELECT tagref,
  52. tagdescription
  53. FROM tags
  54. ORDER BY tagref";
  55. $result=DB_query($SQL,$db);
  56. echo '<option value=0>0 - '._('All tags');
  57. while ($myrow=DB_fetch_array($result)){
  58. if (isset($_POST['tag']) and $_POST['tag']==$myrow['tagref']){
  59. echo '<option selected value="' . $myrow['tagref'] . '">' . $myrow['tagref'].' - ' .$myrow['tagdescription'] . '</option>';
  60. } else {
  61. echo '<option value="' . $myrow['tagref'] . '">' . $myrow['tagref'].' - ' .$myrow['tagdescription'] . '</option>';
  62. }
  63. }
  64. echo '</select></td></tr>';
  65. // End select tag
  66. echo '</table><p>
  67. <div class="centre"><input type="submit" name="MakeCSV" value="'._('Make CSV File').'"></div>
  68. </form>';
  69. /* End of the Form rest of script is what happens if the show button is hit*/
  70. if (isset($_POST['MakeCSV'])){
  71. if (!isset($SelectedPeriod)){
  72. prnMsg(_('A period or range of periods must be selected from the list box'),'info');
  73. include('includes/footer.inc');
  74. exit;
  75. }
  76. if (!isset($_POST['Account'])){
  77. prnMsg(_('An account or range of accounts must be selected from the list box'),'info');
  78. include('includes/footer.inc');
  79. exit;
  80. }
  81. if (!file_exists($_SESSION['reports_dir'])){
  82. $Result = mkdir('./' . $_SESSION['reports_dir']);
  83. }
  84. $FileName = $_SESSION['reports_dir'] . '/Accounts_Listing_' . Date('Y-m-d') .'.csv';
  85. $fp = fopen($FileName,'w');
  86. if ($fp==FALSE){
  87. prnMsg(_('Could not open or create the file under') . ' ' . $FileName,'error');
  88. include('includes/footer.inc');
  89. exit;
  90. }
  91. foreach ($_POST['Account'] as $SelectedAccount){
  92. /*Is the account a balance sheet or a profit and loss account */
  93. $result = DB_query("SELECT chartmaster.accountname,
  94. accountgroups.pandl
  95. FROM accountgroups
  96. INNER JOIN chartmaster ON accountgroups.groupname=chartmaster.group_
  97. WHERE chartmaster.accountcode=$SelectedAccount",$db);
  98. $AccountDetailRow = DB_fetch_row($result);
  99. $AccountName = $AccountDetailRow[1];
  100. if ($AccountDetailRow[1]==1){
  101. $PandLAccount = True;
  102. }else{
  103. $PandLAccount = False; /*its a balance sheet account */
  104. }
  105. $FirstPeriodSelected = min($SelectedPeriod);
  106. $LastPeriodSelected = max($SelectedPeriod);
  107. if ($_POST['tag']==0) {
  108. $sql= "SELECT type,
  109. typename,
  110. gltrans.typeno,
  111. gltrans.trandate,
  112. gltrans.narrative,
  113. gltrans.amount,
  114. gltrans.periodno,
  115. gltrans.tag
  116. FROM gltrans, systypes
  117. WHERE gltrans.account = '" . $SelectedAccount . "'
  118. AND systypes.typeid=gltrans.type
  119. AND posted=1
  120. AND periodno>='" . $FirstPeriodSelected . "'
  121. AND periodno<='" . $LastPeriodSelected . "'
  122. ORDER BY periodno, gltrans.trandate, counterindex";
  123. } else {
  124. $sql= "SELECT gltrans.type,
  125. gltrans.typename,
  126. gltrans.typeno,
  127. gltrans.trandate,
  128. gltrans.narrative,
  129. gltrans.amount,
  130. gltrans.periodno,
  131. gltrans.tag
  132. FROM gltrans, systypes
  133. WHERE gltrans.account = '" . $SelectedAccount . "'
  134. AND systypes.typeid=gltrans.type
  135. AND posted=1
  136. AND periodno>='" . $FirstPeriodSelected . "'
  137. AND periodno<='" . $LastPeriodSelected . "'
  138. AND tag='".$_POST['tag']."'
  139. ORDER BY periodno, gltrans.trandate, counterindex";
  140. }
  141. $ErrMsg = _('The transactions for account') . ' ' . $SelectedAccount . ' ' . _('could not be retrieved because') ;
  142. $TransResult = DB_query($sql,$db,$ErrMsg);
  143. fwrite($fp, $SelectedAccount . ' - ' . $AccountName . ' ' . _('for period'). ' ' . $FirstPeriodSelected . ' ' . _('to') . ' ' . $LastPeriodSelected . "\n");
  144. if ($PandLAccount==True) {
  145. $RunningTotal = 0;
  146. } else {
  147. $sql = "SELECT bfwd,
  148. actual,
  149. period
  150. FROM chartdetails
  151. WHERE chartdetails.accountcode= '" . $SelectedAccount . "'
  152. AND chartdetails.period='" . $FirstPeriodSelected . "'";
  153. $ErrMsg = _('The chart details for account') . ' ' . $SelectedAccount . ' ' . _('could not be retrieved');
  154. $ChartDetailsResult = DB_query($sql,$db,$ErrMsg);
  155. $ChartDetailRow = DB_fetch_array($ChartDetailsResult);
  156. $RunningTotal =$ChartDetailRow['bfwd'];
  157. if ($RunningTotal < 0 ){
  158. fwrite($fp,$SelectedAccount . ', ' .$FirstPeriodSelected . ', ' . _('Brought Forward Balance') . ',,,,' . -$RunningTotal . "\n");
  159. } else {
  160. fwrite($fp,$SelectedAccount . ', ' .$FirstPeriodSelected . ', ' . _('Brought Forward Balance') . ',,,' . $RunningTotal . "\n");
  161. }
  162. }
  163. $PeriodTotal = 0;
  164. $PeriodNo = -9999;
  165. $j = 1;
  166. $k=0; //row colour counter
  167. while ($myrow=DB_fetch_array($TransResult)) {
  168. if ($myrow['periodno']!=$PeriodNo){
  169. if ($PeriodNo!=-9999){ //ie its not the first time around
  170. /*Get the ChartDetails balance b/fwd and the actual movement in the account for the period as recorded in the chart details - need to ensure integrity of transactions to the chart detail movements. Also, for a balance sheet account it is the balance carried forward that is important, not just the transactions*/
  171. $sql = "SELECT bfwd,
  172. actual,
  173. period
  174. FROM chartdetails
  175. WHERE chartdetails.accountcode= '" . $SelectedAccount . "'
  176. AND chartdetails.period='" . $PeriodNo . "'";
  177. $ErrMsg = _('The chart details for account') . ' ' . $SelectedAccount . ' ' . _('could not be retrieved');
  178. $ChartDetailsResult = DB_query($sql,$db,$ErrMsg);
  179. $ChartDetailRow = DB_fetch_array($ChartDetailsResult);
  180. if ($PeriodTotal < 0) {
  181. fwrite($fp, $SelectedAccount . ', ' . $PeriodNo . ', ' . _('Period Total') . ',,,,' . -$PeriodTotal. "\n");
  182. } else {
  183. fwrite($fp, $SelectedAccount . ', ' . $PeriodNo . ', ' . _('Period Total') . ',,,' . $PeriodTotal. "\n");
  184. }
  185. }
  186. $PeriodNo = $myrow['periodno'];
  187. $PeriodTotal = 0;
  188. }
  189. $RunningTotal += $myrow['amount'];
  190. $PeriodTotal += $myrow['amount'];
  191. $FormatedTranDate = ConvertSQLDate($myrow['trandate']);
  192. $tagsql="SELECT tagdescription FROM tags WHERE tagref='".$myrow['tag'] . "'";
  193. $tagresult=DB_query($tagsql,$db);
  194. $tagrow = DB_fetch_array($tagresult);
  195. if ($myrow['amount']<0){
  196. fwrite($fp, $SelectedAccount . ',' . $myrow['periodno'] . ', ' . $myrow['typename'] . ',' . $myrow['typeno'] . ',' . $FormatedTranDate . ',,' . -$myrow['amount'] . ',' . $myrow['narrative'] . ',' . $tagrow['tagdescription']. "\n");
  197. } else {
  198. fwrite($fp, $SelectedAccount . ',' . $myrow['periodno'] . ', ' . $myrow['typename'] . ',' . $myrow['typeno'] . ',' . $FormatedTranDate . ',' . $myrow['amount'] . ',,' . $myrow['narrative'] . ',' . $tagrow['tagdescription']. "\n");
  199. }
  200. } //end loop around GLtrans
  201. if ($PeriodTotal <>0){
  202. if ($PeriodTotal < 0){
  203. fwrite($fp, $SelectedAccount . ', ' . $PeriodNo . ', ' . _('Period Total') . ',,,,' . -$PeriodTotal. "\n");
  204. } else {
  205. fwrite($fp, $SelectedAccount . ', ' . $PeriodNo . ', ' . _('Period Total') . ',,,' . $PeriodTotal. "\n");
  206. }
  207. }
  208. if ($PandLAccount==True){
  209. if ($RunningTotal < 0){
  210. fwrite($fp, $SelectedAccount . ',' . $LastPeriodSelected . ', ' . _('Total Period Movement') . ',,,,' . -$RunningTotal . "\n");
  211. } else {
  212. fwrite($fp, $SelectedAccount . ',' . $LastPeriodSelected . ', ' . _('Total Period Movement') . ',,,' . $RunningTotal . "\n");
  213. }
  214. } else { /*its a balance sheet account*/
  215. if ($RunningTotal < 0){
  216. fwrite($fp, $SelectedAccount . ',' . $LastPeriodSelected . ', ' . _('Balance C/Fwd') . ',,,,' . -$RunningTotal . "\n");
  217. } else {
  218. fwrite($fp, $SelectedAccount . ',' . $LastPeriodSelected . ', ' . _('Balance C/Fwd') . ',,,' . $RunningTotal . "\n");
  219. }
  220. }
  221. } /*end for each SelectedAccount */
  222. fclose($fp);
  223. echo '<p><a href="' . $FileName . '">' . _('click here') . '</a> ' . _('to view the file') . '<br />';
  224. } /* end of if CreateCSV button hit */
  225. include('includes/footer.inc');
  226. ?>