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.

155 lines
6.2KB

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