mirror of
https://github.com/NaN-tic/trytond-purchase_fedicom.git
synced 2023-12-14 04:23:00 +01:00
Add process message and process incidence methods
This commit is contained in:
parent
05ed491227
commit
aacb5f8982
|
@ -2,17 +2,21 @@
|
||||||
msgid ""
|
msgid ""
|
||||||
msgstr "Content-Type: text/plain; charset=utf-8\n"
|
msgstr "Content-Type: text/plain; charset=utf-8\n"
|
||||||
|
|
||||||
msgctxt "field:party.party,host:"
|
msgctxt "field:party.party,fedicom_host:"
|
||||||
msgid "Host"
|
msgid "Fedicom Host"
|
||||||
msgstr "Servidor"
|
msgstr "Servidor Fedicom"
|
||||||
|
|
||||||
msgctxt "field:party.party,port:"
|
msgctxt "field:party.party,fedicom_port:"
|
||||||
msgid "Port"
|
msgid "Fedicom Port"
|
||||||
msgstr "Puerto"
|
msgstr "Puerto Fedicom"
|
||||||
|
|
||||||
msgctxt "field:party.party,timeout:"
|
msgctxt "field:party.party,fedicom_timeout:"
|
||||||
msgid "Timeout"
|
msgid "Fedicom Timeout"
|
||||||
msgstr "Tiempo de espera"
|
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"
|
msgctxt "model:res.group,name:group_purchase_fedicom"
|
||||||
msgid "Purchase Fedicom"
|
msgid "Purchase Fedicom"
|
||||||
|
|
109
purchase.py
109
purchase.py
|
@ -2,6 +2,7 @@
|
||||||
# copyright notices and license terms.
|
# copyright notices and license terms.
|
||||||
import socket
|
import socket
|
||||||
import logging
|
import logging
|
||||||
|
from decimal import Decimal
|
||||||
from trytond.modules.sale_fedicom.service.messages.init_session \
|
from trytond.modules.sale_fedicom.service.messages.init_session \
|
||||||
import InitSession
|
import InitSession
|
||||||
from trytond.modules.sale_fedicom.service.messages.close_session \
|
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.order_line import OrderLine
|
||||||
from trytond.modules.sale_fedicom.service.messages.finish_order \
|
from trytond.modules.sale_fedicom.service.messages.finish_order \
|
||||||
import FinishOrder
|
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.model import fields
|
||||||
from trytond.pool import PoolMeta
|
from trytond.pool import Pool, PoolMeta
|
||||||
|
|
||||||
__all__ = ['Party', 'Product', 'Purchase']
|
__all__ = ['Party', 'Product', 'Purchase']
|
||||||
|
|
||||||
|
_ZERO = Decimal(0)
|
||||||
|
|
||||||
|
|
||||||
class Party:
|
class Party:
|
||||||
__name__ = 'party.party'
|
__name__ = 'party.party'
|
||||||
__metaclass__ = PoolMeta
|
__metaclass__ = PoolMeta
|
||||||
|
|
||||||
host = fields.Char('Host')
|
fedicom_host = fields.Char('Fedicom Host')
|
||||||
port = fields.Integer('Port')
|
fedicom_port = fields.Integer('Fedicom Port')
|
||||||
timeout = fields.Integer('Timeout')
|
fedicom_timeout = fields.Integer('Fedicom Timeout')
|
||||||
|
|
||||||
|
|
||||||
class Product:
|
class Product:
|
||||||
|
@ -53,18 +60,19 @@ class Purchase:
|
||||||
logger.info('Process Purchase %s From Party %s' % (
|
logger.info('Process Purchase %s From Party %s' % (
|
||||||
self.reference, self.party.code))
|
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 = 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()
|
msg = self.send_order()
|
||||||
sock.sendall(msg)
|
sock.sendall(msg)
|
||||||
data = sock.recv(2048)
|
data = sock.recv(2048)
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
||||||
data_list = data.split('\r\n')
|
self.process_message(data)
|
||||||
|
|
||||||
for msg in data_list:
|
|
||||||
print msg
|
|
||||||
|
|
||||||
def send_order(self):
|
def send_order(self):
|
||||||
msg = ""
|
msg = ""
|
||||||
|
@ -81,3 +89,86 @@ class Purchase:
|
||||||
msg += str(f)
|
msg += str(f)
|
||||||
msg += str(CloseSession())
|
msg += str(CloseSession())
|
||||||
return msg
|
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)
|
||||||
|
|
|
@ -3,22 +3,12 @@
|
||||||
# copyright notices and license terms.
|
# copyright notices and license terms.
|
||||||
import unittest
|
import unittest
|
||||||
import trytond.tests.test_tryton
|
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'
|
'Test module'
|
||||||
|
module = 'purchase_fedicom'
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
def suite():
|
||||||
|
|
|
@ -4,11 +4,11 @@
|
||||||
<data>
|
<data>
|
||||||
<xpath expr="/form/notebook/page[@id='fedicom']/field[@name='fedicom_password']"
|
<xpath expr="/form/notebook/page[@id='fedicom']/field[@name='fedicom_password']"
|
||||||
position="after">
|
position="after">
|
||||||
<label name="host"/>
|
<label name="fedicom_host"/>
|
||||||
<field name="host"/>
|
<field name="fedicom_host"/>
|
||||||
<label name="port"/>
|
<label name="fedicom_port"/>
|
||||||
<field name="port"/>
|
<field name="fedicom_port"/>
|
||||||
<label name="timeout"/>
|
<label name="fedicom_timeout"/>
|
||||||
<field name="timeout"/>
|
<field name="fedicom_timeout"/>
|
||||||
</xpath>
|
</xpath>
|
||||||
</data>
|
</data>
|
||||||
|
|
Loading…
Reference in a new issue