D7UserProfileCustomer.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\Core\Extension\ModuleHandlerInterface;
  4. use Drupal\migrate\Row;
  5. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  6. use Drupal\Core\Database\Query\SelectInterface;
  7. use Drupal\Core\Entity\EntityManagerInterface;
  8. use Drupal\Core\Extension\ModuleHandler;
  9. use Drupal\Core\State\StateInterface;
  10. use Drupal\migrate\Plugin\MigrationInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Drupal\Core\Locale\CountryManagerInterface;
  13. use libphonenumber\PhoneNumber;
  14. use libphonenumber\PhoneNumberUtil;
  15. use libphonenumber\PhoneNumberFormat;
  16. /**
  17. * Drupal 7 node source from database.
  18. *
  19. * @MigrateSource(
  20. * id = "d7_user_profile_customer",
  21. * source_module = "profile2"
  22. * )
  23. */
  24. class D7UserProfileCustomer extends FieldableEntity {
  25. /**
  26. * Phone Number util.
  27. *
  28. * @var \libphonenumber\PhoneNumberUtil
  29. */
  30. public $phoneUtils;
  31. /**
  32. * Country Manager service.
  33. *
  34. * @var \Drupal\Core\Locale\CountryManagerInterface
  35. */
  36. public $countryManager;
  37. /**
  38. * The module handler.
  39. *
  40. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  41. */
  42. protected $moduleHandler;
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, CountryManagerInterface $country_manager) {
  47. parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
  48. $this->moduleHandler = $module_handler;
  49. $this->phoneUtils = PhoneNumberUtil::getInstance();
  50. $this->countryManager = $country_manager;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
  56. return new static(
  57. $configuration,
  58. $plugin_id,
  59. $plugin_definition,
  60. $migration,
  61. $container->get('state'),
  62. $container->get('entity.manager'),
  63. $container->get('module_handler'),
  64. $container->get('country_manager')
  65. );
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function query() {
  71. // Select node in its last revision.
  72. $query = $this->select('uc_orders', 'uco')
  73. ->fields('uco', [
  74. 'uid',
  75. // 'order_id',
  76. // 'billing_first_name',
  77. // 'billing_last_name',
  78. // 'billing_phone',
  79. // 'billing_company',
  80. // 'billing_street1',
  81. // 'billing_street2',
  82. // 'billing_city',
  83. // 'billing_zone',
  84. // 'billing_postal_code',
  85. // 'billing_country'
  86. ])
  87. ->groupBy('uco.uid')
  88. // ->groupBy('uco.order_id')
  89. // ->groupBy('uco.billing_first_name')
  90. // ->groupBy('uco.billing_last_name')
  91. // ->groupBy('uco.billing_phone')
  92. // ->groupBy('uco.billing_company')
  93. // ->groupBy('uco.billing_street1')
  94. // ->groupBy('uco.billing_street2')
  95. // ->groupBy('uco.billing_city')
  96. // ->groupBy('uco.billing_zone')
  97. // ->groupBy('uco.billing_postal_code')
  98. // ->groupBy('uco.billing_country')
  99. ->condition('order_status', 'completed')
  100. ->orderBy('modified', 'DESC');
  101. $query->innerJoin('users', 'u', 'uco.uid = u.uid');
  102. return $query;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function prepareRow(Row $row) {
  108. // $order_id = $row->getSourceProperty('order_id');
  109. $uid = $row->getSourceProperty('uid');
  110. drush_print("\n".'-- '.$uid);
  111. $query = $this->select('uc_orders', 'uco')
  112. ->fields('uco', [
  113. 'uid',
  114. 'order_id',
  115. 'billing_first_name',
  116. 'billing_last_name',
  117. 'billing_phone',
  118. 'billing_company',
  119. 'billing_street1',
  120. 'billing_street2',
  121. 'billing_city',
  122. 'billing_zone',
  123. 'billing_postal_code',
  124. 'billing_country',
  125. 'modified'
  126. ])
  127. ->condition('order_status', 'completed')
  128. ->condition('uco.uid', $uid)
  129. ->orderBy('modified', 'ASC');
  130. $query->innerJoin('users', 'u', 'uco.uid = u.uid');
  131. $query->fields('u',['mail']);
  132. // TODO filter by user active
  133. $user_orders = $query->execute()->fetchAll();
  134. $fields = [];
  135. foreach ($user_orders as $order) {
  136. foreach ($order as $field_name => $field){
  137. if ($field !== '') {
  138. $fields[$field_name] = $field;
  139. }
  140. }
  141. }
  142. if (count($user_orders) > 1){
  143. drush_print(print_r($user_orders, true));
  144. drush_print(print_r($fields, true));
  145. }
  146. foreach ($fields as $field_name => $field){
  147. $row->setSourceProperty($field_name, $field);
  148. }
  149. // // convert phone field with libphonenumber
  150. // $phone = $row->getSourceProperty('billing_phone');
  151. // if(isset($phone[0]['number']) && !empty($phone[0]['number'])){
  152. // $national_number = $phone[0]['number'];
  153. // $region = strtoupper($phone[0]['country_codes']);
  154. // // isValidRegionCode($regionCode)
  155. // if($this->phoneUtils->isPossibleNumber($national_number, $region)){
  156. // $number = $this->phoneUtils->parse($national_number, $region);
  157. // $row->setSourceProperty('billing_phone', $this->phoneUtils->format($number, PhoneNumberFormat::E164));
  158. // }else{
  159. // // add bad phone number to memo field
  160. // $memo .= "#migration : invalid phone number: ".$national_number.' region: '.$region."\n";
  161. // drush_print('WARNING: phone number invalide; number: '.$national_number.' region: '.$region);
  162. // }
  163. // }
  164. // country_zone
  165. $query = $this->select('uc_countries', 'ucc')
  166. ->condition('ucc.country_id', $row->getSourceProperty('billing_country'))
  167. ->fields('ucc', [
  168. 'country_id',
  169. 'country_name',
  170. 'country_iso_code_2',
  171. 'country_iso_code_3',]);
  172. $country = array_shift($query->execute()->fetchAll());
  173. drush_print(print_r($country, true));
  174. $row->setSourceProperty('billing_country', $country['country_iso_code_2']);
  175. // billing_zone
  176. $query = $this->select('uc_zones', 'ucz')
  177. ->condition('ucz.zone_id', $row->getSourceProperty('billing_zone'))
  178. ->fields('ucz', [
  179. 'zone_code',
  180. 'zone_name',
  181. ]);
  182. $zone = array_shift($query->execute()->fetchAll());
  183. drush_print(print_r($zone, true));
  184. $row->setSourceProperty('billing_zone', $zone['zone_code']);
  185. // get TVA from old adhérent profil
  186. $query = $this->select('profile', 'p')
  187. ->fields('p', [
  188. 'pid',
  189. 'type',
  190. 'uid',
  191. 'label',
  192. 'created',
  193. 'changed',
  194. ])
  195. ->condition('uid', $uid)
  196. ->condition('type', 'adherent')
  197. ->orderBy('changed');
  198. $profils = $query->execute()->fetchAll();
  199. if(count($profils)){
  200. $profil = array_shift($profils);
  201. // Get Field API field values.
  202. // foreach ($this->getFields('profile2', $profil['type']) as $field_name => $field) {
  203. // // drush_print($field_name);
  204. // $value = $this->getFieldValues('profile2', $field_name, $profil['pid']);
  205. // $row->setSourceProperty($field_name, $value);
  206. // }
  207. $vat = $this->getFieldValues('profile2', 'field_vat_number_intra_ce', $profil['pid']);
  208. $row->setSourceProperty('field_vat_number_intra_ce', $vat);
  209. }
  210. // record migration errors in field_memo
  211. if(isset($memo)){
  212. $field_memo = $row->getSourceProperty('field_memo');
  213. $field_memo[0]['value'] .= "\n".$memo;
  214. $row->setSourceProperty('field_memo', $field_memo);
  215. }
  216. return parent::prepareRow($row);
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function fields() {
  222. $fields = [
  223. 'order_id' => $this->t('Order ID'),
  224. 'uid' => $this->t('User id'),
  225. 'billing_first_name' => $this->t('First name'),
  226. 'billing_last_name' => $this->t('Last name'),
  227. 'billing_phone' => $this->t('Phone'),
  228. 'billing_company' => $this->t('Company'),
  229. 'billing_street1' => $this->t('Street 1'),
  230. 'billing_street2' => $this->t('Street 2'),
  231. 'billing_city' => $this->t('City'),
  232. 'billing_zone' => $this->t('Zone'),
  233. 'billing_postal_code' => $this->t('Postal code'),
  234. 'billing_country' => $this->t('Country'),
  235. ];
  236. return $fields;
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function getIds() {
  242. $ids['uid']['type'] = 'integer';
  243. $ids['uid']['alias'] = 'p';
  244. return $ids;
  245. }
  246. }