GoodERP
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

122 行
4.8KB

  1. from datetime import date
  2. from odoo import models, fields, api
  3. from odoo.exceptions import UserError
  4. class SellOrderTrackWizard(models.TransientModel):
  5. _name = 'sell.order.track.wizard'
  6. _description = '销售订单跟踪表向导'
  7. @api.model
  8. def _default_date_start(self):
  9. return self.env.user.company_id.start_date
  10. @api.model
  11. def _default_date_end(self):
  12. return date.today()
  13. date_start = fields.Date('开始日期', default=_default_date_start,
  14. help='报表汇总的开始日期,默认为公司启用日期')
  15. date_end = fields.Date('结束日期', default=_default_date_end,
  16. help='报表汇总的结束日期,默认为当前日期')
  17. partner_id = fields.Many2one('partner', '客户',
  18. help='只统计选定的客户')
  19. goods_id = fields.Many2one('goods', '商品',
  20. help='只统计选定的商品')
  21. user_id = fields.Many2one('res.users', '销售员',
  22. help='只统计选定的销售员')
  23. warehouse_id = fields.Many2one('warehouse', '仓库',
  24. help='只统计选定的仓库')
  25. company_id = fields.Many2one(
  26. 'res.company',
  27. string='公司',
  28. change_default=True,
  29. default=lambda self: self.env.company)
  30. def _get_domain(self):
  31. '''返回wizard界面上条件'''
  32. domain = [
  33. ('order_id.date', '>=', self.date_start),
  34. ('order_id.date', '<=', self.date_end),
  35. ('order_id.state', '=', 'done')
  36. ]
  37. if self.goods_id:
  38. domain.append(('goods_id', '=', self.goods_id.id))
  39. if self.partner_id:
  40. domain.append(('order_id.partner_id', '=', self.partner_id.id))
  41. if self.user_id:
  42. domain.append(('order_id.user_id', '=', self.user_id.id))
  43. if self.warehouse_id:
  44. domain.append(('order_id.warehouse_id', '=', self.warehouse_id.id))
  45. return domain
  46. def _get_wh_out_date(self, line):
  47. '''对于一个 sell order line,返回一个出库日期'''
  48. wh_out_date = None
  49. wh_move_line = self.env['wh.move.line'].search([
  50. ('sell_line_id', '=', line.id),
  51. ('state', '=', 'done'),
  52. ])
  53. if len(wh_move_line) > 1: # 如果是分批出库,则出库单明细行上的sell_line_id相同
  54. wh_out_date = wh_move_line[0].date
  55. else:
  56. wh_out_date = wh_move_line.date
  57. return wh_out_date
  58. def _prepare_track_line(self, line, qty, amount, qty_not_out):
  59. '''返回跟踪表明细行(非小计行)'''
  60. return {
  61. 'goods_code': line.goods_id.code,
  62. 'goods_id': line.goods_id.id,
  63. 'attribute': line.attribute_id.name,
  64. 'uom': line.uom_id.name,
  65. 'date': line.order_id.date,
  66. 'order_name': line.order_id.name,
  67. 'user_id': line.order_id.user_id.id,
  68. 'partner_id': line.order_id.partner_id.id,
  69. 'warehouse_id': line.order_id.warehouse_id.id,
  70. 'goods_state': line.order_id.goods_state,
  71. 'qty': qty,
  72. 'amount': amount,
  73. 'qty_not_out': qty_not_out,
  74. 'delivery_date': line.order_id.delivery_date,
  75. 'wh_out_date': self._get_wh_out_date(line), # 出库日期
  76. 'note': line.note,
  77. 'type': line.order_id.type,
  78. }
  79. def button_ok(self):
  80. self.ensure_one()
  81. res = []
  82. if self.date_end < self.date_start:
  83. raise UserError('开始日期不能大于结束日期!\n所选开始日期:%s 所选结束日期:%s' %
  84. (self.date_start, self.date_end))
  85. sell_order_line = self.env['sell.order.line']
  86. for line in sell_order_line.search(self._get_domain(), order='goods_id'):
  87. is_sell = line.order_id.type == 'sell' and 1 or -1 # 是否销售订单
  88. # 以下分别为明细行上数量、销售额、未出库数量,退货时均取反
  89. qty = is_sell * line.quantity
  90. amount = is_sell * line.amount
  91. qty_not_out = is_sell * (line.quantity - line.quantity_out)
  92. # 创建跟踪表明细行(非小计行)
  93. track = self.env['sell.order.track'].create(
  94. self._prepare_track_line(line, qty, amount, qty_not_out))
  95. res.append(track.id)
  96. view = self.env.ref('sell.sell_order_track_list')
  97. return {
  98. 'name': '销售订单跟踪表',
  99. 'view_mode': 'list',
  100. 'view_id': False,
  101. 'views': [(view.id, 'list')],
  102. 'res_model': 'sell.order.track',
  103. 'type': 'ir.actions.act_window',
  104. 'target': 'main',
  105. 'domain': [('id', 'in', res)],
  106. 'limit': 65535,
  107. }
上海开阖软件有限公司 沪ICP备12045867号-1