calcinai / xero-php

A php library for the Xero API, with a cleaner OAuth interface and ORM-like abstraction.
MIT License
360 stars 260 forks source link

Xero API TimesheetLines #440

Open adblake opened 6 years ago

adblake commented 6 years ago

I can successfully add a blank timesheet for an employee, however when I try to add a timesheet line I am getting a Bad Request response.

` $numberOfUnits = array ("1", "2", "3", "4", "5", "6", "7");

$timesheetLines = new \XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine($xero);

$timesheetLines ->setEarningsRateID ($earningsRateID) ->setTrackingItemID (NULL) ->addNumberOfUnit ($numberOfUnits);

$timesheet = new \XeroPHP\Models\PayrollAU\Timesheet($xero);

$timesheet ->setTimesheetID($timesheetID) ->setEmployeeID ($employeeID) ->setStartDate(new DateTime($startDate)) ->setEndDate(new DateTime($endDate)) ->setStatus("DRAFT") ->addTimesheetLine($timesheetLines);

$timesheet->save(); `

calcinai commented 6 years ago

@adblake Is the generated payload the same as what's in the Xero documentation?

adblake commented 6 years ago

@calcinai yes.

The problem seems to be with the structure of the payload... I would like to send more than one line per timesheet.

If I try:

$TimesheetLines = array("TimesheetLines"=>$TimesheetLine);

before I add the Timesheet line to the timesheet, I get the response:

Catchable fatal error: Argument 1 passed to XeroPHP\Models\PayrollAU\Timesheet::addTimesheetLine() must be an instance of XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine

If I try:

$TimesheetLines = new \XeroPHP\Models\PayrollAU\Timesheet($xero); $TimesheetLines = addTimesheetLine($TimesheetLines);

I get the response:

Fatal error: Call to undefined function addTimesheetLine()

alowaus commented 5 years ago

Did you end up solving the above? I have run into the same issue

adblake commented 5 years ago

Well... I couldn't find any help anywhere! So I came up with a work around that works for me, then I moved on.

` $x = 0;

while( $tsRow= sqlsrv_fetch_array( $tsStmt, SQLSRV_FETCH_ASSOC)) {

$x = $x + 1;

if ( $tsRow['intMonday'] == '0.00') { $tsRow['intMonday'] = 0; }
if ( $tsRow['intTuesday'] == '0.00') { $tsRow['intTuesday'] = 0; }
if ( $tsRow['intWednesday'] == '0.00') { $tsRow['intWednesday'] = 0; }
if ( $tsRow['intThursday'] == '0.00') { $tsRow['intThursday'] = 0; }
if ( $tsRow['intFriday'] == '0.00') { $tsRow['intFriday'] = 0; }
if ( $tsRow['intSaturday'] == '0.00') { $tsRow['intSaturday'] = 0; }
if ( $tsRow['intSunday'] == '0.00') { $tsRow['intSunday'] = 0; }

$TimesheetLine[$x] = new \XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine($xero);

$TimesheetLine[$x]  ->setEarningsRateID ($tsRow['chvExternalUniqueID'])
                ->setTrackingItemID (NULL)
                ->addNumberOfUnit ($tsRow['intMonday'])
                ->addNumberOfUnit ($tsRow['intTuesday'])
                ->addNumberOfUnit ($tsRow['intWednesday'])
                ->addNumberOfUnit ($tsRow['intThursday'])
                ->addNumberOfUnit ($tsRow['intFriday'])
                ->addNumberOfUnit ($tsRow['intSaturday'])
                ->addNumberOfUnit ($tsRow['intSunday']);

} `

` $timesheet = new \XeroPHP\Models\PayrollAU\Timesheet($xero);

if ( $x == 1 ) { $timesheet ->setEmployeeID ($row['chvExternalUniqueID']) ->setStartDate(new DateTime($_REQUEST['dteStartDate'])) ->setEndDate(new DateTime($_REQUEST['dteEndDate'])) ->setStatus("DRAFT") ->addTimesheetLine($TimesheetLine[1]); } elseif ( $x == 2 ) { $timesheet ->setEmployeeID ($row['chvExternalUniqueID']) ->setStartDate(new DateTime($_REQUEST['dteStartDate'])) ->setEndDate(new DateTime($_REQUEST['dteEndDate'])) ->setStatus("DRAFT") ->addTimesheetLine($TimesheetLine[1]) ->addTimesheetLine($TimesheetLine[2]); } elseif ( $x == 3 ) { $timesheet ->setEmployeeID ($row['chvExternalUniqueID']) ->setStartDate(new DateTime($_REQUEST['dteStartDate'])) ->setEndDate(new DateTime($_REQUEST['dteEndDate'])) ->setStatus("DRAFT") ->addTimesheetLine($TimesheetLine[1]) ->addTimesheetLine($TimesheetLine[2]) ->addTimesheetLine($TimesheetLine[3]); } elseif ( $x == 4 ) { $timesheet ->setEmployeeID ($row['chvExternalUniqueID']) ->setStartDate(new DateTime($_REQUEST['dteStartDate'])) ->setEndDate(new DateTime($_REQUEST['dteEndDate'])) ->setStatus("DRAFT") ->addTimesheetLine($TimesheetLine[1]) ->addTimesheetLine($TimesheetLine[2]) ->addTimesheetLine($TimesheetLine[3]) ->addTimesheetLine($TimesheetLine[4]); } elseif ( $x == 5 ) { $timesheet ->setEmployeeID ($row['chvExternalUniqueID']) ->setStartDate(new DateTime($_REQUEST['dteStartDate'])) ->setEndDate(new DateTime($_REQUEST['dteEndDate'])) ->setStatus("DRAFT") ->addTimesheetLine($TimesheetLine[1]) ->addTimesheetLine($TimesheetLine[2]) ->addTimesheetLine($TimesheetLine[3]) ->addTimesheetLine($TimesheetLine[4]) ->addTimesheetLine($TimesheetLine[5]); } elseif ( $x == 6 ) { $timesheet ->setEmployeeID ($row['chvExternalUniqueID']) ->setStartDate(new DateTime($_REQUEST['dteStartDate'])) ->setEndDate(new DateTime($_REQUEST['dteEndDate'])) ->setStatus("DRAFT") ->addTimesheetLine($TimesheetLine[1]) ->addTimesheetLine($TimesheetLine[2]) ->addTimesheetLine($TimesheetLine[3]) ->addTimesheetLine($TimesheetLine[4]) ->addTimesheetLine($TimesheetLine[5]) ->addTimesheetLine($TimesheetLine[6]); } elseif ( $x == 7 ) { $timesheet ->setEmployeeID ($row['chvExternalUniqueID']) ->setStartDate(new DateTime($_REQUEST['dteStartDate'])) ->setEndDate(new DateTime($_REQUEST['dteEndDate'])) ->setStatus("DRAFT") ->addTimesheetLine($TimesheetLine[1]) ->addTimesheetLine($TimesheetLine[2]) ->addTimesheetLine($TimesheetLine[3]) ->addTimesheetLine($TimesheetLine[4]) ->addTimesheetLine($TimesheetLine[5]) ->addTimesheetLine($TimesheetLine[6]) ->addTimesheetLine($TimesheetLine[7]); } else { $timesheet ->setEmployeeID ($row['chvExternalUniqueID']) ->setStartDate(new DateTime($_REQUEST['dteStartDate'])) ->setEndDate(new DateTime($_REQUEST['dteEndDate'])) ->setStatus("DRAFT") ->addTimesheetLine($TimesheetLine[1]) ->addTimesheetLine($TimesheetLine[2]) ->addTimesheetLine($TimesheetLine[3]) ->addTimesheetLine($TimesheetLine[4]) ->addTimesheetLine($TimesheetLine[5]) ->addTimesheetLine($TimesheetLine[6]) ->addTimesheetLine($TimesheetLine[7]) ->addTimesheetLine($TimesheetLine[8]); }

$timesheet ->save(); `

alowaus commented 5 years ago

Thanks Adam. That has helped me figured out where I was going wrong.

The number of addNumberOfUnit must match how many days you have defined in your timesheet period. So for the month of February (Start Date 01/02/2019 till End Date 28/02/2019) I had to add 28 values via addNumberOfUnit. You helped me get over my blocker :)

simerkaur commented 5 years ago

@calcinai yes.

The problem seems to be with the structure of the payload... I would like to send more than one line per timesheet.

If I try:

$TimesheetLines = array("TimesheetLines"=>$TimesheetLine);

before I add the Timesheet line to the timesheet, I get the response:

Catchable fatal error: Argument 1 passed to XeroPHP\Models\PayrollAU\Timesheet::addTimesheetLine() must be an instance of XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine

If I try:

$TimesheetLines = new \XeroPHP\Models\PayrollAU\Timesheet($xero); $TimesheetLines = addTimesheetLine($TimesheetLines);

I get the response:

Fatal error: Call to undefined function addTimesheetLine()

Does it not need to be : $TimesheetLines = array("TimesheetLines"=>$TimesheetLine);`

$Timesheet = new \XeroPHP\Models\PayrollAU\Timesheet($xero); $Timesheet->addTimesheetLine($TimesheetLines); $Timesheet->save();