0

Need help to fix Xero SDK v2 issue posting/creating timesheet. Xero PHP API reference https://xeroapi.github.io/xero-php-oauth2/docs/v2/payroll_au/index.html#api-PayrollAu-createTimesheet

My code is below

require __DIR__ . '/xero-auth/vendor/autoload.php';
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken(xxxxxxxxx);
$apiInstance = new XeroAPI\XeroPHP\Api\PayrollAuApi(
        new GuzzleHttp\Client(),
        $config
    );
 $xeroTenantId = "xxxxxxxxxxxxxxxxxxx";
 $emp_timeSheet =array();
 $startDate = new DateTime('2021-09-20');
 $endDate = new DateTime('2021-10-03');
 $timesheet = new XeroAPI\XeroPHP\Models\PayrollAu\Timesheet; 
 if($timesheet_id != '') $timesheet->setTimesheetId($timesheet_id);
 $timesheet->setEmployeeId($employee_id);
 $timesheet->setStartDate($Start_Date);                 
 $timesheet->setEndDate($End_Date); 
 $timesheet->setStatus(XeroAPI\XeroPHP\Models\PayrollAu\TimesheetStatus::DRAFT);
 if(count($timelines)>0){                    
                foreach ($timelines as $timeline) {
                    $khours = '';
                    $payItem = $this->xero_model->getPayItem($timeline['EarningsRateKey']); 
                    if(!empty($payItem)) {
                        $timesheetLine = new XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine;
                        $timesheetLine->setEarningsRateId($payItem['EarningsRateID']);
                        foreach ($timeline['hours'] as $hour) { 
                            //$timesheetLine->setNumberOfUnits($hour);
                            $khours .= number_format($hour, 1).',';                          
                        }
                        $khours = rtrim($khours, ',');
                        $timesheetLine->setNumberOfUnits('['.$khours.']');
                        $timesheet->setTimesheetLines(array('TimesheetLines' => $timesheetLine)); 
                                              
                    }
                }                    
            }  
            array_push($emp_timeSheet, $timesheet);
     try { 
        if($timesheet_id == ''){ //print_r($emp_timeSheet); die;
          $result = $apiInstance->createTimesheet($xeroTenantId, $krr);  
        }else{
          $result = $apiInstance->updateTimesheet($xeroTenantId, $timesheet_id, $emp_timeSheet);
        } 

Xero response is below XeroAPI\XeroPHP\ApiException Object ( [responseBody:protected] => 0Bad requestError occurred during JSON de/serialization. Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Paycycle.API.DTO.AU.Timesheet.UpdateTimesheetRequest' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'Timesheets', line 1, position 14. [responseHeaders:protected] => Array ( [Content-Type] => Array ( [0] => text/html )

        [Content-Length] => Array
            (
                [0] => 814
            )

        [Server] => Array
            (
                [0] => nginx
            )

        [Xero-Correlation-Id] => Array
            (
                [0] => 132113121312321211
            )

        [X-AppMinLimit-Remaining] => Array
            (
                [0] => 9998
            )

        [X-MinLimit-Remaining] => Array
            (
                [0] => 58
            )

        [X-DayLimit-Remaining] => Array
            (
                [0] => 4897
            )

        [Expires] => Array
            (
                [0] => Wed, 22 Sep 2021 10:29:05 GMT
            )

        [Cache-Control] => Array
            (
                [0] => max-age=0, no-cache, no-store
            )

        [Pragma] => Array
            (
                [0] => no-cache
            )

        [Date] => Array
            (
                [0] => Wed, 22 Sep 2021 10:29:05 GMT
            )

        [Connection] => Array
            (
                [0] => close
            )

        [X-Client-TLS-ver] => Array
            (
                [0] => tls1.2
            )

    )

[responseObject:protected] => 
[message:protected] => [400] Client error: `POST https://api.xero.com/payroll.xro/1.0/Timesheets` resulted in a `400 Bad Request` response:

0Bad request<Messa (truncated...)

[status] => error [message] => [400] Client error: POST https://api.xero.com/payroll.xro/1.0/Timesheets resulted in a 400 Bad Request response: 0Bad request<Messa (truncated...)

Guri
  • 83
  • 8
  • That message seems to be suggesting that Xero is expecting a JSON-encoded array, but you're not providing one. – droopsnoot Sep 22 '21 at 17:18
  • I think @droopsnoot is right here. The timesheet looks fine, but your timesheetlines code looks a bit funky. Validate that you can create the timesheet with a hardcoded timesheetline, and then debug how you're setting those timesheetlines. – RJaus Sep 22 '21 at 22:59
  • @Guri Did you have any luck figuring this one out? I'm getting a similar with createEmployee – DeanO May 16 '22 at 01:25

0 Answers0