1

community

I would be grateful if you could help me with Payslips creation via Xero Api? I didn't find any answers to my questions on GitHub, Dev community and official documentation..

If we create PayRun in "DRAFT" status, then PaySlip won't be created:

Related to Xero official documentation: https://developer.xero.com/documentation/api/payrollau/integration-guide/#pay-templates

Any items that are available on the Pay Template will automatically be pre-filled on the PaySlip (including the information from the approved timesheets within the Pay Run window) for scheduled Pay Runs. If there is no Pay Template configured for the employee or if the information needs to be adjusted from the default Pay Template, then Payslips will need to be added/edited using the Payslips endpoint. These modifications must be done while the Pay Run is in DRAFT status.

Note: Unscheduled Pay Runs don’t generate Payslips, so developers will need to do this separately.

In our case Payslips aren't created for a "DRAFT" Pay Runs. So there should be a "CREATE" Api endpoint for payslip as might be assumed. But there is no API endpoint to CREATE new PaySlip for "DRAFT" Pay Run. There's a discrepancy between the Api and the Integration guide.

Realy, no endpoint to create Payslip from API side..

enter image description here

I've checked OpenAPI specification e.t.c and nothing found there either.

Also I've found the same question on Xero developer's forum without an answer https://community.xero.com/developer/discussion/130258174/

So the question is how to create Payslip from the Xero API side?

Helen
  • 87,344
  • 17
  • 243
  • 314
MustDie1Bit
  • 560
  • 4
  • 16

1 Answers1

0

//First you need to get the employees payroll calendar and ordinary earnings rate using their employeeID

$tenantId = $this->user_calendar_setting[$_SESSION['user_id']]['XERO_tenant_id'];
$payrollApi =$this->xero_login($tenantId, "Payroll");
$apiResponse = $payrollApi->getEmployee($tenantId, $employeeID);
$employee = $apiResponse->getEmployees()[0];

$payrollCalendarID = $employee->getPayrollCalendarID();
$ordinaryEarningsRateID = $employee->getOrdinaryEarningsRateID();

$apiResponse = $payrollApi->getPayrollCalendar($tenantId,$payrollCalendarID);
$calendar = $apiResponse->getPayrollCalendars()[0];

//Then 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("APPROVED");
$timesheetLines=array();
$timesheets=array();
$numberOfUnits=array();

//for each pay item you need to create a line in the timesheet

$timesheetLine = new XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine();
$timesheetLine->setEarningsRateId('your xero rate id');
$timesheetLine->setTrackingItemID (NULL);

//for each day in the $lengthofCalendar (ie 7 for a week) you need to add how many hrs

    for ($day = 1; $day <= $lengthofCalendar; $day++) { 
  $numberOfUnits[] = (!empty($_POST["payitem"][$day]) ? $_POST["payitem"][$day] : "0");
  }

$timesheetLine->setNumberOfUnits($numberOfUnits);
array_push($timesheetLines, $timesheetLine);

try{
$apiResponse = $payrollApi->createTimesheet($tenantId,$timesheets);
}catch (Exception $e) {
 error_log(print_r($e, TRUE));
}
DeanO
  • 93
  • 1
  • 10
  • We tried this method for Payroll AU API and unfortunately it doesn't work, the only way you can include an employee in the payrun is by going to xero in the browser viewing the payrun and then clicking to include the employee. Read more here https://productideas.xero.com/forums/939198-for-small-businesses/suggestions/46062550-payroll-au-api-unscheduled-payruns-payslips – plexus Dec 08 '22 at 23:21