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.

163 lines
6.5KB

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