초기화
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
# supplier package marker
|
||||
from .db_manager import (
|
||||
add_supplier_to_db,
|
||||
update_supplier_in_db,
|
||||
delete_supplier_from_db,
|
||||
get_all_suppliers_from_db,
|
||||
get_supplier_details_from_db,
|
||||
add_supplier_contact_to_db,
|
||||
update_supplier_contact_in_db,
|
||||
delete_supplier_contact_from_db,
|
||||
add_purchase_activity_to_db,
|
||||
update_purchase_activity_in_db,
|
||||
delete_purchase_activity_from_db,
|
||||
get_supplier_id_from_contact_id,
|
||||
get_supplier_id_from_purchase_activity_id
|
||||
)
|
||||
from .router import router
|
||||
@@ -0,0 +1,243 @@
|
||||
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
|
||||
@@ -0,0 +1,127 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Optional
|
||||
from .db_manager import (
|
||||
add_supplier_to_db,
|
||||
update_supplier_in_db,
|
||||
delete_supplier_from_db,
|
||||
get_all_suppliers_from_db,
|
||||
get_supplier_details_from_db,
|
||||
add_supplier_contact_to_db,
|
||||
update_supplier_contact_in_db,
|
||||
delete_supplier_contact_from_db,
|
||||
add_purchase_activity_to_db,
|
||||
update_purchase_activity_in_db,
|
||||
delete_purchase_activity_from_db
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/suppliers", tags=["suppliers"])
|
||||
|
||||
# --- Pydantic Schemas ---
|
||||
class SupplierSchema(BaseModel):
|
||||
id: Optional[int] = None
|
||||
name_kr: str
|
||||
name_en: Optional[str] = None
|
||||
business_registration_number: Optional[str] = None
|
||||
contact_number: Optional[str] = None
|
||||
address: Optional[str] = None
|
||||
|
||||
class SupplierContactSchema(BaseModel):
|
||||
id: Optional[int] = None
|
||||
supplier_id: int
|
||||
name: str
|
||||
email: Optional[str] = None
|
||||
phone_number: Optional[str] = None
|
||||
position: Optional[str] = None
|
||||
work_location: Optional[str] = None
|
||||
|
||||
class PurchaseActivitySchema(BaseModel):
|
||||
id: Optional[int] = None
|
||||
supplier_id: int
|
||||
contact_id: Optional[int] = None
|
||||
activity_date: str # YYYY-MM-DD
|
||||
activity_type: str
|
||||
memo: Optional[str] = None
|
||||
|
||||
# --- Suppliers APIs ---
|
||||
@router.post("/", response_model=dict)
|
||||
def create_supplier(supplier: SupplierSchema):
|
||||
result = add_supplier_to_db(supplier.dict())
|
||||
if result is None:
|
||||
raise HTTPException(status_code=500, detail="Failed to create supplier")
|
||||
return {"id": result, "status": "success"}
|
||||
|
||||
@router.put("/{supplier_id}", response_model=dict)
|
||||
def update_supplier(supplier_id: int, supplier: SupplierSchema):
|
||||
data = supplier.dict()
|
||||
data['id'] = supplier_id
|
||||
success = update_supplier_in_db(data)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to update supplier")
|
||||
return {"status": "success"}
|
||||
|
||||
@router.delete("/{supplier_id}", response_model=dict)
|
||||
def delete_supplier(supplier_id: int):
|
||||
success = delete_supplier_from_db(supplier_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to delete supplier")
|
||||
return {"status": "success"}
|
||||
|
||||
@router.get("/", response_model=List[SupplierSchema])
|
||||
def list_suppliers():
|
||||
return get_all_suppliers_from_db()
|
||||
|
||||
@router.get("/{supplier_id}", response_model=dict)
|
||||
def get_supplier_details(supplier_id: int):
|
||||
details = get_supplier_details_from_db(supplier_id)
|
||||
if not details:
|
||||
raise HTTPException(status_code=404, detail="Supplier not found")
|
||||
return details
|
||||
|
||||
# --- Contacts APIs ---
|
||||
@router.post("/contacts", response_model=dict)
|
||||
def create_contact(contact: SupplierContactSchema):
|
||||
result = add_supplier_contact_to_db(contact.dict())
|
||||
if result is None:
|
||||
raise HTTPException(status_code=500, detail="Failed to create contact")
|
||||
return {"id": result, "status": "success"}
|
||||
|
||||
@router.put("/contacts/{contact_id}", response_model=dict)
|
||||
def update_contact(contact_id: int, contact: SupplierContactSchema):
|
||||
data = contact.dict()
|
||||
data['id'] = contact_id
|
||||
success = update_supplier_contact_in_db(data)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to update contact")
|
||||
return {"status": "success"}
|
||||
|
||||
@router.delete("/contacts/{contact_id}", response_model=dict)
|
||||
def delete_contact(contact_id: int):
|
||||
success = delete_supplier_contact_from_db(contact_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to delete contact")
|
||||
return {"status": "success"}
|
||||
|
||||
# --- Purchase Activities APIs ---
|
||||
@router.post("/activities", response_model=dict)
|
||||
def create_purchase_activity(activity: PurchaseActivitySchema):
|
||||
result = add_purchase_activity_to_db(activity.dict())
|
||||
if result is None:
|
||||
raise HTTPException(status_code=500, detail="Failed to create purchase activity")
|
||||
return {"id": result, "status": "success"}
|
||||
|
||||
@router.put("/activities/{activity_id}", response_model=dict)
|
||||
def update_purchase_activity(activity_id: int, activity: PurchaseActivitySchema):
|
||||
data = activity.dict()
|
||||
data['id'] = activity_id
|
||||
success = update_purchase_activity_in_db(data)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to update purchase activity")
|
||||
return {"status": "success"}
|
||||
|
||||
@router.delete("/activities/{activity_id}", response_model=dict)
|
||||
def delete_purchase_activity(activity_id: int):
|
||||
success = delete_purchase_activity_from_db(activity_id)
|
||||
if not success:
|
||||
raise HTTPException(status_code=500, detail="Failed to delete purchase activity")
|
||||
return {"status": "success"}
|
||||
Reference in New Issue
Block a user