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.

160 lines
6.4KB

  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 BuySummaryPartner(models.Model):
  6. _name = 'buy.summary.partner'
  7. _inherit = 'report.base'
  8. _description = '采购汇总表(按供应商)'
  9. id_lists = fields.Text('移动明细行id列表')
  10. date = fields.Date('日期')
  11. s_category = fields.Char('供应商类别')
  12. partner = fields.Char('供应商名称')
  13. partner_id = fields.Many2one('partner', '供应商')
  14. goods_code = fields.Char('商品编码')
  15. goods = fields.Char('商品名称')
  16. goods_id = fields.Many2one('goods', '商品')
  17. attribute = fields.Char('属性')
  18. warehouse_dest = fields.Char('仓库')
  19. qty_uos = fields.Float('辅助数量', digits='Quantity')
  20. uos = fields.Char('辅助单位')
  21. qty = fields.Float('基本数量', digits='Quantity')
  22. uom = fields.Char('基本单位')
  23. price = fields.Float('单价', digits='Price')
  24. amount = fields.Float('采购金额', digits='Amount')
  25. tax_amount = fields.Float('税额', digits='Amount')
  26. subtotal = fields.Float('价税合计', digits='Amount')
  27. def select_sql(self, sql_type='out'):
  28. return '''
  29. SELECT MIN(wml.id) as id,
  30. array_agg(wml.id) AS id_lists,
  31. MIN(wml.date) AS date,
  32. c_categ.name AS s_category,
  33. partner.name AS partner,
  34. partner.id AS partner_id,
  35. goods.code AS goods_code,
  36. goods.name AS goods,
  37. goods.id AS goods_id,
  38. attr.name AS attribute,
  39. wh.name AS warehouse_dest,
  40. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_uos_qty
  41. ELSE - wml.goods_uos_qty END) AS qty_uos,
  42. uos.name AS uos,
  43. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_qty
  44. ELSE - wml.goods_qty END) AS qty,
  45. uom.name AS uom,
  46. (CASE WHEN SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_qty
  47. ELSE - wml.goods_qty END) = 0 THEN 0
  48. ELSE
  49. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.amount
  50. ELSE - wml.amount END)
  51. / SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.goods_qty
  52. ELSE - wml.goods_qty END)
  53. END) AS price,
  54. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.amount
  55. ELSE - wml.amount END) AS amount,
  56. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.tax_amount
  57. ELSE - wml.tax_amount END) AS tax_amount,
  58. SUM(CASE WHEN wm.origin = 'buy.receipt.buy' THEN wml.subtotal
  59. ELSE - wml.subtotal END) AS subtotal
  60. '''
  61. def from_sql(self, sql_type='out'):
  62. return '''
  63. FROM wh_move_line AS wml
  64. LEFT JOIN wh_move wm ON wml.move_id = wm.id
  65. LEFT JOIN partner ON wm.partner_id = partner.id
  66. LEFT JOIN core_category AS c_categ
  67. ON partner.s_category_id = c_categ.id
  68. LEFT JOIN goods ON wml.goods_id = goods.id
  69. LEFT JOIN attribute AS attr ON wml.attribute_id = attr.id
  70. LEFT JOIN warehouse AS wh ON wml.warehouse_dest_id = wh.id
  71. OR wml.warehouse_id = wh.id
  72. LEFT JOIN uom AS uos ON goods.uos_id = uos.id
  73. LEFT JOIN uom ON goods.uom_id = uom.id
  74. '''
  75. def where_sql(self, sql_type='out'):
  76. extra = ''
  77. if self.env.context.get('partner_id'):
  78. extra += ' AND partner.id = {partner_id}'
  79. if self.env.context.get('goods_id'):
  80. extra += ' AND goods.id = {goods_id}'
  81. if self.env.context.get('s_category_id'):
  82. extra += ' AND c_categ.id = {s_category_id}'
  83. if self.env.context.get('warehouse_dest_id'):
  84. extra += ' AND wh.id = {warehouse_dest_id}'
  85. return '''
  86. WHERE wml.state = 'done'
  87. AND wml.date >= '{date_start}'
  88. AND wml.date <= '{date_end}'
  89. AND wm.origin like 'buy%%'
  90. AND wh.type = 'stock'
  91. %s
  92. ''' % extra
  93. def order_sql(self, sql_type='out'):
  94. return '''
  95. GROUP BY s_category,partner,goods_code,goods,
  96. attribute,warehouse_dest,uos,uom,goods.id,partner.id
  97. ORDER BY partner,goods_code,attribute,warehouse_dest
  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') or '',
  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. 's_category_id': context.get('s_category_id') and
  108. context.get('s_category_id')[0] or '',
  109. 'warehouse_dest_id': context.get('warehouse_dest_id') and
  110. context.get('warehouse_dest_id')[0] or '',
  111. }
  112. def _compute_order(self, result, order):
  113. order = order or 'partner ASC'
  114. return super(BuySummaryPartner, 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['buy.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': 'buy.order.detail',
  141. 'type': 'ir.actions.act_window',
  142. 'domain': [('id', 'in', res)],
  143. }
上海开阖软件有限公司 沪ICP备12045867号-1