adds a fixed asset

Adds an asset to the system

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
 * adds a fixed asset
7
 * Adds an asset to the system
8
 */
9
export async function main(
10
  auth: Xero,
11
  xero_tenant_id: string,
12
  Idempotency_Key: string,
13
  body: {
14
    assetId?: string;
15
    assetName: string;
16
    assetTypeId?: string;
17
    assetNumber?: string;
18
    purchaseDate?: string;
19
    purchasePrice?: number;
20
    disposalDate?: string;
21
    disposalPrice?: number;
22
    assetStatus?: "Draft" | "Registered" | "Disposed";
23
    warrantyExpiryDate?: string;
24
    serialNumber?: string;
25
    bookDepreciationSetting?: {
26
      depreciationMethod?:
27
        | "NoDepreciation"
28
        | "StraightLine"
29
        | "DiminishingValue100"
30
        | "DiminishingValue150"
31
        | "DiminishingValue200"
32
        | "FullDepreciation";
33
      averagingMethod?: "FullMonth" | "ActualDays";
34
      depreciationRate?: number;
35
      effectiveLifeYears?: number;
36
      depreciationCalculationMethod?: "Rate" | "Life" | "None";
37
      depreciableObjectId?: string;
38
      depreciableObjectType?: string;
39
      bookEffectiveDateOfChangeId?: string;
40
    };
41
    bookDepreciationDetail?: {
42
      currentCapitalGain?: number;
43
      currentGainLoss?: number;
44
      depreciationStartDate?: string;
45
      costLimit?: number;
46
      residualValue?: number;
47
      priorAccumDepreciationAmount?: number;
48
      currentAccumDepreciationAmount?: number;
49
    };
50
    canRollback?: false | true;
51
    accountingBookValue?: number;
52
    isDeleteEnabledForDate?: false | true;
53
  },
54
) {
55
  const url = new URL(`https://api.xero.com/assets.xro/1.0/Assets`);
56

57
  const response = await fetch(url, {
58
    method: "POST",
59
    headers: {
60
      Accept: 'application/json',
61
      "xero-tenant-id": xero_tenant_id,
62
      "Idempotency-Key": Idempotency_Key,
63
      "Content-Type": "application/json",
64
      Authorization: "Bearer " + auth.token,
65
    },
66
    body: JSON.stringify(body),
67
  });
68
  if (!response.ok) {
69
    const text = await response.text();
70
    throw new Error(`${response.status} ${text}`);
71
  }
72
  return await response.json();
73
}
74