GoodERP
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

192 rindas
8.3KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models
  4. class CreateExchangeWizard(models.TransientModel):
  5. """生成每月汇况损益的向导 根据输入的期间"""
  6. _name = "create.exchange.wizard"
  7. _description = '期末调汇向导'
  8. @api.depends('date')
  9. def _compute_period_id(self):
  10. for ex in self:
  11. ex.period_id = self.env['finance.period'].get_period(ex.date)
  12. @api.model
  13. def _get_last_date(self):
  14. return self.env['finance.period'].get_period_month_date_range(
  15. self.env['finance.period'].get_date_now_period_id())[1]
  16. date = fields.Date('记帐日期', required=True, default=_get_last_date)
  17. period_id = fields.Many2one(
  18. 'finance.period',
  19. '会计期间',
  20. compute='_compute_period_id', ondelete='restrict', store=True)
  21. # todo 增加一个可以看到最后汇率的界面
  22. company_id = fields.Many2one(
  23. 'res.company',
  24. string='公司',
  25. change_default=True,
  26. default=lambda self: self.env.company)
  27. def create_partner_exchange_line(self, vals):
  28. '''
  29. 有partner的汇兑损益
  30. '''
  31. account_id = vals.get('account_id')
  32. voucher_lines = self.env['voucher.line'].search(
  33. [('account_id', '=', account_id),
  34. ('state', '=', 'done')])
  35. account = vals.get('account')
  36. res = {}
  37. for line in voucher_lines:
  38. if line.partner_id.id not in res:
  39. res[line.partner_id.id] = {'currency': 0,
  40. 'total_debit': 0,
  41. 'total_credit': 0}
  42. val = res[line.partner_id.id]
  43. if line.debit == 0:
  44. val.update({
  45. 'currency': val.get('currency') + line.currency_amount,
  46. 'total_debit': val.get('total_debit') + line.debit,
  47. 'total_credit': val.get('total_credit') + line.credit})
  48. else:
  49. val.update({
  50. 'currency': val.get('currency') - line.currency_amount,
  51. 'total_debit': val.get('total_debit') + line.debit,
  52. 'total_credit': val.get('total_credit') + line.credit})
  53. exp = val.get('currency') * vals.get('rate_silent') + \
  54. val.get('total_debit') - val.get('total_credit')
  55. if account.balance_directions == 'in':
  56. '''科目为借,则生成借方凭证行,差额为0的凭证行不生成'''
  57. if exp != 0:
  58. val.update({'name': "汇兑损益",
  59. 'account_id': vals.get('account_id'),
  60. 'debit': -exp,
  61. 'credit': 0,
  62. 'voucher_id': vals.get('vouch_obj_id'),
  63. 'currency_id': False,
  64. 'currency_amount': False,
  65. 'rate_silent': False,
  66. 'partner_id': line.partner_id.id,
  67. })
  68. if account.balance_directions == 'out':
  69. '''科目为贷,则生成贷方凭证行,差额为0的凭证行不生成'''
  70. if exp != 0:
  71. val.update({'name': "汇兑损益",
  72. 'account_id': vals.get('account_id'),
  73. 'credit': exp,
  74. 'voucher_id': vals.get('vouch_obj_id'),
  75. 'currency_id': False,
  76. 'currency_amount': False,
  77. 'rate_silent': False,
  78. 'partner_id': line.partner_id.id,
  79. })
  80. '''如果所有差额都为0,则无会计科目,则不生成明细行'''
  81. for partner_id, val in res.items():
  82. del val['currency'], val['total_debit'], val['total_credit']
  83. if val:
  84. self.env['voucher.line'].create(
  85. dict(val, partner_id=partner_id))
  86. def create_account_exchange_line(self, vals):
  87. '''
  88. 无partner的汇兑损益
  89. '''
  90. account_id = vals.get('account_id')
  91. voucher_lines = self.env['voucher.line'].search(
  92. [('account_id', '=', account_id),
  93. ('state', '=', 'done')])
  94. account = vals.get('account')
  95. currency_amount = 0
  96. debit = 0
  97. credit = 0
  98. for line in voucher_lines:
  99. if line.debit:
  100. currency_amount = currency_amount + line.currency_amount
  101. else:
  102. currency_amount = currency_amount - line.currency_amount
  103. debit = line.debit + debit
  104. credit = line.credit + credit
  105. if account.balance_directions == 'in':
  106. '''科目为借,则生成借方凭证行,差额为0的凭证行不生成'''
  107. if currency_amount * vals.get('rate_silent') - debit + credit != 0:
  108. self.env['voucher.line'].create({
  109. 'name': "汇兑损益",
  110. 'account_id': account_id,
  111. 'debit': currency_amount * vals.get(
  112. 'rate_silent') - debit + credit,
  113. 'voucher_id': vals.get('vouch_obj_id'),
  114. 'currency_id': False,
  115. 'currency_amount': False,
  116. 'rate_silent': False,
  117. })
  118. else:
  119. '''科目为贷,则生成贷方凭证行,差额为0的凭证行不生成'''
  120. if currency_amount * vals.get('rate_silent') - credit + debit != 0:
  121. self.env['voucher.line'].create({
  122. 'name': "汇兑损益",
  123. 'account_id': account_id,
  124. 'credit': currency_amount * vals.get(
  125. 'rate_silent') - credit + debit,
  126. 'voucher_id': vals.get('vouch_obj_id'),
  127. 'currency_id': False,
  128. 'currency_amount': False,
  129. 'rate_silent': False,
  130. })
  131. def create_exchang_line(self, vals):
  132. '''
  133. 当有主营业务收入,结汇等不需要汇兑损益的科目出现后,汇兑损益将不平,就会出现财务费用汇兑损益,
  134. '''
  135. vouch_obj = vals.get('vouch_obj_id')
  136. voucher = self.env['voucher'].search([('id', '=', vouch_obj)])
  137. voucher_lines = voucher.line_ids
  138. account_id = self.env.ref('finance.account_exchange')
  139. exp = 0
  140. for line in voucher_lines:
  141. exp = line.credit - line.debit + exp
  142. if exp != 0:
  143. self.env['voucher.line'].create({
  144. 'name': "汇兑损益",
  145. 'account_id': account_id.account_id.id,
  146. 'credit': -exp,
  147. 'voucher_id': vals.get('vouch_obj_id'),
  148. 'currency_id': False,
  149. 'currency_amount': False,
  150. 'rate_silent': False,
  151. })
  152. def create_exchange(self):
  153. vouch_obj = self.env['voucher'].create({'date': self.date})
  154. '''只有外币+期末需要调汇的科目才会能生成调汇凭证的明细行'''
  155. vals = {}
  156. for account_id in self.env['finance.account'].search([
  157. ('currency_id', '!=', self.env.user.company_id.currency_id.id),
  158. ('currency_id', '!=', False),
  159. ('exchange', '=', True)]):
  160. rate_silent = self.env['res.currency'].get_rate_silent(
  161. self.date, account_id.currency_id.id) or 0
  162. vals.update({'account_id': account_id.id,
  163. 'account': account_id,
  164. 'vouch_obj_id': vouch_obj.id,
  165. 'rate_silent': rate_silent,
  166. })
  167. if account_id.auxiliary_financing:
  168. self.create_partner_exchange_line(vals)
  169. else:
  170. self.create_account_exchange_line(vals)
  171. '''
  172. 当出现结汇,主营业务收入等一方不为需期末调汇科目时会出现一方需要调汇,一方不需要调汇,那时期末前面的明细行就会不平衡,就需要贷财务费用-汇兑损益
  173. '''
  174. self.create_exchang_line(vals)
  175. if not vouch_obj.line_ids:
  176. vouch_obj.unlink()
上海开阖软件有限公司 沪ICP备12045867号-1