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.1KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import fields, models, api
  4. from datetime import datetime, timedelta
  5. class BuySummaryGoods(models.Model):
  6. _name = 'buy.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_dest = 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. 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. categ.name AS goods_categ,
  28. goods.code AS goods_code,
  29. goods.name AS goods,
  30. attr.name AS attribute,
  31. wh.name AS warehouse_dest,
  32. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' 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 = 'buy.receipt.buy' 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 = 'buy.receipt.buy' THEN wml.goods_qty
  39. ELSE - wml.goods_qty END) = 0 THEN 0
  40. ELSE
  41. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.amount
  42. ELSE - wml.amount END)
  43. / SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_qty
  44. ELSE - wml.goods_qty END)
  45. END) AS price,
  46. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.amount
  47. ELSE - wml.amount END) AS amount,
  48. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.tax_amount
  49. ELSE - wml.tax_amount END) AS tax_amount,
  50. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.subtotal
  51. ELSE - wml.subtotal END) AS subtotal
  52. '''
  53. def from_sql(self, sql_type='out'):
  54. return '''
  55. FROM wh_move_line AS wml
  56. LEFT JOIN wh_move wm ON wml.move_id = wm.id
  57. LEFT JOIN partner ON wm.partner_id = partner.id
  58. LEFT JOIN goods ON wml.goods_id = goods.id
  59. LEFT JOIN core_category AS categ ON goods.category_id = categ.id
  60. LEFT JOIN attribute AS attr ON wml.attribute_id = attr.id
  61. LEFT JOIN warehouse AS wh ON wml.warehouse_dest_id = wh.id
  62. OR wml.warehouse_id = wh.id
  63. LEFT JOIN uom AS uos ON goods.uos_id = uos.id
  64. LEFT JOIN uom ON goods.uom_id = uom.id
  65. '''
  66. def where_sql(self, sql_type='out'):
  67. extra = ''
  68. if self.env.context.get('partner_id'):
  69. extra += ' AND partner.id = {partner_id}'
  70. if self.env.context.get('goods_id'):
  71. extra += ' AND goods.id = {goods_id}'
  72. if self.env.context.get('goods_categ_id'):
  73. extra += ' AND categ.id = {goods_categ_id}'
  74. if self.env.context.get('warehouse_dest_id'):
  75. extra += ' AND wh.id = {warehouse_dest_id}'
  76. return '''
  77. WHERE wml.state = 'done'
  78. AND wml.date >= '{date_start}'
  79. AND wml.date <= '{date_end}'
  80. AND wm.origin like 'buy%%'
  81. AND wh.type = 'stock'
  82. %s
  83. ''' % extra
  84. def group_sql(self, sql_type='out'):
  85. return '''
  86. GROUP BY goods_categ,goods_code,goods,attribute,warehouse_dest,uos,uom
  87. '''
  88. def order_sql(self, sql_type='out'):
  89. return '''
  90. ORDER BY goods_code,goods,attribute,warehouse_dest
  91. '''
  92. def get_context(self, sql_type='out', context=None):
  93. return {
  94. 'date_start': context.get('date_start') or '',
  95. 'date_end': context.get('date_end') or '',
  96. 'partner_id': context.get('partner_id') and
  97. context.get('partner_id')[0] or '',
  98. 'goods_id': context.get('goods_id') and
  99. context.get('goods_id')[0] or '',
  100. 'goods_categ_id': context.get('goods_categ_id') and
  101. context.get('goods_categ_id')[0] or '',
  102. 'warehouse_dest_id': context.get('warehouse_dest_id') and
  103. context.get('warehouse_dest_id')[0] or '',
  104. }
  105. def _compute_order(self, result, order):
  106. order = order or 'goods_code ASC'
  107. return super(BuySummaryGoods, 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['buy.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': 'buy.order.detail',
  134. 'type': 'ir.actions.act_window',
  135. 'domain': [('id', 'in', res)],
  136. }
上海开阖软件有限公司 沪ICP备12045867号-1