catalyzImportTask.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. class catalyzImportTask extends sfPropelBaseTask {
  3. protected function configure() {
  4. $this->namespace = 'catalyz';
  5. $this->name = 'import';
  6. $this->briefDescription = '';
  7. $this->detailedDescription = <<<EOF
  8. The [catalyz:import|INFO] task does things.
  9. Call it with:
  10. [php symfony catalyz:import|INFO]
  11. EOF;
  12. $this->addArgument('application', sfCommandArgument::REQUIRED, 'The application name');
  13. // add other arguments here
  14. $this->addOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev');
  15. $this->addOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel');
  16. // add other options here
  17. }
  18. protected function execute($arguments = array(), $options = array()) {
  19. // Database initialization
  20. $databaseManager = new sfDatabaseManager($this->configuration);
  21. $connection = Propel::getConnection($options['connection'] ? $options['connection'] : '');
  22. $configuration = ProjectConfiguration::getApplicationConfiguration('admin', 'jroussel', true);
  23. sfContext::createInstance($configuration);
  24. $this->importConfig();
  25. $this->importNodes();
  26. $this->importMembers();
  27. $this->importSuppliers();
  28. $this->importProducts();
  29. $this->addResponsibleToNodes();
  30. $this->updateConfig();
  31. }
  32. protected function importConfig() {
  33. $katao_config = new KataoConfig();
  34. $katao_config->setName('KATAO SARL');
  35. $katao_config->setAddress('LE BAT
  36. 82110 CAZES-MONDENARD');
  37. $katao_config->setPhone('06 82 26 68 19');
  38. $katao_config->setEmail('contact@katao.eu');
  39. $katao_config->setInitialTransactionsCount(2314);
  40. $katao_config->setInitialTransactionsSum(17729.35);
  41. $katao_config->setAccountingCodeBank('512000');
  42. $katao_config->setAccountingCodeBankSol('521000');
  43. $katao_config->setAccountingCodePrefixPurchase('607');
  44. $katao_config->setAccountingCodePrefixSell('707');
  45. $katao_config->setAccountingCodeSolDiscount('709000');
  46. $katao_config->setAccountingCodeErrorAdjustmentPositive('758000');
  47. $katao_config->setAccountingCodeErrorAdjustmentNegative('658000');
  48. $katao_config->setMailingListEmail('katao');
  49. $katao_config->setDefaultProductPicture('/uploads/repository/produits/unavailable.gif');
  50. $katao_config->setCartValidationDaysNumber(3);
  51. $katao_config->setAdditionalCurrencyName('SOL');
  52. $katao_config->setSiretNumber('50139732700013');
  53. $katao_config->setTvaNumber('tva');
  54. $katao_config->setRcsNumber('501397327');
  55. $katao_config->setCapital(1000);
  56. $katao_config->save();
  57. }
  58. protected function importNodes() {
  59. $xml = simplexml_load_file(sfConfig::get('sf_data_dir') . '/xml/noeud.xml');
  60. foreach($xml->children() as $xmlNode) {
  61. $katao_node = new KataoNode();
  62. $katao_node->setCity((string)$xmlNode->NOE_NOM);
  63. $katao_node->setBeginAt(mktime(12, 0, 0, 3, 18, 2006));
  64. $katao_node->setIsDefault('AUCUN' == $katao_node->getCity());
  65. $katao_node->save();
  66. $this->logSection('node+', $katao_node->getCity());
  67. }
  68. }
  69. protected function importMembers() {
  70. $members_data = array();
  71. $handle = fopen(sfConfig::get('sf_data_dir') . '/xml/KATAO ADHERENTS.csv', 'r');
  72. fgetcsv($handle, 1000, ";");
  73. while (($data = fgetcsv($handle, 1000, ";")) !== false) {
  74. $created_at = time();
  75. if ('' != $value = trim($data[2])) {
  76. list($day, $month, $year) = explode('/', $value);
  77. $created_at = mktime(0, 0, 0, (int)$month, (int)$day, (int)$year);
  78. }
  79. $members_data[trim($data[0])] = $created_at;
  80. }
  81. $xml = simplexml_load_file(sfConfig::get('sf_data_dir') . '/xml/ADHERENTS.xml');
  82. foreach($xml->children() as $adherent) {
  83. $katao_member = new KataoMember();
  84. $katao_member->setKataoIndex((int)$adherent->ADH_IND);
  85. $katao_member->setKataoNodeId((int)$adherent->NOE_ID);
  86. $katao_member->setFirstName((string)$adherent->ADH_PRENOM);
  87. $katao_member->setLastName((string)$adherent->ADH_NOM);
  88. $katao_member->setInitialAmountEuro(round((float)$adherent->ADH_SOLDE_EURO, 2));
  89. $katao_member->setInitialAmountSol(0);
  90. $katao_member->setIsReferer(1 == (int)$adherent->ADH_REFERENT);
  91. $katao_member->setIsDelegate(1 == (int)$adherent->ADH_DELEGUE);
  92. $katao_member->setCreatedAt(!empty($members_data[$katao_member->getLastName()])?$members_data[$katao_member->getLastName()]:time());
  93. $katao_member->save();
  94. $katao_user = new KataoUser();
  95. $katao_user->setEmail((string)$adherent->ADH_MAIL);
  96. $katao_user->setAddress1((string)$adherent->ADH_ADR);
  97. $katao_user->setAddress2((string)$adherent->ADH_ADR2);
  98. $katao_user->setZip((string)$adherent->ADH_CPO);
  99. $katao_user->setCity((string)$adherent->ADH_VILLE);
  100. $katao_user->setPhone((string)$adherent->ADH_TEL);
  101. $katao_user->setFax((string)$adherent->ADH_FAX);
  102. $katao_user->setGmapLat((string)$adherent->ADH_LATITUDE);
  103. $katao_user->setGmapLng((string)$adherent->ADH_LONGITUDE);
  104. $katao_user->setLogin((string)$adherent->ADH_LOG);
  105. $katao_user->setPassword((string)$adherent->ADH_PAS);
  106. $katao_user->setIsSeo(1 == (int)$adherent->ADH_GERANT);
  107. $katao_user->setIsAdmin(1 == (int)$adherent->ADH_ADMIN);
  108. $katao_user->setKataoMember($katao_member);
  109. $katao_user->setStatus((1 == (int)$adherent->ADH_ACTIF)?KataoUser::STATUS_ACTIVE:KataoUser::STATUS_DISABLED);
  110. $katao_user->save();
  111. $this->logSection('member+', $katao_member->getFullName());
  112. }
  113. }
  114. protected function importSuppliers() {
  115. $xml = simplexml_load_file(sfConfig::get('sf_data_dir') . '/xml/fournisseur.xml');
  116. foreach($xml->children() as $supplierNode) {
  117. $ADH_IND = (int)$supplierNode->ADH_IND;
  118. $katao_member = null;
  119. if ($ADH_IND && 1 != $ADH_IND) {
  120. $katao_member = KataoMemberPeer::retrieveByKataoIndex($ADH_IND);
  121. }
  122. // do not save supplier related to member
  123. $katao_member = null;
  124. $katao_supplier = new KataoSupplier();
  125. $katao_supplier->setKataoIndex((int)$supplierNode->FOU_IND);
  126. $katao_supplier->setKataoMemberRelatedByMemberId($katao_member);
  127. $katao_supplier->setKataoMemberRelatedByRefererId(KataoMemberPeer::retrieveByKataoIndex((int)$supplierNode->FOU_REFERENT));
  128. $katao_supplier->setName((string)$supplierNode->FOU_NOM);
  129. $katao_supplier->setPresentation('<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec consequat lobortis sapien. Proin quis justo. Nam rhoncus dolor eget libero. Integer pede libero, lobortis vitae, auctor vel, feugiat a, nisi. Pellentesque mattis suscipit urna. Praesent consequat imperdiet pede. Aliquam eu ipsum nec leo dictum pretium. Praesent arcu quam, blandit a, tincidunt nec, aliquam id, urna. Aliquam vulputate pulvinar risus. Nunc sagittis adipiscing purus.</p>
  130. <p>Aenean mi ligula, vehicula ut, ultricies in, porta sed, diam. Curabitur commodo. Vivamus pretium venenatis sapien. Donec ante lacus, fermentum in, euismod euismod, ullamcorper ut, augue. Maecenas adipiscing mi nec tortor. Vestibulum pellentesque augue non mi. Nulla pellentesque varius nisi. Sed tempus tristique magna. Vivamus sapien. Integer vel erat. Etiam semper libero sit amet lorem. Integer erat mauris, sodales at, luctus eget, facilisis ac, nisi. Nunc pede. Mauris turpis. Donec congue, libero at aliquam laoreet, sapien nisi egestas arcu, vitae vulputate nisl metus eu dui. Vestibulum mauris. Aenean vitae diam. In neque felis, ultrices nec, pellentesque venenatis, vehicula nec, eros. Ut sagittis posuere massa.</p>');
  131. $katao_supplier->setEmail((string)$supplierNode->FOU_EMAIL);
  132. $katao_supplier->setAddress1((string)$supplierNode->FOU_ADRESSE);
  133. $katao_supplier->setAddress2((string)$supplierNode->FOU_aDRESSE_FIN);
  134. $katao_supplier->setZip((string)$supplierNode->FOU_CP);
  135. $katao_supplier->setCity((string)$supplierNode->FOU_VILLE);
  136. $katao_supplier->setPhone((string)$supplierNode->FOU_TELEPHONE);
  137. $katao_supplier->setFax((string)$supplierNode->FOU_TELECOPIE);
  138. $katao_supplier->setGmapLat((string)$supplierNode->FOU_LATITUDE);
  139. $katao_supplier->setGmapLng((string)$supplierNode->FOU_LONGITUDE);
  140. $siret = (string)$supplierNode->FOU_SIRET;
  141. $katao_supplier->setSiretNumber(($siret && '?' != $siret)?Utils::formatSiretNmber($siret):sprintf('%d-???', (int)$supplierNode->FOU_IND));
  142. $katao_supplier->setIsGlobal(false);
  143. $katao_supplier->setOrderMinAmount(0);
  144. $katao_supplier->save();
  145. $katao_user = null;
  146. if (!is_null($katao_member)) {
  147. $katao_user = $katao_member->getFirstKataoUser();
  148. }
  149. if (is_null($katao_user)) {
  150. $katao_user = new KataoUser();
  151. $katao_user->setEmail((string)$supplierNode->FOU_EMAIL);
  152. $katao_user->setAddress1((string)$supplierNode->FOU_ADRESSE);
  153. $katao_user->setAddress2((string)$supplierNode->FOU_aDRESSE_FIN);
  154. $katao_user->setZip((string)$supplierNode->FOU_CP);
  155. $katao_user->setCity((string)$supplierNode->FOU_VILLE);
  156. $katao_user->setPhone((string)$supplierNode->FOU_TELEPHONE);
  157. $katao_user->setFax((string)$supplierNode->FOU_TELECOPIE);
  158. $katao_user->setGmapLat((string)$supplierNode->FOU_LATITUDE);
  159. $katao_user->setGmapLng((string)$supplierNode->FOU_LONGITUDE);
  160. $katao_user->setLogin($katao_supplier->getUrlIdentifier());
  161. $katao_user->setPassword('katao');
  162. }
  163. $katao_user->setKataoSupplier($katao_supplier);
  164. $katao_user->save();
  165. $katao_supplier_node = new KataoSupplierNode();
  166. $katao_supplier_node->setKataoSupplier($katao_supplier);
  167. $katao_supplier_node->setKataoNodeId(2); //LAFRANCAISE
  168. $katao_supplier_node->save();
  169. $katao_supplier_node = new KataoSupplierNode();
  170. $katao_supplier_node->setKataoSupplier($katao_supplier);
  171. $katao_supplier_node->setKataoNodeId(3); //MONTAUBAN
  172. $katao_supplier_node->save();
  173. $this->logSection('supplier+', $katao_supplier->getName());
  174. }
  175. }
  176. protected function importProducts() {
  177. $categories = array();
  178. $families = array();
  179. $index = 1;
  180. $xml = simplexml_load_file(sfConfig::get('sf_data_dir') . '/xml/rayon.xml');
  181. foreach($xml->children() as $katao_product_category_node) {
  182. if (1 == (int)$katao_product_category_node->RAY_Envente) {
  183. $katao_product_category = new KataoProductCategory();
  184. $katao_product_category->setName((string)$katao_product_category_node->RAY_Nom);
  185. $katao_product_category->setAccountingCodeSuffix(sprintf('%d00', $index));
  186. $katao_product_category->setSortOrder($index++);
  187. $katao_product_category->save();
  188. $categories[(int)$katao_product_category_node->RAY_ID] = $katao_product_category->getId();
  189. $this->logSection('category+', $katao_product_category->getName());
  190. }
  191. }
  192. $xml = simplexml_load_file(sfConfig::get('sf_data_dir') . '/xml/famille.xml');
  193. foreach($xml->children() as $katao_product_family_node) {
  194. if (1 == (int)$katao_product_family_node->FAM_Envente && !empty($categories[(int)$katao_product_family_node->RAY_ID])) {
  195. $katao_product_family = new KataoProductFamily();
  196. $katao_product_family->setKataoProductCategoryId($categories[(int)$katao_product_family_node->RAY_ID]);
  197. $katao_product_family->setName((string)$katao_product_family_node->FAM_NOM);
  198. $katao_product_family->save();
  199. $families[(int)$katao_product_family_node->FAM_ID] = $katao_product_family->getId();
  200. $this->logSection('family+', $katao_product_family->getName());
  201. }
  202. }
  203. $xml = simplexml_load_file(sfConfig::get('sf_data_dir') . '/xml/produit.xml');
  204. foreach($xml->children() as $katao_product_node) {
  205. if (!empty($categories[(int)$katao_product_node->RAY_ID]) && !empty($families[(int)$katao_product_node->FAM_ID])) {
  206. $katao_product = new KataoProduct();
  207. $katao_product->setReference(trim((string)$katao_product_node->PRO_REF));
  208. $katao_product->setName((string)$katao_product_node->PRO_NOM);
  209. $katao_product->setDescription((string)$katao_product_node->pro_designation);
  210. $katao_product->setKataoProductFamilyId($families[(int)$katao_product_node->FAM_ID]);
  211. $katao_product->setKataoSupplier(KataoSupplierPeer::retrieveByKataoIndex((int)$katao_product_node->FOU_IND));
  212. $katao_product->setMaxSolAmount(floor(CONST_EURO_TO_SOL * (float)$katao_product_node->PRO_TTC * 5 / 100));
  213. $katao_product->setUnitPriceEuro(round((float)$katao_product_node->PRO_TTC / (1 + (float)$katao_product_node->PRO_mar) / (1 + (float)$katao_product_node->PRO_TVA), 3));
  214. $katao_product->setTvaRate((float)$katao_product_node->PRO_TVA);
  215. $katao_product->setMinOrderNumber(max(1, (int)$katao_product_node->PRO_MIN));
  216. $katao_product->setMargin((float)$katao_product_node->PRO_mar);
  217. $picture_distant = utf8_decode(trim((string)$katao_product_node->PRO_IMG));
  218. $picture_local = '/uploads/repository/produits/' . Catalyz::normalizeUrlIdentifier(trim((string)$katao_product_node->PRO_IMG));
  219. if (false === strpos($picture_local, '.bmp') && '' != $picture_distant && '<FONT SIZE=1>Libell&eacute;</FONT>' != $picture_distant && '' != $picture_local) {
  220. if (file_put_contents(sprintf('%s%s', sfConfig::get('sf_web_dir'), $picture_local), file_get_contents(sprintf('http://lereseau.katao.eu/KATAO_WEB/%s', rawurlencode($picture_distant))))) {
  221. $katao_product->setPicture($picture_local);
  222. }
  223. }
  224. $katao_product->setDeliveryDelay((int)$katao_product_node->PRO_DEL);
  225. $katao_product->setIsArchived((int)$katao_product_node->PRO_ARCHIVER);
  226. $katao_product->save();
  227. $this->logSection('product+', $katao_product->getName());
  228. }
  229. }
  230. }
  231. protected function addResponsibleToNodes() {
  232. $xml = simplexml_load_file(sfConfig::get('sf_data_dir') . '/xml/noeud.xml');
  233. foreach($xml->children() as $xmlNode) {
  234. if (null != $katao_node = KataoNodePeer::retrieveByCity((string)$xmlNode->NOE_NOM)) {
  235. if (null != $katao_member = KataoMemberPeer::retrieveByKataoIndex((int)$xmlNode->ADH_IND)) {
  236. if (null != $katao_user = $katao_member->getFirstKataoUser()) {
  237. $katao_node->setResponsible($katao_user->getId());
  238. $katao_node->save();
  239. }
  240. }
  241. }
  242. }
  243. }
  244. protected function updateConfig() {
  245. $katao_config = wpConfig::getInstance(true);
  246. $katao_config->setKataoMemberId(1);
  247. $katao_config->save();
  248. wpConfig::setInstance($katao_config);
  249. }
  250. }