GoodERP
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

460 lines
20KB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import _
  5. from odoo.exceptions import UserError
  6. from odoo import fields, models, api
  7. from odoo.tools import float_compare
  8. class OtherMoneyOrder(models.Model):
  9. _name = 'other.money.order'
  10. _description = '其他收入/其他支出'
  11. _inherit = ['mail.thread', 'mail.activity.mixin']
  12. _order = 'id desc'
  13. TYPE_SELECTION = [
  14. ('other_pay', '其他支出'),
  15. ('other_get', '其他收入'),
  16. ]
  17. @api.model_create_multi
  18. def create(self, vals_list):
  19. # 创建单据时,更新订单类型的不同,生成不同的单据编号
  20. for values in vals_list:
  21. if self.env.context.get('type') == 'other_get':
  22. values.update(
  23. {'name': self.env['ir.sequence'].next_by_code(
  24. 'other.get.order')})
  25. if self.env.context.get('type') == 'other_pay' \
  26. or values.get('name', '/') == '/':
  27. values.update(
  28. {'name': self.env['ir.sequence'].next_by_code(
  29. 'other.pay.order')})
  30. return super(OtherMoneyOrder, self).create(vals_list)
  31. @api.depends('line_ids.amount', 'line_ids.tax_amount')
  32. def _compute_total_amount(self):
  33. # 计算应付金额/应收金额
  34. for omo in self:
  35. omo.total_amount = sum((line.amount + line.tax_amount)
  36. for line in omo.line_ids)
  37. state = fields.Selection([
  38. ('draft', '草稿'),
  39. ('done', '已确认'),
  40. ('cancel', '已作废'),
  41. ], string='状态', readonly=True,
  42. default='draft', copy=False, index=True,
  43. tracking=True,
  44. help='其他收支单状态标识,新建时状态为草稿;确认后状态为已确认')
  45. partner_id = fields.Many2one('partner', string='往来单位',
  46. ondelete='restrict',
  47. help='单据对应的业务伙伴,单据确认时会影响他的应收应付余额')
  48. date = fields.Date(string='单据日期',
  49. default=lambda self: fields.Date.context_today(self),
  50. copy=False,
  51. help='单据创建日期')
  52. name = fields.Char(string='单据编号', copy=False, readonly=True, default='/',
  53. help='单据编号,创建时会根据类型自动生成')
  54. total_amount = fields.Float(string='金额', compute='_compute_total_amount',
  55. store=True, readonly=True,
  56. digits='Amount',
  57. help='本次其他收支的总金额')
  58. bank_id = fields.Many2one('bank.account', string='结算账户',
  59. required=True, ondelete='restrict',
  60. help='本次其他收支的结算账户')
  61. line_ids = fields.One2many('other.money.order.line', 'other_money_id',
  62. string='收支单行',
  63. copy=True,
  64. help='其他收支单明细行')
  65. type = fields.Selection(TYPE_SELECTION, string='类型',
  66. default=lambda self: self._context.get('type'),
  67. help='类型:其他收入 或者 其他支出')
  68. note = fields.Text('备注',
  69. help='可以为该单据添加一些需要的标识信息')
  70. is_init = fields.Boolean('初始化应收应付', help='此单是否为初始化单')
  71. company_id = fields.Many2one(
  72. 'res.company',
  73. string='公司',
  74. change_default=True,
  75. default=lambda self: self.env.company)
  76. receiver = fields.Char('收款人',
  77. help='收款人')
  78. bank_name = fields.Char('开户行')
  79. bank_num = fields.Char('银行账号')
  80. voucher_id = fields.Many2one('voucher',
  81. '对应凭证',
  82. readonly=True,
  83. ondelete='restrict',
  84. copy=False,
  85. help='其他收支单确认时生成的对应凭证')
  86. currency_id = fields.Many2one('res.currency',
  87. '外币币别',
  88. help='外币币别')
  89. currency_rate = fields.Float('汇率', digits='Price')
  90. currency_amount = fields.Float('外币金额',
  91. digits='Amount')
  92. details = fields.Html('明细', compute='_compute_details')
  93. @api.onchange('bank_id')
  94. def onchange_bank_id(self):
  95. for res in self:
  96. res.currency_id = self.env.company.currency_id
  97. res.currency_rate = 1
  98. if res.bank_id:
  99. if res.bank_id.currency_id:
  100. res.currency_id = res.bank_id.currency_id
  101. res.currency_rate = res.env['res.currency'].get_rate_silent(res.date, res.currency_id.id) or 1
  102. @api.depends('line_ids')
  103. def _compute_details(self):
  104. for v in self:
  105. vl = {'col': [], 'val': []}
  106. vl['col'] = [_('类别'), _('会计科目'), _('辅助核算'), _('金额')]
  107. for li in v.line_ids:
  108. vl['val'].append([
  109. li.category_id.name,
  110. li.account_id.display_name,
  111. li.auxiliary_id.name or '',
  112. li.amount])
  113. v.details = v.company_id._get_html_table(vl)
  114. @api.onchange('date')
  115. def onchange_date(self):
  116. if self._context.get('type') == 'other_get':
  117. return {'domain': {'partner_id': [('c_category_id', '!=', False)]}}
  118. else:
  119. return {'domain': {'partner_id': [('s_category_id', '!=', False)]}}
  120. @api.onchange('partner_id')
  121. def onchange_partner_id(self):
  122. """
  123. 更改业务伙伴,自动填入收款人、开户行和银行帐号
  124. """
  125. if self.partner_id:
  126. self.receiver = self.partner_id.name
  127. self.bank_name = self.partner_id.bank_name
  128. self.bank_num = self.partner_id.bank_num
  129. def other_money_done(self):
  130. '''其他收支单的审核按钮'''
  131. self.ensure_one()
  132. if float_compare(self.total_amount, 0, 3) <= 0:
  133. raise UserError(_('金额应该大于0。\n金额:%s') % self.total_amount)
  134. if not self.bank_id.account_id:
  135. raise UserError(_('请配置%s的会计科目') % (self.bank_id.name))
  136. # 根据单据类型更新账户余额
  137. if self.type == 'other_pay':
  138. decimal_amount = self.env.ref('core.decimal_amount')
  139. if float_compare(
  140. self.bank_id.balance,
  141. self.total_amount,
  142. decimal_amount.digits) == -1:
  143. raise UserError(_('账户余额不足。\n账户余额:%s 本次支出金额:%s') %
  144. (self.bank_id.balance, self.total_amount))
  145. self.bank_id.balance -= self.total_amount
  146. else:
  147. self.bank_id.balance += self.total_amount
  148. # 创建凭证并审核非初始化凭证
  149. vouch_obj = self.create_voucher()
  150. return self.write({
  151. 'voucher_id': vouch_obj.id,
  152. 'state': 'done',
  153. })
  154. def other_money_draft(self):
  155. '''其他收支单的反审核按钮'''
  156. self.ensure_one()
  157. # 根据单据类型更新账户余额
  158. if self.type == 'other_pay':
  159. self.bank_id.balance += self.total_amount
  160. else:
  161. decimal_amount = self.env.ref('core.decimal_amount')
  162. if float_compare(
  163. self.bank_id.balance,
  164. self.total_amount,
  165. decimal_amount.digits) == -1:
  166. raise UserError(_('账户余额不足。\n账户余额:%s 本次支出金额:%s') %
  167. (self.bank_id.balance, self.total_amount))
  168. self.bank_id.balance -= self.total_amount
  169. voucher = self.voucher_id
  170. self.write({
  171. 'voucher_id': False,
  172. 'state': 'draft',
  173. })
  174. # 反审核凭证并删除
  175. if voucher.state == 'done':
  176. voucher.voucher_draft()
  177. # 始初化单反审核只删除明细行
  178. if self.is_init:
  179. vouch_obj = self.env['voucher'].search([('id', '=', voucher.id)])
  180. vouch_obj_lines = self.env['voucher.line'].search([
  181. ('voucher_id', '=', vouch_obj.id),
  182. ('account_id', '=', self.bank_id.account_id.id),
  183. ('init_obj', '=', 'other_money_order-%s' % (self.id))])
  184. for vouch_obj_line in vouch_obj_lines:
  185. vouch_obj_line.unlink()
  186. else:
  187. voucher.unlink()
  188. return True
  189. def create_voucher(self):
  190. """创建凭证并审核非初始化凭证"""
  191. init_obj = ''
  192. # 初始化单的话,先找是否有初始化凭证,没有则新建一个
  193. if self.is_init:
  194. vouch_obj = self.env['voucher'].search([('is_init', '=', True)])
  195. if not vouch_obj:
  196. vouch_obj = self.env['voucher'].create({
  197. 'date': self.date,
  198. 'is_init': True,
  199. 'ref': '%s,%s' % (self._name, self.id)})
  200. else:
  201. vouch_obj = self.env['voucher'].create(
  202. {'date': self.date, 'ref': '%s,%s' % (self._name, self.id)})
  203. if self.is_init:
  204. init_obj = 'other_money_order-%s' % (self.id)
  205. if self.type == 'other_get': # 其他收入单
  206. self.other_get_create_voucher_line(vouch_obj, init_obj)
  207. else: # 其他支出单
  208. self.other_pay_create_voucher_line(vouch_obj)
  209. # 如果非初始化单则审核
  210. if not self.is_init:
  211. vouch_obj.voucher_done()
  212. return vouch_obj
  213. def other_get_create_voucher_line(self, vouch_obj, init_obj):
  214. """
  215. 其他收入单生成凭证明细行
  216. :param vouch_obj: 凭证
  217. :return:
  218. """
  219. in_currency_id = (
  220. self.bank_id.currency_id.id
  221. or self.env.user.company_id.currency_id.id)
  222. company_currency_id = self.env.user.company_id.currency_id.id
  223. in_is_company_currency = in_currency_id == company_currency_id
  224. vals = {}
  225. for line in self.line_ids:
  226. if not line.category_id.account_id:
  227. raise UserError(_('请配置%s的会计科目') % (line.category_id.name))
  228. rate_silent = self.env['res.currency'].get_rate_silent(
  229. self.date, self.bank_id.currency_id.id)
  230. vals.update({'vouch_obj_id': vouch_obj.id,
  231. 'name': self.name, 'note': line.note or '',
  232. 'credit_auxiliary_id': line.auxiliary_id.id,
  233. 'amount': abs(line.amount + line.tax_amount),
  234. 'credit_account_id': line.category_id.account_id.id,
  235. 'debit_account_id': self.bank_id.account_id.id,
  236. 'partner_credit': self.partner_id.id,
  237. 'partner_debit': '',
  238. 'sell_tax_amount': line.tax_amount or 0,
  239. 'init_obj': init_obj,
  240. 'currency_id': self.bank_id.currency_id.id,
  241. 'currency_amount': (
  242. not in_is_company_currency
  243. and self.total_amount or 0),
  244. # 判断是否本币
  245. 'rate_silent': rate_silent,
  246. })
  247. # 贷方行
  248. if not init_obj:
  249. self.env['voucher.line'].create({
  250. 'name': "%s %s" % (vals.get('name'), vals.get('note')),
  251. 'partner_id': vals.get('partner_credit', ''),
  252. 'account_id': vals.get('credit_account_id'),
  253. 'credit': (
  254. in_is_company_currency
  255. and line.amount
  256. or line.amount * vals.get('rate_silent')),
  257. 'voucher_id': vals.get('vouch_obj_id'),
  258. 'auxiliary_id': vals.get('credit_auxiliary_id', False),
  259. 'currency_amount': (
  260. not in_is_company_currency
  261. and line.amount or 0),
  262. 'rate_silent': vals.get('rate_silent'),
  263. })
  264. # 销项税行
  265. if vals.get('sell_tax_amount'):
  266. if not self.env.user.company_id.output_tax_account:
  267. raise UserError(
  268. _('您还没有配置公司的销项税科目。\n请通过"配置-->高级配置-->公司"菜单来设置销项税科目!'))
  269. self.env['voucher.line'].create({
  270. 'name': "%s %s" % (vals.get('name'), vals.get('note')),
  271. 'account_id':
  272. self.env.user.company_id.output_tax_account.id,
  273. 'credit': in_is_company_currency
  274. and line.tax_amount
  275. or line.tax_amount * vals.get('rate_silent') or 0,
  276. 'voucher_id': vals.get('vouch_obj_id'),
  277. 'currency_amount':
  278. not in_is_company_currency
  279. and line.tax_amount or 0,
  280. 'rate_silent': vals.get('rate_silent'),
  281. })
  282. # 借方行
  283. self.env['voucher.line'].create({
  284. 'name': "%s" % (vals.get('name')),
  285. 'account_id': vals.get('debit_account_id'),
  286. # 借方和
  287. 'debit':
  288. in_is_company_currency
  289. and self.total_amount
  290. or self.total_amount * vals.get('rate_silent'),
  291. 'voucher_id': vals.get('vouch_obj_id'),
  292. 'partner_id': vals.get('partner_debit', ''),
  293. 'auxiliary_id': vals.get('debit_auxiliary_id', False),
  294. 'init_obj': vals.get('init_obj', False),
  295. 'currency_id': vals.get('currency_id', False),
  296. 'currency_amount': vals.get('currency_amount'),
  297. 'rate_silent': vals.get('rate_silent'),
  298. })
  299. def other_pay_create_voucher_line(self, vouch_obj):
  300. """
  301. 其他支出单生成凭证明细行
  302. :param vouch_obj: 凭证
  303. :return:
  304. """
  305. out_currency_id = \
  306. self.bank_id.currency_id.id \
  307. or self.env.user.company_id.currency_id.id
  308. company_currency_id = self.env.user.company_id.currency_id.id
  309. out_is_company_currency = out_currency_id == company_currency_id
  310. vals = {}
  311. for line in self.line_ids:
  312. if not line.category_id.account_id:
  313. raise UserError(_('请配置%s的会计科目') % (line.category_id.name))
  314. rate_silent = self.env['res.currency'].get_rate_silent(
  315. self.date, self.bank_id.currency_id.id)
  316. vals.update({'vouch_obj_id': vouch_obj.id, 'name': self.name,
  317. 'note': line.note or '',
  318. 'debit_auxiliary_id': line.auxiliary_id.id,
  319. 'amount': abs(line.amount + line.tax_amount),
  320. 'credit_account_id': self.bank_id.account_id.id,
  321. 'debit_account_id': line.category_id.account_id.id,
  322. 'partner_credit': '',
  323. 'partner_debit': self.partner_id.id,
  324. 'buy_tax_amount': line.tax_amount or 0,
  325. 'currency_id': self.bank_id.currency_id.id,
  326. 'currency_amount':
  327. not out_is_company_currency
  328. and self.total_amount or 0, # 判断是否本币
  329. 'rate_silent': rate_silent,
  330. })
  331. # 借方行
  332. self.env['voucher.line'].create({
  333. 'name': "%s %s " % (vals.get('name'), vals.get('note')),
  334. 'account_id': vals.get('debit_account_id'),
  335. 'debit':
  336. out_is_company_currency
  337. and line.amount
  338. or line.amount * vals.get('rate_silent'),
  339. 'voucher_id': vals.get('vouch_obj_id'),
  340. 'partner_id': vals.get('partner_debit', ''),
  341. 'auxiliary_id': vals.get('debit_auxiliary_id', False),
  342. 'init_obj': vals.get('init_obj', False),
  343. 'currency_amount':
  344. not out_is_company_currency
  345. and line.amount or 0,
  346. 'rate_silent': vals.get('rate_silent'),
  347. })
  348. # 进项税行
  349. if vals.get('buy_tax_amount'):
  350. if not self.env.user.company_id.import_tax_account:
  351. raise UserError(_('请通过"配置-->高级配置-->公司"菜单来设置进项税科目'))
  352. self.env['voucher.line'].create({
  353. 'name': "%s %s" % (vals.get('name'), vals.get('note')),
  354. 'account_id':
  355. self.env.user.company_id.import_tax_account.id,
  356. 'debit':
  357. out_is_company_currency
  358. and line.tax_amount
  359. or line.tax_amount * vals.get('rate_silent') or 0,
  360. 'voucher_id': vals.get('vouch_obj_id'),
  361. 'currency_amount':
  362. not out_is_company_currency
  363. and line.tax_amount or 0,
  364. 'rate_silent': vals.get('rate_silent'),
  365. })
  366. # 贷方行
  367. self.env['voucher.line'].create({
  368. 'name': "%s" % (vals.get('name')),
  369. 'partner_id': vals.get('partner_credit', ''),
  370. 'account_id': vals.get('credit_account_id'),
  371. # 借方和
  372. 'credit':
  373. out_is_company_currency
  374. and self.total_amount
  375. or self.total_amount * vals.get('rate_silent'),
  376. 'voucher_id': vals.get('vouch_obj_id'),
  377. 'auxiliary_id': vals.get('credit_auxiliary_id', False),
  378. 'init_obj': vals.get('init_obj', False),
  379. 'currency_id': vals.get('currency_id', False),
  380. 'currency_amount': vals.get('currency_amount'),
  381. 'rate_silent': vals.get('rate_silent'),
  382. })
  383. class OtherMoneyOrderLine(models.Model):
  384. _name = 'other.money.order.line'
  385. _description = '其他收支单明细'
  386. @api.onchange('service')
  387. def onchange_service(self):
  388. # 当选择了收支项后,则自动填充上类别和金额
  389. if self.env.context.get('order_type') == 'other_get':
  390. self.category_id = self.service.get_categ_id.id
  391. elif self.env.context.get('order_type') == 'other_pay':
  392. self.category_id = self.service.pay_categ_id.id
  393. self.amount = self.service.price
  394. self.tax_rate = self.service.tax_rate
  395. @api.onchange('amount', 'tax_rate')
  396. def onchange_tax_amount(self):
  397. '''当订单行的金额、税率改变时,改变税额'''
  398. self.tax_amount = self.amount * self.tax_rate * 0.01
  399. other_money_id = fields.Many2one('other.money.order',
  400. '其他收支', ondelete='cascade',
  401. help='其他收支单行对应的其他收支单')
  402. service = fields.Many2one('service', '收支项', ondelete='restrict',
  403. help='其他收支单行上对应的收支项')
  404. category_id = fields.Many2one('core.category',
  405. '类别', ondelete='restrict',
  406. help='类型:运费、咨询费等')
  407. account_id = fields.Many2one('finance.account',
  408. '科目', related='category_id.account_id')
  409. auxiliary_id = fields.Many2one('auxiliary.financing', '辅助核算',
  410. help='其他收支单行上的辅助核算')
  411. amount = fields.Float('金额',
  412. digits='Amount',
  413. help='其他收支单行上的金额')
  414. tax_rate = fields.Float(
  415. '税率(%)',
  416. help='其他收支单行上的税率')
  417. tax_amount = fields.Float('税额',
  418. digits='Amount',
  419. help='其他收支单行上的税额')
  420. note = fields.Char('备注',
  421. help='可以为该单据添加一些需要的标识信息')
  422. company_id = fields.Many2one(
  423. 'res.company',
  424. string='公司',
  425. change_default=True,
  426. default=lambda self: self.env.company)
上海开阖软件有限公司 沪ICP备12045867号-1