GoodERP
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

257 lines
12KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import fields, models, api
  4. from odoo import _
  5. from odoo.exceptions import UserError
  6. from odoo.tools import float_compare
  7. # 订单确认状态可选值
  8. BUY_ORDER_STATES = [
  9. ('draft', '草稿'),
  10. ('done', '已确认'),
  11. ('cancel', '已作废')]
  12. # 字段只读状态
  13. READONLY_STATES = {
  14. 'done': [('readonly', True)],
  15. 'cancel': [('readonly', True)],
  16. }
  17. class BuyAdjust(models.Model):
  18. _name = "buy.adjust"
  19. _inherit = ['mail.thread']
  20. _description = "采购变更单"
  21. _order = 'date desc, id desc'
  22. name = fields.Char('单据编号', copy=False,
  23. help='变更单编号,保存时可自动生成')
  24. order_id = fields.Many2one('buy.order', '原始单据',
  25. copy=False, ondelete='restrict',
  26. help='要调整的原始采购订单,只能调整已确认且没有全部入库的采购订单')
  27. date = fields.Date('单据日期',
  28. default=lambda self: fields.Date.context_today(self),
  29. index=True, copy=False,
  30. help='变更单创建日期,默认是当前日期')
  31. line_ids = fields.One2many('buy.adjust.line', 'order_id', '变更单行',
  32. copy=True,
  33. help='变更单明细行,不允许为空')
  34. approve_uid = fields.Many2one('res.users', '确认人',
  35. copy=False, ondelete='restrict',
  36. help='确认变更单的人')
  37. state = fields.Selection(BUY_ORDER_STATES, '确认状态',
  38. index=True, copy=False,
  39. default='draft',
  40. help='变更单确认状态')
  41. note = fields.Text('备注',
  42. help='单据备注')
  43. user_id = fields.Many2one(
  44. 'res.users',
  45. '经办人',
  46. ondelete='restrict',
  47. default=lambda self: self.env.user,
  48. help='单据经办人',
  49. )
  50. company_id = fields.Many2one(
  51. 'res.company',
  52. string='公司',
  53. change_default=True,
  54. default=lambda self: self.env.company)
  55. def _get_vals(self, line):
  56. '''返回创建 buy order line 时所需数据'''
  57. return {
  58. 'order_id': self.order_id.id,
  59. 'goods_id': line.goods_id.id,
  60. 'attribute_id': line.attribute_id.id,
  61. 'quantity': line.quantity,
  62. 'uom_id': line.uom_id.id,
  63. 'price_taxed': line.price_taxed,
  64. 'discount_rate': line.discount_rate,
  65. 'discount_amount': line.discount_amount,
  66. 'tax_rate': line.tax_rate,
  67. 'note': line.note or '',
  68. }
  69. def buy_adjust_done(self):
  70. '''确认采购变更单:
  71. 当调整后数量 < 原单据中已入库数量,则报错;
  72. 当调整后数量 > 原单据中已入库数量,则更新原单据及入库单分单的数量;
  73. 当调整后数量 = 原单据中已入库数量,则更新原单据数量,删除入库单行;
  74. 当新增商品时,则更新原单据及入库单分单明细行。
  75. '''
  76. self.ensure_one()
  77. if self.state == 'done':
  78. raise UserError('请不要重复确认!\n采购变更单%s已确认' % self.name)
  79. if not self.line_ids:
  80. raise UserError(_('请输入商品明细行!'))
  81. for line in self.line_ids:
  82. if line.price_taxed < 0:
  83. raise UserError(_('商品含税单价不能小于0!\n单价:%s') % line.price_taxed)
  84. buy_receipt = self.env['buy.receipt'].search(
  85. [('order_id', '=', self.order_id.id),
  86. ('state', '=', 'draft')])
  87. if not buy_receipt:
  88. raise UserError('采购入库单已全部入库,不能调整')
  89. for line in self.line_ids:
  90. # 检查属性是否填充,防止无权限人员不填就可以保存
  91. if line.using_attribute and not line.attribute_id:
  92. raise UserError(_('请输入商品:%s 的属性') % line.goods_id.name)
  93. origin_line = self.env['buy.order.line'].search(
  94. [('goods_id', '=', line.goods_id.id),
  95. ('attribute_id', '=', line.attribute_id.id),
  96. ('order_id', '=', self.order_id.id)])
  97. if len(origin_line) > 1:
  98. raise UserError(_('要调整的商品%s在原始单据中不唯一') % line.goods_id.name)
  99. if origin_line:
  100. origin_line.quantity += line.quantity # 调整后数量
  101. new_note = '变更单:%s %s。\n' % (self.name, line.note)
  102. origin_line.note = (origin_line.note and
  103. origin_line.note + new_note or new_note)
  104. if origin_line.quantity < origin_line.quantity_in:
  105. raise UserError('%s调整后数量不能小于原订单已入库数量' %
  106. line.goods_id.name)
  107. elif origin_line.quantity > origin_line.quantity_in:
  108. # 查找出原采购订单产生的草稿状态的入库单明细行,并更新它
  109. move_line = self.env['wh.move.line'].search(
  110. [('buy_line_id', '=', origin_line.id),
  111. ('state', '=', 'draft')])
  112. if move_line:
  113. move_line.goods_qty += line.quantity
  114. else:
  115. raise UserError(_('商品%s已全部入库,建议新建采购订单') %
  116. line.goods_id.name)
  117. # 调整后数量与已入库数量相等时,删除产生的入库单分单
  118. else:
  119. # 删除对应行
  120. move_line = self.env['wh.move.line'].search(
  121. [('buy_line_id', '=', origin_line.id),
  122. ('state', '=', 'draft')])
  123. move_line.unlink()
  124. else:
  125. new_line = self.env['buy.order.line'].create(
  126. self._get_vals(line))
  127. receipt_line = []
  128. if line.goods_id.force_batch_one:
  129. i = 0
  130. while i < line.quantity:
  131. i += 1
  132. receipt_line.append(
  133. self.order_id.get_receipt_line(new_line, single=True))
  134. else:
  135. receipt_line.append(
  136. self.order_id.get_receipt_line(new_line, single=False))
  137. buy_receipt.write(
  138. {'line_in_ids': [(0, 0, li) for li in receipt_line]})
  139. if not buy_receipt.line_in_ids:
  140. buy_receipt.unlink()
  141. self.state = 'done'
  142. self.approve_uid = self._uid
  143. class BuyAdjustLine(models.Model):
  144. _name = 'buy.adjust.line'
  145. _description = '采购变更单明细'
  146. @api.depends('goods_id')
  147. def _compute_using_attribute(selfs):
  148. '''返回订单行中商品是否使用属性'''
  149. for self in selfs:
  150. self.using_attribute = self.goods_id.attribute_ids and True or False
  151. @api.depends('quantity', 'price_taxed', 'discount_amount', 'tax_rate')
  152. def _compute_all_amount(selfs):
  153. '''当订单行的数量、单价、折扣额、税率改变时,改变采购金额、税额、价税合计'''
  154. for self in selfs:
  155. self.subtotal = self.price_taxed * self.quantity - self.discount_amount # 价税合计
  156. self.tax_amount = self.subtotal / \
  157. (100 + self.tax_rate) * self.tax_rate # 税额
  158. self.amount = self.subtotal - self.tax_amount # 金额
  159. @api.onchange('price', 'tax_rate')
  160. def onchange_price(self):
  161. '''当订单行的不含税单价改变时,改变含税单价'''
  162. price = self.price_taxed / (1 + self.tax_rate * 0.01) # 不含税单价
  163. decimal = self.env.ref('core.decimal_price')
  164. if float_compare(price, self.price, precision_digits=decimal.digits) != 0:
  165. self.price_taxed = self.price * (1 + self.tax_rate * 0.01)
  166. order_id = fields.Many2one('buy.adjust', '订单编号', index=True,
  167. required=True, ondelete='cascade',
  168. help='关联的变更单编号')
  169. goods_id = fields.Many2one('goods', '商品', ondelete='restrict',
  170. help='商品')
  171. using_attribute = fields.Boolean('使用属性', compute=_compute_using_attribute,
  172. help='商品是否使用属性')
  173. attribute_id = fields.Many2one('attribute', '属性',
  174. ondelete='restrict',
  175. domain="[('goods_id', '=', goods_id)]",
  176. help='商品的属性,当商品有属性时,该字段必输')
  177. uom_id = fields.Many2one('uom', '单位', ondelete='restrict',
  178. help='商品计量单位')
  179. quantity = fields.Float('调整数量',
  180. default=1,
  181. required=True,
  182. digits='Quantity',
  183. help='相对于原单据对应明细行的调整数量,可正可负')
  184. price = fields.Float('采购单价',
  185. store=True,
  186. digits='Price',
  187. help='不含税单价,由含税单价计算得出')
  188. price_taxed = fields.Float('含税单价',
  189. digits='Price',
  190. help='含税单价,取自商品成本')
  191. discount_rate = fields.Float('折扣率%',
  192. help='折扣率')
  193. discount_amount = fields.Float('折扣额',
  194. digits='Amount',
  195. help='输入折扣率后自动计算得出,也可手动输入折扣额')
  196. amount = fields.Float('金额', compute=_compute_all_amount,
  197. store=True, readonly=True,
  198. digits='Amount',
  199. help='金额 = 价税合计 - 税额')
  200. tax_rate = fields.Float('税率(%)', default=lambda self: self.env.user.company_id.import_tax_rate,
  201. help='默认值取公司进项税率')
  202. tax_amount = fields.Float('税额', compute=_compute_all_amount,
  203. store=True, readonly=True,
  204. digits='Amount',
  205. help='由税率计算得出')
  206. subtotal = fields.Float('价税合计', compute=_compute_all_amount,
  207. store=True, readonly=True,
  208. digits='Amount',
  209. help='含税单价 乘以 数量')
  210. note = fields.Char('备注',
  211. help='本行备注')
  212. company_id = fields.Many2one(
  213. 'res.company',
  214. string='公司',
  215. change_default=True,
  216. default=lambda self: self.env.company)
  217. @api.onchange('goods_id')
  218. def onchange_goods_id(self):
  219. '''当订单行的商品变化时,带出商品上的单位、默认仓库、成本价'''
  220. if self.goods_id:
  221. self.uom_id = self.goods_id.uom_id
  222. self.price_taxed = self.goods_id.cost
  223. self.tax_rate = self.goods_id.get_tax_rate(self.goods_id, self.order_id.order_id.partner_id, 'buy')
  224. @api.onchange('quantity', 'price_taxed', 'discount_rate')
  225. def onchange_discount_rate(self):
  226. '''当数量、单价或优惠率发生变化时,优惠金额发生变化'''
  227. self.price = self.price_taxed / (1 + self.tax_rate * 0.01)
  228. self.discount_amount = (self.quantity * self.price *
  229. self.discount_rate * 0.01)
  230. @api.constrains('tax_rate')
  231. def _check(selfs):
  232. for self in selfs:
  233. if self.tax_rate > 100:
  234. raise UserError('税率不能输入超过100的数\n税率:%s!' % self.tax_rate)
  235. if self.tax_rate < 0:
  236. raise UserError('税率不能输入负数\n税率:%s!' % self.tax_rate)
上海开阖软件有限公司 沪ICP备12045867号-1