GoodERP
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

602 行
24KB

  1. from odoo.tests.common import TransactionCase
  2. from odoo.exceptions import UserError, ValidationError
  3. from datetime import datetime
  4. from odoo import fields, models, api
  5. from odoo.tools import float_compare, float_is_zero
  6. import calendar
  7. class TestVoucher(TransactionCase):
  8. def test_approve(self):
  9. '''测试审核反审核报错'''
  10. voucher = self.env.ref('finance.voucher_1')
  11. # 正常审批
  12. voucher.voucher_done()
  13. self.assertTrue(voucher.state == 'done')
  14. # 已审批的凭证不可以删除
  15. with self.assertRaises(UserError):
  16. voucher.unlink()
  17. for line in voucher.line_ids:
  18. with self.assertRaises(UserError):
  19. line.unlink()
  20. # 重复审批
  21. with self.assertRaises(UserError):
  22. voucher.voucher_done()
  23. # 正常反审批
  24. voucher.voucher_draft()
  25. self.assertTrue(voucher.state == 'draft')
  26. # 重复反审批
  27. with self.assertRaises(UserError):
  28. voucher.voucher_draft()
  29. # 会计期间已关闭时的审批
  30. voucher.period_id.is_closed = True
  31. with self.assertRaises(UserError):
  32. voucher.voucher_done()
  33. # 会计期间已关闭时的反审批
  34. voucher.period_id.is_closed = False
  35. voucher.voucher_done()
  36. voucher.period_id.is_closed = True
  37. with self.assertRaises(UserError):
  38. voucher.voucher_draft()
  39. def test_voucher_can_be_draft(self):
  40. '''测试反审核报错'''
  41. voucher = self.env.ref('finance.voucher_1')
  42. voucher.voucher_done()
  43. voucher.voucher_can_be_draft() #是否应该放在上面的case里
  44. def test_voucher_done_costs_types_out(self):
  45. '''费用类科目只能在借方记账'''
  46. voucher = self.env['voucher'].create({
  47. 'date': '2017-01-01',
  48. 'line_ids': [(0, 0, {
  49. 'name': '收利息', # 贷方行
  50. 'account_id': self.env.ref(
  51. 'finance.small_business_chart5603002').id,
  52. 'debit': 0,
  53. 'credit': 1.0,
  54. }),
  55. (0, 0, {
  56. 'name': '收利息', # 借方行
  57. 'account_id': self.env.ref('finance.account_bank').id,
  58. 'debit': 1.0,
  59. 'credit': 0,
  60. })]
  61. })
  62. voucher.voucher_done()
  63. def test_voucher_done_costs_types_in(self):
  64. '''收入类科目只能在贷方记账'''
  65. voucher = self.env['voucher'].with_context(
  66. {'entry_manual': 1}).create({
  67. 'date': '2017-01-01',
  68. 'line_ids': [
  69. (0, 0, {
  70. 'name': '退款给客户', # 贷方行
  71. 'account_id': self.env.ref('finance.account_bank').id,
  72. 'debit': 0,
  73. 'credit': 50.0,
  74. }),
  75. (0, 0, {
  76. 'name': '退款给客户', # 借方行
  77. 'account_id': self.env.ref(
  78. 'finance.account_income').id,
  79. 'debit': 50.0,
  80. 'credit': 0,
  81. })]
  82. })
  83. voucher.voucher_done()
  84. def test_restricted_account(self):
  85. ''' 测试受限的记账科目 '''
  86. account_debit = self.env.ref('finance.small_business_chart1801')
  87. account_debit.restricted_debit = True
  88. self.env.ref('finance.account_chart1002').restricted_credit = True
  89. account_credit = self.env.ref('finance.account_bank')
  90. with self.assertRaises(UserError):
  91. voucher = self.env['voucher'].with_context(
  92. {'entry_manual': 1}).create({
  93. 'date': '2017-01-01',
  94. 'line_ids': [(0, 0, {
  95. 'name': '受限科目记账', # 贷方行
  96. 'account_id': account_credit.id,
  97. 'debit': 0,
  98. 'credit': 50.0,
  99. })]
  100. })
  101. with self.assertRaises(UserError):
  102. voucher = self.env['voucher'].with_context(
  103. {'entry_manual': 1}).create({
  104. 'date': '2017-01-01',
  105. 'line_ids': [(0, 0, {
  106. 'name': '受限科目记账', # 借方行
  107. 'account_id': account_debit.id,
  108. 'debit': 50.0,
  109. 'credit': 0,
  110. })]
  111. })
  112. self.env.ref('finance.voucher_1').with_context(
  113. {'entry_manual': 1}).write({'att_count': '15'})
  114. def test_line_unlink(self):
  115. '''测试可正常删除未审核的凭证行'''
  116. voucher = self.env.ref('finance.voucher_1')
  117. for line in voucher.line_ids:
  118. line.unlink()
  119. def test_vourcher_write(self):
  120. ''' 没注释?'''
  121. period_id = self.env.ref('finance.period_201601')
  122. voucher = self.env.ref('finance.voucher_1')
  123. voucher.state = 'done'
  124. voucher.period_id.is_closed = True
  125. with self.assertRaises(UserError):
  126. voucher.write({'period_id': period_id.id})
  127. self.env.context = dict(
  128. self.env.context, **{"call_module": 'checkout_wizard'})
  129. voucher.write({'period_id': period_id.id})
  130. def test_compute(self):
  131. '''新建凭证时计算字段加载'''
  132. voucher = self.env.ref('finance.voucher_1')
  133. self.assertTrue(voucher.period_id.name == '201601')
  134. self.assertTrue(voucher.amount_text == 50000.0)
  135. voucher.unlink()
  136. def test_check_balance(self):
  137. '''检查凭证借贷方合计平衡'''
  138. un_balance_voucher = self.env['voucher'].create({
  139. 'line_ids': [(0, 0, {
  140. 'account_id': self.env.ref('finance.account_cash').id,
  141. 'name': '借贷方不平',
  142. 'debit': 100,
  143. })]
  144. })
  145. with self.assertRaises(ValidationError):
  146. un_balance_voucher.voucher_done()
  147. def test_check_line(self):
  148. '''检查凭证行'''
  149. # 没有凭证行
  150. voucher_no_line = self.env['voucher'].create({
  151. 'line_ids': False
  152. })
  153. with self.assertRaises(ValidationError):
  154. voucher_no_line.voucher_done()
  155. # 检查借贷方都为0
  156. voucher_zero = self.env['voucher'].create({
  157. 'line_ids': [(0, 0, {
  158. 'account_id': self.env.ref('finance.account_cash').id,
  159. 'name': '借贷方全为0',
  160. })]
  161. })
  162. with self.assertRaises(ValidationError):
  163. voucher_zero.voucher_done()
  164. # 检查单行凭证行是否同时输入借和贷
  165. voucher = self.env['voucher'].create({
  166. 'line_ids': [(0, 0, {
  167. 'account_id': self.env.ref('finance.account_cash').id,
  168. 'name': '借贷方同时输入',
  169. 'debit': 100,
  170. 'credit': 100,
  171. })]
  172. })
  173. with self.assertRaises(ValidationError):
  174. voucher.voucher_done()
  175. def test_voucher_line_default_get(self):
  176. line = self.env['voucher.line'].create({
  177. 'account_id': self.env.ref('finance.account_cash').id,
  178. 'name': '借贷方同时输入',
  179. 'debit': 100,
  180. 'credit': 100,
  181. })
  182. self.env['voucher'].with_context({'line_ids': {line.id}}).create({
  183. 'line_ids': [(0, 0, {
  184. 'account_id': self.env.ref('finance.account_cash').id,
  185. 'name': '借贷方同时输入',
  186. 'debit': 100,
  187. 'credit': 100,
  188. })]
  189. })
  190. def test_default_voucher_date(self):
  191. voucher_obj = self.env['voucher']
  192. '''
  193. voucher_rows = self.env['voucher'].search([])
  194. voucher_rows.unlink()
  195. setting_row = self.env['finance.config.settings'].create({
  196. "defaul_period_domain": "can",
  197. "defaul_reset_init_number": 1,
  198. "defaul_auto_reset": True,
  199. "defaul_reset_period": "month",
  200. "defaul_voucher_date": "today", })
  201. setting_row.execute()
  202. setting_row.default_voucher_date = 'last'
  203. voucher_obj._default_voucher_date()
  204. '''
  205. voucher_obj.create({})
  206. def test_default_voucher_date_last(self):
  207. ''' 测试 defaul_voucher_date 等于 last '''
  208. voucher_obj = self.env['voucher']
  209. '''
  210. setting_row = self.env['finance.config.settings'].create({
  211. "defaul_period_domain": "can",
  212. "defaul_reset_init_number": 1,
  213. "defaul_auto_reset": True,
  214. "defaul_reset_period": "month",
  215. "defaul_voucher_date": "last", })
  216. setting_row.execute()
  217. '''
  218. self.env['ir.default'].set(
  219. 'finance.config.settings', 'defaul_voucher_date', 'last')
  220. # voucher_obj._default_voucher_date()
  221. voucher_obj.create({})
  222. class TestVoucherLine(TransactionCase):
  223. def test_view_document(self):
  224. ''' Test: view document method '''
  225. self.env.ref('finance.voucher_line_1_debit').view_document()
  226. def test_post_view(self):
  227. ''' 只能往下级科目记账 '''
  228. with self.assertRaises(UserError):
  229. self.env.ref('finance.voucher_line_1_debit'
  230. ).account_id = self.env.ref(
  231. 'finance.account_chart1002')
  232. class TestPeriod(TransactionCase):
  233. def test_get_period(self):
  234. period_obj = self.env['finance.period']
  235. # 如果对应会计期间不存在,则创建
  236. if not period_obj.search([('year', '=', '2100'),
  237. ('month', '=', '6')]):
  238. that_date = datetime.strptime('2100-06-20', "%Y-%m-%d")
  239. self.assertTrue(period_obj.get_period(that_date).name, '210006')
  240. period_obj.get_year_fist_period_id()
  241. period_row = period_obj.search(
  242. [('year', '=', '2016'), ('month', '=', '10')])
  243. if not period_row:
  244. period_row = period_obj.create({'year': '2016', 'month': '10'})
  245. self.assertTrue(("2016-10-01", "2016-10-31") ==
  246. period_obj.get_period_month_date_range(period_row))
  247. period_row.is_closed = True
  248. with self.assertRaises(UserError): # 得到对应时间的会计期间 ,期间已关闭
  249. that_date = datetime.strptime('2016-10-10', "%Y-%m-%d")
  250. period_obj.get_period(that_date)
  251. # datetime_str = datetime.now().strftime("%Y-%m-%d")
  252. # datetime_str_list = datetime_str.split('-')
  253. # period_row = self.env['finance.period'].search(
  254. # [('year', '=', datetime_str_list[0])])
  255. # if period_row: # 在相应的年份 会计期间不存在
  256. # period_row.unlink()
  257. # period_obj.get_year_fist_period_id()
  258. def test_compute_name_month_02(self):
  259. ''' 测试 compute_name 月份小于10 '''
  260. self.env['finance.period'].create({'year': '2007', 'month': '2'})
  261. def test_onchange_account_id(self):
  262. '''凭证行的科目变更影响到其他字段的可选值'''
  263. voucher = self.env.ref('finance.voucher_1')
  264. for line in voucher.line_ids:
  265. line.account_id = self.env.ref('finance.account_cash').id
  266. line.onchange_account_id()
  267. line.account_id = self.env.ref('finance.account_goods').id
  268. line.onchange_account_id()
  269. line.account_id = self.env.ref('finance.account_ar').id
  270. line.onchange_account_id()
  271. line.account_id = self.env.ref('finance.account_ap').id
  272. line.onchange_account_id()
  273. line.account_id = self.env.ref(
  274. 'finance.small_business_chart2211004').id
  275. line.onchange_account_id()
  276. line.account_id = self.env.ref('finance.account_goods').id
  277. line.account_id.auxiliary_financing = 'goods'
  278. line.onchange_account_id()
  279. line.account_id.auxiliary_financing = 'customer'
  280. line.onchange_account_id()
  281. line.account_id.auxiliary_financing = 'supplier'
  282. line.onchange_account_id()
  283. line.account_id.auxiliary_financing = 'project'
  284. line.onchange_account_id()
  285. def test_period_compare(self):
  286. """测试会计期间比较的 代码 有三种情况 大于 小于 等于"""
  287. period_id = self.env.ref('finance.period_201601')
  288. last_period_id = self.env.ref('finance.period_201512')
  289. self.env['finance.period'].period_compare(period_id, period_id)
  290. self.env['finance.period'].period_compare(last_period_id, period_id)
  291. class TestFinanceAccount(TransactionCase):
  292. def setUp(self):
  293. super(TestFinanceAccount, self).setUp()
  294. self.cash = self.env.ref('finance.account_cash')
  295. def test_name_get(self):
  296. name = self.cash.name_get()
  297. real_name = '%s %s' % (self.cash.code, self.cash.name)
  298. self.assertTrue(name[0][1] == real_name)
  299. # def test_name_get_in_voucher(self):
  300. # """仅在凭证界面选择科目时显示出余额"""
  301. # voucher = self.env.ref('finance.voucher_1')
  302. # self.env['voucher.line'].create({
  303. # 'voucher_id': voucher.id,
  304. # 'name': u'测试科目显示出余额',
  305. # 'account_id': self.cash.with_context(
  306. # {'show_balance': True}).id, # 给该字段传context没有成功
  307. # })
  308. # name = self.cash.name_get()
  309. # real_name = '%s %s %s' % (
  310. # self.cash.code, self.cash.name, self.cash.balance)
  311. # self.assertTrue(name[0][1] == real_name)
  312. def test_name_search(self):
  313. '''会计科目按名字和编号搜索'''
  314. result = self.env['finance.account'].name_search('库存现金')
  315. real_result = [(self.cash.id,
  316. self.cash.code + ' ' + self.cash.name)]
  317. self.assertEqual(result, real_result)
  318. # 搜索输入的是编号 code 1001
  319. result1 = self.env['finance.account'].name_search('1001')
  320. real_result1 = [(self.cash.id, self.cash.code + ' ' + self.cash.name)]
  321. self.assertEqual(result1, real_result1)
  322. def test_get_smallest_code_account(self):
  323. account = self.env['finance.account']
  324. account.get_smallest_code_account()
  325. def test_get_max_code_account(self):
  326. account = self.env['finance.account']
  327. account.get_max_code_account()
  328. def test_compute_balance(self):
  329. """计算会计科目的当前余额"""
  330. self.cash.compute_balance()
  331. self.cash.get_balance()
  332. self.assertEqual(self.cash.balance, 0)
  333. def test_unlink(self):
  334. ''' 测试删除科目 '''
  335. # 界面上删除预设科目报错
  336. with self.assertRaises(UserError):
  337. self.cash.with_context({'modify_from_webclient': 1}).unlink()
  338. # 记过账的不能删除
  339. with self.assertRaises(UserError):
  340. self.env.ref('finance.account_bank').unlink()
  341. # 有下级科目的不能删除
  342. with self.assertRaises(UserError):
  343. self.env.ref('finance.account_chart1002').unlink()
  344. # 下级科目删光了就把上级科目改为可记账的科目
  345. self.env.ref('finance.small_business_chart2221001').child_ids.unlink()
  346. def test_write(self):
  347. ''' 测试删除科目 '''
  348. # 界面上修改预设科目报错
  349. with self.assertRaises(UserError):
  350. self.cash.source = 'init'
  351. self.cash.with_context(
  352. {'modify_from_webclient': 1}).write({'source': 'init'})
  353. # 界面上记过账科目不能修改
  354. account_bank = self.env.ref('finance.account_bank')
  355. with self.assertRaises(UserError):
  356. account_bank.source = ''
  357. account_bank.with_context(
  358. {'modify_from_webclient': 1}).write({'source': 'init'})
  359. def test_add_child(self):
  360. ''' 测试增加子科目 '''
  361. # 末级科目增加子科目
  362. self.cash.button_add_child()
  363. income = self.env.ref('finance.account_income')
  364. wizard = self.env['wizard.account.add.child'].with_context(
  365. {'active_id': income.id}
  366. ).create({
  367. 'account_code': '01',
  368. 'account_name': '纸币',
  369. })
  370. wizard.create_account()
  371. # 非末级科目增加子科目
  372. wizard = self.env['wizard.account.add.child'].with_context(
  373. {'active_id': self.env.ref(
  374. 'finance.small_business_chart1701').id}).create({
  375. 'account_code': '08',
  376. 'account_name': '',
  377. })
  378. wizard.create_account()
  379. # 修改下级编码
  380. wizard.account_code = '02'
  381. wizard._onchange_account_code()
  382. # 必须是数字
  383. wizard.account_code = '02a'
  384. wizard._onchange_account_code()
  385. # 必须是2位
  386. wizard.account_code = '021'
  387. wizard._onchange_account_code()
  388. # 选择的科目层级已经是最低层级科目了,不能建立在它下面建立下级科目! 报错
  389. self.env['ir.default'].set(
  390. 'finance.config.settings', 'defaul_account_hierarchy_level', '2')
  391. business_chart170108 = self.env['finance.account'].search(
  392. [('code', '=', '170108')])
  393. with self.assertRaises(UserError):
  394. wizard = self.env['wizard.account.add.child'].with_context(
  395. {'active_id': business_chart170108.id}). \
  396. create({
  397. 'account_code': '01',
  398. 'account_name': 'test'
  399. })
  400. wizard.create_account()
  401. def test_add_child_multi(self):
  402. with self.assertRaises(UserError):
  403. wizard = self.env['wizard.account.add.child'].with_context(
  404. {'active_ids': [
  405. self.cash.id,
  406. self.env.ref('finance.small_business_chart1701').id]}
  407. ).create({
  408. 'account_code': '01',
  409. 'account_name': '纸币',
  410. })
  411. class TestVoucherTemplateWizard(TransactionCase):
  412. def setUp(self):
  413. super(TestVoucherTemplateWizard, self).setUp()
  414. self.voucher = self.env.ref('finance.voucher_1')
  415. self.voucher_template_wizard = self.env[
  416. 'voucher.template.wizard'].create({
  417. 'name': '测试模板', 'voucher_id': self.voucher.id,
  418. })
  419. def test_save_as_template(self):
  420. """凭证模板相关功能"""
  421. self.voucher_template_wizard.save_as_template()
  422. self.voucher_template_wizard.is_change_old_template = True
  423. old_template_id = self.env['voucher.template'].search(
  424. [])[0].id if self.env['voucher.template'].search([]) else False
  425. self.voucher_template_wizard.old_template_id = old_template_id
  426. self.voucher_template_wizard.save_as_template()
  427. def test_onchange_template_id(self):
  428. """凭证上模板字段的onchange"""
  429. old_template_id = self.env['voucher.template'].search(
  430. [])[0].id if self.env[
  431. 'voucher.template'].search([]) else False
  432. self.voucher.template_id = old_template_id
  433. self.voucher.onchange_template_id()
  434. class TestVoucherTemplateLine(TransactionCase):
  435. def setUp(self):
  436. super(TestVoucherTemplateLine, self).setUp()
  437. self.voucher = self.env.ref('finance.voucher_1')
  438. self.voucher_template_wizard = self.env[
  439. 'voucher.template.wizard'].create({
  440. 'name': '测试模板', 'voucher_id': self.voucher.id,
  441. })
  442. def test_onchange_account_id(self):
  443. """ 凭证模板行上会计科目字段的onchange测试 """
  444. self.voucher_template_wizard.save_as_template()
  445. template = self.env['voucher.template'].search([])[0] if self.env[
  446. 'voucher.template'].search([]) else False
  447. for line in template.line_ids:
  448. # 会计科目不存在
  449. line.account_id = False
  450. line.onchange_account_id()
  451. line.account_id = self.env.ref(
  452. 'finance.small_business_chart1403').id
  453. line.onchange_account_id()
  454. line.account_id.auxiliary_financing = 'goods'
  455. line.onchange_account_id()
  456. line.account_id.auxiliary_financing = 'customer'
  457. line.onchange_account_id()
  458. line.account_id.auxiliary_financing = 'supplier'
  459. line.onchange_account_id()
  460. line.account_id.auxiliary_financing = 'project'
  461. line.onchange_account_id()
  462. break
  463. class TestCheckoutWizard(TransactionCase):
  464. def setUp(self):
  465. super(TestCheckoutWizard, self).setUp()
  466. def test_recreate_voucher_name(self):
  467. ''' 测试 按用户设置重排结账会计期间凭证号(会计要求凭证号必须连续) '''
  468. checkout_wizard_obj = self.env['checkout.wizard']
  469. period_id = self.env.ref('finance.period_201601')
  470. last_period_id = self.env.ref('finance.period_201512')
  471. # 按月 重排结账会计期间凭证号
  472. setting_row_month = self.env['finance.config.settings'].create(
  473. {"defaul_period_domain": "can",
  474. "defaul_reset_init_number": 1,
  475. "defaul_auto_reset": True,
  476. "defaul_voucher_date": "today"})
  477. setting_row_month.execute()
  478. checkout_wizard_obj.recreate_voucher_name(last_period_id)
  479. # 按年 重排结账会计期间凭证号
  480. setting_row_year = self.env['finance.config.settings'].create({
  481. "defaul_period_domain": "can",
  482. "defaul_reset_init_number": 1,
  483. "defaul_auto_reset": True,
  484. "defaul_reset_period": "year",
  485. "defaul_voucher_date": "today"})
  486. setting_row_year.execute()
  487. # 按年重置 上一个期间存在 但未结账
  488. with self.assertRaises(UserError):
  489. checkout_wizard_obj.recreate_voucher_name(period_id)
  490. # 按年重置 上一个期间存在 已结账 两个期间的年相同
  491. last_period_id.is_closed = True
  492. checkout_wizard_obj.recreate_voucher_name(period_id)
  493. # 按年重置 上一个期间存在 已结账 两个期间的年不相同
  494. period_id = self.env.ref('finance.period_201603')
  495. last_period_id = self.env.ref('finance.period_201602')
  496. self.env.ref('finance.period_201601').is_closed = True
  497. self.env.ref('finance.period_201512').is_closed = True
  498. last_period_id.is_closed = True
  499. checkout_wizard_obj.recreate_voucher_name(period_id)
  500. # 按年重置 上一个期间不存在
  501. period_id_init = self.env['finance.period'].search(
  502. [], order='year, month', limit=1)
  503. period_id_init.is_closed = True
  504. period_id = self.env.ref('finance.period_201411')
  505. checkout_wizard_obj.recreate_voucher_name(period_id)
  506. def test_recreate_voucher_name_unEqual_nextVoucherName(self):
  507. ''' 测试 按月重排结账会计期间凭证号 凭证号不连续,更新凭证号 '''
  508. checkout_wizard_obj = self.env['checkout.wizard']
  509. period_id = self.env.ref('finance.period_201601')
  510. # 按月 重排结账会计期间凭证号
  511. setting_row_month = self.env['finance.config.settings'].create({
  512. "defaul_period_domain": "can",
  513. "defaul_reset_init_number": 1,
  514. "defaul_auto_reset": True,
  515. "defaul_voucher_date": "today"})
  516. setting_row_month.execute()
  517. self.env.ref('finance.period_201512').is_closed = True
  518. checkout_wizard_obj.recreate_voucher_name(period_id)
  519. def test_get_last_date(self):
  520. ''' Test: _get_last_date '''
  521. datetime_str_list = datetime.now().strftime("%Y-%m-%d").split('-')
  522. day = calendar.monthrange(
  523. int(datetime_str_list[0]), int(datetime_str_list[1]))
  524. c_w = self.env['checkout.wizard'].create({})
  525. self.assertEqual(c_w.date.strftime("%Y-%m-%d"),
  526. '-'.join(
  527. [datetime_str_list[0], datetime_str_list[1], str(day[1])]))
  528. class TestMonthProductCost(TransactionCase):
  529. def setUp(self):
  530. super(TestMonthProductCost, self).setUp()
  531. self.period_id = self.env.ref('finance.period_201601')
  532. def test_generate_issue_cost(self):
  533. """本月成本结算 相关逻辑的测试"""
  534. checkout_wizard_row = self.env['checkout.wizard'].create(
  535. {'date': '2016-01-31', 'period_id': self.period_id.id})
  536. with self.assertRaises(UserError):
  537. checkout_wizard_row.button_checkout()
上海开阖软件有限公司 沪ICP备12045867号-1