0% found this document useful (0 votes)
7 views

AA

Uploaded by

Aditya Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

AA

Uploaded by

Aditya Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

import csv

# Function to read data from a CSV file


def read_csv(file_name):
with open(file_name, mode='r') as file:
reader = csv.DictReader(file)
return list(reader)

# Function to get customer details by customer_id


def get_customer(customer_id, customers):
for customer in customers:
if customer['Customer ID'] == customer_id:
return customer
return None

# Function to get orders by customer_id


def get_orders(customer_id, orders):
return [order for order in orders if order['CustomerID'] == customer_id]

# Function to get payments by order_id


def get_payments(order_id, payments):
return [payment for payment in payments if payment['OrderID'] == order_id]

# Function to display a customer's order and payment history


def view_customer_order_payment_history(customer_id):
customers = read_csv("D:\\Computer sc\\customers.csv")
orders = read_csv("D:\\Computer sc\\orders.csv")
payments = read_csv("D:\\Computer sc\\payments.csv")

customer = get_customer(customer_id, customers)


if not customer:
print(f"No customer found with ID: {customer_id}")
return

print(f"Customer: {customer['Name']}")
customer_orders = get_orders(customer_id, orders)

if not customer_orders:
print("This customer has no orders.")
return

print(f"\nOrder and Payment History for Customer ID: {customer_id}")


for order in customer_orders:
print(f"\nOrder ID: {order['OrderID']}, Total: ${float(order['Price']) *
float(order['Quantity']):.2f}")
order_payments = get_payments(order['OrderID'], payments)
if order_payments:
print("Payments:")
for payment in order_payments:
print(f" - Payment ID: {payment['PaymentID']}, Amount:
${payment['Amount']}, Date: {payment['PaymentDate']}")
else:
print("No payments made for this order.")
# Function to determine the payment status of an order
def get_payment_status(order, payments):
total_order_amount = float(order['Price']) * float(order['Quantity'])
payments_for_order = get_payments(order['OrderID'], payments)

if not payments_for_order:
return "Unpaid"

total_paid = sum(float(payment['Amount']) for payment in payments_for_order)

if total_paid == total_order_amount:
return "Fully Paid"
elif total_paid > 0:
return "Partially Paid"
else:
return "Unpaid"

# Function to view orders with payment status


def view_orders_with_payment_status():
orders = read_csv("D:\\Computer sc\\orders.csv")
payments = read_csv("D:\\Computer sc\\payments.csv")

print("Order ID | Customer ID | Total Amount | Payment Status")


print("-------------------------------------------------------")

for order in orders:


payment_status = get_payment_status(order, payments)
total_amount = float(order['Price']) * float(order['Quantity'])
print(f"{order['OrderID']} | {order['CustomerID']} | ${total_amount:.2f}
| {payment_status}")

# Function to generate an invoice for a specific customer


def generate_invoice(customer_id):
customers = read_csv("D:\\Computer sc\\customers.csv")
orders = read_csv("D:\\Computer sc\\orders.csv")
payments = read_csv("D:\\Computer sc\\payments.csv")

customer = get_customer(customer_id, customers)

if not customer:
print("Customer not found!")
return

customer_orders = get_orders(customer_id, orders)

if not customer_orders:
print("No orders found for this customer!")
return

print(f"Invoice for Customer: {customer['Name']}")


print(f"Email: {customer['Email']}")
print(f"Address: {customer['Address']}\n")

print("Order ID | Total Amount | Payment Status")


print("------------------------------------------")

for order in customer_orders:


total_order_amount = float(order['Price']) * float(order['Quantity'])
payments_for_order = get_payments(order['OrderID'], payments)
total_paid = sum(float(payment['Amount']) for payment in
payments_for_order)

if total_paid == total_order_amount:
payment_status = "Fully Paid"
elif total_paid > 0:
payment_status = "Partially Paid"
else:
payment_status = "Unpaid"

print(f"{order['OrderID']} | ${total_order_amount:.2f} |
{payment_status}")

# Function to calculate total payments for each customer


def total_payments_per_customer():
customers = read_csv("D:\\Computer sc\\customers.csv")
payments = read_csv("D:\\Computer sc\\payments.csv")

total_payments = {customer['Customer ID']: {'name': customer['Name'],


'total': 0.0} for customer in customers}

for payment in payments:


customer_id = payment['CustomerID']
amount = float(payment['Amount'])

if customer_id in total_payments:
total_payments[customer_id]['total'] += amount

print("Total Payments Received for Each Customer")


print("-------------------------------------------")
print("Customer ID | Customer Name | Total Payments")
print("-------------------------------------------")

for customer_id, info in total_payments.items():


print(f"{customer_id} | {info['name']} |
${info['total']:.2f}")

# Function to track unpaid orders


def track_unpaid_orders():
orders = read_csv("D:\\Computer sc\\orders.csv")
payments = read_csv("D:\\Computer sc\\payments.csv")

payments_by_order_id = {}

for payment in payments:


order_id = payment['OrderID']
amount = float(payment['Amount'])

if order_id not in payments_by_order_id:


payments_by_order_id[order_id] = 0.0

payments_by_order_id[order_id] += amount

print("Unpaid Orders")
print("--------------")
print("Order ID | Customer ID | Amount Due | Total Paid | Unpaid Amount")
print("-----------------------------------------------------------------")

for order in orders:


order_id = order['OrderID']
customer_id = order['CustomerID']
amount_due = float(order['Price']) * float(order['Quantity'])
total_paid = payments_by_order_id.get(order_id, 0.0)
unpaid_amount = amount_due - total_paid

if unpaid_amount > 0:
print(f"{order_id} | {customer_id} | ${amount_due:.2f}
| ${total_paid:.2f} | ${unpaid_amount:.2f}")

# Function to calculate Customer Lifetime Value (CLV)


def calculate_clv():
customers = read_csv("D:\\Computer sc\\customers.csv")
orders = read_csv("D:\\Computer sc\\orders.csv")
payments = read_csv("D:\\Computer sc\\payments.csv")

total_order_amount_by_customer = {}

for order in orders:


customer_id = order['CustomerID']
amount_due = float(order['Price']) * float(order['Quantity'])

if customer_id not in total_order_amount_by_customer:


total_order_amount_by_customer[customer_id] = 0.0

total_order_amount_by_customer[customer_id] += amount_due

total_payment_by_customer = {}

for payment in payments:


customer_id = payment['CustomerID']
amount = float(payment['Amount'])

if customer_id not in total_payment_by_customer:


total_payment_by_customer[customer_id] = 0.0

total_payment_by_customer[customer_id] += amount

print("Customer Lifetime Value (CLV)")


print("-------------------------------")
print("Customer ID | Total Revenue | Total Payments | CLV")
print("-------------------------------------------------------")

for customer in customers:


customer_id = customer['Customer ID']
total_revenue = total_order_amount_by_customer.get(customer_id, 0.0)
total_payments = total_payment_by_customer.get(customer_id, 0.0)
clv = total_revenue - total_payments

print(f"{customer_id} | ${total_revenue:.2f} |
${total_payments:.2f} | ${clv:.2f}")

from collections import Counter

# Function to calculate product popularity


def calculate_product_popularity():
orders = [] # List to hold order data

# Read orders from the CSV file


with open("D:\\Computer sc\\orders.csv", mode='r', newline='') as file:
reader = csv.DictReader(file)
for row in reader:
orders.append(row)

# Extract product names from orders


products = [order['Product'] for order in orders]

# Count occurrences of each product


product_counts = Counter(products)

# Sort products by popularity (count)


sorted_products = product_counts.most_common()

# Print product popularity


print("\nProduct Popularity:")
for product, count in sorted_products:
print(f"{product}: {count} orders")

# Example call (uncomment to run in your environment)


# calculate_product_popularity()

# Main function to demonstrate the functionalities


def main():
while True:
print("\n--- Customer Management System ---")
print("1. View Customer Order and Payment History")
print("2. View Orders with Payment Status")
print("3. Generate Invoice for a Customer")
print("4. Total Payments Received per Customer")
print("5. Track Unpaid Orders")
print("6. Calculate Customer Lifetime Value (CLV)")
print("7. Calculate Product Popularity") # Adjusted option number
print("8. Exit")

choice = input("Enter your choice: ")


if choice == '1':
customer_id = input("Enter Customer ID: ")
view_customer_order_payment_history(customer_id)
elif choice == '2':
view_orders_with_payment_status()
elif choice == '3':
customer_id = input("Enter Customer ID: ")
generate_invoice(customer_id)
elif choice == '4':
total_payments_per_customer()
elif choice == '5':
track_unpaid_orders()
elif choice == '6':
calculate_clv()
elif choice == '7': # Adjusted option number
calculate_product_popularity() # Call the product popularity
function
elif choice == '8':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please try again.")

main()

You might also like