# This is an autogenerated file from the command "q2 generate_hq_api" and will be overwritten if run again
"""
DataSet models for UpdateServiceChargePlanV2.
These typed dataclass models represent the tables and columns within the
DataSet parameters for the UpdateServiceChargePlanV2 HQ operation.
IMPORTANT: Update operations require a GET first to populate the original_record
field. HQ uses the unchanged row for optimistic concurrency checking.
Usage::
from q2_sdk.hq.hq_api.backoffice import UpdateServiceChargePlanV2
q2_service_charge_plan_edit_v2 = UpdateServiceChargePlanV2.Q2ServiceChargePlanEditV2(
plan_name="...",
description="...",
months_free=1,
base_fee=1.0,
max_usage_fee=1.0,
max_txn_fee=1.0,
sum_usage_and_txn_fees=True,
no_max_usage_fee=True,
no_max_txn_fee=True,
)
q2_service_charge_plan_fee_edit_v2 = UpdateServiceChargePlanV2.Q2ServiceChargePlanFeeEditV2(
service_charge_plan_id=1,
base_fee=1.0,
number_free=1,
additional_use_dollars=1.0,
only_if_used=True,
max_fee=1.0,
fee_type_id=1,
short_name="...",
description="...",
additional_use_percent_of_txn=1.0,
is_usage_fee=True,
)
"""
from __future__ import annotations
import copy
from dataclasses import dataclass, field
from typing import Any, ClassVar, Optional
from q2_sdk.hq.models.dataset_builder import DataSetBuilder, DataSetTable, RowState
[docs]
@dataclass
class Q2ServiceChargePlanEditV2:
"""
Represents a row in the Q2_ServiceChargePlanEditV2 table.
"""
service_charge_plan_id: int = -1
plan_name: str = ""
description: str = ""
months_free: int = 0
base_fee: float = 0.0
max_usage_fee: float = 0.0
max_txn_fee: float = 0.0
sum_usage_and_txn_fees: bool = False
no_max_usage_fee: bool = False
no_max_txn_fee: bool = False
original_record: Optional["Q2ServiceChargePlanEditV2"] = field(
default=None, repr=False
)
[docs]
@staticmethod
def get_columns() -> list[list[str]]:
"""Returns [[HqColumnName, pythonAttrName], ...] mapping."""
return [
["ServiceChargePlanID", "service_charge_plan_id"],
["PlanName", "plan_name"],
["Description", "description"],
["MonthsFree", "months_free"],
["BaseFee", "base_fee"],
["MaxUsageFee", "max_usage_fee"],
["MaxTxnFee", "max_txn_fee"],
["SumUsageAndTxnFees", "sum_usage_and_txn_fees"],
["NoMaxUsageFee", "no_max_usage_fee"],
["NoMaxTxnFee", "no_max_txn_fee"],
]
[docs]
def to_row_values(self) -> list[Any]:
"""Returns column values in get_columns() order."""
return [
self.service_charge_plan_id,
self.plan_name,
self.description,
self.months_free,
self.base_fee,
self.max_usage_fee,
self.max_txn_fee,
self.sum_usage_and_txn_fees,
self.no_max_usage_fee,
self.no_max_txn_fee,
]
_COLUMN_TYPES: ClassVar[dict[str, type]] = {
"service_charge_plan_id": int,
"plan_name": str,
"description": str,
"months_free": int,
"base_fee": float,
"max_usage_fee": float,
"max_txn_fee": float,
"sum_usage_and_txn_fees": bool,
"no_max_usage_fee": bool,
"no_max_txn_fee": bool,
}
[docs]
@classmethod
def from_hq_response(cls, row_elem) -> "Q2ServiceChargePlanEditV2":
"""Parse an lxml row element into this model with original_record set."""
kwargs: dict[str, Any] = {}
for hq_name, python_name in cls.get_columns():
child = row_elem.find(hq_name)
if child is not None and child.text is not None:
target_type = cls._COLUMN_TYPES.get(python_name, str)
if target_type is bool:
kwargs[python_name] = child.text.lower() in ("true", "1")
elif target_type in (int, float):
try:
kwargs[python_name] = target_type(child.text)
except (ValueError, TypeError):
kwargs[python_name] = child.text
else:
kwargs[python_name] = child.text
instance = cls(**kwargs)
instance.original_record = copy.deepcopy(instance)
return instance
[docs]
@dataclass
class Q2ServiceChargePlanFeeEditV2:
"""
Represents a row in the Q2_ServiceChargePlanFeeEditV2 table.
"""
service_charge_plan_id: int = 0
base_fee: float = 0.0
number_free: int = 0
additional_use_dollars: float = 0.0
only_if_used: bool = False
max_fee: float = 0.0
fee_type_id: int = 0
short_name: str = ""
description: str = ""
additional_use_percent_of_txn: float = 0.0
is_usage_fee: bool = False
original_record: Optional["Q2ServiceChargePlanFeeEditV2"] = field(
default=None, repr=False
)
[docs]
@staticmethod
def get_columns() -> list[list[str]]:
"""Returns [[HqColumnName, pythonAttrName], ...] mapping."""
return [
["ServiceChargePlanID", "service_charge_plan_id"],
["BaseFee", "base_fee"],
["NumberFree", "number_free"],
["AdditionalUseDollars", "additional_use_dollars"],
["OnlyIfUsed", "only_if_used"],
["MaxFee", "max_fee"],
["FeeTypeID", "fee_type_id"],
["ShortName", "short_name"],
["Description", "description"],
["AdditionalUsePercentOfTxn", "additional_use_percent_of_txn"],
["IsUsageFee", "is_usage_fee"],
]
[docs]
def to_row_values(self) -> list[Any]:
"""Returns column values in get_columns() order."""
return [
self.service_charge_plan_id,
self.base_fee,
self.number_free,
self.additional_use_dollars,
self.only_if_used,
self.max_fee,
self.fee_type_id,
self.short_name,
self.description,
self.additional_use_percent_of_txn,
self.is_usage_fee,
]
_COLUMN_TYPES: ClassVar[dict[str, type]] = {
"service_charge_plan_id": int,
"base_fee": float,
"number_free": int,
"additional_use_dollars": float,
"only_if_used": bool,
"max_fee": float,
"fee_type_id": int,
"short_name": str,
"description": str,
"additional_use_percent_of_txn": float,
"is_usage_fee": bool,
}
[docs]
@classmethod
def from_hq_response(cls, row_elem) -> "Q2ServiceChargePlanFeeEditV2":
"""Parse an lxml row element into this model with original_record set."""
kwargs: dict[str, Any] = {}
for hq_name, python_name in cls.get_columns():
child = row_elem.find(hq_name)
if child is not None and child.text is not None:
target_type = cls._COLUMN_TYPES.get(python_name, str)
if target_type is bool:
kwargs[python_name] = child.text.lower() in ("true", "1")
elif target_type in (int, float):
try:
kwargs[python_name] = target_type(child.text)
except (ValueError, TypeError):
kwargs[python_name] = child.text
else:
kwargs[python_name] = child.text
instance = cls(**kwargs)
instance.original_record = copy.deepcopy(instance)
return instance
[docs]
def build_service_charge_plan_edit_dataset(
q2_service_charge_plan_edit_v2: Optional[
Q2ServiceChargePlanEditV2 | list[Q2ServiceChargePlanEditV2]
] = None,
q2_service_charge_plan_fee_edit_v2: Optional[
Q2ServiceChargePlanFeeEditV2 | list[Q2ServiceChargePlanFeeEditV2]
] = None,
) -> DataSetBuilder:
"""
Build the service_charge_plan_edit DataSet parameter for UpdateServiceChargePlanV2.
For update operations, each model instance should have its original_record
set to the pre-modification state (from a prior GET call).
For add operations, set auto-increment ID fields to negative values.
:returns: DataSetBuilder ready to pass as the 'service_charge_plan_edit' parameter
"""
dataset = DataSetBuilder("DalServiceChargePlanEditV2")
# Q2_ServiceChargePlanEditV2
q2_service_charge_plan_edit_v2_cols = [
col[0] for col in Q2ServiceChargePlanEditV2.get_columns()
]
q2_service_charge_plan_edit_v2_table = dataset.add_table(
"Q2_ServiceChargePlanEditV2", q2_service_charge_plan_edit_v2_cols
)
_add_rows_to_table(
q2_service_charge_plan_edit_v2_table, q2_service_charge_plan_edit_v2
)
# Q2_ServiceChargePlanFeeEditV2
q2_service_charge_plan_fee_edit_v2_cols = [
col[0] for col in Q2ServiceChargePlanFeeEditV2.get_columns()
]
q2_service_charge_plan_fee_edit_v2_table = dataset.add_table(
"Q2_ServiceChargePlanFeeEditV2", q2_service_charge_plan_fee_edit_v2_cols
)
_add_rows_to_table(
q2_service_charge_plan_fee_edit_v2_table, q2_service_charge_plan_fee_edit_v2
)
return dataset
def _add_rows_to_table(
table: DataSetTable,
records: Optional[Any | list],
) -> None:
"""
Add rows to a DataSetTable from one or more model instances.
Handles Modified+Unchanged pairs (for updates), Added rows (for inserts),
and Deleted rows automatically based on each record's original_record field.
"""
if records is None:
return
if not isinstance(records, list):
records = [records]
for record in records:
values = record.to_row_values()
if record.original_record is not None:
# Update: emit Modified row (new values) + Unchanged row (original)
table.add_row(RowState.MODIFIED, values)
table.add_row(RowState.UNCHANGED, record.original_record.to_row_values())
else:
# Add: emit Added row (new record)
table.add_row(RowState.ADDED, values)