From 1806c09228a4b872aa1d7d2802e2415cc535ee8f Mon Sep 17 00:00:00 2001 From: bach Date: Thu, 22 Jul 2021 09:48:07 +0200 Subject: [PATCH] migrate user customer profil --- ...lus.migration.d7_user_profile_customer.yml | 43 +-- ...lus.migration.d7_user_profile_customer.yml | 98 ++++--- .../migrate/source/D7UserProfileCustomer.php | 273 ++++++++++++++++++ 3 files changed, 342 insertions(+), 72 deletions(-) create mode 100644 web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7UserProfileCustomer.php diff --git a/config/sync/migrate_plus.migration.d7_user_profile_customer.yml b/config/sync/migrate_plus.migration.d7_user_profile_customer.yml index 19d697d..05008c4 100644 --- a/config/sync/migrate_plus.migration.d7_user_profile_customer.yml +++ b/config/sync/migrate_plus.migration.d7_user_profile_customer.yml @@ -18,43 +18,26 @@ migration_tags: migration_group: d7_materio label: 'D7 User Customer Profile' source: - plugin: d7_user_profile + plugin: d7_user_profile_customer batch_size: 1000 - high_water_property: - name: changed - alias: p process: type: - - - plugin: skip_on_value - equals: true - source: type - method: row - value: - - contact_operationnel - - - plugin: static_map - source: type - map: - adherent: customer + plugin: default_value + default_value: customer uid: plugin: migration_lookup migration: d7_users source: uid - phone: field_private_phone - address: - plugin: iterator - source: field_adresse - process: - country_code: country - administrative_area: administrative_area - locality: locality - dependent_locality: dependent_locality - postal_code: postal_code - address_line1: thoroughfare - organization: organisation_name - given_name: first_name - family_name: last_name + phone: billing_phone + address/country_code: billing_country + address/administrative_area: billing_zone + address/locality: billing_city + address/postal_code: billing_postal_code + address/address_line1: billing_street1 + address/address_line2: billing_street2 + address/organization: billing_company + address/given_name: billing_first_name + address/family_name: billing_last_name tax_number: plugin: iterator source: field_vat_number_intra_ce diff --git a/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_user_profile_customer.yml b/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_user_profile_customer.yml index bb5e185..bbe67d7 100644 --- a/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_user_profile_customer.yml +++ b/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_user_profile_customer.yml @@ -8,12 +8,9 @@ migration_tags: - Materio source: - plugin: d7_user_profile + plugin: d7_user_profile_customer batch_size: 1000 - high_water_property: - name: changed - alias: p - + destination: plugin: entity:profile @@ -21,51 +18,68 @@ process: # id: pid # label: label - type: - - - plugin: skip_on_value - equals: true - source: type - method: row - value: - - contact_operationnel - - - plugin: static_map - source: type - map: - adherent: customer + type: + plugin: default_value + default_value: customer + # - + # plugin: skip_on_value + # equals: true + # source: type + # method: row + # value: + # - contact_operationnel + # - + # plugin: static_map + # source: type + # map: + # adherent: customer uid: plugin: migration_lookup migration: d7_users source: uid - phone: field_private_phone + phone: billing_phone - address: - plugin: iterator - source: field_adresse - process: - # langcode: - country_code: country - administrative_area: administrative_area - locality: locality - dependent_locality: dependent_locality - postal_code: postal_code - # sorting_code: - address_line1: thoroughfare - # address_line2: - organization: organisation_name - given_name: first_name - # additional_name: - family_name: last_name - # SOURCES - # sub_administrative_area - # premise - # sub_premise - # name_line - # data + + address/country_code: billing_country + address/administrative_area: billing_zone + address/locality: billing_city + # address/dependent_locality: dependent_locality + address/postal_code: billing_postal_code + # address/sorting_code: + address/address_line1: billing_street1 + address/address_line2: billing_street2 + address/organization: billing_company + address/given_name: billing_first_name + # address/additional_name: + address/family_name: billing_last_name + + # address: + # plugin: iterator + # source: field_adresse + # process: + # # langcode: + # country_code: country + # administrative_area: administrative_area + # locality: locality + # dependent_locality: dependent_locality + # postal_code: postal_code + # # sorting_code: + # address_line1: thoroughfare + # # address_line2: + # organization: organisation_name + # given_name: first_name + # # additional_name: + # family_name: last_name + + # # SOURCES + # # sub_administrative_area + # # premise + # # sub_premise + # # name_line + # # data # body: # plugin: iterator diff --git a/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7UserProfileCustomer.php b/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7UserProfileCustomer.php new file mode 100644 index 0000000..18767a8 --- /dev/null +++ b/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7UserProfileCustomer.php @@ -0,0 +1,273 @@ +moduleHandler = $module_handler; + $this->phoneUtils = PhoneNumberUtil::getInstance(); + $this->countryManager = $country_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + $container->get('state'), + $container->get('entity.manager'), + $container->get('module_handler'), + $container->get('country_manager') + ); + } + + /** + * {@inheritdoc} + */ + public function query() { + // Select node in its last revision. + $query = $this->select('uc_orders', 'uco') + ->fields('uco', [ + 'uid', + // 'order_id', + // 'billing_first_name', + // 'billing_last_name', + // 'billing_phone', + // 'billing_company', + // 'billing_street1', + // 'billing_street2', + // 'billing_city', + // 'billing_zone', + // 'billing_postal_code', + // 'billing_country' + ]) + ->groupBy('uco.uid') + // ->groupBy('uco.order_id') + // ->groupBy('uco.billing_first_name') + // ->groupBy('uco.billing_last_name') + // ->groupBy('uco.billing_phone') + // ->groupBy('uco.billing_company') + // ->groupBy('uco.billing_street1') + // ->groupBy('uco.billing_street2') + // ->groupBy('uco.billing_city') + // ->groupBy('uco.billing_zone') + // ->groupBy('uco.billing_postal_code') + // ->groupBy('uco.billing_country') + ->condition('order_status', 'completed') + ->orderBy('modified', 'DESC'); + $query->innerJoin('users', 'u', 'uco.uid = u.uid'); + + return $query; + } + + /** + * {@inheritdoc} + */ + public function prepareRow(Row $row) { + // $order_id = $row->getSourceProperty('order_id'); + $uid = $row->getSourceProperty('uid'); + drush_print("\n".'-- '.$uid); + $query = $this->select('uc_orders', 'uco') + ->fields('uco', [ + 'uid', + 'order_id', + 'billing_first_name', + 'billing_last_name', + 'billing_phone', + 'billing_company', + 'billing_street1', + 'billing_street2', + 'billing_city', + 'billing_zone', + 'billing_postal_code', + 'billing_country', + 'modified' + ]) + ->condition('order_status', 'completed') + ->condition('uco.uid', $uid) + ->orderBy('modified', 'ASC'); + $query->innerJoin('users', 'u', 'uco.uid = u.uid'); + $query->fields('u',['mail']); + + // TODO filter by user active + $user_orders = $query->execute()->fetchAll(); + + $fields = []; + foreach ($user_orders as $order) { + foreach ($order as $field_name => $field){ + if ($field !== '') { + $fields[$field_name] = $field; + } + } + } + + if (count($user_orders) > 1){ + drush_print(print_r($user_orders, true)); + drush_print(print_r($fields, true)); + } + + foreach ($fields as $field_name => $field){ + $row->setSourceProperty($field_name, $field); + } + // // convert phone field with libphonenumber + // $phone = $row->getSourceProperty('billing_phone'); + // if(isset($phone[0]['number']) && !empty($phone[0]['number'])){ + // $national_number = $phone[0]['number']; + // $region = strtoupper($phone[0]['country_codes']); + // // isValidRegionCode($regionCode) + // if($this->phoneUtils->isPossibleNumber($national_number, $region)){ + // $number = $this->phoneUtils->parse($national_number, $region); + // $row->setSourceProperty('billing_phone', $this->phoneUtils->format($number, PhoneNumberFormat::E164)); + // }else{ + // // add bad phone number to memo field + // $memo .= "#migration : invalid phone number: ".$national_number.' region: '.$region."\n"; + // drush_print('WARNING: phone number invalide; number: '.$national_number.' region: '.$region); + // } + // } + + // country_zone + $query = $this->select('uc_countries', 'ucc') + ->condition('ucc.country_id', $row->getSourceProperty('billing_country')) + ->fields('ucc', [ + 'country_id', + 'country_name', + 'country_iso_code_2', + 'country_iso_code_3',]); + $country = array_shift($query->execute()->fetchAll()); + drush_print(print_r($country, true)); + $row->setSourceProperty('billing_country', $country['country_iso_code_2']); + + + // billing_zone + $query = $this->select('uc_zones', 'ucz') + ->condition('ucz.zone_id', $row->getSourceProperty('billing_zone')) + ->fields('ucz', [ + 'zone_code', + 'zone_name', + ]); + $zone = array_shift($query->execute()->fetchAll()); + drush_print(print_r($zone, true)); + $row->setSourceProperty('billing_zone', $zone['zone_code']); + + + + // get TVA from old adhérent profil + $query = $this->select('profile', 'p') + ->fields('p', [ + 'pid', + 'type', + 'uid', + 'label', + 'created', + 'changed', + ]) + ->condition('uid', $uid) + ->condition('type', 'adherent') + ->orderBy('changed'); + $profils = $query->execute()->fetchAll(); + + if(count($profils)){ + $profil = array_shift($profils); + // Get Field API field values. + // foreach ($this->getFields('profile2', $profil['type']) as $field_name => $field) { + // // drush_print($field_name); + // $value = $this->getFieldValues('profile2', $field_name, $profil['pid']); + // $row->setSourceProperty($field_name, $value); + // } + $vat = $this->getFieldValues('profile2', 'field_vat_number_intra_ce', $profil['pid']); + $row->setSourceProperty('field_vat_number_intra_ce', $vat); + } + + + // record migration errors in field_memo + if(isset($memo)){ + $field_memo = $row->getSourceProperty('field_memo'); + $field_memo[0]['value'] .= "\n".$memo; + $row->setSourceProperty('field_memo', $field_memo); + } + + return parent::prepareRow($row); + } + + /** + * {@inheritdoc} + */ + public function fields() { + $fields = [ + 'order_id' => $this->t('Order ID'), + 'uid' => $this->t('User id'), + 'billing_first_name' => $this->t('First name'), + 'billing_last_name' => $this->t('Last name'), + 'billing_phone' => $this->t('Phone'), + 'billing_company' => $this->t('Company'), + 'billing_street1' => $this->t('Street 1'), + 'billing_street2' => $this->t('Street 2'), + 'billing_city' => $this->t('City'), + 'billing_zone' => $this->t('Zone'), + 'billing_postal_code' => $this->t('Postal code'), + 'billing_country' => $this->t('Country'), + ]; + return $fields; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['uid']['type'] = 'integer'; + $ids['uid']['alias'] = 'p'; + return $ids; + } + +}