first success maintenance plan
This commit is contained in:
@ -3,6 +3,20 @@ 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__':
|
||||
|
||||
@ -20,6 +34,7 @@ if __name__ == '__main__':
|
||||
#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
|
||||
@ -29,6 +44,9 @@ if __name__ == '__main__':
|
||||
|
||||
# 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'])
|
||||
|
||||
print(df_sc)
|
||||
print(df_mp)
|
||||
@ -48,6 +66,34 @@ if __name__ == '__main__':
|
||||
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)
|
||||
|
||||
merged_df_mp['DoesAutoGenerateWorkOrders'] = True
|
||||
|
||||
#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)
|
||||
|
Reference in New Issue
Block a user