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.

357 satır
17KB

  1. from odoo import fields, models, api
  2. from odoo import _
  3. from odoo.exceptions import UserError
  4. from odoo.tools import float_compare
  5. # 订单确认状态可选值
  6. SELL_ORDER_STATES = [
  7. ('draft', '草稿'),
  8. ('done', '已确认'),
  9. ('cancel', '已作废')]
  10. class SellAdjust(models.Model):
  11. _name = "sell.adjust"
  12. _inherit = ['mail.thread']
  13. _description = "销售变更单"
  14. _order = 'date desc, id desc'
  15. name = fields.Char('单据编号', copy=False,
  16. help='变更单编号,保存时可自动生成')
  17. order_id = fields.Many2one('sell.order', '原始单据',
  18. copy=False, ondelete='restrict',
  19. help='要调整的原始销售订单,只能调整已确认且没有全部出库的销售订单')
  20. date = fields.Date('单据日期',
  21. default=lambda self: fields.Date.context_today(self),
  22. index=True, copy=False,
  23. help='变更单创建日期,默认是当前日期')
  24. change_type = fields.Selection([('price', '价格'), ('qty', '数量')], '调整类型', default='qty')
  25. line_ids = fields.One2many('sell.adjust.line', 'order_id', '变更单行',
  26. copy=True,
  27. help='变更单明细行,不允许为空')
  28. approve_uid = fields.Many2one('res.users', '确认人',
  29. copy=False, ondelete='restrict',
  30. help='确认变更单的人')
  31. state = fields.Selection(SELL_ORDER_STATES, '确认状态',
  32. index=True, copy=False,
  33. default='draft',
  34. tracking=True,
  35. help='变更单确认状态')
  36. note = fields.Text('备注',
  37. help='单据备注')
  38. user_id = fields.Many2one(
  39. 'res.users',
  40. '经办人',
  41. ondelete='restrict',
  42. default=lambda self: self.env.user,
  43. help='单据经办人',
  44. )
  45. company_id = fields.Many2one(
  46. 'res.company',
  47. string='公司',
  48. change_default=True,
  49. default=lambda self: self.env.company)
  50. @api.onchange('order_id')
  51. def onchange_sell_order(self):
  52. if not self.order_id:
  53. return {}
  54. self.line_ids = False
  55. change_lines = []
  56. for line in self.order_id.line_ids:
  57. vals = {
  58. 'quantity': 0,
  59. 'goods_id': line.goods_id.id,
  60. 'using_attribute': line.using_attribute,
  61. 'attribute_id': line.attribute_id.id,
  62. 'uom_id': line.uom_id.id,
  63. 'price': line.price,
  64. 'price_taxed': line.price_taxed,
  65. 'discount_rate': line.discount_rate,
  66. 'discount_amount': line.discount_amount,
  67. 'amount': line.amount,
  68. 'tax_rate': line.tax_rate,
  69. 'note': line.note,
  70. 'company_id': line.company_id.id,
  71. }
  72. change_lines.append((0, 0, vals))
  73. self.line_ids = change_lines
  74. def check_order(self):
  75. self.ensure_one()
  76. if self.state == 'done':
  77. raise UserError('请不要重复确认!')
  78. if not self.line_ids:
  79. raise UserError(_('请输入商品明细行!'))
  80. for line in self.line_ids:
  81. # 调价格不能有数量变更
  82. if self.change_type == 'price' and line.quantity:
  83. raise UserError('调整价格时不能调整数量!')
  84. # 调数量不能有价格变更
  85. if self.change_type == 'qty':
  86. origin_line = self.env['sell.order.line'].search(
  87. [('goods_id', '=', line.goods_id.id),
  88. ('attribute_id', '=', line.attribute_id.id),
  89. ('order_id', '=', self.order_id.id)])
  90. if len(origin_line) > 1:
  91. raise UserError(_('要调整的商品 %s 在原始单据中不唯一') % line.goods_id.name)
  92. if round(origin_line.price - line.price, 6):
  93. raise UserError('调整数量时不能调整单价!')
  94. def sell_adjust_done(self):
  95. #二种调整不能同时进行,人算不过来
  96. self.check_order()
  97. if self.change_type == 'qty':
  98. self.change_qty()
  99. if self.change_type == 'price':
  100. self.change_price()
  101. self.state = 'done'
  102. self.approve_uid = self._uid
  103. def change_price(self):
  104. '''
  105. 确认销售数量单价变更单:
  106. 当结算单有付款或发票号,报错。
  107. 其他都可以改。
  108. '''
  109. for line in self.line_ids:
  110. origin_line = self.env['sell.order.line'].search(
  111. [('goods_id', '=', line.goods_id.id),
  112. ('attribute_id', '=', line.attribute_id.id),
  113. ('order_id', '=', self.order_id.id)])
  114. if not origin_line:
  115. raise UserError('调整价格时不能新增行!')
  116. #变更销售单
  117. origin_line_amount = round(line.price * origin_line.quantity,2)
  118. origin_line_subtotal = round(line.price_taxed * origin_line.quantity,2)
  119. note = origin_line.note or ''
  120. note += '变更单:%s单价%s变更%s。\n' % (self.name, origin_line.price, line.price)
  121. origin_line.write({
  122. 'price': line.price,
  123. 'price_taxed': line.price_taxed,
  124. 'amount': origin_line_amount,
  125. 'tax_amount': origin_line_subtotal - origin_line_amount,
  126. 'subtotal': origin_line_subtotal,
  127. 'note': note,
  128. })
  129. #变更送货单
  130. move_line = self.env['wh.move.line'].search(
  131. [('sell_line_id', '=', origin_line.id)])
  132. move_line_amount = round(line.price * move_line.goods_qty, 2)
  133. move_line_subtotal = round(line.price_taxed * move_line.goods_qty, 2)
  134. move_line.write({
  135. 'price': line.price,
  136. 'price_taxed': line.price_taxed,
  137. 'amount': move_line_amount,
  138. 'tax_amount': move_line_subtotal - move_line_amount,
  139. 'subtotal': move_line_subtotal,
  140. 'note': note,
  141. })
  142. # 删除结算单
  143. invoice_id = self.env['money.invoice'].search([('move_id', '=', move_line.move_id.id)])
  144. if invoice_id and (invoice_id.reconciled or invoice_id.bill_number):
  145. raise UserError('已付款或有发票号码的订单不能变更价格!')
  146. if invoice_id:
  147. invoice_id.unlink()
  148. #如果送货单无对账单,重新生成
  149. delivery_ids = self.env['sell.delivery'].search(
  150. [('order_id', '=', self.order_id.id),
  151. ('state', '=', 'done')])
  152. for delivery_id in delivery_ids:
  153. if not delivery_id.invoice_id:
  154. delivery_id._delivery_make_invoice()
  155. def change_qty(self):
  156. '''确认销售数量变更单:
  157. 当调整后数量 < 原单据中已出库数量,则报错;
  158. 当调整后数量 > 原单据中已出库数量,则更新原单据及发货单分单的数量;
  159. 当调整后数量 = 原单据中已出库数量,则更新原单据数量,删除发货单分单;
  160. 当新增商品时,则更新原单据及发货单分单明细行。
  161. '''
  162. delivery = self.env['sell.delivery'].search(
  163. [('order_id', '=', self.order_id.id),
  164. ('state', '=', 'draft')])
  165. if not delivery:
  166. raise UserError('销售发货单已全部出库,不能调整')
  167. for line in self.line_ids:
  168. # 检查属性是否填充,防止无权限人员不填就可以保存
  169. if line.using_attribute and not line.attribute_id:
  170. raise UserError(_('请输入商品:%s 的属性') % line.goods_id.name)
  171. origin_line = self.env['sell.order.line'].search(
  172. [('goods_id', '=', line.goods_id.id),
  173. ('attribute_id', '=', line.attribute_id.id),
  174. ('order_id', '=', self.order_id.id)])
  175. if len(origin_line) > 1:
  176. raise UserError(_('要调整的商品 %s 在原始单据中不唯一') % line.goods_id.name)
  177. if origin_line:
  178. origin_line.quantity += line.quantity # 调整后数量
  179. new_note = '变更单:%s %s。\n' % (self.name, line.note)
  180. origin_line.note = (origin_line.note and
  181. origin_line.note + new_note or new_note)
  182. if origin_line.quantity < origin_line.quantity_out:
  183. raise UserError(' %s 调整后数量不能小于原订单已出库数量' %
  184. line.goods_id.name)
  185. elif origin_line.quantity > origin_line.quantity_out:
  186. # 查找出原销售订单产生的草稿状态的发货单明细行,并更新它
  187. move_line = self.env['wh.move.line'].search(
  188. [('sell_line_id', '=', origin_line.id),
  189. ('state', '=', 'draft')])
  190. if move_line:
  191. move_line.goods_qty += line.quantity
  192. else:
  193. raise UserError(_('商品 %s 已全部入库,建议新建采购订单') %
  194. line.goods_id.name)
  195. # 调整后数量与已出库数量相等时,删除产生的发货单分单
  196. else:
  197. # 先删除对应的发货单行
  198. move_line = self.env['wh.move.line'].search(
  199. [('sell_line_id', '=', origin_line.id), ('state', '=',
  200. 'draft')])
  201. if move_line:
  202. move_line.unlink()
  203. # 如果发货单明细没有了,则删除发货单
  204. if len(delivery.sell_move_id.line_out_ids) == 0:
  205. delivery.unlink()
  206. else:
  207. vals = {
  208. 'order_id': self.order_id.id,
  209. 'goods_id': line.goods_id.id,
  210. 'attribute_id': line.attribute_id.id,
  211. 'quantity': line.quantity,
  212. 'uom_id': line.uom_id.id,
  213. 'price_taxed': line.price_taxed,
  214. 'discount_rate': line.discount_rate,
  215. 'discount_amount': line.discount_amount,
  216. 'tax_rate': line.tax_rate,
  217. 'note': line.note or '',
  218. }
  219. new_line = self.env['sell.order.line'].create(vals)
  220. delivery_line = []
  221. if line.goods_id.force_batch_one:
  222. i = 0
  223. while i < line.quantity:
  224. i += 1
  225. delivery_line.append(
  226. self.order_id.get_delivery_line(new_line, single=True))
  227. else:
  228. delivery_line.append(
  229. self.order_id.get_delivery_line(new_line, single=False))
  230. delivery.write(
  231. {'line_out_ids': [(0, 0, li) for li in delivery_line]})
  232. class SellAdjustLine(models.Model):
  233. _name = 'sell.adjust.line'
  234. _description = '销售变更单明细'
  235. @api.depends('goods_id')
  236. def _compute_using_attribute(self):
  237. '''返回订单行中商品是否使用属性'''
  238. for l in self:
  239. l.using_attribute = l.goods_id.attribute_ids and True or False
  240. @api.depends('quantity', 'price_taxed', 'discount_amount', 'tax_rate')
  241. def _compute_all_amount(selfs):
  242. '''当订单行的数量、单价、折扣额、税率改变时,改变采购金额、税额、价税合计'''
  243. for self in selfs:
  244. self.subtotal = self.price_taxed * self.quantity - self.discount_amount # 价税合计
  245. self.tax_amount = self.subtotal / \
  246. (100 + self.tax_rate) * self.tax_rate # 税额
  247. self.amount = self.subtotal - self.tax_amount # 金额
  248. @api.onchange('price', 'tax_rate')
  249. def onchange_price(self):
  250. '''当订单行的不含税单价改变时,改变含税单价'''
  251. price = self.price_taxed / (1 + self.tax_rate * 0.01) # 不含税单价
  252. decimal = self.env.ref('core.decimal_price')
  253. if float_compare(price, self.price, precision_digits=decimal.digits) != 0:
  254. self.price_taxed = self.price * (1 + self.tax_rate * 0.01)
  255. order_id = fields.Many2one('sell.adjust', '订单编号', index=True,
  256. required=True, ondelete='cascade',
  257. help='关联的变更单编号')
  258. goods_id = fields.Many2one('goods', '商品', ondelete='restrict',
  259. help='商品')
  260. using_attribute = fields.Boolean('使用属性', compute=_compute_using_attribute,
  261. help='商品是否使用属性')
  262. attribute_id = fields.Many2one('attribute', '属性',
  263. ondelete='restrict',
  264. domain="[('goods_id', '=', goods_id)]",
  265. help='商品的属性,当商品有属性时,该字段必输')
  266. uom_id = fields.Many2one('uom', '单位', ondelete='restrict',
  267. help='商品计量单位')
  268. quantity = fields.Float('调整数量',
  269. default=1,
  270. required=True,
  271. digits='Quantity',
  272. help='相对于原单据对应明细行的调整数量,可正可负')
  273. price = fields.Float('销售单价',
  274. store=True,
  275. digits='Price',
  276. help='不含税单价,由含税单价计算得出')
  277. price_taxed = fields.Float('含税单价',
  278. digits='Price',
  279. help='含税单价,取自商品零售价')
  280. discount_rate = fields.Float('折扣率%',
  281. help='折扣率')
  282. discount_amount = fields.Float('折扣额',
  283. digits='Amount',
  284. help='输入折扣率后自动计算得出,也可手动输入折扣额')
  285. amount = fields.Float('金额',
  286. compute=_compute_all_amount,
  287. store=True,
  288. digits='Amount',
  289. help='金额 = 价税合计 - 税额')
  290. tax_rate = fields.Float('税率(%)', default=lambda self: self.env.user.company_id.import_tax_rate,
  291. help='默认值取公司销项税率')
  292. tax_amount = fields.Float('税额',
  293. compute=_compute_all_amount,
  294. store=True,
  295. digits='Amount',
  296. help='由税率计算得出')
  297. subtotal = fields.Float('价税合计',
  298. compute=_compute_all_amount,
  299. store=True,
  300. digits='Amount',
  301. help='含税单价 乘以 数量')
  302. note = fields.Char('备注',
  303. help='本行备注')
  304. company_id = fields.Many2one(
  305. 'res.company',
  306. string='公司',
  307. change_default=True,
  308. default=lambda self: self.env.company)
  309. @api.onchange('goods_id')
  310. def onchange_goods_id(self):
  311. '''当订单行的商品变化时,带出商品上的单位、默认仓库、价格'''
  312. if self.goods_id:
  313. self.uom_id = self.goods_id.uom_id
  314. # 修正 单价 及含税单价问题
  315. self.price_taxed = self.goods_id.price * (1 + self.goods_id.tax_rate * 0.01) if self.goods_id.tax_rate else self.goods_id.price
  316. self.tax_rate = self.goods_id.get_tax_rate(self.goods_id, self.order_id.order_id.partner_id, 'sell')
  317. @api.onchange('quantity', 'price_taxed', 'discount_rate')
  318. def onchange_discount_rate(self):
  319. '''当数量、含税单价或优惠率发生变化时,优惠金额发生变化'''
  320. self.price = self.price_taxed / (1 + self.tax_rate * 0.01)
  321. self.discount_amount = (self.quantity * self.price *
  322. self.discount_rate * 0.01)
  323. @api.constrains('tax_rate')
  324. def _check_(self):
  325. for record in self:
  326. if record.tax_rate > 100:
  327. raise UserError('税率不能输入超过100的数')
  328. if record.tax_rate < 0:
  329. raise UserError('税率不能输入负数')
上海开阖软件有限公司 沪ICP备12045867号-1