dolibarr  17.0.4
export-thirdpartyaccount.php
Go to the documentation of this file.
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 
28 if (!defined('NOSESSION')) {
29  define('NOSESSION', '1');
30 }
31 
32 $path = __DIR__.'/';
33 
34 require_once $path."../../htdocs/master.inc.php";
35 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
36 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
37 
38 $langs->loadLangs(array("companies", "compta", "main", "accountancy"));
39 
40 // Security check
41 if (!$user->admin) {
43 }
44 
45 // Date range
46 $year = GETPOST("year");
47 if (empty($year)) {
48  $year_current = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
49  $month_current = strftime("%m", dol_now());
50  $year_start = $year_current;
51 } else {
52  $year_current = $year;
53  $month_current = strftime("%m", dol_now());
54  $year_start = $year;
55 }
56 $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
57 $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
58 
59 // Quarter
60 if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
61  $q = GETPOST("q") ? GETPOST("q") : 0;
62  if ($q == 0) {
63  // We define date_start and date_end
64  $year_end = $year_start;
65  $month_start = GETPOST("month") ? GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
66  if (!GETPOST('month')) {
67  if (!GETPOST("year") && $month_start > $month_current) {
68  $year_start--;
69  $year_end--;
70  }
71  $month_end = $month_start - 1;
72  if ($month_end < 1) {
73  $month_end = 12;
74  } else {
75  $year_end++;
76  }
77  } else {
78  $month_end = $month_start;
79  }
80  $date_start = dol_get_first_day($year_start, $month_start, false);
81  $date_end = dol_get_last_day($year_end, $month_end, false);
82  }
83  if ($q == 1) {
84  $date_start = dol_get_first_day($year_start, 1, false);
85  $date_end = dol_get_last_day($year_start, 3, false);
86  }
87  if ($q == 2) {
88  $date_start = dol_get_first_day($year_start, 4, false);
89  $date_end = dol_get_last_day($year_start, 6, false);
90  }
91  if ($q == 3) {
92  $date_start = dol_get_first_day($year_start, 7, false);
93  $date_end = dol_get_last_day($year_start, 9, false);
94  }
95  if ($q == 4) {
96  $date_start = dol_get_first_day($year_start, 10, false);
97  $date_end = dol_get_last_day($year_start, 12, false);
98  }
99 }
100 
101 /*
102  * Main
103  */
104 
105 llxHeader();
106 
107 $form = new Form($db);
108 
109 $nomlink = '';
110 $periodlink = '';
111 $exportlink = '';
112 
113 $nom = $langs->trans("ReportThirdParty");
114 $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
115 $description = $langs->trans("DescThirdPartyReport");
116 $builddate = dol_now();
117 
118 $moreparam = array('action' => '');
119 report_header($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam);
120 
121 print '<input type="button" class="button" style="float: right;" value="Export CSV" onclick="launch_export();" />';
122 
123 print '
124  <script type="text/javascript">
125  function launch_export()
126  {
127  $("div.fiche div.tabBar form input[name=\"action\"]").val("export_csv");
128  $("div.fiche div.tabBar form input[type=\"submit\"]").click();
129  $("div.fiche div.tabBar form input[name=\"action\"]").val();
130  }
131 </script>';
132 
133 $sql = "(SELECT s.rowid, s.nom as name , s.address, s.zip , s.town";
134 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
135  $sql .= ", spe.accountancy_code_customer as compta";
136 } else {
137  $sql .= ", s.code_compta";
138 }
139 $sql .= ", s.fk_forme_juridique , s.fk_pays , s.phone , s.fax , f.datec , f.fk_soc , cp.label as country ";
140 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
141 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
142  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
143 }
144 $sql .= ", ".MAIN_DB_PREFIX."facture as f";
145 $sql .= ", ".MAIN_DB_PREFIX."c_country as cp";
146 $sql .= " WHERE f.fk_soc = s.rowid";
147 $sql .= " AND s.fk_pays = cp.rowid";
148 if (!empty($date_start) && !empty($date_end)) {
149  $sql .= " AND f.datec >= '".$db->idate($date_start)."' AND f.datec <= '".$db->idate($date_end)."'";
150 }
151 $sql .= " AND f.entity IN (".getEntity('invoice', 0).")";
152 if ($socid > 0) {
153  $sql .= " AND f.fk_soc = ".((int) $socid);
154 }
155 $sql .= " GROUP BY name";
156 $sql .= ")";
157 $sql .= "UNION (SELECT s.rowid, s.nom as name , s.address, s.zip , s.town, , ";
158 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
159  $sql .= ", spe.accountancy_code_supplier as compta";
160 } else {
161  $sql .= ", s.code_compta_fournisseur as compta";
162 }
163 $sql .= " s.fk_forme_juridique , s.fk_pays , s.phone , s.fax , ff.datec , ff.fk_soc , cp.label as country ";
164 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
165 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
166  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
167 }
168 $sql .= ", ".MAIN_DB_PREFIX."facture_fourn as ff";
169 $sql .= ", ".MAIN_DB_PREFIX."c_country as cp";
170 $sql .= " WHERE ff.fk_soc = s.rowid";
171 $sql .= " AND s.fk_pays = cp.rowid";
172 if (!empty($date_start) && !empty($date_end)) {
173  $sql .= " AND ff.datec >= '".$db->idate($date_start)."' AND ff.datec <= '".$db->idate($date_end)."'";
174 }
175 $sql .= " AND ff.entity = ".$conf->entity;
176 if ($socid > 0) {
177  $sql .= " AND f.fk_soc = ".((int) $socid);
178 }
179 $sql .= " GROUP BY name";
180 $sql .= ")";
181 
182 $sql .= "ORDER BY name ASC";
183 
184 dol_syslog('accountancy/admin/thirdpartyaccount.php:: $sql='.$sql);
185 $resql = $db->query($sql);
186 if ($resql) {
187  $num = $db->num_rows($resql);
188  $i = 0;
189 
190  // export csv
191  if (GETPOST('action', 'aZ09') == 'export_csv') {
192  header('Content-Type: text/csv');
193  header('Content-Disposition: attachment;filename=export_csv.csv');
194 
195  $obj = $db->fetch_object($resql);
196 
197  print '"'.$obj->compta.'",';
198  print '"'.$obj->address.'",';
199  print '"'.$obj->zip.'",';
200  print '"'.$obj->town.'",';
201  print '"'.$obj->country.'",';
202  print '"'.$obj->phone.'",';
203  print '"'.$obj->fax.'",';
204  print "\n";
205  $i++;
206  }
207 
208 
209  $thirdpartystatic = new Societe($db);
210 
211  print '<br><br>';
212 
213  print '<table class="noborder centpercent">';
214  print "</table>\n";
215  print '</td><td valign="top" width="70%" class="notopnoleftnoright"></td>';
216  print '</tr><tr><td colspan=2>';
217  print '<table class="noborder centpercent">';
218  print '<tr class="liste_titre"><td class="left">'.$langs->trans("ThirdParties").'</td>';
219  print '<td class="left">'.$langs->trans("AccountNumber").'</td>';
220  print '<td class="left">'.$langs->trans("RaisonSociale").'</td>';
221  print '<td class="left">'.$langs->trans("Address").'</td>';
222  print '<td class="left">'.$langs->trans("Zip").'</td>';
223  print '<td class="left">'.$langs->trans("Town").'</td>';
224  print '<td class="left">'.$langs->trans("Country").'</td>';
225  print '<td class="left">'.$langs->trans("Contact").'</td>';
226  print '<td class="left">'.$langs->trans("Phone").'</td>';
227  print '<td class="left">'.$langs->trans("Fax").'</td></tr>';
228 
229  while ($obj = $db->fetch_object($resql)) {
230  print '<tr class="oddeven">';
231  print '<td>';
232  $thirdpartystatic->id = $obj->rowid;
233  $thirdpartystatic->name = $obj->name;
234  $thirdpartystatic->client = $obj->client;
235  $thirdpartystatic->canvas = $obj->canvas;
236  $thirdpartystatic->status = $obj->status;
237  print $thirdpartystatic->getNomUrl(1);
238  print '</td>';
239  print '<td class="left">'.$obj->compta.'</td>'."\n";
240  print '<td class="left"></td>';
241  print '<td class="left">'.$obj->address.'</td>';
242  print '<td class="left">'.$obj->zip.'</td>';
243  print '<td class="left">'.$obj->town.'</td>';
244  print '<td class="left">'.$obj->country.'</td>';
245  print '<td class="left"></td>';
246  print '<td class="left">'.$obj->phone.'</td>';
247  print '<td class="left">'.$obj->fax.'</td>';
248  print "</tr>\n";
249 
250  $i++;
251  }
252 
253  print "</table>";
254  $db->free($resql);
255 } else {
256  dol_print_error($db);
257 }
258 
259 llxFooter();
260 $db->close();
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
Class to manage generation of HTML components Only common components must be here.
Class to manage third parties objects (customers, suppliers, prospects...)
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:575
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:594
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a report.
Definition: report.lib.php:41
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.