several fixes; Location Van, AssociatedLocation

This commit is contained in:
Rene Kaßeböhmer
2025-05-02 14:23:27 +02:00
parent 3c824e5d4b
commit 4ca8cc06c7
10 changed files with 78 additions and 39 deletions

View File

@ -42,7 +42,7 @@ reindex_columns_product2 = ['Id','Main_Product_Group__c','Family','MaterialType_
reindex_columns_ibr = ['Id', 'InstalledBaseLocation__c', 'Role__c', 'ValidFrom__c', 'ValidTo__c', 'Account__c']
reindex_columns_pricelist = ['Id', 'Name', 'Brand__r.Name', 'Country__c']
reindex_columns_pricelistitem = ['Id', 'Article__r.Name', 'Article__r.EANCode__c', 'Price__c', 'PriceUnit__c', 'Pricelist__c', 'ValidFrom__c', 'ValidTo__c', 'Pricelist__r.Brand__r.Name', 'Pricelist__r.Country__c']
reindex_columns_resourceassignment = ['Id', 'ValidTo__c', 'ValidFrom__c', 'Country__c', 'City__c', 'PostalCode__c', 'District__c', 'Street__c', 'HouseNo__c', 'Extension__c', 'FlatNo__c', 'Floor__c', 'GeoY__c', 'GeoX__c', 'Resource__c', 'Resource__r.Employee__r.Name', 'Stock__c']
reindex_columns_resourceassignment = ['Id', 'ValidTo__c', 'ValidFrom__c', 'Country__c', 'City__c', 'PostalCode__c', 'District__c', 'Street__c', 'HouseNo__c', 'Extension__c', 'FlatNo__c', 'Floor__c', 'GeoY__c', 'GeoX__c', 'Resource__c', 'Resource__r.Employee__r.Name', 'Stock__c', 'Stock__r.ID2__c']
reindex_columns_address_iot = ['Id', 'Country', 'CountryCode', 'Street', 'City', 'ParentId', 'PostalCode']
reindex_columns_location_iot = ['Id', 'Name']
@ -223,7 +223,7 @@ merged_df_ib = merged_df_ib.drop('Id_x', axis=1)
merged_df_ib = merged_df_ib.drop('ProductUnitClass__c', axis=1)
merged_df_ib = merged_df_ib.drop('ProductUnitType__c', axis=1)
merged_df_ib.columns = ['Name', 'FSL_1st_Ignition_Date__c', 'InstallDate', 'Kind_of_Energy__c', 'Product2.Product_Code__c', 'SerialNumber', 'Serialnumber_Exception__c', 'Location.ExternalReference']
merged_df_ib.columns = ['Name', 'FSL_1st_Ignition_Date__c', 'InstallDate', 'Kind_of_Energy__c', 'Product2.Product_Code__c', 'SerialNumber', 'Serialnumber_Exception__c', 'Location.PKey__c']
#merged_df_ib = merged_df_ib.drop('Main_Product_Group__c', axis=1)
# assign Main_Product_Group__c based on product2 records
merged_df_ib = pd.merge(merged_df_ib,
@ -241,8 +241,11 @@ merged_df_ib = merged_df_ib.replace({'Kind_of_Energy__c': {'4': '3', '5': '3'}})
## 5. SCInstalledBaseRole__c.csv
print(df_ibr)
print(child_df)
df_ibr = pd.merge(df_ibr,
child_df[['Id', 'ExternalReference']],
child_df[['Id', 'PKey__c']],
left_on='InstalledBaseLocation__c',
right_on='Id',
how='left')
@ -253,9 +256,9 @@ df_ibr = df_ibr.drop('Id_x', axis=1)
df_ibr = df_ibr.drop('Id_y', axis=1)
df_ibr = df_ibr.drop('InstalledBaseLocation__c', axis=1)
print(df_ibr.columns)
print(df_ibr)
df_ibr.columns = ['Type', 'ActiveFrom', 'ActiveTo', 'ParentRecordId', 'Location.ExternalReference']
df_ibr.columns = ['Type', 'ActiveFrom', 'ActiveTo', 'ParentRecordId', 'Location.PKey__c']
#remove kind_of_energy__c and kind_of_installation if field dependency to main product group is not correct
# Create the mapping dictionary
@ -345,7 +348,7 @@ df_pricelistitem.columns = columns_pricebookentry
##--------------------------------------------------------------------------##
# Create van locations from resource assignments
van_columns = ['Id', 'City__c', 'Country__c', 'Street__c', 'HouseNo__c', 'PostalCode__c', 'GeoY__c', 'GeoX__c', 'Resource__r.Employee__r.Name']
van_columns = ['Id', 'City__c', 'Country__c', 'Street__c', 'HouseNo__c', 'PostalCode__c', 'GeoY__c', 'GeoX__c', 'Resource__r.Employee__r.Name', 'Stock__r.ID2__c']
van_address_columns = ['City__c', 'Country__c', 'Street__c', 'HouseNo__c', 'PostalCode__c', 'GeoY__c', 'GeoX__c', 'ExternalReference']
van_df = df_resourceassignment[van_columns].copy()
@ -362,6 +365,20 @@ van_df['ExternalReference'] = (
van_df['Country__c'].astype(str)
)
# Process Stock__r.ID2__c field
def extract_last_4_digits(value):
if pd.isna(value) or not isinstance(value, str):
return '1000'
try:
last_4 = value[-4:]
if last_4.isdigit():
return last_4
return '1000'
except:
return '1000'
van_df['Stock__r.ID2__c'] = van_df['Stock__r.ID2__c'].apply(extract_last_4_digits)
# Add van records to child_df
if not van_df.empty:
van_records = pd.DataFrame({
@ -371,10 +388,13 @@ if not van_df.empty:
'DuplicateCheck__c': 'false',
'IsInventoryLocation': 'true',
'IsMobile': 'true',
'LocationType': 'Van'
'LocationType': 'Van',
'Location_Number__c': van_df['Stock__r.ID2__c']
})
child_df = pd.concat([child_df, van_records], ignore_index=True)
child_df = child_df.drop('Id', axis=1)
# Address and Parent Location for vans
van_address_df = van_df[van_address_columns].copy()
van_parenlocation_df = van_df[['City__c', 'Country__c', 'Street__c', 'PostalCode__c']].copy()
@ -383,10 +403,7 @@ van_parenlocation_df = van_df[['City__c', 'Country__c', 'Street__c', 'PostalCode
van_address_df['Country'] = van_address_df['Country__c'].map(country_mapping)
van_address_df['Parent.Name'] = van_address_df['ExternalReference']
van_address_df = van_address_df.drop('HouseNo__c', axis=1)
print(van_address_df)
van_address_df.columns = ['City', 'CountryCode', 'Street', 'PostalCode', 'Latitude', 'Longitude', 'PKey__c', 'Country', 'Parent.Name']
print(van_address_df)
# Only add addresses that don't already exist
new_addresses = van_address_df[~van_address_df['PKey__c'].isin(address_df['PKey__c'])]
@ -415,7 +432,7 @@ address_df.to_csv('../4_upsert_address_and_parent_location/Address.csv', index=F
parent_df.to_csv('../4_upsert_address_and_parent_location/Location.csv', index=False)
child_df.to_csv('../6_upsert_child_location/Location.csv', index=False)
merged_df_ib.to_csv('../8_upsert_assets/Asset.csv', index=False)
df_ibr.to_csv('../11_upsert_associated_location/AssociatedLocation.csv', index=False)
df_ibr.to_csv('../11_upsert_associated_location/AssociatedLocation_beforetransform.csv', index=False)
df_pricelist.to_csv('../12_insert_pricebook2_and_pricebookentries/Pricebook2.csv', index=False)
df_pricelistitem.to_csv('../12_insert_pricebook2_and_pricebookentries/PricebookEntry.csv', index=False)
merged_df_location_iot.to_csv('../3_update_address_and_location_data_for_migration/Location.csv', index=False)