GoodERP
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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