GoodERP
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

216 行
10KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import UserError
  5. class HrExpense(models.Model):
  6. _name = 'hr.expense'
  7. _inherit = ['mail.thread', 'mail.activity.mixin']
  8. _description = '报销'
  9. name = fields.Char('单据编号', copy=False)
  10. date = fields.Date(string='报销日期', default=fields.Date.today, copy=False, required=True, help='报销发生日期')
  11. staff = fields.Many2one('staff', '报销员工', required=True, help='选择员工')
  12. invoice_all_total = fields.Float(string='费用金额合计', store=True, readonly=True, compute='_compute_invoice_all_total', tracking=True, digits='Amount')
  13. state = fields.Selection([('draft', '草稿'), ('confirm', '已提交'), ('done', '已支付')], '状态', default='draft', tracking=True)
  14. line_ids = fields.One2many('hr.expense.line', 'order_id', '明细发票行', copy=False)
  15. note = fields.Text('备注')
  16. bank_account_id = fields.Many2one('bank.account', '账号', ondelete='restrict', help='付给个人')
  17. other_money_order = fields.Many2one('other.money.order', '对应其他付款单', readonly=True, ondelete='restrict', copy=False)
  18. @api.depends('line_ids')
  19. def _compute_invoice_all_total(self):
  20. for bill in self:
  21. total = sum(i.invoice_total for i in bill.line_ids)
  22. bill.invoice_all_total = total
  23. def check_consistency(self):
  24. for bill in self:
  25. for line in bill.line_ids:
  26. if line.staff.id != bill.staff.id:
  27. raise UserError('费用明细必须是同一人!')
  28. @api.model_create_multi
  29. def create(self, vals_list):
  30. res = super().create(vals_list)
  31. res.check_consistency()
  32. return res
  33. def write(self, vals):
  34. res = super().write(vals)
  35. self.check_consistency()
  36. return res
  37. def unlink(self):
  38. for record in self:
  39. if record.state != 'draft':
  40. raise UserError('只能删除草稿状态的费用报销单')
  41. # 如果已关联其他付款单,检查其状态
  42. if record.other_money_order:
  43. if record.other_money_order.state == 'done':
  44. raise UserError(
  45. '报销单 %s 已关联已确认的其他付款单(%s),\n'
  46. '不能删除。请先作废其他付款单。'
  47. % (record.name, record.other_money_order.name)
  48. )
  49. else:
  50. # 草稿状态的其他付款单,清除 ref 后删除
  51. record.other_money_order.write({'ref': False})
  52. record.other_money_order.unlink()
  53. return super().unlink()
  54. def hr_expense_confirm(self):
  55. for bill in self:
  56. if bill.state == 'confirm':
  57. raise UserError('请不要重复确认')
  58. bill.to_other_money_order()
  59. bill.state = 'confirm'
  60. # 报销单审核后,自动将报销行状态改为 confirm
  61. bill.line_ids.write({'state': 'confirm'})
  62. def hr_expense_draft(self):
  63. for bill in self:
  64. if bill.state == 'draft':
  65. raise UserError('请不要重复撤销 %s' % bill.name)
  66. if bill.other_money_order:
  67. other_money_order = bill.other_money_order
  68. bill.other_money_order = False
  69. if other_money_order.state == 'done':
  70. other_money_order.other_money_draft()
  71. # 弃审时先清空 ref,再删除其他付款单
  72. other_money_order.write({'ref': False})
  73. other_money_order.unlink()
  74. bill.state = 'draft'
  75. def to_other_money_order(self):
  76. for bill in self:
  77. if not bill.staff.partner_id:
  78. raise UserError(u'员工 %s 未关联往来单位,请先在员工档案中设置往来单位!' % bill.staff.name)
  79. other_money_order = self.env['other.money.order'].create({
  80. 'state': 'draft',
  81. 'partner_id': bill.staff.partner_id.id,
  82. 'date': bill.date,
  83. 'name': '/',
  84. 'type': 'other_pay',
  85. 'bank_id': bill.bank_account_id.id,
  86. 'note': bill.note,
  87. 'ref': '%s,%s' % (bill._name, bill.id)
  88. })
  89. bill.write({'other_money_order': other_money_order.id})
  90. for line in bill.line_ids:
  91. if line.invoice_type != 'zy':
  92. invoice_tax = 0
  93. invoice_amount = line.invoice_amount + line.invoice_tax
  94. else:
  95. invoice_tax = line.invoice_tax
  96. invoice_amount = line.invoice_amount
  97. bill.env['other.money.order.line'].create({
  98. 'other_money_id': other_money_order.id,
  99. 'amount': invoice_amount,
  100. 'auxiliary_id': line.staff.auxiliary_id.id,
  101. 'tax_rate': line.invoice_amount and invoice_tax / line.invoice_amount * 100 or 0,
  102. 'tax_amount': invoice_tax,
  103. 'category_id': line.category_id and line.category_id.id or False,
  104. })
  105. class HrExpenseLine(models.Model):
  106. _name = 'hr.expense.line'
  107. _inherit = ['mail.thread']
  108. _description = '报销行'
  109. _order = 'date'
  110. name = fields.Char('单据编号', index=True, copy=False, help='报销单的唯一编号')
  111. staff = fields.Many2one('staff', string='报销员工', related='order_id.staff', store=True, readonly=True, help='自动关联上级报销单的报销员工')
  112. invoice_type = fields.Selection([('pt', '增值税普通发票'), ('zy', '增值税专用发票'), ('dz', '电子普通发票')], '发票类别')
  113. invoice_code = fields.Char('发票代码', copy=False)
  114. invoice_name = fields.Char('发票号码', copy=False)
  115. invoice_amount = fields.Float(string='发票金额', digits='Amount', required=True, help='如是增值税发票请填不含税金额')
  116. invoice_tax = fields.Float(string='发票税额', digits='Amount', help='如是增值税发票请填不含税金额')
  117. invoice_total = fields.Float(string='发票金额合计', store=True, readonly=True, compute='_compute_cost_total', digits='Amount')
  118. invoice_heck_code = fields.Char('发票校验码', copy=False)
  119. invoice_date = fields.Char('发票日期', copy=False)
  120. note = fields.Text('备注')
  121. state = fields.Selection([('draft', '草稿'), ('confirm', '已提交'), ('done', '已支付')], '状态', default='draft', copy=False)
  122. category_id = fields.Many2one('core.category', '类别', ondelete='restrict', required=True, help='类型:运费、咨询费等', domain=[('type', '=', 'other_pay')])
  123. date = fields.Date(string='费用日期', default=fields.Date.context_today, copy=False, required=True, help='费用发生日期')
  124. order_id = fields.Many2one('hr.expense', '报销单号', index=True, copy=False, readonly=True)
  125. is_pay = fields.Boolean(string='已付款', readonly=True, copy=False)
  126. _sql_constraints = [('unique_invoice_code_name', 'unique (invoice_code, invoice_name)', '发票代码+发票号码不能相同!')]
  127. attachment_number = fields.Integer(compute='_compute_attachment_number', string='附件号')
  128. @api.depends('invoice_amount', 'invoice_tax')
  129. def _compute_cost_total(self):
  130. for bill in self:
  131. bill.invoice_total = bill.invoice_amount + bill.invoice_tax
  132. @api.model
  133. def saomiaofapiao(self, model_name, barcode, order_id):
  134. barcode = barcode.replace(',', ',').replace('。', '.')
  135. code = barcode.split(',')
  136. invoice_heck_code = ''
  137. if len(code) < 5:
  138. raise UserError('请确认扫描是否正确%s' % code)
  139. if code[0] == '01':
  140. if code[1] == '10':
  141. invoice_type = 'dz'
  142. invoice_heck_code = code[6]
  143. if code[1] == '01':
  144. invoice_type = 'zy'
  145. if code[1] == '04':
  146. invoice_type = 'pt'
  147. invoice_heck_code = code[6]
  148. invoice_code = code[2]
  149. invoice_name = code[3]
  150. invoice_amount = round(float(code[4]), 2)
  151. invoice_date = code[5]
  152. invoice_tax = 0
  153. self.browse(order_id).write({
  154. 'invoice_type': invoice_type, 'invoice_code': invoice_code, 'invoice_name': invoice_name,
  155. 'invoice_amount': invoice_amount, 'invoice_tax': invoice_tax,
  156. 'invoice_heck_code': invoice_heck_code, 'invoice_date': invoice_date,
  157. })
  158. def action_get_attachment_view(self):
  159. res = self.env.ref('base.action_attachment')
  160. res['domain'] = [('res_model', '=', 'hr.expense.line'), ('res_id', 'in', self.ids)]
  161. res['context'] = {'default_res_model': 'hr.expense.line', 'default_res_id': self.id}
  162. return res
  163. def _compute_attachment_number(self):
  164. attachment_data = self.env['ir.attachment'].read_group(
  165. [('res_model', '=', 'hr.expense.line'), ('res_id', 'in', self.ids)], ['res_id'], ['res_id'])
  166. attachment = dict((data['res_id'], data['res_id_count']) for data in attachment_data)
  167. for bill in self:
  168. bill.attachment_number = attachment.get(bill.id, 0)
  169. def unlink(self):
  170. for bill in self:
  171. # 只有关联的报销单已提交或已支付时才限制删除
  172. if bill.order_id and bill.order_id.state in ['confirm', 'done']:
  173. raise UserError('关联的报销单已%s,不能删除费用发票' % bill.order_id.state)
  174. return super().unlink()
  175. class OtherMoneyOrder(models.Model):
  176. _inherit = 'other.money.order'
  177. def other_money_done(self):
  178. self.ensure_one()
  179. if self.ref._name == 'hr.expense':
  180. self.ref.state = 'done'
  181. for bill in self.ref.line_ids:
  182. bill.state = 'done'
  183. bill.is_pay = True
  184. return super().other_money_done()
  185. def other_money_draft(self, is_init=False):
  186. self.ensure_one()
  187. if self.ref._name == 'hr.expense':
  188. self.ref.state = 'confirm'
  189. for bill in self.ref.line_ids:
  190. bill.state = 'confirm'
  191. bill.is_pay = False
  192. return super().other_money_draft(is_init)
上海开阖软件有限公司 沪ICP备12045867号-1