GoodERP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 line
6.5KB

  1. # -*- coding: utf-8 -*-
  2. from odoo import _
  3. from odoo import fields, models, api
  4. import datetime
  5. class SellSummaryStaff(models.Model):
  6. _name = 'sell.summary.staff'
  7. _inherit = 'report.base'
  8. _description = '销售汇总表(按销售人员)'
  9. id_lists = fields.Text('移动明细行id列表')
  10. # user_id = fields.Many2one('res.users', '销售员')
  11. salesman = fields.Char('销售员')
  12. goods_code = fields.Char('商品编号')
  13. goods = fields.Char('商品名称')
  14. attribute = fields.Char('属性')
  15. warehouse = fields.Char('仓库')
  16. qty_uos = fields.Float('辅助数量', digits='Quantity')
  17. uos = fields.Char('辅助单位')
  18. qty = fields.Float('基本数量', digits='Quantity')
  19. uom = fields.Char('基本单位')
  20. price = fields.Float('单价', digits='Price')
  21. amount = fields.Float('销售收入', digits='Amount')
  22. tax_amount = fields.Float('税额', digits='Amount')
  23. subtotal = fields.Float('价税合计', digits='Amount')
  24. margin = fields.Float('毛利', digits='Amount')
  25. def select_sql(self, sql_type='out'):
  26. return '''
  27. SELECT MIN(wml.id) as id,
  28. array_agg(wml.id) AS id_lists,
  29. res_partner.name AS salesman,
  30. goods.code AS goods_code,
  31. goods.name AS goods,
  32. attr.name AS attribute,
  33. wh.name AS warehouse,
  34. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_uos_qty
  35. ELSE - wml.goods_uos_qty END) AS qty_uos,
  36. uos.name AS uos,
  37. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
  38. ELSE - wml.goods_qty END) AS qty,
  39. uom.name AS uom,
  40. (CASE WHEN SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
  41. ELSE - wml.goods_qty END) = 0 THEN 0
  42. ELSE
  43. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.amount
  44. ELSE - wml.amount END)
  45. / SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
  46. ELSE - wml.goods_qty END)
  47. END) AS price,
  48. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.amount
  49. ELSE - wml.amount END) AS amount,
  50. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.tax_amount
  51. ELSE - wml.tax_amount END) AS tax_amount,
  52. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.subtotal
  53. ELSE - wml.subtotal END) AS subtotal,
  54. (SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.amount
  55. ELSE - wml.amount END) - SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
  56. ELSE - wml.goods_qty END) * wml.cost_unit) AS margin
  57. '''
  58. def from_sql(self, sql_type='out'):
  59. return '''
  60. FROM wh_move_line AS wml
  61. LEFT JOIN wh_move wm ON wml.move_id = wm.id
  62. LEFT JOIN sell_delivery AS sd
  63. ON wm.id = sd.sell_move_id
  64. LEFT JOIN res_users
  65. ON wm.user_id = res_users.id
  66. LEFT JOIN res_partner
  67. ON res_users.partner_id = res_partner.id
  68. LEFT JOIN goods ON wml.goods_id = goods.id
  69. LEFT JOIN core_category AS categ ON goods.category_id = categ.id
  70. LEFT JOIN attribute AS attr ON wml.attribute_id = attr.id
  71. LEFT JOIN warehouse AS wh ON wml.warehouse_id = wh.id
  72. OR wml.warehouse_dest_id = wh.id
  73. LEFT JOIN uom AS uos ON goods.uos_id = uos.id
  74. LEFT JOIN uom ON goods.uom_id = uom.id
  75. '''
  76. def where_sql(self, sql_type='out'):
  77. extra = ''
  78. if self.env.context.get('user_id'):
  79. extra += ' AND res_users.id = {user_id}'
  80. if self.env.context.get('goods_id'):
  81. extra += ' AND goods.id = {goods_id}'
  82. if self.env.context.get('goods_categ_id'):
  83. extra += ' AND categ.id = {goods_categ_id}'
  84. if self.env.context.get('warehouse_id'):
  85. extra += ' AND wh.id = {warehouse_id}'
  86. return '''
  87. WHERE wml.state = 'done'
  88. AND wml.date >= '{date_start}'
  89. AND wml.date <= '{date_end}'
  90. AND wm.origin like 'sell.delivery%%'
  91. AND wh.type = 'stock'
  92. %s
  93. ''' % extra
  94. def group_sql(self, sql_type='out'):
  95. return '''
  96. GROUP BY res_partner.name,goods_code,goods,attribute,warehouse,uos,uom,wml.cost_unit
  97. '''
  98. def order_sql(self, sql_type='out'):
  99. return '''
  100. ORDER BY res_partner.name,goods_code,attribute,warehouse
  101. '''
  102. def get_context(self, sql_type='out',context=None):
  103. return {
  104. 'date_start': context.get('date_start') or '',
  105. 'date_end': context.get('date_end'),
  106. 'user_id': context.get('user_id') and
  107. context.get('user_id')[0] or '',
  108. 'goods_id': context.get('goods_id') and
  109. context.get('goods_id')[0] or '',
  110. 'goods_categ_id': context.get('goods_categ_id') and
  111. context.get('goods_categ_id')[0] or '',
  112. 'warehouse_id': context.get('warehouse_id') and
  113. context.get('warehouse_id')[0] or '',
  114. }
  115. def _compute_order(self, result, order):
  116. order = order or 'salesman ASC'
  117. return super(SellSummaryStaff, self)._compute_order(result, order)
  118. def collect_data_by_sql(self, sql_type='out'):
  119. collection = self.execute_sql(sql_type='out')
  120. return collection
  121. def view_detail(self):
  122. '''销售汇总表(按销售人员)查看明细按钮'''
  123. self.ensure_one()
  124. line_ids = []
  125. res = []
  126. move_lines = []
  127. result = self.get_data_from_cache()
  128. for line in result:
  129. if line.get('id') == self.id:
  130. line_ids = line.get('id_lists')
  131. move_lines = self.env['wh.move.line'].search(
  132. [('id', 'in', line_ids)])
  133. for move_line in move_lines:
  134. details = self.env['sell.order.detail'].search(
  135. [('order_name', '=', move_line.move_id.name),
  136. ('goods_id', '=', move_line.goods_id.id)])
  137. for detail in details:
  138. res.append(detail.id)
  139. return {
  140. 'name': _('销售明细表'),
  141. 'view_mode': 'list',
  142. 'view_id': False,
  143. 'res_model': 'sell.order.detail',
  144. 'type': 'ir.actions.act_window',
  145. 'domain': [('id', 'in', res)],
  146. }
上海开阖软件有限公司 沪ICP备12045867号-1