Files
test/sf_stock/models/stock_picking.py

109 lines
4.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
import json
import requests
from odoo import models, fields, api
from odoo.exceptions import UserError
import logging
from odoo.tools import date_utils
_logger = logging.getLogger(__name__)
class StockPicking(models.Model):
_inherit = 'stock.picking'
# 重写验证下发发货到bfm
def button_validate(self):
info = super(StockPicking, self).button_validate()
if self.picking_type_code == 'outgoing':
self.send_to_bfm()
return info
def deal_move_ids(self, send_move_ids, send_move_line_ids):
move_ids = [] # 本次发货单
move_line_ids = [] # 本次发货单行
if send_move_ids:
for item in send_move_ids:
val = {
'name': item.product_id.upload_model_file.display_name,
'quantity_done': item.quantity_done,
'date': date_utils.json_default(item.date) if item.date else None,
'description_picking': item.description_picking,
'date_deadline': date_utils.json_default(item.date_deadline) if item.date_deadline else None,
'product_uom_qty': item.product_uom_qty,
'sequence': item.sequence,
'price_unit': item.price_unit,
'priority': item.priority,
'state': item.state,
}
move_ids.append(val)
for item in send_move_line_ids:
val = {
'qty_done': item.qty_done,
'reserved_qty': item.reserved_qty,
'reserved_uom_qty': item.reserved_uom_qty,
'date': date_utils.json_default(item.date) if item.date else None,
'description_picking': item.description_picking,
'state': item.state,
}
move_line_ids.append(val)
return move_ids, move_line_ids
def deal_send_backorder_id(self, backorder_ids1):
backorder_ids = []
if backorder_ids1:
for item in backorder_ids1:
move_ids, move_line_ids = self.deal_move_ids(item.move_ids, item.move_line_ids)
val = {
'receiverName': item.receiverName,
'name': item.sale_id.default_code,
'send_no': item.name,
'scheduled_date': date_utils.json_default(item.scheduled_date) if item.scheduled_date else None,
'date': date_utils.json_default(item.date) if item.date else None,
'date_deadline': date_utils.json_default(item.date_deadline) if item.date_deadline else None,
'date_done': date_utils.json_default(item.date_done) if item.date_done else None,
'move_ids': move_ids,
'move_line_ids': move_line_ids,
'state': item.state,
'move_type': item.move_type,
}
backorder_ids.append(val)
return backorder_ids
def send_to_bfm(self):
skip_backorder = self.env.context.get('skip_backorder')
# 下发发货到bfm
config = self.env['res.config.settings'].get_values()
move_ids, move_line_ids = self.deal_move_ids(self.move_ids, self.move_line_ids)
data = {
'params': {
'receiverName': self.receiverName,
'priority': self.priority,
'name': self.sale_id.default_code,
'send_no': self.name,
'scheduled_date': date_utils.json_default(self.scheduled_date) if self.scheduled_date else None,
'date': date_utils.json_default(self.date) if self.date else None,
'date_deadline': date_utils.json_default(self.date_deadline) if self.date_deadline else None,
'date_done': date_utils.json_default(self.date_done) if self.date_done else None,
'move_ids': move_ids,
'move_line_ids': move_line_ids,
'state': self.state,
'backorder_id': self.deal_send_backorder_id(self.backorder_id),
'backorder_ids': self.deal_send_backorder_id(self.backorder_ids),
'cancel_backorder_ids': skip_backorder,
'move_type': self.move_type,
},
}
url1 = config['bfm_url_new'] + '/api/stock/deliver_goods'
json_str = json.dumps(data)
print('json_str', json_str)
r = requests.post(url1, json=data, data=None)
if r.status_code == 200:
result = json.loads(r.json()['result'])
if result['code'] != 200:
raise UserError(result['message'] or '工厂发货下发bfm失败')
else:
raise UserError('工厂发货下发bfm失败')