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.

220 Zeilen
7.9KB

  1. # -*- coding: utf-8 -*-
  2. from odoo import _
  3. from odoo import models, fields, api
  4. from datetime import datetime, timedelta
  5. class ReportStockTransceive(models.TransientModel):
  6. _name = 'report.stock.transceive'
  7. _description = '商品收发明细表'
  8. goods = fields.Char('商品')
  9. code = fields.Char('编码')
  10. attribute = fields.Char('属性')
  11. id_lists = fields.Text('库存调拨id列表')
  12. uom = fields.Char('单位')
  13. warehouse = fields.Char('仓库')
  14. goods_qty_begain = fields.Float(
  15. '期初数量', digits='Quantity')
  16. cost_begain = fields.Float(
  17. '期初成本', digits='Amount')
  18. goods_qty_end = fields.Float(
  19. '期末数量', digits='Quantity')
  20. cost_end = fields.Float(
  21. '期末成本', digits='Amount')
  22. goods_qty_out = fields.Float(
  23. '出库数量', digits='Quantity')
  24. cost_out = fields.Float(
  25. '出库成本', digits='Amount')
  26. goods_qty_in = fields.Float(
  27. '入库数量', digits='Quantity')
  28. cost_in = fields.Float(
  29. '入库成本', digits='Amount')
  30. def select_sql(self, sql_type='out'):
  31. return '''
  32. SELECT goods.name as goods,
  33. goods.code as code,
  34. att.name as attribute,
  35. array_agg(line.id) as id_lists,
  36. uom.name as uom,
  37. wh.name as warehouse,
  38. sum(case when
  39. line.date < '{date_start}' THEN line.goods_qty ELSE 0 END)
  40. as goods_qty_begain,
  41. sum(case when
  42. line.date < '{date_start}' THEN line.cost ELSE 0 END)
  43. as cost_begain,
  44. sum(case when
  45. line.date <= '{date_end}' THEN line.goods_qty ELSE 0 END)
  46. as goods_qty_end,
  47. sum(case when
  48. line.date <= '{date_end}' THEN line.cost ELSE 0 END)
  49. as cost_end,
  50. sum(case when
  51. line.date <= '{date_end}' AND line.date >= '{date_start}'
  52. THEN
  53. line.goods_qty ELSE 0 END)
  54. as goods_qty,
  55. sum(case when
  56. line.date <= '{date_end}' AND line.date >= '{date_start}'
  57. THEN
  58. line.cost ELSE 0 END)
  59. as cost
  60. '''
  61. def from_sql(self, sql_type='out'):
  62. return '''
  63. FROM wh_move_line line
  64. LEFT JOIN goods goods ON line.goods_id = goods.id
  65. LEFT JOIN attribute att ON line.attribute_id = att.id
  66. LEFT JOIN uom uom ON line.uom_id = uom.id
  67. LEFT JOIN warehouse wh ON line.%s = wh.id
  68. ''' % (sql_type == 'out' and 'warehouse_id' or 'warehouse_dest_id')
  69. def where_sql(self, sql_type='out'):
  70. extra = ''
  71. if self.env.context.get('warehouse_id'):
  72. extra += ' AND wh.id = {warehouse_id}'
  73. if self.env.context.get('goods_id'):
  74. extra += ' AND goods.id = {goods_id}'
  75. return '''
  76. WHERE line.state = 'done'
  77. AND wh.type = 'stock'
  78. AND wh.active = true
  79. AND line.date <= '{date_end}'
  80. %s
  81. ''' % extra
  82. def group_sql(self, sql_type='out'):
  83. return '''
  84. GROUP BY goods.id, goods.code, att.name, uom.name, wh.name
  85. '''
  86. def order_sql(self, sql_type='out'):
  87. return '''
  88. ORDER BY goods.id, wh.name
  89. '''
  90. def get_record_key(self, record, sql_type='out'):
  91. return (
  92. record.get('goods'),
  93. record.get('uom'),
  94. record.get('warehouse'),
  95. record.get('attribute'),
  96. record.get('code')
  97. )
  98. def unzip_record_key(self, key):
  99. return {
  100. 'goods': key[0],
  101. 'uom': key[1],
  102. 'warehouse': key[2],
  103. 'attribute': key[3],
  104. 'code': key[4],
  105. }
  106. def get_default_value_by_record(self, record, sql_type='out'):
  107. return {
  108. 'id': record.get('id'),
  109. }
  110. def update_record_value(self, value, record, sql_type='out'):
  111. tag = sql_type == 'out' and -1 or 1
  112. value.update({
  113. 'goods_qty_begain': value.get('goods_qty_begain', 0) +
  114. (tag * record.get('goods_qty_begain', 0)),
  115. 'cost_begain': value.get('cost_begain', 0) +
  116. (tag * record.get('cost_begain', 0)),
  117. 'goods_qty_end': value.get('goods_qty_end', 0) +
  118. (tag * record.get('goods_qty_end', 0)),
  119. 'cost_end': value.get('cost_end', 0) +
  120. (tag * record.get('cost_end', 0)),
  121. 'goods_qty_out': value.get('goods_qty_out', 0) +
  122. (sql_type == 'out' and record.get('goods_qty', 0) or 0),
  123. 'cost_out': value.get('cost_out', 0) +
  124. (sql_type == 'out' and record.get('cost', 0) or 0),
  125. 'goods_qty_in': value.get('goods_qty_in', 0) +
  126. (sql_type == 'in' and record.get('goods_qty', 0) or 0),
  127. 'cost_in': value.get('cost_in', 0) +
  128. (sql_type == 'in' and record.get('cost', 0) or 0),
  129. 'id_lists': value.get('id_lists', []) + record.get('id_lists', []),
  130. })
  131. def compute_history_stock_by_collect(self, res, records, sql_type='out'):
  132. for record in records:
  133. record_key = self.get_record_key(record, sql_type=sql_type)
  134. if not res.get(record_key):
  135. res[record_key] = self.get_default_value_by_record(
  136. record, sql_type=sql_type)
  137. self.update_record_value(
  138. res[record_key], record, sql_type=sql_type)
  139. def execute_sql(self, sql_type='out', wizard_id=False):
  140. context = {
  141. 'date_start': wizard_id.date_start or '',
  142. 'date_end': wizard_id.date_end or '',
  143. 'warehouse_id': wizard_id.warehouse_id and wizard_id.warehouse_id[0].id or '',
  144. 'goods_id': wizard_id.goods_id and wizard_id.goods_id[0].id or '',
  145. }
  146. for key, value in list(context.items()):
  147. if key == "date_end":
  148. continue
  149. if key == "date_start":
  150. continue
  151. if isinstance(context[key], str):
  152. context[key] = value.encode('utf-8')
  153. self.env.context = dict(self.env.context, **context)
  154. search_sql = (self.select_sql(sql_type) + self.from_sql(sql_type) + self.where_sql(
  155. sql_type) + self.group_sql(sql_type) + self.order_sql(
  156. sql_type)).format(**context)
  157. self.env.cr.execute(search_sql)
  158. return self.env.cr.dictfetchall()
  159. def collect_data_by_sql(self, wizard_id=False):
  160. out_collection = self.execute_sql(sql_type='out', wizard_id=wizard_id)
  161. in_collection = self.execute_sql(sql_type='in', wizard_id=wizard_id)
  162. res = {}
  163. self.compute_history_stock_by_collect(res, in_collection, sql_type='in')
  164. self.compute_history_stock_by_collect(res, out_collection, sql_type='out')
  165. result = []
  166. for key, value in res.items():
  167. value.update(self.unzip_record_key(key))
  168. result.append(value)
  169. return result
  170. def find_source_move_line(self):
  171. # 查看库存调拨明细
  172. move_line_ids = []
  173. # 获得'report.stock.transceive'记录集
  174. date_start = self.env.context.get('date_start')
  175. date_end = self.env.context.get('date_end')
  176. domain_dict = [
  177. ('date', '>=', date_start),
  178. ('date', '<=', date_end),
  179. ('id', 'in', [int(mid) for mid in self.id_lists[1:-1].split(',')]),
  180. ]
  181. view = self.env.ref('warehouse.wh_move_line_list')
  182. return {
  183. 'name': (_('库存调拨') + str(date_start) +
  184. '~' + str(date_end) +
  185. '~' + self.goods),
  186. 'view_mode': 'list',
  187. 'views': [(view.id, 'list')],
  188. 'res_model': 'wh.move.line',
  189. 'type': 'ir.actions.act_window',
  190. 'domain': domain_dict
  191. }
上海开阖软件有限公司 沪ICP备12045867号-1