GoodERP
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

384 linhas
19KB

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