PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/install/couchdb/migrate/convert_agenda.php

https://bitbucket.org/speedealing/speedealing
PHP | 188 lines | 125 code | 32 blank | 31 comment | 16 complexity | 1631c580c17dd8a011f68b7b49035573 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2012 Herve Prot <herve.prot@symeos.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. require_once "../../../main.inc.php";
  18. require_once DOL_DOCUMENT_ROOT . "/core/class/html.formother.class.php";
  19. require_once(DOL_DOCUMENT_ROOT . "/contact/class/contact.class.php");
  20. $langs->load("companies");
  21. $langs->load("customers");
  22. $langs->load("suppliers");
  23. $langs->load("commercial");
  24. /* Array of database columns which should be read and sent back to DataTables. Use a space where
  25. * you want to insert a non-database field (for example a counter or static image)
  26. */
  27. $couchdb = clone $couch;
  28. $flush = $_GET["flush"];
  29. if ($flush) {
  30. // reset old value
  31. $result = $couchdb->limit(50000)->getView('Agenda', 'target_id');
  32. $i = 0;
  33. if (count($result->rows) == 0) {
  34. print "Effacement terminé";
  35. exit;
  36. }
  37. foreach ($result->rows AS $aRow) {
  38. $obj[$i]->_id = $aRow->value->_id;
  39. $obj[$i]->_rev = $aRow->value->_rev;
  40. $i++;
  41. }
  42. try {
  43. $couchdb->deleteDocs($obj);
  44. } catch (Exception $e) {
  45. echo "Something weird happened: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
  46. exit(1);
  47. }
  48. print "Effacement en cours";
  49. exit;
  50. }
  51. $objsoc = new Societe($db);
  52. $result = $objsoc->getView("list");
  53. foreach ($result->rows as $aRow) {
  54. $soc[$aRow->value->rowid] = $aRow->value;
  55. }
  56. $objcon = new Contact($db);
  57. $result = $objcon->getView("list");
  58. foreach ($result->rows as $aRow) {
  59. $contact[$aRow->value->rowid] = $aRow->value;
  60. }
  61. /* basic companies request query */
  62. $sql = "SELECT s.*,";
  63. $sql.= " p.code, p.libelle as codelabel, u1.login as user_author, u2.login as user_action, u3.login as user_done, u4.login as user_mod";
  64. $sql .= " FROM (" . MAIN_DB_PREFIX . "actioncomm as s";
  65. $sql.= " ) ";
  66. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "c_actioncomm as p on (p.id = s.fk_action)";
  67. $sql.= " LEFT JOIN llx_user AS u1 ON u1.rowid = s.fk_user_author";
  68. $sql.= " LEFT JOIN llx_user AS u2 ON u2.rowid = s.fk_user_action";
  69. $sql.= " LEFT JOIN llx_user AS u3 ON u3.rowid = s.fk_user_done";
  70. $sql.= " LEFT JOIN llx_user AS u4 ON u4.rowid = s.fk_user_mod";
  71. if ($type != '')
  72. $sql.= " AND st.type=" . $type;
  73. $sql.= " AND s.entity = " . $conf->entity;
  74. /* get the total of entries */
  75. $resultTotal = $db->query($sql);
  76. $iTotal = $db->num_rows($resultTotal);
  77. //print $sql;exit;
  78. $resultContacts = $db->query($sql);
  79. //$cb = new couchClient("http://193.169.46.49:5984/","dolibarr");
  80. //$cb = new Couchbase;
  81. //$cb->default_bucket_name="dolibarr";
  82. //$cb->addCouchbaseServer("localhost",11211,8092);
  83. //$cb->flush();
  84. //$uuid=$cb->uuid($iTotal); //generation des uuids
  85. /* get companies. usefull to get their sales and categories */
  86. $i = 0;
  87. while ($aRow = $db->fetch_object($resultAgenda)) {
  88. $aRow->rowid = $aRow->id;
  89. $col[$aRow->rowid]->rowid = (int) $aRow->id;
  90. $col[$aRow->rowid]->class = "Agenda";
  91. $col[$aRow->rowid]->entity = $conf->Couchdb->name;
  92. $col[$aRow->rowid]->datep = $db->jdate($aRow->datep);
  93. $col[$aRow->rowid]->datef = $db->jdate($aRow->datef);
  94. $col[$aRow->rowid]->type_code = $aRow->code;
  95. if(!empty($aRow->label))
  96. $col[$aRow->rowid]->label = $aRow->label;
  97. else
  98. $col[$aRow->rowid]->label = $aRow->codelabel;
  99. $col[$aRow->rowid]->datec = $db->jdate($aRow->datec);
  100. $col[$aRow->rowid]->tms = $db->jdate($aRow->tms);
  101. if (!empty($aRow->user_author)) {
  102. $col[$aRow->rowid]->author->id = "user:" . $aRow->user_author;
  103. $col[$aRow->rowid]->author->name = $aRow->user_author;
  104. }
  105. else
  106. $col[$aRow->rowid]->author = new stdClass ();
  107. if (!empty($aRow->user_action)) {
  108. $col[$aRow->rowid]->usertodo->id = "user:" . $aRow->user_action;
  109. $col[$aRow->rowid]->usertodo->name = $aRow->user_action;
  110. } else
  111. $col[$aRow->rowid]->usertodo = new stdClass ();
  112. if (!empty($aRow->user_done)) {
  113. $col[$aRow->rowid]->userdone->id = "user:" . $aRow->user_done;
  114. $col[$aRow->rowid]->userdone->name = $aRow->user_done;
  115. } else
  116. $col[$aRow->rowid]->userdone = new stdClass ();
  117. if (!empty($aRow->user_mod))
  118. $col[$aRow->rowid]->user_modif = "user:" . $aRow->user_mod;
  119. if (isset($soc[$aRow->fk_soc]->rowid)) {
  120. $col[$aRow->rowid]->societe->id = $soc[$aRow->fk_soc]->_id;
  121. $col[$aRow->rowid]->societe->name = $soc[$aRow->fk_soc]->name;
  122. } else
  123. $col[$aRow->rowid]->societe = new stdClass();
  124. if (isset($contact[$aRow->fk_contact]->rowid)) {
  125. $col[$aRow->rowid]->contact->id = $contact[$aRow->fk_contact]->_id;
  126. $col[$aRow->rowid]->contact->name = $contact[$aRow->fk_contact]->name;
  127. }else
  128. $col[$aRow->rowid]->contact = new stdClass();
  129. $col[$aRow->rowid]->fulldayevent = (bool) $aRow->fulldayevent;
  130. $col[$aRow->rowid]->punctual = (bool) $aRow->punctual;
  131. $col[$aRow->rowid]->durationp = (int) $aRow->durationp;
  132. $col[$aRow->rowid]->percentage = (int) $aRow->percent;
  133. $col[$aRow->rowid]->location = $aRow->location;
  134. $col[$aRow->rowid]->notes = $aRow->note;
  135. if ($aRow->percent == 0)
  136. $col[$aRow->rowid]->Status = "TODO";
  137. elseif ($aRow->percent == 100)
  138. $col[$aRow->rowid]->Status = "DONE";
  139. else
  140. $col[$aRow->rowid]->Status = "ON";
  141. $i++;
  142. }
  143. $db->free($resultAgenda);
  144. unset($resultAgenda);
  145. //print_r($col);
  146. //exit;
  147. try {
  148. $couchdb->clean($col);
  149. $result = $couchdb->storeDocs($col, false);
  150. } catch (Exception $e) {
  151. echo "Something weird happened: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
  152. exit(1);
  153. }
  154. print_r($result);
  155. print "Import société terminée : " . count($col);
  156. ?>