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 satır
6.4KB

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