export-thirdpartyaccount.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env php
  2. <?php
  3. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  4. * Copyright (C) 2013-2014 Alexandre Spangaro <aspangaro@open-dsi.fr>
  5. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file scripts/accountancy/export-thirdpartyaccount.php
  23. * \ingroup Accounting Expert
  24. * \brief Page to detect empty accounting account
  25. */
  26. if (!defined('NOSESSION')) {
  27. define('NOSESSION', '1');
  28. }
  29. $path = __DIR__.'/';
  30. require_once $path."../../htdocs/master.inc.php";
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  33. $langs->loadLangs(array("companies", "compta", "main", "accountancy"));
  34. // Security check
  35. if (!$user->admin) {
  36. accessforbidden();
  37. }
  38. // Date range
  39. $year = GETPOST("year");
  40. if (empty($year)) {
  41. $year_current = strftime("%Y", dol_now());
  42. $month_current = strftime("%m", dol_now());
  43. $year_start = $year_current;
  44. } else {
  45. $year_current = $year;
  46. $month_current = strftime("%m", dol_now());
  47. $year_start = $year;
  48. }
  49. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  50. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  51. // Quarter
  52. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  53. $q = GETPOST("q") ? GETPOST("q") : 0;
  54. if ($q == 0) {
  55. // We define date_start and date_end
  56. $year_end = $year_start;
  57. $month_start = GETPOST("month") ? GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
  58. if (!GETPOST('month')) {
  59. if (!GETPOST("year") && $month_start > $month_current) {
  60. $year_start--;
  61. $year_end--;
  62. }
  63. $month_end = $month_start - 1;
  64. if ($month_end < 1) {
  65. $month_end = 12;
  66. } else {
  67. $year_end++;
  68. }
  69. } else {
  70. $month_end = $month_start;
  71. }
  72. $date_start = dol_get_first_day($year_start, $month_start, false);
  73. $date_end = dol_get_last_day($year_end, $month_end, false);
  74. }
  75. if ($q == 1) {
  76. $date_start = dol_get_first_day($year_start, 1, false);
  77. $date_end = dol_get_last_day($year_start, 3, false);
  78. }
  79. if ($q == 2) {
  80. $date_start = dol_get_first_day($year_start, 4, false);
  81. $date_end = dol_get_last_day($year_start, 6, false);
  82. }
  83. if ($q == 3) {
  84. $date_start = dol_get_first_day($year_start, 7, false);
  85. $date_end = dol_get_last_day($year_start, 9, false);
  86. }
  87. if ($q == 4) {
  88. $date_start = dol_get_first_day($year_start, 10, false);
  89. $date_end = dol_get_last_day($year_start, 12, false);
  90. }
  91. } else {
  92. }
  93. llxHeader();
  94. $form = new Form($db);
  95. $nomlink = '';
  96. $periodlink = '';
  97. $exportlink = '';
  98. $nom = $langs->trans("ReportThirdParty");
  99. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  100. $description = $langs->trans("DescThirdPartyReport");
  101. $builddate = dol_now();
  102. $moreparam = array('action' => '');
  103. report_header($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam);
  104. print '<input type="button" class="button" style="float: right;" value="Export CSV" onclick="launch_export();" />';
  105. print '
  106. <script type="text/javascript">
  107. function launch_export()
  108. {
  109. $("div.fiche div.tabBar form input[name=\"action\"]").val("export_csv");
  110. $("div.fiche div.tabBar form input[type=\"submit\"]").click();
  111. $("div.fiche div.tabBar form input[name=\"action\"]").val();
  112. }
  113. </script>';
  114. $sql = "(SELECT s.rowid, s.nom as name , s.address, s.zip , s.town, s.code_compta as compta , ";
  115. $sql .= " s.fk_forme_juridique , s.fk_pays , s.phone , s.fax , f.datec , f.fk_soc , cp.label as country ";
  116. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  117. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  118. $sql .= ", ".MAIN_DB_PREFIX."c_country as cp";
  119. $sql .= " WHERE f.fk_soc = s.rowid";
  120. $sql .= " AND s.fk_pays = cp.rowid";
  121. if (!empty($date_start) && !empty($date_end)) {
  122. $sql .= " AND f.datec >= '".$db->idate($date_start)."' AND f.datec <= '".$db->idate($date_end)."'";
  123. }
  124. $sql .= " AND f.entity IN (".getEntity('invoice', 0).")";
  125. if ($socid > 0) {
  126. $sql .= " AND f.fk_soc = ".((int) $socid);
  127. }
  128. $sql .= " GROUP BY name";
  129. $sql .= ")";
  130. $sql .= "UNION (SELECT s.rowid, s.nom as name , s.address, s.zip , s.town, s.code_compta_fournisseur as compta , ";
  131. $sql .= " s.fk_forme_juridique , s.fk_pays , s.phone , s.fax , ff.datec , ff.fk_soc , cp.label as country ";
  132. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  133. $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as ff";
  134. $sql .= ", ".MAIN_DB_PREFIX."c_country as cp";
  135. $sql .= " WHERE ff.fk_soc = s.rowid";
  136. $sql .= " AND s.fk_pays = cp.rowid";
  137. if (!empty($date_start) && !empty($date_end)) {
  138. $sql .= " AND ff.datec >= '".$db->idate($date_start)."' AND ff.datec <= '".$db->idate($date_end)."'";
  139. }
  140. $sql .= " AND ff.entity = ".$conf->entity;
  141. if ($socid > 0) {
  142. $sql .= " AND f.fk_soc = ".((int) $socid);
  143. }
  144. $sql .= " GROUP BY name";
  145. $sql .= ")";
  146. $sql .= "ORDER BY name ASC";
  147. dol_syslog('accountancy/admin/thirdpartyaccount.php:: $sql='.$sql);
  148. $resql = $db->query($sql);
  149. if ($resql) {
  150. $num = $db->num_rows($resql);
  151. $i = 0;
  152. // export csv
  153. if (GETPOST('action', 'aZ09') == 'export_csv') {
  154. header('Content-Type: text/csv');
  155. header('Content-Disposition: attachment;filename=export_csv.csv');
  156. $obj = $db->fetch_object($resql);
  157. print '"'.$obj->compta.'",';
  158. print '"'.$obj->address.'",';
  159. print '"'.$obj->zip.'",';
  160. print '"'.$obj->town.'",';
  161. print '"'.$obj->country.'",';
  162. print '"'.$obj->phone.'",';
  163. print '"'.$obj->fax.'",';
  164. print "\n";
  165. $i++;
  166. }
  167. /*
  168. * View
  169. */
  170. $thirdpartystatic = new Societe($db);
  171. print '<br><br>';
  172. print '<table class="noborder centpercent">';
  173. print "</table>\n";
  174. print '</td><td valign="top" width="70%" class="notopnoleftnoright"></td>';
  175. print '</tr><tr><td colspan=2>';
  176. print '<table class="noborder centpercent">';
  177. print '<tr class="liste_titre"><td class="left">'.$langs->trans("ThirdParties").'</td>';
  178. print '<td class="left">'.$langs->trans("AccountNumber").'</td>';
  179. print '<td class="left">'.$langs->trans("RaisonSociale").'</td>';
  180. print '<td class="left">'.$langs->trans("Address").'</td>';
  181. print '<td class="left">'.$langs->trans("Zip").'</td>';
  182. print '<td class="left">'.$langs->trans("Town").'</td>';
  183. print '<td class="left">'.$langs->trans("Country").'</td>';
  184. print '<td class="left">'.$langs->trans("Contact").'</td>';
  185. print '<td class="left">'.$langs->trans("Phone").'</td>';
  186. print '<td class="left">'.$langs->trans("Fax").'</td></tr>';
  187. while ($obj = $db->fetch_object($resql)) {
  188. print '<tr class="oddeven">';
  189. print '<td>';
  190. $thirdpartystatic->id = $obj->rowid;
  191. $thirdpartystatic->name = $obj->name;
  192. $thirdpartystatic->client = $obj->client;
  193. $thirdpartystatic->canvas = $obj->canvas;
  194. $thirdpartystatic->status = $obj->status;
  195. print $thirdpartystatic->getNomUrl(1);
  196. print '</td>';
  197. print '<td class="left">'.$obj->compta.'</td>'."\n";
  198. print '<td class="left"></td>';
  199. print '<td class="left">'.$obj->address.'</td>';
  200. print '<td class="left">'.$obj->zip.'</td>';
  201. print '<td class="left">'.$obj->town.'</td>';
  202. print '<td class="left">'.$obj->country.'</td>';
  203. print '<td class="left"></td>';
  204. print '<td class="left">'.$obj->phone.'</td>';
  205. print '<td class="left">'.$obj->fax.'</td>';
  206. print "</tr>\n";
  207. $i++;
  208. }
  209. print "</table>";
  210. $db->free($resql);
  211. } else {
  212. dol_print_error($db);
  213. }
  214. llxFooter();
  215. $db->close();