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

504 行
26KB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. import datetime
  5. from odoo import fields, models, api, _
  6. from odoo.exceptions import UserError, ValidationError
  7. change = [('time', '计时'),
  8. ('piece', '计件'),
  9. ('efficiency', '计效')]
  10. class StaffWages(models.Model):
  11. _name = 'staff.wages'
  12. _description = '员工工资'
  13. _inherit = ['mail.thread']
  14. _order = "name"
  15. date = fields.Date('记帐日期', required=True)
  16. name = fields.Many2one(
  17. 'finance.period',
  18. '会计期间',
  19. compute='compute_period_id', ondelete='restrict', store=True)
  20. state = fields.Selection([('draft', '草稿'),
  21. ('done', '已完成')], '状态', default='draft',
  22. index=True,
  23. )
  24. line_ids = fields.One2many('wages.line', 'order_id', '工资明细行',
  25. copy=True)
  26. payment = fields.Many2one('bank.account', '付款方式', default=lambda self: self.env.company.bank_account_id)
  27. wage_other_money_order = fields.Many2one('other.money.order', '工资对应付款单', readonly=True, ondelete='restrict',
  28. help='确认时生成的对应付款单', copy=False)
  29. housing_other_money_order = fields.Many2one('other.money.order', '住房公积金对应付款单', readonly=True,
  30. ondelete='restrict', help='确认时生成的对应付款单', copy=False)
  31. social_security_other_money_order = fields.Many2one('other.money.order', '社保对应付款单', readonly=True,
  32. ondelete='restrict', help='确认时生成的对应付款单', copy=False)
  33. voucher_id = fields.Many2one(
  34. 'voucher', '计提工资凭证', readonly=True, ondelete='set null', copy=False)
  35. change_voucher_id = fields.Many2one(
  36. 'voucher', '修正计提凭证', readonly=True, ondelete='set null', copy=False)
  37. note = fields.Char('备注', help='本月备注')
  38. # 汇总字段
  39. total_wage = fields.Float('应发工资合计', digits=(12, 2))
  40. total_endowment = fields.Float('应扣养老合计', digits=(12, 2))
  41. total_health = fields.Float('应扣医疗合计', digits=(12, 2))
  42. total_unemployment = fields.Float('应扣失业合计', digits=(12, 2))
  43. total_housing_fund = fields.Float('应扣住房合计', digits=(12, 2))
  44. total_personal_tax = fields.Float('应扣个税合计', digits=(12, 2))
  45. total_amount = fields.Float('实发工资合计', digits=(12, 2))
  46. total_endowment_co = fields.Float('应扣公司养老合计', digits=(12, 2))
  47. total_health_co = fields.Float('应扣公司医疗合计', digits=(12, 2))
  48. total_unemployment_co = fields.Float('应扣公司失业合计', digits=(12, 2))
  49. total_housing_fund_co = fields.Float('应扣公司住房合计', digits=(12, 2))
  50. total_injury = fields.Float('应扣公司工伤保险合计', digits=(12, 2), help='公司承担的工伤保险合计')
  51. total_maternity = fields.Float('应扣公司生育保险合计', digits=(12, 2), help='公司承担的生育保险合计')
  52. @api.depends('date')
  53. def compute_period_id(self):
  54. for record in self:
  55. record.name = self.env['finance.period'].get_period(record.date)
  56. @api.onchange('line_ids')
  57. def _total_amount_wage(self):
  58. for record in self:
  59. record.total_amount = sum(line.amount_wage for line in record.line_ids)
  60. record.total_wage = sum(line.all_wage for line in record.line_ids)
  61. record.total_endowment = sum(line.endowment for line in record.line_ids)
  62. record.total_health = sum(line.health for line in record.line_ids)
  63. record.total_unemployment = sum(line.unemployment for line in record.line_ids)
  64. record.total_housing_fund = sum(line.housing_fund for line in record.line_ids)
  65. record.total_personal_tax = sum(line.personal_tax for line in record.line_ids)
  66. record.total_endowment_co = sum(line.endowment_co for line in record.line_ids)
  67. record.total_health_co = sum(line.health_co for line in record.line_ids)
  68. record.total_unemployment_co = sum(line.unemployment_co for line in record.line_ids)
  69. record.total_housing_fund_co = sum(line.housing_fund_co for line in record.line_ids)
  70. record.total_injury = sum(line.injury for line in record.line_ids)
  71. record.total_maternity = sum(line.maternity for line in record.line_ids)
  72. def staff_wages_accrued(self):
  73. """计提工资"""
  74. self._total_amount_wage()
  75. for record in self:
  76. if not record.line_ids:
  77. raise UserError('明细行不能为空')
  78. if record.voucher_id:
  79. # 如计提凭证已存在,判断当前期间是否与工资期间相同
  80. date = fields.Date.context_today(self)
  81. if self.voucher_id.period_id == self.env['finance.period'].get_period(date):
  82. # 如相同,反审核计提凭证并重新生成计提凭证
  83. voucher, self.voucher_id = self.voucher_id, False
  84. if voucher.state == 'done':
  85. voucher.voucher_draft()
  86. voucher.unlink()
  87. voucher = self.create_voucher(self.date)
  88. voucher.voucher_done()
  89. self.voucher_id = voucher
  90. else:
  91. # 如不同,生成并审核修正凭证
  92. self.change_voucher()
  93. else:
  94. voucher = self.create_voucher(self.date)
  95. voucher.voucher_done()
  96. self.voucher_id = voucher
  97. def change_voucher(self):
  98. """
  99. 生成并审核修正计提凭证
  100. :return:
  101. """
  102. before_voucher = self.voucher_id
  103. if self.change_voucher_id:
  104. # 如果修正计提凭证存在,则删除后重新生成修正计提凭证
  105. change_voucher, self.change_voucher_id = self.change_voucher_id, False
  106. if change_voucher.state == 'done':
  107. change_voucher.voucher_draft()
  108. change_voucher.unlink()
  109. change_voucher = self.create_voucher()
  110. for change_line in change_voucher.line_ids:
  111. for before_line in before_voucher.line_ids:
  112. if change_line.account_id == before_line.account_id and change_line.auxiliary_id == before_line.auxiliary_id:
  113. change_line.credit -= before_line.credit
  114. change_line.debit -= before_line.debit
  115. continue
  116. for change_line in change_voucher.line_ids:
  117. if change_line.credit == 0 and change_line.debit == 0:
  118. change_line.unlink()
  119. if not change_voucher.line_ids:
  120. change_voucher.unlink()
  121. else:
  122. change_voucher.voucher_done()
  123. self.write({'change_voucher_id': change_voucher.id})
  124. def create_credit_line(self, voucher, name, account, credit, auxiliary_id):
  125. """
  126. 生成贷方行
  127. :param voucher: 凭证
  128. :param name: 摘要
  129. :param account: 借方科目
  130. :param credit: 贷方金额
  131. :return:
  132. """
  133. if credit:
  134. vals = {
  135. 'voucher_id': voucher.id,
  136. 'name': name,
  137. 'account_id': account.id,
  138. 'credit': credit,
  139. 'auxiliary_id': auxiliary_id and auxiliary_id.id,
  140. }
  141. self.env['voucher.line'].create(vals)
  142. def create_voucher(self, date=datetime.date.today()):
  143. """
  144. 生成并审核计提凭证
  145. :param date: 一个日期
  146. :return:
  147. """
  148. self.ensure_one()
  149. vouch_obj = self.env['voucher'].create({'date': date, 'ref': '%s,%s' % (self._name, self.id)})
  150. credit_account = self.env.ref('staff_wages.staff_wages')
  151. res = {}
  152. for line in self.line_ids:
  153. staff = self.env['staff.contract'].search([('staff_id', '=', line.name.id)])
  154. debit_account = staff.job_id and staff.job_id.account_id or self.env.ref('finance.small_business_chart5602001')
  155. if debit_account not in res:
  156. res[debit_account] = {'debit': 0}
  157. val = res[debit_account]
  158. debit = val.get('debit') + line.all_wage + line.housing_fund_co + line.endowment_co + line.health_co + line.unemployment_co + line.injury + line.maternity
  159. if debit:
  160. val.update({
  161. 'debit': val.get('debit') + line.all_wage + line.housing_fund_co
  162. + line.endowment_co + line.health_co + line.unemployment_co
  163. + line.injury + line.maternity,
  164. 'voucher_id': vouch_obj.id,
  165. 'account_id': debit_account.id,
  166. 'name': u'提本月工资'})
  167. # 生成借方凭证行
  168. for account_id, val in res.items():
  169. self.env['voucher.line'].create(
  170. dict(val, account_id=account_id.id))
  171. # 生成贷方凭证行
  172. endowment_co = self.env.ref('staff_wages.categ_endowment_co') # 公司缴纳养老类别
  173. health_co = self.env.ref('staff_wages.categ_health_co') # 公司缴纳医疗类别
  174. unemployment_co = self.env.ref('staff_wages.categ_unemployment_co') # 公司缴纳失业类别
  175. maternity = self.env.ref('staff_wages.categ_maternity') # 公司缴纳生育类别
  176. injury = self.env.ref('staff_wages.categ_injury') # 公司缴纳工伤类别
  177. housing_co = self.env.ref('staff_wages.categ_housing_fund_co') # 公司缴纳住房公积金类别
  178. for line in self.line_ids:
  179. self.create_credit_line(vouch_obj, u'提本月工资', credit_account.account_id, line.all_wage, line.name.auxiliary_id)
  180. self.create_credit_line(vouch_obj, u'提本月养老保险', endowment_co.account_id, self.total_endowment_co, False)
  181. self.create_credit_line(vouch_obj, u'提本月医疗保险', health_co.account_id, self.total_health_co, False)
  182. self.create_credit_line(vouch_obj, u'提本月失业保险', unemployment_co.account_id, self.total_unemployment_co, False)
  183. self.create_credit_line(vouch_obj, u'提本月生育保险', maternity.account_id, self.total_maternity, False)
  184. self.create_credit_line(vouch_obj, u'提本月工伤保险', injury.account_id, self.total_injury, False)
  185. self.create_credit_line(vouch_obj, u'提本月公积金', housing_co.account_id, self.total_housing_fund_co, False)
  186. return vouch_obj
  187. def staff_wages_confirm(self):
  188. """确认工资单,生成付款单"""
  189. for record in self:
  190. if record.state == 'done':
  191. raise UserError('请不要重复确认')
  192. if not record.voucher_id:
  193. raise UserError('工资单还未计提,请先计提')
  194. record.create_other_pay_wage()
  195. record.create_other_pay_housing_fund()
  196. record.create_other_pay_social_security()
  197. record.state = 'done'
  198. def create_other_pay_wage(self):
  199. '''选择结算账户,生成其他支出单 '''
  200. self.ensure_one()
  201. StaffWages = self.env.ref('staff_wages.staff_wages')
  202. endowment = self.env.ref('staff_wages.endowment')
  203. unemployment = self.env.ref('staff_wages.unemployment')
  204. housing_fund = self.env.ref('staff_wages.housing_fund')
  205. health = self.env.ref('staff_wages.health')
  206. personal_tax = self.env.ref('staff_wages.personal_tax')
  207. other_money_order = self.env['other.money.order'].create({
  208. 'state': 'draft',
  209. 'type': 'other_pay',
  210. 'date': self.date,
  211. 'bank_id': self.payment.id,
  212. 'ref': '%s,%s' % (self._name, self.id)
  213. })
  214. for line in self.line_ids:
  215. self.env['other.money.order.line'].create({
  216. 'other_money_id': other_money_order.id,
  217. 'amount': line.all_wage,
  218. 'category_id': StaffWages and StaffWages.id,
  219. 'auxiliary_id': line.name.auxiliary_id.id,
  220. })
  221. if self.total_endowment:
  222. self.env['other.money.order.line'].create({
  223. 'other_money_id': other_money_order.id,
  224. 'amount': -1 * self.total_endowment,
  225. 'category_id': endowment and endowment.id
  226. })
  227. if self.total_unemployment:
  228. self.env['other.money.order.line'].create({
  229. 'other_money_id': other_money_order.id,
  230. 'amount': -1 * self.total_unemployment,
  231. 'category_id': unemployment and unemployment.id
  232. })
  233. if self.total_housing_fund:
  234. self.env['other.money.order.line'].create({
  235. 'other_money_id': other_money_order.id,
  236. 'amount': -1 * self.total_housing_fund,
  237. 'category_id': housing_fund and housing_fund.id
  238. })
  239. if self.total_health:
  240. self.env['other.money.order.line'].create({
  241. 'other_money_id': other_money_order.id,
  242. 'amount': -1 * self.total_health,
  243. 'category_id': health and health.id
  244. })
  245. if self.total_personal_tax:
  246. self.env['other.money.order.line'].create({
  247. 'other_money_id': other_money_order.id,
  248. 'amount': -1 * self.total_personal_tax,
  249. 'category_id': personal_tax and personal_tax.id
  250. })
  251. self.wage_other_money_order = other_money_order
  252. def create_other_pay_housing_fund(self):
  253. """
  254. 生成住房公积金的其他支出单
  255. 审核时生成凭证:
  256. 借:应付职工薪酬-公积金 公司公积金
  257. 其他应付款-代扣公积金 个人公积金
  258. 贷: 付款方式对应的科目
  259. :return: 其他支出单
  260. """
  261. self.ensure_one()
  262. housing_co = self.env.ref('staff_wages.categ_housing_fund_co') # 公司缴纳住房公积金类别
  263. housing = self.env.ref('staff_wages.housing_fund') # 个人缴纳住房公积金类别
  264. order = self.env['other.money.order'].create({
  265. 'state': 'draft',
  266. 'type': 'other_pay',
  267. 'date': self.date,
  268. 'bank_id': self.payment.id,
  269. 'ref': '%s,%s' % (self._name, self.id)
  270. })
  271. self.create_other_order_line(order, housing_co, self.total_housing_fund_co)
  272. self.create_other_order_line(order, housing, self.total_housing_fund)
  273. self.housing_other_money_order = order
  274. def create_other_pay_social_security(self):
  275. """
  276. 生成社保的其他支出单
  277. 审核时生成凭证:
  278. 借: 5个公司的,3个个人的
  279. 贷: 付款方式对应的科目
  280. :return: 其他支出单
  281. """
  282. for record in self:
  283. other_order = self.env['other.money.order'].create({
  284. 'type': 'other_pay',
  285. 'date': record.date,
  286. 'bank_id': record.payment.id,
  287. 'state': 'draft',
  288. 'ref': '%s,%s' % (record._name, record.id)
  289. })
  290. endowment_co = self.env.ref('staff_wages.categ_endowment_co') # 公司缴纳养老类别
  291. health_co = self.env.ref('staff_wages.categ_health_co') # 公司缴纳医疗类别
  292. unemployment_co = self.env.ref('staff_wages.categ_unemployment_co') # 公司缴纳失业类别
  293. maternity = self.env.ref('staff_wages.categ_maternity') # 公司缴纳生育类别
  294. injury = self.env.ref('staff_wages.categ_injury') # 公司缴纳工伤类别
  295. endowment = self.env.ref('staff_wages.endowment') # 个人缴纳养老类别
  296. health = self.env.ref('staff_wages.health') # 个人缴纳医疗类别
  297. unemployment = self.env.ref('staff_wages.unemployment') # 个人缴纳失业类别
  298. record.create_other_order_line(other_order, endowment_co, record.total_endowment_co)
  299. record.create_other_order_line(other_order, health_co, record.total_health_co)
  300. record.create_other_order_line(other_order, unemployment_co, record.total_unemployment_co)
  301. record.create_other_order_line(other_order, maternity, record.total_maternity)
  302. record.create_other_order_line(other_order, injury, record.total_injury)
  303. # 个人的
  304. record.create_other_order_line(other_order, endowment, record.total_endowment)
  305. record.create_other_order_line(other_order, health, record.total_health)
  306. record.create_other_order_line(other_order, unemployment, record.total_unemployment)
  307. record.social_security_other_money_order = other_order
  308. def create_other_order_line(self, order, category_id, amount):
  309. """
  310. 生成其他支出单明细行
  311. :param order: 其他支出单
  312. :param category_id: 类别
  313. :param amount: 金额
  314. :return:
  315. """
  316. if amount:
  317. self.env['other.money.order.line'].create({
  318. 'other_money_id': order.id,
  319. 'amount': amount,
  320. 'category_id': category_id and category_id.id
  321. })
  322. def staff_wages_draft(self):
  323. """撤销确认"""
  324. for record in self:
  325. if self.state == 'draft':
  326. raise UserError(u'请不要重复撤销')
  327. if record.housing_other_money_order:
  328. housing_other_money_order, record.housing_other_money_order = record.housing_other_money_order, False
  329. if housing_other_money_order.state == 'done':
  330. housing_other_money_order.other_money_draft()
  331. # 草稿状态的其他付款单,清除 ref 后删除
  332. housing_other_money_order.write({'ref': False})
  333. housing_other_money_order.unlink()
  334. if record.social_security_other_money_order:
  335. social_security_other_money_order, record.social_security_other_money_order = record.social_security_other_money_order, False
  336. if social_security_other_money_order.state == 'done':
  337. social_security_other_money_order.other_money_draft()
  338. social_security_other_money_order.write({'ref': False})
  339. social_security_other_money_order.unlink()
  340. if record.wage_other_money_order:
  341. wage_other_money_order, record.wage_other_money_order = record.wage_other_money_order, False
  342. if wage_other_money_order.state == 'done':
  343. wage_other_money_order.other_money_draft()
  344. wage_other_money_order.write({'ref': False})
  345. wage_other_money_order.unlink()
  346. record.state = 'draft'
  347. def unlink(self):
  348. for record in self:
  349. # 先解除凭证和工资单关系,再将它们删除
  350. voucher, record.voucher_id = record.voucher_id, False
  351. if voucher:
  352. voucher, record.voucher_id = record.voucher_id, False
  353. if voucher.state == 'done':
  354. voucher.voucher_draft()
  355. voucher.unlink()
  356. change_voucher, record.change_voucher_id = record.change_voucher_id, False
  357. if change_voucher:
  358. if change_voucher.state == 'done':
  359. change_voucher.voucher_draft()
  360. change_voucher.unlink()
  361. return super(StaffWages, self).unlink()
  362. class WagesLine(models.Model):
  363. _name = 'wages.line'
  364. _description = '工资明细行'
  365. order_id = fields.Many2one('staff.wages', '工资单', required=True, ondelete='cascade')
  366. name = fields.Many2one('staff', '员工', required=True)
  367. basic_wage = fields.Float('基本工资', digits=(12, 2), required=True)
  368. basic_date = fields.Integer('月计薪天数', required=True, default=21.75)
  369. date_number = fields.Integer('出勤天数', required=False, default=0)
  370. add_hour = fields.Integer('加班小时数', default=0)
  371. add_wage = fields.Float('加班工资', digits=(12, 2), default=0)
  372. other_wage = fields.Float('补助', digits=(12, 2), default=0)
  373. deduction = fields.Float('扣款', digits=(12, 2), default=0)
  374. wage = fields.Float('出勤工资金额', digits=(12, 2), compute='_all_wage_value', store=True)
  375. all_wage = fields.Float('应发工资', digits=(12, 2), compute='_all_wage_value', store=True)
  376. endowment = fields.Float('个人养老保险', digits=(12, 2), default=0)
  377. health = fields.Float('个人医疗保险', digits=(12, 2), default=0)
  378. unemployment = fields.Float('个人失业保险', digits=(12, 2), default=0)
  379. housing_fund = fields.Float('个人住房公积金', digits=(12, 2), default=0)
  380. endowment_co = fields.Float('公司养老保险', digits=(12, 2), default=0, help='公司承担的养老保险')
  381. health_co = fields.Float('公司医疗保险', digits=(12, 2), default=0, help='公司承担的医疗保险')
  382. unemployment_co = fields.Float('公司失业保险', digits=(12, 2), default=0, help='公司承担的失业保险')
  383. housing_fund_co = fields.Float('公司住房公积金', digits=(12, 2), default=0, help='公司承担的住房公积金')
  384. injury = fields.Float('公司工伤保险', digits=(12, 2), default=0, help='公司承担的工伤保险')
  385. maternity = fields.Float('公司生育保险', digits=(12, 2), default=0, help='公司承担的生育保险')
  386. # 因为个人所得税的计算不确定,暂改为手动录入。
  387. # personal_tax = fields.Float('个人所得税', digits=(12, 2), compute='_personal_tax_value', store=True)
  388. personal_tax = fields.Float('个人所得税', digits=(12, 2))
  389. amount_wage = fields.Float('实发工资', digits=(12, 2), compute='_amount_wage_value', store=True)
  390. wages_change = fields.Selection(change, '记工类型')
  391. _sql_constraints = [
  392. ('staff_uniq', 'unique(order_id, name)', '同一个工资单不能出现重名员工工资行')
  393. ]
  394. @api.onchange('date_number', 'basic_wage', 'basic_date')
  395. def change_wage_addhour(self):
  396. for record in self:
  397. if record.date_number > 31 or record.basic_date > 31:
  398. raise UserError('一个月不可能超过31天')
  399. if record.date_number >= record.basic_date:
  400. record.add_hour = 8 * (record.date_number - record.basic_date)
  401. record.date_number = record.basic_date
  402. record.add_wage = round(
  403. 2 * (record.add_hour / 8) * (record.basic_wage / (record.basic_date or 1)), 2)
  404. @api.onchange('add_hour')
  405. def change_add_wage(self):
  406. for record in self:
  407. if record.add_hour:
  408. record.add_wage = round(
  409. 2 * (record.add_hour / 8) * (record.basic_wage / (record.basic_date or 1)), 2)
  410. @api.onchange('name')
  411. def change_social_security(self):
  412. """选择员工后,自动带出五险一金"""
  413. for record in self:
  414. social_security = self.env['staff.contract'].search(
  415. [('staff_id', '=', record.name.id)], limit=1)
  416. if social_security:
  417. record.basic_wage = social_security.basic_wage
  418. record.endowment = social_security.endowment
  419. record.health = social_security.health
  420. record.unemployment = social_security.unemployment
  421. record.housing_fund = social_security.housing_fund
  422. record.endowment_co = social_security.endowment_co
  423. record.health_co = social_security.health_co
  424. record.unemployment_co = social_security.unemployment_co
  425. record.housing_fund_co = social_security.housing_fund_co
  426. record.injury = social_security.injury
  427. record.maternity = social_security.maternity
  428. @api.depends('date_number', 'basic_date', 'add_wage', 'other_wage', 'basic_wage', 'deduction')
  429. def _all_wage_value(self):
  430. for record in self:
  431. if record.date_number >= record.basic_date:
  432. record.wage = record.basic_wage
  433. else:
  434. record.wage = round(
  435. (record.date_number / record.basic_date or 1) * record.basic_wage, 2)
  436. record.all_wage = record.wage + record.add_wage + record.other_wage - record.deduction
  437. @api.depends('all_wage', 'endowment', 'health', 'unemployment', 'housing_fund')
  438. def _personal_tax_value(self):
  439. for record in self:
  440. total = record.all_wage - record.endowment - \
  441. record.health - record.unemployment - record.housing_fund
  442. amount = total - 3500
  443. if amount > 80000:
  444. record.personal_tax = round(amount * 0.45 - 13505, 2)
  445. elif amount > 55000:
  446. record.personal_tax = round(amount * 0.35 - 5505, 2)
  447. elif amount > 35000:
  448. record.personal_tax = round(amount * 0.3 - 2755, 2)
  449. elif amount > 9000:
  450. record.personal_tax = round(amount * 0.25 - 1005, 2)
  451. elif amount > 4500:
  452. record.personal_tax = round(amount * 0.2 - 555, 2)
  453. elif amount > 1500:
  454. record.personal_tax = round(amount * 0.1 - 105, 2)
  455. elif amount >= 0:
  456. record.personal_tax = round(amount * 0.03, 2)
  457. else:
  458. record.personal_tax = 0
  459. @api.depends('all_wage', 'endowment', 'health', 'unemployment', 'housing_fund', 'personal_tax')
  460. def _amount_wage_value(self):
  461. for record in self:
  462. record.amount_wage = record.all_wage - record.endowment - record.health - \
  463. record.unemployment - record.housing_fund - record.personal_tax
  464. class AddWagesChange(models.Model):
  465. _inherit = 'staff.contract'
  466. wages_change = fields.Selection(change, '记工类型')
上海开阖软件有限公司 沪ICP备12045867号-1