PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/chronique/Z_CreateCollegeTemplateFile.php

http://chronique.googlecode.com/
PHP | 247 lines | 197 code | 30 blank | 20 comment | 13 complexity | 94d8fdd7e6f02056f6c415659ed2f16e 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: Z_CreateCompanyTemplateFile.php 4734 2011-10-29 03:26:27Z daintree $*/
  3. include ('includes/session.inc');
  4. $title = _('Create Database Template File');
  5. include ('includes/header.inc');
  6. if (isset($_POST['CreateTemplate'])){
  7. $InputError = false; //assume the best - but check for the worst
  8. if (mb_strlen($_POST['TemplateName'])<=1){
  9. prnMsg(_('The name of the template has not been entered or is just 1 character - an informative name is required e.g. Deutsche-distribution in the case of a german distribution company'),'error');
  10. $InputError = true;
  11. }
  12. if ($InputError==false){
  13. $CurrResult = DB_query( "SELECT currabrev,
  14. currency,
  15. country,
  16. debtorsact,
  17. creditorsact,
  18. payrollact,
  19. grnact,
  20. exchangediffact,
  21. purchasesexchangediffact,
  22. retainedearnings,
  23. freightact
  24. FROM currencies INNER JOIN companies
  25. ON companies.currencydefault=currencies.currabrev
  26. WHERE coycode='1'",$db);
  27. $CurrRow = DB_fetch_array($CurrResult);
  28. $SQLScript = "SET FOREIGN_KEY_CHECKS=0;
  29. DELETE FROM currencies WHERE currabrev='" . $CurrRow['currabrev'] ."';\n";
  30. $SQLScript .= "INSERT INTO currencies (currabrev, currency, country, rate)
  31. VALUES ('" . $CurrRow['currabrev'] . "', '" . $CurrRow['currency'] ."', '" . $CurrRow['country'] . "', 1);\n";
  32. $SQLScript .= "UPDATE companies SET currencydefault='" . $CurrRow['currabrev'] ."',
  33. regoffice6='" . $CurrRow['country'] . "',
  34. debtorsact=" . $CurrRow['debtorsact'] . ",
  35. creditorsact=" . $CurrRow['creditorsact'] . ",
  36. payrollact=" . $CurrRow['payrollact'] . ",
  37. grnact=" . $CurrRow['grnact'] . ",
  38. exchangediffact=" . $CurrRow['exchangediffact'] . ",
  39. purchasesexchangediffact=" . $CurrRow['purchasesexchangediffact'] . ",
  40. retainedearnings=" . $CurrRow['retainedearnings'] . ",
  41. freightact=" . $CurrRow['freightact'] . "
  42. WHERE coycode='1';\n";
  43. /*empty out any existing records in
  44. chartmaster,
  45. accountgroups,
  46. taxauthorities,
  47. taxauthrates,
  48. taxgroups,
  49. taxgrouptaxes,
  50. taxcategories,
  51. taxprovinces */
  52. $SQLScript .= "TRUNCATE TABLE chartmaster;\n";
  53. $SQLScript .= "TRUNCATE TABLE accountgroups;\n";
  54. $SQLScript .= "TRUNCATE TABLE taxauthorities;\n";
  55. $SQLScript .= "TRUNCATE TABLE taxauthrates;\n";
  56. $SQLScript .= "TRUNCATE TABLE taxgroups;\n";
  57. $SQLScript .= "TRUNCATE TABLE taxgrouptaxes;\n";
  58. $SQLScript .= "TRUNCATE TABLE taxcategories;\n";
  59. $SQLScript .= "TRUNCATE TABLE taxprovinces;\n";
  60. $GroupsResult = DB_query("SELECT groupname,
  61. sectioninaccounts,
  62. pandl,
  63. sequenceintb,
  64. parentgroupname
  65. FROM accountgroups",$db);
  66. while ($GroupRow = DB_fetch_array($GroupsResult)){
  67. $SQLScript .= "INSERT INTO accountgroups (groupname,sectioninaccounts,pandl, sequenceintb, parentgroupname)
  68. VALUES ('" . $GroupRow['groupname'] . "',
  69. '" . $GroupRow['sectioninaccounts'] . "',
  70. " . $GroupRow['pandl'] . ",
  71. " . $GroupRow['sequenceintb'] . ",
  72. '" . $GroupRow['parentgroupname'] . "');\n";
  73. }
  74. $ChartResult = DB_query("SELECT accountcode, accountname, group_ FROM chartmaster",$db);
  75. $i=0;
  76. while ($ChartRow = DB_fetch_array($ChartResult)){
  77. if ($_POST['IncludeAccount_' .$i]=='on'){
  78. $SQLScript .= "INSERT INTO chartmaster (accountcode,accountname,group_)
  79. VALUES ('" . $ChartRow['accountcode'] . "',
  80. '" . $ChartRow['accountname'] . "',
  81. '" . $ChartRow['group_'] . "');\n";
  82. }
  83. $i++;
  84. }
  85. /*Now the tax set up */
  86. /*Tax Authorities table */
  87. $TaxAuthoritiesResult = DB_query("SELECT taxid,
  88. description,
  89. taxglcode,
  90. purchtaxglaccount,
  91. bank,
  92. bankacctype,
  93. bankacc,
  94. bankswift
  95. FROM taxauthorities",$db);
  96. while ($TaxAuthoritiesRow = DB_fetch_array($TaxAuthoritiesResult)){
  97. $SQLScript .= "INSERT INTO taxauthorities (taxid,
  98. description,
  99. taxglcode,
  100. purchtaxglaccount,
  101. bank,
  102. bankacctype,
  103. bankacc,
  104. bankswift)
  105. VALUES (" . $TaxAuthoritiesRow['taxid'] . ",
  106. '" . $TaxAuthoritiesRow['description'] . "',
  107. " . $TaxAuthoritiesRow['taxglcode'] . ",
  108. " . $TaxAuthoritiesRow['purchtaxglaccount'] . ",
  109. '" . $TaxAuthoritiesRow['bank'] . "',
  110. '" . $TaxAuthoritiesRow['bankacctype'] . "',
  111. '" . $TaxAuthoritiesRow['bankacc'] . "',
  112. '" . $TaxAuthoritiesRow['bankaccswift'] . "');\n";
  113. }
  114. /*taxauthrates table */
  115. $TaxAuthRatesResult = DB_query("SELECT taxauthority,
  116. dispatchtaxprovince,
  117. taxcatid,
  118. taxrate
  119. FROM taxauthrates",$db);
  120. while ($TaxAuthRatesRow = DB_fetch_array($TaxAuthRatesResult)){
  121. $SQLScript .= "INSERT INTO taxauthrates (taxauthority,
  122. dispatchtaxprovince,
  123. taxcatid,
  124. taxrate)
  125. VALUES (" . $TaxAuthRatesRow['taxauthority'] . ",
  126. " . $TaxAuthRatesRow['dispatchtaxprovince'] . ",
  127. " . $TaxAuthRatesRow['taxcatid'] . ",
  128. " . $TaxAuthRatesRow['taxrate'] . ");\n";
  129. }
  130. /*taxgroups table */
  131. $TaxGroupsResult = DB_query("SELECT taxgroupid,
  132. taxgroupdescription
  133. FROM taxgroups",$db);
  134. while ($TaxGroupsRow = DB_fetch_array($TaxGroupsResult)){
  135. $SQLScript .= "INSERT INTO taxgroups (taxgroupid,
  136. taxgroupdescription)
  137. VALUES ('" . $TaxGroupsRow['taxgroupid'] . "',
  138. '" . $TaxGroupsRow['taxgroupdescription'] . "');\n";
  139. }
  140. /*tax categories table */
  141. $TaxCategoriesResult = DB_query("SELECT taxcatid,
  142. taxcatname
  143. FROM taxcategories",$db);
  144. while ($TaxCategoriesRow = DB_fetch_array($TaxCategoriesResult)){
  145. $SQLScript .= "INSERT INTO taxcategories (taxcatid,
  146. taxcatname)
  147. VALUES (" . $TaxCategoriesRow['taxcatid'] . ",
  148. '" . $TaxCategoriesRow['taxcatname'] . "');\n";
  149. }
  150. /*tax provinces table */
  151. $TaxProvincesResult = DB_query("SELECT taxprovinceid,
  152. taxprovincename
  153. FROM taxprovinces",$db);
  154. while ($TaxProvincesRow = DB_fetch_array($TaxProvincesResult)){
  155. $SQLScript .= "INSERT INTO taxprovinces (taxprovinceid,
  156. taxprovincename)
  157. VALUES (" . $TaxProvincesRow['taxprovinceid'] . ",
  158. '" . $TaxProvincesRow['taxprovincename'] . "');\n";
  159. }
  160. /*taxgroup taxes table */
  161. $TaxGroupTaxesResult = DB_query("SELECT taxgroupid,
  162. taxauthid,
  163. calculationorder,
  164. taxontax
  165. FROM taxgrouptaxes",$db);
  166. while ($TaxGroupTaxesRow = DB_fetch_array($TaxGroupTaxesResult)){
  167. $SQLScript .= "INSERT INTO taxgrouptaxes (taxgroupid,
  168. taxauthid,
  169. calculationorder,
  170. taxontax)
  171. VALUES (" . $TaxGroupTaxesRow['taxgroupid'] . ",
  172. " . $TaxGroupTaxesRow['taxauthid'] . ",
  173. " . $TaxGroupTaxesRow['calculationorder'] . ",
  174. " . $TaxGroupTaxesRow['taxontax'] . ");\n";
  175. }
  176. $SQLScript .= "SET FOREIGN_KEY_CHECKS=1;";
  177. /*Now write $SQLScript to a file */
  178. $FileHandle = fopen("./companies/" . $_SESSION['DatabaseName'] . "/reports/" . $_POST['TemplateName'] .".sql","w");
  179. fwrite ($FileHandle, $SQLScript);
  180. fclose ($FileHandle);
  181. echo '<P><a href="' . $rootpath . '/companies/' . $_SESSION['DatabaseName'] . '/reports/' . $_POST['TemplateName'] .'.sql">' . _('Show the sql template file produced') . '</a>';
  182. include('includes/htmlMimeMail.php');
  183. $Recipients = array('"Submissions" <submissions@weberp.org>');
  184. $mail = new htmlMimeMail();
  185. $attachment = $mail->getFile( $rootpath . '/companies/' . $_SESSION['DatabaseName'] . '/reports/' . $_POST['TemplateName'] .'.sql');
  186. $mail->setText('Please find company template ' . $_POST['TemplateName']);
  187. $mail->addAttachment($attachment, 'CompanyTemplate.sql', 'application/txt');
  188. $mail->setSubject('Company Template Submission');
  189. $mail->setFrom($_SESSION['CompanyRecord']['coyname'] . '<' . $_SESSION['CompanyRecord']['email'] . '>');
  190. $result = $mail->send($Recipients);
  191. /*end of SQL Script creation */
  192. }/*end if Input error*/
  193. } /*end submit button hit */
  194. echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '">';
  195. echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
  196. prnMsg(_('Running the create a new company template script will export all account groups, account codes and tax set up tables including tax groups, tax authorities, tax rates etc. However, no transactions or private data will be exported. There is opportunity to prevent specific general ledger accounts from being exported where these are considered private - again no transactional or balance data is exported and you can inspect the contents of the sql file. The template file will be emailed automatically to the webERP project'),'info');
  197. echo _('Enter the name of the template to be created') . ':<input type="text" name="TemplateName">';
  198. prnMsg(_('Warning: All selected accounts will be exported - please de-select the accounts you do not wish to export to the new template file'),'warn');
  199. echo '<table>';
  200. /*Show the chart of accounts to be exported for deslection of company specific ones */
  201. $ChartResult = DB_query("SELECT accountcode, accountname, group_ FROM chartmaster",$db);
  202. $TableHeadings = '<tr><th>' . _('Account Code') . '</th>
  203. <th>' . _('Account Name') . '</th></tr>';
  204. $i = 0;
  205. while ($ChartRow = DB_fetch_array($ChartResult)){
  206. echo '<tr><td>' . $ChartRow['accountcode'] . '</td>
  207. <td>' . $ChartRow['accountname'] . '</td>
  208. <td><input type="checkbox" name="IncludeAccount_' . $i . '" CHECKED></td>
  209. </tr>';
  210. $i++;
  211. }
  212. echo '</table>';
  213. echo '<hr>';
  214. echo '<center><input type="submit" name="CreateTemplate" value="' . _('Create Template and Email') . '"></center>';
  215. echo '</form>';
  216. include('includes/footer.inc');
  217. ?>