Creates a pay item

Script xero Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 515 days ago
1
//native
2
type Xero = {
3
  token: string;
4
};
5
/**
6
 * Creates a pay item
7
 *
8
 */
9
export async function main(
10
  auth: Xero,
11
  Xero_Tenant_Id: string,
12
  Idempotency_Key: string,
13
  body: {
14
    EarningsRates?: {
15
      Name?: string;
16
      AccountCode?: string;
17
      TypeOfUnits?: string;
18
      IsExemptFromTax?: false | true;
19
      IsExemptFromSuper?: false | true;
20
      IsReportableAsW1?: false | true;
21
      AllowanceContributesToAnnualLeaveRate?: false | true;
22
      AllowanceContributesToOvertimeRate?: false | true;
23
      EarningsType?:
24
        | "FIXED"
25
        | "ORDINARYTIMEEARNINGS"
26
        | "OVERTIMEEARNINGS"
27
        | "ALLOWANCE"
28
        | "LUMPSUMD"
29
        | "EMPLOYMENTTERMINATIONPAYMENT"
30
        | "LUMPSUMA"
31
        | "LUMPSUMB"
32
        | "BONUSESANDCOMMISSIONS"
33
        | "LUMPSUME"
34
        | "LUMPSUMW"
35
        | "DIRECTORSFEES"
36
        | "PAIDPARENTALLEAVE"
37
        | "WORKERSCOMPENSATION";
38
      EarningsRateID?: string;
39
      RateType?: "FIXEDAMOUNT" | "MULTIPLE" | "RATEPERUNIT";
40
      RatePerUnit?: string;
41
      Multiplier?: number;
42
      AccrueLeave?: false | true;
43
      Amount?: number;
44
      EmploymentTerminationPaymentType?: "O" | "R";
45
      UpdatedDateUTC?: string;
46
      CurrentRecord?: false | true;
47
      AllowanceType?:
48
        | "CAR"
49
        | "TRANSPORT"
50
        | "LAUNDRY"
51
        | "MEALS"
52
        | "TRAVEL"
53
        | "OTHER"
54
        | "TOOLS"
55
        | "TASKS"
56
        | "QUALIFICATIONS";
57
      AllowanceCategory?:
58
        | "TRANSPORT"
59
        | "OTHER"
60
        | "NONDEDUCTIBLE"
61
        | "UNIFORM"
62
        | "PRIVATEVEHICLE"
63
        | "HOMEOFFICE"
64
        | "GENERAL";
65
    }[];
66
    DeductionTypes?: {
67
      Name?: string;
68
      AccountCode?: string;
69
      ReducesTax?: false | true;
70
      ReducesSuper?: false | true;
71
      IsExemptFromW1?: false | true;
72
      DeductionTypeID?: string;
73
      UpdatedDateUTC?: string;
74
      DeductionCategory?: "NONE" | "UNIONFEES" | "WORKPLACEGIVING";
75
      CurrentRecord?: false | true;
76
    }[];
77
    LeaveTypes?: {
78
      Name?: string;
79
      TypeOfUnits?: string;
80
      LeaveTypeID?: string;
81
      NormalEntitlement?: number;
82
      LeaveLoadingRate?: number;
83
      UpdatedDateUTC?: string;
84
      IsPaidLeave?: false | true;
85
      ShowOnPayslip?: false | true;
86
      CurrentRecord?: false | true;
87
      LeaveCategoryCode?:
88
        | "ANNUALLEAVE"
89
        | "LONGSERVICELEAVE"
90
        | "PERSONALSICKCARERSLEAVE"
91
        | "ROSTEREDDAYOFF"
92
        | "TIMEOFFINLIEU"
93
        | "COMPASSIONATEANDBEREAVEMENTLEAVE"
94
        | "STUDYLEAVE"
95
        | "FAMILYANDDOMESTICVIOLENCELEAVE"
96
        | "SPECIALPAIDLEAVE"
97
        | "COMMUNITYSERVICELEAVE"
98
        | "JURYDUTYLEAVE"
99
        | "DEFENCERESERVELEAVE";
100
      SGCExempt?: false | true;
101
    }[];
102
    ReimbursementTypes?: {
103
      Name?: string;
104
      AccountCode?: string;
105
      ReimbursementTypeID?: string;
106
      UpdatedDateUTC?: string;
107
      CurrentRecord?: false | true;
108
    }[];
109
  },
110
) {
111
  const url = new URL(`https://api.xero.com/payroll.xro/1.0/PayItems`);
112

113
  const response = await fetch(url, {
114
    method: "POST",
115
    headers: {
116
      Accept: 'application/json',
117
      "Xero-Tenant-Id": Xero_Tenant_Id,
118
      "Idempotency-Key": Idempotency_Key,
119
      "Content-Type": "application/json",
120
      Authorization: "Bearer " + auth.token,
121
    },
122
    body: JSON.stringify(body),
123
  });
124
  if (!response.ok) {
125
    const text = await response.text();
126
    throw new Error(`${response.status} ${text}`);
127
  }
128
  return await response.json();
129
}
130