order.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. # # -*- coding: utf-8 -*-
  2. #
  3. # import json
  4. #
  5. # from odoo import http, exceptions
  6. # from odoo.http import request
  7. #
  8. # from .. import defs
  9. # from .base import BaseController
  10. #
  11. # import logging
  12. #
  13. # _logger = logging.getLogger(__name__)
  14. #
  15. #
  16. # class WxappOrder(http.Controller, BaseController):
  17. #
  18. # @http.route('/<string:sub_domain>/order/create',
  19. # auth='public', methods=['POST'], csrf=False, type='http')
  20. # def create(self, sub_domain, **kwargs):
  21. # token = kwargs.pop('token', None)
  22. # try:
  23. # res, wechat_user, entry = self._check_user(sub_domain, token)
  24. # if res:return res
  25. #
  26. # # [{"goodsId":1,"number":3,"propertyChildIds":"1:1,2:4,","logisticsType":0, "inviter_id":0}]
  27. # goods_json = json.loads(kwargs.pop('goodsJsonStr'))
  28. # province_id = int(kwargs.pop('provinceId'))
  29. # city_id = int(kwargs.pop('cityId'))
  30. # district_id = int(kwargs.pop('districtId')) if 'districtId' in kwargs.keys() else False
  31. # zipcode = kwargs.pop('code')
  32. # link_man = kwargs.pop('linkMan')
  33. # calculate = kwargs.pop('calculate', False)
  34. # remark = kwargs.pop('remark', '')
  35. #
  36. # goods_price, logistics_price, total, goods_list = self.parse_goods_json(
  37. # goods_json, province_id, city_id, district_id, calculate
  38. # )
  39. #
  40. # order_dict = {
  41. # 'zipcode': zipcode,
  42. # 'partner_id': wechat_user.partner_id.id,
  43. # 'number_goods': sum(map(lambda r: r['product_uom_qty'], goods_list)),
  44. # 'logistics_price': logistics_price,
  45. # 'province_id': province_id,
  46. # 'city_id': city_id,
  47. # 'district_id': district_id,
  48. # 'team_id': entry.team_id.id,
  49. # 'note': remark,
  50. # 'linkman': link_man,
  51. # }
  52. # order_dict.update(kwargs)
  53. #
  54. # if calculate:
  55. # _data = {
  56. # 'score': 0,
  57. # 'isNeedLogistics': 1,
  58. # 'amountTotle': goods_price,
  59. # 'amountLogistics': logistics_price,
  60. # }
  61. # else:
  62. # order = request.env(user=1)['sale.order'].create(order_dict)
  63. # for each_goods in goods_list:
  64. # each_goods['order_id'] = order.id
  65. # request.env(user=1)['sale.order.line'].create(each_goods)
  66. #
  67. # #mail_template = request.env.ref('wechat_mall_order_create')
  68. # #mail_template.sudo().send_mail(order.id, force_send=True, raise_exception=False)
  69. # _data = {
  70. # "amountReal": order.total,
  71. # "dateAdd": order.create_date,
  72. # "id": order.id,
  73. # "orderNumber": order.name,
  74. # "status": defs.OrderResponseStatus.attrs[order.customer_status],
  75. # "statusStr": defs.OrderStatus.attrs[order.customer_status],
  76. # }
  77. #
  78. # return self.res_ok(_data)
  79. #
  80. # except Exception as e:
  81. # _logger.exception(e)
  82. # return self.res_err(-1, e.name)
  83. #
  84. # def parse_goods_json(self, goods_json, province_id, city_id, district_id, calculate):
  85. # """
  86. # :param goods_json: dict
  87. # :param province_id: 省
  88. # :param city_id: 市
  89. # :param district_id: 区
  90. # :return: goods_price, logistics_price, total, goods_list
  91. # """
  92. # # [{"goodsId":1,"number":3,"propertyChildIds":"1:1,2:4,","logisticsType":0, "inviter_id":0}]
  93. # goods_price, logistics_price = 0.0, 0.0
  94. # goods_list = []
  95. #
  96. # goods_id_set = set(map(lambda r: r['goodsId'], goods_json))
  97. # product_list = []
  98. # for data in goods_json:
  99. # rs = request.env['product.product'].sudo().search([
  100. # ('product_tmpl_id', '=', data['goodsId']),
  101. # ('attr_val_str', '=', data['propertyChildIds'])
  102. # ])
  103. # product_list += [p for p in rs]
  104. #
  105. # template_list = request.env['product.template'].sudo().search([
  106. # ('id', 'in', list(goods_id_set)),
  107. # ('wxapp_published', '=', True)
  108. # ])
  109. # template_dict = {template.id: template for template in template_list}
  110. #
  111. # if set(template_dict.keys()) - goods_id_set:
  112. # raise exceptions.ValidationError('订单中包含已下架的商品')
  113. #
  114. # for each_goods in goods_json:
  115. # property_child_ids = each_goods.get('propertyChildIds')
  116. # amount = each_goods['number']
  117. # transport_type = each_goods['logisticsType']
  118. # template = template_dict[each_goods['goodsId']]
  119. #
  120. # each_goods_price, each_goods_total, property_str, product = self.calculate_goods_fee(template, amount, property_child_ids, calculate)
  121. # each_logistics_price = self.calculate_logistics_fee(template, amount, transport_type, province_id, city_id, district_id)
  122. # goods_list.append({
  123. # 'product_id': product.id,
  124. # 'price_unit': each_goods_price,
  125. # 'product_uom_qty': amount,
  126. # })
  127. # goods_price += each_goods_total
  128. # logistics_price += each_logistics_price
  129. #
  130. # return goods_price, logistics_price, goods_price + logistics_price, goods_list
  131. #
  132. # def calculate_goods_fee(self, goods, amount, property_child_ids, calculate):
  133. # property_str = ''
  134. #
  135. # if 1:#property_child_ids:
  136. # property_child_ids = property_child_ids or ''
  137. # product = request.env['product.product'].sudo().search([
  138. # ('product_tmpl_id', '=', goods.id),
  139. # ('attr_val_str', '=', property_child_ids)
  140. # ])
  141. # if not product:
  142. # raise exceptions.ValidationError('商品不存在!')
  143. #
  144. # price = product.get_present_price()
  145. # total = price * amount
  146. # property_str = product.name
  147. #
  148. # stores = product.get_present_qty() - amount
  149. # if not property_child_ids:
  150. # stores = goods.get_present_qty() - amount
  151. #
  152. # if stores < 0:
  153. # raise exceptions.ValidationError('库存不足!')
  154. # if stores == 0:
  155. # # todo 发送库存空预警
  156. # pass
  157. # if not calculate:
  158. # product.sudo().change_qty(-amount)
  159. # if not property_child_ids:
  160. # goods.sudo().change_qty(-amount)
  161. #
  162. # return price, total, property_str, product
  163. #
  164. # def calculate_logistics_fee(self, goods, amount, transport_type, province_id, city_id, district_id):
  165. # return 0
  166. #
  167. #
  168. # @http.route('/<string:sub_domain>/order/statistics', auth='public', method=['GET'])
  169. # def statistics(self, sub_domain, token=None, **kwargs):
  170. # '''
  171. # closed = ('closed', u'已关闭')
  172. # unpaid = ('unpaid', u'待支付')
  173. # pending = ('pending', u'待发货')
  174. # unconfirmed = ('unconfirmed', u'待收货')
  175. # unevaluated = ('unevaluated', u'待评价')
  176. # completed = ('completed', u'已完成')
  177. # '''
  178. # try:
  179. # res, wechat_user, entry = self._check_user(sub_domain, token)
  180. # if res:return res
  181. #
  182. # orders = request.env['sale.order'].sudo().search([('partner_id', '=', wechat_user.partner_id.id)])
  183. # order_statistics_dict = {order_status: 0 for order_status in defs.OrderStatus.attrs.keys()}
  184. # for each_order in orders:
  185. # order_statistics_dict[each_order.customer_status] += 1
  186. #
  187. # data = {
  188. # "count_id_no_reputation": order_statistics_dict['unevaluated'],
  189. # "count_id_no_transfer": order_statistics_dict['pending'],
  190. # "count_id_close": order_statistics_dict['closed'],
  191. # "count_id_no_pay": order_statistics_dict['unpaid'],
  192. # "count_id_no_confirm": order_statistics_dict['unconfirmed'],
  193. # "count_id_success": order_statistics_dict['completed']
  194. # }
  195. # return self.res_ok(data)
  196. #
  197. # except Exception as e:
  198. # _logger.exception(e)
  199. # return self.res_err(-1, e.name)
  200. #
  201. #
  202. # @http.route('/<string:sub_domain>/order/list', auth='public', method=['GET'])
  203. # def list(self, sub_domain, token=None, status=None, **kwargs):
  204. # try:
  205. # res, wechat_user, entry = self._check_user(sub_domain, token)
  206. # if res:return res
  207. #
  208. # if status is not None:
  209. # orders = request.env['sale.order'].sudo().search([
  210. # ('partner_id', '=', wechat_user.partner_id.id),
  211. # ('customer_status', '=', defs.OrderRequestStatus.attrs[int(status)])
  212. # ])
  213. # else:
  214. # orders = request.env['sale.order'].search([
  215. # ('partner_id', '=', wechat_user.partner_id)
  216. # ])
  217. #
  218. # data = {
  219. # "orderList": [{
  220. # "amountReal": each_order.total,
  221. # "dateAdd": each_order.create_date,
  222. # "id": each_order.id,
  223. # "orderNumber": each_order.name,
  224. # "status": defs.OrderResponseStatus.attrs[each_order.customer_status],
  225. # "statusStr": defs.OrderStatus.attrs[each_order.customer_status],
  226. # } for each_order in orders],
  227. # "goodsMap": {
  228. # each_order.id: [
  229. # {
  230. # "pic": each_goods.product_id.product_tmpl_id.get_main_image(),
  231. # } for each_goods in each_order.order_line]
  232. # for each_order in orders}
  233. # }
  234. # return self.res_ok(data)
  235. #
  236. # except Exception as e:
  237. # _logger.exception(e)
  238. # return self.res_err(-1, e.name)
  239. #
  240. #
  241. # @http.route('/<string:sub_domain>/order/detail', auth='public', method=['GET'])
  242. # def detail(self, sub_domain, token=None, id=None, **kwargs):
  243. # order_id = id
  244. # try:
  245. # res, wechat_user, entry = self._check_user(sub_domain, token)
  246. # if res:return res
  247. #
  248. # if not order_id:
  249. # return self.res_err(300)
  250. #
  251. # order = request.env['sale.order'].sudo().search([
  252. # ('partner_id', '=', wechat_user.partner_id.id),
  253. # ('id', '=', int(order_id))
  254. # ])
  255. #
  256. # if not order:
  257. # return self.res_err(404)
  258. #
  259. # data = {
  260. # "code": 0,
  261. # "data": {
  262. # "orderInfo": {
  263. # "amount": order.amount_total,
  264. # "amountLogistics": order.logistics_price,
  265. # "amountReal": order.total,
  266. # "dateAdd": order.create_date,
  267. # "dateUpdate": order.write_date,
  268. # "goodsNumber": order.number_goods,
  269. # "id": order.id,
  270. # "orderNumber": order.name,
  271. # "remark": order.note,
  272. # "status": defs.OrderResponseStatus.attrs[order.customer_status],
  273. # "statusStr": defs.OrderStatus.attrs[order.customer_status],
  274. # "type": 0,
  275. # "uid": 1,#user.id,
  276. # "userId": wechat_user.id
  277. # },
  278. # "goods": [
  279. # {
  280. # "amount": each_goods.price_unit,
  281. # "goodsId": each_goods.product_id.product_tmpl_id.id,
  282. # "goodsName": each_goods.name,
  283. # "id": each_goods.id,
  284. # "number": each_goods.product_uom_qty,
  285. # "orderId": order.id,
  286. # "pic": each_goods.product_id.product_tmpl_id.get_main_image(),
  287. # "property": each_goods.product_id.get_property_str(),
  288. # } for each_goods in order.order_line
  289. # ],
  290. # "logistics": {
  291. # "address": order.address,
  292. # "cityId": order.city_id.id,
  293. # "code": order.zipcode,
  294. # "dateUpdate": order.write_date,
  295. # "districtId": order.district_id.id or 0,
  296. # "linkMan": order.linkman,
  297. # "mobile": order.mobile,
  298. # "provinceId": order.province_id.id,
  299. # "shipperCode": order.shipper_id.code if order.shipper_id else '',
  300. # "shipperName": order.shipper_id.name if order.shipper_id else '',
  301. # "status": 0 if order.shipper_id else '',
  302. # "trackingNumber": order.shipper_no if order.shipper_no else ''
  303. # },
  304. # },
  305. # "msg": "success"
  306. # }
  307. # if order.shipper_no:
  308. # self.build_traces(order, data)
  309. #
  310. # return self.res_ok(data["data"])
  311. #
  312. # except Exception as e:
  313. # _logger.exception(e)
  314. # return self.res_err(-1, e.name)
  315. #
  316. # def build_traces(self, order, data):
  317. # pass
  318. #
  319. # @http.route('/<string:sub_domain>/order/close', auth='public', method=['GET'])
  320. # def close(self, sub_domain, token=None, orderId=None, **kwargs):
  321. # order_id = orderId
  322. # try:
  323. # res, wechat_user, entry = self._check_user(sub_domain, token)
  324. # if res:return res
  325. #
  326. # if not order_id:
  327. # return self.res_err(300)
  328. #
  329. # order = request.env['sale.order'].sudo().search([
  330. # ('partner_id', '=', wechat_user.partner_id.id),
  331. # ('id', '=', int(order_id))
  332. # ])
  333. #
  334. # if not order:
  335. # return self.res_err(404)
  336. #
  337. # order.write({'customer_status': 'closed'})
  338. # order.action_cancel()
  339. #
  340. # #mail_template = request.env.ref('wechat_mall_order_closed')
  341. # #mail_template.sudo().send_mail(order.id, force_send=True, raise_exception=False)
  342. #
  343. # return self.res_ok()
  344. #
  345. # except Exception as e:
  346. # _logger.exception(e)
  347. # return self.res_err(-1, e.name)
  348. #
  349. #
  350. # @http.route('/<string:sub_domain>/order/delivery', auth='public', method=['GET'])
  351. # def delivery(self, sub_domain, token=None, orderId=None, **kwargs):
  352. # '''
  353. # 确认收货接口
  354. # '''
  355. # order_id = orderId
  356. # try:
  357. # res, wechat_user, entry = self._check_user(sub_domain, token)
  358. # if res:return res
  359. #
  360. # if not order_id:
  361. # return self.res_err(300)
  362. #
  363. # order = request.env['sale.order'].sudo().search([
  364. # ('partner_id', '=', wechat_user.partner_id.id),
  365. # ('id', '=', int(order_id))
  366. # ])
  367. #
  368. # if not order:
  369. # return self.res_err(404)
  370. #
  371. # order.write({'customer_status': 'unevaluated'})
  372. #
  373. # #mail_template = request.env.ref('wechat_mall_order_confirmed')
  374. # #mail_template.sudo().send_mail(order.id, force_send=True, raise_exception=False)
  375. #
  376. # return self.res_ok()
  377. #
  378. # except Exception as e:
  379. # _logger.exception(e)
  380. # return self.res_err(-1, e.name)
  381. #
  382. #
  383. # @http.route('/<string:sub_domain>/order/reputation', auth='public', method=['GET'])
  384. # def reputation(self, sub_domain, token=None, order_id=None, reputation=2, **kwargs):
  385. # '''
  386. # 评论接口
  387. # {
  388. # "token": "xxx",
  389. # "orderId": "4",
  390. # "reputations": [{
  391. # "id": "4",
  392. # "reputation": "2",
  393. # "remark": "xxx"
  394. # }]
  395. # }
  396. # '''
  397. # try:
  398. # post_json = json.loads(kwargs.pop('postJsonString'))
  399. # token = post_json.get('token',None)
  400. # order_id = post_json.get('orderId',None)
  401. # reputations = post_json.get('reputations',[])
  402. #
  403. # res, wechat_user, entry = self._check_user(sub_domain, token)
  404. # if res:return res
  405. #
  406. # if not order_id:
  407. # return self.res_err(300)
  408. #
  409. # order = request.env['sale.order'].sudo().search([
  410. # ('partner_id', '=', wechat_user.partner_id.id),
  411. # ('id', '=', int(order_id))
  412. # ])
  413. #
  414. # if not order:
  415. # return self.res_err(404)
  416. #
  417. # order.write({'customer_status': 'completed'})
  418. #
  419. # for reputation in reputations:
  420. # # 保存评论
  421. # pass
  422. #
  423. # return request.make_response(json.dumps({'code': 0, 'msg': 'success'}))
  424. #
  425. # except Exception as e:
  426. # _logger.exception(e)
  427. # return self.res_err(-1, e.name)
  428. #
  429. #
  430. # @http.route('/<string:sub_domain>/order/pay', auth='public', method=['POST'], csrf=False)
  431. # def pay(self, sub_domain, token=None, orderId=None, **kwargs):
  432. # order_id = orderId
  433. # try:
  434. # res, wechat_user, entry = self._check_user(sub_domain, token)
  435. # if res:return res
  436. #
  437. # if not order_id:
  438. # return self.res_err(300)
  439. #
  440. # order = request.env['sale.order'].sudo().search([
  441. # ('partner_id', '=', wechat_user.partner_id.id),
  442. # ('id', '=', int(order_id))
  443. # ])
  444. #
  445. # if not order:
  446. # return self.res_err(404)
  447. #
  448. # order.write({'customer_status': 'pending'})
  449. # order.action_confirm()
  450. # return request.make_response(json.dumps({'code': 0, 'msg': 'success'}))
  451. #
  452. # except Exception as e:
  453. # _logger.exception(e)
  454. # return self.res_err(-1, e.name)
  455. #