91 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			91 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
# Copyright 2018-Today datenpol gmbh (<http://www.datenpol.at>)
 | 
						|
# License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses).
 | 
						|
 | 
						|
from odoo import tools, api, fields, models, _
 | 
						|
from odoo.exceptions import ValidationError, UserError, Warning
 | 
						|
 | 
						|
import json
 | 
						|
import requests
 | 
						|
 | 
						|
class WizardResetOrder(models.TransientModel):
 | 
						|
    _name = 'wizard.reset_order'
 | 
						|
    _description = 'Reset imos-Order'
 | 
						|
 | 
						|
    # name = fields.Char()
 | 
						|
    #
 | 
						|
    # @api.onchange('name')
 | 
						|
    # def _onchange_name(self):
 | 
						|
    #     return {
 | 
						|
    #         'warning': {
 | 
						|
    #             'title': _('Warning!'),
 | 
						|
    #             'message': _("Achtung, es gab Fehler"),
 | 
						|
    #         }
 | 
						|
    #     }
 | 
						|
 | 
						|
    @api.multi
 | 
						|
    def button_reset_order(self):
 | 
						|
        for wizard in self:
 | 
						|
            error_at_quotation = False
 | 
						|
            info = 'Bitte prüfen Sie den Angebotsstatus & Auftragsart folgender Angebote:'
 | 
						|
            active_ids = self.env.context.get('active_ids', [])
 | 
						|
            sale_orders = self.env['sale.order'].browse(active_ids)
 | 
						|
            if sale_orders.exists():
 | 
						|
                for so in sale_orders:
 | 
						|
                    if so.state == 'cancel' and so.order_type == 'D':
 | 
						|
                        info += "\n %s -- OK" % so.name
 | 
						|
                    else:
 | 
						|
                        info += "\n %s state='%s',order_type='%s'" % (so.name,so.state,so.order_type)
 | 
						|
                        error_at_quotation = True
 | 
						|
                if error_at_quotation:
 | 
						|
                    raise ValidationError(_(info))
 | 
						|
 | 
						|
                info = 'Mindestens ein Warenkorb konnte nicht zurückgesetzt werden:'
 | 
						|
                for so in sale_orders:
 | 
						|
                    order_info = self.reset_order_status(so.origin)
 | 
						|
                    if order_info != '':
 | 
						|
                        error_at_quotation = True
 | 
						|
                        info += "\n%s" % order_info
 | 
						|
                        so.message_post(body='Warenkorb konnte im DesignBox-Portal nicht zurückgesetzt werden!\n %s' % order_info)
 | 
						|
                    else:
 | 
						|
                        info += "\n%s --> OK" % so.name
 | 
						|
                        so.message_post(body='Warenkorb im DesignBox-Portal zurückgesetzt!\n%s --> OK' % so.origin)
 | 
						|
 | 
						|
                if error_at_quotation:
 | 
						|
                    raise Warning(_(info))
 | 
						|
#                    self.name = _(info)
 | 
						|
 | 
						|
                action = self.env.ref('sale.action_orders').read()[0]
 | 
						|
                action['domain'] = [('id', 'in', active_ids)]
 | 
						|
            return action
 | 
						|
 | 
						|
    def reset_order_status(self, order_id):
 | 
						|
        self.ensure_one()
 | 
						|
        imos_base_url = tools.config.get('imos_base_url')
 | 
						|
        imos_pass = tools.config.get('imos_pass')
 | 
						|
        imos_url = imos_base_url + r'&tx_imosnetpublic_api[controller]=Basket&tx_imosnetpublic_api[action]=update'
 | 
						|
        idata = {"uid" : int(order_id[-6:]), "status" : 0}
 | 
						|
        data = {
 | 
						|
            'pass' : imos_pass,
 | 
						|
            'user' : 'publicapiuser',
 | 
						|
            'pid' : 5,
 | 
						|
            'logintype' : 'login',
 | 
						|
            'tx_imosnetpublic_api[data]' : json.dumps(idata)
 | 
						|
        }
 | 
						|
        response = requests.post(imos_url, data=data)
 | 
						|
 | 
						|
        info = ''
 | 
						|
        try:
 | 
						|
            lRes = json.loads(response.content.decode())
 | 
						|
        except Exception as e:
 | 
						|
            info = 'unexpected error %s' % e
 | 
						|
            raise ValidationError(_(info))
 | 
						|
 | 
						|
        if response.status_code == 200:
 | 
						|
            if lRes.get('status') != 0:
 | 
						|
                info = "Warenkorb: %s, Fehler='%s'" % (order_id,lRes)
 | 
						|
        else:
 | 
						|
            if lRes.get('status') != 0:
 | 
						|
                info = "Warenkorb: %s, Fehler='%s'" % (order_id,lRes.get('data'))
 | 
						|
 | 
						|
        return info
 |