added export button

This commit is contained in:
2026-02-20 16:41:27 +03:30
parent 569b66a623
commit b88af8605c
12 changed files with 158 additions and 0 deletions

View File

@@ -0,0 +1 @@
from . import main

View File

@@ -0,0 +1,109 @@
import io
from odoo import fields, http
from odoo.http import request
class MrpOrderXlsxExportController(http.Controller):
@http.route(
"/mrp/production/<int:production_id>/export_xlsx",
type="http",
auth="user",
methods=["GET"],
)
def export_mrp_order_xlsx(self, production_id, **kwargs):
try:
import xlsxwriter
except ImportError:
try:
from odoo.tools.misc import xlsxwriter
except ImportError:
return request.make_response(
"xlsxwriter library is not available in this Odoo environment.",
headers=[("Content-Type", "text/plain; charset=utf-8")],
status=500,
)
production = request.env["mrp.production"].browse(production_id).exists()
if not production:
return request.not_found()
production.check_access_rights("read")
production.check_access_rule("read")
output = io.BytesIO()
workbook = xlsxwriter.Workbook(output, {"in_memory": True})
worksheet = workbook.add_worksheet("MO")
header_fmt = workbook.add_format({"bold": True})
date_fmt = workbook.add_format({"num_format": "yyyy-mm-dd"})
row = 0
worksheet.write(row, 0, "Manufacturing Order", header_fmt)
worksheet.write(row, 1, production.name or "")
row += 1
worksheet.write(row, 0, "Product", header_fmt)
worksheet.write(row, 1, production.product_id.display_name or "")
row += 1
worksheet.write(row, 0, "Quantity", header_fmt)
worksheet.write(row, 1, production.product_qty or 0.0)
row += 1
worksheet.write(row, 0, "UoM", header_fmt)
worksheet.write(row, 1, production.product_uom_id.name or "")
row += 1
worksheet.write(row, 0, "Planned Date", header_fmt)
if production.date_start:
dt_start = fields.Datetime.from_string(production.date_start)
worksheet.write_datetime(row, 1, dt_start, date_fmt)
else:
worksheet.write(row, 1, "")
row += 1
worksheet.write(row, 0, "Responsible", header_fmt)
worksheet.write(row, 1, production.user_id.name or "")
row += 2
table_headers = [
"Component",
"Required Qty",
"Consumed Qty",
"UoM",
"Source Location",
]
for col, title in enumerate(table_headers):
worksheet.write(row, col, title, header_fmt)
row += 1
for move in production.move_raw_ids.sorted(key=lambda item: item.id):
consumed_qty = getattr(move, "quantity", None)
if consumed_qty is None:
consumed_qty = getattr(move, "quantity_done", 0.0)
worksheet.write(row, 0, move.product_id.display_name or "")
worksheet.write(row, 1, move.product_uom_qty or 0.0)
worksheet.write(row, 2, consumed_qty or 0.0)
worksheet.write(row, 3, move.product_uom.name or "")
worksheet.write(row, 4, move.location_id.display_name or "")
row += 1
worksheet.set_column(0, 0, 36)
worksheet.set_column(1, 4, 18)
workbook.close()
output.seek(0)
filename = f"{(production.name or 'MO').replace('/', '-')}.xlsx"
headers = [
(
"Content-Disposition",
http.content_disposition(filename),
),
(
"Content-Type",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
),
]
return request.make_response(output.read(), headers=headers)