wx_message.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. # -*- coding: utf-8 -*-
  2. from odoo import models, fields, api
  3. from odoo.exceptions import ValidationError
  4. from odoo.utils import bill_define
  5. from odoo.utils.constant import APPROVE, REFUSE, APPROVE_SUCCESS, ABNORMAL, MESSAGE_TYPE, PRODUCT_NAME # , REMINDER
  6. NOT_READ = '10'
  7. ALREADY_READ = '20'
  8. SEND_FAIL = '30'
  9. STATUS = [
  10. (NOT_READ, u'未读'),
  11. (ALREADY_READ, u'已读'),
  12. (SEND_FAIL, u'发送失败')
  13. ]
  14. class Message(models.Model):
  15. _name = 'we_chart.wx_message'
  16. _description = u'微信小程序:微信消息'
  17. _order = 'id desc'
  18. message_type = fields.Selection(MESSAGE_TYPE, string=u'消息类型', readonly=True, copy=False)
  19. user_id = fields.Many2one('res.users', string=u'接收用户')
  20. related_user_name = fields.Char(u'关联用户') # 用于显示在微信信息中的“姓名”里
  21. status = fields.Selection(STATUS, string=u'状态', readonly=True, default=NOT_READ)
  22. title = fields.Char(u'标题')
  23. title_show = fields.Char(u'标题')
  24. contents = fields.Char(u'内容')
  25. source_bill_type = fields.Selection(bill_define.BILL_TYPE, string=u'来源单据类型', readonly=True, copy=False)
  26. source_bill_id = fields.Integer(string=u"来源单据号", readonly=True, copy=False, default=0)
  27. wx_url = fields.Char(u'微信路径')
  28. remark = fields.Char(string=u'摘要')
  29. fail_msg = fields.Char(u'失败信息')
  30. content1 = fields.Char(u'内容1')
  31. content2 = fields.Char(u'内容2')
  32. content3 = fields.Char(u'内容3')
  33. number1 = fields.Float(u'数量1')
  34. number2 = fields.Float(u'数量2')
  35. number3 = fields.Float(u'数量3')
  36. # @api.multi
  37. def name_get(self):
  38. result = []
  39. for record in self:
  40. result.append((record.id, "%s: %s" % (record.user_id.name, record.title)))
  41. return result
  42. def add(self, message_type, receiver_id, related_user_name, title, source_bill_type, source_bill_id, contents=None,
  43. content1=None, content2=None, content3=None, number1=None, number2=None, number3=None, title_show=None):
  44. # is_exist = self.check_exist(message_type, receiver_id, source_bill_type, source_bill_id)
  45. # if is_exist:
  46. # return
  47. wx_url = Message._get_wx_url(source_bill_type)
  48. data = {
  49. "message_type": message_type,
  50. "user_id": receiver_id,
  51. "related_user_name": related_user_name,
  52. "status": NOT_READ,
  53. "title": title,
  54. "source_bill_type": source_bill_type,
  55. "source_bill_id": source_bill_id,
  56. "wx_url": wx_url,
  57. }
  58. if contents:
  59. data["contents"] = contents
  60. if content1:
  61. data["content1"] = content1
  62. if content2:
  63. data["content2"] = content2
  64. if content3:
  65. data["content3"] = content3
  66. if number1:
  67. data["number1"] = number1
  68. if number2:
  69. data["number2"] = number2
  70. if number3:
  71. data["number3"] = number3
  72. if title_show:
  73. data["title_show"] = title_show
  74. # print "wx message:", data
  75. return self.sudo().create(data)
  76. def check_exist(self, message_type, receiver_id, source_bill_type, source_bill_id):
  77. domain = [('source_bill_type', '=', source_bill_type), ('source_bill_id', '=', source_bill_id),
  78. ('status', '=', NOT_READ), ('user_id', '=', receiver_id), ('message_type', '=', message_type)]
  79. count = self.sudo().search_count(domain)
  80. return count > 0
  81. @staticmethod
  82. def _get_wx_url(source_bill_type):
  83. if source_bill_type == bill_define.sale_order:
  84. return "/pages/order/order"
  85. if source_bill_type == bill_define.sale_receipt:
  86. return "/pages_sub/finance/receipt/bill/receipt"
  87. if source_bill_type == bill_define.sale_class_price:
  88. return "/pages_sub/sale/class_price/bill/bill"
  89. if source_bill_type == bill_define.memo_ticket:
  90. return "/pages_sub/finance/memo_ticket/bill/bill"
  91. if source_bill_type == bill_define.purchase_apply:
  92. return "/pages_sub/purchase/purchase_apply/bill/bill"
  93. # if source_bill_type == bill_define.A_NOTICE_200:
  94. # return "/pages_sub/archives/notice/bill/bill"
  95. raise ValidationError(u'开发错误:没有设置来源单据类型{}对应的微信页面路径'.format(source_bill_type))
  96. def send(self):
  97. sub_domain, template_id, data, page = self._get_message_info()
  98. # self.fail_msg = u'测试数据,不发送'
  99. success, fail_msg = self.sudo().env['wxapp.user'].send_wx_message(self.user_id.id, sub_domain, template_id,
  100. data, page)
  101. if not success:
  102. self.sudo().write({'status': SEND_FAIL, 'fail_msg': fail_msg})
  103. return
  104. def _get_message_info(self):
  105. if self.sudo().message_type == APPROVE:
  106. if self.sudo().source_bill_type == bill_define.sale_order:
  107. return self._get_order_message()
  108. if self.sudo().source_bill_type == bill_define.sale_receipt:
  109. return self._get_receipt__or__memo_ticket__message()
  110. if self.sudo().source_bill_type == bill_define.sale_class_price:
  111. return self._get_class_price_message()
  112. if self.sudo().source_bill_type == bill_define.memo_ticket:
  113. return self._get_receipt__or__memo_ticket__message()
  114. if self.sudo().source_bill_type == bill_define.purchase_apply:
  115. return self._get_purchase_apply_message()
  116. # elif self.sudo().message_type == REMINDER:
  117. # if self.sudo().source_bill_type == bill_define.A_NOTICE_200:
  118. # return self._get_notice_message()
  119. elif self.sudo().message_type == APPROVE_SUCCESS:
  120. if self.sudo().source_bill_type == bill_define.sale_order:
  121. return self._get_order_message_result(u'通过')
  122. elif self.sudo().message_type == REFUSE:
  123. if self.sudo().source_bill_type == bill_define.sale_order:
  124. return self._get_order_message_result(u'驳回')
  125. raise ValidationError(u'未处理的消息种类')
  126. def _get_order_message(self):
  127. sub_domain = PRODUCT_NAME
  128. # template_id = 'NVwh3ddTQiFko5mPO5_ao-xQz6BNCeFappqQNbBU4mQ'#待审批提醒:审批标题/备注
  129. # template_id = 'NVwh3ddTQiFko5mPO5_ao_simcN8jqfRPSBDOE-3ZVs'#待审批提醒:审批标题/审批内容/金额/备注
  130. template_id = 'NVwh3ddTQiFko5mPO5_aox7OsHyWIabdQ-umWSQ0F8k'
  131. page = '/pages/message/list'
  132. data = {
  133. "thing19": { # 客户
  134. "value": self.sudo().title,
  135. },
  136. "thing20": { # 业务员
  137. "value": self.sudo().contents,
  138. },
  139. "amount14": { # 金额
  140. "value": str(self.sudo().number1),
  141. },
  142. "name5": { # 申请人
  143. "value": self.sudo().content1,
  144. },
  145. }
  146. return sub_domain, template_id, data, page
  147. # 参数approve_result:不超过5个汉字,只能是汉字
  148. def _get_order_message_result(self, approve_result):
  149. sub_domain = PRODUCT_NAME
  150. template_id = 'wYovlP0w9t03rTxMlu3HjJBmt2FObLLuFwWMlLijuWQ'
  151. page = '/pages/message/list'
  152. data = {
  153. "thing22": { # 客户
  154. "value": self.sudo().title,
  155. },
  156. "thing23": { # 业务员
  157. "value": self.sudo().contents,
  158. },
  159. "amount24": { # 金额
  160. "value": str(self.sudo().number1),
  161. },
  162. "phrase4": { # 审批结果
  163. "value": approve_result,
  164. },
  165. }
  166. return sub_domain, template_id, data, page
  167. def _get_receipt__or__memo_ticket__message(self):
  168. sub_domain = PRODUCT_NAME
  169. template_id = 'NVwh3ddTQiFko5mPO5_ao7M-c9FgnU8Pi9t3LybHNR8'
  170. page = '/pages/message/list'
  171. data = {
  172. "thing1": { # 审批标题:单位
  173. "value": self.sudo().title,
  174. },
  175. "amount14": { # 金额
  176. "value": str(self.sudo().number1),
  177. },
  178. "name5": { # 申请人
  179. "value": self.sudo().content1,
  180. },
  181. "thing8": { # 备注
  182. "value": self.sudo().content2,
  183. },
  184. }
  185. return sub_domain, template_id, data, page
  186. def _get_class_price_message(self):
  187. sub_domain = PRODUCT_NAME
  188. template_id = 'NVwh3ddTQiFko5mPO5_aowl3NGrnxuh-E-tk4L4JiTA'
  189. page = '/pages/message/list'
  190. data = {
  191. "thing1": { # 审批标题:名称
  192. "value": self.sudo().title,
  193. },
  194. "name5": { # 申请人
  195. "value": self.sudo().content1,
  196. },
  197. "thing8": { # 备注
  198. "value": self.sudo().content2,
  199. },
  200. }
  201. return sub_domain, template_id, data, page
  202. def _get_purchase_apply_message(self):
  203. sub_domain = PRODUCT_NAME
  204. template_id = 'NVwh3ddTQiFko5mPO5_ao0uVtPj5nWxqoTGrjDBv01w'
  205. page = '/pages/message/list'
  206. data = {
  207. "thing1": { # 审批标题:名称
  208. "value": self.sudo().title,
  209. },
  210. "name5": { # 申请人
  211. "value": self.sudo().contents,
  212. },
  213. "phrase7": { # 申请类型
  214. "value": self.sudo().content1,
  215. },
  216. "amount14": { # 金额
  217. "value": self.sudo().number1,
  218. },
  219. "thing8": { # 备注
  220. "value": self.sudo().content2,
  221. },
  222. }
  223. return sub_domain, template_id, data, page
  224. def _get_notice_message(self):
  225. sub_domain = PRODUCT_NAME
  226. template_id = 'fCVmc0_E41KG8_b50kkzTDXjT2DgvRtjmJKIbAu3Be0'
  227. page = '/pages/message/list'
  228. data = {
  229. "thing30": { # 标题
  230. "value": self.sudo().title,
  231. },
  232. "time11": { # 日期
  233. "value": self.sudo().contents,
  234. },
  235. "thing7": { # 通知内容
  236. "value": self.sudo().content1,
  237. },
  238. }
  239. return sub_domain, template_id, data, page
  240. def go_to_source_bill(self):
  241. res_model = self._get_bill_model()
  242. return {
  243. 'type': 'ir.actions.act_window',
  244. 'res_model': res_model,
  245. 'view_type': 'form',
  246. 'view_mode': 'form',
  247. 'target': 'current',
  248. 'res_id': self.source_bill_id,
  249. }
  250. def _get_bill_model(self):
  251. if self.source_bill_type == bill_define.sale_account:
  252. return 'jc_finance.sale_account'
  253. elif self.source_bill_type == bill_define.sale_invoice:
  254. return 'jc_finance.sale_invoice'
  255. elif self.source_bill_type == bill_define.sale_order:
  256. return 'jc_sale.sale_order'
  257. raise ValidationError(u'未处理的类型:' + str(self.source_bill_type))