Files
test/sf_tool_management/models/fixture_enroll.py

95 lines
4.3 KiB
Python

import json
import base64
import requests
import logging
from odoo import models, api
from odoo.addons.sf_base.commons.common import Common
from odoo.exceptions import UserError
class StockLot(models.Model):
_inherit = 'stock.lot'
_description = '夹具物料序列号注册'
def sync_enroll_fixture_material_stock_all(self):
logging.info("调用 sync_enroll_fixture_material_stock_all 同步接口")
sf_sync_config = self.env['res.config.settings'].get_values()
token = sf_sync_config['token']
sf_secret_key = sf_sync_config['sf_secret_key']
headers = Common.get_headers(self, token, sf_secret_key)
str_url = sf_sync_config['sf_url'] + "/api/fixture_material_stock/create"
product_ids = self.env['product.product'].sudo().search([('categ_type', '=', '夹具')]).ids
objs_all = self.env['stock.lot'].search([('rfid', '!=', False), ('product_id', 'in', product_ids)])
fixture_material_stock_list = []
try:
if objs_all:
for item in objs_all:
val = {
'name': item.name,
'tool_material_status': item.tool_material_status,
'location': [] if not item.quant_ids else item.quant_ids[-1].location_id.name,
'fixture_material_search_id': item.fixture_material_search_id.id,
}
fixture_material_stock_list.append(val)
kw = json.dumps(fixture_material_stock_list, ensure_ascii=False)
r = requests.post(str_url, json={}, data={'kw': kw, 'token': token}, headers=headers)
ret = r.json()
if ret.get('code') == 200:
logging.info("夹具物料序列号每日同步成功")
return '夹具物料序列号注册成功'
else:
logging.info("没有注册夹具物料序列号信息")
except Exception as e:
logging.info("夹具物料序列号同步失败:%s" % e)
class FixtureMaterialSearch(models.Model):
_inherit = 'sf.fixture.material.search'
_description = '夹具物料搜索注册'
crea_url = "/api/fixture_material/create"
def sync_enroll_fixture_material_all(self):
logging.info("调用 sync_enroll_fixture_material_all 同步接口")
sf_sync_config = self.env['res.config.settings'].get_values()
token = sf_sync_config['token']
sf_secret_key = sf_sync_config['sf_secret_key']
headers = Common.get_headers(self, token, sf_secret_key)
str_url = sf_sync_config['sf_url'] + self.crea_url
objs_all = self.search([])
fixture_material_list = []
try:
if objs_all:
for obj in objs_all:
val = {
'name': obj.name,
'id': obj.id,
'fixture_material_code': obj.fixture_material_id.code,
'fixture_model_code': obj.fixture_model_id.code,
'specification_name': obj.specification_fixture_id.name,
'image': '' if not obj.image else base64.b64encode(obj.image).decode('utf-8'),
'number': obj.number,
'usable_num': obj.usable_num,
'have_been_used_num': obj.have_been_used_num,
'scrap_num': obj.scrap_num
}
fixture_material_list.append(val)
kw = json.dumps(fixture_material_list, ensure_ascii=False)
r = requests.post(str_url, json={}, data={'kw': kw, 'token': token}, headers=headers)
ret = r.json()
if ret.get('code') == 200:
logging.info("夹具物料每日同步成功")
return '夹具物料注册成功'
else:
logging.info("没有注册夹具物料信息")
except Exception as e:
logging.info("夹具物料同步失败:%s" % e)
# @api.model_create_multi
# def create(self, vals_list):
# records = super(FixtureMaterialSearch, self).create(vals_list)
# for record in records:
# if record:
# record.enroll_fixture_material()
# return records