Closed CraigSheppardSec closed 4 years ago
Hi @CraigSheppardSec
I checked the log for your application and see that both the JSON and XML produced by your app are enclosed by double quotes thus turned them into strings and not valid.
You can check the logs yourself by login to the Xero Developer portal and then go to the application's History tab.
@CraigSheppardSec - the PHP SDK does not accept raw XML or JSON. Instead you'll create an instance of the Timesheet object and set the properties on it.
Let me see if someone can mock up an example. @wobinb
Thank you @SidneyAllen, that would be very helpful (I'm sure not just to me!).
OK, so here goes, this retrieves the list of employees and finds one with the payroll calendar set. It then retrieves the calendar to establish how long the period should be. Finally it creates the timesheet.
Please note that you would need to add in extra logic to calculate the length of the calendar types (Monthly, twice-monthly etc.)
$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);
Huge thanks @wobinb ! Very much appreciated.
SDK you're using (please complete the following information):
Describe the bug When using createTimesheet method with AU Payroll I get a 500 error. All get methods are working which indicates the auth etc is all fine.
To Reproduce Tried xml (which has been working for years in a private app)
and JSON
(which I know is blank, but should behave as such)
and many variations of elements, all without success.
Using
Expected behavior Draft Timesheet lands in Xero
Additional context The scope is set to allow
The 500 Error is remarkably unhelpful, if anyone is able to spot something in the formatting of the timesheet (which is all I can imagine it is?) or anything else I'm missing it would be very much appreciated.