compta-files.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. /* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2017 Pierre-Henry Favre <support@atm-consulting.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/compta-files.php
  21. * \ingroup compta
  22. * \brief Page to show portoflio and files of a thirdparty and download it
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
  32. restrictedArea($user,'banque');
  33. $langs->load("companies");
  34. if (! empty($conf->facture->enabled)) $langs->load("bills");
  35. $date_start =GETPOST('date_start','alpha');
  36. $date_startDay= GETPOST('date_startday','int');
  37. $date_startMonth= GETPOST('date_startmonth','int');
  38. $date_startYear= GETPOST('date_startyear','int');
  39. $date_start=($date_startDay)?dol_mktime(0,0,0,$date_startMonth,$date_startDay,$date_startYear):strtotime($date_start);
  40. $date_stop =GETPOST('date_stop','alpha');
  41. $date_stopDay= GETPOST('date_stopday','int');
  42. $date_stopMonth= GETPOST('date_stopmonth','int');
  43. $date_stopYear= GETPOST('date_stopyear','int');
  44. //FIXME doldate
  45. $date_stop=($date_stopDay)?dol_mktime(0,0,0,$date_stopMonth,$date_stopDay,$date_stopYear):strtotime($date_stop);
  46. $action =GETPOST('action','alpha');
  47. // Security check
  48. //if ($user->societe_id) $id=$user->societe_id;
  49. //$result = restrictedArea($user, 'societe', $id, '&societe');
  50. //$object = new Societe($db);
  51. //if ($id > 0) $object->fetch($id);
  52. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  53. $hookmanager->initHooks(array('comptafilescard','globalcard'));
  54. // Load variable for pagination
  55. $limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
  56. $sortfield = GETPOST('sortfield','alpha');
  57. $sortorder = GETPOST('sortorder','alpha');
  58. $page = GETPOST('page','int');
  59. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  60. $offset = $limit * $page;
  61. $pageprev = $page - 1;
  62. $pagenext = $page + 1;
  63. if (! $sortfield) $sortfield="f.datef,f.rowid"; // Set here default search field
  64. if (! $sortorder) $sortorder="DESC";
  65. $arrayfields=array(
  66. 'date'=>array('label'=>"Date", 'checked'=>1),
  67. //...
  68. );
  69. /*
  70. * Actions
  71. */
  72. //$parameters = array('socid' => $id);
  73. //$reshook = $hookmanager->executeHooks('doActions', $parameters, $object); // Note that $object may have been modified by some hooks
  74. //if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  75. /*
  76. * View
  77. */
  78. $filesarray=array();
  79. $result=false;
  80. if(($action=="searchfiles"||$action=="dl" ) && $date_start && $date_stop){
  81. $wheretail=" '".$db->idate($date_start)."' AND '".$db->idate($date_stop)."'";
  82. $sql="SELECT rowid as id, facnumber as ref,paye as paid,total_ttc,fk_soc,datef as date, 'Invoice' as item FROM ".MAIN_DB_PREFIX."facture";
  83. $sql.=" WHERE datef between ".$wheretail;
  84. $sql.=" UNION ALL";
  85. $sql.=" SELECT rowid as id,ref, paye as paid, total_ttc, fk_soc,datef as date, 'InvoiceSupplier' as item FROM ".MAIN_DB_PREFIX."facture_fourn";
  86. $sql.=" WHERE datef between ".$wheretail;
  87. $sql.=" UNION ALL";
  88. $sql.=" SELECT rowid as id,ref,paid,total_ttc,fk_user_author as fk_soc,date_fin as date,'Expense' as item FROM ".MAIN_DB_PREFIX."expensereport";
  89. $sql.=" WHERE date_fin between ".$wheretail;
  90. $sql.=" UNION ALL";
  91. $sql.=" SELECT rowid as id,ref,paid,amount as total_ttc,CONCAT(firstname,' ',lastname) as fk_soc,datedon as date,'Donation' as item FROM ".MAIN_DB_PREFIX."don";
  92. $sql.=" WHERE datedon between ".$wheretail;
  93. $sql.=" UNION ALL";
  94. $sql.=" SELECT rowid as id,label as ref,1 as paid,amount as total_ttc,fk_user as fk_soc,datep as date,'Salary' as item FROM ".MAIN_DB_PREFIX."payment_salary";
  95. $sql.=" WHERE datep between ".$wheretail;
  96. $sql.=" UNION ALL";
  97. $sql.=" SELECT rowid as id,num_paiement as ref,1 as paid,amount as total_ttc,fk_charge as fk_soc,datep as date,'Charge' as item FROM ".MAIN_DB_PREFIX."paiementcharge";
  98. $sql.=" WHERE datep between ".$wheretail;
  99. $resd = $db->query($sql);
  100. $files=array();
  101. $link='';
  102. if ($resd)
  103. {
  104. $numd = $db->num_rows($resd);
  105. $upload_dir ='';
  106. $i=0;
  107. while($i<$numd)
  108. {
  109. $objd = $db->fetch_object($resd);
  110. switch($objd->item){
  111. case "Invoice":
  112. $subdir=dol_sanitizeFileName($objd->ref);
  113. $upload_dir = $conf->facture->dir_output.'/'.$subdir;
  114. $link="../../document.php?modulepart=facture&file=".str_replace('/','%2F',$subdir).'%2F';
  115. break;
  116. case "InvoiceSupplier":
  117. $subdir=get_exdir($objd->id,2,0,0,$objd,'invoice_supplier').dol_sanitizeFileName($objd->ref);
  118. $upload_dir = $conf->fournisseur->facture->dir_output.'/'.$subdir;
  119. $link="../../document.php?modulepart=facture_fournisseur&file=".str_replace('/','%2F',$subdir).'%2F';
  120. break;
  121. case "Expense":
  122. $subdir=dol_sanitizeFileName($objd->ref);
  123. $upload_dir = $conf->expensereport->dir_output.'/'.$subdir;
  124. $link="../../document.php?modulepart=expensereport&file=".str_replace('/','%2F',$subdir).'%2F';
  125. break;
  126. case "Salary":
  127. $subdir=dol_sanitizeFileName($objd->id);
  128. $upload_dir = $conf->salaries->dir_output.'/'.$subdir;
  129. $link="../../document.php?modulepart=salaries&file=".str_replace('/','%2F',$subdir).'%2F';
  130. break;
  131. case "Donation":
  132. $subdir=get_exdir(null,2,0,1,$objd,'donation'). '/'. dol_sanitizeFileName($objd->id);
  133. $upload_dir = $conf->don->dir_output . '/' . $subdir;
  134. $link="../../document.php?modulepart=don&file=".str_replace('/','%2F',$subdir).'%2F';
  135. break;
  136. case "Charge":
  137. $subdir=dol_sanitizeFileName($objd->id);
  138. $upload_dir = $conf->tax->dir_output . '/' . $subdir;
  139. $link="../../document.php?modulepart=tax&file=".str_replace('/','%2F',$subdir).'%2F';
  140. break;
  141. default:
  142. break;
  143. }
  144. if(!empty($upload_dir)){
  145. $result=true;
  146. $files=dol_dir_list($upload_dir,"files",0,'','(\.meta|_preview\.png)$','',SORT_ASC,1);
  147. foreach ($files as $key => $file){
  148. $file['date']=$db->idate($objd->date);
  149. $file['paid']=$objd->paid;
  150. $file['amount']=$objd->total_ttc;
  151. $file['ref']=$objd->ref;
  152. $file['fk']=$objd->fk_soc;
  153. $file['item']=$objd->item;
  154. $file['link']=$link.$file['name'];
  155. $out.= '<br><a href="'.$link.$file['name'].'">'.$file['name'].'</a>';
  156. $filesarray[]=$file;
  157. }
  158. if(count($files)<1){
  159. $nofile['date']=$db->idate($objd->date);
  160. $nofile['paid']=$objd->paid;
  161. $nofile['amount']=$objd->total_ttc;
  162. $nofile['ref']=$objd->ref;
  163. $nofile['fk']=$objd->fk_soc;
  164. $nofile['item']=$objd->item;
  165. $filesarray[]=$nofile;
  166. }
  167. }
  168. $i++;
  169. }
  170. }
  171. $db->free($resd);
  172. }
  173. /*
  174. * cleanup of old ZIP
  175. */
  176. //FIXME
  177. /*
  178. *ZIP creation
  179. */
  180. if($result & $action=="dl"){
  181. unset($zip);
  182. $log='date,type,ref,total,paid,filename,item_id'."\n";
  183. $zipname = ($date_start)."-".($date_stop).'_export.zip';
  184. $zip = new ZipArchive;
  185. $res = $zip->open($zipname, ZipArchive::OVERWRITE|ZipArchive::CREATE);
  186. if ($res){
  187. foreach ($filesarray as $key=> $file) {
  188. if(file_exists($file["fullname"])) $zip->addFile($file["fullname"],$file["name"]);//
  189. $log.=$file['date'].','.$file['item'].','.$file['ref'].','.$file['amount'].','.$file['paid'].','.$file["name"].','.$file['fk']."\n";
  190. }
  191. $zip->addFromString('log.csv', $log);
  192. $zip->close();
  193. ///Then download the zipped file.
  194. header('Content-Type: application/zip');
  195. header('Content-disposition: attachment; filename='.$zipname);
  196. header('Content-Length: ' . filesize($zipname));
  197. readfile($zipname);
  198. unlink($zipname);
  199. exit();
  200. }
  201. }
  202. // None
  203. /*
  204. * View
  205. */
  206. $form = new Form($db);
  207. $userstatic=new User($db);
  208. $title=$langs->trans("ComptaFiles").' - '.$langs->trans("List");
  209. //if (! empty($conf->global->MAIN_HTML_TITLE) && preg_match('/thirdpartynameonly/',$conf->global->MAIN_HTML_TITLE) && $object->name) $title=$object->name.' - '.$langs->trans("Symmary");
  210. $help_url='EN:Module_Accounting|FR:Module_Compatibilite'; //FIXME
  211. llxHeader('',$title,$help_url);
  212. print '<div><form name="searchfiles" action="?action=searchfiles'.$tail.'" method="POST" >'."\n\t\t\t";
  213. print '<a>'.$langs->trans("dateStart").': '.$form->select_date($date_start,'date_start',0,0,0,"",1,1,1)."\n</a>";
  214. print '<a>'.$langs->trans("dateStop").': '.$form->select_date($date_stop,'date_stop',0,0,0,"",1,1,1)."\n</a>";
  215. print '<input class="butAction" type="submit" value="Go" /></form></div>'."\n\t\t";
  216. if (!empty($date_start) && !empty($date_stop))echo dol_print_date($date_start)." - ".dol_print_date($date_stop);
  217. print '<table class="noborder" width="100%">';
  218. print '<tr class="liste_titre">';
  219. //if (! empty($arrayfields['f.datef']['checked']))
  220. print_liste_field_titre($arrayfields['date']['label'],$_SERVER["PHP_SELF"],"date","",$param,'align="center" class="nowrap"',$sortfield,$sortorder);
  221. print '<td>'.$langs->trans("Type").'</td>';
  222. print '<td align="right">'.$langs->trans("Ref").'</td>';
  223. print '<td>'.$langs->trans("File").'</td>';
  224. print '<td>'.$langs->trans("Paid").'</td>';
  225. print '<td align="right">'.$langs->trans("Debit").'</td>';
  226. print '<td align="right">'.$langs->trans("Credit").'</td>';
  227. print '<td align="right">'.$langs->trans("Balance").'</td>';
  228. print '</tr>';
  229. if ($result)
  230. {
  231. $TData = dol_sort_array($filesarray, 'date', 'ASC');
  232. if(empty($TData)) {
  233. print '<tr class="oddeven"><td colspan="7">'.$langs->trans("NoItem").'</td></tr>';
  234. } else {
  235. // Sort array by date ASC to calucalte balance
  236. $totalDebit = 0;
  237. $totalCredit = 0;
  238. // Balance calculation
  239. $balance = 0;
  240. foreach($TData as &$data1) {
  241. if($data1['item']!='Invoice'&& $data1['item']!='Donation' ){
  242. $data1['amount']=-$data1['amount'];
  243. }
  244. if ($data1['amount']>0){
  245. }else{
  246. }
  247. $balance += $data1['amount'];
  248. $data1['balance'] = $balance;
  249. }
  250. // Display array
  251. foreach($TData as $data) {
  252. $html_class = '';
  253. //if (!empty($data['fk_facture'])) $html_class = 'facid-'.$data['fk_facture'];
  254. //elseif (!empty($data['fk_paiement'])) $html_class = 'payid-'.$data['fk_paiement'];
  255. print '<tr class="oddeven '.$html_class.'">';
  256. print "<td align=\"center\">";
  257. print dol_print_date($data['date'],'day');
  258. print "</td>\n";
  259. print '<td aling="left">'.$data['item'].'</td>';
  260. print '<td aling="left">'.$data['ref'].'</td>';
  261. print '<td> <a href='.$data['link'].">".$data['name']."</a></td>\n";
  262. print '<td aling="left">'.$data['paid'].'</td>';
  263. print '<td align="right">'.(($data['amount'] > 0) ? price(abs($data['amount'])) : '')."</td>\n";
  264. $totalDebit += ($data['amount'] > 0) ? abs($data['amount']) : 0;
  265. print '<td align="right">'.(($data['amount'] > 0) ? '' : price(abs($data['amount'])))."</td>\n";
  266. $totalCredit += ($data['amount'] > 0) ? 0 : abs($data['amount']);
  267. // Balance
  268. print '<td align="right">'.price($data['balance'])."</td>\n";
  269. print "</tr>\n";
  270. }
  271. print '<tr class="liste_total">';
  272. print '<td colspan="5">&nbsp;</td>';
  273. print '<td align="right">'.price($totalDebit).'</td>';
  274. print '<td align="right">'.price($totalCredit).'</td>';
  275. print '<td align="right">'.price(price2num($totalDebit - $totalCredit, 'MT')).'</td>';
  276. print '<td></td>';
  277. print "</tr>\n";
  278. }
  279. }
  280. print "</table>";
  281. print '<form name="dl" action="?action=dl" method="POST" >'."\n\t\t\t";
  282. print '<input type="hidden" name="date_start" value="'.dol_print_date($date_start,'dayxcard').'" />';
  283. print '<input type="hidden" name="date_stop" value="'.dol_print_date($date_stop, 'dayxcard').'" />';
  284. //print '<input type="hidden" name="date_stopDay" value="'.dol_print_date($date_stop, '%d').'" />';
  285. //print '<input type="hidden" name="date_stopMonth" value="'.dol_print_date($date_stop, '%m').'" />';
  286. //print '<input type="hidden" name="date_stopYear" value="'.dol_print_date($date_stop, '%Y').'" />';
  287. //print '<input type="hidden" name="date_startDay" value="'.dol_print_date($date_start, '%d').'" />';
  288. //print '<input type="hidden" name="date_startMonth" value="'.dol_print_date($date_start, '%m').'" />';
  289. //print '<input type="hidden" name="date_startYear" value="'.dol_print_date($date_start, '%m').'" />';
  290. print '<input class="butAction" type="submit" value="Download" /></form>'."\n\t\t</th>\n\t\t<th>\n\t\t\t";
  291. llxFooter();
  292. $db->close();