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.

134 lines
5.7KB

  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, tools
  4. class BankStatementsReport(models.Model):
  5. _name = "bank.statements.report"
  6. _description = u"现金银行报表"
  7. _auto = False
  8. _order = 'date'
  9. @api.depends('get', 'pay', 'bank_id')
  10. def _compute_balance(self):
  11. # 相邻的两条记录,bank_id不同,重新计算账户余额
  12. for bsr in self:
  13. pre_record = bsr.search(
  14. [('id', '<=', bsr.id), ('bank_id', '=', bsr.bank_id.id)])
  15. for pre in pre_record:
  16. bsr.balance += pre.get - pre.pay
  17. bank_id = fields.Many2one('bank.account', string=u'账户名称', readonly=True)
  18. date = fields.Date(string=u'日期', readonly=True)
  19. name = fields.Char(string=u'单据编号', readonly=True)
  20. get = fields.Float(string=u'收入', readonly=True,
  21. digits='Amount')
  22. pay = fields.Float(string=u'支出', readonly=True,
  23. digits='Amount')
  24. balance = fields.Float(string=u'账户余额',
  25. compute='_compute_balance', readonly=True,
  26. digits='Amount')
  27. partner_id = fields.Many2one('partner', string=u'往来单位', readonly=True)
  28. note = fields.Char(string=u'备注', readonly=True)
  29. def init(self):
  30. # union money_order, other_money_order, money_transfer_order
  31. cr = self._cr
  32. tools.drop_view_if_exists(cr, 'bank_statements_report')
  33. cr.execute("""
  34. CREATE or REPLACE VIEW bank_statements_report AS (
  35. SELECT ROW_NUMBER() OVER(ORDER BY bank_id,date) AS id,
  36. bank_id,
  37. date,
  38. name,
  39. get,
  40. pay,
  41. balance,
  42. partner_id,
  43. note
  44. FROM
  45. (
  46. SELECT mol.bank_id,
  47. mo.date,
  48. mo.name,
  49. (CASE WHEN mo.type = 'get' THEN mol.amount ELSE 0 END) AS get,
  50. (CASE WHEN mo.type = 'pay' THEN mol.amount ELSE 0 END) AS pay,
  51. 0 AS balance,
  52. mo.partner_id,
  53. mo.note
  54. FROM money_order_line AS mol
  55. LEFT JOIN money_order AS mo ON mol.money_id = mo.id
  56. WHERE mo.state = 'done'
  57. UNION ALL
  58. SELECT omo.bank_id,
  59. omo.date,
  60. omo.name,
  61. (CASE WHEN omo.type = 'other_get' THEN
  62. (CASE WHEN ba.currency_id IS NULL THEN omo.total_amount ELSE omo.currency_amount END)
  63. ELSE 0 END) AS get,
  64. (CASE WHEN omo.type = 'other_pay' THEN
  65. (CASE WHEN ba.currency_id IS NULL THEN omo.total_amount ELSE omo.currency_amount END)
  66. ELSE 0 END) AS pay,
  67. 0 AS balance,
  68. omo.partner_id,
  69. omo.note AS note
  70. FROM other_money_order AS omo
  71. LEFT JOIN bank_account AS ba ON ba.id = omo.bank_id
  72. LEFT JOIN res_currency AS rc ON rc.id = ba.currency_id
  73. WHERE omo.state = 'done'
  74. UNION ALL
  75. SELECT mtol.out_bank_id AS bank_id,
  76. mto.date,
  77. mto.name,
  78. 0 AS get,
  79. (CASE WHEN ba.currency_id IS NULL THEN mtol.amount ELSE mtol.currency_amount END) AS pay,
  80. 0 AS balance,
  81. NULL AS partner_id,
  82. mto.note
  83. FROM money_transfer_order_line AS mtol
  84. LEFT JOIN money_transfer_order AS mto ON mtol.transfer_id = mto.id
  85. LEFT JOIN bank_account AS ba ON ba.id = mtol.out_bank_id
  86. LEFT JOIN res_currency AS rc ON rc.id = ba.currency_id
  87. WHERE mto.state = 'done'
  88. UNION ALL
  89. SELECT mtol.in_bank_id AS bank_id,
  90. mto.date,
  91. mto.name,
  92. mtol.amount AS get,
  93. 0 AS pay,
  94. 0 AS balance,
  95. NULL AS partner_id,
  96. mto.note
  97. FROM money_transfer_order_line AS mtol
  98. LEFT JOIN money_transfer_order AS mto ON mtol.transfer_id = mto.id
  99. WHERE mto.state = 'done'
  100. ) AS bs)
  101. """)
  102. def find_source_order(self):
  103. # 查看原始单据,三种情况:收付款单、其他收支单、资金转换单
  104. self.ensure_one()
  105. model_view = {
  106. 'money.order': {'name': u'收付款单',
  107. 'view': 'money.money_order_form'},
  108. 'other.money.order': {'name': u'其他收支单',
  109. 'view': 'money.other_money_order_form'},
  110. 'money.transfer.order': {
  111. 'name': '资金转换单',
  112. 'view': 'money.money_transfer_order_form'}}
  113. for model, view_dict in model_view.items():
  114. res = self.env[model].search([('name', '=', self.name)])
  115. name = view_dict['name']
  116. view = self.env.ref(view_dict['view'])
  117. if res:
  118. return {
  119. 'name': name,
  120. 'view_mode': 'form',
  121. 'view_id': False,
  122. 'views': [(view.id, 'form')],
  123. 'res_model': model,
  124. 'type': 'ir.actions.act_window',
  125. 'res_id': res.id
  126. }
上海开阖软件有限公司 沪ICP备12045867号-1