Add process message and process incidence methods

This commit is contained in:
Sebastian Marro 2016-11-17 20:38:22 -03:00
parent 05ed491227
commit aacb5f8982
4 changed files with 122 additions and 37 deletions

View file

@ -2,17 +2,21 @@
msgid ""
msgstr "Content-Type: text/plain; charset=utf-8\n"
msgctxt "field:party.party,host:"
msgid "Host"
msgstr "Servidor"
msgctxt "field:party.party,fedicom_host:"
msgid "Fedicom Host"
msgstr "Servidor Fedicom"
msgctxt "field:party.party,port:"
msgid "Port"
msgstr "Puerto"
msgctxt "field:party.party,fedicom_port:"
msgid "Fedicom Port"
msgstr "Puerto Fedicom"
msgctxt "field:party.party,timeout:"
msgid "Timeout"
msgstr "Tiempo de espera"
msgctxt "field:party.party,fedicom_timeout:"
msgid "Fedicom Timeout"
msgstr "Tiempo de espera Fedicom"
msgctxt "field:product.product,supplier_code:"
msgid "Supplier Code"
msgstr "Código de Proveedor"
msgctxt "model:res.group,name:group_purchase_fedicom"
msgid "Purchase Fedicom"

View file

@ -2,6 +2,7 @@
# copyright notices and license terms.
import socket
import logging
from decimal import Decimal
from trytond.modules.sale_fedicom.service.messages.init_session \
import InitSession
from trytond.modules.sale_fedicom.service.messages.close_session \
@ -10,19 +11,25 @@ from trytond.modules.sale_fedicom.service.messages.order import Order
from trytond.modules.sale_fedicom.service.messages.order_line import OrderLine
from trytond.modules.sale_fedicom.service.messages.finish_order \
import FinishOrder
from trytond.modules.sale_fedicom.service.messages.incidence_header \
import IncidenceHeader
from trytond.modules.sale_fedicom.service.messages.incidence_order_line \
import IncidenceOrderLine
from trytond.model import fields
from trytond.pool import PoolMeta
from trytond.pool import Pool, PoolMeta
__all__ = ['Party', 'Product', 'Purchase']
_ZERO = Decimal(0)
class Party:
__name__ = 'party.party'
__metaclass__ = PoolMeta
host = fields.Char('Host')
port = fields.Integer('Port')
timeout = fields.Integer('Timeout')
fedicom_host = fields.Char('Fedicom Host')
fedicom_port = fields.Integer('Fedicom Port')
fedicom_timeout = fields.Integer('Fedicom Timeout')
class Product:
@ -53,18 +60,19 @@ class Purchase:
logger.info('Process Purchase %s From Party %s' % (
self.reference, self.party.code))
if not self.party.fedicom_host or not self.party.fedicom_port:
return
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.party.host, self.party.port))
sock.settimeout(self.party.fedicom_timeout or 30)
sock.connect((self.party.fedicom_host, self.party.fedicom_port))
msg = self.send_order()
sock.sendall(msg)
data = sock.recv(2048)
sock.close()
data_list = data.split('\r\n')
for msg in data_list:
print msg
self.process_message(data)
def send_order(self):
msg = ""
@ -81,3 +89,86 @@ class Purchase:
msg += str(f)
msg += str(CloseSession())
return msg
def process_message(self, msg):
logger = logging.getLogger('purchase_fedicom')
logger.info('Process Order Incidence')
msg_list = msg.split('\r\n')
i = 0
init_session = InitSession()
init_session.set_message(msg_list[i])
i = i + 1
next_message = init_session.next_state()
incidence = {}
incidence_lines = {}
while i < len(msg_list) - 1:
msg = msg_list[i]
if not msg[0:4] in next_message:
logger.warning("Se ha producido un "
"error, Trama enviada no pertenece al estado siguiente")
break
for state in next_message:
if msg.startswith(state):
if msg.startswith('0199'): # Close Session
logger.info('Procesando Cierre de Sesion')
next_message = None
if incidence_lines:
self.process_incidence(incidence_lines)
return
elif msg.startswith('2010'): # incidencia
logger.info('Procesando Cabecera de Incidencia')
incidence = IncidenceHeader()
incidence.set_msg(msg)
next_message = incidence.next_state()
elif msg.startswith('2015'):
logger.info('Procesando Linea de Incidencia')
incidence_line = IncidenceOrderLine()
incidence_line.set_msg(msg)
next_message = incidence_line.next_state()
product_code = incidence_line.article_code[-7:]
incidence_lines[product_code] = \
int(incidence_line.amount_not_served)
else:
logger.warning("Se ha producido un error. "
"Trama enviada no pertenece al estado siguiente")
return
i = i + 1
return
def process_incidence(self, incidence_lines):
pool = Pool()
Purchase = pool.get('purchase.purchase')
PurchaseLine = pool.get('purchase.line')
purchase, = Purchase.copy([self])
# Update purchase quantities
amount = Decimal('0.0')
for line in self.lines:
if incidence_lines.get(line.product.supplier_code, 0) > 0:
line.quantity = (line.quantity -
incidence_lines[line.product.supplier_code])
line.save()
amount += line.amount if line.type == 'line' else _ZERO
# Update cached fields
self.untaxed_amount_cache = amount
self.tax_amount_cache = self.get_tax_amount()
self.total_amount_cache = (
self.untaxed_amount_cache + self.tax_amount_cache)
self.save()
# Create new purchase for missing quantities
lines_to_delete = []
for line in purchase.lines:
if incidence_lines.get(line.product.supplier_code, 0) > 0:
line.quantity = incidence_lines[line.product.supplier_code]
line.save()
else:
lines_to_delete.append(line)
PurchaseLine.delete(lines_to_delete)

View file

@ -3,22 +3,12 @@
# copyright notices and license terms.
import unittest
import trytond.tests.test_tryton
from trytond.tests.test_tryton import test_view, test_depends
from trytond.tests.test_tryton import ModuleTestCase
class TestCase(unittest.TestCase):
class TestCase(ModuleTestCase):
'Test module'
def setUp(self):
trytond.tests.test_tryton.install_module('purchase_fedicom')
def test0005views(self):
'Test views'
test_view('purchase_fedicom')
def test0006depends(self):
'Test depends'
test_depends()
module = 'purchase_fedicom'
def suite():

View file

@ -4,11 +4,11 @@
<data>
<xpath expr="/form/notebook/page[@id='fedicom']/field[@name='fedicom_password']"
position="after">
<label name="host"/>
<field name="host"/>
<label name="port"/>
<field name="port"/>
<label name="timeout"/>
<field name="timeout"/>
<label name="fedicom_host"/>
<field name="fedicom_host"/>
<label name="fedicom_port"/>
<field name="fedicom_port"/>
<label name="fedicom_timeout"/>
<field name="fedicom_timeout"/>
</xpath>
</data>