iT邦幫忙

2024 iThome 鐵人賽

DAY 8
0
Odoo

30天就算 0 基礎,也能使用 GenAI 創造簡單的 Odoo 模組應用系列 第 8

【Day08】函數設計 (function):自動化報表輸出和客戶資料管理

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20240922/20163326tmkTYEulVo.png

https://ithelp.ithome.com.tw/upload/images/20240922/20163326TaT857eXao.png

訂單總額計算:位置參數與回傳值的應用 (Function and Return)

ERP 系統中的範例: 我們可以寫一個函數來計算客戶的訂單總價。

def calculate_total(order_items):
    total = 0
    for item in order_items:
        total += item['price'] * item['quantity']
    return total

# 假設訂單項目
order = [{'price': 100, 'quantity': 2}, {'price': 50, 'quantity': 3}]
print(calculate_total(order))  # 輸出 350

客戶資料管理:位置參數 (Positional Arguments)

ERP 系統中的範例: 設定一個函數來新增一個客戶,並用位置參數傳遞名稱、地址

def add_customer(name, address, phone):
    return f"新增客戶: 名稱: {name}, 地址: {address}, 電話: {phone}"

print(add_customer("John Doe", "1234 Main St", "555-1234"))

客戶資料管理:關鍵字參數 (Keyword Arguments)

ERP 系統中的範例: 你可以選擇使用關鍵字參數來更清楚地指定客戶資料。

def add_customer(name, address, phone):
    return f"新增客戶: 名稱: {name}, 地址: {address}, 電話: {phone}"

print(add_customer(name="Jane Doe", address="5678 Oak St", phone="555-5678"))

客戶資料管理:預設參數 (Default Arguments)

ERP 系統中的範例: 例如,當建立客戶資料時,若沒有提供狀態,則預設為「活躍」。

def add_customer(name, address, phone, status="活躍"):
    return f"新增客戶: 名稱: {name}, 地址: {address}, 電話: {phone}, 狀態: {status}"

print(add_customer(name="Alice", address="9101 Maple St", phone="555-9101"))
print(add_customer(name="Bob", address="1122 Pine St", phone="555-1122", status="非活躍"))

訂單處理系統:任意參數 (Arbitrary Arguments) *args 和 **kwargs

ERP 系統中的範例: 使用 *args 來處理可變數量的訂單項目,或使用 **kwargs 來處理多個客戶屬性。.

# *args 處理不定數量的訂單項目
def add_items(*items):
    for item in items:
        print(f"新增訂單項目: {item}")

add_items("item1", "item2", "item3")

# **kwargs 處理不定數量的客戶屬性
def update_customer(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

update_customer(name="David", address="7890 Elm St", status="活躍")

上一篇
【Day07】迴圈控制使用情境:購物車總金額、連鎖店調貨邏輯
下一篇
【Day09】 物件導向構建電商系統:Class、繼承與封裝的應用
系列文
30天就算 0 基礎,也能使用 GenAI 創造簡單的 Odoo 模組應用21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言