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.

365 lines
18KB

  1. from odoo import models, fields, api
  2. from datetime import datetime, timedelta
  3. class ReportStockReceiptDelivery(models.Model):
  4. _name = 'report.stock.receipt.delivery'
  5. _description = '商品收发汇总表'
  6. _inherit = 'report.base'
  7. date = fields.Date('日期')
  8. goods_class = fields.Char('商品类别')
  9. code = fields.Char('商品编号')
  10. goods = fields.Char('商品名称')
  11. uom = fields.Char('单位')
  12. attribute = fields.Char('属性')
  13. id_lists = fields.Text('库存调拨id列表')
  14. warehouse = fields.Char('仓库')
  15. origin = fields.Char('业务类型')
  16. goods_qty_begin = fields.Float(
  17. '期初数量', digits='Quantity')
  18. cost_begin = fields.Float(
  19. '期初成本', digits='Amount')
  20. goods_qty_end = fields.Float(
  21. '期末数量', digits='Quantity')
  22. cost_end = fields.Float(
  23. '期末成本', digits='Amount')
  24. goods_qty_out = fields.Float(
  25. '出库数量', digits='Quantity')
  26. cost_out = fields.Float(
  27. '出库成本', digits='Amount')
  28. goods_qty_in = fields.Float(
  29. '入库数量', digits='Quantity')
  30. cost_in = fields.Float(
  31. '入库成本', digits='Amount')
  32. # 入库数据
  33. goods_buy_receipt = fields.Float(
  34. '采购入库', digits='Quantity')
  35. cost_buy_receipt = fields.Float(
  36. '采购成本', digits='Amount')
  37. goods_in_others = fields.Float(
  38. '其他入库', digits='Quantity')
  39. cost_in_others = fields.Float(
  40. '其他成本', digits='Amount')
  41. goods_in_inventory = fields.Float(
  42. '盘盈入库', digits='Quantity')
  43. cost_in_inventory = fields.Float(
  44. '盘盈成本', digits='Amount')
  45. goods_delivery_return = fields.Float(
  46. '销售退货', digits='Quantity')
  47. cost_delivery_return = fields.Float(
  48. '退货成本', digits='Amount')
  49. goods_assembly_in = fields.Float(
  50. '组装入库', digits='Quantity')
  51. cost_assembly_in = fields.Float(
  52. '组装入库成本', digits='Amount')
  53. goods_internal_in = fields.Float(
  54. '调拨入库', digits='Quantity')
  55. cost_internal_in = fields.Float(
  56. '调拨入库成本', digits='Amount')
  57. # 出库数据
  58. goods_delivery_sell = fields.Float(
  59. '销售出库', digits='Quantity')
  60. cost_delivery_sell = fields.Float(
  61. '出库成本', digits='Amount')
  62. goods_out_others = fields.Float(
  63. '其他出库', digits='Quantity')
  64. cost_out_others = fields.Float(
  65. '其他成本', digits='Amount')
  66. goods_out_inventory = fields.Float(
  67. '盘亏出库', digits='Quantity')
  68. cost_out_inventory = fields.Float(
  69. '盘亏成本', digits='Amount')
  70. goods_receipt_return = fields.Float(
  71. '采购退货', digits='Quantity')
  72. cost_receipt_return = fields.Float(
  73. '退货成本', digits='Amount')
  74. goods_assembly_out = fields.Float(
  75. '组装出库', digits='Quantity')
  76. cost_assembly_out = fields.Float(
  77. '组装出库成本', digits='Amount')
  78. goods_internal_out = fields.Float(
  79. '调拨出库', digits='Quantity')
  80. cost_internal_out = fields.Float(
  81. '调拨出库成本', digits='Amount')
  82. def select_sql_inherit(self, sql_type='out', sql_origin=''):
  83. return '''
  84. SELECT min(line.id) as id,
  85. goods.name as goods,
  86. gc.name as goods_class,
  87. goods.code as code,
  88. att.name as attribute,
  89. array_agg(line.id) as id_lists,
  90. uom.name as uom,
  91. wh.name as warehouse,
  92. case
  93. when wm.origin = 'buy.receipt.buy' then '采购入库'
  94. when wm.origin = 'wh.in.others' then '其他入库'
  95. when wm.origin = 'wh.in.inventory' then '盘盈'
  96. when wm.origin = 'sell.delivery.return' then '销售退货'
  97. when wm.origin = 'sell.delivery.sell' then '销售出库'
  98. when wm.origin = 'wh.out.others' then '其他出库'
  99. when wm.origin = 'wh.out.inventory' then '盘亏'
  100. when wm.origin = 'buy.receipt.return' then '采购退货'
  101. when wm.origin = 'wh.internal' then '%s'
  102. when wm.origin = 'wh.assembly' and line.type = 'out' then '组装单子件'
  103. when wm.origin = 'wh.assembly' and line.type = 'in' then '组装单组合件'
  104. when wm.origin = 'outsource' and line.type = 'out' then '委外单子件'
  105. when wm.origin = 'outsource' and line.type = 'in' then '委外单组合件'
  106. end as origin,
  107. att.name as attribute,
  108. array_agg(line.id order by line.id) as id_lists,
  109. wh.name as warehouse,
  110. sum(case when
  111. line.date < '{date_start}' THEN line.goods_qty ELSE 0 END)
  112. as goods_qty_begin,
  113. sum(case when
  114. line.date < '{date_start}' THEN line.cost ELSE 0 END)
  115. as cost_begin,
  116. sum(case when
  117. line.date <= '{date_end}' THEN line.goods_qty ELSE 0 END)
  118. as goods_qty_end,
  119. sum(case when
  120. line.date <= '{date_end}' THEN line.cost ELSE 0 END)
  121. as cost_end,
  122. sum(case when
  123. line.date <= '{date_end}' AND line.date >= '{date_start}'
  124. THEN
  125. line.goods_qty ELSE 0 END)
  126. as goods_qty,
  127. sum(case when
  128. line.date <= '{date_end}' AND line.date >= '{date_start}'
  129. THEN
  130. line.cost ELSE 0 END)
  131. as cost
  132. ''' % ('调拨出库' if sql_origin == 'internal_in' else '调拨入库')
  133. def from_sql_inherit(self, sql_type='out', sql_origin=''):
  134. return '''
  135. FROM wh_move_line line
  136. inner join wh_move wm on wm.id = line.move_id
  137. inner join goods goods ON line.goods_id = goods.id
  138. left join goods_class gc on gc.id = goods.goods_class_id
  139. LEFT JOIN attribute att ON line.attribute_id = att.id
  140. LEFT JOIN uom uom ON line.uom_id = uom.id
  141. LEFT JOIN warehouse wh ON line.%s = wh.id
  142. ''' % ((sql_type == 'out' or (sql_type == 'internal' and sql_origin == 'internal_in')) and 'warehouse_id'
  143. or 'warehouse_dest_id')
  144. def where_sql_inherit(self, sql_type='out', sql_origin=''):
  145. extra = ''
  146. if self.env.context.get('warehouse_id'):
  147. extra += 'AND wh.id = {warehouse_id}'
  148. if self.env.context.get('goods_id'):
  149. extra += 'AND goods.id = {goods_id}'
  150. return '''
  151. WHERE line.state = 'done'
  152. AND wh.type = 'stock'
  153. AND wh.active = true
  154. AND line.date <= '{date_end}'
  155. %s
  156. ''' % extra
  157. def group_sql_inherit(self, sql_type='out', sql_origin=''):
  158. return '''
  159. GROUP BY gc.name, goods.id, wm.origin, line.type, goods.code,
  160. att.name, uom.name, wh.name, line.warehouse_id, line.type
  161. '''
  162. def order_sql_inherit(self, sql_type='out', sql_origin=''):
  163. return '''
  164. ORDER BY goods.id, wh.name
  165. '''
  166. def get_context_inherit(self, sql_type='out', context=None):
  167. return {
  168. 'date_start': context.get('date_start') or '',
  169. 'date_end': context.get('date_end'),
  170. 'warehouse_id': context.get('warehouse_id') and context.get('warehouse_id')[0] or '',
  171. 'goods_id': context.get('goods_id') and context.get('goods_id')[0] or '',
  172. }
  173. def make_hashable(self, item):
  174. """将可能包含字典的元素转换为可哈希形式"""
  175. if isinstance(item, dict):
  176. # 转换为元组的元组,确保顺序一致
  177. return tuple(sorted((k, self.make_hashable(v)) for k, v in item.items()))
  178. elif isinstance(item, list):
  179. return tuple(self.make_hashable(x) for x in item)
  180. else:
  181. return item
  182. def get_record_key(self, record, sql_type='out'):
  183. return (
  184. self.make_hashable(record.get('goods')),
  185. self.make_hashable(record.get('uom')),
  186. self.make_hashable(record.get('warehouse')),
  187. self.make_hashable(record.get('attribute')),
  188. self.make_hashable(record.get('code')),
  189. self.make_hashable(record.get('goods_class'))
  190. )
  191. def unzip_record_key(self, key):
  192. return {
  193. 'goods': key[0],
  194. 'uom': key[1],
  195. 'warehouse': key[2],
  196. 'attribute': key[3],
  197. 'code': key[4],
  198. 'goods_class': key[5]
  199. }
  200. def get_default_value_by_record(self, record, sql_type='out', sql_origin=''):
  201. return {
  202. 'id': record.get('id'),
  203. }
  204. def update_record_value(self, value, record, sql_type='out', sql_origin=''):
  205. tag = sql_type == 'out' and -1 or 1
  206. value.update({
  207. # 调拨入库
  208. 'goods_internal_in': value.get('goods_internal_in', 0) + (
  209. sql_type == 'internal' and sql_origin == '调拨入库' and record.get('goods_qty', 0) or 0),
  210. 'cost_internal_in': value.get('cost_internal_in', 0) + (
  211. sql_type == 'internal' and sql_origin == '调拨入库' and record.get('cost', 0) or 0),
  212. # 调拨出库
  213. 'goods_internal_out': value.get('goods_internal_out', 0) + (
  214. sql_type == 'internal' and sql_origin == '调拨出库' and record.get('goods_qty', 0) or 0),
  215. 'cost_internal_out': value.get('cost_internal_out', 0) + (
  216. sql_type == 'internal' and sql_origin == '调拨出库' and record.get('cost', 0) or 0),
  217. # =============================期初数量=============================
  218. 'goods_qty_begin': value.get('goods_qty_begin', 0) + (
  219. sql_type != 'internal' and tag * record.get('goods_qty_begin', 0)),
  220. 'cost_begin': value.get('cost_begin', 0) + (
  221. sql_type != 'internal' and tag * record.get('cost_begin', 0)),
  222. 'goods_qty_end': value.get('goods_qty_end', 0) + (
  223. sql_type != 'internal' and tag * record.get('goods_qty_end', 0)),
  224. 'cost_end': value.get('cost_end', 0) + (
  225. sql_type != 'internal' and tag * record.get('cost_end', 0)),
  226. # =============================一批入库数据=============================
  227. # 采购入库
  228. 'goods_buy_receipt': value.get('goods_buy_receipt', 0) +
  229. (sql_type == 'in' and sql_origin == '采购入库' and record.get('goods_qty', 0) or 0),
  230. 'cost_buy_receipt': value.get('cost_buy_receipt', 0) +
  231. (sql_type == 'in' and sql_origin == '采购入库' and record.get('cost', 0) or 0),
  232. # 其他入库
  233. 'goods_in_others': value.get('goods_in_others', 0) +
  234. (sql_type == 'in' and sql_origin == '其他入库' and record.get('goods_qty', 0) or 0),
  235. 'cost_in_others': value.get('cost_in_others', 0) +
  236. (sql_type == 'in' and sql_origin == '其他入库' and record.get('cost', 0) or 0),
  237. # 盘盈入库
  238. 'goods_in_inventory': value.get('goods_in_inventory', 0) +
  239. (sql_type == 'in' and sql_origin == '盘盈' and record.get('goods_qty', 0) or 0),
  240. 'cost_in_inventory': value.get('cost_in_inventory', 0) +
  241. (sql_type == 'in' and sql_origin == '盘盈' and record.get('cost', 0) or 0),
  242. # 销售退货
  243. 'goods_delivery_return': value.get('goods_delivery_return', 0) +
  244. (sql_type == 'in' and sql_origin == '销售退货' and record.get('goods_qty', 0) or 0),
  245. 'cost_delivery_return': value.get('cost_delivery_return', 0) +
  246. (sql_type == 'in' and sql_origin == '销售退货' and record.get('cost', 0) or 0),
  247. # 组装单组合件(组装入库)
  248. 'goods_assembly_in': value.get('goods_assembly_in', 0) +
  249. (sql_type == 'in' and sql_origin == '组装单组合件' and record.get('goods_qty',
  250. 0) or 0),
  251. 'cost_assembly_in': value.get('cost_assembly_in', 0) +
  252. (sql_type == 'in' and sql_origin == '组装单组合件' and record.get('cost', 0) or 0),
  253. # =============================一批出库数据=============================
  254. # 销售出库
  255. 'goods_delivery_sell': value.get('goods_delivery_sell', 0) +
  256. (sql_type == 'out' and sql_origin == '销售出库' and record.get('goods_qty', 0) or 0),
  257. 'cost_delivery_sell': value.get('cost_delivery_sell', 0) +
  258. (sql_type == 'out' and sql_origin == '销售出库' and record.get('cost', 0) or 0),
  259. # 其他出库
  260. 'goods_out_others': value.get('goods_out_others', 0) +
  261. (sql_type == 'out' and sql_origin == '其他出库' and record.get('goods_qty', 0) or 0),
  262. 'cost_out_others': value.get('cost_out_others', 0) +
  263. (sql_type == 'out' and sql_origin == '其他出库' and record.get('cost', 0) or 0),
  264. # 盘亏出库
  265. 'goods_out_inventory': value.get('goods_out_inventory', 0) +
  266. (sql_type == 'out' and sql_origin == '盘亏' and record.get('goods_qty', 0) or 0),
  267. 'cost_out_inventory': value.get('cost_out_inventory', 0) +
  268. (sql_type == 'out' and sql_origin == '盘亏' and record.get('cost', 0) or 0),
  269. # 采购退货
  270. 'goods_receipt_return': value.get('goods_receipt_return', 0) +
  271. (sql_type == 'out' and sql_origin == '采购退货' and record.get('goods_qty',
  272. 0) or 0),
  273. 'cost_receipt_return': value.get('cost_receipt_return', 0) +
  274. (sql_type == 'out' and sql_origin == '采购退货' and record.get('cost', 0) or 0),
  275. # 组装单子件(组装出库)
  276. 'goods_assembly_out': value.get('goods_assembly_out', 0) +
  277. (sql_type == 'out' and sql_origin == '组装单子件' and record.get('goods_qty',
  278. 0) or 0),
  279. 'cost_assembly_out': value.get('cost_assembly_out', 0) +
  280. (sql_type == 'out' and sql_origin == '组装单子件' and record.get('cost', 0) or 0),
  281. # =============================期末数量=============================
  282. 'goods_qty_in': value.get('goods_qty_in', 0) + (sql_type == 'in' and record.get('goods_qty', 0) or 0),
  283. 'cost_in': value.get('cost_in', 0) + (sql_type == 'in' and record.get('cost', 0) or 0),
  284. 'goods_qty_out': value.get('goods_qty_out', 0) + (sql_type == 'out' and record.get('goods_qty', 0) or 0),
  285. 'cost_out': value.get('cost_out', 0) + (sql_type == 'out' and record.get('cost', 0) or 0),
  286. 'id_lists': value.get('id_lists', []) + record.get('id_lists', []),
  287. })
  288. def compute_history_stock_by_collect(self, res, records, sql_type='out', sql_origin=''):
  289. for record in records:
  290. record_key = self.get_record_key(record, sql_type=sql_type)
  291. if not res.get(record_key):
  292. res[record_key] = self.get_default_value_by_record(
  293. record, sql_type=sql_type, sql_origin=record['origin'])
  294. self.update_record_value(
  295. res[record_key], record, sql_type=sql_type, sql_origin=record['origin'])
  296. def collect_data_by_sql(self, sql_type='out', sql_origin=''):
  297. out_collection = self.execute_sql_inherit(sql_type='out', sql_origin='')
  298. in_collection = self.execute_sql_inherit(sql_type='in', sql_origin='')
  299. internal_in_collection = self.execute_sql_inherit(sql_type='internal', sql_origin='internal_in')
  300. internal_out_collection = self.execute_sql_inherit(sql_type='internal', sql_origin='internal_out')
  301. res = {}
  302. self.compute_history_stock_by_collect(res, out_collection, sql_type='out')
  303. self.compute_history_stock_by_collect(res, in_collection, sql_type='in')
  304. self.compute_history_stock_by_collect(res, internal_out_collection, sql_type='internal')
  305. self.compute_history_stock_by_collect(res, internal_in_collection, sql_type='internal')
  306. result = []
  307. for key, value in res.items():
  308. value.update(self.unzip_record_key(key))
  309. result.append(value)
  310. return result
  311. def find_source_move_line(self):
  312. # 查看库存调拨明细
  313. move_line_ids = []
  314. # 获得'report.stock.receipt.delivery'记录集
  315. move_line_lists = self.get_data_from_cache(sql_type='out')
  316. date_start = self.env.context.get('date_start')
  317. date_end = self.env.context.get('date_end')
  318. for line in move_line_lists:
  319. if line.get('id') == self.id:
  320. domain_dict = [('date', '>=', date_start),
  321. ('date', '<=', date_end),
  322. ('id', 'in', line.get('id_lists'))
  323. ]
  324. move_line_ids = [line.id for line in self.env['wh.move.line'].search(domain_dict)]
  325. view = self.env.ref('warehouse.wh_move_line_list')
  326. return {
  327. 'name': ('库存调拨' + str(date_start) +
  328. '~' + str(date_end) +
  329. '~' + line['goods']),
  330. 'view_mode': 'list',
  331. 'views': [(view.id, 'list')],
  332. 'res_model': 'wh.move.line',
  333. 'type': 'ir.actions.act_window',
  334. 'domain': [('id', 'in', move_line_ids)],
  335. 'context': {'group_by':'goods'}
  336. }
上海开阖软件有限公司 沪ICP备12045867号-1