GoodERP
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

255 lines
12KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import models, fields, api
  4. from odoo.exceptions import UserError
  5. class MoneyInvoice(models.Model):
  6. _inherit = 'money.invoice'
  7. voucher_id = fields.Many2one(
  8. 'voucher',
  9. '对应凭证',
  10. readonly=True,
  11. ondelete='restrict',
  12. copy=False,
  13. help='结算单确认时生成的对应凭证')
  14. cogs_voucher_id = fields.Many2one(
  15. 'voucher',
  16. '成本凭证',
  17. readonly=True,
  18. ondelete='set null',
  19. copy=False,
  20. help='结算单确认时生成的成本凭证')
  21. def money_invoice_draft(self):
  22. """
  23. 撤销确认结算单时,撤销确认凭证
  24. """
  25. res = super(MoneyInvoice, self).money_invoice_draft()
  26. for invoice in self:
  27. voucher, invoice.voucher_id = invoice.voucher_id, False
  28. if voucher.state == 'done':
  29. voucher.voucher_draft()
  30. # 始初化单反审核只删除明细行:生成初始化凭证时,不生成往来单位对方的科目,所以要删除相关明细行
  31. if invoice.is_init:
  32. vouch_obj = self.env['voucher'].search(
  33. [('id', '=', voucher.id)])
  34. vouch_obj_lines = self.env['voucher.line'].search([
  35. ('voucher_id', '=', vouch_obj.id),
  36. ('partner_id', '=', invoice.partner_id.id),
  37. ('init_obj', '=', 'money_invoice'), ])
  38. for vouch_obj_line in vouch_obj_lines:
  39. vouch_obj_line.unlink()
  40. else: # 非初始化单反审核时删除凭证
  41. voucher.unlink()
  42. # 删除发出成本凭证
  43. cogs_voucher = self.env['voucher'].search(
  44. [('ref', '=', '%s,%s' % (self._name, invoice.id))])
  45. if cogs_voucher:
  46. if cogs_voucher.state == 'done':
  47. cogs_voucher.voucher_draft()
  48. cogs_voucher.unlink()
  49. return res
  50. def money_invoice_done(self):
  51. """
  52. 确认结算单时,创建凭证并确认
  53. """
  54. res = super(MoneyInvoice, self).money_invoice_done()
  55. vals = {}
  56. # 初始化单的话,先找是否有初始化凭证,没有则新建一个
  57. for invoice in self:
  58. if invoice.is_init:
  59. vouch_obj = self.env['voucher'].search(
  60. [('is_init', '=', True)])
  61. if not vouch_obj:
  62. vouch_obj = self.env['voucher'].create(
  63. {'date': invoice.date,
  64. 'is_init': True,
  65. 'ref': '%s,%s' % (self._name, self.id)})
  66. invoice.write({'voucher_id': vouch_obj.id})
  67. else:
  68. vouch_obj = self.env['voucher'].create({
  69. 'date': invoice.invoice_date or
  70. fields.Date.context_today(self),
  71. 'ref': '%s,%s' % (self._name, self.id)})
  72. invoice.write({'voucher_id': vouch_obj.id})
  73. if not invoice.category_id.account_id:
  74. raise UserError('请配置%s的会计科目' % (invoice.category_id.name))
  75. partner_cat = invoice.category_id.type == 'income' \
  76. and invoice.partner_id.c_category_id \
  77. or invoice.partner_id.s_category_id
  78. partner_account_id = partner_cat.account_id.id
  79. if not partner_account_id:
  80. raise UserError('请配置%s的会计科目' % (partner_cat.name))
  81. if invoice.category_id.type == 'income':
  82. vals.update({
  83. 'vouch_obj_id': vouch_obj.id,
  84. 'partner_credit': invoice.partner_id.id,
  85. 'name': invoice.name,
  86. 'string': invoice.note or '',
  87. 'amount': invoice.amount,
  88. 'credit_account_id': invoice.category_id.account_id.id,
  89. 'partner_debit': invoice.partner_id.id,
  90. 'debit_account_id': partner_account_id,
  91. 'sell_tax_amount': invoice.tax_amount or 0,
  92. 'credit_auxiliary_id': invoice.auxiliary_id.id,
  93. 'currency_id': invoice.currency_id.id or '',
  94. 'rate_silent': self.env['res.currency'].get_rate_silent(
  95. self.date, invoice.currency_id.id) or 0,
  96. })
  97. # 找到发票对应发货单的凭证,复制修改, 结转主营业务成本
  98. send_voucher = False
  99. if self.env['ir.module.module'].sudo().search(
  100. [('name', '=', 'sell'), ('state', '=', 'installed')]):
  101. delivery = self.env['sell.delivery'].search(
  102. [('name', '=', invoice.name)])
  103. send_voucher = delivery.voucher_id
  104. # 该公司不是月末一次性结转发出成本
  105. if send_voucher:
  106. cost_voucher_data = send_voucher.copy_data()
  107. out_account_id = self.env.ref(
  108. 'finance.categ_sell_goods').account_id.id
  109. cogs_account_id = self.env.company.cogs_account.id
  110. # 如果发货单没有直接结转成本
  111. if out_account_id != cogs_account_id:
  112. for vl in cost_voucher_data[0]['line_ids']:
  113. if vl[2]['account_id'] != out_account_id:
  114. vl[2].update({'account_id': cogs_account_id})
  115. vl[2].update({
  116. 'debit': vl[2]['credit'],
  117. 'credit': vl[2]['debit'],
  118. 'voucher_id': False})
  119. cost_voucher_data[0].update(
  120. {'ref': '%s,%s' % (self._name, self.id)})
  121. if invoice.invoice_date:
  122. cost_voucher_data[0].update(
  123. {'date': invoice.invoice_date})
  124. cogs_voucher = self.env['voucher'].create(
  125. cost_voucher_data)
  126. cogs_voucher.voucher_done()
  127. invoice.cogs_voucher_id = cogs_voucher.id
  128. else:
  129. vals.update({
  130. 'vouch_obj_id': vouch_obj.id,
  131. 'name': invoice.name,
  132. 'string': invoice.note or '',
  133. 'amount': invoice.amount,
  134. 'credit_account_id': partner_account_id,
  135. 'debit_account_id': invoice.category_id.account_id.id,
  136. 'partner_debit': invoice.partner_id.id,
  137. 'partner_credit': invoice.partner_id.id,
  138. 'buy_tax_amount': invoice.tax_amount or 0,
  139. 'debit_auxiliary_id': invoice.auxiliary_id.id,
  140. 'currency_id': invoice.currency_id.id or '',
  141. 'rate_silent': self.env['res.currency'].get_rate_silent(
  142. self.date, invoice.currency_id.id) or 0,
  143. })
  144. if invoice.is_init:
  145. vals.update({'init_obj': 'money_invoice', })
  146. invoice.create_voucher_line(vals)
  147. # 删除初始非需要的凭证明细行,不确认凭证
  148. if invoice.is_init:
  149. vouch_line_ids = self.env['voucher.line'].search([
  150. ('account_id', '=', invoice.category_id.account_id.id),
  151. ('init_obj', '=', 'money_invoice')])
  152. # 删除凭证明细行之前先确保凭证是草稿状态
  153. if invoice.voucher_id.state == 'done':
  154. invoice.voucher_id.voucher_draft()
  155. for vouch_line_id in vouch_line_ids:
  156. vouch_line_id.unlink()
  157. else:
  158. vouch_obj.voucher_done()
  159. return res
  160. def create_voucher_line(self, vals):
  161. if vals.get('currency_id') == self.env.user.company_id.currency_id.id \
  162. or not vals.get('rate_silent'):
  163. debit = credit = vals.get('amount')
  164. sell_tax_amount = vals.get('sell_tax_amount')
  165. else:
  166. # 外币免税
  167. debit = credit = vals.get('amount') * vals.get('rate_silent')
  168. # 把税从金额中减去
  169. if vals.get('buy_tax_amount'): # 如果传入了进项税
  170. debit = vals.get('amount') - vals.get('buy_tax_amount')
  171. if vals.get('sell_tax_amount'): # 如果传入了销项税
  172. credit = vals.get('amount') - sell_tax_amount
  173. # 借方行
  174. currency_id = vals.get(
  175. 'currency_id') or self.env.user.company_id.currency_id.id
  176. if currency_id != self.env.user.company_id.currency_id.id: # 结算单上是外币
  177. self.env['voucher.line'].create({
  178. 'name': "%s %s" % (vals.get('name'), vals.get('string')),
  179. 'account_id': vals.get('debit_account_id'),
  180. 'debit': debit,
  181. 'voucher_id': vals.get('vouch_obj_id'),
  182. 'partner_id': vals.get('partner_debit', ''),
  183. 'auxiliary_id': vals.get('debit_auxiliary_id', False),
  184. 'currency_id': vals.get('currency_id'),
  185. 'currency_amount': vals.get('amount'),
  186. 'rate_silent': vals.get('rate_silent'),
  187. 'init_obj': vals.get('init_obj', False),
  188. })
  189. else: # 结算单上是本位币
  190. self.env['voucher.line'].create({
  191. 'name': "%s %s" % (vals.get('name'), vals.get('string')),
  192. 'account_id': vals.get('debit_account_id'),
  193. 'debit': debit,
  194. 'voucher_id': vals.get('vouch_obj_id'),
  195. 'partner_id': vals.get('partner_debit', ''),
  196. 'auxiliary_id': vals.get('debit_auxiliary_id', False),
  197. 'init_obj': vals.get('init_obj', False),
  198. })
  199. # 进项税行
  200. if vals.get('buy_tax_amount'):
  201. if not self.env.user.company_id.import_tax_account:
  202. raise UserError('请通过"配置-->高级配置-->公司"菜单来设置进项税科目')
  203. self.env['voucher.line'].create({
  204. 'name': "%s %s" % (vals.get('name'), vals.get('string')),
  205. 'account_id': self.env.user.company_id.import_tax_account.id,
  206. 'debit': vals.get('buy_tax_amount'),
  207. 'voucher_id': vals.get('vouch_obj_id'),
  208. })
  209. # 贷方行
  210. currency_id = vals.get(
  211. 'currency_id') or self.env.user.company_id.currency_id.id
  212. if currency_id != self.env.user.company_id.currency_id.id:
  213. self.env['voucher.line'].create({
  214. 'name': "%s %s" % (vals.get('name'), vals.get('string')),
  215. 'partner_id': vals.get('partner_credit', ''),
  216. 'account_id': vals.get('credit_account_id'),
  217. 'credit': credit,
  218. 'voucher_id': vals.get('vouch_obj_id'),
  219. 'auxiliary_id': vals.get('credit_auxiliary_id', False),
  220. 'currency_amount': vals.get('amount'),
  221. 'rate_silent': vals.get('rate_silent'),
  222. 'currency_id': vals.get('currency_id'),
  223. 'init_obj': vals.get('init_obj', False),
  224. })
  225. else:
  226. self.env['voucher.line'].create({
  227. 'name': "%s %s" % (vals.get('name'), vals.get('string')),
  228. 'partner_id': vals.get('partner_credit', ''),
  229. 'account_id': vals.get('credit_account_id'),
  230. 'credit': credit,
  231. 'voucher_id': vals.get('vouch_obj_id'),
  232. 'auxiliary_id': vals.get('credit_auxiliary_id', False),
  233. 'init_obj': vals.get('init_obj', False),
  234. })
  235. # 销项税行
  236. if vals.get('sell_tax_amount'):
  237. if not self.env.user.company_id.output_tax_account:
  238. raise UserError(
  239. '您还没有配置公司的销项税科目。\n请通过"配置-->高级配置-->公司"菜单来设置销项税科目!')
  240. self.env['voucher.line'].create({
  241. 'name': "%s %s" % (vals.get('name'), vals.get('string')),
  242. 'account_id': self.env.user.company_id.output_tax_account.id,
  243. 'credit': sell_tax_amount,
  244. 'voucher_id': vals.get('vouch_obj_id'),
  245. })
  246. return True
上海开阖软件有限公司 沪ICP备12045867号-1