diff --git a/sf_tool_management/__manifest__.py b/sf_tool_management/__manifest__.py index a7750f69..fe65c05e 100644 --- a/sf_tool_management/__manifest__.py +++ b/sf_tool_management/__manifest__.py @@ -21,6 +21,7 @@ 'views/menu_view.xml', 'views/tool_material_search.xml', 'views/fixture_material_search_views.xml', + 'data/tool_data.xml', ], 'demo': [ ], diff --git a/sf_tool_management/data/tool_data.xml b/sf_tool_management/data/tool_data.xml index 714d03ee..282f8c2a 100644 --- a/sf_tool_management/data/tool_data.xml +++ b/sf_tool_management/data/tool_data.xml @@ -1,8 +1,12 @@ - - - - - + + 定时同步刀具物料、功能刀具信息到cloud + + code + model._cron_tool_datasync_all() + 1 + days + -1 + \ No newline at end of file diff --git a/sf_tool_management/models/functional_tool_enroll.py b/sf_tool_management/models/functional_tool_enroll.py index 8fb781f8..6f49af02 100644 --- a/sf_tool_management/models/functional_tool_enroll.py +++ b/sf_tool_management/models/functional_tool_enroll.py @@ -1,9 +1,10 @@ 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 +from odoo.exceptions import UserError, ValidationError def get_suitable_machining_method_names(item): @@ -30,6 +31,30 @@ def get_suitable_coolant_names(item): return suitable_coolant_names +class ToolDatasync(models.Model): + _name = 'sf.tool.datasync' + _description = '定时同步所有刀具' + + def _cron_tool_datasync_all(self): + try: + self.env['stock.lot'].sudo().sync_enroll_tool_material_stock_all() + logging.info("刀具物料序列号每日同步成功") + self.env['sf.tool.material.search'].sudo().sync_enroll_tool_material_all() + logging.info("刀具物料每日同步成功") + self.env['sf.functional.cutting.tool.entity'].sudo().esync_enroll_functional_tool_entity_all() + logging.info("功能刀具列表每日同步成功") + self.env['sf.functional.tool.warning'].sudo().sync_enroll_functional_tool_warning_all() + logging.info("功能刀具列表每日同步成功") + self.env['stock.move.line'].sudo().sync_enroll_functional_tool_move_all() + logging.info("功能刀具出入库记录每日同步成功") + self.env[ + 'sf.real.time.distribution.of.functional.tools'].sudo().sync_enroll_functional_tool_real_time_distribution_all() + logging.info("功能刀具安全库存每日同步成功") + except Exception as e: + logging.info("捕获错误信息:%s" % e) + raise ValidationError("数据错误导致同步失败,请联系管理员") + + class StockLot(models.Model): _inherit = 'stock.lot' _description = '刀具物料序列号注册' @@ -41,6 +66,18 @@ class StockLot(models.Model): headers = Common.get_headers(self, token, sf_secret_key) str_url = sf_sync_config['sf_url'] + "/api/tool_material_stock/create" objs_all = self.env['stock.lot'].search([('id', '=', self.id), ('active', 'in', [True, False])]) + self._get_sync_stock_lot(objs_all, str_url, token, headers) + + def sync_enroll_tool_material_stock_all(self): + 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/tool_material_stock/create" + objs_all = self.env['stock.lot'].search([('rfid', '!=', False)]) + self._get_sync_stock_lot(objs_all, str_url, token, headers) + + def _get_sync_stock_lot(self, objs_all, str_url, token, headers): tool_material_stock_list = [] if objs_all: for item in objs_all: @@ -73,6 +110,18 @@ class ToolMaterial(models.Model): headers = Common.get_headers(self, token, sf_secret_key) str_url = sf_sync_config['sf_url'] + self.crea_url objs_all = self.search([('id', '=', self.id)]) + self._get_sync_tool_material_search(objs_all, str_url, token, headers) + + def sync_enroll_tool_material_all(self): + 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([]) + self._get_sync_tool_material_search(objs_all, str_url, token, headers) + + def _get_sync_tool_material_search(self, objs_all, str_url, token, headers): tool_material_list = [] if objs_all: for item in objs_all: @@ -120,6 +169,18 @@ class FunctionalCuttingToolEntity(models.Model): headers = Common.get_headers(self, token, sf_secret_key) str_url = sf_sync_config['sf_url'] + self.crea_url objs_all = self.env['sf.functional.cutting.tool.entity'].search([('id', '=', self.id)]) + self._get_sync_functional_cutting_tool_entity(objs_all, str_url, token, headers) + + def esync_enroll_functional_tool_entity_all(self): + 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.env['sf.functional.cutting.tool.entity'].search([]) + self._get_sync_functional_cutting_tool_entity(objs_all, str_url, token, headers) + + def _get_sync_functional_cutting_tool_entity(self, objs_all, str_url, token, headers): functional_tool_list = [] if objs_all: for item in objs_all: @@ -201,6 +262,18 @@ class FunctionalToolWarning(models.Model): headers = Common.get_headers(self, token, sf_secret_key) str_url = sf_sync_config['sf_url'] + self.crea_url objs_all = self.env['sf.functional.tool.warning'].search([('id', '=', self.id)]) + self.get_sync_functional_tool_warning(objs_all, str_url, token, headers) + + def sync_enroll_functional_tool_warning_all(self): + 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.env['sf.functional.tool.warning'].search([]) + self.get_sync_functional_tool_warning(objs_all, str_url, token, headers) + + def get_sync_functional_tool_warning(self, objs_all, str_url, token, headers): tool_warning_list = [] if objs_all: for item in objs_all: @@ -262,6 +335,18 @@ class StockMoveLine(models.Model): headers = Common.get_headers(self, token, sf_secret_key) str_url = sf_sync_config['sf_url'] + self.crea_url objs_all = self.env['stock.move.line'].search([('id', '=', self.id), ('functional_tool_name_id', '!=', False)]) + self.get_sync_stock_move_line(objs_all, str_url, token, headers) + + def sync_enroll_functional_tool_move_all(self): + 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.env['stock.move.line'].search([('functional_tool_name_id', '!=', False)]) + self.get_sync_stock_move_line(objs_all, str_url, token, headers) + + def get_sync_stock_move_line(self, objs_all, str_url, token, headers): tool_stock_list = [] if objs_all: for item in objs_all: @@ -314,6 +399,18 @@ class RealTimeDistributionFunctionalTools(models.Model): headers = Common.get_headers(self, token, sf_secret_key) str_url = sf_sync_config['sf_url'] + self.crea_url objs_all = self.env['sf.real.time.distribution.of.functional.tools'].search([('id', '=', self.id)]) + self.get_sync_real_time_distribution_functional_tools(objs_all, str_url, token, headers) + + def sync_enroll_functional_tool_real_time_distribution_all(self): + 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.env['sf.real.time.distribution.of.functional.tools'].search([]) + self.get_sync_real_time_distribution_functional_tools(objs_all, str_url, token, headers) + + def get_sync_real_time_distribution_functional_tools(self, objs_all, str_url, token, headers): tool_distribution_list = [] if objs_all: for item in objs_all: