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.

153 lines
6.2KB

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