Xero API php包装器在createTimesheet上出现500错误

问题描述

我正在使用Xero API PHP包装程序发布时间表,除了“发生错误”和状态500之外,没有其他详细信息回来。

我正在验证,它包含在范围中,据我所知,它是所请求格式的有效json ...

[{"EmployeeID":"a valid employee id","StartDate":"2020-08-03","EndDate":"2020-08-09","Status":"Draft","TimesheetLines":{"TimesheetLine":[{"EarningsRateID":"a valid pay item id","NumberOfUnits":{"NumberOfUnit":["0.00","0.00","8","0.00"]}},{"EarningsRateID":"a valid pay item id","8.5","8.5"]}}]}}]

通过...发送。

try {
      $result = $payrollAuApi->createTimesheet($xeroTenantId,$timesheet);
      print_r($result);
  } catch (Exception $e) {
      echo 'Exception when calling PayrollAuApi->createTimesheet: ',$e->getMessage(),PHP_EOL;
  }

租户ID正常。

对于任何关于我要出问题的地方的指导,我深表感谢。

解决方法

看起来您的TimesheetLines是一个包含名为TimesheetLine的属性的对象,该属性是一个集合-但根据docs,TimesheetLines本身应该是时间表行的集合。

即你应该有这样的东西:

[{ EmployeeID : 4fd71c0f-8612-4fd8-99d0-b3a2e8272b02,StartDate : 2020-08-03,EndDate : 2020-08-09,Status : Draft,TimesheetLines :[{ EarningsRateID : 0e30f933-04ed-4b96-9357-9747209c306f,NumberOfUnits :{ NumberOfUnit :[ 0.00,0.00,8,0.00 ]}},{ EarningsRateID : 0e30f933-04ed-4b96-9357-9747209c306f,8.5,8.5 ]}}]}]

...而不是...

[{ EmployeeID : 4fd71c0f-8612-4fd8-99d0-b3a2e8272b02,TimesheetLines :{ TimesheetLine :[{ EarningsRateID : 0e30f933-04ed-4b96-9357-9747209c306f,8.5 ]}}]}}]

更改该帮助吗?

,

对于遇到此问题的任何人,问题在于,与我在私有应用中使用的旧包装程序不同,我在该应用程序中构造了完整的XML时间表并将其发送,而Xero的oauth2包装程序并没有填写完整的时间表。而是创建时间表的实例并为其设置属性。

代码信用来自repo的@wobinb

$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()- 
>setAccessToken( (string)$storage->getAccessToken() );
$payrollApi = new XeroAPI\XeroPHP\Api\PayrollAuApi(
  new GuzzleHttp\Client(),$config
);

//first retrieve a list of active employees
$apiResponse = $payrollApi->getEmployees($tenantId,null,"Status==\"ACTIVE\"");
$employees = $apiResponse->getEmployees();
foreach ($employees AS $employee) {
  print $employee->getFirstName()." ".$employee->getLastName()." - ".$employee->getPayrollCalendarID();
   if ($employee->getPayrollCalendarID() != null) {
     $employeeID = $employee->getEmployeeID();
     $payrollCalendarID = $employee->getPayrollCalendarID();
     $ordinaryEarningsRateID = $employee->getOrdinaryEarningsRateID();
   }
   print "<br />";
}
print "<br />";
//and now the payroll calendar
$apiResponse = $payrollApi->getPayrollCalendar($tenantId,$payrollCalendarID);
$calendar = $apiResponse->getPayrollCalendars()[0];
print $calendar->getName()." - ".$calendar->getCalendarType();
print "<br />";

//finally create the timesheet
$timesheet = new XeroAPI\XeroPHP\Models\PayrollAu\Timesheet();
$timesheet->setEmployeeID($employeeID);
$timesheet->setStartDateAsDate($calendar->getStartDateAsDate());
//need to calculate how many days the timesheet will cover
switch ($calendar->getCalendarType()) {
  case "WEEKLY":
    $lengthofCalendar = 7;
    break;
  case "FORTNIGHTLY":
    $lengthofCalendar = 14;
    break;
  case "FOURWEEKLY":
    $lengthofCalendar = 28;
    break;
//monthly pay runs will be more complicated to calculate  
}
$endDate = $calendar->getStartDateAsDate();
//end date will be start date plus the length and minus one day
$period = "P". ($lengthofCalendar - 1) . "D";
$endDate->add(new DateInterval($period));
$timesheet->setEndDateAsDate($endDate);
$timesheet->setStatus("DRAFT");
$timesheetLine = new XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine();
$timesheetLine->setEarningsRateId($ordinaryEarningsRateID);
for ($day = 1; $day <= $lengthofCalendar; $day++) { 
  $numberOfUnits[] = $day;
}
$timesheetLine->setNumberOfUnits($numberOfUnits);
$timesheetLines[] = $timesheetLine;
$timesheet->setTimeSheetLines($timesheetLines);
$timesheets[] = $timesheet;
$apiResponse = $payrollApi->createTimesheet($tenantId,$timesheets);