@cloudcommerce/app-tiny-erp 0.0.109 → 0.0.111
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +25 -5
- package/lib/event-to-tiny.js +94 -94
- package/lib/index.js +1 -1
- package/lib/integration/after-tiny-queue.js +71 -74
- package/lib/integration/export-order-to-tiny.js +70 -73
- package/lib/integration/export-product-to-tiny.js +49 -53
- package/lib/integration/helpers/format-tiny-date.js +3 -3
- package/lib/integration/import-order-from-tiny.js +76 -75
- package/lib/integration/import-product-from-tiny.js +137 -140
- package/lib/integration/parsers/order-from-tiny.js +40 -39
- package/lib/integration/parsers/order-to-tiny.js +173 -178
- package/lib/integration/parsers/product-from-tiny.js +173 -171
- package/lib/integration/parsers/product-to-tiny.js +123 -127
- package/lib/integration/parsers/status-from-tiny.js +32 -32
- package/lib/integration/parsers/status-to-tiny.js +37 -37
- package/lib/integration/post-tiny-erp.js +42 -43
- package/lib/tiny-erp.js +8 -6
- package/lib/tiny-webhook.js +76 -73
- package/package.json +4 -4
|
@@ -1,45 +1,46 @@
|
|
|
1
1
|
import postTiny from '../post-tiny-erp.js';
|
|
2
|
+
|
|
2
3
|
export default (tinyOrder, shippingLines) => new Promise((resolve, reject) => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
const partialOrder = {};
|
|
5
|
+
if (tinyOrder.obs_interna) {
|
|
6
|
+
partialOrder.staff_notes = tinyOrder.obs_interna;
|
|
7
|
+
}
|
|
8
|
+
if (shippingLines && shippingLines.length) {
|
|
9
|
+
const shippingLine = shippingLines[0];
|
|
10
|
+
if ((tinyOrder.codigo_rastreamento || tinyOrder.url_rastreamento)
|
|
10
11
|
&& (!shippingLine.tracking_codes || !shippingLine.tracking_codes.length)) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
let link;
|
|
13
|
+
if (tinyOrder.url_rastreamento) {
|
|
14
|
+
link = tinyOrder.url_rastreamento;
|
|
15
|
+
}
|
|
16
|
+
const tracking = {
|
|
17
|
+
code: String(tinyOrder.codigo_rastreamento)
|
|
17
18
|
|| link.replace(/^https?:\/\/[^/]+/, '').replace(/^[^?]+\?/, '').substring(0, 70),
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
19
|
+
link,
|
|
20
|
+
};
|
|
21
|
+
shippingLine.tracking_codes = [tracking];
|
|
22
|
+
partialOrder.shipping_lines = shippingLines;
|
|
23
|
+
}
|
|
24
|
+
if (tinyOrder.id_nota_fiscal > 0) {
|
|
25
|
+
if (!shippingLine.invoices) {
|
|
26
|
+
shippingLine.invoices = [];
|
|
27
|
+
}
|
|
28
|
+
postTiny('/nota.fiscal.obter.php', { id: tinyOrder.id_nota_fiscal })
|
|
29
|
+
.then((tinyInvoice) => {
|
|
30
|
+
const number = String(tinyInvoice.nota_fiscal.numero);
|
|
31
|
+
if (number && !shippingLine.invoices.find((invoice) => invoice.number === number)) {
|
|
32
|
+
shippingLine.invoices.push({
|
|
33
|
+
number,
|
|
34
|
+
serial_number: String(tinyInvoice.nota_fiscal.serie),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
partialOrder.shipping_lines = shippingLines;
|
|
38
|
+
resolve(partialOrder);
|
|
39
|
+
})
|
|
40
|
+
.catch(reject);
|
|
41
|
+
return;
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
}
|
|
44
|
+
resolve(partialOrder);
|
|
44
45
|
});
|
|
45
|
-
|
|
46
|
+
// # sourceMappingURL=order-from-tiny.js.map
|
|
@@ -1,198 +1,193 @@
|
|
|
1
1
|
import ecomUtils from '@ecomplus/utils';
|
|
2
2
|
import formatDate from '../helpers/format-tiny-date.js';
|
|
3
3
|
import parseStatus from './status-to-tiny.js';
|
|
4
|
+
|
|
4
5
|
export default (order, appData) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
6
|
+
const orderRef = String(order.number) || order._id;
|
|
7
|
+
const tinyOrder = {
|
|
8
|
+
numero_pedido_ecommerce: orderRef,
|
|
9
|
+
data_pedido: formatDate(new Date(order.opened_at || order.created_at)),
|
|
10
|
+
ecommerce: 'E-Com Plus',
|
|
11
|
+
situacao: parseStatus(order),
|
|
12
|
+
itens: [],
|
|
13
|
+
};
|
|
14
|
+
const buyer = order.buyers && order.buyers[0];
|
|
15
|
+
const shippingLine = order.shipping_lines && order.shipping_lines[0];
|
|
16
|
+
const transaction = order.transactions && order.transactions[0];
|
|
17
|
+
const shippingAddress = shippingLine && shippingLine.to;
|
|
18
|
+
const billingAddress = transaction && transaction.billing_address;
|
|
19
|
+
const parseAddress = (address, tinyObject) => {
|
|
20
|
+
[
|
|
21
|
+
['street', 'endereco', 50],
|
|
22
|
+
['number', 'numero', 10],
|
|
23
|
+
['complement', 'complemento', 50],
|
|
24
|
+
['borough', 'bairro', 30],
|
|
25
|
+
['zip', 'cep', 10],
|
|
26
|
+
['city', 'cidade', 30],
|
|
27
|
+
['province_code', 'uf', 30],
|
|
28
|
+
].forEach(([addressField, tinyField, maxLength]) => {
|
|
29
|
+
if (address[addressField]) {
|
|
30
|
+
tinyObject[tinyField] = String(address[addressField])
|
|
31
|
+
.substring(0, maxLength);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
if (buyer) {
|
|
36
|
+
const tinyCustomer = {
|
|
37
|
+
codigo: buyer._id,
|
|
38
|
+
nome: (buyer.corporate_name || ecomUtils.fullName(buyer)).substring(0, 30)
|
|
38
39
|
|| `Comprador de #${orderRef}`,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
if (buyer.doc_number && buyer.doc_number.length <= 18) {
|
|
45
|
-
tinyCustomer.cpf_cnpj = buyer.doc_number;
|
|
46
|
-
}
|
|
47
|
-
if (buyer.inscription_number && buyer.inscription_number.length <= 18) {
|
|
48
|
-
tinyCustomer.ie = buyer.inscription_number;
|
|
49
|
-
}
|
|
50
|
-
if (buyer.main_email && buyer.main_email.length <= 50) {
|
|
51
|
-
tinyCustomer.email = buyer.main_email;
|
|
52
|
-
}
|
|
53
|
-
if (shippingAddress) {
|
|
54
|
-
parseAddress(billingAddress || shippingAddress, tinyCustomer);
|
|
55
|
-
}
|
|
56
|
-
const phone = buyer.phones && buyer.phones[0];
|
|
57
|
-
if (phone) {
|
|
58
|
-
tinyCustomer.fone = phone.country_code ? `+${phone.country_code} ` : '';
|
|
59
|
-
tinyCustomer.fone += phone.number;
|
|
60
|
-
}
|
|
61
|
-
tinyOrder.cliente = tinyCustomer;
|
|
40
|
+
tipo_pessoa: buyer.registry_type === 'j' ? 'J' : 'F',
|
|
41
|
+
};
|
|
42
|
+
if (buyer.display_name) {
|
|
43
|
+
tinyCustomer.nome_fantasia = buyer.display_name.substring(0, 30);
|
|
62
44
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
nome: `Comprador de #${orderRef}`,
|
|
66
|
-
};
|
|
45
|
+
if (buyer.doc_number && buyer.doc_number.length <= 18) {
|
|
46
|
+
tinyCustomer.cpf_cnpj = buyer.doc_number;
|
|
67
47
|
}
|
|
68
|
-
if (
|
|
69
|
-
|
|
70
|
-
parseAddress(shippingAddress, tinyOrder.endereco_entrega);
|
|
71
|
-
if (shippingAddress.name) {
|
|
72
|
-
tinyOrder.endereco_entrega.nome_destinatario = shippingAddress.name.substring(0, 60);
|
|
73
|
-
}
|
|
48
|
+
if (buyer.inscription_number && buyer.inscription_number.length <= 18) {
|
|
49
|
+
tinyCustomer.ie = buyer.inscription_number;
|
|
74
50
|
}
|
|
75
|
-
if (
|
|
76
|
-
|
|
77
|
-
if (item.quantity) {
|
|
78
|
-
const itemRef = (item.sku || item._id || Math.random().toString()).substring(0, 30);
|
|
79
|
-
tinyOrder.itens.push({
|
|
80
|
-
item: {
|
|
81
|
-
codigo: itemRef,
|
|
82
|
-
descricao: item.name ? item.name.substring(0, 120) : itemRef,
|
|
83
|
-
unidade: 'UN',
|
|
84
|
-
quantidade: item.quantity,
|
|
85
|
-
valor_unitario: ecomUtils.price(item),
|
|
86
|
-
},
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
});
|
|
51
|
+
if (buyer.main_email && buyer.main_email.length <= 50) {
|
|
52
|
+
tinyCustomer.email = buyer.main_email;
|
|
90
53
|
}
|
|
91
|
-
if (
|
|
92
|
-
|
|
54
|
+
if (shippingAddress) {
|
|
55
|
+
parseAddress(billingAddress || shippingAddress, tinyCustomer);
|
|
93
56
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
break;
|
|
99
|
-
case 'banking_billet':
|
|
100
|
-
tinyOrder.forma_pagamento = 'boleto';
|
|
101
|
-
break;
|
|
102
|
-
case 'account_deposit':
|
|
103
|
-
tinyOrder.forma_pagamento = 'deposito';
|
|
104
|
-
break;
|
|
105
|
-
case 'online_debit':
|
|
106
|
-
case 'debit_card':
|
|
107
|
-
case 'balance_on_intermediary':
|
|
108
|
-
tinyOrder.forma_pagamento = 'debito';
|
|
109
|
-
break;
|
|
110
|
-
default:
|
|
111
|
-
tinyOrder.forma_pagamento = 'multiplas';
|
|
112
|
-
}
|
|
113
|
-
if (!tinyOrder.meio_pagamento && transaction.payment_method.name) {
|
|
114
|
-
tinyOrder.meio_pagamento = transaction.payment_method.name.substring(0, 100);
|
|
115
|
-
}
|
|
57
|
+
const phone = buyer.phones && buyer.phones[0];
|
|
58
|
+
if (phone) {
|
|
59
|
+
tinyCustomer.fone = phone.country_code ? `+${phone.country_code} ` : '';
|
|
60
|
+
tinyCustomer.fone += phone.number;
|
|
116
61
|
}
|
|
117
|
-
|
|
118
|
-
|
|
62
|
+
tinyOrder.cliente = tinyCustomer;
|
|
63
|
+
} else {
|
|
64
|
+
tinyOrder.cliente = {
|
|
65
|
+
nome: `Comprador de #${orderRef}`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (shippingAddress && billingAddress) {
|
|
69
|
+
tinyOrder.endereco_entrega = {};
|
|
70
|
+
parseAddress(shippingAddress, tinyOrder.endereco_entrega);
|
|
71
|
+
if (shippingAddress.name) {
|
|
72
|
+
tinyOrder.endereco_entrega.nome_destinatario = shippingAddress.name.substring(0, 60);
|
|
119
73
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
74
|
+
}
|
|
75
|
+
if (order.items) {
|
|
76
|
+
order.items.forEach((item) => {
|
|
77
|
+
if (item.quantity) {
|
|
78
|
+
const itemRef = (item.sku || item._id || Math.random().toString()).substring(0, 30);
|
|
79
|
+
tinyOrder.itens.push({
|
|
80
|
+
item: {
|
|
81
|
+
codigo: itemRef,
|
|
82
|
+
descricao: item.name ? item.name.substring(0, 120) : itemRef,
|
|
83
|
+
unidade: 'UN',
|
|
84
|
+
quantidade: item.quantity,
|
|
85
|
+
valor_unitario: ecomUtils.price(item),
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
if (order.payment_method_label) {
|
|
92
|
+
tinyOrder.meio_pagamento = order.payment_method_label;
|
|
93
|
+
}
|
|
94
|
+
if (transaction) {
|
|
95
|
+
switch (transaction.payment_method.code) {
|
|
96
|
+
case 'credit_card':
|
|
97
|
+
tinyOrder.forma_pagamento = 'credito';
|
|
98
|
+
break;
|
|
99
|
+
case 'banking_billet':
|
|
100
|
+
tinyOrder.forma_pagamento = 'boleto';
|
|
101
|
+
break;
|
|
102
|
+
case 'account_deposit':
|
|
103
|
+
tinyOrder.forma_pagamento = 'deposito';
|
|
104
|
+
break;
|
|
105
|
+
case 'online_debit':
|
|
106
|
+
case 'debit_card':
|
|
107
|
+
case 'balance_on_intermediary':
|
|
108
|
+
tinyOrder.forma_pagamento = 'debito';
|
|
109
|
+
break;
|
|
110
|
+
default:
|
|
111
|
+
tinyOrder.forma_pagamento = 'multiplas';
|
|
146
112
|
}
|
|
147
|
-
|
|
148
|
-
|
|
113
|
+
if (!tinyOrder.meio_pagamento && transaction.payment_method.name) {
|
|
114
|
+
tinyOrder.meio_pagamento = transaction.payment_method.name.substring(0, 100);
|
|
149
115
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
116
|
+
}
|
|
117
|
+
if (order.shipping_method_label) {
|
|
118
|
+
tinyOrder.forma_frete = order.shipping_method_label;
|
|
119
|
+
}
|
|
120
|
+
if (shippingLine) {
|
|
121
|
+
tinyOrder.forma_envio = 'X';
|
|
122
|
+
if (shippingLine.app) {
|
|
123
|
+
const { carrier } = shippingLine.app;
|
|
124
|
+
if (carrier) {
|
|
125
|
+
if (/correios/i.test(carrier)) {
|
|
126
|
+
tinyOrder.forma_envio = 'C';
|
|
127
|
+
} else if (/b2w/i.test(carrier)) {
|
|
128
|
+
tinyOrder.forma_envio = 'B';
|
|
129
|
+
} else if (/mercado envios/i.test(carrier)) {
|
|
130
|
+
tinyOrder.forma_envio = 'M';
|
|
131
|
+
} else {
|
|
132
|
+
tinyOrder.forma_envio = 'T';
|
|
160
133
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
134
|
+
}
|
|
135
|
+
if ((!tinyOrder.forma_envio || tinyOrder.forma_envio === 'X' || tinyOrder.forma_envio === 'T')
|
|
136
|
+
&& shippingLine.app.service_name && /(pac|sedex)/i.test(shippingLine.app.service_name)) {
|
|
137
|
+
tinyOrder.forma_envio = 'C';
|
|
138
|
+
}
|
|
139
|
+
if (!tinyOrder.forma_frete && shippingLine.app.label) {
|
|
140
|
+
tinyOrder.forma_frete = shippingLine.app.label;
|
|
141
|
+
}
|
|
167
142
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
143
|
+
} else {
|
|
144
|
+
tinyOrder.forma_envio = 'S';
|
|
145
|
+
}
|
|
146
|
+
const { amount } = order;
|
|
147
|
+
if (amount) {
|
|
148
|
+
if (typeof amount.freight === 'number') {
|
|
149
|
+
tinyOrder.valor_frete = amount.freight;
|
|
150
|
+
if (amount.tax) {
|
|
151
|
+
tinyOrder.valor_frete += amount.tax;
|
|
152
|
+
}
|
|
153
|
+
if (amount.extra) {
|
|
154
|
+
tinyOrder.valor_frete += amount.extra;
|
|
155
|
+
}
|
|
171
156
|
}
|
|
172
|
-
if (
|
|
173
|
-
|
|
157
|
+
if (amount.discount) {
|
|
158
|
+
tinyOrder.valor_desconto = amount.discount;
|
|
174
159
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
160
|
+
}
|
|
161
|
+
if (order.notes) {
|
|
162
|
+
tinyOrder.obs = order.notes.substring(0, 100);
|
|
163
|
+
}
|
|
164
|
+
if (order.extra_discount && order.extra_discount.discount_coupon) {
|
|
165
|
+
tinyOrder.obs = `${(tinyOrder.obs || '')} - ${order.extra_discount.discount_coupon}`
|
|
166
|
+
.substring(0, 100);
|
|
167
|
+
}
|
|
168
|
+
if (order.staff_notes) {
|
|
169
|
+
tinyOrder.obs_internas = order.staff_notes.substring(0, 100);
|
|
170
|
+
}
|
|
171
|
+
if (appData.tiny_order_data && typeof appData.tiny_order_data === 'object') {
|
|
172
|
+
Object.keys(appData.tiny_order_data).forEach((field) => {
|
|
173
|
+
let value = appData.tiny_order_data[field];
|
|
174
|
+
switch (value) {
|
|
175
|
+
case undefined:
|
|
176
|
+
case '':
|
|
177
|
+
case null:
|
|
178
|
+
break;
|
|
179
|
+
default:
|
|
180
|
+
if (typeof value === 'string') {
|
|
181
|
+
value = value.trim();
|
|
182
|
+
if (value) {
|
|
183
|
+
tinyOrder[field] = value;
|
|
193
184
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
185
|
+
} else {
|
|
186
|
+
tinyOrder[field] = value;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return tinyOrder;
|
|
197
192
|
};
|
|
198
|
-
|
|
193
|
+
// # sourceMappingURL=order-to-tiny.js.map
|