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.

741 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'), False)
  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. digits='Amount',
  141. help='整单优惠金额,可由优惠率自动计算出来,也可手动输入')
  142. amount = fields.Float(string='成交金额', store=True, readonly=True,
  143. compute='_compute_amount',
  144. digits='Amount',
  145. help='总金额减去优惠金额')
  146. tax_amount = fields.Float(string='税额', store=True, readonly=True,
  147. compute='_compute_amount',
  148. digits='Amount',
  149. help='税额')
  150. untax_amount = fields.Float(string='不含税金额', store=True, readonly=True,
  151. compute='_compute_amount',
  152. digits='Amount',
  153. help='不含税金额')
  154. total_qty = fields.Float(string='数量合计', store=True, readonly=True, copy=False,
  155. compute='_compute_qty',
  156. digits='Quantity',
  157. help='数量总计')
  158. pre_receipt = fields.Float('预收款',
  159. digits='Amount',
  160. help='输入预收款确认销售订单,会产生一张收款单')
  161. bank_account_id = fields.Many2one('bank.account', '结算账户',
  162. ondelete='restrict',
  163. help='用来核算和监督企业与其他单位或个人之间的债权债务的结算情况')
  164. approve_uid = fields.Many2one('res.users', '确认人', copy=False,
  165. ondelete='restrict',
  166. help='确认单据的人')
  167. state = fields.Selection(SELL_ORDER_STATES, '确认状态', readonly=True,
  168. help="销售订单的确认状态", index=True,
  169. tracking=True,
  170. copy=False, default='draft')
  171. goods_state = fields.Char('发货状态', compute=_get_sell_goods_state,
  172. default='未出库',
  173. store=True,
  174. help="销售订单的发货状态", index=True, copy=False)
  175. cancelled = fields.Boolean('已终止',
  176. help='该单据是否已终止')
  177. currency_id = fields.Many2one('res.currency',
  178. '外币币别',
  179. compute='_compute_currency_id',
  180. store=True,
  181. readonly=True,
  182. help='外币币别')
  183. is_multi_currency = fields.Boolean('多币种', related='company_id.is_multi_currency')
  184. pay_base_currency = fields.Boolean('以本币结算', help='客户以本币付款到我公司基本账户内')
  185. company_id = fields.Many2one(
  186. 'res.company',
  187. string='公司',
  188. change_default=True,
  189. default=lambda self: self.env.company)
  190. received_amount = fields.Float(
  191. '已收金额', compute=_get_received_amount, readonly=True)
  192. delivery_ids = fields.One2many(
  193. 'sell.delivery', 'order_id', string='发货单', copy=False)
  194. delivery_count = fields.Integer(
  195. compute='_compute_delivery', string='发货单数量', default=0)
  196. return_count = fields.Integer(
  197. compute='_compute_delivery', string='退货单数量', default=0)
  198. pay_method = fields.Many2one('pay.method',
  199. string='付款方式',
  200. ondelete='restrict')
  201. term_id = fields.Many2one('core.value', "贸易条款",
  202. domain=[('type', '=', 'price_term')],
  203. context={'type': 'price_term'}
  204. )
  205. pol = fields.Char('起运港')
  206. pod = fields.Char('目的港')
  207. express_type = fields.Char('承运商')
  208. money_order_id = fields.Many2one(
  209. 'money.order',
  210. '预收款单',
  211. readonly=True,
  212. copy=False,
  213. help='输入预收款确认时产生的预收款单')
  214. net_weight = fields.Float(
  215. string='净重合计', compute='_compute_net_weight', store=True)
  216. invoice_ids = fields.One2many(
  217. 'money.invoice', compute='_compute_invoice', string='Invoices')
  218. invoice_count = fields.Integer(
  219. compute='_compute_invoice', string='Invoices Count', default=0)
  220. details = fields.Html('明细',compute='_compute_details')
  221. paid_no_goods = fields.Boolean('已收款未发货',compute="_compute_paid_no_goods",store=True)
  222. goods_id = fields.Many2one(
  223. 'goods', related='line_ids.goods_id', string='商品') #用于在列表上根据商品搜索
  224. project_id = fields.Many2one('project', 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. 'project_id': self.project_id.id,
  398. })
  399. if self.type == 'sell':
  400. delivery_id.write({'line_out_ids': [
  401. (0, 0, line) for line in delivery_line]})
  402. else:
  403. delivery_id.write({'line_in_ids': [
  404. (0, 0, line) for line in delivery_line]})
  405. return delivery_id
  406. def sell_generate_delivery(self):
  407. '''由销售订单生成销售发货单'''
  408. self.ensure_one()
  409. delivery_line = [] # 销售发货单行
  410. for line in self.line_ids:
  411. # 如果订单部分出库,则点击此按钮时生成剩余数量的出库单
  412. to_out = line.quantity - line.quantity_out
  413. if to_out <= 0:
  414. continue
  415. if line.goods_id.force_batch_one:
  416. i = 0
  417. while i < to_out:
  418. i += 1
  419. delivery_line.append(
  420. self.get_delivery_line(line, single=True))
  421. else:
  422. delivery_line.append(
  423. self.get_delivery_line(line, single=False))
  424. if not delivery_line:
  425. return {}
  426. self._generate_delivery(delivery_line)
  427. return {}
  428. @api.depends('delivery_ids')
  429. def _compute_invoice(self):
  430. for order in self:
  431. money_invoices = self.env['money.invoice'].search([
  432. ('name', '=', order.name)])
  433. order.invoice_ids = not money_invoices and order.delivery_ids.mapped('invoice_id') or money_invoices + order.delivery_ids.mapped('invoice_id')
  434. order.invoice_count = len(order.invoice_ids.ids)
  435. def action_view_invoice(self):
  436. self.ensure_one()
  437. if self.invoice_count == 0:
  438. return False
  439. action = {
  440. 'name': '结算单(客户发票)',
  441. 'type': 'ir.actions.act_window',
  442. 'view_mode': 'form',
  443. 'res_model': 'money.invoice',
  444. 'view_id': False,
  445. 'target': 'current',
  446. }
  447. invoice_ids = self.invoice_ids.ids
  448. # choose the view_mode accordingly
  449. action['domain'] = "[('id','in',[" + \
  450. ','.join(map(str, invoice_ids)) + "])]"
  451. action['view_mode'] = 'list'
  452. return action
  453. def action_view_delivery(self):
  454. '''
  455. This function returns an action that display existing deliverys of given sells order ids.
  456. When only one found, show the delivery immediately.
  457. '''
  458. self.ensure_one()
  459. action = {
  460. 'name': '销售发货单',
  461. 'type': 'ir.actions.act_window',
  462. 'view_mode': 'form',
  463. 'res_model': 'sell.delivery',
  464. 'view_id': False,
  465. 'target': 'current',
  466. }
  467. delivery_ids = [delivery.id for delivery in self.delivery_ids if not delivery.is_return]
  468. if len(delivery_ids) > 1:
  469. action['domain'] = "[('id','in',[" + \
  470. ','.join(map(str, delivery_ids)) + "])]"
  471. action['view_mode'] = 'list,form'
  472. elif len(delivery_ids) == 1:
  473. view_id = self.env.ref('sell.sell_delivery_form').id
  474. action['views'] = [(view_id, 'form')]
  475. action['res_id'] = delivery_ids and delivery_ids[0] or False
  476. return action
  477. def action_view_return(self):
  478. '''
  479. 该销售订单对应的退货单
  480. '''
  481. self.ensure_one()
  482. action = {
  483. 'name': '销售退货单',
  484. 'type': 'ir.actions.act_window',
  485. 'view_type': 'form',
  486. 'view_mode': 'form',
  487. 'res_model': 'sell.delivery',
  488. 'view_id': False,
  489. 'target': 'current',
  490. }
  491. list_view_id = self.env.ref('sell.sell_return_list').id
  492. form_view_id = self.env.ref('sell.sell_return_form').id
  493. delivery_ids = [delivery.id for delivery in self.delivery_ids if delivery.is_return]
  494. if len(delivery_ids) > 1:
  495. action['domain'] = "[('id','in',[" + \
  496. ','.join(map(str, delivery_ids)) + "])]"
  497. action['view_mode'] = 'list,form'
  498. action['views'] = [(list_view_id, 'list'), (form_view_id, 'form')]
  499. elif len(delivery_ids) == 1:
  500. action['views'] = [(form_view_id, 'form')]
  501. action['res_id'] = delivery_ids and delivery_ids[0] or False
  502. return action
  503. class SellOrderLine(models.Model):
  504. _name = 'sell.order.line'
  505. _description = '销售订单明细'
  506. @api.depends('goods_id')
  507. def _compute_using_attribute(self):
  508. '''返回订单行中商品是否使用属性'''
  509. for l in self:
  510. l.using_attribute = l.goods_id.attribute_ids and True or False
  511. @api.depends('quantity', 'price_taxed', 'discount_amount', 'tax_rate')
  512. def _compute_all_amount(selfs):
  513. '''当订单行的数量、含税单价、折扣额、税率改变时,改变销售金额、税额、价税合计'''
  514. for self in selfs:
  515. if self.order_id.currency_id.id == self.env.user.company_id.currency_id.id:
  516. self.subtotal = self.price_taxed * self.quantity - self.discount_amount # 价税合计
  517. self.tax_amount = self.subtotal / \
  518. (100 + self.tax_rate) * self.tax_rate # 税额
  519. self.amount = self.subtotal - self.tax_amount # 金额
  520. else:
  521. rate_silent = self.env['res.currency'].get_rate_silent(
  522. self.order_id.date, self.order_id.currency_id.id) or 1
  523. if not self.order_id.pay_base_currency:
  524. rate_silent = 1
  525. self.subtotal = (self.price_taxed * self.quantity -
  526. self.discount_amount) * rate_silent # 价税合计
  527. self.tax_amount = self.subtotal / \
  528. (100 + self.tax_rate) * self.tax_rate # 税额
  529. self.amount = self.subtotal - self.tax_amount # 本位币金额
  530. @api.onchange('price', 'tax_rate')
  531. def onchange_price(self):
  532. '''当订单行的不含税单价改变时,改变含税单价。
  533. 如果将含税价改为99,则self.price计算出来为84.62,price=99/1.17,
  534. 跟84.62保留相同位数比较时是相等的,这种情况则保留含税价不变,
  535. 这样处理是为了使得修改含税价时不再重新计算含税价。
  536. '''
  537. _logger.info('单价或税率发生变化')
  538. price = self.price_taxed / (1 + self.tax_rate * 0.01) # 不含税单价
  539. decimal = self.env.ref('core.decimal_price')
  540. if float_compare(price, self.price, precision_digits=decimal.digits) != 0:
  541. self.price_taxed = self.price * (1 + self.tax_rate * 0.01)
  542. order_id = fields.Many2one('sell.order', '订单编号', index=True,
  543. required=True, ondelete='cascade',
  544. help='关联订单的编号')
  545. partner_id = fields.Many2one(
  546. 'partner',
  547. string="客户",
  548. related='order_id.partner_id',
  549. store=True)
  550. goods_id = fields.Many2one('goods',
  551. '商品',
  552. required=True,
  553. ondelete='restrict',
  554. help='商品')
  555. using_attribute = fields.Boolean('使用属性', compute=_compute_using_attribute,
  556. help='商品是否使用属性')
  557. attribute_id = fields.Many2one('attribute', '属性',
  558. ondelete='restrict',
  559. domain="[('goods_id', '=', goods_id)]",
  560. help='商品的属性,当商品有属性时,该字段必输')
  561. uom_id = fields.Many2one('uom', '单位', ondelete='restrict',
  562. help='商品计量单位')
  563. quantity = fields.Float('数量',
  564. default=1,
  565. required=True,
  566. digits='Quantity',
  567. help='下单数量')
  568. quantity_out = fields.Float('已执行数量', copy=False,
  569. digits='Quantity',
  570. help='销售订单产生的发货单/退货单已执行数量')
  571. price = fields.Float('销售单价',
  572. store=True,
  573. digits='Price',
  574. help='不含税单价,由含税单价计算得出')
  575. price_taxed = fields.Float('含税单价',
  576. digits='Price',
  577. help='含税单价,取商品零售价')
  578. discount_rate = fields.Float('折扣率%',
  579. help='折扣率')
  580. discount_amount = fields.Float('折扣额',
  581. help='输入折扣率后自动计算得出,也可手动输入折扣额')
  582. amount = fields.Float('金额',
  583. compute=_compute_all_amount,
  584. store=True,
  585. digits='Amount',
  586. help='金额 = 价税合计 - 税额')
  587. tax_rate = fields.Float('税率(%)',
  588. help='税率')
  589. tax_amount = fields.Float('税额',
  590. compute=_compute_all_amount,
  591. store=True,
  592. digits='Amount',
  593. help='税额')
  594. subtotal = fields.Float('价税合计',
  595. compute=_compute_all_amount,
  596. store=True,
  597. digits='Amount',
  598. help='含税单价 乘以 数量')
  599. note = fields.Char('备注',
  600. help='本行备注')
  601. company_id = fields.Many2one(
  602. 'res.company', string='公司',
  603. change_default=True,
  604. default=lambda self: self.env.company)
  605. # 销售订单行上增加订单的重要字段以便销售订单明细表界面可针对其筛选分组
  606. order_date = fields.Date(related='order_id.date', string='订单日期', store=True)
  607. order_state = fields.Selection(related='order_id.state', string='订单状态', store=True)
  608. order_currency = fields.Many2one('res.currency', related='order_id.currency_id', string='订单币种', store=True)
  609. quantity_todo = fields.Float(
  610. '未执行数量', compute="_compute_quantity_todo",
  611. store=True, digits='Quantity')
  612. @api.depends('quantity_out', 'quantity')
  613. def _compute_quantity_todo(self):
  614. for s in self:
  615. s.quantity_todo = s.quantity - s.quantity_out
  616. @api.onchange('goods_id')
  617. def onchange_warehouse_id(self):
  618. '''当订单行的仓库变化时,带出定价策略中的折扣率'''
  619. if self.order_id.warehouse_id and self.goods_id:
  620. partner = self.order_id.partner_id
  621. warehouse = self.order_id.warehouse_id
  622. goods = self.goods_id
  623. date = self.order_id.date
  624. pricing = self.env['pricing'].get_pricing_id(
  625. partner, warehouse, goods, date)
  626. if pricing:
  627. self.discount_rate = pricing.discount_rate
  628. else:
  629. self.discount_rate = 0
  630. @api.onchange('goods_id')
  631. def onchange_goods_id(self):
  632. '''当订单行的商品变化时,带出商品上的单位、默认仓库、价格、税率'''
  633. if self.goods_id:
  634. self.uom_id = self.goods_id.uom_id
  635. self.price = self.goods_id.price
  636. self.tax_rate = self.goods_id.get_tax_rate(self.goods_id, self.order_id.partner_id, 'sell')
  637. @api.onchange('quantity', 'price_taxed', 'discount_rate')
  638. def onchange_discount_rate(self):
  639. '''当数量、单价或优惠率发生变化时,优惠金额发生变化'''
  640. _logger.info('数量、含税价、折扣率发生变化')
  641. self.price = self.price_taxed / (1 + self.tax_rate * 0.01)
  642. self.discount_amount = self.quantity * self.price \
  643. * self.discount_rate * 0.01
  644. @api.constrains('tax_rate')
  645. def _check_tax_rate(selfs):
  646. for self in selfs:
  647. if self.tax_rate > 100:
  648. raise UserError('税率不能输入超过100的数!\n输入税率:%s' % self.tax_rate)
  649. if self.tax_rate < 0:
  650. raise UserError('税率不能输入负数\n 输入税率:%s' % self.tax_rate)
  651. class ApproveMultiSellOrder(models.TransientModel):
  652. _name = "approve.multi.sell.order"
  653. _description = '批量确认销售订单'
  654. def set_default_note(self):
  655. """
  656. 设置默认值, 用来确认要批量确认的订单
  657. """
  658. context = self.env.context
  659. order_names = [order.name for order in self.env['sell.order'].browse(context.get('active_ids'))]
  660. return '-'.join(order_names)
  661. note = fields.Char('本次处理销售订单', default=set_default_note, readonly=True)
  662. @api.model
  663. def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
  664. """ 根据内容判断 报出错误 """
  665. res = super(ApproveMultiSellOrder, self).fields_view_get(view_id, view_type, toolbar=toolbar, submenu=False)
  666. orders = self.env['sell.order'].browse(self.env.context.get('active_ids'))
  667. done_lists = ''
  668. for order in orders:
  669. if order.state == 'done':
  670. done_lists += order.name
  671. if done_lists:
  672. raise UserError('销售订单 ' + done_lists + ' 已确认!')
  673. return res
  674. def approve_sell_order(self):
  675. """ 确认销售订单 """
  676. for order in self.env['sell.order'].search([('id', 'in', self.env.context.get('active_ids'))]):
  677. order.sell_order_done()
上海开阖软件有限公司 沪ICP备12045867号-1