accountancyexport.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. /*
  3. * Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2015 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  7. * Copyright (C) 2016 Pierre-Henry Favre <phf@atm-consulting.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/accountancy/class/accountancyexport.class.php
  24. */
  25. /**
  26. * Class AccountancyExport
  27. *
  28. * Manage the different format accountancy export
  29. */
  30. require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
  31. class AccountancyExport
  32. {
  33. /**
  34. *
  35. * @var Type of export
  36. */
  37. public static $EXPORT_TYPE_NORMAL = 1;
  38. public static $EXPORT_TYPE_CEGID = 2;
  39. public static $EXPORT_TYPE_COALA = 3;
  40. public static $EXPORT_TYPE_BOB50 = 4;
  41. public static $EXPORT_TYPE_CIEL = 5;
  42. public static $EXPORT_TYPE_QUADRATUS = 6;
  43. public static $EXPORT_TYPE_EBP = 7;
  44. public static $EXPORT_TYPE_COGILOG = 8;
  45. /**
  46. *
  47. * @var string[] Error codes (or messages)
  48. */
  49. public $errors = array ();
  50. /**
  51. *
  52. * @var string Separator
  53. */
  54. public $separator = '';
  55. /**
  56. *
  57. * @var string End of line
  58. */
  59. public $end_line = '';
  60. /**
  61. * Constructor
  62. *
  63. * @param DoliDb $db Database handler
  64. */
  65. public function __construct(DoliDB &$db) {
  66. global $conf;
  67. $this->db = &$db;
  68. $this->separator = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  69. $this->end_line = "\n";
  70. }
  71. /**
  72. * Get all export type are available
  73. *
  74. * @return array of type
  75. */
  76. public static function getType() {
  77. global $langs;
  78. return array (
  79. self::$EXPORT_TYPE_NORMAL => $langs->trans('Modelcsv_normal'),
  80. self::$EXPORT_TYPE_CEGID => $langs->trans('Modelcsv_CEGID'),
  81. self::$EXPORT_TYPE_COALA => $langs->trans('Modelcsv_COALA'),
  82. self::$EXPORT_TYPE_BOB50 => $langs->trans('Modelcsv_bob50'),
  83. self::$EXPORT_TYPE_CIEL => $langs->trans('Modelcsv_ciel'),
  84. self::$EXPORT_TYPE_QUADRATUS => $langs->trans('Modelcsv_quadratus'),
  85. self::$EXPORT_TYPE_EBP => $langs->trans('Modelcsv_ebp'),
  86. self::$EXPORT_TYPE_COGILOG => $langs->trans('Modelcsv_cogilog'),
  87. );
  88. }
  89. /**
  90. * Download the export
  91. *
  92. * @return void
  93. */
  94. public static function downloadFile() {
  95. global $conf;
  96. $journal = 'bookkepping';
  97. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  98. }
  99. /**
  100. * Function who chose which export to use with the default config
  101. *
  102. * @param unknown $TData data
  103. */
  104. public function export(&$TData) {
  105. global $conf, $langs;
  106. self::downloadFile();
  107. switch ($conf->global->ACCOUNTING_EXPORT_MODELCSV) {
  108. case self::$EXPORT_TYPE_NORMAL :
  109. $this->exportNormal($TData);
  110. break;
  111. case self::$EXPORT_TYPE_CEGID :
  112. $this->exportCegid($TData);
  113. break;
  114. case self::$EXPORT_TYPE_COALA :
  115. $this->exportCoala($TData);
  116. break;
  117. case self::$EXPORT_TYPE_BOB50 :
  118. $this->exportBob50($TData);
  119. break;
  120. case self::$EXPORT_TYPE_CIEL :
  121. $this->exportCiel($TData);
  122. break;
  123. case self::$EXPORT_TYPE_QUADRATUS :
  124. $this->exportQuadratus($TData);
  125. break;
  126. case self::$EXPORT_TYPE_EBP :
  127. $this->exportEbp($TData);
  128. break;
  129. case self::$EXPORT_TYPE_COGILOG :
  130. $this->exportCogilog($TData);
  131. break;
  132. default:
  133. $this->errors[] = $langs->trans('accountancy_error_modelnotfound');
  134. break;
  135. }
  136. }
  137. /**
  138. * Export format : Normal
  139. *
  140. * @param array $objectLines data
  141. *
  142. * @return void
  143. */
  144. public function exportNormal($objectLines) {
  145. global $conf;
  146. foreach ( $objectLines as $line ) {
  147. // Std export
  148. $date = dol_print_date($line->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
  149. print $date . $this->separator;
  150. print $line->doc_ref . $this->separator;
  151. print length_accountg($line->numero_compte) . $this->separator;
  152. print length_accounta($line->code_tiers) . $this->separator;
  153. print price($line->debit) . $this->separator;
  154. print price($line->credit) . $this->separator;
  155. print $line->code_journal . $this->separator;
  156. print $this->end_line;
  157. }
  158. }
  159. /**
  160. * Export format : CEGID
  161. *
  162. * @param array $objectLines data
  163. *
  164. * @return void
  165. */
  166. public function exportCegid($objectLines) {
  167. foreach ( $objectLines as $line ) {
  168. $date = dol_print_date($line->doc_date, '%d%m%Y');
  169. print $date . $this->separator;
  170. print $line->code_journal . $this->separator;
  171. print length_accountg($line->numero_compte) . $this->separator;
  172. print ' ' . $this->separator;
  173. print $line->sens . $this->separator;
  174. print price($line->montant) . $this->separator;
  175. print dol_trunc($line->label_compte, 32) . $this->separator;
  176. print $line->doc_ref . $this->separator;
  177. print $this->end_line;
  178. }
  179. }
  180. /**
  181. * Export format : COGILOG
  182. *
  183. * @param array $objectLines data
  184. *
  185. * @return void
  186. */
  187. public function exportCogilog($objectLines) {
  188. foreach ( $objectLines as $line ) {
  189. $date = dol_print_date($line->doc_date, '%d%m%Y');
  190. print $line->code_journal . $this->separator;
  191. print $date . $this->separator;
  192. print $line->piece_num . $this->separator;
  193. print length_accountg($line->numero_compte) . $this->separator;
  194. print '' . $this->separator;
  195. print $line->label_compte . $this->separator;
  196. print $date . $this->separator;
  197. if ($line->sens=='D') {
  198. print price($line->montant) . $this->separator;
  199. print '' . $this->separator;
  200. }elseif ($line->sens=='C') {
  201. print '' . $this->separator;
  202. print price($line->montant) . $this->separator;
  203. }
  204. print $line->doc_ref . $this->separator;
  205. print $line->label_compte . $this->separator;
  206. print $this->end_line;
  207. }
  208. }
  209. /**
  210. * Export format : COALA
  211. *
  212. * @param array $objectLines data
  213. *
  214. * @return void
  215. */
  216. public function exportCoala($objectLines) {
  217. // Coala export
  218. foreach ( $objectLines as $line ) {
  219. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  220. print $date . $this->separator;
  221. print $line->code_journal . $this->separator;
  222. print length_accountg($line->numero_compte) . $this->separator;
  223. print $line->piece_num . $this->separator;
  224. print $line->doc_ref . $this->separator;
  225. print price($line->debit) . $this->separator;
  226. print price($line->credit) . $this->separator;
  227. print 'E' . $this->separator;
  228. print length_accountg($line->code_tiers) . $this->separator;
  229. print $this->end_line;
  230. }
  231. }
  232. /**
  233. * Export format : BOB50
  234. *
  235. * @param array $objectLines data
  236. *
  237. * @return void
  238. */
  239. public function exportBob50($objectLines) {
  240. // Bob50
  241. foreach ( $objectLines as $line ) {
  242. print $line->piece_num . $this->separator;
  243. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  244. print $date . $this->separator;
  245. if (empty($line->code_tiers)) {
  246. print 'G' . $this->separator;
  247. print length_accounta($line->numero_compte) . $this->separator;
  248. } else {
  249. if (substr($line->numero_compte, 0, 3) == '411') {
  250. print 'C' . $this->separator;
  251. }
  252. if (substr($line->numero_compte, 0, 3) == '401') {
  253. print 'F' . $this->separator;
  254. }
  255. print length_accountg($line->code_tiers) . $this->separator;
  256. }
  257. print price($line->debit) . $this->separator;
  258. print price($line->credit) . $this->separator;
  259. print dol_trunc($line->label_compte, 32) . $this->separator;
  260. print $this->end_line;
  261. }
  262. }
  263. /**
  264. * Export format : CIEL
  265. *
  266. * @param array $TData data
  267. *
  268. * @return void
  269. */
  270. public function exportCiel(&$TData) {
  271. global $conf;
  272. $i = 1;
  273. $date_ecriture = dol_print_date(time(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be yyyymmdd
  274. foreach ( $TData as $data ) {
  275. $code_compta = $data->numero_compte;
  276. if (! empty($data->code_tiers))
  277. $code_compta = $data->code_tiers;
  278. $Tab = array ();
  279. $Tab['num_ecriture'] = str_pad($i, 5);
  280. $Tab['code_journal'] = str_pad($data->code_journal, 2);
  281. $Tab['date_ecriture'] = $date_ecriture;
  282. $Tab['date_ope'] = dol_print_date($data->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
  283. $Tab['num_piece'] = str_pad(self::trunc($data->piece_num, 12), 12);
  284. $Tab['num_compte'] = str_pad(self::trunc($code_compta, 11), 11);
  285. $Tab['libelle_ecriture'] = str_pad(self::trunc($data->doc_ref . $data->label_compte, 25), 25);
  286. $Tab['montant'] = str_pad(abs($data->montant), 13, ' ', STR_PAD_LEFT);
  287. $Tab['type_montant'] = str_pad($data->sens, 1);
  288. $Tab['vide'] = str_repeat(' ', 18);
  289. $Tab['intitule_compte'] = str_pad(self::trunc($data->label_compte, 34), 34);
  290. $Tab['end'] = 'O2003';
  291. $Tab['end_line'] = $this->end_line;
  292. print implode($Tab);
  293. $i ++;
  294. }
  295. }
  296. /**
  297. * Export format : Quadratus
  298. *
  299. * @param array $TData data
  300. *
  301. * @return void
  302. */
  303. public function exportQuadratus(&$TData) {
  304. global $conf;
  305. $date_ecriture = dol_print_date(time(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
  306. foreach ( $TData as $data ) {
  307. $code_compta = $data->numero_compte;
  308. if (! empty($data->code_tiers))
  309. $code_compta = $data->code_tiers;
  310. $Tab = array ();
  311. $Tab['type_ligne'] = 'M';
  312. $Tab['num_compte'] = str_pad(self::trunc($code_compta, 8), 8);
  313. $Tab['code_journal'] = str_pad(self::trunc($data->code_journal, 2), 2);
  314. $Tab['folio'] = '000';
  315. $Tab['date_ecriture'] = $date_ecriture;
  316. $Tab['filler'] = ' ';
  317. $Tab['libelle_ecriture'] = str_pad(self::trunc($data->doc_ref . ' ' . $data->label_compte, 20), 20);
  318. $Tab['sens'] = $data->sens; // C or D
  319. $Tab['signe_montant'] = '+';
  320. $Tab['montant'] = str_pad(abs($data->montant) * 100, 12, '0', STR_PAD_LEFT); // TODO manage negative amount
  321. $Tab['contrepartie'] = str_repeat(' ', 8);
  322. if (! empty($data->date_echeance))
  323. $Tab['date_echeance'] = dol_print_date($data->date_echeance, $conf->global->ACCOUNTING_EXPORT_DATE);
  324. else
  325. $Tab['date_echeance'] = '000000';
  326. $Tab['lettrage'] = str_repeat(' ', 5);
  327. $Tab['num_piece'] = str_pad(self::trunc($data->piece_num, 5), 5);
  328. $Tab['filler2'] = str_repeat(' ', 20);
  329. $Tab['num_piece2'] = str_pad(self::trunc($data->piece_num, 8), 8);
  330. $Tab['devis'] = str_pad($conf->currency, 3);
  331. $Tab['code_journal2'] = str_pad(self::trunc($data->code_journal, 3), 3);
  332. $Tab['filler3'] = str_repeat(' ', 3);
  333. $Tab['libelle_ecriture2'] = str_pad(self::trunc($data->doc_ref . ' ' . $data->label_compte, 32), 32);
  334. $Tab['num_piece3'] = str_pad(self::trunc($data->piece_num, 10), 10);
  335. $Tab['filler4'] = str_repeat(' ', 73);
  336. $Tab['end_line'] = $this->end_line;
  337. print implode($Tab);
  338. }
  339. }
  340. /**
  341. * Export format : Normal
  342. *
  343. * @param array $objectLines data
  344. *
  345. * @return void
  346. */
  347. public function exportEbp($objectLines) {
  348. $this->separator = ',';
  349. foreach ( $objectLines as $line ) {
  350. $date = dol_print_date($line->doc_date, '%d%m%Y');
  351. print $line->id . $this->separator;
  352. print $date . $this->separator;
  353. print $line->code_journal . $this->separator;
  354. print length_accountg($line->numero_compte) . $this->separator;
  355. print substr(length_accountg($line->numero_compte),0,2) . $this->separator;
  356. print '"'.dol_trunc($line->label_compte,40,'right','UTF-8',1).'"' . $this->separator;
  357. print '"'.dol_trunc($line->piece_num,15,'right','UTF-8',1)."'".$this->separator;
  358. print price2num($line->montant).$this->separator;
  359. print $line->sens.$this->separator;
  360. print $date . $this->separator;
  361. print 'EUR';
  362. print $this->end_line;
  363. }
  364. }
  365. /**
  366. *
  367. * @param unknown $str data
  368. * @param integer $size data
  369. */
  370. public static function trunc($str, $size) {
  371. return dol_trunc($str, $size, 'right', 'UTF-8', 1);
  372. }
  373. }