PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/2.071.0/_import_commandes_csv.class.php

https://github.com/crepeausucre/soothERP
PHP | 421 lines | 289 code | 68 blank | 64 comment | 20 complexity | 35e724cd8adbe3d5aff5cd1a446196f4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0
  1. <?php
  2. // *************************************************************************************************************
  3. // CLASSE REGISSANT les import de commandes depuis un fichier CSV
  4. // *************************************************************************************************************
  5. class import_commandes_csv {
  6. private $id_import;
  7. private $etape; //etape en cours de l'import
  8. private $commandes;
  9. private $type_ref_art;
  10. private $id_categ_client;
  11. private $ligne_number;
  12. private $erreurs;
  13. public function __construct($id = 0) {
  14. global $bdd;
  15. //*********************************
  16. // INITAILISATION DES MEMBRES
  17. $this->etape = 0;
  18. $this->commandes = array();
  19. $this->erreur = array();
  20. $this->id_categ_client = 1;
  21. //création d'un import
  22. if($id == 0){
  23. $this->id_import = $this->create();
  24. }else{
  25. $this->id_import = $id;
  26. }
  27. return true;
  28. }
  29. /**
  30. * @return int l'id de l'import en base de données
  31. */
  32. private function create(){
  33. global $bdd;
  34. $query= "INSERT INTO csv_import_cdc
  35. VALUES('', '' ,'', NOW(), 0);";
  36. $res = $bdd->exec($query);
  37. if($res==1){return $bdd->lastInsertId();}
  38. return 0;
  39. }
  40. /**
  41. * @param unknown_type $file
  42. * @return unknown_type
  43. */
  44. public function parser_fichier($file){
  45. global $bdd;
  46. global $DEFAUT_ID_PAYS;
  47. $tab_ref_cdc = array();
  48. $fp = fopen($_FILES['fichier_csv']['tmp_name'], "r");
  49. if($fp){
  50. //*****************************************
  51. // INITIALISATION DES ID
  52. $this->ligne_number = 0;
  53. $query = "SELECT MAX(id_import_cdc_infos_doc) as id FROM csv_import_cdc_infos_doc";
  54. $stt = $bdd->query($query);
  55. $ref_commande_tmp = 0;
  56. if(is_object($stt) && $cdc = $stt->fetchObject()){
  57. $ref_commande_tmp = $cdc->id;
  58. $stt->closeCursor();
  59. }
  60. $query = "SELECT MAX(id_import_cdc_infos_lines) as id FROM csv_import_cdc_infos_lines";
  61. $stt = $bdd->query($query);
  62. $ligne_article_number = 0;
  63. if(is_object($stt) && $cdc_line = $stt->fetchObject()){
  64. $ligne_article_number = $cdc_line->id;
  65. $stt->closeCursor();
  66. }
  67. //********************************************
  68. // parcours du fichier
  69. while($ligne = fgets($fp)){
  70. ++$this->ligne_number;
  71. //si commentaire
  72. if($ligne[0] == '#'){ continue; }
  73. $infos_ligne = explode(';',$ligne);
  74. switch($infos_ligne[0]){
  75. case 'CDC':
  76. ++$ref_commande_tmp;
  77. //*****************************************
  78. //attribution des colonnes
  79. $ref_cdc_externe = $infos_ligne[1];
  80. $ref_client = $infos_ligne[2];
  81. $nom_client = $infos_ligne[3];
  82. $adr_fac_text = $infos_ligne[4];
  83. $adr_fac_cp = $infos_ligne[5];
  84. $adr_fac_ville = $infos_ligne[6];
  85. $adr_fac_id_pays = (is_numeric($infos_ligne[7]))? $infos_ligne[7] : $DEFAUT_ID_PAYS ;
  86. $adr_liv_text = $infos_ligne[8];
  87. $adr_liv_cp = $infos_ligne[9];
  88. $adr_liv_ville = $infos_ligne[10];
  89. $adr_liv_id_pays = (is_numeric($infos_ligne[11]))? $infos_ligne[11] : $DEFAUT_ID_PAYS ;
  90. $note = $infos_ligne[12];
  91. $date = $infos_ligne[13];
  92. $tab_ref_cdc[$ref_cdc_externe] = $ref_commande_tmp;
  93. $query = "INSERT INTO csv_import_cdc_infos_doc VALUES
  94. ('".$ref_commande_tmp."', '".$this->id_import."', '".$ref_cdc_externe."', '".$ref_client."', '".$nom_client."',
  95. '".$adr_fac_text."','".$adr_fac_cp."','".$adr_fac_ville."','".$adr_fac_id_pays."',
  96. '".$adr_liv_text."','".$adr_liv_cp."','".$adr_liv_ville."','".$adr_liv_id_pays."', '".$note."', '".$date."' );
  97. ";
  98. $val = $bdd->exec($query);
  99. break;
  100. case 'ART':
  101. ++$ligne_article_number;
  102. //*****************************************
  103. //attribution des colonnes
  104. $ref_cdc_externe = $infos_ligne[1];
  105. $ref_lmb = $infos_ligne[2];
  106. $ref_interne = $infos_ligne[3];
  107. $ref_oem = $infos_ligne[4];
  108. $lib = $infos_ligne[5];
  109. $pu_ht = $infos_ligne[6];
  110. $qte = $infos_ligne[7];
  111. $remise = $infos_ligne[8];
  112. $tva = $infos_ligne[9];
  113. $desc_courte = $infos_ligne[10];
  114. //Gestion des erreurs
  115. if(count($infos_ligne) != 12 ){ $this->erreurs[] = "Nombre de colonne incorrect ligne $this->ligne_number."; }
  116. if(is_numeric($infos_ligne[2])){ $this->erreurs[] = "La colonne PU doit ętre de type numerique ligne $this->ligne_number."; }
  117. if(!empty($ref_cdc_externe) && empty($tab_ref_cdc[$ref_cdc_externe])){
  118. $this->erreurs[] = "La commande de référence $ref_cdc_externe n'est pas défini ŕ la ligne $this->ligne_number .";
  119. return false;
  120. }
  121. $id_cdc = (!empty($ref_cdc_externe))? $tab_ref_cdc[$ref_cdc_externe] : $ref_commande_tmp;
  122. $query = "INSERT INTO csv_import_cdc_infos_lines VALUES
  123. ('".$ligne_article_number ."', '".$id_cdc."', '".$ref_lmb."', '".$ref_interne."', '".$ref_oem."', '".$lib."',
  124. '".$pu_ht."', '".$qte."', '".$remise."', '".$tva."', '".$desc_courte."' );
  125. ";
  126. $val = $bdd->exec($query);
  127. break;
  128. default:
  129. $this->erreurs[] = "Type de ligne incorrect ligne $this->ligne_number.";
  130. break;
  131. }
  132. }
  133. //*****************************************
  134. // ETAPE SUIVANTE
  135. //*****************************************
  136. if(empty($this->erreurs)){
  137. $this->setEtape(1);
  138. }else{
  139. $this->delete();
  140. }
  141. }
  142. }
  143. public function traitement(){
  144. global $bdd;
  145. if($this->etape != 1 ){
  146. return false;
  147. }
  148. //******************************************************
  149. // Chargement des commandes
  150. $query = "SELECT * FROM csv_import_cdc_infos_doc
  151. WHERE id_import = '".$this->id_import."' ";
  152. $stt = $bdd->query($query);
  153. if(is_object($stt)){
  154. //parcours des commandes
  155. while($cdc = $stt->fetchObject()){
  156. //*************************************************************
  157. // Création du contact
  158. $contact = null;
  159. if(!empty($cdc->ref_client)){
  160. $contact = new contact_client($cdc->ref_client);
  161. if(!$contact->getRef_contact()){
  162. $contact = null;
  163. }
  164. }
  165. if($contact == null){
  166. //@TODO verifier si ok
  167. $infos_generales['nom'] = $cdc->nom_client;
  168. $infos_generales['id_civilite'] = 1;
  169. $infos_generales['id_categorie'] = $this->id_categ_client;
  170. $infos_generales['note'] = '';
  171. $infos_generales['siret'] = '';
  172. $infos_generales['tva_intra'] = '';
  173. $infos_profils['profil']['id_client_categ'] = 1;
  174. $infos_profils['profil']['id_profil'] = $GLOBALS['CLIENT_ID_PROFIL'];
  175. $infos_profils['profil']['app_tarifs'] = "";
  176. $contact = new contact();
  177. $contact->create($infos_generales, $infos_profils);
  178. }
  179. //*************************************************************
  180. // Création de la commande
  181. $GLOBALS['_OPTIONS']['CREATE_DOC']['ref_contact'] = $contact->getRef_contact();
  182. $GLOBALS['_OPTIONS']['CREATE_DOC']['id_etat_doc'] = '8';
  183. $GLOBALS['_OPTIONS']['CREATE_DOC']['id_stock'] = $_SESSION['magasin']->getId_stock();
  184. $GLOBALS['_OPTIONS']['CREATE_DOC']['description'] = $cdc->note;
  185. $doc = create_doc(2);
  186. $doc->maj_date_creation($cdc->date);
  187. $doc->maj_text_adresse_contact($cdc->adr_fac_text);
  188. $doc->maj_text_code_postal_contact($cdc->adr_fac_cp);
  189. $doc->maj_text_ville_contact($cdc->adr_fac_ville);
  190. $doc->maj_text_id_pays_contact ($cdc->adr_fac_id_pays);
  191. $doc->maj_text_adresse_livraison($cdc->adr_liv_text);
  192. $doc->maj_text_code_postal_livraison($cdc->adr_liv_cp);
  193. $doc->maj_text_ville_livraison($cdc->adr_liv_ville);
  194. $doc->maj_text_id_pays_livraison ($cdc->adr_liv_id_pays);
  195. //**************************************************************
  196. // Création des lignes de la commande
  197. $query2 = "SELECT * FROM csv_import_cdc_infos_lines
  198. WHERE id_import_cdc_infos_doc = '".$cdc->id_import_cdc_infos_doc."' ";
  199. $stt2 = $bdd->query($query2);
  200. if(is_object($stt2)){
  201. while($art = $stt2->fetchObject()){
  202. $article = new article($art->ref_lmb);
  203. if(!$article->getRef_article()){
  204. $article = article::getArticle_by_ref_interne($art->ref_interne);
  205. if(is_null($article)){ return article::getArticle_by_ref_oem($art->ref_oem); }
  206. }
  207. if($article->getRef_article()){
  208. $infos['type_of_line'] = 'article';
  209. $infos['ref_article'] = $article->getRef_article();
  210. $infos['pu_ht'] = $art->pu_ht;
  211. $infos['qte'] = $art->qte;
  212. $infos['remise'] = $art->remise;
  213. $infos['tva'] = $art->tva;
  214. $infos['desc_courte'] = $art->desc_courte;
  215. $doc->add_line($infos);
  216. }else{
  217. $this->erreurs[] = "l'article $art->ref_lmb / $art->ref_interne / $art->ref_oem / $art->lib n'a pas été trouvé";
  218. //@TODO Création de l'article ?
  219. }
  220. }
  221. }
  222. }// end while
  223. //*****************************************
  224. // ETAPE SUIVANTE
  225. //*****************************************
  226. if(empty($this->erreurs)){
  227. $this->setEtape(2);
  228. }
  229. $this->delete();
  230. }
  231. }
  232. private function delete(){
  233. global $bdd;
  234. $query = " DELETE FROM csv_import_cdc
  235. WHERE id_import_cdc = '".$this->id_import."' ";
  236. $bdd->exec($query);
  237. }
  238. //***********************************************************************************************
  239. // SETTERS
  240. //***********************************************************************************************
  241. /*public function setType_ref_article($type_ref_article){
  242. global $bdd;
  243. if($type_ref_article == 'interne' || $type_ref_article == 'oem' || $type_ref_article == 'lmb'){
  244. $this->type_ref_art = $type_ref_article;
  245. $query = "UPDATE csv_import_cdc SET
  246. type_ref_article = '".$type_ref_article."'
  247. WHERE id_import_cdc = '".$this->id_import."' ;";
  248. $bdd->exec($query);
  249. }else{
  250. $this->erreurs[] = 'Type de référence article incorrect.';
  251. }
  252. }
  253. public function setId_categ_client($id_categ_client){
  254. global $bdd;
  255. if(!is_numeric($id_categ_client)){ $this->erreurs[] = 'Identifiant de catégorie client incorrect.'; }
  256. $this->id_categ_client = $id_categ_client;
  257. $query = "UPDATE csv_import_cdc SET
  258. id_categ_client = '".$id_categ_client."'
  259. WHERE id_import_cdc = '".$this->id_import."' ;";
  260. $bdd->exec($query);
  261. }*/
  262. public function setEtape($id_etape){
  263. global $bdd;
  264. $this->etape = $id_etape;
  265. $query = "UPDATE csv_import_cdc SET
  266. etape = '".$this->etape."'
  267. WHERE id_import_cdc = '".$this->id_import."' ;";
  268. $bdd->exec($query);
  269. }
  270. //***********************************************************************************************
  271. // GETTERS
  272. //***********************************************************************************************
  273. public function getId_import(){
  274. return $this->id_import;
  275. }
  276. public function getContenu(){
  277. return $this->commandes;
  278. }
  279. public function getErreurs(){
  280. $erreurs = $this->erreurs;
  281. $this->erreurs = array();
  282. return $erreurs;
  283. }
  284. function getEtape () {
  285. return $this->etape;
  286. }
  287. /**
  288. * @return int le nombre de lignes traitées.
  289. */
  290. public function getNb_lignes(){
  291. return $this->ligne_number;
  292. }
  293. //***********************************************************************************************
  294. // FONCTIONS STATIQUES
  295. //***********************************************************************************************
  296. public static function getAncien_import($id){
  297. global $bdd;
  298. $query = "SELECT * FROM csv_import_cdc WHERE id_import_cdc = '".$id."' ;";
  299. $stt = $bdd->query($query);
  300. if(is_object($stt) && $imp = $stt->fetchObject()){
  301. $import = new import_commandes_csv($id);
  302. $import->etape = $imp->etape;
  303. return $import;
  304. }
  305. return null;
  306. }
  307. public static function getLast_import($etape){
  308. global $bdd;
  309. $query = "SELECT MAX(id_import_cdc) as id FROM csv_import_cdc WHERE etape = '".$etape."' ;";
  310. $stt = $bdd->query($query);
  311. if(is_object($stt) && $import = $stt->fetchObject()){
  312. return self::getAncien_import($import->id);
  313. }
  314. return null;
  315. }
  316. }
  317. ?>