Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fluxo offline para cancelar cupom no SAT #132

Open
wants to merge 4 commits into
base: 12.0-mig-l10n_br_pos
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion l10n_br_pos/models/pos_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ def _process_order(self, pos_order_vals):
order.document_file_id = order._save_attachment(
file_name=order.document_key + ".xml", file_content=document_file
).id

cancel_document_file = pos_order_vals.get("cancel_document_file")
if cancel_document_file:
order.cancel_document_file_id = order._save_attachment(
file_name=order.cancel_document_key + ".xml",
file_content=cancel_document_file
).id

self._refund_cancelled_order(order)

return order

@api.model
Expand All @@ -224,6 +234,13 @@ def _order_fields(self, ui_order):
"document_serie": key.numero_serie,
}
)

if ui_order.get("cancel_document_key"):
temp.update({
"cancel_document_session_number": ui_order["cancel_document_session_number"],
"cancel_document_key": ui_order["cancel_document_key"],
})

result.update(temp)
return result

Expand Down Expand Up @@ -431,14 +448,20 @@ def _populate_cancel_order_fields(self, order_vals):
@api.model
def cancelar_order(self, result):
_logger.info(f'Result: {result}')
order = self.browse(result['order_id'])
if result.get('order_id'):
order = self.browse(result['order_id'])
else:
order = self.search([('document_key', '=', result['chave_consulta'])])

order._populate_cancel_order_fields(result)
order.cancel_document_file_id = order._save_attachment(
file_name=result['chave_cfe'] + ".xml",
file_content=result['xml']
).id

self._refund_cancelled_order(order)

def _refund_cancelled_order(self, order):
order.with_context(
mail_create_nolog=True, tracking_disable=True,
mail_create_nosubscribe=True, mail_notrack=True
Expand Down
74 changes: 56 additions & 18 deletions l10n_br_pos/static/src/js/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,37 +206,75 @@ odoo.define("l10n_br_pos.devices", function (require) {
},
cancel_order: function (order) {
var self = this;
var paid_order = self.pos.paid_orders.find(x => x.id === order.id);
order['cnpj_software_house'] = self.pos.config.cnpj_software_house;
self.pos.proxy.message("sessao_sat", {json: self.pos.config.sessao_sat}, {timeout: 5000}).then(
function (result) {
if (result.num_sessao != self.pos.config.sessao_sat) {
self.message('cancelar_cfe', {json: order}, {timeout: 5000})
.then(function (result) {
.then(function (retorno_sat) {
let result = retorno_sat;
if (result && typeof result === 'object') {
rpc.query({
model: 'pos.order',
method: 'cancelar_order',
args: [result],
}).then(function (orders) {
self.pos.gui.show_popup('error', {
message: _t('Venda Cancelada!'),
comment: _t('A venda foi cancelada com sucesso.'),
});
if ($('.js_synch .js_connected').hasClass('.hidden')) {
lfdivino marked this conversation as resolved.
Show resolved Hide resolved
rpc.query({
model: 'pos.order',
method: 'cancelar_order',
args: [result],
}).then(function (orders) {
self.pos.gui.show_popup('error', {
message: _t('Venda Cancelada!'),
comment: _t('A venda foi cancelada com sucesso.'),
});
var config_id = self.pos.config.id;
self.pos.config.sessao_sat++;
rpc.query({
model: 'pos.config',
method: 'update_sessao_sat',
args: [config_id]
})
}, function (error, event) {
event.preventDefault();
self.pos.gui.show_popup('error', {
'title': _t('Error: Tempo Excedido'),
'body': _t('Tempo limite de 30 minutos para cancelamento foi excedido.'),
});
return false;
});
});
} else {
var json_orders = JSON.parse(self.pos.export_paid_orders());
var cancelled_order = null;
for (var index in json_orders.paid_orders) {
if (json_orders.paid_orders[index].data.document_key == order['chaveConsulta']) {
cancelled_order = json_orders.paid_orders[index].data;
}
}
if (cancelled_order) {
cancelled_order.cancel_document_session_number = result['numSessao'];
cancelled_order.cancel_document_key = result['chave_cfe'];
cancelled_order.cancel_document_file = result['xml'];
var json_as_str = JSON.stringify(json_orders);
self.pos.import_orders(json_as_str);

var config_id = self.pos.config.id;
self.pos.config.sessao_sat++;
rpc.query({
model: 'pos.config',
method: 'update_sessao_sat',
args: [config_id]
})
} else {
rpc.query({
model: 'pos.order',
method: 'cancelar_order',
args: [result],
}).then(function (orders) {
self.pos.gui.show_popup('error', {
message: _t('Venda Cancelada!'),
comment: _t('A venda foi cancelada com sucesso.'),
});
var config_id = self.pos.config.id;
self.pos.config.sessao_sat++;
rpc.query({
model: 'pos.config',
method: 'update_sessao_sat',
args: [config_id]
})
});
}
}
} else {
self.pos.gui.show_popup('error', {
'tittle': _t('Erro SAT: '),
Expand Down
3 changes: 3 additions & 0 deletions l10n_br_pos/static/src/js/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ odoo.define("l10n_br_pos.models", function (require) {
this.document_session_number = this.document_session_number || null;
this.document_key = this.document_key || null;
this.document_file = this.document_file || null;
this.cancel_document_session_number = this.cancel_document_session_number || null;
this.cancel_document_key = this.cancel_document_key || null;
this.cancel_document_file = this.cancel_document_file || null;

if (options.json) {
this.init_from_JSON(options.json);
Expand Down
53 changes: 45 additions & 8 deletions l10n_br_pos/static/src/js/screens.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ odoo.define("l10n_br_pos.screens", function (require) {
(response) => {
console.log(response);
order.set_cfe_return(response);
self.pos.paid_orders.splice(0, 0, order);
resolve(true);
},
(error) => {
Expand Down Expand Up @@ -136,23 +137,47 @@ odoo.define("l10n_br_pos.screens", function (require) {
var self = this;
this._super();
this.$('.order-list-contents').delegate('.pos_order_reprint','click',function(event){
let selected_order_id = $(this).parent().parent().data('id');
rpc.query({
model: 'pos.order',
method: 'retornar_order_by_id',
args: [$(this).parent().parent().data('id')],
args: [selected_order_id],
limit: 1,
}).then(function (orders){
self.pos.proxy.reprint_cfe(orders);
}, function (error, event) {
let order = self.pos.paid_orders.find(x => x.id === selected_order_id);
if (order.hasOwnProperty("document_file") && order.document_file) {
let orderSatObj = {
'order_id': order.id,
'chaveConsulta': order.document_key,
'doc_destinatario': order.cnpj_cpf || '',
'xml_cfe_venda': order.document_file,
}
self.pos.proxy.reprint_cfe(orderSatObj);
}
});
});
this.$('.order-list-contents').delegate('.pos_order_cancel','click',function(event){
let selected_order_id = $(this).parent().parent().data('id');
rpc.query({
model: 'pos.order',
method: 'retornar_order_by_id',
args: [$(this).parent().parent().data('id')],
args: [selected_order_id],
limit: 1,
}).then(function (orders){
self.cancel_order_sat(orders);
}, function (error, event) {
let order = self.pos.paid_orders.find(x => x.id === selected_order_id);
if (order.hasOwnProperty("document_key") && order.document_key) {
let orderSatObj = {
'order_id': order.id,
'chaveConsulta': order.document_key,
'doc_destinatario': order.cnpj_cpf || '',
'xml_cfe_venda': order.document_file,
}
self.cancel_order_sat(orderSatObj);
}
});
});
},
Expand All @@ -161,15 +186,27 @@ odoo.define("l10n_br_pos.screens", function (require) {

var status = this.pos.proxy.get('status');
this.pos.proxy.cancel_order(order);
// var sat_status = status.drivers.satcfe ? status.drivers.satcfe.status : false;
// if( sat_status == 'connected'){
// if(this.pos.config.iface_sat_via_proxy){
// this.pos.proxy.cancel_order(order);
// }
// }
},
});

pos_order_screens.PosOrderScreenWidget.include({
show: function () {
var self = this;
this._super();
this.$('.order-list-contents').delegate('.pos_order_reprint','click',function(event){
rpc.query({
model: 'pos.order',
method: 'retornar_order_by_id',
args: [$(this).parent().parent().data('id')],
limit: 1,
}).then(function (orders){
self.pos.proxy.reprint_cfe(orders);
});
});
},
});

==== BASE ====
// Screens.PaymentScreenWidget = screens.PaymentScreenWidget.extend({
// order_is_valid: function (parent, options) {
// var res = PaymentScreenWidgetSuper.prototype.order_is_valid.call(this, parent, options);
Expand Down
10 changes: 7 additions & 3 deletions l10n_br_pos/static/src/xml/pos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@
</t>
</t>

<t t-extend="PosOrderLine">
<t t-jquery='.order-line' t-operation="append">
<t t-name="PosOrderLine">
<tr class='order-line' t-att-data-id='order.id' t-att-data-chave='order.document_key' t-att-data-xml='order.document_file'>
<td><t t-esc='order.name' /></td>
<td><t t-esc='order.cnpj_cpf'/></td>
<td><t t-esc="order.date_order || moment(order.validation_date).format('YYYY-MM-DD HH:mm:ss')" /></td>
<td><t t-esc='order.amount_total || Math.round(order.get_total_with_tax())' /></td>
<td>
<span class="control-button pos_order_reprint">
Reimprimir
Expand All @@ -67,7 +71,7 @@
Cancelar
</span>
</td>
</t>
</tr>
</t>

<!-- <t t-extend="OrderWidget">-->
Expand Down