diff --git a/stock_voucher_ux/controllers/main.py b/stock_voucher_ux/controllers/main.py index 7a7921337..d0838a679 100644 --- a/stock_voucher_ux/controllers/main.py +++ b/stock_voucher_ux/controllers/main.py @@ -1,3 +1,7 @@ +############################################################################## +# For copyright and license notices, see __manifest__.py file in module root +# directory +############################################################################## import io import json import re @@ -21,7 +25,10 @@ def _count_pages_with_products(self, pdf_reader, picking_id): if not move_lines: move_lines = picking.move_ids - product_codes = [line.product_id.default_code or line.product_id.name for line in move_lines if line.product_id] + product_codes = [ + line.product_id.default_code or line.product_id.name + for line in move_lines if line.product_id + ] pages_with_products = 0 @@ -54,7 +61,6 @@ def report_download(self, data, context=None): :returns: Response with a filetoken cookie and an attachment header """ response = super().report_download(data, context) - # NTH detect if the binary is a PDF, no matter ifn it was generated by a QWeb or Aeroo requestcontent = json.loads(data) url, type = requestcontent[0], requestcontent[1] if type == "aeroo" and "remito" in url: @@ -64,51 +70,62 @@ def report_download(self, data, context=None): assign = context_dict.get("assign") book_id = request.env["stock.picking"].browse(picking_id).book_id if assign and book_id and picking_id: - copies_result = request.env["ir.actions.report"].search_read( - [("report_name", "ilike", "remito")], ["copies"], limit=1 - ) - copies = copies_result[0]["copies"] if copies_result else None - pdf_response = response.response[0] - reader = PdfFileReader(io.BytesIO(pdf_response)) - - # Usar el nuevo método para contar páginas con productos - if copies: - total_pages = int(len(reader.pages) / copies) - number_pages = self._count_pages_with_products(reader, picking_id) - # Ajustar por número de copias - number_pages = min(number_pages, total_pages) - else: - number_pages = self._count_pages_with_products(reader, picking_id) - - # See if there are vouchers already assigned. If not, then it assigns the vouchers + file_response = response.response[0] + number_pages = 1 # Default for non-PDF files + + try: + if file_response.startswith(b'%PDF'): + copies_result = request.env["ir.actions.report"].search_read( + [("report_name", "ilike", "remito")], ["copies"], limit=1 + ) + copies = copies_result[0]["copies"] if copies_result else None + reader = PdfFileReader(io.BytesIO(file_response)) + + # Usar el nuevo método para contar páginas con productos + if copies: + total_pages = int(len(reader.pages) / copies) + number_pages = self._count_pages_with_products(reader, picking_id) + # Ajustar por número de copias + number_pages = min(number_pages, total_pages) + else: + number_pages = self._count_pages_with_products(reader, picking_id) + except Exception: + number_pages = 1 + if not request.env["stock.picking"].browse(picking_id).voucher_ids and book_id: request.env["stock.picking"].browse(picking_id).assign_numbers(number_pages, book_id) elif "report_deliveryslip" in url: - # If the report is not an aeroo, the assign method should only assign one voucher match = re.search(r"(\d+)$", json.loads(data)[0]) if match: picking_id = int(match.group(1)) book_id = request.env["stock.picking"].browse(picking_id).book_id if book_id and book_id.autoprinted == False and picking_id: - pdf_response = response.response[0] - reader = PdfFileReader(io.BytesIO(pdf_response)) - - # Usar el nuevo método para contar páginas con productos - copies_result = request.env["ir.actions.report"].search_read( - [("report_name", "=", "stock.report_deliveryslip")], ["l10n_ar_copies"], limit=1 - ) - copies = copies_result[0]["l10n_ar_copies"] if copies_result else None - - if copies == "triplicado": - total_pages = int(len(reader.pages) / 3) - elif copies == "duplicado": - total_pages = int(len(reader.pages) / 2) - else: - total_pages = len(reader.pages) - - number_pages = self._count_pages_with_products(reader, picking_id) - number_pages = min(number_pages, total_pages) + file_response = response.response[0] + number_pages = 1 # Default for non-PDF files + + # Check if the response is a PDF by looking at the first few bytes + try: + if file_response.startswith(b'%PDF'): + reader = PdfFileReader(io.BytesIO(file_response)) + # The number of pages will assign the number of vouchers + copies = ( + request.env["ir.actions.report"] + .search([("report_name", "=", "stock.report_deliveryslip")]) + .l10n_ar_copies + ) + if copies == "triplicado": + total_pages = int(len(reader.pages) / 3) + elif copies == "duplicado": + total_pages = int(len(reader.pages) / 2) + else: + total_pages = len(reader.pages) + + number_pages = self._count_pages_with_products(reader, picking_id) + number_pages = min(number_pages, total_pages) + except Exception: + # If PDF reading fails, fallback to 1 page + number_pages = 1 if not request.env["stock.picking"].browse(picking_id).voucher_ids and book_id: request.env["stock.picking"].browse(picking_id).assign_numbers(number_pages, book_id)