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.

459 line
20KB

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