GoodERP
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

155 Zeilen
6.1KB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import _
  5. from odoo import fields, models, api
  6. from datetime import datetime, timedelta
  7. class BuySummaryGoods(models.Model):
  8. _name = 'buy.summary.goods'
  9. _inherit = 'report.base'
  10. _description = '采购汇总表(按商品)'
  11. id_lists = fields.Text('移动明细行id列表')
  12. goods_categ = fields.Char('商品类别')
  13. goods_code = fields.Char('商品编码')
  14. goods = fields.Char('商品名称')
  15. attribute = fields.Char('属性')
  16. warehouse_dest = fields.Char('仓库')
  17. qty_uos = fields.Float('辅助数量', digits='Quantity')
  18. uos = fields.Char('辅助单位')
  19. qty = fields.Float('基本数量', digits='Quantity')
  20. uom = fields.Char('基本单位')
  21. price = fields.Float('单价', digits='Price')
  22. amount = fields.Float('采购金额', digits='Amount')
  23. tax_amount = fields.Float('税额', digits='Amount')
  24. subtotal = 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. categ.name AS goods_categ,
  30. goods.code AS goods_code,
  31. goods.name AS goods,
  32. attr.name AS attribute,
  33. wh.name AS warehouse_dest,
  34. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_uos_qty
  35. ELSE - wml.goods_uos_qty END) AS qty_uos,
  36. uos.name AS uos,
  37. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_qty
  38. ELSE - wml.goods_qty END) AS qty,
  39. uom.name AS uom,
  40. (CASE WHEN SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_qty
  41. ELSE - wml.goods_qty END) = 0 THEN 0
  42. ELSE
  43. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.amount
  44. ELSE - wml.amount END)
  45. / SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_qty
  46. ELSE - wml.goods_qty END)
  47. END) AS price,
  48. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.amount
  49. ELSE - wml.amount END) AS amount,
  50. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.tax_amount
  51. ELSE - wml.tax_amount END) AS tax_amount,
  52. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.subtotal
  53. ELSE - wml.subtotal END) AS subtotal
  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_dest_id = wh.id
  64. OR wml.warehouse_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_dest_id'):
  77. extra += ' AND wh.id = {warehouse_dest_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 'buy%%'
  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_dest,uos,uom
  89. '''
  90. def order_sql(self, sql_type='out'):
  91. return '''
  92. ORDER BY goods_code,goods,attribute,warehouse_dest
  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') or '',
  98. 'partner_id': context.get('partner_id') and
  99. context.get('partner_id')[0] or '',
  100. 'goods_id': context.get('goods_id') and
  101. context.get('goods_id')[0] or '',
  102. 'goods_categ_id': context.get('goods_categ_id') and
  103. context.get('goods_categ_id')[0] or '',
  104. 'warehouse_dest_id': context.get('warehouse_dest_id') and
  105. context.get('warehouse_dest_id')[0] or '',
  106. }
  107. def _compute_order(self, result, order):
  108. order = order or 'goods_code ASC'
  109. return super(BuySummaryGoods, self)._compute_order(result, order)
  110. def collect_data_by_sql(self, sql_type='out'):
  111. collection = self.execute_sql(sql_type='out')
  112. return collection
  113. def view_detail(self):
  114. '''采购汇总表(按商品)查看明细按钮'''
  115. self.ensure_one()
  116. line_ids = []
  117. res = []
  118. move_lines = []
  119. result = self.get_data_from_cache()
  120. for line in result:
  121. if line.get('id') == self.id:
  122. line_ids = line.get('id_lists')
  123. move_lines = self.env['wh.move.line'].search(
  124. [('id', 'in', line_ids)])
  125. for move_line in move_lines:
  126. details = self.env['buy.order.detail'].search(
  127. [('order_name', '=', move_line.move_id.name),
  128. ('goods_id', '=', move_line.goods_id.id)])
  129. for detail in details:
  130. res.append(detail.id)
  131. return {
  132. 'name': _('采购汇总表'),
  133. 'view_mode': 'list',
  134. 'view_id': False,
  135. 'res_model': 'buy.order.detail',
  136. 'type': 'ir.actions.act_window',
  137. 'domain': [('id', 'in', res)],
  138. }
上海开阖软件有限公司 沪ICP备12045867号-1