PageRenderTime 110ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/elegend_pro/class/carrier.php

https://gitlab.com/ptisky/API_prestashop
PHP | 185 lines | 112 code | 49 blank | 24 comment | 14 complexity | 7d859660d494d5ae3d36511b350fd58d MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package e-legend pro
  5. * @author Abouloula Taoufik - E-LEGEND PRO
  6. * @version 1.0
  7. * @copyright Copyright (c) Abouloula Taoufik - E-LEGEND PRO
  8. *
  9. */
  10. class Carriers
  11. {
  12. public function getApi()
  13. {
  14. return Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'pro_elegend_api where id=1');
  15. }
  16. public function getMaxId()
  17. {
  18. $sql = Db::getInstance()->getValue('SELECT max(`id_carrier`) FROM '._DB_PREFIX_.'carrier');
  19. return $sql + 1;
  20. }
  21. public function getMaxPosition()
  22. {
  23. $sql = Db::getInstance()->getValue('SELECT max(`position`) FROM '._DB_PREFIX_.'carrier');
  24. return $sql + 1;
  25. }
  26. public function getMaxIdRangPrice()
  27. {
  28. $sql = Db::getInstance()->getValue('SELECT max(`id_range_price`) FROM '._DB_PREFIX_.'range_price');
  29. return $sql + 1;
  30. }
  31. public function setGroupCarrier($id_carrier)
  32. {
  33. $sql = Db::getInstance()->ExecuteS('INSERT IGNORE INTO '._DB_PREFIX_.'carrier_group SELECT '.pSQL($id_carrier).', id_group FROM '._DB_PREFIX_.'group');
  34. return $sql;
  35. }
  36. public function setZoneCarrier($id_carrier)
  37. {
  38. $sql = Db::getInstance()->ExecuteS('INSERT IGNORE INTO '._DB_PREFIX_.'carrier_zone SELECT '.pSQL($id_carrier).', id_zone FROM '._DB_PREFIX_.'zone WHERE active=1');
  39. return $sql;
  40. }
  41. public function setDeliveryCarrier($id_carrier,$id_range_price,$price)
  42. {
  43. return Db::getInstance()->ExecuteS('INSERT IGNORE INTO '._DB_PREFIX_.'delivery (id_carrier, id_range_price, price, id_zone)
  44. SELECT '.pSQL($id_carrier).', '.pSQL($id_range_price).', '.pSQL($price).', id_zone FROM '._DB_PREFIX_.'zone WHERE active=1');
  45. }
  46. public function getCarrier()
  47. {
  48. $sql = Db::getInstance()->ExecuteS('SELECT id_reference FROM '._DB_PREFIX_.'carrier WHERE external_module_name = "expedition_ell" AND active=1 AND deleted=0');
  49. return count($sql);
  50. }
  51. /**
  52. * mettreajourManufacturer Insertion manufacturer
  53. *
  54. * @param string $id_category id de la categorie *
  55. * @return true
  56. */
  57. public function _carrier($id_carrier)
  58. {
  59. try
  60. {
  61. $webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
  62. $opt = array('resource' => 'carriers');
  63. $opt['id'] = (int)$id_carrier;
  64. $xml = $webService->get($opt);
  65. $c = $xml->children()->children();
  66. $id_c = (int)$this->getMaxId();
  67. $get_id_carrier = $this->getCarrier();
  68. if($get_id_carrier == 0){
  69. // insertion table carrier
  70. if(!Db::getInstance()->insert('carrier', array(
  71. 'id_carrier' => pSQL((int)$id_c),
  72. 'id_reference' => pSQL((int)$id_c),
  73. 'name' => pSQL($c->name),
  74. 'url' => pSQL($c->url),
  75. 'active' => pSQL((int)1),
  76. 'shipping_handling' => pSQL((int)1),
  77. 'need_range' => pSQL($c->name),
  78. 'external_module_name' => pSQL('expedition_ell'),
  79. 'shipping_method' => pSQL((int)2),
  80. 'position' => (int)$this->getMaxId(),
  81. ))) die('Error in carrier insert : '.$c->id);
  82. // insertion table carrier_lang
  83. if(!Db::getInstance()->insert('carrier_lang', array(
  84. 'id_carrier' => pSQL((int)$id_c),
  85. 'id_shop' => pSQL((int)1),
  86. 'id_lang' => pSQL((int)1),
  87. 'delay' => pSQL($c->delay->language[0][0]),
  88. ))) die('Error in carrier_lang insert : '.$c->id);
  89. // insertion table carrier_shop
  90. if(!Db::getInstance()->insert('carrier_shop', array(
  91. 'id_carrier' => pSQL((int)$id_c),
  92. 'id_shop' => 1,
  93. ))) die('Error in manufacturer_shop insert : '.$c->id);
  94. // insertion table carrier_zone
  95. $this->setZoneCarrier($id_c);
  96. // insertion table carrier_group
  97. $this->setGroupCarrier($id_c);
  98. // insertion table carrier_tax_rules_group_shop
  99. if(!Db::getInstance()->insert('carrier_tax_rules_group_shop', array(
  100. 'id_carrier' => pSQL((int)$id_c),
  101. 'id_tax_rules_group' => 0,
  102. 'id_shop' => 1,
  103. ))) die('Error in carrier_tax_rules_group_shop insert : '.$c->id);
  104. $maxIdRangPrice = $this->getMaxIdRangPrice();
  105. //$price = 1.8;
  106. // insertion table range_price
  107. if(!Db::getInstance()->insert('range_price', array(
  108. 'id_range_price' => pSQL((int)$maxIdRangPrice),
  109. 'id_carrier' => pSQL((int)$id_c),
  110. 'delimiter1' => 0,
  111. 'delimiter2' => 8,
  112. ))) die('Error in range_price insert : '.$c->id);
  113. $this->setDeliveryCarrier($id_c,$maxIdRangPrice,1.8);
  114. // insertion table carrier_tax_rules_group_shop
  115. $nextMaxIdRangPrice = $this->getMaxIdRangPrice();
  116. //$price = 0;
  117. if(!Db::getInstance()->insert('range_price', array(
  118. 'id_range_price' => $nextMaxIdRangPrice,
  119. 'id_carrier' => pSQL((int)$id_c),
  120. 'delimiter1' => 8,
  121. 'delimiter2' => 999999,
  122. ))) die('Error in range_price insert : '.$c->id);
  123. $this->setDeliveryCarrier($id_c,$nextMaxIdRangPrice,0);
  124. }
  125. }
  126. catch (PrestaShopWebserviceException $e)
  127. {
  128. $trace = $e->getTrace();
  129. if ($trace[0]['args'][0] == 404) echo 'Bad ID';
  130. else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
  131. else echo 'Other error'.$e->getMessage().'</b>';
  132. }
  133. return true;
  134. }
  135. } //Fin class Catalog
  136. ?>