export-thirdpartyaccount.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. }
  92. /*
  93. * Main
  94. */
  95. llxHeader();
  96. $form = new Form($db);
  97. $nomlink = '';
  98. $periodlink = '';
  99. $exportlink = '';
  100. $nom = $langs->trans("ReportThirdParty");
  101. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  102. $description = $langs->trans("DescThirdPartyReport");
  103. $builddate = dol_now();
  104. $moreparam = array('action' => '');
  105. report_header($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam);
  106. print '<input type="button" class="button" style="float: right;" value="Export CSV" onclick="launch_export();" />';
  107. print '
  108. <script type="text/javascript">
  109. function launch_export()
  110. {
  111. $("div.fiche div.tabBar form input[name=\"action\"]").val("export_csv");
  112. $("div.fiche div.tabBar form input[type=\"submit\"]").click();
  113. $("div.fiche div.tabBar form input[name=\"action\"]").val();
  114. }
  115. </script>';
  116. $sql = "(SELECT s.rowid, s.nom as name , s.address, s.zip , s.town";
  117. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  118. $sql .= ", spe.accountancy_code_customer as compta";
  119. } else {
  120. $sql .= ", s.code_compta";
  121. }
  122. $sql .= ", s.fk_forme_juridique , s.fk_pays , s.phone , s.fax , f.datec , f.fk_soc , cp.label as country ";
  123. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  124. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  125. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
  126. }
  127. $sql .= ", ".MAIN_DB_PREFIX."facture as f";
  128. $sql .= ", ".MAIN_DB_PREFIX."c_country as cp";
  129. $sql .= " WHERE f.fk_soc = s.rowid";
  130. $sql .= " AND s.fk_pays = cp.rowid";
  131. if (!empty($date_start) && !empty($date_end)) {
  132. $sql .= " AND f.datec >= '".$db->idate($date_start)."' AND f.datec <= '".$db->idate($date_end)."'";
  133. }
  134. $sql .= " AND f.entity IN (".getEntity('invoice', 0).")";
  135. if ($socid > 0) {
  136. $sql .= " AND f.fk_soc = ".((int) $socid);
  137. }
  138. $sql .= " GROUP BY name";
  139. $sql .= ")";
  140. $sql .= "UNION (SELECT s.rowid, s.nom as name , s.address, s.zip , s.town, , ";
  141. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  142. $sql .= ", spe.accountancy_code_supplier as compta";
  143. } else {
  144. $sql .= ", s.code_compta_fournisseur as compta";
  145. }
  146. $sql .= " s.fk_forme_juridique , s.fk_pays , s.phone , s.fax , ff.datec , ff.fk_soc , cp.label as country ";
  147. $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
  148. if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
  149. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
  150. }
  151. $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as ff";
  152. $sql .= ", ".MAIN_DB_PREFIX."c_country as cp";
  153. $sql .= " WHERE ff.fk_soc = s.rowid";
  154. $sql .= " AND s.fk_pays = cp.rowid";
  155. if (!empty($date_start) && !empty($date_end)) {
  156. $sql .= " AND ff.datec >= '".$db->idate($date_start)."' AND ff.datec <= '".$db->idate($date_end)."'";
  157. }
  158. $sql .= " AND ff.entity = ".$conf->entity;
  159. if ($socid > 0) {
  160. $sql .= " AND f.fk_soc = ".((int) $socid);
  161. }
  162. $sql .= " GROUP BY name";
  163. $sql .= ")";
  164. $sql .= "ORDER BY name ASC";
  165. dol_syslog('accountancy/admin/thirdpartyaccount.php:: $sql='.$sql);
  166. $resql = $db->query($sql);
  167. if ($resql) {
  168. $num = $db->num_rows($resql);
  169. $i = 0;
  170. // export csv
  171. if (GETPOST('action', 'aZ09') == 'export_csv') {
  172. header('Content-Type: text/csv');
  173. header('Content-Disposition: attachment;filename=export_csv.csv');
  174. $obj = $db->fetch_object($resql);
  175. print '"'.$obj->compta.'",';
  176. print '"'.$obj->address.'",';
  177. print '"'.$obj->zip.'",';
  178. print '"'.$obj->town.'",';
  179. print '"'.$obj->country.'",';
  180. print '"'.$obj->phone.'",';
  181. print '"'.$obj->fax.'",';
  182. print "\n";
  183. $i++;
  184. }
  185. $thirdpartystatic = new Societe($db);
  186. print '<br><br>';
  187. print '<table class="noborder centpercent">';
  188. print "</table>\n";
  189. print '</td><td valign="top" width="70%" class="notopnoleftnoright"></td>';
  190. print '</tr><tr><td colspan=2>';
  191. print '<table class="noborder centpercent">';
  192. print '<tr class="liste_titre"><td class="left">'.$langs->trans("ThirdParties").'</td>';
  193. print '<td class="left">'.$langs->trans("AccountNumber").'</td>';
  194. print '<td class="left">'.$langs->trans("RaisonSociale").'</td>';
  195. print '<td class="left">'.$langs->trans("Address").'</td>';
  196. print '<td class="left">'.$langs->trans("Zip").'</td>';
  197. print '<td class="left">'.$langs->trans("Town").'</td>';
  198. print '<td class="left">'.$langs->trans("Country").'</td>';
  199. print '<td class="left">'.$langs->trans("Contact").'</td>';
  200. print '<td class="left">'.$langs->trans("Phone").'</td>';
  201. print '<td class="left">'.$langs->trans("Fax").'</td></tr>';
  202. while ($obj = $db->fetch_object($resql)) {
  203. print '<tr class="oddeven">';
  204. print '<td>';
  205. $thirdpartystatic->id = $obj->rowid;
  206. $thirdpartystatic->name = $obj->name;
  207. $thirdpartystatic->client = $obj->client;
  208. $thirdpartystatic->canvas = $obj->canvas;
  209. $thirdpartystatic->status = $obj->status;
  210. print $thirdpartystatic->getNomUrl(1);
  211. print '</td>';
  212. print '<td class="left">'.$obj->compta.'</td>'."\n";
  213. print '<td class="left"></td>';
  214. print '<td class="left">'.$obj->address.'</td>';
  215. print '<td class="left">'.$obj->zip.'</td>';
  216. print '<td class="left">'.$obj->town.'</td>';
  217. print '<td class="left">'.$obj->country.'</td>';
  218. print '<td class="left"></td>';
  219. print '<td class="left">'.$obj->phone.'</td>';
  220. print '<td class="left">'.$obj->fax.'</td>';
  221. print "</tr>\n";
  222. $i++;
  223. }
  224. print "</table>";
  225. $db->free($resql);
  226. } else {
  227. dol_print_error($db);
  228. }
  229. llxFooter();
  230. $db->close();