savestate before github

This commit is contained in:
Rene Kaßeböhmer
2025-07-09 11:15:51 +02:00
parent 294c640096
commit b09b136ad7
14 changed files with 392 additions and 26 deletions

View File

@ -0,0 +1 @@
sf sfdmu run --sourceusername csvfile --targetusername rene.kasseboehmer@vaillant.de.devrene

View File

@ -0,0 +1,23 @@
{
"allOrNone": true,
"excludeIdsFromCSVFiles": true,
"objects": [
{
"query": "SELECT Id, PKey__c FROM ServiceContract WHERE TemplateId__c != null AND Pricebook2.Country__c = 'NL'",
"operation": "Readonly",
"externalId": "PKey__c",
"master": false
},{
"query": "SELECT Id, PKey__c FROM Asset WHERE Location.ParentLocation.VisitorAddress.CountryCode = 'NL'",
"operation": "Readonly",
"externalId": "PKey__c",
"master": false
},{
"query": "SELECT Id, ServiceContractId, AssetId FROM ContractLineItem",
"operation": "Insert",
"externalId": "Id",
"master": true,
"useSourceCSVFile": true
}
]
}

View File

@ -0,0 +1,119 @@
import pandas as pd
import argparse
from sys import path
path.append('../..')
from utils import bulk_insert_records
from datetime import datetime, timedelta
# Find last maintenance/order date for each ServiceContractId
def get_last_maintenance_date(service_contract_id):
# Try to find in LastMaintenanceInformation
lm_rows = df_lm[df_lm['Order__r.Contract__c'] == service_contract_id]
if not lm_rows.empty:
# Use the most recent Order__r.Closed__c
return lm_rows['Order__r.Closed__c'].max()
# If not found, try LastOrderInformation
lo_rows = df_lo[df_lo['Contract__c'] == service_contract_id]
if not lo_rows.empty:
return lo_rows['Closed__c'].max()
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Insert MaintenancePlan records via Bulk API')
parser.add_argument('--context', type=str, required=True,
help='Salesforce org context (e.g., "qa2", "prod")')
parser.add_argument('--csv', type=str, default='MaintenancePlan.csv',
help='CSV file to process (default: MaintenancePlan.csv)')
args = parser.parse_args()
#read_df_sc = pd.read_csv('../16_insert_servicecontract/successful_records.csv', header=0, keep_default_na=False, dtype=str)
read_df_mp = pd.read_csv('./MaintenancePlan_beforetransform.csv', header=0, keep_default_na=False, dtype=str)
#"sf__Id","sf__Created",PKey__c,BillingCountryCode,Term,EndDate,StartDate,AccountId,Service_Recipient__c,IoT_Registration_Status__c,Name,Pricebook2Id,TemplateId__c
#reindex_columns_sc = ['sf__Id','sf__Created','PKey__c', 'BillingCountryCode', 'Term', 'EndDate', 'StartDate', 'AccountId', 'Service_Recipient__c', 'IoT_Registration_Status__c', 'Name', 'Pricebook2Id', 'TemplateId__c']
#ServiceContract.PKey__c,StartDate,EndDate,AccountId,MaintenanceWindowEndDays,MaintenanceWindowStartDays,GenerationTimeframe,Frequency,GenerationTimeframeType,WorkTypeId
reindex_columns_mp = ['ServiceContract.PKey__c', 'StartDate', 'EndDate', 'AccountId', 'MaintenanceWindowEndDays', 'MaintenanceWindowStartDays', 'GenerationTimeframe', 'Frequency', 'GenerationTimeframeType', 'WorkTypeId']
reindex_columns_lastmaintenance = []
# Reindex the columns to match the desired format
#df_sc = read_df_sc.reindex(reindex_columns_sc, axis=1)
df_mp = read_df_mp.reindex(reindex_columns_mp, axis=1)
# Only load 'sf__Id' and 'PKey__c' from the ServiceContract CSV
df_sc = pd.read_csv('../16_insert_servicecontract/successful_records.csv', usecols=['sf__Id', 'PKey__c'])
# Load LastMaintenanceInformation and LastOrderInformation CSVs for NextSuggestedMaintenanceDate calculation
df_lm = pd.read_csv('../1_extract_data/results/LastMaintenanceInformation.csv', usecols=['Id', 'Order__r.Closed__c', 'Order__r.Contract__c'])
df_lo = pd.read_csv('../1_extract_data/results/LastOrderInformation.csv', usecols=['Id', 'Closed__c', 'Contract__c'])
#
# Merge df_mp with df_sc including Id based on PKey__c
merged_df_mp = pd.merge(df_mp,
df_sc[['sf__Id', 'PKey__c']],
left_on='ServiceContract.PKey__c',
right_on='PKey__c',
how='left')
# Rename only 'old_name' to 'new_name'
merged_df_mp = merged_df_mp.rename(columns={'sf__Id': 'ServiceContractId'})
merged_df_mp = merged_df_mp.drop('ServiceContract.PKey__c', axis=1)
merged_df_mp = merged_df_mp.drop('PKey__c', axis=1)
def get_next_suggested_maintenance_date(service_contract_id):
# Try to find in LastMaintenanceInformation
lm_rows = df_lm[df_lm['Order__r.Contract__c'] == service_contract_id]
if not lm_rows.empty:
last_date = lm_rows['Order__r.Closed__c'].max()
else:
# If not found, try LastOrderInformation
lo_rows = df_lo[df_lo['Contract__c'] == service_contract_id]
if not lo_rows.empty:
last_date = lo_rows['Closed__c'].max()
else:
last_date = None
if pd.notnull(last_date):
try:
dt = pd.to_datetime(last_date)
next_date = dt + pd.DateOffset(years=1)
return next_date.strftime('%Y-%m-%d')
except Exception:
pass
# If no date found or parsing fails, use today
next_date = datetime.today()
return next_date.strftime('%Y-%m-%d')
merged_df_mp['NextSuggestedMaintenanceDate'] = merged_df_mp['ServiceContractId'].apply(get_next_suggested_maintenance_date)
# Set DoesAutoGenerateWorkOrders to True only if a date was found (not defaulted to today)
def does_auto_generate_work_orders(service_contract_id):
# Try to find in LastMaintenanceInformation
lm_rows = df_lm[df_lm['Order__r.Contract__c'] == service_contract_id]
if not lm_rows.empty:
last_date = lm_rows['Order__r.Closed__c'].max()
else:
# If not found, try LastOrderInformation
lo_rows = df_lo[df_lo['Contract__c'] == service_contract_id]
if not lo_rows.empty:
last_date = lo_rows['Closed__c'].max()
else:
last_date = None
return pd.notnull(last_date)
merged_df_mp['DoesAutoGenerateWorkOrders'] = merged_df_mp['ServiceContractId'].apply(does_auto_generate_work_orders)
#transform values into int
merged_df_mp['MaintenanceWindowEndDays'] = pd.to_numeric(merged_df_mp['MaintenanceWindowEndDays'], errors='coerce').fillna(0).astype(int)
merged_df_mp['MaintenanceWindowStartDays'] = pd.to_numeric(merged_df_mp['MaintenanceWindowStartDays'], errors='coerce').fillna(0).astype(int)
merged_df_mp['GenerationTimeframe'] = pd.to_numeric(merged_df_mp['GenerationTimeframe'], errors='coerce').fillna(0).astype(int)
merged_df_mp['Frequency'] = pd.to_numeric(merged_df_mp['Frequency'], errors='coerce').fillna(0).astype(int)
#safe csv
merged_df_mp.to_csv('./MaintenancePlan.csv', index=False)
bulk_insert_records(args.context, 'MaintenancePlan', args.csv)