import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import PatternFill
from openpyxl.utils import range_boundaries
from datetime import datetime

# Load MomentsPay_Bank_new.xlsx
moments_pay_bank_df = pd.read_excel('Abankstatement10-06.xlsx')

# Load Transaction details as on 14.12.2023
transaction_details_df = pd.read_excel('AHISmanual10-06.xlsx')

# Convert relevant columns to a common data type (string)
transaction_details_df['ChequeorCardNo'] = transaction_details_df['ChequeorCardNo'].astype(str).replace(r'\.0$', '', regex=True).replace(r"'", '', regex=True).apply(lambda x: x.zfill(4))
transaction_details_df['Amount'] = transaction_details_df['Amount'].astype(str).replace(r'\.0$', '', regex=True)

moments_pay_bank_df['CARD NUMBER'] = moments_pay_bank_df['CARD NUMBER'].astype(str).replace(r'\.0$', '', regex=True)
moments_pay_bank_df['TRANSACTION AMT'] = moments_pay_bank_df['TRANSACTION AMT'].astype(str).replace(r'\.0$', '', regex=True)
moments_pay_bank_df['AUTH CODE'] = moments_pay_bank_df['AUTH CODE'].astype(str).replace(r'\.0$', '', regex=True)
print(moments_pay_bank_df['AUTH CODE'])

moments_pay_bank_df['Last4_CARDNBR'] = moments_pay_bank_df['CARD NUMBER'].apply(lambda x: x if '@' in x else x[-4:])
transaction_details_df['Last4_card_num'] = transaction_details_df['ChequeorCardNo'].apply(lambda x: x if '@' in x else x[-4:])

# Specify the mapping between fields excluding 'processing_id' and 'transaction_id'
field_mapping = {'Last4_card_num': 'Last4_CARDNBR','Amount':'TRANSACTION AMT'}

# Remove time component from 'Date' and convert to datetime format
#transaction_details_df['Doc Date'] = transaction_details_df['Doc Date'].str.split().str[0]
#transaction_details_df['Doc Date'] = pd.to_datetime(transaction_details_df['Doc Date'], format='%d/%m/%Y')

# Apply the location renaming and categorization
unit_mapping = {
    'DATA CENTER':'DATA CENTER',
    'NUNGAMBAKKAM':'NUNGAMBAKKAM',
    'ADYAR ANDERSON':'ADYAR ANDERSON',
    'CHROMEPET MAIN CENTRE':'CHROMEPET MAIN CENTRE',
    'ANDERSON VADAPALANI':'ANDERSON VADAPALANI',
    'GREAMS ROAD':'GREAMS ROAD',
    'NIRAM HOSPITAL':'NIRAM HOSPITAL',
    'NELLORE':'NELLORE',
    'VELLORE NALAM HOSPITAL':'VELLORE NALAM HOSPITAL',
    'ANDERSON KANCHIPURAM':'ANDERSON KANCHIPURAM',
    'JAYA EYE CARE CENTRE':'JAYA EYE CARE CENTRE',
    'TAMBARAM CC':'TAMBARAM CC',
    'DEEN ORTHO CENTER':'DEEN ORTHO CENTER',
    'NANGANALLUR CENTRE':'NANGANALLUR CENTRE',
    'DR P MUTHUKUMAR NEUROLOGY CLINIC VALASARAVAKKAM':'DR P MUTHUKUMAR NEUROLOGY CLINIC VALASARAVAKKAM',
    'HOME COLLECTION CENTRE':'HOME COLLECTION CENTRE',
    'THIRUMULLAIVOYIL':'THIRUMULLAIVOYIL',
    'ANDERSON DELHI NCR':'ANDERSON DELHI NCR',
    'PERAMBUR COLLECTION POINT':'PERAMBUR COLLECTION POINT',
    'TIRUPATHI CC':'TIRUPATHI CC',
    'CHILDRENS MEDICAL CENTRE':'CHILDRENS MEDICAL CENTRE',
    'SAIBABA COLONY, COIMBATORE':'SAIBABA COLONY, COIMBATORE',
    'I CARE CANCER CLINIC':'I CARE CANCER CLINIC',
    'CHENNAI B2B CENTRE':'CHENNAI B2B CENTRE',
    'KARNATAKA B2B':'KARNATAKA B2B',
    'HEMA CHILD CARE - TAMBARAM':'HEMA CHILD CARE - TAMBARAM',
    'WEST MAMBALAM ANDERSON':'WEST MAMBALAM ANDERSON',
    'GENETICS KILPAUK':'GENETICS KILPAU',
    'TRICHY ANDERSON':'TRICHY ANDERSON',
    'PDR ORTHOPAEDIC HOSPITAL':'PDR ORTHOPAEDIC HOSPITAL',
    'ANDERSON MADURAI':'ANDERSON MADURAI',
    'ANDERSON COIMBATORE':'ANDERSON COIMBATORE',
    'MUMBAI B2B CENTRE':'MUMBAI B2B CENTRE',
    'BANGALORE MAIN LAB - ANDERSON' : 'BANGALORE MAIN LAB - ANDERSON',
}

transaction_details_df['TenentName'] = transaction_details_df['TenentName'].replace(unit_mapping)

#transaction_details_df = transaction_details_df[transaction_details_df['ChequeorCardNo'].str.startswith('*')]

# Merge dataframes on the specified fields
merged_df = pd.merge(transaction_details_df, moments_pay_bank_df[['AUTH CODE'] + list(field_mapping.values())],
                     left_on=list(field_mapping.keys()),
                     right_on=list(field_mapping.values()),
                     how='left')
unmerged_df = pd.merge(transaction_details_df, moments_pay_bank_df[list(field_mapping.values())],
                       left_on=list(field_mapping.keys()),
                       right_on=list(field_mapping.values()),
                       how='outer', indicator=True)

print(merged_df.columns)
equal_records = merged_df[~merged_df['Last4_CARDNBR'].isnull()]
equal_records.insert(0, 'S.No.', range(1, len(equal_records) + 1))
equal_records['manual matched'] = 'YES'
print(equal_records.columns)
#equal_records['txn_identifier'] = equal_records.apply(
#    lambda row: moments_pay_bank_df[
#        (moments_pay_bank_df['Last4_CARDNBR'] == row['Last4_card_num']) &
#        (moments_pay_bank_df['TRANSACTION AMT'] == row['Amount'])
#    ]
#    .reset_index(drop=True)['AUTH CODE'].values[0] + "@hdfccardmanual.com", 
#    axis=1
#)

equal_records['txn_identifier'] = equal_records.apply(
    lambda row: moments_pay_bank_df.loc[
        (moments_pay_bank_df['Last4_CARDNBR'] == row['Last4_card_num']) &
        (moments_pay_bank_df['TRANSACTION AMT'] == row['Amount']),
        'AUTH CODE'
    ].reset_index(drop=True).iloc[0] + "@icicicardmanual.com"
    if not moments_pay_bank_df.loc[
        (moments_pay_bank_df['Last4_CARDNBR'] == row['Last4_card_num']) &
        (moments_pay_bank_df['TRANSACTION AMT'] == row['Amount'])
    ].empty else None,
    axis=1
)

unequal_records = unmerged_df[unmerged_df['_merge'] == 'left_only']
unequal_records.insert(0, 'S.No.', range(1, len(unequal_records) + 1))
unequal_records['manual matched'] = 'NO'

# Function to create or overwrite an Excel sheet
def create_or_overwrite_sheet(sheet_name, data):
    with pd.ExcelWriter('Anderson-HISBANK10-06.xlsx', mode='a', engine='openpyxl', if_sheet_exists='replace') as writer:
        data.to_excel(writer, sheet_name=sheet_name, index=False)

# Generate sheet names
sheet_name_matched = 'M-HISBANK-Matched'
sheet_name_unmatched = 'M-HISBANK-Unmatched'
create_or_overwrite_sheet(sheet_name_matched, equal_records)
create_or_overwrite_sheet(sheet_name_unmatched, unequal_records)

wb = load_workbook('Anderson-HISBANK10-06.xlsx')
Summary_Sheet = wb['Summary']

File_amount = moments_pay_bank_df['TRANSACTION AMT'].astype(float).sum()
print(File_amount)

# Assuming TRANSACTION DATE contains '11/25/2024'
transaction_date = moments_pay_bank_df['TRANSACTION DATE'].mode()[0]

# Ensure the input format matches the actual data format
#transaction_date = datetime.strptime(transaction_date, "%m/%d/%Y")

# Format it to the desired string format
#formatted_date = transaction_date.strftime("%d-%m-%Y %H:%M:%S")
#print(formatted_date)

#settled_date = moments_pay_bank_df['POST DATE'].mode()[0]
#Summary_Sheet['A12'] = settled_date

# Update summary sheet with total transaction details
num_rows = len(transaction_details_df)
Summary_Sheet['L161'] = num_rows
Summary_Sheet['L4'] = num_rows

total_amount = transaction_details_df['Amount'].astype(float).sum()
Summary_Sheet['M161'] = f"₹{total_amount:,.2f}"
Summary_Sheet['M4'] = f"₹{total_amount:,.2f}"

# Update summary sheet with matched transaction details
HIS_Matched_df = pd.read_excel('Anderson-HISBANK10-06.xlsx', sheet_name='M-HISBANK-Matched')
num_rows = len(HIS_Matched_df)
Summary_Sheet['N161'] = num_rows
Summary_Sheet['N4'] = num_rows

total_amount = HIS_Matched_df['Amount'].astype(float).sum()
Summary_Sheet['O161'] = f"₹{total_amount:,.2f}"
Summary_Sheet['O4'] = f"₹{total_amount:,.2f}"

# Update summary sheet with unmatched transaction details
HIS_UnMatched_df = pd.read_excel('Anderson-HISBANK10-06.xlsx', sheet_name='M-HISBANK-Unmatched')
num_rows = len(HIS_UnMatched_df)
Summary_Sheet['P161'] = num_rows
Summary_Sheet['P4'] = num_rows

total_amount = HIS_UnMatched_df['Amount'].astype(float).sum()
Summary_Sheet['Q161'] = f"₹{total_amount:,.2f}"
Summary_Sheet['Q4'] = f"₹{total_amount:,.2f}"

# Define the color for the header
header_fill = PatternFill(fgColor='1274bd', fill_type='solid')

for sheet_name in ['M-HISBANK-Matched', 'M-HISBANK-Unmatched']:
    worksheet = wb[sheet_name]

    # Apply style to the header row
    for row in worksheet.iter_rows(min_row=1, max_row=1):
        for cell in row:
            cell.fill = header_fill

# Save the workbook
wb.save('Anderson-HISBANK10-06.xlsx')

# Combine the matched and unmatched DataFrames to calculate total counts and amounts
combined_df = pd.concat([HIS_Matched_df, HIS_UnMatched_df], ignore_index=True)

# Define the column name for unit/location and total amount
unit_location_col = 'TenentName'
total_amount_col = 'Amount'

# Calculate total count and total amount for each unique location
total_grouped = combined_df.groupby(unit_location_col).agg(
    total_count=pd.NamedAgg(column=unit_location_col, aggfunc='count'),
    total_amount=pd.NamedAgg(column=total_amount_col, aggfunc='sum')
).reset_index()

# Calculate matched count and matched total amount for each unique location
matched_grouped = HIS_Matched_df.groupby(unit_location_col).agg(
    matched_count=pd.NamedAgg(column=unit_location_col, aggfunc='count'),
    matched_amount=pd.NamedAgg(column=total_amount_col, aggfunc='sum')
).reset_index()

# Calculate unmatched count and unmatched total amount for each unique location
unmatched_grouped = HIS_UnMatched_df.groupby(unit_location_col).agg(
    unmatched_count=pd.NamedAgg(column=unit_location_col, aggfunc='count'),
    unmatched_amount=pd.NamedAgg(column=total_amount_col, aggfunc='sum')
).reset_index()


# Create a dataframe for all the locations from unit_mapping with default 0 values
location_df = pd.DataFrame(list(unit_mapping.items()), columns=[unit_location_col, 'dummy'])
location_df['total_count'] = 0
location_df['total_amount'] = 0.0
location_df['matched_count'] = 0
location_df['matched_amount'] = 0.0
location_df['unmatched_count'] = 0
location_df['unmatched_amount'] = 0.0

# Merge the grouped data with the location_df to ensure all locations from unit_mapping are included
summary_df = total_grouped.merge(matched_grouped, on=unit_location_col, how='left')\
                              .merge(unmatched_grouped, on=unit_location_col, how='left')

# Merge with location_df to ensure all unit_mapping locations are included, even with 0 values
summary_df = summary_df.merge(location_df, on=unit_location_col, how='right', suffixes=('_data', '_mapped'))

# Fill NaN values with 0 (in case there are locations that are only in unit_mapping and not in the data)
summary_df['total_count'] = summary_df['total_count_data'].fillna(0).astype(int)
summary_df['total_amount'] = summary_df['total_amount_data'].fillna(0.0)
summary_df['matched_count'] = summary_df['matched_count_data'].fillna(0).astype(int)
summary_df['matched_amount'] = summary_df['matched_amount_data'].fillna(0.0)
summary_df['unmatched_count'] = summary_df['unmatched_count_data'].fillna(0).astype(int)
summary_df['unmatched_amount'] = summary_df['unmatched_amount_data'].fillna(0.0)

# Drop unnecessary columns
summary_df.drop(columns=[col for col in summary_df.columns if col.endswith('_data')], inplace=True)

# Reorder the columns
summary_df = summary_df[['TenentName', 'total_count', 'total_amount', 'matched_count', 'matched_amount', 'unmatched_count', 'unmatched_amount']]

# Load the summary sheet where you want to update the values
summary_sheet_path = 'Anderson-HISBANK10-06.xlsx'
book = load_workbook(summary_sheet_path)
summary_sheet = book['Summary']

# Define the starting row and column for updating the Summary sheet
#start_row = 134  
#start_col = 11

#start_row = 5
#start_col = 11

# Function to find the top-left cell of a merged cell range
#def find_top_left_cell(merged_ranges, row, col):
#    for merged_range in merged_ranges:
#        min_col, min_row, max_col, max_row = range_boundaries(str(merged_range))
#        if min_row <= row <= max_row and min_col <= col <= max_col:
#            return min_row, min_col
#    return row, col

# Update the Summary sheet with the new summary data
#merged_ranges = summary_sheet.merged_cells.ranges
#for index, row in summary_df.iterrows():
#    row_num = rows_to_update[index] 
#    for col_num, value in enumerate(row):
        # Find the top-left cell if the cell is part of a merged range
#        r, c = find_top_left_cell(merged_ranges, start_row + index, start_col + col_num)
        # Write the value to the correct cell
#        summary_sheet.cell(row=r, column=c, value=value)

# Save the workbook without deleting existing sheets
#book.save(summary_sheet_path)

# Define the starting rows and columns for updating the Summary sheet
rows_to_update = [162, 5]  # List of rows where the data should be written
start_col = 11  # Column remains the same

# Function to find the top-left cell of a merged cell range
def find_top_left_cell(merged_ranges, row, col):
    for merged_range in merged_ranges:
        min_col, min_row, max_col, max_row = range_boundaries(str(merged_range))
        if min_row <= row <= max_row and min_col <= col <= max_col:
            return min_row, min_col
    return row, col

# Update the Summary sheet with the new summary data in both locations
merged_ranges = summary_sheet.merged_cells.ranges

for start_row in rows_to_update:  # Loop through both row positions
    for index, row in summary_df.iterrows():
        for col_num, value in enumerate(row):
            # Find the top-left cell if the cell is part of a merged range
            r, c = find_top_left_cell(merged_ranges, start_row + index, start_col + col_num)
            # Write the value to the correct cell
            summary_sheet.cell(row=r, column=c, value=value)

# Save the workbook without deleting existing sheets
book.save(summary_sheet_path)

print(summary_df)

