@cloudcommerce/app-tiny-erp 0.0.107 → 0.0.109

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,193 +1,198 @@
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
-
5
4
  export default (order, appData) => {
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)
39
- || `Comprador de #${orderRef}`,
40
- tipo_pessoa: buyer.registry_type === 'j' ? 'J' : 'F',
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
+ });
41
33
  };
42
- if (buyer.display_name) {
43
- tinyCustomer.nome_fantasia = buyer.display_name.substring(0, 30);
34
+ if (buyer) {
35
+ const tinyCustomer = {
36
+ codigo: buyer._id,
37
+ nome: (buyer.corporate_name || ecomUtils.fullName(buyer)).substring(0, 30)
38
+ || `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;
44
62
  }
45
- if (buyer.doc_number && buyer.doc_number.length <= 18) {
46
- tinyCustomer.cpf_cnpj = buyer.doc_number;
63
+ else {
64
+ tinyOrder.cliente = {
65
+ nome: `Comprador de #${orderRef}`,
66
+ };
47
67
  }
48
- if (buyer.inscription_number && buyer.inscription_number.length <= 18) {
49
- tinyCustomer.ie = buyer.inscription_number;
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
+ }
50
74
  }
51
- if (buyer.main_email && buyer.main_email.length <= 50) {
52
- tinyCustomer.email = buyer.main_email;
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
+ });
53
90
  }
54
- if (shippingAddress) {
55
- parseAddress(billingAddress || shippingAddress, tinyCustomer);
91
+ if (order.payment_method_label) {
92
+ tinyOrder.meio_pagamento = order.payment_method_label;
56
93
  }
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;
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
+ }
61
116
  }
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);
117
+ if (order.shipping_method_label) {
118
+ tinyOrder.forma_frete = order.shipping_method_label;
73
119
  }
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';
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
+ }
112
146
  }
113
- if (!tinyOrder.meio_pagamento && transaction.payment_method.name) {
114
- tinyOrder.meio_pagamento = transaction.payment_method.name.substring(0, 100);
147
+ else {
148
+ tinyOrder.forma_envio = 'S';
115
149
  }
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';
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
+ }
160
+ }
161
+ if (amount.discount) {
162
+ tinyOrder.valor_desconto = amount.discount;
133
163
  }
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
- }
142
164
  }
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
- }
165
+ if (order.notes) {
166
+ tinyOrder.obs = order.notes.substring(0, 100);
156
167
  }
157
- if (amount.discount) {
158
- tinyOrder.valor_desconto = amount.discount;
168
+ if (order.extra_discount && order.extra_discount.discount_coupon) {
169
+ tinyOrder.obs = `${(tinyOrder.obs || '')} - ${order.extra_discount.discount_coupon}`
170
+ .substring(0, 100);
159
171
  }
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;
172
+ if (order.staff_notes) {
173
+ tinyOrder.obs_internas = order.staff_notes.substring(0, 100);
174
+ }
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
+ }
184
193
  }
185
- } else {
186
- tinyOrder[field] = value;
187
- }
188
- }
189
- });
190
- }
191
- return tinyOrder;
194
+ });
195
+ }
196
+ return tinyOrder;
192
197
  };
193
- // # sourceMappingURL=order-to-tiny.js.map
198
+ //# sourceMappingURL=order-to-tiny.js.map