GoodERP
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

356 行
17KB

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