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.

298 satır
13KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo.tests.common import TransactionCase
  4. from odoo.exceptions import UserError
  5. class TestBuyAdjust(TransactionCase):
  6. def setUp(self):
  7. '''采购变更单准备基本数据'''
  8. super(TestBuyAdjust, self).setUp()
  9. self.order = self.env.ref('buy.buy_order_1')
  10. for line in self.order.line_ids:
  11. line.tax_rate = 0
  12. self.order.buy_order_done()
  13. self.keyboard = self.env.ref('goods.keyboard')
  14. self.keyboard_black = self.env.ref('goods.keyboard_black')
  15. self.mouse = self.env.ref('goods.mouse')
  16. self.cable = self.env.ref('goods.cable')
  17. def test_unlink(self):
  18. '''测试删除已审核的采购变更单'''
  19. adjust = self.env['buy.adjust'].create({
  20. 'order_id': self.order.id,
  21. 'line_ids': [(0, 0, {'goods_id': self.keyboard.id,
  22. 'attribute_id': self.keyboard_black.id,
  23. 'quantity': 3.0,
  24. }),
  25. ]
  26. })
  27. adjust.buy_adjust_done()
  28. with self.assertRaises(UserError):
  29. adjust.unlink()
  30. # 删除草稿状态的采购变更单
  31. new = adjust.copy()
  32. new.unlink()
  33. def test_buy_adjust_done(self):
  34. '''审核采购变更单:正常情况'''
  35. # 正常情况下审核,新增商品鼠标(每批次为1的)、网线(无批次的)
  36. adjust = self.env['buy.adjust'].create({
  37. 'order_id': self.order.id,
  38. 'line_ids': [(0, 0, {'goods_id': self.keyboard.id,
  39. 'attribute_id': self.keyboard_black.id,
  40. 'quantity': 3.0,
  41. }),
  42. (0, 0, {'goods_id': self.mouse.id,
  43. 'quantity': 1,
  44. }),
  45. (0, 0, {'goods_id': self.cable.id,
  46. 'quantity': 1,
  47. })
  48. ]
  49. })
  50. adjust.buy_adjust_done()
  51. # 重复审核时报错
  52. with self.assertRaises(UserError):
  53. adjust.buy_adjust_done()
  54. def test_buy_adjust_done_no_line(self):
  55. '''审核采购变更单:没输入明细行,审核时报错'''
  56. adjust_no_line = self.env['buy.adjust'].create({
  57. 'order_id': self.order.id,
  58. })
  59. with self.assertRaises(UserError):
  60. adjust_no_line.buy_adjust_done()
  61. def test_buy_adjust_done_price_negative(self):
  62. '''审核采购变更单:商品价格为负,审核时报错'''
  63. adjust = self.env['buy.adjust'].create({
  64. 'order_id': self.order.id,
  65. 'line_ids': [(0, 0, {'goods_id': self.keyboard.id,
  66. 'attribute_id': self.keyboard_black.id,
  67. 'quantity': 3,
  68. 'price_taxed': -1,
  69. })]
  70. })
  71. with self.assertRaises(UserError):
  72. adjust.buy_adjust_done()
  73. def test_buy_adjust_done_quantity_lt(self):
  74. '''审核采购变更单:调整后数量 5 < 原订单已入库数量 6,审核时报错'''
  75. self.env.ref('core.goods_category_1').account_id = self.env.ref(
  76. 'finance.account_goods').id
  77. buy_receipt = self.env['buy.receipt'].search(
  78. [('order_id', '=', self.order.id)])
  79. for line in buy_receipt.line_in_ids:
  80. line.goods_qty = 6
  81. buy_receipt.buy_receipt_done()
  82. adjust = self.env['buy.adjust'].create({
  83. 'order_id': self.order.id,
  84. 'line_ids': [(0, 0, {'goods_id': self.keyboard.id,
  85. 'attribute_id': self.keyboard_black.id,
  86. 'quantity': -5,
  87. })]
  88. })
  89. with self.assertRaises(UserError):
  90. adjust.buy_adjust_done()
  91. def test_buy_adjust_done_quantity_equal(self):
  92. '''审核采购变更单:调整后数量6 == 原订单已入库数量 6,审核后将产生的入库单分单删除'''
  93. self.env.ref('core.goods_category_1').account_id = self.env.ref(
  94. 'finance.account_goods').id
  95. buy_receipt = self.env['buy.receipt'].search(
  96. [('order_id', '=', self.order.id)])
  97. for line in buy_receipt.line_in_ids:
  98. line.goods_qty = 6
  99. buy_receipt.buy_receipt_done()
  100. adjust = self.env['buy.adjust'].create({
  101. 'order_id': self.order.id,
  102. 'line_ids': [(0, 0, {'goods_id': self.keyboard.id,
  103. 'attribute_id': self.keyboard_black.id,
  104. 'quantity': -4,
  105. })]
  106. })
  107. '''
  108. adjust.buy_adjust_done()
  109. new_receipt = self.env['buy.receipt'].search(
  110. [('order_id', '=', self.order.id),
  111. ('state', '=', 'draft')])
  112. self.assertTrue(not new_receipt)
  113. '''
  114. def test_buy_adjust_done_all_in(self):
  115. '''审核采购变更单:采购订单生成的采购入库单已全部入库,审核时报错'''
  116. self.env.ref('core.goods_category_1').account_id = self.env.ref(
  117. 'finance.account_goods').id
  118. receipt = self.env['buy.receipt'].search(
  119. [('order_id', '=', self.order.id)])
  120. receipt.buy_receipt_done()
  121. adjust = self.env['buy.adjust'].create({
  122. 'order_id': self.order.id,
  123. 'line_ids': [(0, 0, {'goods_id': self.keyboard.id,
  124. 'attribute_id': self.keyboard_black.id,
  125. 'quantity': 3.0,
  126. }),
  127. ]
  128. })
  129. with self.assertRaises(UserError):
  130. adjust.buy_adjust_done()
  131. def test_buy_adjust_done_more_same_line(self):
  132. '''审核采购变更单:查找到采购订单中多行同一商品,不能调整'''
  133. self.env.ref('core.goods_category_1').account_id = self.env.ref(
  134. 'finance.account_goods').id
  135. self.order.buy_order_draft()
  136. self.order.line_ids.create({'order_id': self.order.id,
  137. 'goods_id': self.keyboard.id,
  138. 'attribute_id': self.keyboard_black.id,
  139. 'quantity': 10,
  140. 'price_taxed': 10.0,
  141. 'tax_rate': 0, })
  142. self.order.buy_order_done()
  143. receipt = self.env['buy.receipt'].search(
  144. [('order_id', '=', self.order.id)])
  145. for line in receipt.line_in_ids:
  146. line.goods_qty = 1
  147. receipt.buy_receipt_done()
  148. adjust = self.env['buy.adjust'].create({
  149. 'order_id': self.order.id,
  150. 'line_ids': [(0, 0, {'goods_id': self.keyboard.id,
  151. 'attribute_id': self.keyboard_black.id,
  152. 'quantity': 3.0,
  153. }),
  154. ]
  155. })
  156. with self.assertRaises(UserError):
  157. adjust.buy_adjust_done()
  158. def test_buy_adjust_done_goods_done(self):
  159. '''审核采购变更单:原始单据中一行商品已全部入库,另一行没有'''
  160. self.env.ref('core.goods_category_1').account_id = self.env.ref(
  161. 'finance.account_goods').id
  162. self.order.buy_order_draft()
  163. self.order.line_ids.create({'order_id': self.order.id,
  164. 'goods_id': self.cable.id,
  165. 'price_taxed': 10.0,
  166. 'quantity': 10,
  167. 'tax_rate': 0})
  168. self.order.buy_order_done()
  169. receipt = self.env['buy.receipt'].search(
  170. [('order_id', '=', self.order.id)])
  171. for line in receipt.line_in_ids:
  172. if line.goods_id.id != self.cable.id:
  173. line.unlink()
  174. receipt.buy_receipt_done()
  175. adjust = self.env['buy.adjust'].create({
  176. 'order_id': self.order.id,
  177. 'line_ids': [(0, 0, {'goods_id': self.cable.id,
  178. 'quantity': 3.0,
  179. }),
  180. ]
  181. })
  182. with self.assertRaises(UserError):
  183. adjust.buy_adjust_done()
  184. def test_buy_adjust_done_no_attribute(self):
  185. '''检查属性是否填充'''
  186. adjust = self.env['buy.adjust'].create({
  187. 'order_id': self.order.id,
  188. 'line_ids': [(0, 0, {'goods_id': self.env.ref('goods.keyboard').id,
  189. 'quantity': 10,
  190. })]
  191. })
  192. with self.assertRaises(UserError):
  193. adjust.buy_adjust_done()
  194. class TestBuyAdjustLine(TransactionCase):
  195. def setUp(self):
  196. super(TestBuyAdjustLine, self).setUp()
  197. # 采购 10个键盘 单价 50
  198. self.order = self.env.ref('buy.buy_order_1')
  199. self.order.buy_order_done()
  200. self.keyboard = self.env.ref('goods.keyboard')
  201. self.cable = self.env.ref('goods.cable')
  202. self.adjust = self.env['buy.adjust'].create({
  203. 'order_id': self.order.id,
  204. 'line_ids': [(0, 0, {'goods_id': self.cable.id,
  205. 'quantity': 10,
  206. })]
  207. })
  208. def test_compute_using_attribute(self):
  209. '''返回订单行中商品是否使用属性'''
  210. for line in self.adjust.line_ids:
  211. self.assertTrue(not line.using_attribute)
  212. line.goods_id = self.keyboard
  213. self.assertTrue(line.using_attribute)
  214. def test_compute_all_amount(self):
  215. '''当订单行的数量、单价、折扣额、税率改变时,改变采购金额、税额、价税合计'''
  216. for line in self.adjust.line_ids:
  217. line.price_taxed = 11.7
  218. self.assertTrue(line.amount == 100)
  219. self.assertTrue(line.tax_amount == 17)
  220. self.assertTrue(line.subtotal == 117)
  221. def test_compute_all_amount_wrong_tax_rate(self):
  222. '''明细行上输入错误税率,应报错'''
  223. for line in self.adjust.line_ids:
  224. with self.assertRaises(UserError):
  225. line.tax_rate = -1
  226. with self.assertRaises(UserError):
  227. line.tax_rate = 102
  228. def test_onchange_price(self):
  229. '''当订单行的不含税单价改变时,改变含税单价'''
  230. for line in self.adjust.line_ids:
  231. line.price_taxed = 0
  232. line.price = 10
  233. line.onchange_price()
  234. self.assertAlmostEqual(line.price_taxed, 11.7)
  235. def test_onchange_goods_id(self):
  236. '''当订单行的商品变化时,带出商品上的单位、成本'''
  237. for line in self.adjust.line_ids:
  238. line.goods_id = self.cable
  239. line.onchange_goods_id()
  240. self.assertTrue(line.uom_id.name == '件')
  241. # 测试价格是否是商品的成本
  242. self.assertTrue(line.price_taxed == self.cable.cost)
  243. def test_onchange_discount_rate(self):
  244. ''' 订单行优惠率改变时,改变优惠金额'''
  245. for line in self.adjust.line_ids:
  246. line.price_taxed = 11.7
  247. line.discount_rate = 10
  248. line.onchange_discount_rate()
  249. self.assertTrue(line.discount_amount == 10)
  250. def test_onchange_goods_id_tax_rate(self):
  251. ''' 测试 修改商品时,商品行税率变化 '''
  252. for order_line in self.adjust.line_ids:
  253. # partner 无 税率,采购调整单行商品无税率
  254. self.env.ref('core.lenovo').tax_rate = 0
  255. self.env.ref('goods.cable').tax_rate = 0
  256. order_line.onchange_goods_id()
  257. # partner 有 税率,采购调整单行商品无税率
  258. self.env.ref('core.lenovo').tax_rate = 10
  259. self.env.ref('goods.cable').tax_rate = 0
  260. order_line.onchange_goods_id()
  261. # partner 无税率,采购调整单行商品有税率
  262. self.env.ref('core.lenovo').tax_rate = 0
  263. self.env.ref('goods.cable').tax_rate = 10
  264. order_line.onchange_goods_id()
  265. # partner 税率 > 采购调整单行商品税率
  266. self.env.ref('core.lenovo').tax_rate = 11
  267. self.env.ref('goods.cable').tax_rate = 10
  268. order_line.onchange_goods_id()
  269. # partner 税率 =< 入库单行商品税率
  270. self.env.ref('core.lenovo').tax_rate = 9
  271. self.env.ref('goods.cable').tax_rate = 10
  272. order_line.onchange_goods_id()
上海开阖软件有限公司 沪ICP备12045867号-1