99 lines
3.8 KiB
Python
99 lines
3.8 KiB
Python
|
from openerp import api, fields, models
|
||
|
import logging
|
||
|
from openerp.exceptions import Warning
|
||
|
import pycountry
|
||
|
from inema import Internetmarke
|
||
|
|
||
|
_logger = logging.getLogger(__name__)
|
||
|
|
||
|
TRACKING_URL = 'https://www.deutschepost.de/sendung/simpleQuery.html?locale=en_GB'
|
||
|
|
||
|
# convert from ISO3166 2-digit to 3-digit
|
||
|
def get_alpha3_country_from_alpha2(twodigit):
|
||
|
c = pycountry.countries.get(alpha2=twodigit)
|
||
|
return c.alpha3
|
||
|
|
||
|
# split the last word of a string containing stree name + house number
|
||
|
def split_street_house(streethouse):
|
||
|
r = streethouse.rsplit(' ', 1)
|
||
|
return (r[0], r[1])
|
||
|
|
||
|
def split_first_lastname(name):
|
||
|
r = name.rsplit(' ', 1)
|
||
|
return (r[0], r[1])
|
||
|
|
||
|
class DPDeliveryCarrier(models.Model):
|
||
|
_inherit="delivery.carrier"
|
||
|
|
||
|
def conn_auth_im(self):
|
||
|
config = self._get_config()
|
||
|
partner_id = config['dp_partner_id']
|
||
|
key = config['dp_key']
|
||
|
key_phase = config['dp_key_phase']
|
||
|
pk_user = config['dp_portokasse_user']
|
||
|
pk_passwd = config['dp_portokasse_passwd']
|
||
|
im = Internetmarke(partner_id, key, key_phase)
|
||
|
im.authenticate(pk_user, pk_passwd)
|
||
|
return im
|
||
|
|
||
|
# Convert an Odoo Partner object into Internetmarke Address
|
||
|
def build_im_addr(self, im, partner):
|
||
|
(street, house) = split_street_house(partner.street)
|
||
|
country = get_alpha3_country_from_alpha2(partner.country_id.code)
|
||
|
addr = im.build_addr(street = street,
|
||
|
house = house,
|
||
|
additional = partner.street2,
|
||
|
zipcode = partner.zip,
|
||
|
city = partner.city,
|
||
|
country = country)
|
||
|
if partner.parent_id.name:
|
||
|
return im.build_comp_addr(company = partner.parent_id.name,
|
||
|
address = addr,
|
||
|
person = partner.name)
|
||
|
else:
|
||
|
(first, last) = split_first_lastname(partner.name)
|
||
|
return im.build_pers_addr(first = first,
|
||
|
last = last,
|
||
|
address = addr)
|
||
|
|
||
|
@api.one
|
||
|
def dp_send_shipping(self, pickings):
|
||
|
config = self._get_config()
|
||
|
order = self.env['sale.order'].search([('name','=',pickings.origin)])
|
||
|
recipient = pickings.partner_id
|
||
|
warehouse = pickings.picking_type_id.warehouse_id.partner_id
|
||
|
service = self.sudo().dp_service_type
|
||
|
im = self.conn_auth_im()
|
||
|
im_recipient = self.build_im_addr(im, recipient)
|
||
|
im_sender = self.build_im_addr(im, wareohouse)
|
||
|
service = self.sudo().dp_service_type
|
||
|
position = im.build_position(service_type.code, im_sender, im_recipient)
|
||
|
im.add_position(position)
|
||
|
r = im.checkoutPNG()
|
||
|
voucher = r.shoppingCart.voucherList.voucher[0]
|
||
|
filename = 'DP'+voucher.trackId+'.png'
|
||
|
result = { 'exact_price': im.compute_total(),
|
||
|
'weight': service.weight,
|
||
|
'date_delivery': None,
|
||
|
'tracking_number': voucher.trackId,
|
||
|
'voucher_id' : voucher.voucherId,
|
||
|
'order_id' : r.shoppingCart.shopOrderId,
|
||
|
'wallet_balance': r.walletBalance,
|
||
|
'attachments': [(filename, voucher.png_bin)]},
|
||
|
return result
|
||
|
|
||
|
def dp_get_shipping_price_from_so(self, order):
|
||
|
config = self._get_config()
|
||
|
recipient = order.partner_shipping_id if order.partner_shipping_id else order.partner_id
|
||
|
warehouse = order.warehouse_id.partner_id
|
||
|
service = self.sudo().dp_service_type
|
||
|
return service.cost_price
|
||
|
|
||
|
@api.one
|
||
|
def dp_get_tracking_link(self, pickings):
|
||
|
return TRACKING_URL
|
||
|
|
||
|
@api.one
|
||
|
def dp_cancel_shipment(self, pickings):
|
||
|
raise Warning('Cancelling DP Shipments not supported!')
|