GoodERP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 satır
12KB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import _
  5. from odoo import models, fields, api
  6. import calendar
  7. class CreateBalanceSheetWizard(models.TransientModel):
  8. """创建资产负债 和利润表的 wizard"""
  9. _name = "create.balance.sheet.wizard"
  10. _description = '资产负债表和利润表的向导'
  11. company_id = fields.Many2one(
  12. 'res.company',
  13. string='公司',
  14. change_default=True,
  15. default=lambda self: self.env.company)
  16. @api.model
  17. def _default_period_domain(self):
  18. """
  19. 用来设定期间的 可选的范围(这个是一个范围)
  20. :return: domain条件
  21. """
  22. period_domain_setting = self.env['ir.default']._get(
  23. 'finance.config.settings', 'defaul_period_domain')
  24. return [('is_closed', '!=', False)] \
  25. if period_domain_setting == 'cannot' else []
  26. @api.model
  27. def _default_period_id(self):
  28. return self._default_period_id_impl()
  29. def _default_period_id_impl(self):
  30. """
  31. 默认是当前会计期间
  32. :return: 当前会计期间的对象
  33. """
  34. return self.env['finance.period'].get_date_now_period_id()
  35. period_id = fields.Many2one(
  36. 'finance.period',
  37. string='会计期间',
  38. domain=_default_period_domain,
  39. default=_default_period_id, help='用来设定报表的期间')
  40. def compute_balance(self, parameter_str, period_id, compute_field_list):
  41. """根据所填写的 科目的code 和计算的字段 进行计算对应的资产值"""
  42. if parameter_str:
  43. parameter_str_list = parameter_str.split('~')
  44. subject_vals = []
  45. if len(parameter_str_list) == 1:
  46. subject_ids = self.env['finance.account'].search(
  47. [('code', '=', parameter_str_list[0]),
  48. ('account_type', '!=', 'view')])
  49. else:
  50. subject_ids = self.env['finance.account'].search(
  51. [('code', '>=', parameter_str_list[0]),
  52. ('code', '<=', parameter_str_list[1]),
  53. ('account_type', '!=', 'view')])
  54. trial_balances = self.env['trial.balance'].search(
  55. [('subject_name_id', 'in', [
  56. subject.id for subject in subject_ids]),
  57. ('period_id', '=', period_id.id)])
  58. for trial_balance in trial_balances:
  59. # 根据参数code 对应的科目的 方向 进行不同的操作
  60. # 解决:累计折旧 余额记贷方
  61. if trial_balance.subject_name_id.costs_types == 'assets' \
  62. or trial_balance.subject_name_id.costs_types == 'cost':
  63. subject_vals.append(
  64. trial_balance[compute_field_list[0]]
  65. - trial_balance[compute_field_list[1]])
  66. elif trial_balance.subject_name_id.costs_types == 'debt' or \
  67. trial_balance.subject_name_id.costs_types == 'equity':
  68. subject_vals.append(
  69. trial_balance[compute_field_list[1]]
  70. - trial_balance[compute_field_list[0]])
  71. return sum(subject_vals)
  72. def deal_with_balance_formula(self, balance_formula,
  73. period_id, year_begain_field):
  74. if balance_formula:
  75. return_vals = sum([self.compute_balance(
  76. one_formula, period_id, year_begain_field)
  77. for one_formula in balance_formula.split(';')])
  78. else:
  79. return_vals = 0
  80. return return_vals
  81. def balance_sheet_create(self, balance_sheet_obj,
  82. year_begain_field, current_period_field):
  83. balance_sheet_obj.write(
  84. {'beginning_balance': self.deal_with_balance_formula(
  85. balance_sheet_obj.balance_formula,
  86. self.period_id,
  87. year_begain_field),
  88. 'ending_balance': self.deal_with_balance_formula(
  89. balance_sheet_obj.balance_formula,
  90. self.period_id,
  91. current_period_field),
  92. 'beginning_balance_two': self.deal_with_balance_formula(
  93. balance_sheet_obj.balance_two_formula,
  94. self.period_id, year_begain_field),
  95. 'ending_balance_two': self.deal_with_balance_formula(
  96. balance_sheet_obj.balance_two_formula,
  97. self.period_id, current_period_field)})
  98. def create_balance_sheet(self):
  99. """ 资产负债表的创建 """
  100. balance_wizard = self.env['create.trial.balance.wizard'].create(
  101. {'period_id': self.period_id.id})
  102. balance_wizard.create_trial_balance()
  103. view_id = self.env.ref('finance.balance_sheet_list_wizard').id
  104. balance_sheet_objs = self.env['balance.sheet'].search([])
  105. year_begain_field = ['year_init_debit', 'year_init_credit']
  106. current_period_field = [
  107. 'ending_balance_debit', 'ending_balance_credit']
  108. for balance_sheet_obj in balance_sheet_objs:
  109. self.balance_sheet_create(
  110. balance_sheet_obj, year_begain_field, current_period_field)
  111. force_company = self._context.get('force_company')
  112. if not force_company:
  113. force_company = self.env.user.company_id.id
  114. company_row = self.env['res.company'].browse(force_company)
  115. days = calendar.monthrange(
  116. int(self.period_id.year), int(self.period_id.month))[1]
  117. # TODO 格子不对
  118. attachment_information = _('编制单位:') + company_row.name + ',' \
  119. + self.period_id.year \
  120. + _('年') + self.period_id.month + _('月') + \
  121. str(days) + _('日') + ',' + _('单位:元')
  122. domain = [
  123. ('id', 'in', [balance_sheet_obj.id
  124. for balance_sheet_obj in balance_sheet_objs])]
  125. return { # 返回生成资产负债表的数据的列表
  126. 'type': 'ir.actions.act_window',
  127. 'name': _('资产负债表:') + self.period_id.name,
  128. 'view_mode': 'list',
  129. 'res_model': 'balance.sheet',
  130. 'target': 'main',
  131. 'view_id': False,
  132. 'views': [(view_id, 'list')],
  133. 'context': {'period_id': self.period_id.id,
  134. 'attachment_information': attachment_information},
  135. 'domain': domain,
  136. 'limit': 65535,
  137. }
  138. def deal_with_profit_formula(self, occurrence_balance_formula,
  139. period_id, year_begain_field):
  140. if occurrence_balance_formula:
  141. return_vals = sum(
  142. [self.compute_profit(
  143. balance_formula, period_id, year_begain_field)
  144. for balance_formula in
  145. occurrence_balance_formula.split(";")
  146. ])
  147. else:
  148. return_vals = 0
  149. return return_vals
  150. def create_profit_statement(self):
  151. """生成利润表"""
  152. balance_wizard = self.env['create.trial.balance.wizard'].create(
  153. {'period_id': self.period_id.id})
  154. balance_wizard.create_trial_balance()
  155. view_id = self.env.ref('finance.profit_statement_list').id
  156. balance_sheet_objs = self.env['profit.statement'].search([])
  157. year_begain_field = ['cumulative_occurrence_debit',
  158. 'cumulative_occurrence_credit']
  159. current_period_field = [
  160. 'current_occurrence_debit', 'current_occurrence_credit']
  161. for balance_sheet_obj in balance_sheet_objs:
  162. balance_sheet_obj.write(
  163. {'cumulative_occurrence_balance':
  164. self.deal_with_profit_formula(
  165. balance_sheet_obj.occurrence_balance_formula,
  166. self.period_id, year_begain_field),
  167. 'current_occurrence_balance': self.compute_profit(
  168. balance_sheet_obj.occurrence_balance_formula,
  169. self.period_id,
  170. current_period_field)})
  171. force_company = self._context.get('force_company')
  172. if not force_company:
  173. force_company = self.env.user.company_id.id
  174. company_row = self.env['res.company'].browse(force_company)
  175. days = calendar.monthrange(
  176. int(self.period_id.year), int(self.period_id.month))[1]
  177. attachment_information = _('编制单位:') + company_row.name + ',' \
  178. + self.period_id.year \
  179. + _('年') + self.period_id.month + _('月') \
  180. + ',' + _('单位:元')
  181. domain = [
  182. ('id', 'in', [
  183. balance_sheet_obj.id
  184. for balance_sheet_obj in balance_sheet_objs])]
  185. return { # 返回生成利润表的数据的列表
  186. 'type': 'ir.actions.act_window',
  187. 'name': _('利润表:') + self.period_id.name,
  188. 'view_mode': 'list',
  189. 'res_model': 'profit.statement',
  190. 'target': 'main',
  191. 'view_id': False,
  192. 'views': [(view_id, 'list')],
  193. 'context': {'period_id': self.period_id.id,
  194. 'attachment_information': attachment_information},
  195. 'domain': domain,
  196. 'limit': 65535,
  197. }
  198. def compute_profit(self, parameter_str, period_id, compute_field_list):
  199. """ 根据传进来的 的科目的code 进行利润表的计算 """
  200. if parameter_str:
  201. parameter_str_list = parameter_str.split('~')
  202. subject_vals_in = []
  203. subject_vals_out = []
  204. total_sum = 0
  205. sign_in = False
  206. sign_out = False
  207. if len(parameter_str_list) == 1:
  208. subject_ids = self.env['finance.account'].search(
  209. [('code', '=', parameter_str_list[0]),
  210. ('account_type', '!=', 'view')])
  211. else:
  212. subject_ids = self.env['finance.account'].search(
  213. [('code', '>=', parameter_str_list[0]),
  214. ('code', '<=', parameter_str_list[1]),
  215. ('account_type', '!=', 'view')])
  216. if subject_ids: # 本行计算科目借贷方向
  217. for line in subject_ids:
  218. if line.balance_directions == 'in':
  219. sign_in = True
  220. if line.balance_directions == 'out':
  221. sign_out = True
  222. trial_balances = self.env['trial.balance'].search([
  223. ('subject_name_id', 'in', [
  224. subject.id for subject in subject_ids]),
  225. ('period_id', '=', period_id.id)])
  226. for trial_balance in trial_balances:
  227. if trial_balance.subject_name_id.balance_directions == 'in':
  228. subject_vals_in.append(
  229. trial_balance[compute_field_list[0]])
  230. elif trial_balance.subject_name_id.balance_directions == 'out':
  231. subject_vals_out.append(
  232. trial_balance[compute_field_list[1]])
  233. if sign_out and sign_in: # 方向有借且有贷
  234. total_sum = sum(subject_vals_out) - sum(subject_vals_in)
  235. else:
  236. if subject_vals_in:
  237. total_sum = sum(subject_vals_in)
  238. else:
  239. total_sum = sum(subject_vals_out)
  240. return total_sum
上海开阖软件有限公司 沪ICP备12045867号-1