Skip to content

TypeError on POS Invoice submit: doc.creation string not converted to datetime in calculate_and_set_times #102

@os-erps

Description

@os-erps

Environment

  • ERPNext: 15.102.0
  • Frappe: 15.103.1
  • URY: 0.2.1

Error

TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'

Location

ury/ury/hooks/ury_pos_invoice.py line 93 — calculate_and_set_times()

Traceback

File "apps/ury/ury/ury/hooks/ury_pos_invoice.py", line 93, in calculate_and_set_times
    time_difference = current_time - doc.creation
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'

Cause

current_time is a datetime object created via datetime.strptime()
but doc.creation arrives as a string when submitted via the API/form.
Subtracting a datetime from a str raises TypeError.

Fix

Convert doc.creation to datetime before the subtraction:

# Before (broken)
time_difference = current_time - doc.creation

# After (fixed)
time_difference = current_time - datetime.strptime(str(doc.creation), "%Y-%m-%d %H:%M:%S.%f")

Fixed function

def calculate_and_set_times(doc, method):
    doc.arrived_time = doc.creation
    current_time_str = now()
    current_time = datetime.strptime(current_time_str, "%Y-%m-%d %H:%M:%S.%f")
    time_difference = current_time - datetime.strptime(str(doc.creation), "%Y-%m-%d %H:%M:%S.%f")
    total_seconds = int(time_difference.total_seconds())
    hours, remainder = divmod(total_seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    formatted_spend_time = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
    doc.total_spend_time = formatted_spend_time

Impact

POS Invoice cannot be submitted through the ERPNext UI or API
when URY is installed. Affects all restaurants using URY on ERPNext v15.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions