GoodERP
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

344 行
17KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo.exceptions import UserError
  4. from odoo import fields, models, api
  5. from odoo.tools import float_compare
  6. class MoneyTransferOrder(models.Model):
  7. _name = 'money.transfer.order'
  8. _description = u'资金转账单'
  9. _inherit = ['mail.thread', 'mail.activity.mixin']
  10. _order = 'id desc'
  11. state = fields.Selection([
  12. ('draft', u'草稿'),
  13. ('done', u'已确认'),
  14. ('cancel', u'已作废'),
  15. ], string=u'状态', readonly=True,
  16. default='draft', copy=False, index=True,
  17. tracking=True,
  18. help=u'资金转账单状态标识,新建时状态为草稿;确认后状态为已确认')
  19. name = fields.Char(string=u'单据编号', copy=False, default='/',
  20. help=u'单据编号,创建时会自动生成')
  21. date = fields.Date(string=u'单据日期',
  22. default=lambda self: fields.Date.context_today(self),
  23. help=u'单据创建日期')
  24. note = fields.Text(string=u'备注', help=u'可以为该单据添加一些需要的标识信息')
  25. line_ids = fields.One2many('money.transfer.order.line', 'transfer_id',
  26. string=u'资金转账单行',
  27. help=u'资金转账单明细行')
  28. company_id = fields.Many2one(
  29. 'res.company',
  30. string=u'公司',
  31. change_default=True,
  32. default=lambda self: self.env.company)
  33. voucher_id = fields.Many2one('voucher',
  34. u'对应凭证',
  35. readonly=True,
  36. ondelete='restrict',
  37. help=u'资金转账单确认时生成的对应凭证',
  38. copy=False)
  39. transfer_amount = fields.Float(
  40. compute='_compute_transfer_amount', string='转账总金额')
  41. @api.depends('line_ids', 'line_ids.amount')
  42. def _compute_transfer_amount(self):
  43. for mto in self:
  44. mto.transfer_amount = sum([line.amount for line in mto.line_ids])
  45. def money_transfer_done(self):
  46. '''转账单的审核按钮'''
  47. self.ensure_one()
  48. if not self.line_ids:
  49. raise UserError('请先输入转账金额')
  50. decimal_amount = self.env.ref('core.decimal_amount')
  51. for line in self.line_ids:
  52. company_currency_id = self.env.user.company_id.currency_id.id
  53. out_currency_id = \
  54. line.out_bank_id.currency_id.id or company_currency_id
  55. in_currency_id = \
  56. line.in_bank_id.currency_id.id or company_currency_id
  57. if line.out_bank_id == line.in_bank_id:
  58. raise UserError('转出账户与转入账户不能相同')
  59. if line.amount <= 0:
  60. raise UserError('转账金额必须大于0。\n 转账金额:%s' % line.amount)
  61. if out_currency_id == company_currency_id: # 如果转出账户是公司本位币
  62. if float_compare(
  63. line.out_bank_id.balance,
  64. line.amount,
  65. decimal_amount.digits) == -1:
  66. raise UserError('转出账户余额不足。\n转出账户余额:%s 本次转出金额:%s' % (
  67. line.out_bank_id.balance, line.amount))
  68. else: # 转出账户余额充足
  69. line.out_bank_id.balance -= line.amount
  70. if in_currency_id == company_currency_id: # 如果转入账户是公司本位币
  71. line.in_bank_id.balance += line.amount
  72. else: # 如果转入账户是外币
  73. line.in_bank_id.balance += line.currency_amount
  74. else: # 如果转出账户是外币
  75. if float_compare(
  76. line.out_bank_id.balance,
  77. line.currency_amount,
  78. precision_digits=decimal_amount.digits) == -1:
  79. raise UserError(
  80. '转出账户余额不足。\n转出账户余额:%s 本次转出外币金额:%s'
  81. % (line.out_bank_id.balance, line.currency_amount))
  82. if in_currency_id == company_currency_id: # 如果转入账户是公司本位币
  83. line.in_bank_id.balance += line.amount
  84. line.out_bank_id.balance -= line.currency_amount
  85. elif (in_currency_id != company_currency_id
  86. and in_currency_id == out_currency_id):
  87. # 如果转入账户是外币,只支持同种外币互转
  88. line.in_bank_id.balance += line.currency_amount
  89. line.out_bank_id.balance -= line.currency_amount
  90. else:
  91. raise UserError('系统不支持外币转不同外币')
  92. # 创建凭证并审核
  93. voucher = self.create_voucher()
  94. return self.write({
  95. 'voucher_id': voucher.id,
  96. 'state': 'done',
  97. })
  98. def money_transfer_draft(self):
  99. '''转账单的反审核按钮,外币要考虑是转入还是转出'''
  100. self.ensure_one()
  101. decimal_amount = self.env.ref('core.decimal_amount')
  102. for line in self.line_ids:
  103. company_currency_id = self.env.user.company_id.currency_id.id
  104. out_currency_id = \
  105. line.out_bank_id.currency_id.id or company_currency_id
  106. in_currency_id = \
  107. line.in_bank_id.currency_id.id or company_currency_id
  108. if line.currency_amount > 0:
  109. if (in_currency_id != company_currency_id
  110. and out_currency_id == in_currency_id):
  111. # 转入和转出都是外币
  112. if float_compare(
  113. line.in_bank_id.balance,
  114. line.currency_amount,
  115. precision_digits=decimal_amount.digits) == -1:
  116. raise UserError(
  117. '转入账户余额不足。\n转入账户余额:%s 本次转出外币金额:%s'
  118. % (line.in_bank_id.balance, line.currency_amount))
  119. else: # 转入账户余额充足
  120. line.in_bank_id.balance -= line.currency_amount
  121. line.out_bank_id.balance += line.currency_amount
  122. elif (in_currency_id == company_currency_id
  123. and out_currency_id != company_currency_id):
  124. # 转入账户为本位币,转出是外币
  125. if float_compare(
  126. line.in_bank_id.balance,
  127. line.amount,
  128. precision_digits=decimal_amount.digits) == -1:
  129. raise UserError(
  130. '转入账户余额不足。\n转入账户余额:%s 本次转出金额:%s'
  131. % (line.in_bank_id.balance, line.amount))
  132. else:
  133. line.in_bank_id.balance -= line.amount
  134. line.out_bank_id.balance += line.currency_amount
  135. elif (in_currency_id != company_currency_id
  136. and out_currency_id == company_currency_id):
  137. # 转入账户为外币,转出是人民币
  138. if float_compare(
  139. line.in_bank_id.balance,
  140. line.currency_amount,
  141. precision_digits=decimal_amount.digits) == -1:
  142. raise UserError(
  143. '转入账户余额不足。\n转入账户余额:%s 本次转出金额:%s'
  144. % (line.in_bank_id.balance, line.currency_amount))
  145. else:
  146. line.in_bank_id.balance -= line.currency_amount
  147. line.out_bank_id.balance += line.amount
  148. else:
  149. raise UserError('系统不支持外币转不同外币')
  150. else: # 转入/转出账户都为本位币
  151. if float_compare(
  152. line.in_bank_id.balance,
  153. line.amount,
  154. precision_digits=decimal_amount.digits) == -1:
  155. raise UserError('转入账户余额不足。\n转入账户余额:%s 本次转出金额:%s'
  156. % (line.in_bank_id.balance, line.amount))
  157. else:
  158. line.in_bank_id.balance -= line.amount
  159. line.out_bank_id.balance += line.amount
  160. voucher = self.voucher_id
  161. self.write({
  162. 'voucher_id': False,
  163. 'state': 'draft',
  164. })
  165. # 反审核凭证并删除
  166. if voucher.state == 'done':
  167. voucher.voucher_draft()
  168. voucher.unlink()
  169. return True
  170. def create_voucher(self):
  171. """创建凭证并审核"""
  172. vouch_obj = self.env['voucher'].create(
  173. {'date': self.date, 'ref': '%s,%s' % (self._name, self.id)})
  174. vals = {}
  175. for line in self.line_ids:
  176. out_currency_id = (
  177. line.out_bank_id.currency_id.id
  178. or self.env.user.company_id.currency_id.id)
  179. in_currency_id = (
  180. line.in_bank_id.currency_id.id
  181. or self.env.user.company_id.currency_id.id)
  182. company_currency_id = self.env.user.company_id.currency_id.id
  183. if (out_currency_id != company_currency_id
  184. or in_currency_id != company_currency_id) \
  185. and not line.currency_amount:
  186. raise UserError('错误, 请请输入外币金额。')
  187. if (line.currency_amount
  188. and in_currency_id == company_currency_id
  189. and out_currency_id != company_currency_id):
  190. '''结汇'''
  191. '''借方行'''
  192. self.env['voucher.line'].create({
  193. 'name': "%s %s结汇至%s %s" % (
  194. self.name,
  195. line.out_bank_id.name,
  196. line.in_bank_id.name,
  197. self.note),
  198. 'account_id': line.in_bank_id.account_id.id,
  199. 'debit': line.amount,
  200. 'voucher_id': vouch_obj.id,
  201. 'partner_id': '',
  202. 'currency_id': '',
  203. 'currency_amount': line.currency_amount,
  204. 'rate_silent': line.amount / line.currency_amount
  205. })
  206. '''贷方行'''
  207. self.env['voucher.line'].create({
  208. 'name': "%s %s结汇至%s %s" % (
  209. self.name, line.out_bank_id.name,
  210. line.in_bank_id.name,
  211. self.note),
  212. 'account_id': line.out_bank_id.account_id.id,
  213. 'credit': line.amount,
  214. 'voucher_id': vouch_obj.id,
  215. 'partner_id': '',
  216. 'currency_id': out_currency_id,
  217. 'currency_amount': line.currency_amount,
  218. 'rate_silent': line.amount / line.currency_amount
  219. })
  220. elif (line.currency_amount
  221. and in_currency_id != company_currency_id
  222. and out_currency_id == company_currency_id):
  223. '''买汇'''
  224. '''借方行'''
  225. self.env['voucher.line'].create({
  226. 'name': u"%s %s买汇至%s %s" % (
  227. self.name,
  228. line.out_bank_id.name,
  229. line.in_bank_id.name,
  230. self.note),
  231. 'account_id': line.in_bank_id.account_id.id,
  232. 'debit': line.amount,
  233. 'voucher_id': vouch_obj.id,
  234. 'partner_id': '',
  235. 'currency_id': in_currency_id,
  236. 'currency_amount': line.currency_amount,
  237. 'rate_silent': line.amount / line.currency_amount
  238. })
  239. '''贷方行'''
  240. self.env['voucher.line'].create({
  241. 'name': u"%s %s买汇至%s %s" % (
  242. self.name,
  243. line.out_bank_id.name,
  244. line.in_bank_id.name,
  245. self.note),
  246. 'account_id': line.out_bank_id.account_id.id,
  247. 'credit': line.amount,
  248. 'voucher_id': vouch_obj.id,
  249. 'partner_id': '',
  250. 'currency_id': '',
  251. 'currency_amount': line.currency_amount,
  252. 'rate_silent': line.amount / line.currency_amount
  253. })
  254. elif (line.currency_amount
  255. and in_currency_id == out_currency_id
  256. and in_currency_id != company_currency_id):
  257. '''相同外币币别互转'''
  258. '''借方行'''
  259. rate_silent = self.env['res.currency'].get_rate_silent(
  260. self.date, in_currency_id)
  261. self.env['voucher.line'].create({
  262. 'name': u"%s %s外币互转至%s %s" % (
  263. self.name,
  264. line.out_bank_id.name,
  265. line.in_bank_id.name,
  266. self.note),
  267. 'account_id': line.in_bank_id.account_id.id,
  268. 'debit': line.currency_amount * rate_silent,
  269. 'voucher_id': vouch_obj.id,
  270. 'partner_id': '',
  271. 'currency_id': in_currency_id,
  272. 'currency_amount': line.currency_amount,
  273. 'rate_silent': rate_silent
  274. })
  275. '''贷方行'''
  276. self.env['voucher.line'].create({
  277. 'name': u"%s %s外币互转至%s %s" % (
  278. self.name, line.out_bank_id.name,
  279. line.in_bank_id.name, self.note),
  280. 'account_id': line.out_bank_id.account_id.id,
  281. 'credit': line.currency_amount * rate_silent,
  282. 'voucher_id': vouch_obj.id, 'partner_id': '',
  283. 'currency_id': out_currency_id,
  284. 'currency_amount': line.currency_amount,
  285. 'rate_silent': rate_silent
  286. })
  287. else:
  288. '''人民币间'''
  289. vals.update({
  290. 'vouch_obj_id': vouch_obj.id,
  291. 'name': self.name, 'string': self.note or '',
  292. 'amount': abs(line.amount),
  293. 'credit_account_id': line.out_bank_id.account_id.id,
  294. 'debit_account_id': line.in_bank_id.account_id.id,
  295. })
  296. self.env['money.invoice'].create_voucher_line(vals)
  297. vouch_obj.voucher_done()
  298. return vouch_obj
  299. class MoneyTransferOrderLine(models.Model):
  300. _name = 'money.transfer.order.line'
  301. _description = u'资金转账单明细'
  302. transfer_id = fields.Many2one('money.transfer.order',
  303. string=u'资金转账单', ondelete='cascade',
  304. help=u'资金转账单行对应的资金转账单')
  305. out_bank_id = fields.Many2one('bank.account', string=u'转出账户',
  306. required=True, ondelete='restrict',
  307. help=u'资金转账单行上的转出账户')
  308. in_bank_id = fields.Many2one('bank.account', string=u'转入账户',
  309. required=True, ondelete='restrict',
  310. help=u'资金转账单行上的转入账户')
  311. currency_amount = fields.Float(string=u'外币金额',
  312. digits='Amount',
  313. help=u'转出或转入的外币金额')
  314. amount = fields.Float(string=u'金额',
  315. digits='Amount',
  316. help=u'转出或转入的金额')
  317. mode_id = fields.Many2one('settle.mode', string=u'结算方式',
  318. ondelete='restrict',
  319. help=u'结算方式:支票、信用卡等')
  320. number = fields.Char(string=u'结算号', help=u'本次结算号')
  321. note = fields.Char(string=u'备注',
  322. help=u'可以为该单据添加一些需要的标识信息')
  323. company_id = fields.Many2one(
  324. 'res.company',
  325. string=u'公司',
  326. change_default=True,
  327. default=lambda self: self.env.company)
上海开阖软件有限公司 沪ICP备12045867号-1