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

669 行
31KB

  1. from odoo import fields, models, api
  2. import datetime
  3. from odoo.exceptions import UserError
  4. from odoo.tools import float_compare, float_is_zero
  5. ISODATEFORMAT = '%Y-%m-%d'
  6. class SellDelivery(models.Model):
  7. _name = 'sell.delivery'
  8. _inherits = {'wh.move': 'sell_move_id'}
  9. _inherit = ['mail.thread', 'barcodes.barcode_events_mixin', 'mail.activity.mixin']
  10. _description = '销售发货单'
  11. _order = 'date desc, id desc'
  12. @api.depends('line_out_ids.subtotal', 'discount_amount', 'partner_cost',
  13. 'receipt', 'partner_id', 'line_in_ids.subtotal')
  14. def _compute_all_amount(selfs):
  15. '''当优惠金额改变时,改变成交金额'''
  16. for self in selfs:
  17. total = 0
  18. if self.line_out_ids:
  19. # 发货时优惠前总金
  20. total = sum(line.subtotal for line in self.line_out_ids)
  21. elif self.line_in_ids:
  22. # 退货时优惠前总金额
  23. total = sum(line.subtotal for line in self.line_in_ids)
  24. self.amount = total - self.discount_amount
  25. @api.depends('is_return', 'invoice_id.reconciled', 'invoice_id.amount')
  26. def _get_sell_money_state(selfs):
  27. '''返回收款状态'''
  28. for self in selfs:
  29. if not self.is_return:
  30. if self.invoice_id.reconciled == 0:
  31. self.money_state = '未收款'
  32. elif self.invoice_id.reconciled < self.invoice_id.amount:
  33. self.money_state = '部分收款'
  34. elif self.invoice_id.reconciled == self.invoice_id.amount:
  35. self.money_state = '全部收款'
  36. # 返回退款状态
  37. if self.is_return:
  38. if self.invoice_id.reconciled == 0:
  39. self.return_state = '未退款'
  40. elif abs(self.invoice_id.reconciled) < abs(self.invoice_id.amount):
  41. self.return_state = '部分退款'
  42. elif self.invoice_id.reconciled == self.invoice_id.amount:
  43. self.return_state = '全部退款'
  44. currency_id = fields.Many2one('res.currency', '外币币别', readonly=True,
  45. help='外币币别')
  46. sell_move_id = fields.Many2one('wh.move', '发货单', required=True,
  47. ondelete='cascade',
  48. help='发货单号')
  49. is_return = fields.Boolean('是否退货', default=lambda self:
  50. self.env.context.get('is_return'),
  51. help='是否为退货类型')
  52. order_id = fields.Many2one('sell.order', '订单号', copy=False,
  53. ondelete='cascade',
  54. help='产生发货单/退货单的销售订单')
  55. invoice_id = fields.Many2one('money.invoice', '发票号',
  56. copy=False, ondelete='set null',
  57. help='产生的发票号')
  58. date_due = fields.Date('到期日期', copy=False,
  59. default=lambda self: fields.Date.context_today(
  60. self),
  61. help='收款截止日期')
  62. discount_rate = fields.Float('优惠率(%)',
  63. help='整单优惠率')
  64. discount_amount = fields.Float('优惠金额',
  65. digits='Amount',
  66. help='整单优惠金额,可由优惠率自动计算得出,也可手动输入')
  67. amount = fields.Float('成交金额', compute=_compute_all_amount,
  68. store=True, readonly=True,
  69. digits='Amount',
  70. help='总金额减去优惠金额')
  71. partner_cost = fields.Float('客户承担费用',
  72. digits='Amount',
  73. help='客户承担费用')
  74. receipt = fields.Float('本次收款',
  75. digits='Amount',
  76. help='本次收款金额')
  77. bank_account_id = fields.Many2one('bank.account',
  78. '结算账户', ondelete='restrict',
  79. help='用来核算和监督企业与其他单位或个人之间的债权债务的结算情况')
  80. cost_line_ids = fields.One2many('cost.line', 'sell_id', '销售费用',
  81. copy=False,
  82. help='销售费用明细行')
  83. money_state = fields.Char('收款状态', compute=_get_sell_money_state,
  84. store=True, default='未收款',
  85. help="销售发货单的收款状态", index=True, copy=False)
  86. return_state = fields.Char('退款状态', compute=_get_sell_money_state,
  87. store=True, default='未退款',
  88. help="销售退货单的退款状态", index=True, copy=False)
  89. contact = fields.Char('联系人',
  90. help='客户方的联系人')
  91. address_id = fields.Many2one('partner.address', '联系人地址',
  92. domain="[('partner_id','=',partner_id)]",
  93. help='联系地址')
  94. mobile = fields.Char('手机',
  95. help='联系手机')
  96. origin_id = fields.Many2one('sell.delivery', '来源单据')
  97. voucher_id = fields.Many2one('voucher', '出库凭证', readonly=True,
  98. help='发货时产生的出库凭证')
  99. money_order_id = fields.Many2one(
  100. 'money.order',
  101. '收款单',
  102. readonly=True,
  103. copy=False,
  104. help='输入本次收款确认时产生的收款单')
  105. project_id = fields.Many2one('project', string='项目')
  106. def set_today(self):
  107. self.date = fields.Date.today()
  108. @api.onchange('address_id')
  109. def onchange_address_id(self):
  110. ''' 选择地址填充 联系人、电话 '''
  111. if self.address_id:
  112. self.contact = self.address_id.contact
  113. self.mobile = self.address_id.mobile
  114. @api.onchange('partner_id')
  115. def onchange_partner_id(self):
  116. '''选择客户带出其默认地址信息'''
  117. if self.partner_id:
  118. self.contact = self.partner_id.contact
  119. self.mobile = self.partner_id.mobile
  120. for child in self.partner_id.child_ids:
  121. if child.is_default_add:
  122. self.address_id = child.id
  123. if self.partner_id.child_ids and not any([child.is_default_add for child in self.partner_id.child_ids]):
  124. partners_add = self.env['partner.address'].search(
  125. [('partner_id', '=', self.partner_id.id)], order='id')
  126. self.address_id = partners_add[0].id
  127. for line in self.line_out_ids:
  128. line.tax_rate = line.goods_id.get_tax_rate(line.goods_id, self.partner_id, 'sell')
  129. address_list = [
  130. child_list.id for child_list in self.partner_id.child_ids]
  131. if address_list:
  132. return {'domain': {'address_id': [('id', 'in', address_list)]}}
  133. else:
  134. self.address_id = False
  135. @api.onchange('discount_rate', 'line_in_ids', 'line_out_ids')
  136. def onchange_discount_rate(self):
  137. '''当优惠率或订单行发生变化时,单据优惠金额发生变化'''
  138. total = 0
  139. if self.line_out_ids:
  140. # 发货时优惠前总金额
  141. total = sum(line.subtotal for line in self.line_out_ids)
  142. elif self.line_in_ids:
  143. # 退货时优惠前总金额
  144. total = sum(line.subtotal for line in self.line_in_ids)
  145. if self.discount_rate:
  146. self.discount_amount = total * self.discount_rate * 0.01
  147. def get_move_origin(self, vals):
  148. return self._name + (self.env.context.get('is_return') and '.return'
  149. or '.sell')
  150. @api.model_create_multi
  151. def create(self, vals_list):
  152. '''创建销售发货单时生成有序编号'''
  153. if not self.env.context.get('is_return'):
  154. name = self._name
  155. else:
  156. name = 'sell.return'
  157. for vals in vals_list:
  158. if vals.get('name', '/') == '/':
  159. vals['name'] = self.env['ir.sequence'].next_by_code(name) or '/'
  160. vals.update({
  161. 'origin': self.get_move_origin(vals),
  162. 'finance_category_id': self.env.ref('finance.categ_sell_goods').id,
  163. })
  164. return super(SellDelivery, self).create(vals_list)
  165. def unlink(self):
  166. for delivery in self:
  167. delivery.sell_move_id.unlink()
  168. def goods_inventory(self, vals):
  169. """
  170. 审核时若仓库中商品不足,则产生补货向导生成其他入库单并审核。
  171. :param vals: 创建其他入库单需要的字段及取值信息构成的字典
  172. :return:
  173. """
  174. auto_in = self.env['wh.in'].create(vals)
  175. line_ids = [line.id for line in auto_in.line_in_ids]
  176. self.with_context({'wh_in_line_ids': line_ids}).sell_delivery_done()
  177. return True
  178. def _wrong_delivery_done(self):
  179. self.ensure_one()
  180. '''审核时不合法的给出报错'''
  181. if self.state == 'done':
  182. raise UserError('请不要重复发货')
  183. for line in self.line_in_ids:
  184. if line.goods_qty <= 0 or line.price_taxed < 0:
  185. raise UserError('商品 %s 的数量和商品含税单价不能小于0!' % line.goods_id.name)
  186. if not self.bank_account_id and self.receipt:
  187. raise UserError('收款额不为空时,请选择结算账户!')
  188. decimal_amount = self.env.ref('core.decimal_amount')
  189. if float_compare(self.receipt, self.amount + self.partner_cost, precision_digits=decimal_amount.digits) == 1:
  190. raise UserError('本次收款金额不能大于成交金额!\n本次收款金额:%s 成交金额:%s' %
  191. (self.receipt, self.amount + self.partner_cost))
  192. # TODO:客户有自己的发货时检查信用额度的逻辑,这里暂时去掉gooderp原生的检查
  193. return
  194. # 发库单/退货单 计算客户的 本次发货金额+客户应收余额 是否小于客户信用额度, 否则报错
  195. if not self.is_return:
  196. amount = self.amount + self.partner_cost
  197. if self.partner_id.credit_limit != 0:
  198. if float_compare(amount - self.receipt + self.partner_id.receivable, self.partner_id.credit_limit,
  199. precision_digits=decimal_amount.digits) == 1:
  200. raise UserError('本次发货金额 + 客户应收余额 - 本次收款金额 不能大于客户信用额度!\n\
  201. 本次发货金额:%s\n 客户应收余额:%s\n 本次收款金额:%s\n客户信用额度:%s' % (
  202. amount, self.partner_id.receivable, self.receipt, self.partner_id.credit_limit))
  203. def _line_qty_write(self):
  204. if self.order_id:
  205. for line in self.line_in_ids:
  206. if self.order_id.type == 'return':
  207. line.sell_line_id.quantity_out += line.goods_qty
  208. else:
  209. line.sell_line_id.quantity_out -= line.goods_qty
  210. for line in self.line_out_ids:
  211. decimal_quantity = self.env.ref('core.decimal_quantity')
  212. if float_compare(
  213. line.sell_line_id.quantity_out + line.goods_qty,
  214. line.sell_line_id.quantity,
  215. decimal_quantity.digits) == 1:
  216. if not line.goods_id.excess:
  217. raise UserError('%s发货数量大于订单数量' % line.goods_id.name)
  218. line.sell_line_id.write({'quantity_out':line.sell_line_id.quantity_out + line.goods_qty})
  219. return
  220. def _get_invoice_vals(self, partner_id, category_id, date, amount, tax_amount):
  221. '''返回创建 money_invoice 时所需数据'''
  222. return {
  223. 'move_id': self.sell_move_id.id,
  224. 'name': self.name,
  225. 'partner_id': partner_id.id,
  226. 'pay_method': self.order_id.pay_method.id,
  227. 'category_id': category_id.id,
  228. 'date': date,
  229. 'amount': amount,
  230. 'reconciled': 0,
  231. 'to_reconcile': amount,
  232. 'tax_amount': tax_amount,
  233. 'date_due': self.date_due,
  234. 'state': 'draft',
  235. 'currency_id': self.currency_id.id,
  236. }
  237. def _delivery_make_invoice(self):
  238. '''发货单/退货单 生成结算单'''
  239. if not self.is_return:
  240. amount = self.amount + self.partner_cost
  241. tax_amount = sum(line.tax_amount for line in self.line_out_ids)
  242. else:
  243. amount = -(self.amount + self.partner_cost)
  244. tax_amount = - sum(line.tax_amount for line in self.line_in_ids)
  245. category = self.env.ref('money.core_category_sale')
  246. invoice_id = False
  247. if not float_is_zero(amount, 2):
  248. invoice_id = self.env['money.invoice'].create(
  249. self._get_invoice_vals(
  250. self.partner_id, category, self.date, amount, tax_amount)
  251. )
  252. return invoice_id
  253. def _sell_amount_to_invoice(self):
  254. '''销售费用产生结算单'''
  255. invoice_id = False
  256. if sum(cost_line.amount for cost_line in self.cost_line_ids) > 0:
  257. for line in self.cost_line_ids:
  258. if not float_is_zero(line.amount, 2):
  259. invoice_id = self.env['money.invoice'].create(
  260. self._get_invoice_vals(
  261. line.partner_id, line.category_id, self.date, line.amount + line.tax, line.tax)
  262. )
  263. return invoice_id
  264. def _make_money_order(self, invoice_id, amount, this_reconcile):
  265. '''生成收款单'''
  266. categ = self.env.ref('money.core_category_sale')
  267. money_lines = [{
  268. 'bank_id': self.bank_account_id.id,
  269. 'amount': this_reconcile,
  270. }]
  271. source_lines = [{
  272. 'name': invoice_id and invoice_id.id,
  273. 'category_id': categ.id,
  274. 'date': invoice_id and invoice_id.date,
  275. 'amount': amount,
  276. 'reconciled': 0.0,
  277. 'to_reconcile': amount,
  278. 'this_reconcile': this_reconcile,
  279. }]
  280. rec = self.with_context(type='get')
  281. money_order = rec.env['money.order'].create({
  282. 'partner_id': self.partner_id.id,
  283. 'date': self.date,
  284. 'line_ids': [(0, 0, line) for line in money_lines],
  285. 'source_ids': [(0, 0, line) for line in source_lines] if invoice_id.state == 'done' else False,
  286. 'amount': amount,
  287. 'reconciled': this_reconcile,
  288. 'to_reconcile': amount,
  289. 'state': 'draft',
  290. 'origin_name': self.name,
  291. 'sell_id': self.order_id.id,
  292. })
  293. return money_order
  294. def _create_voucher_line(self, account_id, debit, credit, voucher, goods_id, goods_qty):
  295. """
  296. 创建凭证明细行
  297. :param account_id: 科目
  298. :param debit: 借方
  299. :param credit: 贷方
  300. :param voucher: 凭证
  301. :param goods_id: 商品
  302. :return:
  303. """
  304. voucher_line = self.env['voucher.line'].create({
  305. 'name': '%s ' % (self.name),
  306. 'account_id': account_id and account_id.id,
  307. 'partner_id': not goods_id and self.partner_id.id,
  308. 'debit': debit,
  309. 'credit': credit,
  310. 'voucher_id': voucher and voucher.id,
  311. 'goods_qty': goods_qty,
  312. 'goods_id': goods_id and goods_id.id,
  313. 'auxiliary_id': self.project_id.auxiliary_id.id,
  314. })
  315. return voucher_line
  316. def create_voucher(self):
  317. '''
  318. 销售发货单、退货单审核时生成会计凭证
  319. 借:主营业务成本(核算分类上会计科目)
  320. 贷:库存商品(商品分类上会计科目)
  321. 当一张发货单有多个商品的时候,按对应科目汇总生成多个贷方凭证行。
  322. 退货单生成的金额为负
  323. '''
  324. self.ensure_one()
  325. voucher = self.env['voucher'].create({'date': self.date, 'ref': '%s,%s' % (self._name, self.id)})
  326. sum_amount = 0
  327. line_ids = self.is_return and self.line_in_ids or self.line_out_ids
  328. for line in line_ids: # 发货单/退货单明细
  329. cost = self.is_return and -line.cost or line.cost
  330. if not cost:
  331. continue # 缺货审核发货单时不产生出库凭证
  332. else: # 贷方明细
  333. sum_amount += cost
  334. self._create_voucher_line(line.goods_id.category_id.account_id,
  335. 0, cost, voucher, line.goods_id, line.goods_qty)
  336. if sum_amount: # 借方明细
  337. self._create_voucher_line(self.sell_move_id.finance_category_id.account_id,
  338. sum_amount, 0, voucher, False, 0)
  339. if len(voucher.line_ids) > 0:
  340. voucher.voucher_done()
  341. return voucher
  342. else:
  343. voucher.unlink()
  344. def auto_reconcile_sell_order(self):
  345. ''' 预收款与结算单自动核销 '''
  346. self.ensure_one()
  347. all_delivery_amount = 0
  348. for delivery in self.order_id.delivery_ids:
  349. all_delivery_amount += delivery.amount
  350. if (self.order_id.received_amount and self.order_id.received_amount == all_delivery_amount and
  351. not self.env.user.company_id.draft_invoice): #根据发票确认应收应付勾选时不自动核销
  352. adv_pay_result = []
  353. receive_source_result = []
  354. # 预收款
  355. adv_pay_orders = self.env['money.order'].search([('partner_id', '=', self.partner_id.id),
  356. ('type', '=', 'get'),
  357. ('state', '=', 'done'),
  358. ('to_reconcile',
  359. '!=', 0),
  360. ('sell_id', '=', self.order_id.id)])
  361. for order in adv_pay_orders:
  362. adv_pay_result.append((0, 0, {'name': order.id,
  363. 'amount': order.amount,
  364. 'date': order.date,
  365. 'reconciled': order.reconciled,
  366. 'to_reconcile': order.to_reconcile,
  367. 'this_reconcile': order.to_reconcile,
  368. }))
  369. # 结算单
  370. receive_source_name = [
  371. delivery.name for delivery in self.order_id.delivery_ids]
  372. receive_source_orders = self.env['money.invoice'].search([('category_id.type', '=', 'income'),
  373. ('partner_id', '=',
  374. self.partner_id.id),
  375. ('to_reconcile',
  376. '!=', 0),
  377. ('name', 'in', receive_source_name)])
  378. for invoice in receive_source_orders:
  379. receive_source_result.append((0, 0, {
  380. 'name': invoice.id,
  381. 'category_id': invoice.category_id.id,
  382. 'amount': invoice.amount,
  383. 'date': invoice.date,
  384. 'reconciled': invoice.reconciled,
  385. 'to_reconcile': invoice.to_reconcile,
  386. 'date_due': invoice.date_due,
  387. 'this_reconcile': invoice.to_reconcile,
  388. }))
  389. # 创建核销单
  390. reconcile_order = self.env['reconcile.order'].create({
  391. 'partner_id': self.partner_id.id,
  392. 'business_type': 'adv_pay_to_get',
  393. 'advance_payment_ids': adv_pay_result,
  394. 'receivable_source_ids': receive_source_result,
  395. 'note': '自动核销',
  396. })
  397. reconcile_order.reconcile_order_done() # 自动审核
  398. def sell_delivery_done(self):
  399. '''审核销售发货单/退货单,更新本单的收款状态/退款状态,并生成结算单和收款单'''
  400. for record in self:
  401. record._wrong_delivery_done()
  402. # 库存不足 生成零的
  403. if self.env.user.company_id.is_enable_negative_stock:
  404. result_vals = self.env['wh.move'].create_zero_wh_in(
  405. record, record._name)
  406. if result_vals:
  407. return result_vals
  408. # 调用wh.move中审核方法,更新审核人和审核状态
  409. record.sell_move_id.approve_order()
  410. # 将发货/退货数量写入销售订单行
  411. if record.order_id:
  412. record._line_qty_write()
  413. voucher = False
  414. # 创建出库的会计凭证,生成盘盈的入库单的不产生出库凭证
  415. if not self.env.user.company_id.endmonth_generation_cost:
  416. voucher = record.create_voucher()
  417. # 发货单/退货单 生成结算单
  418. invoice_id = record._delivery_make_invoice()
  419. # 销售费用产生结算单
  420. record._sell_amount_to_invoice()
  421. # 生成收款单,并审核
  422. money_order = False
  423. if record.receipt:
  424. flag = not record.is_return and 1 or -1
  425. amount = flag * (record.amount + record.partner_cost)
  426. this_reconcile = flag * record.receipt
  427. money_order = record._make_money_order(
  428. invoice_id, amount, this_reconcile)
  429. money_order.money_order_done()
  430. record.write({
  431. 'voucher_id': voucher and voucher.id,
  432. 'invoice_id': invoice_id and invoice_id.id,
  433. 'money_order_id': money_order and money_order.id,
  434. 'state': 'done', # 为保证审批流程顺畅,否则,未审批就可审核
  435. })
  436. # 先收款后发货订单自动核销
  437. self.auto_reconcile_sell_order()
  438. if record.order_id:
  439. # 如果已退货也已退款,不生成新的分单
  440. if record.is_return and record.receipt:
  441. return True
  442. #产生新的销售发货单时,如果已经存在草稿的销售发货单时,先将已经存在的草稿发货单进行删除
  443. self.env['sell.delivery'].search(['&',('state', '=', 'draft'),'&',('order_id','=', record.order_id.id),('is_return', '=', False)]).unlink()
  444. return record.order_id.sell_generate_delivery()
  445. def sell_delivery_draft(self):
  446. '''反审核销售发货单/退货单,更新本单的收款状态/退款状态,并删除生成的结算单、收款单及凭证'''
  447. self.ensure_one()
  448. if self.state == 'draft':
  449. raise UserError('请不要重复撤销 %s' % self._description)
  450. # 查找产生的结算单(除了开给客户的发票还有可能是采购费用发票)
  451. invoice_ids = self.env['money.invoice'].search([('name', '=', self.name)])
  452. for invoice in invoice_ids:
  453. # 不能反审核已核销的发货单
  454. if invoice.reconciled != 0:
  455. raise UserError('发票已核销,不能撤销发货!')
  456. if invoice.state == 'done':
  457. if self.env.company.draft_invoice:
  458. raise UserError('发票已开不可撤销发货')
  459. invoice.money_invoice_draft()
  460. invoice.unlink()
  461. # 删除产生的出库凭证
  462. voucher = self.voucher_id
  463. if voucher and voucher.state == 'done':
  464. voucher.voucher_draft()
  465. voucher.unlink()
  466. self.write({
  467. 'state': 'draft',
  468. })
  469. # 将原始订单中已执行数量清零
  470. if self.order_id:
  471. for line in self.line_out_ids:
  472. line.sell_line_id.quantity_out -= line.goods_qty
  473. for line in self.line_in_ids:
  474. if self.order_id.type == 'return':
  475. line.sell_line_id.quantity_out -= line.goods_qty
  476. else:
  477. line.sell_line_id.quantity_out += line.goods_qty
  478. # 调用wh.move中反审核方法,更新审核人和审核状态
  479. self.sell_move_id.cancel_approved_order()
  480. return True
  481. def sell_to_return(self):
  482. '''销售发货单转化为销售退货单'''
  483. return_goods = {}
  484. return_order_draft = self.search([
  485. ('is_return', '=', True),
  486. ('origin_id', '=', self.id),
  487. ('state', '=', 'draft')
  488. ])
  489. if return_order_draft:
  490. raise UserError('销售发货单存在草稿状态的退货单!')
  491. return_order = self.search([
  492. ('is_return', '=', True),
  493. ('origin_id', '=', self.id),
  494. ('state', '=', 'done')
  495. ])
  496. for order in return_order:
  497. for return_line in order.line_in_ids:
  498. # 用产品、属性、批次做key记录已退货数量
  499. t_key = (return_line.goods_id.id,
  500. return_line.attribute_id.id, return_line.lot)
  501. if return_goods.get(t_key):
  502. return_goods[t_key] += return_line.goods_qty
  503. else:
  504. return_goods[t_key] = return_line.goods_qty
  505. receipt_line = []
  506. for line in self.line_out_ids:
  507. qty = line.goods_qty
  508. l_key = (line.goods_id.id, line.attribute_id.id, line.lot_id.lot)
  509. if return_goods.get(l_key):
  510. qty = qty - return_goods[l_key]
  511. if qty > 0:
  512. dic = {
  513. 'goods_id': line.goods_id.id,
  514. 'attribute_id': line.attribute_id.id,
  515. 'uom_id': line.uom_id.id,
  516. 'warehouse_id': line.warehouse_dest_id.id,
  517. 'warehouse_dest_id': line.warehouse_id.id,
  518. 'goods_qty': qty,
  519. 'sell_line_id': line.sell_line_id.id,
  520. 'price_taxed': line.price_taxed,
  521. 'price': line.price,
  522. 'tax_rate':line.tax_rate,
  523. 'cost_unit': line.cost_unit,
  524. 'cost': line.cost,#退货取不到成本
  525. 'discount_rate': line.discount_rate,
  526. 'discount_amount': line.discount_amount,
  527. 'type': 'in',
  528. }
  529. if line.goods_id.using_batch:
  530. dic.update({'lot': line.lot_id.lot})
  531. receipt_line.append(dic)
  532. if len(receipt_line) == 0:
  533. raise UserError('该订单已全部退货!')
  534. vals = {'partner_id': self.partner_id.id,
  535. 'is_return': True,
  536. 'order_id': self.order_id.id,
  537. 'user_id': self.user_id.id,
  538. 'origin_id': self.id,
  539. 'origin': 'sell.delivery.return',
  540. 'warehouse_dest_id': self.warehouse_id.id,
  541. 'warehouse_id': self.warehouse_dest_id.id,
  542. 'bank_account_id': self.bank_account_id.id,
  543. 'date_due': (datetime.datetime.now()).strftime(ISODATEFORMAT),
  544. 'date': (datetime.datetime.now()).strftime(ISODATEFORMAT),
  545. 'line_in_ids': [(0, 0, line) for line in receipt_line],
  546. 'discount_amount': self.discount_amount,
  547. }
  548. delivery_return = self.with_context(is_return=True).create(vals)
  549. view_id = self.env.ref('sell.sell_return_form').id
  550. name = '销售退货单'
  551. return {
  552. 'name': name,
  553. 'view_mode': 'form',
  554. 'view_id': False,
  555. 'views': [(view_id, 'form')],
  556. 'res_model': 'sell.delivery',
  557. 'type': 'ir.actions.act_window',
  558. 'res_id': delivery_return.id,
  559. 'target': 'current'
  560. }
  561. class WhMoveLine(models.Model):
  562. _inherit = 'wh.move.line'
  563. sell_line_id = fields.Many2one('sell.order.line', '销售单行',
  564. ondelete='cascade',
  565. help='对应的销售订单行')
  566. @api.onchange('warehouse_id', 'goods_id')
  567. def onchange_warehouse_id(self):
  568. '''当订单行的仓库变化时,带出定价策略中的折扣率'''
  569. if self.warehouse_id and self.goods_id:
  570. partner_id = self.env.context.get('default_partner')
  571. partner = self.env['partner'].browse(
  572. partner_id) or self.move_id.partner_id
  573. warehouse = self.warehouse_id
  574. goods = self.goods_id
  575. date = self.env.context.get('default_date') or self.move_id.date
  576. if self.env.context.get('warehouse_type') == 'customer' or \
  577. self.env.context.get('warehouse_dest_type') == 'customer':
  578. pricing = self.env['pricing'].get_pricing_id(
  579. partner, warehouse, goods, date)
  580. if pricing:
  581. self.discount_rate = pricing.discount_rate
  582. else:
  583. self.discount_rate = 0
  584. def _delivery_get_price_and_tax(self):
  585. self.tax_rate = self.env.user.company_id.output_tax_rate
  586. self.price_taxed = self.goods_id.price
  587. if self.env.context.get('order_id'):
  588. line = self.env['sell.order.line'].search([
  589. ('order_id', '=', self.env.context.get('order_id')),
  590. ('goods_id', '=', self.goods_id.id)
  591. ], limit=1)
  592. if line:
  593. self.sell_line_id = line.id
  594. self.uos_id = line.goods_id.uos_id.id
  595. self.uom_id = line.uom_id.id
  596. self.price = line.price
  597. self.price_taxed = line.price_taxed
  598. self.discount_rate = line.discount_rate
  599. self.tax_rate = line.tax_rate
  600. self.plan_date = line.order_id.delivery_date
  601. else:
  602. raise UserError('无此商品的订单行')
  603. @api.onchange('goods_id')
  604. def onchange_goods_id(self):
  605. '''当订单行的商品变化时,带出商品上的零售价,以及公司的销项税'''
  606. self.ensure_one()
  607. is_return = self.env.context.get('default_is_return')
  608. if self.goods_id:
  609. # 如果是销售发货单行 或 销售退货单行
  610. if is_return is not None and \
  611. ((self.type == 'out' and not is_return) or (self.type == 'in' and is_return)):
  612. self._delivery_get_price_and_tax()
  613. return super(WhMoveLine, self).onchange_goods_id()
上海开阖软件有限公司 沪ICP备12045867号-1