portal = context.getPortalObject()
project = context

causality_value = portal.restrictedTraverse(causality)

if causality_value.Base_getSupportRequestInProgress(title=title) is not None:
  return

# Create a temp Sale Order to find the trade condition
now = DateTime()
module = portal.portal_trash
tmp_sale_order = module.newContent(
  portal_type='Sale Order',
  temp_object=True,
  trade_condition_type="ticket",
  start_date=now,
  destination=destination_decision,
  destination_decision=destination_decision,
  source_project_value=project,
  ledger_value=portal.portal_categories.ledger.automated
)
tmp_sale_order.SaleOrder_applySaleTradeCondition(batch_mode=1, force=1)
"""
if tmp_sale_order.getSpecialise(None) is None:
  raise AssertionError('Can not find a trade condition to generate the Support Request')
"""
resource = portal.service_module.slapos_crm_monitoring
support_request = portal.support_request_module.newContent(
  portal_type="Support Request",
  title=title,
  description=text_content,
  start_date=tmp_sale_order.getStartDate(),
  source=tmp_sale_order.getSource(),
  source_section=tmp_sale_order.getSourceSection(),
  source_project=tmp_sale_order.getSourceProject(),
  destination=tmp_sale_order.getDestination(),
  destination_section=tmp_sale_order.getDestinationSection(),
  destination_project=tmp_sale_order.getDestinationProject(),
  destination_decision=tmp_sale_order.getDestinationDecision(),
  specialise=tmp_sale_order.getSpecialise(),
  causality=causality,
  # Ensure resoure is Monitoring
  resource_value=resource,
  quantity_unit=resource.getQuantityUnit(),
  base_contribution_list=resource.getBaseContributionList(),
  use=resource.getUse(),
  quantity=1,
  price=0
)
support_request.validate()
return support_request
