244 lines
9.8 KiB
Python
244 lines
9.8 KiB
Python
import mysql.connector
|
|
from database.connection import get_db_connection
|
|
import logging
|
|
|
|
# 공급사 추가
|
|
def add_supplier_to_db(supplier_data):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
sql = """
|
|
INSERT INTO suppliers (name_kr, name_en, business_registration_number, contact_number, address)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
"""
|
|
cursor.execute(sql, (
|
|
supplier_data.get('name_kr'),
|
|
supplier_data.get('name_en'),
|
|
supplier_data.get('business_registration_number'),
|
|
supplier_data.get('contact_number'),
|
|
supplier_data.get('address')
|
|
))
|
|
conn.commit()
|
|
return cursor.lastrowid
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error adding supplier: {err}")
|
|
return None
|
|
|
|
# 공급사 수정
|
|
def update_supplier_in_db(supplier_data):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
sql = """
|
|
UPDATE suppliers SET
|
|
name_kr = %s,
|
|
name_en = %s,
|
|
business_registration_number = %s,
|
|
contact_number = %s,
|
|
address = %s
|
|
WHERE id = %s
|
|
"""
|
|
cursor.execute(sql, (
|
|
supplier_data.get('name_kr'),
|
|
supplier_data.get('name_en'),
|
|
supplier_data.get('business_registration_number'),
|
|
supplier_data.get('contact_number'),
|
|
supplier_data.get('address'),
|
|
supplier_data.get('id')
|
|
))
|
|
conn.commit()
|
|
return True
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error updating supplier: {err}")
|
|
return False
|
|
|
|
# 공급사 삭제
|
|
def delete_supplier_from_db(supplier_id):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
cursor.execute("DELETE FROM purchase_activities WHERE supplier_id = %s", (supplier_id,))
|
|
cursor.execute("DELETE FROM supplier_contacts WHERE supplier_id = %s", (supplier_id,))
|
|
cursor.execute("DELETE FROM suppliers WHERE id = %s", (supplier_id,))
|
|
conn.commit()
|
|
return True
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error deleting supplier: {err}")
|
|
return False
|
|
|
|
# 모든 공급사 목록 조회
|
|
def get_all_suppliers_from_db():
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
cursor.execute("SELECT id, name_kr, name_en, business_registration_number, contact_number, address FROM suppliers")
|
|
return cursor.fetchall()
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error getting all suppliers: {err}")
|
|
return []
|
|
|
|
# 특정 공급사 상세 정보 조회 (담당자, 구매활동 포함)
|
|
def get_supplier_details_from_db(supplier_id):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
cursor.execute("SELECT * FROM suppliers WHERE id = %s", (supplier_id,))
|
|
supplier_data = cursor.fetchone()
|
|
if not supplier_data:
|
|
return None
|
|
|
|
cursor.execute("SELECT * FROM supplier_contacts WHERE supplier_id = %s", (supplier_id,))
|
|
contacts_data = cursor.fetchall()
|
|
|
|
cursor.execute("SELECT * FROM purchase_activities WHERE supplier_id = %s", (supplier_id,))
|
|
purchase_activities_data = cursor.fetchall()
|
|
|
|
return {
|
|
"supplier": supplier_data,
|
|
"contacts": contacts_data,
|
|
"purchaseActivities": purchase_activities_data
|
|
}
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error getting supplier details: {err}")
|
|
return None
|
|
|
|
# 공급사 담당자 추가
|
|
def add_supplier_contact_to_db(contact_data):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
sql = """
|
|
INSERT INTO supplier_contacts (supplier_id, name, email, phone_number, position, work_location)
|
|
VALUES (%s, %s, %s, %s, %s, %s)
|
|
"""
|
|
cursor.execute(sql, (
|
|
contact_data.get('supplier_id'),
|
|
contact_data.get('name'),
|
|
contact_data.get('email'),
|
|
contact_data.get('phone_number'),
|
|
contact_data.get('position'),
|
|
contact_data.get('work_location')
|
|
))
|
|
conn.commit()
|
|
return cursor.lastrowid
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error adding supplier contact: {err}")
|
|
return None
|
|
|
|
# 공급사 담당자 수정
|
|
def update_supplier_contact_in_db(contact_data):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
sql = """
|
|
UPDATE supplier_contacts SET
|
|
name = %s, email = %s, phone_number = %s, position = %s, work_location = %s
|
|
WHERE id = %s
|
|
"""
|
|
cursor.execute(sql, (
|
|
contact_data.get('name'),
|
|
contact_data.get('email'),
|
|
contact_data.get('phone_number'),
|
|
contact_data.get('position'),
|
|
contact_data.get('work_location'),
|
|
contact_data.get('id')
|
|
))
|
|
conn.commit()
|
|
return True
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error updating supplier contact: {err}")
|
|
return False
|
|
|
|
# 공급사 담당자 삭제
|
|
def delete_supplier_contact_from_db(contact_id):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
cursor.execute("DELETE FROM supplier_contacts WHERE id = %s", (contact_id,))
|
|
conn.commit()
|
|
return True
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error deleting supplier contact: {err}")
|
|
return False
|
|
|
|
# 공급사 담당자 ID로 공급사 ID 조회
|
|
def get_supplier_id_from_contact_id(contact_id):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
cursor.execute("SELECT supplier_id FROM supplier_contacts WHERE id = %s", (contact_id,))
|
|
result = cursor.fetchone()
|
|
return result['supplier_id'] if result else None
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error getting supplier ID from contact: {err}")
|
|
return None
|
|
|
|
# 구매 활동 추가
|
|
def add_purchase_activity_to_db(activity_data):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
sql = """
|
|
INSERT INTO purchase_activities (supplier_id, contact_id, activity_date, activity_type, memo)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
"""
|
|
cursor.execute(sql, (
|
|
activity_data.get('supplier_id'),
|
|
activity_data.get('contact_id'),
|
|
activity_data.get('activity_date'),
|
|
activity_data.get('activity_type'),
|
|
activity_data.get('memo')
|
|
))
|
|
conn.commit()
|
|
return cursor.lastrowid
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error adding purchase activity: {err}")
|
|
return None
|
|
|
|
# 구매 활동 수정
|
|
def update_purchase_activity_in_db(activity_data):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
sql = """
|
|
UPDATE purchase_activities SET
|
|
contact_id = %s, activity_date = %s, activity_type = %s, memo = %s
|
|
WHERE id = %s
|
|
"""
|
|
cursor.execute(sql, (
|
|
activity_data.get('contact_id'),
|
|
activity_data.get('activity_date'),
|
|
activity_data.get('activity_type'),
|
|
activity_data.get('memo'),
|
|
activity_data.get('id')
|
|
))
|
|
conn.commit()
|
|
return True
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error updating purchase activity: {err}")
|
|
return False
|
|
|
|
# 구매 활동 삭제
|
|
def delete_purchase_activity_from_db(activity_id):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
cursor.execute("DELETE FROM purchase_activities WHERE id = %s", (activity_id,))
|
|
conn.commit()
|
|
return True
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error deleting purchase activity: {err}")
|
|
return False
|
|
|
|
# 구매 활동 ID로 공급사 ID 조회
|
|
def get_supplier_id_from_purchase_activity_id(activity_id):
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor(dictionary=True) as cursor:
|
|
cursor.execute("SELECT supplier_id FROM purchase_activities WHERE id = %s", (activity_id,))
|
|
result = cursor.fetchone()
|
|
return result['supplier_id'] if result else None
|
|
except mysql.connector.Error as err:
|
|
logging.error(f"Error getting supplier ID from purchase activity: {err}")
|
|
return None
|