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.

162 line
6.4KB

  1. # -*- coding: utf-8 -*-
  2. from odoo import _
  3. from odoo import fields, models, api
  4. import datetime
  5. class SellSummaryPartner(models.Model):
  6. _name = 'sell.summary.partner'
  7. _inherit = 'report.base'
  8. _description = '销售汇总表(按客户)'
  9. id_lists = fields.Text('移动明细行id列表')
  10. c_category = fields.Char('客户类别')
  11. partner = 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. c_categ.name AS c_category,
  30. partner.name AS partner,
  31. goods.code AS goods_code,
  32. goods.name AS goods,
  33. attr.name AS attribute,
  34. wh.name AS warehouse,
  35. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_uos_qty
  36. ELSE - wml.goods_uos_qty END) AS qty_uos,
  37. uos.name AS uos,
  38. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
  39. ELSE - wml.goods_qty END) AS qty,
  40. uom.name AS uom,
  41. (CASE WHEN SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
  42. ELSE - wml.goods_qty END) = 0 THEN 0
  43. ELSE
  44. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.amount
  45. ELSE - wml.amount END)
  46. / SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
  47. ELSE - wml.goods_qty END)
  48. END) AS price,
  49. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.amount
  50. ELSE - wml.amount END) AS amount,
  51. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.tax_amount
  52. ELSE - wml.tax_amount END) AS tax_amount,
  53. SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.subtotal
  54. ELSE - wml.subtotal END) AS subtotal,
  55. (SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.amount
  56. ELSE - wml.amount END) - SUM(CASE WHEN wm.origin = 'sell.delivery.sell' THEN wml.goods_qty
  57. ELSE - wml.goods_qty END) * wml.cost_unit) AS margin
  58. '''
  59. def from_sql(self, sql_type='out'):
  60. return '''
  61. FROM wh_move_line AS wml
  62. LEFT JOIN wh_move wm ON wml.move_id = wm.id
  63. LEFT JOIN partner ON wm.partner_id = partner.id
  64. LEFT JOIN core_category AS c_categ
  65. ON partner.c_category_id = c_categ.id
  66. LEFT JOIN goods ON wml.goods_id = goods.id
  67. LEFT JOIN attribute AS attr ON wml.attribute_id = attr.id
  68. LEFT JOIN warehouse AS wh ON wml.warehouse_id = wh.id
  69. OR wml.warehouse_dest_id = wh.id
  70. LEFT JOIN uom AS uos ON goods.uos_id = uos.id
  71. LEFT JOIN uom ON goods.uom_id = uom.id
  72. '''
  73. def where_sql(self, sql_type='out'):
  74. extra = ''
  75. if self.env.context.get('partner_id'):
  76. extra += ' AND partner.id = {partner_id}'
  77. if self.env.context.get('goods_id'):
  78. extra += ' AND goods.id = {goods_id}'
  79. if self.env.context.get('c_category_id'):
  80. extra += ' AND c_categ.id = {c_category_id}'
  81. if self.env.context.get('warehouse_id'):
  82. extra += ' AND wh.id = {warehouse_id}'
  83. return '''
  84. WHERE wml.state = 'done'
  85. AND wml.date >= '{date_start}'
  86. AND wml.date <= '{date_end}'
  87. AND wm.origin like 'sell.delivery%%'
  88. AND wh.type = 'stock'
  89. %s
  90. ''' % extra
  91. def group_sql(self, sql_type='out'):
  92. return '''
  93. GROUP BY c_category,partner,goods_code,goods,attribute,warehouse,uos,uom,wml.cost_unit
  94. '''
  95. def order_sql(self, sql_type='out'):
  96. return '''
  97. ORDER BY c_category,partner,goods_code,attribute,warehouse
  98. '''
  99. def get_context(self, sql_type='out', context=None):
  100. return {
  101. 'date_start': context.get('date_start') or '',
  102. 'date_end': context.get('date_end'),
  103. 'partner_id': context.get('partner_id') and
  104. context.get('partner_id')[0] or '',
  105. 'goods_id': context.get('goods_id') and
  106. context.get('goods_id')[0] or '',
  107. 'c_category_id': context.get('c_category_id') and
  108. context.get('c_category_id')[0] or '',
  109. 'warehouse_id': context.get('warehouse_id') and
  110. context.get('warehouse_id')[0] or '',
  111. }
  112. def _compute_order(self, result, order):
  113. order = order or 'partner ASC'
  114. return super(SellSummaryPartner, self)._compute_order(result, order)
  115. def collect_data_by_sql(self, sql_type='out'):
  116. collection = self.execute_sql(sql_type='out')
  117. return collection
  118. def view_detail(self):
  119. '''销售汇总表(按客户)查看明细按钮'''
  120. self.ensure_one()
  121. line_ids = []
  122. res = []
  123. move_lines = []
  124. result = self.get_data_from_cache()
  125. for line in result:
  126. if line.get('id') == self.id:
  127. line_ids = line.get('id_lists')
  128. move_lines = self.env['wh.move.line'].search(
  129. [('id', 'in', line_ids)])
  130. for move_line in move_lines:
  131. details = self.env['sell.order.detail'].search(
  132. [('order_name', '=', move_line.move_id.name),
  133. ('goods_id', '=', move_line.goods_id.id)])
  134. for detail in details:
  135. res.append(detail.id)
  136. return {
  137. 'name': _('销售明细表'),
  138. 'view_mode': 'list',
  139. 'view_id': False,
  140. 'res_model': 'sell.order.detail',
  141. 'type': 'ir.actions.act_window',
  142. 'domain': [('id', 'in', res)],
  143. }
上海开阖软件有限公司 沪ICP备12045867号-1