Skip to content

Commit 5b320d2

Browse files
committed
0.3.0 Added Sales orders method
1 parent 8fd001e commit 5b320d2

File tree

9 files changed

+296
-44
lines changed

9 files changed

+296
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,5 @@ dist
103103
# TernJS port file
104104
.tern-port
105105

106+
nodemon.json
106107
test.js

README.md

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const fivaldi = new FivaldiApiClient({
4444
timeout: 120000
4545
});
4646
```
47+
4748
## Usage
4849

4950
To make API requests to certain company, the API client needs id for the company, which is called `cuid`.
@@ -53,21 +54,23 @@ Fivaldi API client has few general root methods that you can use without cuid. T
5354
### Root methods
5455

5556
If you don't know the cuid of the company you want to access to with the API, you can fetch it with root methods. Easiest way is to search the company's info by `businessId` (y-tunnus):
57+
5658
```ts
5759
const companyInfo = await fivaldi.getCompanies({ businessId: '1234567-0' });
5860
const cuid = companyInfo.cuid;
5961
fivaldi.setCuid(cuid);
6062
```
61-
If you don't know the business id of the company or you want to see all the customers your partner id has access to, you can use `getCustomers()` method.
6263

64+
If you don't know the business id of the company or you want to see all the customers your partner id has access to, you can use `getCustomers()` method.
6365

64-
:heavy_exclamation_mark: **_NOTE:_** One partner id can have access to multiple customers. One customer can have access to multiple companies.
65-
66+
:heavy*exclamation_mark: \*\*\_NOTE:*\*\* One partner id can have access to multiple customers. One customer can have access to multiple companies.
6667

6768
```ts
6869
const customers = await fivaldi.getCustomers();
6970
```
71+
7072
Then you can use `getCompanies()` again to search all the companies that certain customer has access to.
73+
7174
```ts
7275
// See all the companies chosen customer has access to
7376
const customers = await fivaldi.getCompanies({ customerId: 'CUSTOMER_ID' });
@@ -76,10 +79,13 @@ const customers = await fivaldi.getCompanies({ customerId: 'CUSTOMER_ID' });
7679
// You can then use the business id of the company you want access to and use it to get that company's cuid
7780
const companyInfo = await fivaldi.getCompanies({ businessId: '1234567-0' });
7881
```
82+
7983
After you've gotten the CUID of the company you want to access to, use `setCuid()` method to set the cuid.
84+
8085
```ts
8186
const cuid = fivaldi.setCuid('CUID_OF_THE_COMPANY');
8287
```
88+
8389
After this, you can use all the methods available.
8490

8591
## Implemented methods
@@ -91,6 +97,7 @@ The following API methods have been implemented:
9197
- `purchaseInvoices` Purchase invoices
9298
- `chartOfAccounts` Chart of Accounts
9399
- `archive` Archive of files
100+
- `sales` Sales orders
94101

95102
### Bookkeeping examples
96103

@@ -101,27 +108,27 @@ const vouchers = await fivaldi.bookkeeping.getVouchers({ startDate: '2021-01-01'
101108
// Get a single voucher with voucher number
102109
const voucher = await fivaldi.bookkeeping.getVoucherById({ voucherNumber: '802206001' });
103110

104-
// Create a new voucher and receive
111+
// Create a new voucher and receive
105112
const voucher = {
106-
voucherTypeId: 80,
107-
originalVoucherNumber: '123',
108-
voucherDate: '2022-06-15',
109-
bookkeepingMonth: 202206,
110-
vatType: 'CALCULATED',
111-
validateEntrySum: true,
112-
voucherEntries: [
113-
{
114-
amount: -120,
115-
accountNumber: 3000,
116-
description: 'Entry number 1'
117-
},
118-
{
119-
amount: 120,
120-
accountNumber: 1701,
121-
description: 'Entry number 2'
122-
}
123-
]
124-
};
113+
voucherTypeId: 80,
114+
originalVoucherNumber: '123',
115+
voucherDate: '2022-06-15',
116+
bookkeepingMonth: 202206,
117+
vatType: 'CALCULATED',
118+
validateEntrySum: true,
119+
voucherEntries: [
120+
{
121+
amount: -120,
122+
accountNumber: 3000,
123+
description: 'Entry number 1'
124+
},
125+
{
126+
amount: 120,
127+
accountNumber: 1701,
128+
description: 'Entry number 2'
129+
}
130+
]
131+
};
125132

126133
const voucherNumber = await fivaldi.bookkeeping.createVoucher(voucher);
127134
```
@@ -150,14 +157,13 @@ const product = await fivaldi.products.updateProductAllFields({
150157
});
151158

152159
// Update only the product fields that are given
153-
const
154-
product = await fivaldi.products.updateProduct({ purchaseCostPrice: 155 });
160+
const product = await fivaldi.products.updateProduct({ purchaseCostPrice: 155 });
155161

156162
// Create product language description (translation)
157163
const product = await fivaldi.products.createProductDescription(
158164
{
159165
description: 'Tegelstenar',
160-
languageCode: 'SWE',
166+
languageCode: 'SWE'
161167
},
162168
{ productCode: '123' }
163169
);
@@ -198,31 +204,75 @@ const purchaseInvoiceComments = await fivaldi.purchaseInvoices.getPurchaseInvoic
198204

199205
// Create a new comment to purchase invoice
200206
const purchaseInvoiceComment = await fivaldi.purchaseInvoices.createPurchaseInvoiceComment('123', purchaseInvoiceCommentObject);
201-
202207
```
203208

204209
### Chart of Accounts example
210+
205211
```ts
206212
// Get accounts and dimensions
207213
const chartOfAccounts = await fivaldi.chartOfAccounts.getChartOfAccounts();
208214
```
209215

210216
### Archive of files example
217+
211218
```ts
212219
// Get download url for the file with file id
213220
const downloadUrl = await fivaldi.archive.getFileUrl('123');
214221
```
215222

223+
### Sales orders examples
224+
225+
```ts
226+
// Get company's invoicing details
227+
const invoicingDetails = await fivaldi.sales.getInvoicingDetails();
228+
229+
// Get sales orders by external batch id
230+
const ordersBatch = await fivaldi.sales.getOrdersByBatchId('123');
231+
232+
// Get all sales orders
233+
const ordersAll = await fivaldi.sales.getAllOrders();
234+
235+
// Get sales orders with filters
236+
const ordersFiltered = await fivaldi.sales.getAllOrders({ fromDate: '23.08.2022', status: 'DRAFT' });
237+
238+
// Get sales order by id
239+
const order = await fivaldi.sales.getOrderById('63029cd00000000000000000');
240+
241+
// Update sales order by id
242+
const updatedOrder = await fivaldi.sales.updateOrderById('63029cd00000000000000000', order);
243+
244+
// Create one or multiple sales orders
245+
const { externalBatchId } = await fivaldi.sales.createOrders({
246+
customerId: '1002',
247+
languageCode: 'FIN',
248+
currency: 'EUR',
249+
currencyRate: 1,
250+
paymentTermId: '1',
251+
postingGroupId: '0000000001',
252+
transmissionTypeId: '0000000003',
253+
salesOrderRowDTOS: [
254+
{
255+
productCode: 'M1',
256+
description: 'mallituote 2',
257+
unitPriceExcludingTax: 10,
258+
quantity: 5,
259+
unitId: '2'
260+
}
261+
]
262+
});
263+
```
264+
216265
## Resources
217266

218267
- Fivaldi website: https://www.visma.fi/visma-fivaldi/
219268
- Fivaldi API Documentation: https://ohjeet.visma.fi/articles/#!visma-fivaldi/visma-fivaldi-api
220269
- Fivaldi API Documentation (swagger): https://manuals.fivaldi.net/customer/api/index.html
221-
- Fivaldi login page: https://api2.fivaldi.net/fivaldi-angular/
270+
- Fivaldi login page: https://asp.fivaldi.net/fvlogin/login/connect
222271

223272
## Changelog
224273

225274
- 0.0.1 First release
226275
- 0.1.0 Added Chart of Accounts method and fixed error logic
227276
- 0.1.1 Updated dependencies
228-
- 0.2.0 Added Archive of files method
277+
- 0.2.0 Added Archive of files method
278+
- 0.3.0 Added Sales orders method

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "@rantalainen/fivaldi-api-client",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "Third party Fivaldi API client.",
55
"main": "dist/index.js",
66
"scripts": {
77
"dev": "tsc --watch",
8+
"test": "nodemon test.js",
89
"build": "tsc"
910
},
1011
"repository": {

src/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BookkeepingMethods } from './methods/bookkeeping.methods';
55
import { ChartOfAccountsMethods } from './methods/chart-of-accounts.methods';
66
import { ProductMethods } from './methods/products.methods';
77
import { PurchaseInvoicesMethods } from './methods/purchaseInvoices.methods';
8+
import { SalesMethods } from './methods/sales.methods';
89
import { getHeaders } from './signature';
910

1011
export class FivaldiApiClient {
@@ -15,6 +16,7 @@ export class FivaldiApiClient {
1516
readonly purchaseInvoices: PurchaseInvoicesMethods;
1617
readonly chartOfAccounts: ChartOfAccountsMethods;
1718
readonly archive: ArchiveMethods;
19+
readonly sales: SalesMethods;
1820

1921
constructor(options: IFivaldiApiClientOptions) {
2022
options.apiBaseUrl = options.apiBaseUrl || 'https://api.fivaldi.net/customer/api';
@@ -35,6 +37,7 @@ export class FivaldiApiClient {
3537
this.purchaseInvoices = new PurchaseInvoicesMethods(this);
3638
this.chartOfAccounts = new ChartOfAccountsMethods(this);
3739
this.archive = new ArchiveMethods(this);
40+
this.sales = new SalesMethods(this);
3841
}
3942

4043
async request(method: Method, url: string, body?: any, params?: any): Promise<any> {
@@ -61,11 +64,10 @@ export class FivaldiApiClient {
6164

6265
let response: any;
6366
// If there is a body, parse and return it
64-
if (result.body) {
65-
if (typeof result.body === 'object') response = JSON.parse(result.body);
66-
else response = result.body;
67-
} else {
68-
// Else just return the empty body
67+
try {
68+
response = JSON.parse(result.body);
69+
} catch (e) {
70+
// Else just return the original body
6971
response = result.body;
7072
}
7173

@@ -75,7 +77,7 @@ export class FivaldiApiClient {
7577
}
7678

7779
// Else, throw error with error response included
78-
throw new Error(`HTTP Error: ${result.statusCode} ${result.statusMessage}
80+
throw new Error(`Fivaldi HTTP Error: ${result.statusCode} ${result.statusMessage}
7981
${JSON.stringify(response)}`);
8082
}
8183

src/interfaces/bookkeeping.interfaces.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export interface IFetchVoucherResponse {
7272
voucherEntries: IVoucherEntryResponse[];
7373
}
7474

75-
interface IVoucherEntryResponse {
75+
export interface IVoucherEntryResponse {
7676
/** Voucher entry number inside voucher. */
7777
entryNumber: number;
7878
/** Voucher description. */
@@ -92,7 +92,7 @@ interface IVoucherEntryResponse {
9292
vat: IVoucherEntryVat;
9393
}
9494

95-
interface IVoucherEntryVat {
95+
export interface IVoucherEntryVat {
9696
/** Fivaldi vat code. */
9797
vatCode: string;
9898
vatAmount: number;
@@ -109,7 +109,7 @@ export interface IVatCodeResponse {
109109
vatPercentages: IVatPercentageResponse[];
110110
}
111111

112-
interface IVatPercentageResponse {
112+
export interface IVatPercentageResponse {
113113
/** Vat code in Fivaldi. */
114114
vatPercentageCode: string;
115115
percentage: number;
@@ -159,7 +159,7 @@ export interface IVoucherAttachmentResponse {
159159
archiveObjects: IArchiveObjectResponse[];
160160
}
161161

162-
interface IArchiveObjectResponse {
162+
export interface IArchiveObjectResponse {
163163
objectId: string;
164164
objectDescription: string;
165165
sizeBytes: number;
@@ -183,7 +183,7 @@ export interface IVoucherImportRequest {
183183
voucherEntries: IVoucherEntryRequest[];
184184
}
185185

186-
interface IVoucherEntryRequest {
186+
export interface IVoucherEntryRequest {
187187
/** Amount of voucher entry. */
188188
amount: number;
189189
accountNumber: number;
@@ -196,7 +196,7 @@ interface IVoucherEntryRequest {
196196
vat?: IVoucherEntryVatRequest;
197197
}
198198

199-
interface IVoucherEntryVatRequest {
199+
export interface IVoucherEntryVatRequest {
200200
/** Vat code in Fivaldi. */
201201
vatCode: string;
202202
/** Voucher entry vat amount. */

0 commit comments

Comments
 (0)