cursor.execute('SELECT id,agreement_number,customer_id,status,workflow_coverage,monthly_price FROM customer_fixed_price_agreements'+where+' ORDER BY customer_id,id',params)
agreements=cursor.fetchall()
cursor.execute('SELECT id,card_number,customer_id,status,remaining_hours,workflow_coverage FROM tticket_prepaid_cards'+where+' ORDER BY customer_id,id',params)
ifany(tag_id<=0fortag_idintag_ids):raiseHTTPException(422,'Tag-ID skal være positivt')
withtransaction()ascursor:
cursor.execute(f'SELECT * FROM {table} WHERE id=%s',(resource_id,))
resource=cursor.fetchone()
ifnotresource:raiseHTTPException(404,'Aftale eller kort findes ikke')
iftag_ids:
cursor.execute('SELECT id FROM tags WHERE id=ANY(%s) AND is_active=TRUE',(tag_ids,))
if{r['id']forrincursor.fetchall()}!=set(tag_ids):raiseHTTPException(422,'Et tag findes ikke eller er inaktivt')
# Lock pending time rows before the resource, matching settlement lock order.
cursor.execute("SELECT t.id FROM tmodule_times t LEFT JOIN sag_sager s ON s.id=t.sag_id LEFT JOIN tmodule_customers c ON c.id=t.customer_id WHERE COALESCE(s.customer_id,c.hub_customer_id)=%s AND t.status NOT IN ('approved','billed') AND t.economy_order_draft_id IS NULL ORDER BY t.id FOR UPDATE OF t",(resource['customer_id'],))
time_ids=[r['id']forrincursor.fetchall()]
cursor.execute(f'SELECT * FROM {table} WHERE id=%s FOR UPDATE',(resource_id,))
cursor.execute(f'UPDATE {table} SET workflow_coverage=%s{price_sql},updated_at=now() WHERE id=%s RETURNING *',tuple(values))
updated=cursor.fetchone()
cursor.execute('INSERT INTO bmc_workflow_resource_history(resource_type,resource_id,created_by,before_value,after_value) VALUES(%s,%s,%s,%s,%s)',(kind,resource_id,str(user['id']),Json(json_safe(dict(resource))),Json(json_safe(dict(updated)))))
iftime_ids:
cursor.execute('UPDATE tmodule_times SET workflow_revision=workflow_revision+1 WHERE id=ANY(%s) RETURNING id,workflow_revision',(time_ids,))
raiseHTTPException(422,'Tag-ID skal være positivt')
withtransaction()ascursor:
cursor.execute('SELECT id FROM customers WHERE id=%s',(customer_id,))
ifnotcursor.fetchone():
raiseHTTPException(404,'Kunden findes ikke')
iftag_ids:
cursor.execute('SELECT id FROM tags WHERE id=ANY(%s) AND is_active=TRUE',(tag_ids,))
if{r['id']forrincursor.fetchall()}!=set(tag_ids):
raiseHTTPException(422,'Et tag findes ikke eller er inaktivt')
# Match settlement lock order. Approved decisions are immutable and revalidated at settlement.
cursor.execute("SELECT t.id FROM tmodule_times t LEFT JOIN sag_sager s ON s.id=t.sag_id LEFT JOIN tmodule_customers c ON c.id=t.customer_id WHERE COALESCE(s.customer_id,c.hub_customer_id)=%s AND t.status NOT IN ('approved','billed') AND t.economy_order_draft_id IS NULL ORDER BY t.id FOR UPDATE OF t",(customer_id,))
time_ids=[r['id']forrincursor.fetchall()]
cursor.execute('SELECT id FROM customers WHERE id=%s FOR UPDATE',(customer_id,))
cursor.execute('SELECT workflow_coverage FROM bmc_workflow_customer_card_coverage WHERE customer_id=%s',(customer_id,))
previous=cursor.fetchone()
cursor.execute('INSERT INTO bmc_workflow_customer_card_coverage(customer_id,workflow_coverage) VALUES(%s,%s) ON CONFLICT(customer_id) DO UPDATE SET workflow_coverage=EXCLUDED.workflow_coverage,updated_at=now()',(customer_id,Json(coverage)))
cursor.execute('INSERT INTO bmc_workflow_resource_history(resource_type,resource_id,created_by,before_value,after_value) VALUES(%s,%s,%s,%s,%s)',('customer_card',customer_id,str(user['id']),Json(json_safe(dict(previous)))ifpreviouselseJson({}),Json({'workflow_coverage':coverage})))
iftime_ids:
cursor.execute('UPDATE tmodule_times SET workflow_revision=workflow_revision+1 WHERE id=ANY(%s) RETURNING id,workflow_revision',(time_ids,))