@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.
@@ -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
- const partialOrder = {};
4
- if (tinyOrder.obs_interna) {
5
- partialOrder.staff_notes = tinyOrder.obs_interna;
6
- }
7
- if (shippingLines && shippingLines.length) {
8
- const shippingLine = shippingLines[0];
9
- if ((tinyOrder.codigo_rastreamento || tinyOrder.url_rastreamento)
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
- let link;
12
- if (tinyOrder.url_rastreamento) {
13
- link = tinyOrder.url_rastreamento;
14
- }
15
- const tracking = {
16
- code: String(tinyOrder.codigo_rastreamento)
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
- link,
19
- };
20
- shippingLine.tracking_codes = [tracking];
21
- partialOrder.shipping_lines = shippingLines;
22
- }
23
- if (tinyOrder.id_nota_fiscal > 0) {
24
- if (!shippingLine.invoices) {
25
- shippingLine.invoices = [];
26
- }
27
- postTiny('/nota.fiscal.obter.php', { id: tinyOrder.id_nota_fiscal })
28
- .then((tinyInvoice) => {
29
- const number = String(tinyInvoice.nota_fiscal.numero);
30
- if (number && !shippingLine.invoices.find((invoice) => invoice.number === number)) {
31
- shippingLine.invoices.push({
32
- number,
33
- serial_number: String(tinyInvoice.nota_fiscal.serie),
34
- });
35
- }
36
- partialOrder.shipping_lines = shippingLines;
37
- resolve(partialOrder);
38
- })
39
- .catch(reject);
40
- return;
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
- resolve(partialOrder);
43
+ }
44
+ resolve(partialOrder);
44
45
  });
45
- //# sourceMappingURL=order-from-tiny.js.map
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
- const orderRef = String(order.number) || order._id;
6
- const tinyOrder = {
7
- numero_pedido_ecommerce: orderRef,
8
- data_pedido: formatDate(new Date(order.opened_at || order.created_at)),
9
- ecommerce: 'E-Com Plus',
10
- situacao: parseStatus(order),
11
- itens: [],
12
- };
13
- const buyer = order.buyers && order.buyers[0];
14
- const shippingLine = order.shipping_lines && order.shipping_lines[0];
15
- const transaction = order.transactions && order.transactions[0];
16
- const shippingAddress = shippingLine && shippingLine.to;
17
- const billingAddress = transaction && transaction.billing_address;
18
- const parseAddress = (address, tinyObject) => {
19
- [
20
- ['street', 'endereco', 50],
21
- ['number', 'numero', 10],
22
- ['complement', 'complemento', 50],
23
- ['borough', 'bairro', 30],
24
- ['zip', 'cep', 10],
25
- ['city', 'cidade', 30],
26
- ['province_code', 'uf', 30],
27
- ].forEach(([addressField, tinyField, maxLength]) => {
28
- if (address[addressField]) {
29
- tinyObject[tinyField] = String(address[addressField])
30
- .substring(0, maxLength);
31
- }
32
- });
33
- };
34
- if (buyer) {
35
- const tinyCustomer = {
36
- codigo: buyer._id,
37
- nome: (buyer.corporate_name || ecomUtils.fullName(buyer)).substring(0, 30)
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
- tipo_pessoa: buyer.registry_type === 'j' ? 'J' : 'F',
40
- };
41
- if (buyer.display_name) {
42
- tinyCustomer.nome_fantasia = buyer.display_name.substring(0, 30);
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
- else {
64
- tinyOrder.cliente = {
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 (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);
73
- }
48
+ if (buyer.inscription_number && buyer.inscription_number.length <= 18) {
49
+ tinyCustomer.ie = buyer.inscription_number;
74
50
  }
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
- });
51
+ if (buyer.main_email && buyer.main_email.length <= 50) {
52
+ tinyCustomer.email = buyer.main_email;
90
53
  }
91
- if (order.payment_method_label) {
92
- tinyOrder.meio_pagamento = order.payment_method_label;
54
+ if (shippingAddress) {
55
+ parseAddress(billingAddress || shippingAddress, tinyCustomer);
93
56
  }
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';
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
- if (order.shipping_method_label) {
118
- tinyOrder.forma_frete = order.shipping_method_label;
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
- 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
- }
128
- else if (/b2w/i.test(carrier)) {
129
- tinyOrder.forma_envio = 'B';
130
- }
131
- else if (/mercado envios/i.test(carrier)) {
132
- tinyOrder.forma_envio = 'M';
133
- }
134
- else {
135
- tinyOrder.forma_envio = 'T';
136
- }
137
- }
138
- if ((!tinyOrder.forma_envio || tinyOrder.forma_envio === 'X' || tinyOrder.forma_envio === 'T')
139
- && shippingLine.app.service_name && /(pac|sedex)/i.test(shippingLine.app.service_name)) {
140
- tinyOrder.forma_envio = 'C';
141
- }
142
- if (!tinyOrder.forma_frete && shippingLine.app.label) {
143
- tinyOrder.forma_frete = shippingLine.app.label;
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
- else {
148
- tinyOrder.forma_envio = 'S';
113
+ if (!tinyOrder.meio_pagamento && transaction.payment_method.name) {
114
+ tinyOrder.meio_pagamento = transaction.payment_method.name.substring(0, 100);
149
115
  }
150
- const { amount } = order;
151
- if (amount) {
152
- if (typeof amount.freight === 'number') {
153
- tinyOrder.valor_frete = amount.freight;
154
- if (amount.tax) {
155
- tinyOrder.valor_frete += amount.tax;
156
- }
157
- if (amount.extra) {
158
- tinyOrder.valor_frete += amount.extra;
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
- if (amount.discount) {
162
- tinyOrder.valor_desconto = amount.discount;
163
- }
164
- }
165
- if (order.notes) {
166
- tinyOrder.obs = order.notes.substring(0, 100);
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
- if (order.extra_discount && order.extra_discount.discount_coupon) {
169
- tinyOrder.obs = `${(tinyOrder.obs || '')} - ${order.extra_discount.discount_coupon}`
170
- .substring(0, 100);
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 (order.staff_notes) {
173
- tinyOrder.obs_internas = order.staff_notes.substring(0, 100);
157
+ if (amount.discount) {
158
+ tinyOrder.valor_desconto = amount.discount;
174
159
  }
175
- if (appData.tiny_order_data && typeof appData.tiny_order_data === 'object') {
176
- Object.keys(appData.tiny_order_data).forEach((field) => {
177
- let value = appData.tiny_order_data[field];
178
- switch (value) {
179
- case undefined:
180
- case '':
181
- case null:
182
- break;
183
- default:
184
- if (typeof value === 'string') {
185
- value = value.trim();
186
- if (value) {
187
- tinyOrder[field] = value;
188
- }
189
- }
190
- else {
191
- tinyOrder[field] = value;
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
- return tinyOrder;
185
+ } else {
186
+ tinyOrder[field] = value;
187
+ }
188
+ }
189
+ });
190
+ }
191
+ return tinyOrder;
197
192
  };
198
- //# sourceMappingURL=order-to-tiny.js.map
193
+ // # sourceMappingURL=order-to-tiny.js.map