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.

740 lines
32KB

  1. from odoo import fields, models, api
  2. from odoo.exceptions import UserError
  3. from odoo.tools import float_compare
  4. import logging
  5. _logger = logging.getLogger(__name__)
  6. # 销售订单确认状态可选值
  7. SELL_ORDER_STATES = [
  8. ('draft', '草稿'),
  9. ('done', '已确认'),
  10. ('cancel', '已作废')]
  11. class SellOrder(models.Model):
  12. _name = 'sell.order'
  13. _description = '销售订单'
  14. _inherit = ['mail.thread']
  15. _order = 'date desc, id desc'
  16. @api.depends('line_ids.subtotal', 'discount_amount')
  17. def _compute_amount(self):
  18. '''当订单行和优惠金额改变时,改变成交金额'''
  19. for o in self:
  20. total = sum(line.subtotal for line in o.line_ids)
  21. o.amount = total - o.discount_amount
  22. o.untax_amount = sum(line.amount for line in o.line_ids)
  23. o.tax_amount = sum(line.tax_amount for line in o.line_ids)
  24. @api.depends('line_ids.quantity')
  25. def _compute_qty(self):
  26. '''当订单行数量改变时,更新总数量'''
  27. for o in self:
  28. o.total_qty = sum(line.quantity for line in o.line_ids)
  29. @api.depends('delivery_ids.state')
  30. def _get_sell_goods_state(selfs):
  31. '''返回发货状态'''
  32. for self in selfs:
  33. if all(line.quantity_out == 0 for line in self.line_ids):
  34. if any(r.state == 'draft' for r in self.delivery_ids) or self.state=='draft':
  35. self.goods_state = '未出库'
  36. else:
  37. self.goods_state = '全部作废'
  38. elif any(line.quantity > line.quantity_out for line in self.line_ids):
  39. if any(r.state == 'draft' for r in self.delivery_ids):
  40. self.goods_state = '部分出库'
  41. else:
  42. self.goods_state = '部分出库剩余作废'
  43. else:
  44. self.goods_state = '全部出库'
  45. @api.depends('partner_id')
  46. def _compute_currency_id(self):
  47. for o in self:
  48. self.currency_id = \
  49. self.partner_id.c_category_id.account_id.currency_id.id \
  50. or self.partner_id.s_category_id.account_id.currency_id.id
  51. @api.model
  52. def _default_warehouse(self):
  53. return self._default_warehouse_impl()
  54. @api.model
  55. def _default_warehouse_impl(self):
  56. if self.env.context.get('warehouse_type'):
  57. return self.env['warehouse'].get_warehouse_by_type(
  58. self.env.context.get('warehouse_type'))
  59. def _get_received_amount(selfs):
  60. '''计算销售订单收款/退款状态'''
  61. for self in selfs:
  62. deliverys = self.env['sell.delivery'].search(
  63. [('order_id', '=', self.id)])
  64. money_order_rows = self.env['money.order'].search([('sell_id', '=', self.id),
  65. ('reconciled', '=', 0),
  66. ('state', '=', 'done')])
  67. self.received_amount = sum([delivery.invoice_id.reconciled for delivery in deliverys]) +\
  68. sum([order_row.amount for order_row in money_order_rows])
  69. @api.depends('delivery_ids')
  70. def _compute_delivery(self):
  71. for order in self:
  72. order.delivery_count = len([deli for deli in order.delivery_ids if not deli.is_return])
  73. order.return_count = len([deli for deli in order.delivery_ids if deli.is_return])
  74. @api.depends('partner_id')
  75. def _get_sell_user(selfs):
  76. '''计算销售单据的业务员,不允许修改'''
  77. for self in selfs:
  78. if self.partner_id:
  79. if self.partner_id.responsible_id:
  80. self.user_id = self.partner_id.responsible_id
  81. else:
  82. self.user_id = self._uid
  83. @api.depends('line_ids.goods_id', 'line_ids.quantity')
  84. def _compute_net_weight(self):
  85. '''计算净重合计'''
  86. for o in self:
  87. o.net_weight = sum(
  88. line.goods_id.net_weight * line.quantity
  89. for line in o.line_ids
  90. )
  91. partner_id = fields.Many2one('partner', '客户',
  92. ondelete='restrict',
  93. help='签约合同的客户')
  94. contact = fields.Char('联系人',
  95. help='客户方的联系人')
  96. address_id = fields.Many2one('partner.address', '地址',
  97. domain="[('partner_id', '=', partner_id)]",
  98. help='联系地址')
  99. mobile = fields.Char('手机',
  100. help='联系手机')
  101. user_id = fields.Many2one(
  102. 'res.users',
  103. '销售员',
  104. ondelete='restrict',store=True,
  105. compute='_get_sell_user',
  106. help='单据经办人',
  107. )
  108. date = fields.Date('单据日期',
  109. required=True,
  110. default=lambda self: fields.Date.context_today(self),
  111. index=True,
  112. copy=False,
  113. help="默认是订单创建日期")
  114. delivery_date = fields.Date(
  115. '要求交货日期',
  116. required=True,
  117. default=lambda self: fields.Date.context_today(self),
  118. index=True,
  119. copy=False,
  120. help="订单的要求交货日期")
  121. type = fields.Selection([('sell', '销售'), ('return', '退货')], '类型',
  122. default='sell',
  123. help='销售订单的类型,分为销售或退货')
  124. ref = fields.Char('客户订单号')
  125. warehouse_id = fields.Many2one('warehouse',
  126. '调出仓库',
  127. required=True,
  128. ondelete='restrict',
  129. default=_default_warehouse,
  130. help='商品将从该仓库调出')
  131. name = fields.Char('单据编号', index=True, copy=False,
  132. default='/', help="创建时它会自动生成下一个编号")
  133. line_ids = fields.One2many('sell.order.line', 'order_id', '销售订单行',
  134. copy=True,
  135. help='销售订单的明细行,不能为空')
  136. note = fields.Text('备注', help='单据备注')
  137. discount_rate = fields.Float('优惠率(%)',
  138. help='整单优惠率')
  139. discount_amount = fields.Float('抹零',
  140. track_visibility='always',
  141. digits='Amount',
  142. help='整单优惠金额,可由优惠率自动计算出来,也可手动输入')
  143. amount = fields.Float(string='成交金额', store=True, readonly=True,
  144. compute='_compute_amount', track_visibility='always',
  145. digits='Amount',
  146. help='总金额减去优惠金额')
  147. tax_amount = fields.Float(string='税额', store=True, readonly=True,
  148. compute='_compute_amount', track_visibility='always',
  149. digits='Amount',
  150. help='税额')
  151. untax_amount = fields.Float(string='不含税金额', store=True, readonly=True,
  152. compute='_compute_amount', track_visibility='always',
  153. digits='Amount',
  154. help='不含税金额')
  155. total_qty = fields.Float(string='数量合计', store=True, readonly=True, copy=False,
  156. compute='_compute_qty',
  157. track_visibility='always',
  158. digits='Quantity',
  159. help='数量总计')
  160. pre_receipt = fields.Float('预收款',
  161. digits='Amount',
  162. help='输入预收款确认销售订单,会产生一张收款单')
  163. bank_account_id = fields.Many2one('bank.account', '结算账户',
  164. ondelete='restrict',
  165. help='用来核算和监督企业与其他单位或个人之间的债权债务的结算情况')
  166. approve_uid = fields.Many2one('res.users', '确认人', copy=False,
  167. ondelete='restrict',
  168. help='确认单据的人')
  169. state = fields.Selection(SELL_ORDER_STATES, '确认状态', readonly=True,
  170. help="销售订单的确认状态", index=True,
  171. tracking=True,
  172. copy=False, default='draft')
  173. goods_state = fields.Char('发货状态', compute=_get_sell_goods_state,
  174. default='未出库',
  175. store=True,
  176. help="销售订单的发货状态", index=True, copy=False)
  177. cancelled = fields.Boolean('已终止',
  178. help='该单据是否已终止')
  179. currency_id = fields.Many2one('res.currency',
  180. '外币币别',
  181. compute='_compute_currency_id',
  182. store=True,
  183. readonly=True,
  184. help='外币币别')
  185. pay_base_currency = fields.Boolean('以本币结算', help='客户以本币付款到我公司基本账户内')
  186. company_id = fields.Many2one(
  187. 'res.company',
  188. string='公司',
  189. change_default=True,
  190. default=lambda self: self.env.company)
  191. received_amount = fields.Float(
  192. '已收金额', compute=_get_received_amount, readonly=True)
  193. delivery_ids = fields.One2many(
  194. 'sell.delivery', 'order_id', string='发货单', copy=False)
  195. delivery_count = fields.Integer(
  196. compute='_compute_delivery', string='发货单数量', default=0)
  197. return_count = fields.Integer(
  198. compute='_compute_delivery', string='退货单数量', default=0)
  199. pay_method = fields.Many2one('pay.method',
  200. string='付款方式',
  201. ondelete='restrict')
  202. term_id = fields.Many2one('core.value', "贸易条款",
  203. domain=[('type', '=', 'price_term')],
  204. context={'type': 'price_term'}
  205. )
  206. pol = fields.Char('起运港')
  207. pod = fields.Char('目的港')
  208. express_type = fields.Char('承运商')
  209. money_order_id = fields.Many2one(
  210. 'money.order',
  211. '预收款单',
  212. readonly=True,
  213. copy=False,
  214. help='输入预收款确认时产生的预收款单')
  215. net_weight = fields.Float(
  216. string='净重合计', compute='_compute_net_weight', store=True)
  217. invoice_ids = fields.One2many(
  218. 'money.invoice', compute='_compute_invoice', string='Invoices')
  219. invoice_count = fields.Integer(
  220. compute='_compute_invoice', string='Invoices Count', default=0)
  221. details = fields.Html('明细',compute='_compute_details')
  222. paid_no_goods = fields.Boolean('已收款未发货',compute="_compute_paid_no_goods",store=True)
  223. goods_id = fields.Many2one(
  224. 'goods', related='line_ids.goods_id', string='商品') #用于在列表上根据商品搜索
  225. @api.depends('money_order_id.state','goods_state')
  226. def _compute_paid_no_goods(self):
  227. for o in self:
  228. o.paid_no_goods = False
  229. if o.state == 'done' and o.goods_state == '未出库' and o.received_amount:
  230. if not all(line.goods_id.no_stock for line in self.line_ids):
  231. o.paid_no_goods = True
  232. @api.depends('line_ids')
  233. def _compute_details(self):
  234. for v in self:
  235. vl = {'col':[],'val':[]}
  236. vl['col'] = ['商品','数量','单价','已发']
  237. for l in v.line_ids:
  238. vl['val'].append([l.goods_id.name,l.quantity,l.price,l.quantity_out])
  239. v.details = v.company_id._get_html_table(vl)
  240. @api.onchange('address_id')
  241. def onchange_partner_address(self):
  242. ''' 选择地址填充 联系人、电话 '''
  243. if self.address_id:
  244. self.contact = self.address_id.contact
  245. self.mobile = self.address_id.mobile
  246. @api.onchange('partner_id')
  247. def onchange_partner_id(self):
  248. ''' 选择客户带出其默认地址信息 '''
  249. if self.partner_id:
  250. self.contact = self.partner_id.contact
  251. self.mobile = self.partner_id.mobile
  252. self.pay_method = self.partner_id.pay_method
  253. for child in self.partner_id.child_ids:
  254. if child.is_default_add:
  255. self.address_id = child.id
  256. if self.partner_id.child_ids and not any([child.is_default_add for child in self.partner_id.child_ids]):
  257. partners_add = self.env['partner.address'].search(
  258. [('partner_id', '=', self.partner_id.id)], order='id')
  259. self.address_id = partners_add[0].id
  260. for line in self.line_ids:
  261. line.tax_rate = line.goods_id.get_tax_rate(line.goods_id, self.partner_id, 'sell')
  262. address_list = [
  263. child_list.id for child_list in self.partner_id.child_ids]
  264. if address_list:
  265. return {'domain': {'address_id': [('id', 'in', address_list)]}}
  266. else:
  267. self.address_id = False
  268. @api.onchange('discount_rate', 'line_ids')
  269. def onchange_discount_rate(self):
  270. '''当优惠率或销售订单行发生变化时,单据优惠金额发生变化'''
  271. total = sum(line.subtotal for line in self.line_ids)
  272. self.discount_amount = total * self.discount_rate * 0.01
  273. def _get_vals(self):
  274. '''返回创建 money_order 时所需数据'''
  275. flag = (self.type == 'sell' and 1 or -1) # 用来标志发库或退货
  276. amount = flag * self.amount
  277. this_reconcile = flag * self.pre_receipt
  278. money_lines = [{
  279. 'bank_id': self.bank_account_id.id,
  280. 'amount': this_reconcile,
  281. }]
  282. return {
  283. 'partner_id': self.partner_id.id,
  284. 'date': fields.Date.context_today(self),
  285. 'line_ids':
  286. [(0, 0, line) for line in money_lines],
  287. 'amount': amount,
  288. 'reconciled': this_reconcile,
  289. 'to_reconcile': amount,
  290. 'state': 'draft',
  291. 'origin_name': self.name,
  292. 'sell_id': self.id,
  293. }
  294. def generate_receipt_order(self):
  295. '''由销售订单生成收款单'''
  296. # 发库单/退货单
  297. if self.pre_receipt:
  298. money_order = self.with_context(type='get').env['money.order'].create(
  299. self._get_vals()
  300. )
  301. return money_order
  302. def sell_order_done(self):
  303. '''确认销售订单'''
  304. self.ensure_one()
  305. if self.state == 'done':
  306. raise UserError('请不要重复确认!')
  307. if not self.line_ids:
  308. raise UserError('请输入商品明细行!')
  309. for line in self.line_ids:
  310. # 检查属性是否填充,防止无权限人员不填就可以保存
  311. if line.using_attribute and not line.attribute_id:
  312. raise UserError('请输入商品:%s 的属性' % line.goods_id.name)
  313. if line.quantity <= 0 or line.price_taxed < 0:
  314. raise UserError('商品 %s 的数量和含税单价不能小于0!' % line.goods_id.name)
  315. if line.tax_amount > 0 and self.currency_id:
  316. raise UserError('外贸免税!')
  317. if not self.bank_account_id and self.pre_receipt:
  318. raise UserError('预付款不为空时,请选择结算账户!')
  319. # 销售预收款生成收款单
  320. money_order = self.generate_receipt_order()
  321. self.sell_generate_delivery()
  322. self.approve_uid = self._uid
  323. self.write({
  324. 'money_order_id': money_order and money_order.id,
  325. 'state': 'done', # 为保证审批流程顺畅,否则,未审批就可审核
  326. })
  327. return True
  328. def sell_order_draft(self):
  329. '''撤销确认销售订单'''
  330. self.ensure_one()
  331. if self.state == 'draft':
  332. raise UserError('请不要重复撤销 %s' % self._description)
  333. if any(r.state == 'done' for r in self.delivery_ids):
  334. raise UserError('该销售订单已经发货,不能撤销确认!')
  335. # 查找产生的发货单并删除
  336. self.delivery_ids.unlink()
  337. # 查找产生的收款单并删除
  338. if self.money_order_id:
  339. self.money_order_id.unlink()
  340. self.approve_uid = False
  341. self.state = 'draft'
  342. def get_delivery_line(self, line, single=False):
  343. '''返回销售发货/退货单行'''
  344. self.ensure_one()
  345. qty = 0
  346. discount_amount = 0
  347. if single:
  348. qty = 1
  349. discount_amount = line.discount_amount \
  350. / ((line.quantity - line.quantity_out) or 1)
  351. else:
  352. qty = line.quantity - line.quantity_out
  353. discount_amount = line.discount_amount
  354. return {
  355. 'type': self.type == 'sell' and 'out' or 'in',
  356. 'sell_line_id': line.id,
  357. 'goods_id': line.goods_id.id,
  358. 'attribute_id': line.attribute_id.id,
  359. 'uos_id': line.goods_id.uos_id.id,
  360. 'goods_qty': qty,
  361. 'uom_id': line.uom_id.id,
  362. 'cost_unit': line.goods_id.cost,
  363. 'price': line.price,
  364. 'price_taxed': line.price_taxed,
  365. 'discount_rate': line.discount_rate,
  366. 'discount_amount': discount_amount,
  367. 'tax_rate': line.tax_rate,
  368. 'plan_date':self.delivery_date,
  369. }
  370. def _generate_delivery(self, delivery_line):
  371. '''根据明细行生成发货单或退货单'''
  372. # 如果退货,warehouse_dest_id,warehouse_id要调换
  373. warehouse = (self.type == 'sell'
  374. and self.warehouse_id
  375. or self.env.ref("warehouse.warehouse_customer"))
  376. warehouse_dest = (self.type == 'sell'
  377. and self.env.ref("warehouse.warehouse_customer")
  378. or self.warehouse_id)
  379. rec = (self.type == 'sell' and self.with_context(is_return=False)
  380. or self.with_context(is_return=True))
  381. delivery_id = rec.env['sell.delivery'].create({
  382. 'partner_id': self.partner_id.id,
  383. 'warehouse_id': warehouse.id,
  384. 'warehouse_dest_id': warehouse_dest.id,
  385. 'user_id': self.user_id.id,
  386. 'date': self.delivery_date,
  387. 'order_id': self.id,
  388. 'ref':self.ref,
  389. 'origin': 'sell.delivery',
  390. 'discount_rate': self.discount_rate,
  391. 'discount_amount': self.discount_amount,
  392. 'currency_id': self.currency_id.id,
  393. 'contact': self.contact,
  394. 'address_id': self.address_id.id,
  395. 'mobile': self.mobile,
  396. 'express_type': self.express_type,
  397. })
  398. if self.type == 'sell':
  399. delivery_id.write({'line_out_ids': [
  400. (0, 0, line) for line in delivery_line]})
  401. else:
  402. delivery_id.write({'line_in_ids': [
  403. (0, 0, line) for line in delivery_line]})
  404. return delivery_id
  405. def sell_generate_delivery(self):
  406. '''由销售订单生成销售发货单'''
  407. self.ensure_one()
  408. delivery_line = [] # 销售发货单行
  409. for line in self.line_ids:
  410. # 如果订单部分出库,则点击此按钮时生成剩余数量的出库单
  411. to_out = line.quantity - line.quantity_out
  412. if to_out <= 0:
  413. continue
  414. if line.goods_id.force_batch_one:
  415. i = 0
  416. while i < to_out:
  417. i += 1
  418. delivery_line.append(
  419. self.get_delivery_line(line, single=True))
  420. else:
  421. delivery_line.append(
  422. self.get_delivery_line(line, single=False))
  423. if not delivery_line:
  424. return {}
  425. self._generate_delivery(delivery_line)
  426. return {}
  427. @api.depends('delivery_ids')
  428. def _compute_invoice(self):
  429. for order in self:
  430. money_invoices = self.env['money.invoice'].search([
  431. ('name', '=', order.name)])
  432. order.invoice_ids = not money_invoices and order.delivery_ids.mapped('invoice_id') or money_invoices + order.delivery_ids.mapped('invoice_id')
  433. order.invoice_count = len(order.invoice_ids.ids)
  434. def action_view_invoice(self):
  435. self.ensure_one()
  436. if self.invoice_count == 0:
  437. return False
  438. action = {
  439. 'name': '结算单(客户发票)',
  440. 'type': 'ir.actions.act_window',
  441. 'view_mode': 'form',
  442. 'res_model': 'money.invoice',
  443. 'view_id': False,
  444. 'target': 'current',
  445. }
  446. invoice_ids = self.invoice_ids.ids
  447. # choose the view_mode accordingly
  448. action['domain'] = "[('id','in',[" + \
  449. ','.join(map(str, invoice_ids)) + "])]"
  450. action['view_mode'] = 'list'
  451. return action
  452. def action_view_delivery(self):
  453. '''
  454. This function returns an action that display existing deliverys of given sells order ids.
  455. When only one found, show the delivery immediately.
  456. '''
  457. self.ensure_one()
  458. action = {
  459. 'name': '销售发货单',
  460. 'type': 'ir.actions.act_window',
  461. 'view_mode': 'form',
  462. 'res_model': 'sell.delivery',
  463. 'view_id': False,
  464. 'target': 'current',
  465. }
  466. delivery_ids = [delivery.id for delivery in self.delivery_ids if not delivery.is_return]
  467. if len(delivery_ids) > 1:
  468. action['domain'] = "[('id','in',[" + \
  469. ','.join(map(str, delivery_ids)) + "])]"
  470. action['view_mode'] = 'list,form'
  471. elif len(delivery_ids) == 1:
  472. view_id = self.env.ref('sell.sell_delivery_form').id
  473. action['views'] = [(view_id, 'form')]
  474. action['res_id'] = delivery_ids and delivery_ids[0] or False
  475. return action
  476. def action_view_return(self):
  477. '''
  478. 该销售订单对应的退货单
  479. '''
  480. self.ensure_one()
  481. action = {
  482. 'name': '销售退货单',
  483. 'type': 'ir.actions.act_window',
  484. 'view_type': 'form',
  485. 'view_mode': 'form',
  486. 'res_model': 'sell.delivery',
  487. 'view_id': False,
  488. 'target': 'current',
  489. }
  490. list_view_id = self.env.ref('sell.sell_return_list').id
  491. form_view_id = self.env.ref('sell.sell_return_form').id
  492. delivery_ids = [delivery.id for delivery in self.delivery_ids if delivery.is_return]
  493. if len(delivery_ids) > 1:
  494. action['domain'] = "[('id','in',[" + \
  495. ','.join(map(str, delivery_ids)) + "])]"
  496. action['view_mode'] = 'list,form'
  497. action['views'] = [(list_view_id, 'list'), (form_view_id, 'form')]
  498. elif len(delivery_ids) == 1:
  499. action['views'] = [(form_view_id, 'form')]
  500. action['res_id'] = delivery_ids and delivery_ids[0] or False
  501. return action
  502. class SellOrderLine(models.Model):
  503. _name = 'sell.order.line'
  504. _description = '销售订单明细'
  505. @api.depends('goods_id')
  506. def _compute_using_attribute(self):
  507. '''返回订单行中商品是否使用属性'''
  508. for l in self:
  509. l.using_attribute = l.goods_id.attribute_ids and True or False
  510. @api.depends('quantity', 'price_taxed', 'discount_amount', 'tax_rate')
  511. def _compute_all_amount(selfs):
  512. '''当订单行的数量、含税单价、折扣额、税率改变时,改变销售金额、税额、价税合计'''
  513. for self in selfs:
  514. if self.order_id.currency_id.id == self.env.user.company_id.currency_id.id:
  515. self.subtotal = self.price_taxed * self.quantity - self.discount_amount # 价税合计
  516. self.tax_amount = self.subtotal / \
  517. (100 + self.tax_rate) * self.tax_rate # 税额
  518. self.amount = self.subtotal - self.tax_amount # 金额
  519. else:
  520. rate_silent = self.env['res.currency'].get_rate_silent(
  521. self.order_id.date, self.order_id.currency_id.id) or 1
  522. if not self.order_id.pay_base_currency:
  523. rate_silent = 1
  524. self.subtotal = (self.price_taxed * self.quantity -
  525. self.discount_amount) * rate_silent # 价税合计
  526. self.tax_amount = self.subtotal / \
  527. (100 + self.tax_rate) * self.tax_rate # 税额
  528. self.amount = self.subtotal - self.tax_amount # 本位币金额
  529. @api.onchange('price', 'tax_rate')
  530. def onchange_price(self):
  531. '''当订单行的不含税单价改变时,改变含税单价。
  532. 如果将含税价改为99,则self.price计算出来为84.62,price=99/1.17,
  533. 跟84.62保留相同位数比较时是相等的,这种情况则保留含税价不变,
  534. 这样处理是为了使得修改含税价时不再重新计算含税价。
  535. '''
  536. _logger.info('单价或税率发生变化')
  537. price = self.price_taxed / (1 + self.tax_rate * 0.01) # 不含税单价
  538. decimal = self.env.ref('core.decimal_price')
  539. if float_compare(price, self.price, precision_digits=decimal.digits) != 0:
  540. self.price_taxed = self.price * (1 + self.tax_rate * 0.01)
  541. order_id = fields.Many2one('sell.order', '订单编号', index=True,
  542. required=True, ondelete='cascade',
  543. help='关联订单的编号')
  544. partner_id = fields.Many2one(
  545. 'partner',
  546. string="客户",
  547. related='order_id.partner_id',
  548. store=True)
  549. goods_id = fields.Many2one('goods',
  550. '商品',
  551. required=True,
  552. ondelete='restrict',
  553. help='商品')
  554. using_attribute = fields.Boolean('使用属性', compute=_compute_using_attribute,
  555. help='商品是否使用属性')
  556. attribute_id = fields.Many2one('attribute', '属性',
  557. ondelete='restrict',
  558. domain="[('goods_id', '=', goods_id)]",
  559. help='商品的属性,当商品有属性时,该字段必输')
  560. uom_id = fields.Many2one('uom', '单位', ondelete='restrict',
  561. help='商品计量单位')
  562. quantity = fields.Float('数量',
  563. default=1,
  564. required=True,
  565. digits='Quantity',
  566. help='下单数量')
  567. quantity_out = fields.Float('已执行数量', copy=False,
  568. digits='Quantity',
  569. help='销售订单产生的发货单/退货单已执行数量')
  570. price = fields.Float('销售单价',
  571. store=True,
  572. digits='Price',
  573. help='不含税单价,由含税单价计算得出')
  574. price_taxed = fields.Float('含税单价',
  575. digits='Price',
  576. help='含税单价,取商品零售价')
  577. discount_rate = fields.Float('折扣率%',
  578. help='折扣率')
  579. discount_amount = fields.Float('折扣额',
  580. help='输入折扣率后自动计算得出,也可手动输入折扣额')
  581. amount = fields.Float('金额',
  582. compute=_compute_all_amount,
  583. store=True,
  584. digits='Amount',
  585. help='金额 = 价税合计 - 税额')
  586. tax_rate = fields.Float('税率(%)',
  587. help='税率')
  588. tax_amount = fields.Float('税额',
  589. compute=_compute_all_amount,
  590. store=True,
  591. digits='Amount',
  592. help='税额')
  593. subtotal = fields.Float('价税合计',
  594. compute=_compute_all_amount,
  595. store=True,
  596. digits='Amount',
  597. help='含税单价 乘以 数量')
  598. note = fields.Char('备注',
  599. help='本行备注')
  600. company_id = fields.Many2one(
  601. 'res.company', string='公司',
  602. change_default=True,
  603. default=lambda self: self.env.company)
  604. # 销售订单行上增加订单的重要字段以便销售订单明细表界面可针对其筛选分组
  605. order_date = fields.Date(related='order_id.date', string='订单日期', store=True)
  606. order_state = fields.Selection(related='order_id.state', string='订单状态', store=True)
  607. order_currency = fields.Many2one('res.currency', related='order_id.currency_id', string='订单币种', store=True)
  608. quantity_todo = fields.Float(
  609. '未执行数量', compute="_compute_quantity_todo",
  610. store=True, digits='Quantity')
  611. @api.depends('quantity_out')
  612. def _compute_quantity_todo(self):
  613. for s in self:
  614. s.quantity_todo = s.quantity - s.quantity_out
  615. @api.onchange('goods_id')
  616. def onchange_warehouse_id(self):
  617. '''当订单行的仓库变化时,带出定价策略中的折扣率'''
  618. if self.order_id.warehouse_id and self.goods_id:
  619. partner = self.order_id.partner_id
  620. warehouse = self.order_id.warehouse_id
  621. goods = self.goods_id
  622. date = self.order_id.date
  623. pricing = self.env['pricing'].get_pricing_id(
  624. partner, warehouse, goods, date)
  625. if pricing:
  626. self.discount_rate = pricing.discount_rate
  627. else:
  628. self.discount_rate = 0
  629. @api.onchange('goods_id')
  630. def onchange_goods_id(self):
  631. '''当订单行的商品变化时,带出商品上的单位、默认仓库、价格、税率'''
  632. if self.goods_id:
  633. self.uom_id = self.goods_id.uom_id
  634. self.price = self.goods_id.price
  635. self.tax_rate = self.goods_id.get_tax_rate(self.goods_id, self.order_id.partner_id, 'sell')
  636. @api.onchange('quantity', 'price_taxed', 'discount_rate')
  637. def onchange_discount_rate(self):
  638. '''当数量、单价或优惠率发生变化时,优惠金额发生变化'''
  639. _logger.info('数量、含税价、折扣率发生变化')
  640. self.price = self.price_taxed / (1 + self.tax_rate * 0.01)
  641. self.discount_amount = self.quantity * self.price \
  642. * self.discount_rate * 0.01
  643. @api.constrains('tax_rate')
  644. def _check_tax_rate(selfs):
  645. for self in selfs:
  646. if self.tax_rate > 100:
  647. raise UserError('税率不能输入超过100的数!\n输入税率:%s' % self.tax_rate)
  648. if self.tax_rate < 0:
  649. raise UserError('税率不能输入负数\n 输入税率:%s' % self.tax_rate)
  650. class ApproveMultiSellOrder(models.TransientModel):
  651. _name = "approve.multi.sell.order"
  652. _description = '批量确认销售订单'
  653. def set_default_note(self):
  654. """
  655. 设置默认值, 用来确认要批量确认的订单
  656. """
  657. context = self.env.context
  658. order_names = [order.name for order in self.env['sell.order'].browse(context.get('active_ids'))]
  659. return '-'.join(order_names)
  660. note = fields.Char('本次处理销售订单', default=set_default_note, readonly=True)
  661. @api.model
  662. def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
  663. """ 根据内容判断 报出错误 """
  664. res = super(ApproveMultiSellOrder, self).fields_view_get(view_id, view_type, toolbar=toolbar, submenu=False)
  665. orders = self.env['sell.order'].browse(self.env.context.get('active_ids'))
  666. done_lists = ''
  667. for order in orders:
  668. if order.state == 'done':
  669. done_lists += order.name
  670. if done_lists:
  671. raise UserError('销售订单 ' + done_lists + ' 已确认!')
  672. return res
  673. def approve_sell_order(self):
  674. """ 确认销售订单 """
  675. for order in self.env['sell.order'].search([('id', 'in', self.env.context.get('active_ids'))]):
  676. order.sell_order_done()
上海开阖软件有限公司 沪ICP备12045867号-1