Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 16 additions & 28 deletions stock_voucher_ux/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,44 +70,32 @@ def report_download(self, data, context=None, token=None, **kwargs):
# 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:
context_part = json.loads(data)[0].split("context=")[1]
context_dict = json.loads(urllib.parse.unquote(context_part))
picking_id = context_dict.get("active_ids")
if type == "aeroo" and "context=" in url:
context_dict = json.loads(urllib.parse.unquote(url.split("context=")[1]))
assign = context_dict.get("assign")
book_id = request.env["stock.picking"].browse(picking_id).book_id
if assign and book_id and picking_id:
# Copias del reporte que se imprime, resuelto por su report_name en la URL.
picking = request.env["stock.picking"].browse(context_dict.get("active_ids") or [])
# Only the single-picking voucher print numbers here (do_print_and_assign
# sets ``assign``); batch prints are handled in stock_batch_picking_voucher.
if assign and len(picking) == 1 and picking.book_id and not picking.voucher_ids:
copies = request.env["ir.actions.report"]._get_voucher_copies_from_url(url)
# Check if response is PDF, if not (like .doc), assign 1 voucher
try:
pdf_response = response.response[0]
reader = PdfFileReader(io.BytesIO(pdf_response))

# Usar el nuevo método para contar páginas con productos
reader = PdfFileReader(io.BytesIO(response.response[0]))
number_pages = self._count_pages_with_products(reader, picking.id)
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:
# If not PDF or can't process (like .doc), assign only 1 voucher
number_pages = 1

# See if there are vouchers already assigned. If not, assign them
# based on the real page count, then re-render so the numbers show.
picking = request.env["stock.picking"].browse(picking_id)
if not picking.voucher_ids and book_id:
picking.assign_numbers(number_pages, book_id)
picking.env.flush_all()
# Re-render: the first PDF had no numbers yet; this second
# render includes the just-assigned voucher numbers.
try:
response = super().report_download(data, context=context, token=token, **kwargs)
except Exception:
pass
# Assign by the real page count, then re-render so the numbers
# show up on the delivered PDF.
picking.assign_numbers(number_pages, picking.book_id)
picking.env.flush_all()
try:
response = super().report_download(data, context=context, token=token, **kwargs)
except Exception:
pass

elif "report_deliveryslip" in url:
# Fallback: if numbers weren't pre-assigned (e.g. printed outside
Expand Down
Loading