6.6.0.1Timsession.allinstances->first.licproduct=2ScriptGetYourLawyerData transmission GetYourLawyerProjektfalsezusatzfeldasstring('getyourlawyer_mandate_ident') <>''# coding: windows-1252
#
#---Bezeichnung: Data transfer GetYourLawyer
# Klassen: Projekt
# CondExpression: zusatzfeldasstring('getyourlawyer_mandate_ident') <>''
# ObjectScript: Y
# ContainerScript: N
# EventType:
# EventClass:
# EventMembers:
# ExtendedRights: N
#---Script for transferring open services and expenses to GetYourLawyer.
# After successful transfer, the entry is marked with the key 'getYourLawyerTransferSuccessful'.
# For services that have already been transferred, the modifieddatetime is also sent so that an update is carried out instead of a new creation.
# Failed transactions can be repeated several times.
import requests
import vtcapp
def main():
project = argobject
# check if required properties are set
authtoken = vtcapp.getpropertyvalue("getYourLawyerToken")
url = vtcapp.getpropertyvalue("getYourLawyerURL")
if not url:
vtcapp.msgbox(vtcapp.translate("GetYourLawyer URL is missing. Please enter a valid GetYourLawyer URL in System Settings -> Project -> GetYourLawyer URL"))
return
if not authtoken:
vtcapp.msgbox(vtcapp.translate("GetYourLawyer Token is missing. Please enter a valid GetYourLawyer Token in System Settings -> Project -> GetYourLawyer Token"))
return
# we collect services and expenses in a common listfor transfer
servicesAndExpensesList = []
for vtcService in project.eval("offeneLeistungen"):
# the field updated_at may only be set, wenn the entry already exists in GetYourLawyer,
# so we have to check for a succesful transfer first
updated_at = None
if vtcService.getkeybool("getYourLawyerTransferSuccessful"):
updated_at = vtcService.modifieddatetime.isoformat()
# if the service has no external value, it should be marked non billable in GetYourLawyer
if vtcService.minutenext > 0:
billable = 1
else:
billable = 0
# if a type is entered on the service, we want to transfer the text of the type in the field type_detail
# otherwise we just write "Service" as type
if vtcService.typ:
type_detail = vtcService.typ.text
else:
type_detail = vtcapp.translate("Service")
service = {
"id": vtcService.objid,
"mandate_identifier": vtcService.projekt.getyourlawyer_mandate_ident,
"lawyer_identifier": vtcService.bearbeiter.getyourlawyer_lawyer_ident,
"type": 1,
"type_detail": unicode(type_detail),
"quantity": vtcService.minutenext,
"rate_chf": vtcService.ansatzext,
"subject_to_vat": 1,
"billable": billable,
"date": vtcService.datum.isoformat(),
"description": unicode(vtcService.text),
"updated_at": updated_at,
"deleted_at": None
}
servicesAndExpensesList.append(service)
for vtcExpenses in project.eval("offeneSpesen"):
# unlike services, we cannot set a type_detail for expenses,
# therefore we write the expense type with the text
if vtcExpenses.typ:
description = vtcExpenses.typ.text + " " + vtcExpenses.text
else:
description = vtcExpenses.text
# the field updated_at may only be set, wenn the entry already exists in GetYourLawyer,
# so we have to check for a succesful transfer first
updated_at = None
if vtcExpenses.getkeybool("getYourLawyerTransferSuccessful"):
updated_at = vtcExpenses.modifieddatetime.isoformat()
expenses = {
"id": vtcExpenses.objid,
"mandate_identifier": vtcExpenses.projekt.getyourlawyer_mandate_ident,
"lawyer_identifier": vtcExpenses.bearbeiter.getyourlawyer_lawyer_ident,
"type": 2,
"quantity": 1,
"rate_chf": vtcExpenses.wertext,
"subject_to_vat": 1,
"billable": 1,
"date": vtcExpenses.datum.isoformat(),
"description": unicode(description),
"updated_at": updated_at,
"deleted_at": None
}
servicesAndExpensesList.append(expenses)
if not servicesAndExpensesList:
vtcapp.msgbox(vtcapp.translate("No data available for transmission"))
return
headers = {}
headers["Authorization"] = "Bearer %s" % authtoken
headers["Accept"] = "application/json"
headers["Content-Type"] = "application/json"
"""f = open(r"c:\temp\json.txt", "w")
f.write(payload)
f.close()"""
r = requests.post(url, headers=headers, json=servicesAndExpensesList)
resp = r.json()
rowErrors = False
if resp['successful']:
for row in resp['rows']:
obj = vtcapp.getobjectbyid(row['id'])
if obj:
if row['successful']:
keyText = vtcapp.translate("Successfully transferred on {} by {}.").format(vtcapp.datetostr(vtcapp.currentdate()), vtcapp.currentlogin().name)
obj.setkeyvalue("getYourLawyerTransferSuccessful", True)
else:
keyText = vtcapp.translate("Transmission failed on {} by {}.").format(vtcapp.datetostr(vtcapp.currentdate()), vtcapp.currentlogin().name)
keyText += " " + vtcapp.translate("Error message") + ": {}".format(row['message'])
obj.setkeyvalue("getYourLawyerTransferSuccessful", False)
rowErrors = True
obj.setkeyvalue("getYourLawyer", keyText)
if rowErrors:
vtcapp.msgbox(vtcapp.translate("Errors occurred during data transmission. Please check the transmitted services and expenses."))
else:
vtcapp.msgbox(vtcapp.translate("Data transmission successful"))
else:
errorStr = vtcapp.translate("The following error occurred during data transmission:")
errorStr += "\r\n" + resp['code'] + "\r\n" + resp['message']
vtcapp.msgbox(errorStr)
main()
Nonetruefalse