# -*- coding: utf-8 -*- from odoo import models, fields, api from odoo.exceptions import ValidationError from odoo.utils import bill_define from odoo.utils.constant import APPROVE, REFUSE, APPROVE_SUCCESS, ABNORMAL, MESSAGE_TYPE, PRODUCT_NAME # , REMINDER NOT_READ = '10' ALREADY_READ = '20' SEND_FAIL = '30' STATUS = [ (NOT_READ, u'未读'), (ALREADY_READ, u'已读'), (SEND_FAIL, u'发送失败') ] class Message(models.Model): _name = 'we_chart.wx_message' _description = u'微信小程序:微信消息' _order = 'id desc' message_type = fields.Selection(MESSAGE_TYPE, string=u'消息类型', readonly=True, copy=False) user_id = fields.Many2one('res.users', string=u'接收用户') related_user_name = fields.Char(u'关联用户') # 用于显示在微信信息中的“姓名”里 status = fields.Selection(STATUS, string=u'状态', readonly=True, default=NOT_READ) title = fields.Char(u'标题') title_show = fields.Char(u'标题') contents = fields.Char(u'内容') source_bill_type = fields.Selection(bill_define.BILL_TYPE, string=u'来源单据类型', readonly=True, copy=False) source_bill_id = fields.Integer(string=u"来源单据号", readonly=True, copy=False, default=0) wx_url = fields.Char(u'微信路径') remark = fields.Char(string=u'摘要') fail_msg = fields.Char(u'失败信息') content1 = fields.Char(u'内容1') content2 = fields.Char(u'内容2') content3 = fields.Char(u'内容3') number1 = fields.Float(u'数量1') number2 = fields.Float(u'数量2') number3 = fields.Float(u'数量3') # @api.multi def name_get(self): result = [] for record in self: result.append((record.id, "%s: %s" % (record.user_id.name, record.title))) return result def add(self, message_type, receiver_id, related_user_name, title, source_bill_type, source_bill_id, contents=None, content1=None, content2=None, content3=None, number1=None, number2=None, number3=None, title_show=None): # is_exist = self.check_exist(message_type, receiver_id, source_bill_type, source_bill_id) # if is_exist: # return wx_url = Message._get_wx_url(source_bill_type) data = { "message_type": message_type, "user_id": receiver_id, "related_user_name": related_user_name, "status": NOT_READ, "title": title, "source_bill_type": source_bill_type, "source_bill_id": source_bill_id, "wx_url": wx_url, } if contents: data["contents"] = contents if content1: data["content1"] = content1 if content2: data["content2"] = content2 if content3: data["content3"] = content3 if number1: data["number1"] = number1 if number2: data["number2"] = number2 if number3: data["number3"] = number3 if title_show: data["title_show"] = title_show # print "wx message:", data return self.sudo().create(data) def check_exist(self, message_type, receiver_id, source_bill_type, source_bill_id): domain = [('source_bill_type', '=', source_bill_type), ('source_bill_id', '=', source_bill_id), ('status', '=', NOT_READ), ('user_id', '=', receiver_id), ('message_type', '=', message_type)] count = self.sudo().search_count(domain) return count > 0 @staticmethod def _get_wx_url(source_bill_type): if source_bill_type == bill_define.sale_order: return "/pages/order/order" if source_bill_type == bill_define.sale_receipt: return "/pages_sub/finance/receipt/bill/receipt" if source_bill_type == bill_define.sale_class_price: return "/pages_sub/sale/class_price/bill/bill" if source_bill_type == bill_define.memo_ticket: return "/pages_sub/finance/memo_ticket/bill/bill" if source_bill_type == bill_define.purchase_apply: return "/pages_sub/purchase/purchase_apply/bill/bill" # if source_bill_type == bill_define.A_NOTICE_200: # return "/pages_sub/archives/notice/bill/bill" raise ValidationError(u'开发错误:没有设置来源单据类型{}对应的微信页面路径'.format(source_bill_type)) def send(self): sub_domain, template_id, data, page = self._get_message_info() # self.fail_msg = u'测试数据,不发送' success, fail_msg = self.sudo().env['wxapp.user'].send_wx_message(self.user_id.id, sub_domain, template_id, data, page) if not success: self.sudo().write({'status': SEND_FAIL, 'fail_msg': fail_msg}) return def _get_message_info(self): if self.sudo().message_type == APPROVE: if self.sudo().source_bill_type == bill_define.sale_order: return self._get_order_message() if self.sudo().source_bill_type == bill_define.sale_receipt: return self._get_receipt__or__memo_ticket__message() if self.sudo().source_bill_type == bill_define.sale_class_price: return self._get_class_price_message() if self.sudo().source_bill_type == bill_define.memo_ticket: return self._get_receipt__or__memo_ticket__message() if self.sudo().source_bill_type == bill_define.purchase_apply: return self._get_purchase_apply_message() # elif self.sudo().message_type == REMINDER: # if self.sudo().source_bill_type == bill_define.A_NOTICE_200: # return self._get_notice_message() elif self.sudo().message_type == APPROVE_SUCCESS: if self.sudo().source_bill_type == bill_define.sale_order: return self._get_order_message_result(u'通过') elif self.sudo().message_type == REFUSE: if self.sudo().source_bill_type == bill_define.sale_order: return self._get_order_message_result(u'驳回') raise ValidationError(u'未处理的消息种类') def _get_order_message(self): sub_domain = PRODUCT_NAME # template_id = 'NVwh3ddTQiFko5mPO5_ao-xQz6BNCeFappqQNbBU4mQ'#待审批提醒:审批标题/备注 # template_id = 'NVwh3ddTQiFko5mPO5_ao_simcN8jqfRPSBDOE-3ZVs'#待审批提醒:审批标题/审批内容/金额/备注 template_id = 'NVwh3ddTQiFko5mPO5_aox7OsHyWIabdQ-umWSQ0F8k' page = '/pages/message/list' data = { "thing19": { # 客户 "value": self.sudo().title, }, "thing20": { # 业务员 "value": self.sudo().contents, }, "amount14": { # 金额 "value": str(self.sudo().number1), }, "name5": { # 申请人 "value": self.sudo().content1, }, } return sub_domain, template_id, data, page # 参数approve_result:不超过5个汉字,只能是汉字 def _get_order_message_result(self, approve_result): sub_domain = PRODUCT_NAME template_id = 'wYovlP0w9t03rTxMlu3HjJBmt2FObLLuFwWMlLijuWQ' page = '/pages/message/list' data = { "thing22": { # 客户 "value": self.sudo().title, }, "thing23": { # 业务员 "value": self.sudo().contents, }, "amount24": { # 金额 "value": str(self.sudo().number1), }, "phrase4": { # 审批结果 "value": approve_result, }, } return sub_domain, template_id, data, page def _get_receipt__or__memo_ticket__message(self): sub_domain = PRODUCT_NAME template_id = 'NVwh3ddTQiFko5mPO5_ao7M-c9FgnU8Pi9t3LybHNR8' page = '/pages/message/list' data = { "thing1": { # 审批标题:单位 "value": self.sudo().title, }, "amount14": { # 金额 "value": str(self.sudo().number1), }, "name5": { # 申请人 "value": self.sudo().content1, }, "thing8": { # 备注 "value": self.sudo().content2, }, } return sub_domain, template_id, data, page def _get_class_price_message(self): sub_domain = PRODUCT_NAME template_id = 'NVwh3ddTQiFko5mPO5_aowl3NGrnxuh-E-tk4L4JiTA' page = '/pages/message/list' data = { "thing1": { # 审批标题:名称 "value": self.sudo().title, }, "name5": { # 申请人 "value": self.sudo().content1, }, "thing8": { # 备注 "value": self.sudo().content2, }, } return sub_domain, template_id, data, page def _get_purchase_apply_message(self): sub_domain = PRODUCT_NAME template_id = 'NVwh3ddTQiFko5mPO5_ao0uVtPj5nWxqoTGrjDBv01w' page = '/pages/message/list' data = { "thing1": { # 审批标题:名称 "value": self.sudo().title, }, "name5": { # 申请人 "value": self.sudo().contents, }, "phrase7": { # 申请类型 "value": self.sudo().content1, }, "amount14": { # 金额 "value": self.sudo().number1, }, "thing8": { # 备注 "value": self.sudo().content2, }, } return sub_domain, template_id, data, page def _get_notice_message(self): sub_domain = PRODUCT_NAME template_id = 'fCVmc0_E41KG8_b50kkzTDXjT2DgvRtjmJKIbAu3Be0' page = '/pages/message/list' data = { "thing30": { # 标题 "value": self.sudo().title, }, "time11": { # 日期 "value": self.sudo().contents, }, "thing7": { # 通知内容 "value": self.sudo().content1, }, } return sub_domain, template_id, data, page def go_to_source_bill(self): res_model = self._get_bill_model() return { 'type': 'ir.actions.act_window', 'res_model': res_model, 'view_type': 'form', 'view_mode': 'form', 'target': 'current', 'res_id': self.source_bill_id, } def _get_bill_model(self): if self.source_bill_type == bill_define.sale_account: return 'jc_finance.sale_account' elif self.source_bill_type == bill_define.sale_invoice: return 'jc_finance.sale_invoice' elif self.source_bill_type == bill_define.sale_order: return 'jc_sale.sale_order' raise ValidationError(u'未处理的类型:' + str(self.source_bill_type))