accountancyexport.class.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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. * Copyright (C) 2016-2023 Alexandre Spangaro <aspangaro@open-dsi.fr>
  9. * Copyright (C) 2022 Lionel Vessiller <lvessiller@open-dsi.fr>
  10. * Copyright (C) 2013-2017 Olivier Geffroy <jeff@jeffinfo.com>
  11. * Copyright (C) 2017 Elarifr. Ari Elbaz <github@accedinfo.com>
  12. * Copyright (C) 2017-2019 Frédéric France <frederic.france@netlogic.fr>
  13. * Copyright (C) 2017 André Schild <a.schild@aarboard.ch>
  14. * Copyright (C) 2020 Guillaume Alexandre <guillaume@tag-info.fr>
  15. * Copyright (C) 2022 Joachim Kueter <jkueter@gmx.de>
  16. * Copyright (C) 2022 Progiseize <a.bisotti@progiseize.fr>
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  30. */
  31. /**
  32. * \file htdocs/accountancy/class/accountancyexport.class.php
  33. * \ingroup Accountancy (Double entries)
  34. * \brief Class accountancy export
  35. */
  36. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  38. require_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  40. /**
  41. * Manage the different format accountancy export
  42. */
  43. class AccountancyExport
  44. {
  45. // Type of export. Used into $conf->global->ACCOUNTING_EXPORT_MODELCSV
  46. public static $EXPORT_TYPE_CONFIGURABLE = 1; // CSV
  47. public static $EXPORT_TYPE_AGIRIS = 10;
  48. public static $EXPORT_TYPE_EBP = 15;
  49. public static $EXPORT_TYPE_CEGID = 20;
  50. public static $EXPORT_TYPE_COGILOG = 25;
  51. public static $EXPORT_TYPE_COALA = 30;
  52. public static $EXPORT_TYPE_BOB50 = 35;
  53. public static $EXPORT_TYPE_CIEL = 40;
  54. public static $EXPORT_TYPE_SAGE50_SWISS = 45;
  55. public static $EXPORT_TYPE_CHARLEMAGNE = 50;
  56. public static $EXPORT_TYPE_QUADRATUS = 60;
  57. public static $EXPORT_TYPE_WINFIC = 70;
  58. public static $EXPORT_TYPE_OPENCONCERTO = 100;
  59. public static $EXPORT_TYPE_LDCOMPTA = 110;
  60. public static $EXPORT_TYPE_LDCOMPTA10 = 120;
  61. public static $EXPORT_TYPE_GESTIMUMV3 = 130;
  62. public static $EXPORT_TYPE_GESTIMUMV5 = 135;
  63. public static $EXPORT_TYPE_ISUITEEXPERT = 200;
  64. // Generic FEC after that
  65. public static $EXPORT_TYPE_FEC = 1000;
  66. public static $EXPORT_TYPE_FEC2 = 1010;
  67. /**
  68. * @var DoliDB Database handler
  69. */
  70. public $db;
  71. /**
  72. * @var string[] Error codes (or messages)
  73. */
  74. public $errors = array();
  75. /**
  76. *
  77. * @var string Separator
  78. */
  79. public $separator = '';
  80. /**
  81. *
  82. * @var string End of line
  83. */
  84. public $end_line = '';
  85. /**
  86. * Constructor
  87. *
  88. * @param DoliDb $db Database handler
  89. */
  90. public function __construct(DoliDB $db)
  91. {
  92. global $conf, $hookmanager;
  93. $this->db = $db;
  94. $this->separator = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  95. $this->end_line = empty($conf->global->ACCOUNTING_EXPORT_ENDLINE) ? "\n" : ($conf->global->ACCOUNTING_EXPORT_ENDLINE == 1 ? "\n" : "\r\n");
  96. $hookmanager->initHooks(array('accountancyexport'));
  97. }
  98. /**
  99. * Array with all export type available (key + label)
  100. *
  101. * @return array of type
  102. */
  103. public function getType()
  104. {
  105. global $langs, $hookmanager;
  106. $listofexporttypes = array(
  107. self::$EXPORT_TYPE_CONFIGURABLE => $langs->trans('Modelcsv_configurable'),
  108. self::$EXPORT_TYPE_CEGID => $langs->trans('Modelcsv_CEGID'),
  109. self::$EXPORT_TYPE_COALA => $langs->trans('Modelcsv_COALA'),
  110. self::$EXPORT_TYPE_BOB50 => $langs->trans('Modelcsv_bob50'),
  111. self::$EXPORT_TYPE_CIEL => $langs->trans('Modelcsv_ciel'),
  112. self::$EXPORT_TYPE_QUADRATUS => $langs->trans('Modelcsv_quadratus'),
  113. self::$EXPORT_TYPE_WINFIC => $langs->trans('Modelcsv_winfic'),
  114. self::$EXPORT_TYPE_EBP => $langs->trans('Modelcsv_ebp'),
  115. self::$EXPORT_TYPE_COGILOG => $langs->trans('Modelcsv_cogilog'),
  116. self::$EXPORT_TYPE_AGIRIS => $langs->trans('Modelcsv_agiris'),
  117. self::$EXPORT_TYPE_OPENCONCERTO => $langs->trans('Modelcsv_openconcerto'),
  118. self::$EXPORT_TYPE_SAGE50_SWISS => $langs->trans('Modelcsv_Sage50_Swiss'),
  119. self::$EXPORT_TYPE_CHARLEMAGNE => $langs->trans('Modelcsv_charlemagne'),
  120. self::$EXPORT_TYPE_LDCOMPTA => $langs->trans('Modelcsv_LDCompta'),
  121. self::$EXPORT_TYPE_LDCOMPTA10 => $langs->trans('Modelcsv_LDCompta10'),
  122. self::$EXPORT_TYPE_GESTIMUMV3 => $langs->trans('Modelcsv_Gestinumv3'),
  123. self::$EXPORT_TYPE_GESTIMUMV5 => $langs->trans('Modelcsv_Gestinumv5'),
  124. self::$EXPORT_TYPE_FEC => $langs->trans('Modelcsv_FEC'),
  125. self::$EXPORT_TYPE_FEC2 => $langs->trans('Modelcsv_FEC2'),
  126. self::$EXPORT_TYPE_ISUITEEXPERT => 'Export iSuite Expert',
  127. );
  128. // allow modules to define export formats
  129. $parameters = array();
  130. $reshook = $hookmanager->executeHooks('getType', $parameters, $listofexporttypes);
  131. ksort($listofexporttypes, SORT_NUMERIC);
  132. return $listofexporttypes;
  133. }
  134. /**
  135. * Return string to summarize the format (Used to generated export filename)
  136. *
  137. * @param int $type Format id
  138. * @return string Format code
  139. */
  140. public static function getFormatCode($type)
  141. {
  142. $formatcode = array(
  143. self::$EXPORT_TYPE_CONFIGURABLE => 'csv',
  144. self::$EXPORT_TYPE_CEGID => 'cegid',
  145. self::$EXPORT_TYPE_COALA => 'coala',
  146. self::$EXPORT_TYPE_BOB50 => 'bob50',
  147. self::$EXPORT_TYPE_CIEL => 'ciel',
  148. self::$EXPORT_TYPE_QUADRATUS => 'quadratus',
  149. self::$EXPORT_TYPE_WINFIC => 'winfic',
  150. self::$EXPORT_TYPE_EBP => 'ebp',
  151. self::$EXPORT_TYPE_COGILOG => 'cogilog',
  152. self::$EXPORT_TYPE_AGIRIS => 'agiris',
  153. self::$EXPORT_TYPE_OPENCONCERTO => 'openconcerto',
  154. self::$EXPORT_TYPE_SAGE50_SWISS => 'sage50ch',
  155. self::$EXPORT_TYPE_CHARLEMAGNE => 'charlemagne',
  156. self::$EXPORT_TYPE_LDCOMPTA => 'ldcompta',
  157. self::$EXPORT_TYPE_LDCOMPTA10 => 'ldcompta10',
  158. self::$EXPORT_TYPE_GESTIMUMV3 => 'gestimumv3',
  159. self::$EXPORT_TYPE_GESTIMUMV5 => 'gestimumv5',
  160. self::$EXPORT_TYPE_FEC => 'fec',
  161. self::$EXPORT_TYPE_FEC2 => 'fec2',
  162. self::$EXPORT_TYPE_ISUITEEXPERT => 'isuiteexpert',
  163. );
  164. global $hookmanager;
  165. $code = $formatcode[$type];
  166. $parameters = array('type' => $type);
  167. $reshook = $hookmanager->executeHooks('getFormatCode', $parameters, $code);
  168. return $code;
  169. }
  170. /**
  171. * Array with all export type available (key + label) and parameters for config
  172. *
  173. * @return array of type
  174. */
  175. public function getTypeConfig()
  176. {
  177. global $conf, $langs;
  178. $exporttypes = array(
  179. 'param' => array(
  180. self::$EXPORT_TYPE_CONFIGURABLE => array(
  181. 'label' => $langs->trans('Modelcsv_configurable'),
  182. 'ACCOUNTING_EXPORT_FORMAT' => empty($conf->global->ACCOUNTING_EXPORT_FORMAT) ? 'txt' : $conf->global->ACCOUNTING_EXPORT_FORMAT,
  183. 'ACCOUNTING_EXPORT_SEPARATORCSV' => empty($conf->global->ACCOUNTING_EXPORT_SEPARATORCSV) ? ',' : $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV,
  184. 'ACCOUNTING_EXPORT_ENDLINE' => empty($conf->global->ACCOUNTING_EXPORT_ENDLINE) ? 1 : $conf->global->ACCOUNTING_EXPORT_ENDLINE,
  185. 'ACCOUNTING_EXPORT_DATE' => getDolGlobalString('ACCOUNTING_EXPORT_DATE', '%Y-%m-%d'),
  186. ),
  187. self::$EXPORT_TYPE_CEGID => array(
  188. 'label' => $langs->trans('Modelcsv_CEGID'),
  189. ),
  190. self::$EXPORT_TYPE_COALA => array(
  191. 'label' => $langs->trans('Modelcsv_COALA'),
  192. ),
  193. self::$EXPORT_TYPE_BOB50 => array(
  194. 'label' => $langs->trans('Modelcsv_bob50'),
  195. ),
  196. self::$EXPORT_TYPE_CIEL => array(
  197. 'label' => $langs->trans('Modelcsv_ciel'),
  198. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  199. ),
  200. self::$EXPORT_TYPE_QUADRATUS => array(
  201. 'label' => $langs->trans('Modelcsv_quadratus'),
  202. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  203. ),
  204. self::$EXPORT_TYPE_WINFIC => array(
  205. 'label' => $langs->trans('Modelcsv_winfic'),
  206. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  207. ),
  208. self::$EXPORT_TYPE_EBP => array(
  209. 'label' => $langs->trans('Modelcsv_ebp'),
  210. ),
  211. self::$EXPORT_TYPE_COGILOG => array(
  212. 'label' => $langs->trans('Modelcsv_cogilog'),
  213. ),
  214. self::$EXPORT_TYPE_AGIRIS => array(
  215. 'label' => $langs->trans('Modelcsv_agiris'),
  216. ),
  217. self::$EXPORT_TYPE_OPENCONCERTO => array(
  218. 'label' => $langs->trans('Modelcsv_openconcerto'),
  219. ),
  220. self::$EXPORT_TYPE_SAGE50_SWISS => array(
  221. 'label' => $langs->trans('Modelcsv_Sage50_Swiss'),
  222. ),
  223. self::$EXPORT_TYPE_CHARLEMAGNE => array(
  224. 'label' => $langs->trans('Modelcsv_charlemagne'),
  225. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  226. ),
  227. self::$EXPORT_TYPE_LDCOMPTA => array(
  228. 'label' => $langs->trans('Modelcsv_LDCompta'),
  229. ),
  230. self::$EXPORT_TYPE_LDCOMPTA10 => array(
  231. 'label' => $langs->trans('Modelcsv_LDCompta10'),
  232. ),
  233. self::$EXPORT_TYPE_GESTIMUMV3 => array(
  234. 'label' => $langs->trans('Modelcsv_Gestinumv3'),
  235. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  236. ),
  237. self::$EXPORT_TYPE_GESTIMUMV5 => array(
  238. 'label' => $langs->trans('Modelcsv_Gestinumv5'),
  239. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  240. ),
  241. self::$EXPORT_TYPE_FEC => array(
  242. 'label' => $langs->trans('Modelcsv_FEC'),
  243. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  244. ),
  245. self::$EXPORT_TYPE_FEC2 => array(
  246. 'label' => $langs->trans('Modelcsv_FEC2'),
  247. 'ACCOUNTING_EXPORT_FORMAT' => 'txt',
  248. ),
  249. self::$EXPORT_TYPE_ISUITEEXPERT => array(
  250. 'label' => 'iSuite Expert',
  251. 'ACCOUNTING_EXPORT_FORMAT' => 'csv',
  252. ),
  253. ),
  254. 'cr'=> array(
  255. '1' => $langs->trans("Unix"),
  256. '2' => $langs->trans("Windows")
  257. ),
  258. 'format' => array(
  259. 'csv' => $langs->trans("csv"),
  260. 'txt' => $langs->trans("txt")
  261. ),
  262. );
  263. global $hookmanager;
  264. $parameters = array();
  265. $reshook = $hookmanager->executeHooks('getTypeConfig', $parameters, $exporttypes);
  266. return $exporttypes;
  267. }
  268. /**
  269. * Return the MIME type of a file
  270. *
  271. * @param int $formatexportset Id of export format
  272. * @return string MIME type.
  273. */
  274. public function getMimeType($formatexportset)
  275. {
  276. $mime = 'text/csv';
  277. switch ($formatexportset) {
  278. case self::$EXPORT_TYPE_FEC:
  279. $mime = 'text/tab-separated-values';
  280. break;
  281. default:
  282. $mime = 'text/csv';
  283. break;
  284. }
  285. return $mime;
  286. }
  287. /**
  288. * Function who chose which export to use with the default config, and make the export into a file
  289. *
  290. * @param array $TData Array with data
  291. * @param int $formatexportset Id of export format
  292. * @param int $withAttachment [=0] Not add files or 1 to have attached in an archive (ex : Quadratus)
  293. * @return int <0 if KO, >0 OK
  294. */
  295. public function export(&$TData, $formatexportset, $withAttachment = 0)
  296. {
  297. global $conf, $langs;
  298. global $search_date_end; // Used into /accountancy/tpl/export_journal.tpl.php
  299. // Define name of file to save
  300. $filename = 'general_ledger-'.$this->getFormatCode($formatexportset);
  301. $type_export = 'general_ledger';
  302. global $db; // The tpl file use $db
  303. $completefilename = '';
  304. $exportFile = null;
  305. $exportFileName = '';
  306. $exportFilePath = '';
  307. $archiveFileList = array();
  308. if ($withAttachment == 1) {
  309. // PHP ZIP extension must be enabled
  310. if (!extension_loaded('zip')) {
  311. $langs->load('install');
  312. $this->errors[] = $langs->trans('ErrorPHPDoesNotSupport', 'ZIP');
  313. return -1;
  314. }
  315. } else {
  316. $mimetype = $this->getMimeType($formatexportset);
  317. top_httphead($mimetype, 1);
  318. }
  319. include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
  320. if ($withAttachment == 1 && !empty($completefilename)) {
  321. // create export file
  322. $tmpDir = !empty($conf->accounting->multidir_temp[$conf->entity]) ? $conf->accounting->multidir_temp[$conf->entity] : $conf->accounting->dir_temp;
  323. $exportFileFullName = $completefilename;
  324. $exportFileBaseName = basename($exportFileFullName);
  325. $exportFileName = pathinfo($exportFileBaseName, PATHINFO_FILENAME);
  326. $exportFilePath = $tmpDir.'/'.$exportFileFullName;
  327. $exportFile = fopen($exportFilePath, 'w');
  328. if (!$exportFile) {
  329. $this->errors[] = $langs->trans('ErrorFileNotFound', $exportFilePath);
  330. return -1;
  331. }
  332. $archiveFileList[0] = array(
  333. 'path' => $exportFilePath,
  334. 'name' => $exportFileFullName,
  335. );
  336. // archive name and path
  337. $archiveFullName = $exportFileName.'.zip';
  338. $archivePath = $tmpDir.'/'.$archiveFullName;
  339. }
  340. switch ($formatexportset) {
  341. case self::$EXPORT_TYPE_CONFIGURABLE:
  342. $this->exportConfigurable($TData);
  343. break;
  344. case self::$EXPORT_TYPE_CEGID:
  345. $this->exportCegid($TData);
  346. break;
  347. case self::$EXPORT_TYPE_COALA:
  348. $this->exportCoala($TData);
  349. break;
  350. case self::$EXPORT_TYPE_BOB50:
  351. $this->exportBob50($TData);
  352. break;
  353. case self::$EXPORT_TYPE_CIEL:
  354. $this->exportCiel($TData);
  355. break;
  356. case self::$EXPORT_TYPE_QUADRATUS:
  357. $archiveFileList = $this->exportQuadratus($TData, $exportFile, $archiveFileList, $withAttachment);
  358. break;
  359. case self::$EXPORT_TYPE_WINFIC:
  360. $this->exportWinfic($TData);
  361. break;
  362. case self::$EXPORT_TYPE_EBP:
  363. $this->exportEbp($TData);
  364. break;
  365. case self::$EXPORT_TYPE_COGILOG:
  366. $this->exportCogilog($TData);
  367. break;
  368. case self::$EXPORT_TYPE_AGIRIS:
  369. $this->exportAgiris($TData);
  370. break;
  371. case self::$EXPORT_TYPE_OPENCONCERTO:
  372. $this->exportOpenConcerto($TData);
  373. break;
  374. case self::$EXPORT_TYPE_SAGE50_SWISS:
  375. $this->exportSAGE50SWISS($TData);
  376. break;
  377. case self::$EXPORT_TYPE_CHARLEMAGNE:
  378. $this->exportCharlemagne($TData);
  379. break;
  380. case self::$EXPORT_TYPE_LDCOMPTA:
  381. $this->exportLDCompta($TData);
  382. break;
  383. case self::$EXPORT_TYPE_LDCOMPTA10:
  384. $this->exportLDCompta10($TData);
  385. break;
  386. case self::$EXPORT_TYPE_GESTIMUMV3:
  387. $this->exportGestimumV3($TData);
  388. break;
  389. case self::$EXPORT_TYPE_GESTIMUMV5:
  390. $this->exportGestimumV5($TData);
  391. break;
  392. case self::$EXPORT_TYPE_FEC:
  393. $this->exportFEC($TData);
  394. break;
  395. case self::$EXPORT_TYPE_FEC2:
  396. $this->exportFEC2($TData);
  397. break;
  398. case self::$EXPORT_TYPE_ISUITEEXPERT :
  399. $this->exportiSuiteExpert($TData);
  400. break;
  401. default:
  402. global $hookmanager;
  403. $parameters = array('format' => $formatexportset);
  404. // file contents will be created in the hooked function via print
  405. $reshook = $hookmanager->executeHooks('export', $parameters, $TData);
  406. if ($reshook != 1) {
  407. $this->errors[] = $langs->trans('accountancy_error_modelnotfound');
  408. }
  409. break;
  410. }
  411. // create and download export file or archive
  412. if ($withAttachment == 1) {
  413. $error = 0;
  414. // close export file
  415. if ($exportFile) {
  416. fclose($exportFile);
  417. }
  418. if (!empty($archiveFullName) && !empty($archivePath) && !empty($archiveFileList)) {
  419. // archive files
  420. $downloadFileMimeType = 'application/zip';
  421. $downloadFileFullName = $archiveFullName;
  422. $downloadFilePath = $archivePath;
  423. // create archive
  424. $archive = new ZipArchive();
  425. $res = $archive->open($archivePath, ZipArchive::OVERWRITE | ZipArchive::CREATE);
  426. if ($res !== true) {
  427. $error++;
  428. $this->errors[] = $langs->trans('ErrorFileNotFound', $archivePath);
  429. }
  430. if (!$error) {
  431. // add files
  432. foreach ($archiveFileList as $archiveFileArr) {
  433. $res = $archive->addFile($archiveFileArr['path'], $archiveFileArr['name']);
  434. if (!$res) {
  435. $error++;
  436. $this->errors[] = $langs->trans('ErrorArchiveAddFile', $archiveFileArr['name']);
  437. break;
  438. }
  439. }
  440. }
  441. if (!$error) {
  442. // close archive
  443. $archive->close();
  444. }
  445. } elseif (!empty($exportFileFullName) && !empty($exportFilePath)) {
  446. // only one file to download
  447. $downloadFileMimeType = 'text/csv';
  448. $downloadFileFullName = $exportFileFullName;
  449. $downloadFilePath = $exportFilePath;
  450. }
  451. if (!$error) {
  452. // download export file
  453. if (!empty($downloadFileMimeType) && !empty($downloadFileFullName) && !empty($downloadFilePath)) {
  454. header('Content-Type: '.$downloadFileMimeType);
  455. header('Content-Disposition: attachment; filename='.$downloadFileFullName);
  456. header('Cache-Control: Public, must-revalidate');
  457. header('Pragma: public');
  458. header('Content-Length: '.dol_filesize($downloadFilePath));
  459. readfileLowMemory($downloadFilePath);
  460. }
  461. }
  462. if ($error) {
  463. return -1;
  464. }
  465. }
  466. return 1;
  467. }
  468. /**
  469. * Export format : CEGID
  470. *
  471. * @param array $objectLines data
  472. * @return void
  473. */
  474. public function exportCegid($objectLines)
  475. {
  476. foreach ($objectLines as $line) {
  477. $date = dol_print_date($line->doc_date, '%d%m%Y');
  478. $separator = ";";
  479. $end_line = "\n";
  480. print $date.$separator;
  481. print $line->code_journal.$separator;
  482. print length_accountg($line->numero_compte).$separator;
  483. print length_accounta($line->subledger_account).$separator;
  484. print $line->sens.$separator;
  485. print price2fec(abs($line->debit - $line->credit)).$separator;
  486. print dol_string_unaccent($line->label_operation).$separator;
  487. print dol_string_unaccent($line->doc_ref);
  488. print $end_line;
  489. }
  490. }
  491. /**
  492. * Export format : COGILOG
  493. *
  494. * @param array $objectLines data
  495. * @return void
  496. */
  497. public function exportCogilog($objectLines)
  498. {
  499. foreach ($objectLines as $line) {
  500. $date = dol_print_date($line->doc_date, '%d%m%Y');
  501. $separator = ";";
  502. $end_line = "\n";
  503. print $line->code_journal.$separator;
  504. print $date.$separator;
  505. print $line->piece_num.$separator;
  506. print length_accountg($line->numero_compte).$separator;
  507. print $separator;
  508. print $line->label_operation.$separator;
  509. print $date.$separator;
  510. if ($line->sens == 'D') {
  511. print price($line->debit).$separator;
  512. print $separator;
  513. } elseif ($line->sens == 'C') {
  514. print $separator;
  515. print price($line->credit).$separator;
  516. }
  517. print $line->doc_ref.$separator;
  518. print $line->label_operation.$separator;
  519. print $end_line;
  520. }
  521. }
  522. /**
  523. * Export format : COALA
  524. *
  525. * @param array $objectLines data
  526. * @return void
  527. */
  528. public function exportCoala($objectLines)
  529. {
  530. // Coala export
  531. $separator = ";";
  532. $end_line = "\n";
  533. foreach ($objectLines as $line) {
  534. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  535. print $date.$separator;
  536. print $line->code_journal.$separator;
  537. print length_accountg($line->numero_compte).$separator;
  538. print $line->piece_num.$separator;
  539. print $line->doc_ref.$separator;
  540. print price($line->debit).$separator;
  541. print price($line->credit).$separator;
  542. print 'E'.$separator;
  543. print length_accounta($line->subledger_account).$separator;
  544. print $end_line;
  545. }
  546. }
  547. /**
  548. * Export format : BOB50
  549. *
  550. * @param array $objectLines data
  551. * @return void
  552. */
  553. public function exportBob50($objectLines)
  554. {
  555. // Bob50
  556. $separator = ";";
  557. $end_line = "\n";
  558. foreach ($objectLines as $line) {
  559. print $line->piece_num.$separator;
  560. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  561. print $date.$separator;
  562. if (empty($line->subledger_account)) {
  563. print 'G'.$separator;
  564. print length_accounta($line->numero_compte).$separator;
  565. } else {
  566. if (substr($line->numero_compte, 0, 3) == '411') {
  567. print 'C'.$separator;
  568. }
  569. if (substr($line->numero_compte, 0, 3) == '401') {
  570. print 'F'.$separator;
  571. }
  572. print length_accountg($line->subledger_account).$separator;
  573. }
  574. print price($line->debit).$separator;
  575. print price($line->credit).$separator;
  576. print dol_trunc($line->label_operation, 32).$separator;
  577. print $end_line;
  578. }
  579. }
  580. /**
  581. * Export format : CIEL (Format XIMPORT)
  582. * Format since 2003 compatible CIEL version > 2002 / Sage50
  583. * Last review for this format : 2021-09-13 Alexandre Spangaro (aspangaro@open-dsi.fr)
  584. *
  585. * Help : https://sage50c.online-help.sage.fr/aide-technique/
  586. * In sage software | Use menu : "Exchange" > "Importing entries..."
  587. *
  588. * If you want to force filename to "XIMPORT.TXT" for automatically import file present in a directory :
  589. * use constant ACCOUNTING_EXPORT_XIMPORT_FORCE_FILENAME
  590. *
  591. * @param array $TData data
  592. * @return void
  593. */
  594. public function exportCiel(&$TData)
  595. {
  596. $end_line = "\r\n";
  597. $i = 1;
  598. foreach ($TData as $data) {
  599. $code_compta = length_accountg($data->numero_compte);
  600. if (!empty($data->subledger_account)) {
  601. $code_compta = length_accounta($data->subledger_account);
  602. }
  603. $date_document = dol_print_date($data->doc_date, '%Y%m%d');
  604. $date_echeance = dol_print_date($data->date_lim_reglement, '%Y%m%d');
  605. $Tab = array();
  606. $Tab['num_ecriture'] = str_pad($data->piece_num, 5);
  607. $Tab['code_journal'] = str_pad(self::trunc($data->code_journal, 2), 2);
  608. $Tab['date_ecriture'] = str_pad($date_document, 8, ' ', STR_PAD_LEFT);
  609. $Tab['date_echeance'] = str_pad($date_echeance, 8, ' ', STR_PAD_LEFT);
  610. $Tab['num_piece'] = str_pad(self::trunc($data->doc_ref, 12), 12);
  611. $Tab['num_compte'] = str_pad(self::trunc($code_compta, 11), 11);
  612. $Tab['libelle_ecriture'] = str_pad(self::trunc(dol_string_unaccent($data->doc_ref).dol_string_unaccent($data->label_operation), 25), 25);
  613. $Tab['montant'] = str_pad(price2fec(abs($data->debit - $data->credit)), 13, ' ', STR_PAD_LEFT);
  614. $Tab['type_montant'] = str_pad($data->sens, 1);
  615. $Tab['vide'] = str_repeat(' ', 18); // Analytical accounting - Not managed in Dolibarr
  616. $Tab['intitule_compte'] = str_pad(self::trunc(dol_string_unaccent($data->label_operation), 34), 34);
  617. $Tab['end'] = 'O2003'; // 0 = EUR | 2003 = Format Ciel
  618. $Tab['end_line'] = $end_line;
  619. print implode($Tab);
  620. $i++;
  621. }
  622. }
  623. /**
  624. * Export format : Quadratus (Format ASCII)
  625. * Format since 2015 compatible QuadraCOMPTA
  626. * Last review for this format : 2023/01/28 Alexandre Spangaro (aspangaro@open-dsi.fr)
  627. *
  628. * Help : https://docplayer.fr/20769649-Fichier-d-entree-ascii-dans-quadracompta.html
  629. * In QuadraCompta | Use menu : "Outils" > "Suivi des dossiers" > "Import ASCII(Compta)"
  630. *
  631. * @param array $TData Data
  632. * @param resource $exportFile [=null] File resource to export or print if null
  633. * @param array $archiveFileList [=array()] Archive file list : array of ['path', 'name']
  634. * @param bool $withAttachment [=0] Not add files or 1 to have attached in an archive
  635. * @return array Archive file list : array of ['path', 'name']
  636. */
  637. public function exportQuadratus(&$TData, $exportFile = null, $archiveFileList = array(), $withAttachment = 0)
  638. {
  639. global $conf, $db;
  640. $end_line = "\r\n";
  641. // We should use dol_now function not time however this is wrong date to transfert in accounting
  642. // $date_ecriture = dol_print_date(dol_now(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
  643. // $date_ecriture = dol_print_date(time(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
  644. foreach ($TData as $data) {
  645. // Clean some data
  646. $data->doc_ref = dol_string_unaccent($data->doc_ref);
  647. $data->label_operation = dol_string_unaccent($data->label_operation);
  648. $data->numero_compte = dol_string_unaccent($data->numero_compte);
  649. $data->label_compte = dol_string_unaccent($data->label_compte);
  650. $data->subledger_account = dol_string_unaccent($data->subledger_account);
  651. $data->subledger_label = dol_string_unaccent($data->subledger_label);
  652. $code_compta = $data->numero_compte;
  653. if (!empty($data->subledger_account)) {
  654. $code_compta = $data->subledger_account;
  655. }
  656. $Tab = array();
  657. if (!empty($data->subledger_account)) {
  658. $Tab['type_ligne'] = 'C';
  659. $Tab['num_compte'] = str_pad(self::trunc($data->subledger_account, 8), 8);
  660. $Tab['lib_compte'] = str_pad(self::trunc($data->subledger_label, 30), 30);
  661. if ($data->doc_type == 'customer_invoice') {
  662. $Tab['lib_alpha'] = strtoupper(str_pad('C'.self::trunc($data->subledger_label, 6), 6));
  663. $Tab['filler'] = str_repeat(' ', 52);
  664. $Tab['coll_compte'] = str_pad(self::trunc($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER, 8), 8);
  665. } elseif ($data->doc_type == 'supplier_invoice') {
  666. $Tab['lib_alpha'] = strtoupper(str_pad('F'.self::trunc($data->subledger_label, 6), 6));
  667. $Tab['filler'] = str_repeat(' ', 52);
  668. $Tab['coll_compte'] = str_pad(self::trunc($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER, 8), 8);
  669. } else {
  670. $Tab['filler'] = str_repeat(' ', 59);
  671. $Tab['coll_compte'] = str_pad(' ', 8);
  672. }
  673. $Tab['filler2'] = str_repeat(' ', 110);
  674. $Tab['Maj'] = 2; // Partial update (alpha key, label, address, collectif, RIB)
  675. if ($data->doc_type == 'customer_invoice') {
  676. $Tab['type_compte'] = 'C';
  677. } elseif ($data->doc_type == 'supplier_invoice') {
  678. $Tab['type_compte'] = 'F';
  679. } else {
  680. $Tab['type_compte'] = 'G';
  681. }
  682. $Tab['filler3'] = str_repeat(' ', 235);
  683. $Tab['end_line'] = $end_line;
  684. if ($exportFile) {
  685. fwrite($exportFile, implode($Tab));
  686. } else {
  687. print implode($Tab);
  688. }
  689. }
  690. $Tab = array();
  691. $Tab['type_ligne'] = 'M';
  692. $Tab['num_compte'] = str_pad(self::trunc($code_compta, 8), 8);
  693. $Tab['code_journal'] = str_pad(self::trunc($data->code_journal, 2), 2);
  694. $Tab['folio'] = '000';
  695. // We use invoice date $data->doc_date not $date_ecriture which is the transfert date
  696. // maybe we should set an option for customer who prefer to keep in accounting software the tranfert date instead of invoice date ?
  697. //$Tab['date_ecriture'] = $date_ecriture;
  698. $Tab['date_ecriture'] = dol_print_date($data->doc_date, '%d%m%y');
  699. $Tab['filler'] = ' ';
  700. $Tab['libelle_ecriture'] = str_pad(self::trunc($data->doc_ref.' '.$data->label_operation, 20), 20);
  701. // Credit invoice - invert sens
  702. /*
  703. if ($data->montant < 0) {
  704. if ($data->sens == 'C') {
  705. $Tab['sens'] = 'D';
  706. } else {
  707. $Tab['sens'] = 'C';
  708. }
  709. $Tab['signe_montant'] = '-';
  710. } else {
  711. $Tab['sens'] = $data->sens; // C or D
  712. $Tab['signe_montant'] = '+';
  713. }*/
  714. $Tab['sens'] = $data->sens; // C or D
  715. $Tab['signe_montant'] = '+';
  716. // The amount must be in centimes without decimal points.
  717. $Tab['montant'] = str_pad(abs(($data->debit - $data->credit) * 100), 12, '0', STR_PAD_LEFT);
  718. $Tab['contrepartie'] = str_repeat(' ', 8);
  719. // Force date format : %d%m%y
  720. if (!empty($data->date_lim_reglement)) {
  721. //$Tab['date_echeance'] = dol_print_date($data->date_lim_reglement, $conf->global->ACCOUNTING_EXPORT_DATE);
  722. $Tab['date_echeance'] = dol_print_date($data->date_lim_reglement, '%d%m%y'); // Format must be ddmmyy
  723. } else {
  724. $Tab['date_echeance'] = '000000';
  725. }
  726. // Please keep quadra named field lettrage(2) + codestat(3) instead of fake lettrage(5)
  727. // $Tab['lettrage'] = str_repeat(' ', 5);
  728. $Tab['lettrage'] = str_repeat(' ', 2);
  729. $Tab['codestat'] = str_repeat(' ', 3);
  730. $Tab['num_piece'] = str_pad(self::trunc($data->piece_num, 5), 5);
  731. // Keep correct quadra named field instead of anon filler
  732. // $Tab['filler2'] = str_repeat(' ', 20);
  733. $Tab['affaire'] = str_repeat(' ', 10);
  734. $Tab['quantity1'] = str_repeat(' ', 10);
  735. $Tab['num_piece2'] = str_pad(self::trunc($data->piece_num, 8), 8);
  736. $Tab['devis'] = str_pad($conf->currency, 3);
  737. $Tab['code_journal2'] = str_pad(self::trunc($data->code_journal, 3), 3);
  738. $Tab['filler3'] = str_repeat(' ', 3);
  739. // Keep correct quadra named field instead of anon filler libelle_ecriture2 is 30 char not 32 !!!!
  740. // as we use utf8, we must remove accent to have only one ascii char instead of utf8 2 chars for specials that report wrong line size that will exceed import format spec
  741. // TODO: we should filter more than only accent to avoid wrong line size
  742. // TODO: remove invoice number doc_ref in libelle,
  743. // TODO: we should offer an option for customer to build the libelle using invoice number / name / date in accounting software
  744. //$Tab['libelle_ecriture2'] = str_pad(self::trunc($data->doc_ref . ' ' . $data->label_operation, 30), 30);
  745. $Tab['libelle_ecriture2'] = str_pad(self::trunc($data->label_operation, 30), 30);
  746. $Tab['codetva'] = str_repeat(' ', 2);
  747. // We need to keep the 10 lastest number of invoice doc_ref not the beginning part that is the unusefull almost same part
  748. // $Tab['num_piece3'] = str_pad(self::trunc($data->piece_num, 10), 10);
  749. $Tab['num_piece3'] = substr(self::trunc($data->doc_ref, 20), -10);
  750. $Tab['reserved'] = str_repeat(' ', 10); // position 159
  751. $Tab['currency_amount'] = str_repeat(' ', 13); // position 169
  752. // get document file
  753. $attachmentFileName = '';
  754. if ($withAttachment == 1) {
  755. $attachmentFileKey = trim($data->piece_num);
  756. if (!isset($archiveFileList[$attachmentFileKey])) {
  757. $objectDirPath = '';
  758. $objectFileName = dol_sanitizeFileName($data->doc_ref);
  759. if ($data->doc_type == 'customer_invoice') {
  760. $objectDirPath = !empty($conf->facture->multidir_output[$conf->entity]) ? $conf->facture->multidir_output[$conf->entity] : $conf->facture->dir_output;
  761. } elseif ($data->doc_type == 'expense_report') {
  762. $objectDirPath = !empty($conf->expensereport->multidir_output[$conf->entity]) ? $conf->expensereport->multidir_output[$conf->entity] : $conf->factureexpensereport->dir_output;
  763. } elseif ($data->doc_type == 'supplier_invoice') {
  764. $objectDirPath = !empty($conf->fournisseur->facture->multidir_output[$conf->entity]) ? $conf->fournisseur->facture->multidir_output[$conf->entity] : $conf->fournisseur->facture->dir_output;
  765. }
  766. $arrayofinclusion = array();
  767. $arrayofinclusion[] = '^'.preg_quote($objectFileName, '/').'\.pdf$';
  768. $fileFoundList = dol_dir_list($objectDirPath.'/'.$objectFileName, 'files', 0, implode('|', $arrayofinclusion), '(\.meta|_preview.*\.png)$', 'date', SORT_DESC, 0, true);
  769. if (!empty($fileFoundList)) {
  770. $attachmentFileNameTrunc = str_pad(self::trunc($data->piece_num, 8), 8, '0', STR_PAD_LEFT);
  771. foreach ($fileFoundList as $fileFound) {
  772. if (strstr($fileFound['name'], $objectFileName)) {
  773. $fileFoundPath = $objectDirPath.'/'.$objectFileName.'/'.$fileFound['name'];
  774. if (file_exists($fileFoundPath)) {
  775. $archiveFileList[$attachmentFileKey] = array(
  776. 'path' => $fileFoundPath,
  777. 'name' => $attachmentFileNameTrunc.'.pdf',
  778. );
  779. break;
  780. }
  781. }
  782. }
  783. }
  784. }
  785. if (isset($archiveFileList[$attachmentFileKey])) {
  786. $attachmentFileName = $archiveFileList[$attachmentFileKey]['name'];
  787. }
  788. }
  789. if (dol_strlen($attachmentFileName) == 12) {
  790. $Tab['attachment'] = $attachmentFileName; // position 182
  791. } else {
  792. $Tab['attachment'] = str_repeat(' ', 12); // position 182
  793. }
  794. $Tab['filler4'] = str_repeat(' ', 38);
  795. $Tab['end_line'] = $end_line;
  796. if ($exportFile) {
  797. fwrite($exportFile, implode($Tab));
  798. } else {
  799. print implode($Tab);
  800. }
  801. }
  802. return $archiveFileList;
  803. }
  804. /**
  805. * Export format : WinFic - eWinfic - WinSis Compta
  806. * Last review for this format : 2022-11-01 Alexandre Spangaro (aspangaro@open-dsi.fr)
  807. *
  808. * Help : https://wiki.gestan.fr/lib/exe/fetch.php?media=wiki:v15:compta:accountancy-format_winfic-ewinfic-winsiscompta.pdf
  809. *
  810. * @param array $TData data
  811. *
  812. * @return void
  813. */
  814. public function exportWinfic(&$TData)
  815. {
  816. global $conf;
  817. $end_line = "\r\n";
  818. $index = 1;
  819. //We should use dol_now function not time however this is wrong date to transfert in accounting
  820. //$date_ecriture = dol_print_date(dol_now(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
  821. //$date_ecriture = dol_print_date(time(), $conf->global->ACCOUNTING_EXPORT_DATE); // format must be ddmmyy
  822. // Warning ! When truncation is necessary, no dot because 3 dots = three characters. The columns are shifted
  823. foreach ($TData as $data) {
  824. $code_compta = $data->numero_compte;
  825. if (!empty($data->subledger_account)) {
  826. $code_compta = $data->subledger_account;
  827. }
  828. $Tab = array();
  829. //$Tab['type_ligne'] = 'M';
  830. $Tab['code_journal'] = str_pad(dol_trunc($data->code_journal, 2, 'right', 'UTF-8', 1), 2);
  831. //We use invoice date $data->doc_date not $date_ecriture which is the transfert date
  832. //maybe we should set an option for customer who prefer to keep in accounting software the tranfert date instead of invoice date ?
  833. //$Tab['date_ecriture'] = $date_ecriture;
  834. $Tab['date_operation'] = dol_print_date($data->doc_date, '%d%m%Y');
  835. $Tab['folio'] = ' 1';
  836. $Tab['num_ecriture'] = str_pad(dol_trunc($index, 6, 'right', 'UTF-8', 1), 6, ' ', STR_PAD_LEFT);
  837. $Tab['jour_ecriture'] = dol_print_date($data->doc_date, '%d%m%y');
  838. $Tab['num_compte'] = str_pad(dol_trunc($code_compta, 6, 'right', 'UTF-8', 1), 6, '0');
  839. if ($data->sens == 'D') {
  840. $Tab['montant_debit'] = str_pad(number_format($data->debit, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
  841. $Tab['montant_crebit'] = str_pad(number_format(0, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
  842. } else {
  843. $Tab['montant_debit'] = str_pad(number_format(0, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
  844. $Tab['montant_crebit'] = str_pad(number_format($data->credit, 2, ',', ''), 13, ' ', STR_PAD_LEFT);
  845. }
  846. $Tab['libelle_ecriture'] = str_pad(dol_trunc(dol_string_unaccent($data->doc_ref).' '.dol_string_unaccent($data->label_operation), 30, 'right', 'UTF-8', 1), 30);
  847. $Tab['lettrage'] = str_repeat(dol_trunc($data->lettering_code, 2, 'left', 'UTF-8', 1), 2);
  848. $Tab['code_piece'] = str_pad(dol_trunc($data->piece_num, 5, 'left', 'UTF-8', 1), 5, ' ', STR_PAD_LEFT);
  849. $Tab['code_stat'] = str_repeat(' ', 4);
  850. if (!empty($data->date_lim_reglement)) {
  851. //$Tab['date_echeance'] = dol_print_date($data->date_lim_reglement, $conf->global->ACCOUNTING_EXPORT_DATE);
  852. $Tab['date_echeance'] = dol_print_date($data->date_lim_reglement, '%d%m%Y');
  853. } else {
  854. $Tab['date_echeance'] = dol_print_date($data->doc_date, '%d%m%Y');
  855. }
  856. $Tab['monnaie'] = '1';
  857. $Tab['filler'] = ' ';
  858. $Tab['ind_compteur'] = ' ';
  859. $Tab['quantite'] = '0,000000000';
  860. $Tab['code_pointage'] = str_repeat(' ', 2);
  861. $Tab['end_line'] = $end_line;
  862. print implode('|', $Tab);
  863. $index++;
  864. }
  865. }
  866. /**
  867. * Export format : EBP
  868. *
  869. * @param array $objectLines data
  870. * @return void
  871. */
  872. public function exportEbp($objectLines)
  873. {
  874. $separator = ',';
  875. $end_line = "\n";
  876. foreach ($objectLines as $line) {
  877. $date = dol_print_date($line->doc_date, '%d%m%Y');
  878. print $line->id.$separator;
  879. print $date.$separator;
  880. print $line->code_journal.$separator;
  881. if (empty($line->subledger_account)) {
  882. print $line->numero_compte.$separator;
  883. } else {
  884. print $line->subledger_account.$separator;
  885. }
  886. //print substr(length_accountg($line->numero_compte), 0, 2) . $separator;
  887. print '"'.dol_trunc($line->label_operation, 40, 'right', 'UTF-8', 1).'"'.$separator;
  888. print '"'.dol_trunc($line->piece_num, 15, 'right', 'UTF-8', 1).'"'.$separator;
  889. print price2num(abs($line->debit - $line->credit)).$separator;
  890. print $line->sens.$separator;
  891. print $date.$separator;
  892. //print 'EUR';
  893. print $end_line;
  894. }
  895. }
  896. /**
  897. * Export format : Agiris Isacompta
  898. *
  899. * @param array $objectLines data
  900. * @return void
  901. */
  902. public function exportAgiris($objectLines)
  903. {
  904. $separator = ';';
  905. $end_line = "\n";
  906. foreach ($objectLines as $line) {
  907. $date = dol_print_date($line->doc_date, '%d%m%Y');
  908. print $line->piece_num.$separator;
  909. print self::toAnsi($line->label_operation).$separator;
  910. print $date.$separator;
  911. print self::toAnsi($line->label_operation).$separator;
  912. if (empty($line->subledger_account)) {
  913. print length_accountg($line->numero_compte).$separator;
  914. print self::toAnsi($line->label_compte).$separator;
  915. } else {
  916. print length_accounta($line->subledger_account).$separator;
  917. print self::toAnsi($line->subledger_label).$separator;
  918. }
  919. print self::toAnsi($line->doc_ref).$separator;
  920. print price($line->debit).$separator;
  921. print price($line->credit).$separator;
  922. print price(abs($line->debit - $line->credit)).$separator;
  923. print $line->sens.$separator;
  924. print $line->lettering_code.$separator;
  925. print $line->code_journal;
  926. print $end_line;
  927. }
  928. }
  929. /**
  930. * Export format : OpenConcerto
  931. *
  932. * @param array $objectLines data
  933. * @return void
  934. */
  935. public function exportOpenConcerto($objectLines)
  936. {
  937. $separator = ';';
  938. $end_line = "\n";
  939. foreach ($objectLines as $line) {
  940. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  941. print $date.$separator;
  942. print $line->code_journal.$separator;
  943. if (empty($line->subledger_account)) {
  944. print length_accountg($line->numero_compte).$separator;
  945. } else {
  946. print length_accounta($line->subledger_account).$separator;
  947. }
  948. print $line->doc_ref.$separator;
  949. print $line->label_operation.$separator;
  950. print price($line->debit).$separator;
  951. print price($line->credit).$separator;
  952. print $end_line;
  953. }
  954. }
  955. /**
  956. * Export format : Configurable CSV
  957. *
  958. * @param array $objectLines data
  959. * @return void
  960. */
  961. public function exportConfigurable($objectLines)
  962. {
  963. global $conf;
  964. $separator = $this->separator;
  965. foreach ($objectLines as $line) {
  966. $tab = array();
  967. // export configurable
  968. $date = dol_print_date($line->doc_date, $conf->global->ACCOUNTING_EXPORT_DATE);
  969. $tab[] = $line->piece_num;
  970. $tab[] = $date;
  971. $tab[] = $line->doc_ref;
  972. $tab[] = preg_match('/'.$separator.'/', $line->label_operation) ? "'".$line->label_operation."'" : $line->label_operation;
  973. $tab[] = length_accountg($line->numero_compte);
  974. $tab[] = length_accounta($line->subledger_account);
  975. $tab[] = price2num($line->debit);
  976. $tab[] = price2num($line->credit);
  977. $tab[] = price2num($line->debit - $line->credit);
  978. $tab[] = $line->code_journal;
  979. print implode($separator, $tab).$this->end_line;
  980. }
  981. }
  982. /**
  983. * Export format : FEC
  984. *
  985. * @param array $objectLines data
  986. * @return void
  987. */
  988. public function exportFEC($objectLines)
  989. {
  990. global $langs;
  991. $separator = "\t";
  992. $end_line = "\r\n";
  993. print "JournalCode".$separator;
  994. print "JournalLib".$separator;
  995. print "EcritureNum".$separator;
  996. print "EcritureDate".$separator;
  997. print "CompteNum".$separator;
  998. print "CompteLib".$separator;
  999. print "CompAuxNum".$separator;
  1000. print "CompAuxLib".$separator;
  1001. print "PieceRef".$separator;
  1002. print "PieceDate".$separator;
  1003. print "EcritureLib".$separator;
  1004. print "Debit".$separator;
  1005. print "Credit".$separator;
  1006. print "EcritureLet".$separator;
  1007. print "DateLet".$separator;
  1008. print "ValidDate".$separator;
  1009. print "Montantdevise".$separator;
  1010. print "Idevise".$separator;
  1011. print "DateLimitReglmt".$separator;
  1012. print "NumFacture";
  1013. print $end_line;
  1014. foreach ($objectLines as $line) {
  1015. if ($line->debit == 0 && $line->credit == 0) {
  1016. //unset($array[$line]);
  1017. } else {
  1018. $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
  1019. $date_document = dol_print_date($line->doc_date, '%Y%m%d');
  1020. $date_lettering = dol_print_date($line->date_lettering, '%Y%m%d');
  1021. $date_validation = dol_print_date($line->date_validation, '%Y%m%d');
  1022. $date_limit_payment = dol_print_date($line->date_lim_reglement, '%Y%m%d');
  1023. $refInvoice = '';
  1024. if ($line->doc_type == 'customer_invoice') {
  1025. // Customer invoice
  1026. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  1027. $invoice = new Facture($this->db);
  1028. $invoice->fetch($line->fk_doc);
  1029. $refInvoice = $invoice->ref;
  1030. } elseif ($line->doc_type == 'supplier_invoice') {
  1031. // Supplier invoice
  1032. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  1033. $invoice = new FactureFournisseur($this->db);
  1034. $invoice->fetch($line->fk_doc);
  1035. $refInvoice = $invoice->ref_supplier;
  1036. }
  1037. // FEC:JournalCode
  1038. print $line->code_journal . $separator;
  1039. // FEC:JournalLib
  1040. print dol_string_unaccent($langs->transnoentities($line->journal_label)) . $separator;
  1041. // FEC:EcritureNum
  1042. print $line->piece_num . $separator;
  1043. // FEC:EcritureDate
  1044. print $date_document . $separator;
  1045. // FEC:CompteNum
  1046. print $line->numero_compte . $separator;
  1047. // FEC:CompteLib
  1048. print dol_string_unaccent($line->label_compte) . $separator;
  1049. // FEC:CompAuxNum
  1050. print $line->subledger_account . $separator;
  1051. // FEC:CompAuxLib
  1052. print dol_string_unaccent($line->subledger_label) . $separator;
  1053. // FEC:PieceRef
  1054. print $line->doc_ref . $separator;
  1055. // FEC:PieceDate
  1056. print dol_string_unaccent($date_creation) . $separator;
  1057. // FEC:EcritureLib
  1058. // Clean label operation to prevent problem on export with tab separator & other character
  1059. $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
  1060. print dol_string_unaccent($line->label_operation) . $separator;
  1061. // FEC:Debit
  1062. print price2fec($line->debit) . $separator;
  1063. // FEC:Credit
  1064. print price2fec($line->credit) . $separator;
  1065. // FEC:EcritureLet
  1066. print $line->lettering_code . $separator;
  1067. // FEC:DateLet
  1068. print $date_lettering . $separator;
  1069. // FEC:ValidDate
  1070. print $date_validation . $separator;
  1071. // FEC:Montantdevise
  1072. print $line->multicurrency_amount . $separator;
  1073. // FEC:Idevise
  1074. print $line->multicurrency_code . $separator;
  1075. // FEC_suppl:DateLimitReglmt
  1076. print $date_limit_payment . $separator;
  1077. // FEC_suppl:NumFacture
  1078. // Clean ref invoice to prevent problem on export with tab separator & other character
  1079. $refInvoice = str_replace(array("\t", "\n", "\r"), " ", $refInvoice);
  1080. print dol_trunc(self::toAnsi($refInvoice), 17, 'right', 'UTF-8', 1);
  1081. print $end_line;
  1082. }
  1083. }
  1084. }
  1085. /**
  1086. * Export format : FEC2
  1087. *
  1088. * @param array $objectLines data
  1089. * @return void
  1090. */
  1091. public function exportFEC2($objectLines)
  1092. {
  1093. global $langs;
  1094. $separator = "\t";
  1095. $end_line = "\r\n";
  1096. print "JournalCode".$separator;
  1097. print "JournalLib".$separator;
  1098. print "EcritureNum".$separator;
  1099. print "EcritureDate".$separator;
  1100. print "CompteNum".$separator;
  1101. print "CompteLib".$separator;
  1102. print "CompAuxNum".$separator;
  1103. print "CompAuxLib".$separator;
  1104. print "PieceRef".$separator;
  1105. print "PieceDate".$separator;
  1106. print "EcritureLib".$separator;
  1107. print "Debit".$separator;
  1108. print "Credit".$separator;
  1109. print "EcritureLet".$separator;
  1110. print "DateLet".$separator;
  1111. print "ValidDate".$separator;
  1112. print "Montantdevise".$separator;
  1113. print "Idevise".$separator;
  1114. print "DateLimitReglmt".$separator;
  1115. print "NumFacture";
  1116. print $end_line;
  1117. foreach ($objectLines as $line) {
  1118. if ($line->debit == 0 && $line->credit == 0) {
  1119. //unset($array[$line]);
  1120. } else {
  1121. $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
  1122. $date_document = dol_print_date($line->doc_date, '%Y%m%d');
  1123. $date_lettering = dol_print_date($line->date_lettering, '%Y%m%d');
  1124. $date_validation = dol_print_date($line->date_validation, '%Y%m%d');
  1125. $date_limit_payment = dol_print_date($line->date_lim_reglement, '%Y%m%d');
  1126. $refInvoice = '';
  1127. if ($line->doc_type == 'customer_invoice') {
  1128. // Customer invoice
  1129. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  1130. $invoice = new Facture($this->db);
  1131. $invoice->fetch($line->fk_doc);
  1132. $refInvoice = $invoice->ref;
  1133. } elseif ($line->doc_type == 'supplier_invoice') {
  1134. // Supplier invoice
  1135. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  1136. $invoice = new FactureFournisseur($this->db);
  1137. $invoice->fetch($line->fk_doc);
  1138. $refInvoice = $invoice->ref_supplier;
  1139. }
  1140. // FEC:JournalCode
  1141. print $line->code_journal . $separator;
  1142. // FEC:JournalLib
  1143. print dol_string_unaccent($langs->transnoentities($line->journal_label)) . $separator;
  1144. // FEC:EcritureNum
  1145. print $line->piece_num . $separator;
  1146. // FEC:EcritureDate
  1147. print $date_creation . $separator;
  1148. // FEC:CompteNum
  1149. print length_accountg($line->numero_compte) . $separator;
  1150. // FEC:CompteLib
  1151. print dol_string_unaccent($line->label_compte) . $separator;
  1152. // FEC:CompAuxNum
  1153. print length_accounta($line->subledger_account) . $separator;
  1154. // FEC:CompAuxLib
  1155. print dol_string_unaccent($line->subledger_label) . $separator;
  1156. // FEC:PieceRef
  1157. print $line->doc_ref . $separator;
  1158. // FEC:PieceDate
  1159. print $date_document . $separator;
  1160. // FEC:EcritureLib
  1161. // Clean label operation to prevent problem on export with tab separator & other character
  1162. $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
  1163. print dol_string_unaccent($line->label_operation) . $separator;
  1164. // FEC:Debit
  1165. print price2fec($line->debit) . $separator;
  1166. // FEC:Credit
  1167. print price2fec($line->credit) . $separator;
  1168. // FEC:EcritureLet
  1169. print $line->lettering_code . $separator;
  1170. // FEC:DateLet
  1171. print $date_lettering . $separator;
  1172. // FEC:ValidDate
  1173. print $date_validation . $separator;
  1174. // FEC:Montantdevise
  1175. print $line->multicurrency_amount . $separator;
  1176. // FEC:Idevise
  1177. print $line->multicurrency_code . $separator;
  1178. // FEC_suppl:DateLimitReglmt
  1179. print $date_limit_payment . $separator;
  1180. // FEC_suppl:NumFacture
  1181. // Clean ref invoice to prevent problem on export with tab separator & other character
  1182. $refInvoice = str_replace(array("\t", "\n", "\r"), " ", $refInvoice);
  1183. print dol_trunc(self::toAnsi($refInvoice), 17, 'right', 'UTF-8', 1);
  1184. print $end_line;
  1185. }
  1186. }
  1187. }
  1188. /**
  1189. * Export format : SAGE50SWISS
  1190. *
  1191. * https://onlinehelp.sageschweiz.ch/default.aspx?tabid=19984
  1192. * http://media.topal.ch/Public/Schnittstellen/TAF/Specification/Sage50-TAF-format.pdf
  1193. *
  1194. * @param array $objectLines data
  1195. *
  1196. * @return void
  1197. */
  1198. public function exportSAGE50SWISS($objectLines)
  1199. {
  1200. // SAGE50SWISS
  1201. $this->separator = ',';
  1202. $this->end_line = "\r\n";
  1203. // Print header line
  1204. print "Blg,Datum,Kto,S/H,Grp,GKto,SId,SIdx,KIdx,BTyp,MTyp,Code,Netto,Steuer,FW-Betrag,Tx1,Tx2,PkKey,OpId,Flag";
  1205. print $this->end_line;
  1206. $thisPieceNum = "";
  1207. $thisPieceAccountNr = "";
  1208. $aSize = count($objectLines);
  1209. foreach ($objectLines as $aIndex => $line) {
  1210. $sammelBuchung = false;
  1211. if ($aIndex - 2 >= 0 && $objectLines[$aIndex - 2]->piece_num == $line->piece_num) {
  1212. $sammelBuchung = true;
  1213. } elseif ($aIndex + 2 < $aSize && $objectLines[$aIndex + 2]->piece_num == $line->piece_num) {
  1214. $sammelBuchung = true;
  1215. } elseif ($aIndex + 1 < $aSize
  1216. && $objectLines[$aIndex + 1]->piece_num == $line->piece_num
  1217. && $aIndex - 1 < $aSize
  1218. && $objectLines[$aIndex - 1]->piece_num == $line->piece_num
  1219. ) {
  1220. $sammelBuchung = true;
  1221. }
  1222. //Blg
  1223. print $line->piece_num.$this->separator;
  1224. // Datum
  1225. $date = dol_print_date($line->doc_date, '%d.%m.%Y');
  1226. print $date.$this->separator;
  1227. // Kto
  1228. print length_accountg($line->numero_compte).$this->separator;
  1229. // S/H
  1230. if ($line->sens == 'D') {
  1231. print 'S'.$this->separator;
  1232. } else {
  1233. print 'H'.$this->separator;
  1234. }
  1235. //Grp
  1236. print self::trunc($line->code_journal, 1).$this->separator;
  1237. // GKto
  1238. if (empty($line->code_tiers)) {
  1239. if ($line->piece_num == $thisPieceNum) {
  1240. print length_accounta($thisPieceAccountNr).$this->separator;
  1241. } else {
  1242. print "div".$this->separator;
  1243. }
  1244. } else {
  1245. print length_accounta($line->code_tiers).$this->separator;
  1246. }
  1247. //SId
  1248. print $this->separator;
  1249. //SIdx
  1250. print "0".$this->separator;
  1251. //KIdx
  1252. print "0".$this->separator;
  1253. //BTyp
  1254. print "0".$this->separator;
  1255. //MTyp 1=Fibu Einzelbuchung 2=Sammebuchung
  1256. if ($sammelBuchung) {
  1257. print "2".$this->separator;
  1258. } else {
  1259. print "1".$this->separator;
  1260. }
  1261. // Code
  1262. print '""'.$this->separator;
  1263. // Netto
  1264. print abs($line->debit - $line->credit).$this->separator;
  1265. // Steuer
  1266. print "0.00".$this->separator;
  1267. // FW-Betrag
  1268. print "0.00".$this->separator;
  1269. // Tx1
  1270. $line1 = self::toAnsi($line->label_compte, 29);
  1271. if ($line1 == "LIQ" || $line1 == "LIQ Beleg ok" || strlen($line1) <= 3) {
  1272. $line1 = "";
  1273. }
  1274. $line2 = self::toAnsi($line->doc_ref, 29);
  1275. if (strlen($line1) == 0) {
  1276. $line1 = $line2;
  1277. $line2 = "";
  1278. }
  1279. if (strlen($line1) > 0 && strlen($line2) > 0 && (strlen($line1) + strlen($line2)) < 27) {
  1280. $line1 = $line1.' / '.$line2;
  1281. $line2 = "";
  1282. }
  1283. print '"'.self::toAnsi($line1).'"'.$this->separator;
  1284. // Tx2
  1285. print '"'.self::toAnsi($line2).'"'.$this->separator;
  1286. //PkKey
  1287. print "0".$this->separator;
  1288. //OpId
  1289. print $this->separator;
  1290. // Flag
  1291. print "0";
  1292. print $this->end_line;
  1293. if ($line->piece_num !== $thisPieceNum) {
  1294. $thisPieceNum = $line->piece_num;
  1295. $thisPieceAccountNr = $line->numero_compte;
  1296. }
  1297. }
  1298. }
  1299. /**
  1300. * Export format : LD Compta version 9
  1301. * http://www.ldsysteme.fr/fileadmin/telechargement/np/ldcompta/Documentation/IntCptW9.pdf
  1302. *
  1303. * @param array $objectLines data
  1304. *
  1305. * @return void
  1306. */
  1307. public function exportLDCompta($objectLines)
  1308. {
  1309. $separator = ';';
  1310. $end_line = "\r\n";
  1311. foreach ($objectLines as $line) {
  1312. $date_document = dol_print_date($line->doc_date, '%Y%m%d');
  1313. $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
  1314. $date_lim_reglement = dol_print_date($line->date_lim_reglement, '%Y%m%d');
  1315. // TYPE
  1316. $type_enregistrement = 'E'; // For write movement
  1317. print $type_enregistrement.$separator;
  1318. // JNAL
  1319. print substr($line->code_journal, 0, 2).$separator;
  1320. // NECR
  1321. print $line->id.$separator;
  1322. // NPIE
  1323. print $line->piece_num.$separator;
  1324. // DATP
  1325. print $date_document.$separator;
  1326. // LIBE
  1327. print $line->label_operation.$separator;
  1328. // DATH
  1329. print $date_lim_reglement.$separator;
  1330. // CNPI
  1331. if ($line->doc_type == 'supplier_invoice') {
  1332. if (($line->debit - $line->credit) > 0) {
  1333. $nature_piece = 'AF';
  1334. } else {
  1335. $nature_piece = 'FF';
  1336. }
  1337. } elseif ($line->doc_type == 'customer_invoice') {
  1338. if (($line->debit - $line->credit) < 0) {
  1339. $nature_piece = 'AC';
  1340. } else {
  1341. $nature_piece = 'FC';
  1342. }
  1343. } else {
  1344. $nature_piece = '';
  1345. }
  1346. print $nature_piece.$separator;
  1347. // RACI
  1348. // if (!empty($line->subledger_account)) {
  1349. // if ($line->doc_type == 'supplier_invoice') {
  1350. // $racine_subledger_account = '40';
  1351. // } elseif ($line->doc_type == 'customer_invoice') {
  1352. // $racine_subledger_account = '41';
  1353. // } else {
  1354. // $racine_subledger_account = '';
  1355. // }
  1356. // } else {
  1357. $racine_subledger_account = ''; // for records of type E leave this field blank
  1358. // }
  1359. print $racine_subledger_account.$separator; // deprecated CPTG & CPTA use instead
  1360. // MONT
  1361. print price(abs($line->debit - $line->credit), 0, '', 1, 2, 2).$separator;
  1362. // CODC
  1363. print $line->sens.$separator;
  1364. // CPTG
  1365. print length_accountg($line->numero_compte).$separator;
  1366. // DATE
  1367. print $date_creation.$separator;
  1368. // CLET
  1369. print $line->lettering_code.$separator;
  1370. // DATL
  1371. print $line->date_lettering.$separator;
  1372. // CPTA
  1373. if (!empty($line->subledger_account)) {
  1374. print length_accounta($line->subledger_account).$separator;
  1375. } else {
  1376. print $separator;
  1377. }
  1378. // CNAT
  1379. if ($line->doc_type == 'supplier_invoice' && !empty($line->subledger_account)) {
  1380. print 'F'.$separator;
  1381. } elseif ($line->doc_type == 'customer_invoice' && !empty($line->subledger_account)) {
  1382. print 'C'.$separator;
  1383. } else {
  1384. print $separator;
  1385. }
  1386. // SECT
  1387. print $separator;
  1388. // CTRE
  1389. print $separator;
  1390. // NORL
  1391. print $separator;
  1392. // DATV
  1393. print $separator;
  1394. // REFD
  1395. print $line->doc_ref.$separator;
  1396. // CODH
  1397. print $separator;
  1398. // NSEQ
  1399. print $separator;
  1400. // MTDV
  1401. print '0'.$separator;
  1402. // CODV
  1403. print $separator;
  1404. // TXDV
  1405. print '0'.$separator;
  1406. // MOPM
  1407. print $separator;
  1408. // BONP
  1409. print $separator;
  1410. // BQAF
  1411. print $separator;
  1412. // ECES
  1413. print $separator;
  1414. // TXTL
  1415. print $separator;
  1416. // ECRM
  1417. print $separator;
  1418. // DATK
  1419. print $separator;
  1420. // HEUK
  1421. print $separator;
  1422. print $end_line;
  1423. }
  1424. }
  1425. /**
  1426. * Export format : LD Compta version 10 & higher
  1427. * Last review for this format : 08-15-2021 Alexandre Spangaro (aspangaro@open-dsi.fr)
  1428. *
  1429. * Help : http://www.ldsysteme.fr/fileadmin/telechargement/np/ldcompta/Documentation/IntCptW10.pdf
  1430. *
  1431. * @param array $objectLines data
  1432. *
  1433. * @return void
  1434. */
  1435. public function exportLDCompta10($objectLines)
  1436. {
  1437. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  1438. $separator = ';';
  1439. $end_line = "\r\n";
  1440. $last_codeinvoice = '';
  1441. foreach ($objectLines as $line) {
  1442. // TYPE C
  1443. if ($last_codeinvoice != $line->doc_ref) {
  1444. //recherche societe en fonction de son code client
  1445. $sql = "SELECT code_client, fk_forme_juridique, nom, address, zip, town, fk_pays, phone, siret FROM ".MAIN_DB_PREFIX."societe";
  1446. $sql .= " WHERE code_client = '".$this->db->escape($line->thirdparty_code)."'";
  1447. $resql = $this->db->query($sql);
  1448. if ($resql && $this->db->num_rows($resql) > 0) {
  1449. $soc = $this->db->fetch_object($resql);
  1450. $address = array('', '', '');
  1451. if (strpos($soc->address, "\n") !== false) {
  1452. $address = explode("\n", $soc->address);
  1453. if (is_array($address) && count($address) > 0) {
  1454. foreach ($address as $key => $data) {
  1455. $address[$key] = str_replace(array("\t", "\n", "\r"), "", $data);
  1456. $address[$key] = dol_trunc($address[$key], 40, 'right', 'UTF-8', 1);
  1457. }
  1458. }
  1459. } else {
  1460. $address[0] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 0, 40);
  1461. $address[1] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 41, 40);
  1462. $address[2] = substr(str_replace(array("\t", "\r"), " ", $soc->address), 82, 40);
  1463. }
  1464. $type_enregistrement = 'C';
  1465. //TYPE
  1466. print $type_enregistrement.$separator;
  1467. //NOCL
  1468. print $soc->code_client.$separator;
  1469. //NMCM
  1470. print $separator;
  1471. //LIBI
  1472. print $separator;
  1473. //TITR
  1474. print $separator;
  1475. //RSSO
  1476. print $soc->nom.$separator;
  1477. //CAD1
  1478. print $address[0].$separator;
  1479. //CAD2
  1480. print $address[1].$separator;
  1481. //CAD3
  1482. print $address[2].$separator;
  1483. //COPO
  1484. print $soc->zip.$separator;
  1485. //BUDI
  1486. print substr($soc->town, 0, 40).$separator;
  1487. //CPAY
  1488. print $separator;
  1489. //PAYS
  1490. print substr(getCountry($soc->fk_pays), 0, 40).$separator;
  1491. //NTEL
  1492. print $soc->phone.$separator;
  1493. //TLEX
  1494. print $separator;
  1495. //TLPO
  1496. print $separator;
  1497. //TLCY
  1498. print $separator;
  1499. //NINT
  1500. print $separator;
  1501. //COMM
  1502. print $separator;
  1503. //SIRE
  1504. print str_replace(" ", "", $soc->siret).$separator;
  1505. //RIBP
  1506. print $separator;
  1507. //DOBQ
  1508. print $separator;
  1509. //IBBQ
  1510. print $separator;
  1511. //COBQ
  1512. print $separator;
  1513. //GUBQ
  1514. print $separator;
  1515. //CPBQ
  1516. print $separator;
  1517. //CLBQ
  1518. print $separator;
  1519. //BIBQ
  1520. print $separator;
  1521. //MOPM
  1522. print $separator;
  1523. //DJPM
  1524. print $separator;
  1525. //DMPM
  1526. print $separator;
  1527. //REFM
  1528. print $separator;
  1529. //SLVA
  1530. print $separator;
  1531. //PLCR
  1532. print $separator;
  1533. //ECFI
  1534. print $separator;
  1535. //CREP
  1536. print $separator;
  1537. //NREP
  1538. print $separator;
  1539. //TREP
  1540. print $separator;
  1541. //MREP
  1542. print $separator;
  1543. //GRRE
  1544. print $separator;
  1545. //LTTA
  1546. print $separator;
  1547. //CACT
  1548. print $separator;
  1549. //CODV
  1550. print $separator;
  1551. //GRTR
  1552. print $separator;
  1553. //NOFP
  1554. print $separator;
  1555. //BQAF
  1556. print $separator;
  1557. //BONP
  1558. print $separator;
  1559. //CESC
  1560. print $separator;
  1561. print $end_line;
  1562. }
  1563. }
  1564. $date_document = dol_print_date($line->doc_date, '%Y%m%d');
  1565. $date_creation = dol_print_date($line->date_creation, '%Y%m%d');
  1566. $date_lim_reglement = dol_print_date($line->date_lim_reglement, '%Y%m%d');
  1567. // TYPE E
  1568. $type_enregistrement = 'E'; // For write movement
  1569. print $type_enregistrement.$separator;
  1570. // JNAL
  1571. print substr($line->code_journal, 0, 2).$separator;
  1572. // NECR
  1573. print $line->id.$separator;
  1574. // NPIE
  1575. print $line->piece_num.$separator;
  1576. // DATP
  1577. print $date_document.$separator;
  1578. // LIBE
  1579. print dol_trunc($line->label_operation, 25, 'right', 'UTF-8', 1).$separator;
  1580. // DATH
  1581. print $date_lim_reglement.$separator;
  1582. // CNPI
  1583. if ($line->doc_type == 'supplier_invoice') {
  1584. if (($line->amount) < 0) { // Currently, only the sign of amount allows to know the type of invoice (standard or credit note). Other solution is to analyse debit/credit/role of account. TODO Add column doc_type_long or make amount mandatory with rule on sign.
  1585. $nature_piece = 'AF';
  1586. } else {
  1587. $nature_piece = 'FF';
  1588. }
  1589. } elseif ($line->doc_type == 'customer_invoice') {
  1590. if (($line->amount) < 0) {
  1591. $nature_piece = 'AC'; // Currently, only the sign of amount allows to know the type of invoice (standard or credit note). Other solution is to analyse debit/credit/role of account. TODO Add column doc_type_long or make amount mandatory with rule on sign.
  1592. } else {
  1593. $nature_piece = 'FC';
  1594. }
  1595. } else {
  1596. $nature_piece = '';
  1597. }
  1598. print $nature_piece.$separator;
  1599. // RACI
  1600. // if (!empty($line->subledger_account)) {
  1601. // if ($line->doc_type == 'supplier_invoice') {
  1602. // $racine_subledger_account = '40';
  1603. // } elseif ($line->doc_type == 'customer_invoice') {
  1604. // $racine_subledger_account = '41';
  1605. // } else {
  1606. // $racine_subledger_account = '';
  1607. // }
  1608. // } else {
  1609. $racine_subledger_account = ''; // for records of type E leave this field blank
  1610. // }
  1611. print $racine_subledger_account.$separator; // deprecated CPTG & CPTA use instead
  1612. // MONT
  1613. print price(abs($line->debit - $line->credit), 0, '', 1, 2).$separator;
  1614. // CODC
  1615. print $line->sens.$separator;
  1616. // CPTG
  1617. print length_accountg($line->numero_compte).$separator;
  1618. // DATE
  1619. print $date_document.$separator;
  1620. // CLET
  1621. print $line->lettering_code.$separator;
  1622. // DATL
  1623. print $line->date_lettering.$separator;
  1624. // CPTA
  1625. if (!empty($line->subledger_account)) {
  1626. print length_accounta($line->subledger_account).$separator;
  1627. } else {
  1628. print $separator;
  1629. }
  1630. // CNAT
  1631. if ($line->doc_type == 'supplier_invoice' && !empty($line->subledger_account)) {
  1632. print 'F'.$separator;
  1633. } elseif ($line->doc_type == 'customer_invoice' && !empty($line->subledger_account)) {
  1634. print 'C'.$separator;
  1635. } else {
  1636. print $separator;
  1637. }
  1638. // CTRE
  1639. print $separator;
  1640. // NORL
  1641. print $separator;
  1642. // DATV
  1643. print $separator;
  1644. // REFD
  1645. print $line->doc_ref.$separator;
  1646. // NECA
  1647. print '0'.$separator;
  1648. // CSEC
  1649. print $separator;
  1650. // CAFF
  1651. print $separator;
  1652. // CDES
  1653. print $separator;
  1654. // QTUE
  1655. print $separator;
  1656. // MTDV
  1657. print '0'.$separator;
  1658. // CODV
  1659. print $separator;
  1660. // TXDV
  1661. print '0'.$separator;
  1662. // MOPM
  1663. print $separator;
  1664. // BONP
  1665. print $separator;
  1666. // BQAF
  1667. print $separator;
  1668. // ECES
  1669. print $separator;
  1670. // TXTL
  1671. print $separator;
  1672. // ECRM
  1673. print $separator;
  1674. // DATK
  1675. print $separator;
  1676. // HEUK
  1677. print $separator;
  1678. print $end_line;
  1679. $last_codeinvoice = $line->doc_ref;
  1680. }
  1681. }
  1682. /**
  1683. * Export format : Charlemagne
  1684. *
  1685. * @param array $objectLines data
  1686. * @return void
  1687. */
  1688. public function exportCharlemagne($objectLines)
  1689. {
  1690. global $langs;
  1691. $langs->load('compta');
  1692. $separator = "\t";
  1693. $end_line = "\n";
  1694. /*
  1695. * Charlemagne export need header
  1696. */
  1697. print $langs->transnoentitiesnoconv('Date').$separator;
  1698. print self::trunc($langs->transnoentitiesnoconv('Journal'), 6).$separator;
  1699. print self::trunc($langs->transnoentitiesnoconv('Account'), 15).$separator;
  1700. print self::trunc($langs->transnoentitiesnoconv('LabelAccount'), 60).$separator;
  1701. print self::trunc($langs->transnoentitiesnoconv('Piece'), 20).$separator;
  1702. print self::trunc($langs->transnoentitiesnoconv('LabelOperation'), 60).$separator;
  1703. print $langs->transnoentitiesnoconv('Amount').$separator;
  1704. print 'S'.$separator;
  1705. print self::trunc($langs->transnoentitiesnoconv('Analytic').' 1', 15).$separator;
  1706. print self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 1', 60).$separator;
  1707. print self::trunc($langs->transnoentitiesnoconv('Analytic').' 2', 15).$separator;
  1708. print self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 2', 60).$separator;
  1709. print self::trunc($langs->transnoentitiesnoconv('Analytic').' 3', 15).$separator;
  1710. print self::trunc($langs->transnoentitiesnoconv('AnalyticLabel').' 3', 60).$separator;
  1711. print $end_line;
  1712. foreach ($objectLines as $line) {
  1713. $date = dol_print_date($line->doc_date, '%Y%m%d');
  1714. print $date.$separator; //Date
  1715. print self::trunc($line->code_journal, 6).$separator; //Journal code
  1716. if (!empty($line->subledger_account)) {
  1717. $account = $line->subledger_account;
  1718. } else {
  1719. $account = $line->numero_compte;
  1720. }
  1721. print self::trunc($account, 15).$separator; //Account number
  1722. print self::trunc($line->label_compte, 60).$separator; //Account label
  1723. print self::trunc($line->doc_ref, 20).$separator; //Piece
  1724. // Clean label operation to prevent problem on export with tab separator & other character
  1725. $line->label_operation = str_replace(array("\t", "\n", "\r"), " ", $line->label_operation);
  1726. print self::trunc($line->label_operation, 60).$separator; //Operation label
  1727. print price(abs($line->debit - $line->credit)).$separator; //Amount
  1728. print $line->sens.$separator; //Direction
  1729. print $separator; //Analytic
  1730. print $separator; //Analytic
  1731. print $separator; //Analytic
  1732. print $separator; //Analytic
  1733. print $separator; //Analytic
  1734. print $separator; //Analytic
  1735. print $end_line;
  1736. }
  1737. }
  1738. /**
  1739. * Export format : Gestimum V3
  1740. *
  1741. * @param array $objectLines data
  1742. *
  1743. * @return void
  1744. */
  1745. public function exportGestimumV3($objectLines)
  1746. {
  1747. global $langs;
  1748. $this->separator = ',';
  1749. $invoices_infos = array();
  1750. $supplier_invoices_infos = array();
  1751. foreach ($objectLines as $line) {
  1752. if ($line->debit == 0 && $line->credit == 0) {
  1753. //unset($array[$line]);
  1754. } else {
  1755. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  1756. $invoice_ref = $line->doc_ref;
  1757. $company_name = "";
  1758. if (($line->doc_type == 'customer_invoice' || $line->doc_type == 'supplier_invoice') && $line->fk_doc > 0) {
  1759. if (($line->doc_type == 'customer_invoice' && !isset($invoices_infos[$line->fk_doc])) ||
  1760. ($line->doc_type == 'supplier_invoice' && !isset($supplier_invoices_infos[$line->fk_doc]))) {
  1761. if ($line->doc_type == 'customer_invoice') {
  1762. // Get new customer invoice ref and company name
  1763. $sql = 'SELECT f.ref, s.nom FROM ' . MAIN_DB_PREFIX . 'facture as f';
  1764. $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'societe AS s ON f.fk_soc = s.rowid';
  1765. $sql .= ' WHERE f.rowid = '.((int) $line->fk_doc);
  1766. $resql = $this->db->query($sql);
  1767. if ($resql) {
  1768. if ($obj = $this->db->fetch_object($resql)) {
  1769. // Save invoice infos
  1770. $invoices_infos[$line->fk_doc] = array('ref' => $obj->ref, 'company_name' => $obj->nom);
  1771. $invoice_ref = $obj->ref;
  1772. $company_name = $obj->nom;
  1773. }
  1774. }
  1775. } else {
  1776. // Get new supplier invoice ref and company name
  1777. $sql = 'SELECT ff.ref, s.nom FROM ' . MAIN_DB_PREFIX . 'facture_fourn as ff';
  1778. $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'societe AS s ON ff.fk_soc = s.rowid';
  1779. $sql .= ' WHERE ff.rowid = '.((int) $line->fk_doc);
  1780. $resql = $this->db->query($sql);
  1781. if ($resql) {
  1782. if ($obj = $this->db->fetch_object($resql)) {
  1783. // Save invoice infos
  1784. $supplier_invoices_infos[$line->fk_doc] = array('ref' => $obj->ref, 'company_name' => $obj->nom);
  1785. $invoice_ref = $obj->ref;
  1786. $company_name = $obj->nom;
  1787. }
  1788. }
  1789. }
  1790. } elseif ($line->doc_type == 'customer_invoice') {
  1791. // Retrieve invoice infos
  1792. $invoice_ref = $invoices_infos[$line->fk_doc]['ref'];
  1793. $company_name = $invoices_infos[$line->fk_doc]['company_name'];
  1794. } else {
  1795. // Retrieve invoice infos
  1796. $invoice_ref = $supplier_invoices_infos[$line->fk_doc]['ref'];
  1797. $company_name = $supplier_invoices_infos[$line->fk_doc]['company_name'];
  1798. }
  1799. }
  1800. print $line->id . $this->separator;
  1801. print $date . $this->separator;
  1802. print substr($line->code_journal, 0, 4) . $this->separator;
  1803. if ((substr($line->numero_compte, 0, 3) == '411') || (substr($line->numero_compte, 0, 3) == '401')) {
  1804. print length_accountg($line->subledger_account) . $this->separator;
  1805. } else {
  1806. print substr(length_accountg($line->numero_compte), 0, 15) . $this->separator;
  1807. }
  1808. //Libellé Auto
  1809. print $this->separator;
  1810. //print '"'.dol_trunc(str_replace('"', '', $line->label_operation),40,'right','UTF-8',1).'"' . $this->separator;
  1811. //Libellé manuel
  1812. print dol_trunc(str_replace('"', '', $invoice_ref . (!empty($company_name) ? ' - ' : '') . $company_name), 40, 'right', 'UTF-8', 1) . $this->separator;
  1813. //Numéro de pièce
  1814. print dol_trunc(str_replace('"', '', $line->piece_num), 10, 'right', 'UTF-8', 1) . $this->separator;
  1815. //Devise
  1816. print 'EUR' . $this->separator;
  1817. //Amount
  1818. print price2num(abs($line->debit - $line->credit)) . $this->separator;
  1819. //Sens
  1820. print $line->sens . $this->separator;
  1821. //Code lettrage
  1822. print $this->separator;
  1823. //Date Echéance
  1824. print $date;
  1825. print $this->end_line;
  1826. }
  1827. }
  1828. }
  1829. /**
  1830. * Export format : Gestimum V5
  1831. *
  1832. * @param array $objectLines data
  1833. *
  1834. * @return void
  1835. */
  1836. public function exportGestimumV5($objectLines)
  1837. {
  1838. $this->separator = ',';
  1839. foreach ($objectLines as $line) {
  1840. if ($line->debit == 0 && $line->credit == 0) {
  1841. //unset($array[$line]);
  1842. } else {
  1843. $date = dol_print_date($line->doc_date, '%d%m%Y');
  1844. print $line->id . $this->separator;
  1845. print $date . $this->separator;
  1846. print substr($line->code_journal, 0, 4) . $this->separator;
  1847. if ((substr($line->numero_compte, 0, 3) == '411') || (substr($line->numero_compte, 0, 3) == '401')) { // TODO No hard code value
  1848. print length_accountg($line->subledger_account) . $this->separator;
  1849. } else {
  1850. print substr(length_accountg($line->numero_compte), 0, 15) . $this->separator;
  1851. }
  1852. print $this->separator;
  1853. //print '"'.dol_trunc(str_replace('"', '', $line->label_operation),40,'right','UTF-8',1).'"' . $this->separator;
  1854. print '"' . dol_trunc(str_replace('"', '', $line->doc_ref), 40, 'right', 'UTF-8', 1) . '"' . $this->separator;
  1855. print '"' . dol_trunc(str_replace('"', '', $line->piece_num), 10, 'right', 'UTF-8', 1) . '"' . $this->separator;
  1856. print price2num(abs($line->debit - $line->credit)) . $this->separator;
  1857. print $line->sens . $this->separator;
  1858. print $date . $this->separator;
  1859. print $this->separator;
  1860. print $this->separator;
  1861. print 'EUR';
  1862. print $this->end_line;
  1863. }
  1864. }
  1865. }
  1866. /**
  1867. * Export format : iSuite Expert
  1868. *
  1869. * by OpenSolus [https://opensolus.fr]
  1870. *
  1871. * @param array $objectLines data
  1872. *
  1873. * @return void
  1874. */
  1875. public function exportiSuiteExpert($objectLines)
  1876. {
  1877. $this->separator = ';';
  1878. $this->end_line = "\r\n";
  1879. foreach ($objectLines as $line) {
  1880. $tab = array();
  1881. $date = dol_print_date($line->doc_date, '%d/%m/%Y');
  1882. $tab[] = $line->piece_num;
  1883. $tab[] = $date;
  1884. $tab[] = substr($date, 6, 4);
  1885. $tab[] = substr($date, 3, 2);
  1886. $tab[] = substr($date, 0, 2);
  1887. $tab[] = $line->doc_ref;
  1888. //Conversion de chaine UTF8 en Latin9
  1889. $tab[] = mb_convert_encoding(str_replace(' - Compte auxiliaire', '', $line->label_operation), "Windows-1252", 'UTF-8');
  1890. //Calcul de la longueur des numéros de comptes
  1891. $taille_numero = strlen(length_accountg($line->numero_compte));
  1892. //Création du numéro de client générique
  1893. $numero_cpt_client = '411';
  1894. for ($i = 1; $i <= ($taille_numero - 3); $i++) {
  1895. $numero_cpt_client .= '0';
  1896. }
  1897. //Création des comptes auxiliaire des clients
  1898. if (length_accountg($line->numero_compte) == $numero_cpt_client) {
  1899. $tab[] = rtrim(length_accounta($line->subledger_account), "0");
  1900. } else {
  1901. $tab[] = length_accountg($line->numero_compte);
  1902. }
  1903. $nom_client = explode(" - ", $line->label_operation);
  1904. $tab[] = mb_convert_encoding($nom_client[0], "Windows-1252", 'UTF-8');
  1905. $tab[] = price($line->debit);
  1906. $tab[] = price($line->credit);
  1907. $tab[] = price($line->montant);
  1908. $tab[] = $line->code_journal;
  1909. $separator = $this->separator;
  1910. print implode($separator, $tab) . $this->end_line;
  1911. }
  1912. }
  1913. /**
  1914. * trunc
  1915. *
  1916. * @param string $str String
  1917. * @param integer $size Data to trunc
  1918. * @return string
  1919. */
  1920. public static function trunc($str, $size)
  1921. {
  1922. return dol_trunc($str, $size, 'right', 'UTF-8', 1);
  1923. }
  1924. /**
  1925. * toAnsi
  1926. *
  1927. * @param string $str Original string to encode and optionaly truncate
  1928. * @param integer $size Truncate string after $size characters
  1929. * @return string String encoded in Windows-1251 charset
  1930. */
  1931. public static function toAnsi($str, $size = -1)
  1932. {
  1933. $retVal = dol_string_nohtmltag($str, 1, 'Windows-1251');
  1934. if ($retVal >= 0 && $size >= 0) {
  1935. $retVal = mb_substr($retVal, 0, $size, 'Windows-1251');
  1936. }
  1937. return $retVal;
  1938. }
  1939. }