可以使用 Odoo 的 ORM語法來過濾訂單。以下是一個範例代碼片段,可以在 Odoo 的 Python 環境中使用:
# 假設在一個Odoo模型或控制器中
from odoo import models, fields, api
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
@api.model
def filter_purchase_orders(self, delivery_date=None, supplier_id=None, product_id=None):
domain = []
# 添加交貨日期的過濾條件
if delivery_date:
domain.append(('date_planned', '=', delivery_date))
# 添加供應商的過濾條件
if supplier_id:
domain.append(('partner_id', '=', supplier_id))
# 添加產品類型的過濾條件
if product_id:
domain.append(('order_line.product_id', '=', product_id))
# 過濾已付款的訂單
domain.append(('state', '=', 'purchase')) # 只顯示已確認的訂單
domain.append(('invoice_status', '=', 'invoiced')) # 確認已付款
# 查詢符合條件的訂單
purchase_orders = self.search(domain)
return purchase_orders
接下來,可以添加匯出功能,以下是使用 csv
模組匯出篩選後訂單的示例:
import csv
from odoo import http
from odoo.http import request
class PurchaseOrderController(http.Controller):
@http.route('/export_purchase_orders', type='http', auth='user')
def export_purchase_orders(self, **kwargs):
# 假設篩選條件已通過參數傳遞
delivery_date = kwargs.get('delivery_date')
supplier_id = kwargs.get('supplier_id')
product_id = kwargs.get('product_id')
# 獲取符合條件的訂單
orders = request.env['purchase.order'].filter_purchase_orders(delivery_date, supplier_id, product_id)
# 創建CSV文件
output = request.make_response()
output.headers['Content-Disposition'] = 'attachment; filename="purchase_orders.csv"'
output.headers['Content-Type'] = 'text/csv'
writer = csv.writer(output.stream, quoting=csv.QUOTE_MINIMAL)
writer.writerow(['PO Number', 'Supplier', 'Amount', 'Delivery Date']) # 標題行
for order in orders:
writer.writerow([order.name, order.partner_id.name, order.amount_total, order.date_planned])
return output
filter_purchase_orders
方法,傳入交貨日期、供應商ID和產品ID進行過濾。/export_purchase_orders
路由以匯出篩選結果為CSV文件。