141 lines
6.2 KiB
Python
141 lines
6.2 KiB
Python
from datetime import datetime
|
|
from odoo import models, fields, api
|
|
import datetime
|
|
|
|
|
|
class InformationSummaryTable(models.Model):
|
|
_name = 'information.summary.table'
|
|
_description = '信息汇总表收集'
|
|
|
|
employee_id = fields.Many2one('hr.employee', string='人员')
|
|
company_id = fields.Many2one(related='employee_id.company_id', string='公司')
|
|
department_id = fields.Many2one(related='employee_id.department_id', string="部门")
|
|
user_role_id = fields.Many2one(related='employee_id.user_role_id', string="角色")
|
|
account_type = fields.Selection([('to', '待确认', ), ('ok', '已确认')], string='状态', default='to')
|
|
summary_line = fields.One2many('information.summary.line', 'summary_id', string='汇总表')
|
|
month = fields.Selection(selection=[(str(i), str(i)) for i in range(1, 13)], string='月份')
|
|
year = fields.Integer('年')
|
|
create_date = fields.Datetime(string='创建日期', default=fields.Datetime.now)
|
|
|
|
def open_record(self):
|
|
return {
|
|
"type": "ir.actions.act_window",
|
|
"res_model": 'information.summary.table',
|
|
"res_id": self.id,
|
|
"name": "详情",
|
|
"view_mode": "form",
|
|
}
|
|
|
|
def button_put(self):
|
|
menu_id = self.env['ir.ui.menu'].search([('name', '=', '信息汇总表收集')])
|
|
for record in self.search([('account_type', '=', 'to')]):
|
|
url = f'https://oa.thtzjt.com/web#id={self.id}&cids=2&action={menu_id.action.id}&model=information.summary.table&view_type=list&menu_id={menu_id.id}'
|
|
self.sudo().env["wecom.apps"].sync_send_message_textcard(category='notice', user_id=record.employee_id.wecom_userid, title="信息汇总表",
|
|
description="您有待确认的信息汇总表请尽快确认", url=url, btntxt="详细信息")
|
|
record.account_type = 'ok'
|
|
|
|
def account_task(self):
|
|
self.ensure_one()
|
|
summary_settings = self.env['summary.table.setting'].search([
|
|
('employee_ids', '!=', False)
|
|
])
|
|
current_month = str(datetime.now().month)
|
|
for setting in summary_settings:
|
|
employees = setting.employee_ids
|
|
for employee in employees:
|
|
summary_table = self.env['information.summary.table'].search([
|
|
('employee_id', '=', employee.id),
|
|
('month', '=', current_month)
|
|
], limit=1) #
|
|
|
|
if not summary_table:
|
|
summary_table = self.env['information.summary.table'].create({
|
|
'employee_id': employee.id,
|
|
'month': current_month,
|
|
'account_type': 'to',
|
|
'create_date': fields.Datetime.now()
|
|
})
|
|
|
|
existing_line = self.env['information.summary.line'].search([
|
|
('summary_id', '=', summary_table.id),
|
|
('table_id', '=', setting.id)
|
|
], limit=1)
|
|
|
|
if not existing_line:
|
|
self.env['information.summary.line'].create({
|
|
'summary_id': summary_table.id,
|
|
'table_id': setting.id,
|
|
'this_year': 0.0,
|
|
'remake': ''
|
|
})
|
|
|
|
def compute_total(self, date, company=None):
|
|
company = company or []
|
|
if company == 0:
|
|
company = []
|
|
# 转变为日期格式方便取月份
|
|
date_obj = datetime.datetime.strptime(date, "%Y-%m-%d").date()
|
|
year = date_obj.year
|
|
month = date_obj.month
|
|
domain = [('month', '=', month), ('year', '=', year)]
|
|
if len(company) > 0:
|
|
domain.append(('company_id', 'in', company))
|
|
cons = self.search(domain)
|
|
one = {}
|
|
# 各项本期数B 说明 公司当期数
|
|
for con in cons:
|
|
for re in con.summary_line:
|
|
if re.table_id not in one.keys():
|
|
one[re.table_id] = {'B': re.this_year or 0, 'E': re.remake or ""}
|
|
one[re.table_id][con.company_id.id] = re.this_year
|
|
else:
|
|
one[re.table_id]['B'] += re.this_year
|
|
one[re.table_id]['E'] += re.this_year
|
|
# 去年年底A
|
|
last_domain = [('month', '=', 12), ('year', '=', year-1)]
|
|
if len(company) > 0:
|
|
last_domain.append(('company_id', 'in', company))
|
|
last_cons = self.search(last_domain)
|
|
for last_con in last_cons:
|
|
for last_re in last_con.summary_line:
|
|
if last_re.table_id not in one.keys():
|
|
one[last_re.table_id] = {"A": last_re.this_year or 0}
|
|
else:
|
|
if 'A' not in one[last_re.table_id].keys():
|
|
one[last_re.table_id]["A"] = last_re.this_year or 0
|
|
else:
|
|
one[last_re.table_id]["A"] += last_re.this_year
|
|
# 去年同期C
|
|
lat_domain = [('month', '=', month), ('year', '=', year-1)]
|
|
if len(company) > 0:
|
|
lat_domain.append(('company_id', 'in', company))
|
|
lat_cons = self.search(lat_domain)
|
|
for lat_con in lat_cons:
|
|
for lat_re in lat_con.summary_line:
|
|
if lat_re.table_id not in one.keys():
|
|
one[lat_re.table_id] = {"C": lat_re.this_year or 0}
|
|
else:
|
|
if 'C' not in one[lat_re.table_id].keys():
|
|
one[lat_re.table_id]["C"] = lat_re.this_year or 0
|
|
else:
|
|
one[lat_re.table_id]["C"] += lat_re.this_year
|
|
|
|
# 同比增长D
|
|
for key, val in one.items():
|
|
if val['C'] > 0:
|
|
val['D'] = round((val['B'] - val['C']) / val['C'] * 100, 2)
|
|
else:
|
|
val['D'] = 0
|
|
return one
|
|
|
|
|
|
class InformationSummaryLine(models.Model):
|
|
_name = 'information.summary.line'
|
|
_description = '信息汇总表明细'
|
|
|
|
summary_id = fields.Many2one('information.summary.table', string='关联汇总表')
|
|
table_id = fields.Many2one('summary.table.setting', string='项目名称')
|
|
this_year = fields.Float(string='本年底')
|
|
remake = fields.Text(string='说明')
|
|
|