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.

806 satır
35KB

  1. # Copyright 2016 上海开阖软件有限公司 (http://www.osbzr.com)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import fields, models, api
  4. from odoo.exceptions import UserError
  5. from datetime import datetime
  6. from odoo.tools import float_compare, float_is_zero
  7. # 采购订单确认状态可选值
  8. BUY_ORDER_STATES = [
  9. ('draft', '草稿'),
  10. ('done', '已确认'),
  11. ('cancel', '已作废')]
  12. # 字段只读状态
  13. READONLY_STATES = {
  14. 'done': [('readonly', True)],
  15. 'cancel': [('readonly', True)],
  16. }
  17. class BuyOrder(models.Model):
  18. _name = "buy.order"
  19. _inherit = ['mail.thread', 'mail.activity.mixin']
  20. _description = "采购订单"
  21. _order = 'date desc, id desc'
  22. def _check_buy_order_manage_access(self):
  23. if not self.env.user.has_group('buy.group_buy'):
  24. raise UserError('只有采购组可以确认或撤销采购订单')
  25. @api.depends('line_ids.subtotal', 'discount_amount')
  26. def _compute_amount(selfs):
  27. '''当订单行和优惠金额改变时,改变成交金额'''
  28. for self in selfs:
  29. total = sum(line.subtotal for line in self.line_ids)
  30. self.amount = total - self.discount_amount
  31. self.untax_amount = sum(line.amount for line in self.line_ids)
  32. self.tax_amount = sum(line.tax_amount for line in self.line_ids)
  33. @api.depends('line_ids.quantity')
  34. def _compute_qty(selfs):
  35. '''当订单行数量改变时,更新总数量'''
  36. for self in selfs:
  37. self.total_qty = sum(line.quantity for line in self.line_ids)
  38. @api.depends('receipt_ids.state')
  39. def _get_buy_goods_state(selfs):
  40. '''返回收货状态'''
  41. for self in selfs:
  42. if all(line.quantity_in == 0 for line in self.line_ids):
  43. if any(r.state == 'draft' for r in self.receipt_ids) or self.state=='draft':
  44. self.goods_state = '未入库'
  45. else:
  46. self.goods_state = '全部作废'
  47. elif any(line.quantity > line.quantity_in for line in self.line_ids):
  48. if any(r.state == 'draft' for r in self.receipt_ids):
  49. self.goods_state = '部分入库'
  50. else:
  51. self.goods_state = '部分入库剩余作废'
  52. else:
  53. self.goods_state = '全部入库'
  54. @api.model
  55. def _default_warehouse_dest_impl(self):
  56. if self.env.context.get('warehouse_dest_type'):
  57. return self.env['warehouse'].get_warehouse_by_type(
  58. self.env.context.get('warehouse_dest_type'), False)
  59. @api.model
  60. def _default_warehouse_dest(self):
  61. '''获取默认调入仓库'''
  62. return self._default_warehouse_dest_impl()
  63. def _get_paid_amount(selfs):
  64. '''计算采购订单付款/退款状态'''
  65. for self in selfs:
  66. if not self.invoice_by_receipt: # 分期付款时
  67. money_invoices = self.env['money.invoice'].search([
  68. ('name', '=', self.name),
  69. ('state', '=', 'done')])
  70. self.paid_amount = sum([invoice.reconciled for invoice in money_invoices])
  71. else:
  72. receipts = self.env['buy.receipt'].search([('order_id', '=', self.id)])
  73. # 采购订单上输入预付款时
  74. money_order_rows = self.env['money.order'].search([('buy_id', '=', self.id),
  75. ('partner_id', '=', self.partner_id.id),
  76. ('state', '=', 'done')])
  77. self.paid_amount = sum([receipt.invoice_id.reconciled for receipt in receipts]) +\
  78. sum([order_row.to_reconcile for order_row in money_order_rows])
  79. @api.depends('receipt_ids')
  80. def _compute_receipt(self):
  81. for order in self:
  82. order.receipt_count = len([receipt for receipt in order.receipt_ids if not receipt.is_return])
  83. order.return_count = len([receipt for receipt in order.receipt_ids if receipt.is_return])
  84. @api.depends('receipt_ids')
  85. def _compute_invoice(self):
  86. for order in self:
  87. money_invoices = self.env['money.invoice'].search([
  88. ('name', '=', order.name)])
  89. order.invoice_ids = not money_invoices and order.receipt_ids.mapped('invoice_id') or money_invoices + order.receipt_ids.mapped('invoice_id')
  90. order.invoice_count = len(order.invoice_ids.ids)
  91. partner_id = fields.Many2one('partner', '供应商',
  92. ondelete='restrict',
  93. help='供应商')
  94. contact = fields.Char('联系人')
  95. address_id = fields.Many2one('partner.address', '地址',
  96. domain="[('partner_id', '=', partner_id)]",
  97. help='联系地址')
  98. date = fields.Date('单据日期',
  99. default=lambda self: fields.Date.context_today(self),
  100. index=True,
  101. copy=False,
  102. help="默认是订单创建日期")
  103. planned_date = fields.Date(
  104. '要求交货日期',
  105. default=lambda self: fields.Date.context_today(
  106. self),
  107. index=True,
  108. copy=False,
  109. help="订单的要求交货日期")
  110. name = fields.Char('单据编号',
  111. index=True,
  112. copy=False,
  113. help="采购订单的唯一编号,当创建时它会自动生成下一个编号。")
  114. type = fields.Selection([('buy', '采购'),
  115. ('return', '退货')],
  116. '类型',
  117. default='buy',
  118. help='采购订单的类型,分为采购或退货')
  119. ref = fields.Char('供应商订单号')
  120. warehouse_dest_id = fields.Many2one('warehouse',
  121. '调入仓库',
  122. required=True,
  123. default=_default_warehouse_dest,
  124. ondelete='restrict',
  125. help='将商品调入到该仓库')
  126. invoice_by_receipt = fields.Boolean(string="按收货结算",
  127. default=True,
  128. help='如未勾选此项,可在资金行里输入付款金额,订单保存后,采购人员可以单击资金行上的【确认】按钮。')
  129. line_ids = fields.One2many('buy.order.line',
  130. 'order_id',
  131. '采购订单行',
  132. copy=True,
  133. help='采购订单的明细行,不能为空')
  134. pay_method = fields.Many2one('pay.method',
  135. string='付款方式',
  136. ondelete='restrict')
  137. note = fields.Text('备注',
  138. help='单据备注')
  139. discount_rate = fields.Float('优惠率(%)',
  140. digits='Amount',
  141. help='整单优惠率')
  142. discount_amount = fields.Float('抹零',
  143. digits='Amount',
  144. help='整单优惠金额,可由优惠率自动计算出来,也可手动输入')
  145. amount = fields.Float('成交金额',
  146. store=True,
  147. compute='_compute_amount',
  148. digits='Amount',
  149. help='总金额减去优惠金额')
  150. untax_amount = fields.Float('不含税合计',
  151. store=True,
  152. compute='_compute_amount',
  153. digits='Amount')
  154. tax_amount = fields.Float('税金合计',
  155. store=True,
  156. compute='_compute_amount',
  157. digits='Amount')
  158. total_qty = fields.Float(string='数量合计', store=True, readonly=True,
  159. compute='_compute_qty',
  160. digits='Quantity',
  161. help='数量总计')
  162. prepayment = fields.Float('预付款',
  163. digits='Amount',
  164. help='输入预付款确认采购订单,会产生一张付款单')
  165. bank_account_id = fields.Many2one('bank.account',
  166. '结算账户',
  167. ondelete='restrict',
  168. help='用来核算和监督企业与其他单位或个人之间的债权债务的结算情况')
  169. approve_uid = fields.Many2one('res.users',
  170. '确认人',
  171. copy=False,
  172. ondelete='restrict',
  173. help='确认单据的人')
  174. state = fields.Selection(BUY_ORDER_STATES,
  175. '确认状态',
  176. readonly=True,
  177. help="采购订单的确认状态",
  178. index=True,
  179. copy=False,
  180. default='draft')
  181. goods_state = fields.Char('收货状态',
  182. compute=_get_buy_goods_state,
  183. default='未入库',
  184. store=True,
  185. help="采购订单的收货状态",
  186. index=True,
  187. copy=False)
  188. cancelled = fields.Boolean('已终止',
  189. help='该单据是否已终止')
  190. pay_ids = fields.One2many("payment.plan",
  191. "buy_id",
  192. string="付款计划",
  193. help='分批付款时使用付款计划')
  194. goods_id = fields.Many2one(
  195. 'goods', related='line_ids.goods_id', string='商品')
  196. receipt_ids = fields.One2many(
  197. 'buy.receipt', 'order_id', string='入库单', copy=False)
  198. receipt_count = fields.Integer(
  199. compute='_compute_receipt', string='入库单数量', default=0)
  200. return_count = fields.Integer(
  201. compute='_compute_receipt', string='退货单数量', default=0)
  202. invoice_ids = fields.One2many(
  203. 'money.invoice', compute='_compute_invoice', string='Invoices')
  204. invoice_count = fields.Integer(
  205. compute='_compute_invoice', string='Invoices Count', default=0)
  206. currency_id = fields.Many2one('res.currency',
  207. '外币币别',
  208. store=True,
  209. related='partner_id.s_category_id.account_id.currency_id',
  210. help='外币币别')
  211. express_type = fields.Char(string='承运商',)
  212. term_id = fields.Many2one('core.value', "贸易条款",
  213. domain=[('type', '=', 'price_term')],
  214. context={'type': 'price_term'})
  215. user_id = fields.Many2one(
  216. 'res.users',
  217. '经办人',
  218. ondelete='restrict',
  219. default=lambda self: self.env.user,
  220. help='单据经办人',
  221. )
  222. company_id = fields.Many2one(
  223. 'res.company',
  224. string='公司',
  225. change_default=True,
  226. default=lambda self: self.env.company)
  227. paid_amount = fields.Float(
  228. '已付金额', compute=_get_paid_amount, readonly=True)
  229. paid_no_goods = fields.Boolean('已付款未到货',compute="_compute_paid_no_goods",store=True)
  230. money_order_id = fields.Many2one(
  231. 'money.order',
  232. '预付款单',
  233. readonly=True,
  234. copy=False,
  235. help='输入预付款确认时产生的预付款单')
  236. details = fields.Html('明细',compute='_compute_details')
  237. project_id = fields.Many2one('project', string='项目')
  238. @api.depends('money_order_id.state','goods_state')
  239. def _compute_paid_no_goods(self):
  240. for o in self:
  241. o.paid_no_goods = False
  242. if o.state == 'done' and o.goods_state == '未入库' and o.paid_amount:
  243. if not all(line.goods_id.no_stock for line in self.line_ids):
  244. o.paid_no_goods = True
  245. @api.depends('line_ids')
  246. def _compute_details(self):
  247. for v in self:
  248. vl = {'col':[],'val':[]}
  249. vl['col'] = ['商品','数量','单价','已收']
  250. for l in v.line_ids:
  251. vl['val'].append([l.goods_id.name,l.quantity,l.price,l.quantity_in])
  252. v.details = v.company_id._get_html_table(vl)
  253. @api.onchange('discount_rate', 'line_ids')
  254. def onchange_discount_rate(self):
  255. '''当优惠率或采购订单行发生变化时,单据优惠金额发生变化'''
  256. total = sum(line.subtotal for line in self.line_ids)
  257. self.discount_amount = total * self.discount_rate * 0.01
  258. @api.onchange('partner_id')
  259. def onchange_partner_id(self):
  260. if self.partner_id:
  261. for line in self.line_ids:
  262. line.tax_rate = line.goods_id.get_tax_rate(line.goods_id, self.partner_id, 'buy')
  263. self.contact = self.partner_id.main_contact
  264. self.pay_method = self.partner_id.pay_method
  265. @api.onchange('address_id')
  266. def onchange_address_id(self):
  267. if self.address_id:
  268. self.contact = self.address_id.contact
  269. def _get_vals(self):
  270. '''返回创建 money_order 时所需数据'''
  271. flag = (self.type == 'buy' and 1 or -1) # 用来标志入库或退货
  272. amount = flag * self.amount
  273. this_reconcile = flag * self.prepayment
  274. money_lines = [{
  275. 'bank_id': self.bank_account_id.id,
  276. 'amount': this_reconcile,
  277. }]
  278. return {
  279. 'partner_id': self.partner_id.id,
  280. 'bank_name': self.partner_id.bank_name,
  281. 'bank_num': self.partner_id.bank_num,
  282. 'date': fields.Date.context_today(self),
  283. 'line_ids':
  284. [(0, 0, line) for line in money_lines],
  285. 'amount': amount,
  286. 'reconciled': this_reconcile,
  287. 'to_reconcile': amount,
  288. 'state': 'draft',
  289. 'origin_name': self.name,
  290. 'buy_id': self.id,
  291. }
  292. def generate_payment_order(self):
  293. '''由采购订单生成付款单'''
  294. # 入库单/退货单
  295. if self.prepayment:
  296. money_order = self.with_context(type='pay').env['money.order'].create(
  297. self._get_vals()
  298. )
  299. return money_order
  300. def buy_order_done(self):
  301. '''确认采购订单'''
  302. self.ensure_one()
  303. self._check_buy_order_manage_access()
  304. if self.state == 'done':
  305. raise UserError('请不要重复确认')
  306. if not self.line_ids:
  307. raise UserError('请输入商品明细行')
  308. for line in self.line_ids:
  309. # 检查属性是否填充,防止无权限人员不填就可以保存
  310. if line.using_attribute and not line.attribute_id:
  311. raise UserError('请输入商品:%s 的属性' % line.goods_id.name)
  312. if line.quantity <= 0 or line.price_taxed < 0:
  313. raise UserError('商品 %s 的数量和含税单价不能小于0' % line.goods_id.name)
  314. if line.tax_amount > 0 and self.currency_id:
  315. raise UserError('外贸免税')
  316. if not self.bank_account_id and self.prepayment:
  317. raise UserError('预付款不为空时,请选择结算账户')
  318. if not self.amount and self.env.context.get('func', '') != 'amount_not_value':
  319. vals = {}
  320. return self.env[self.sudo()._name].with_context(
  321. {'active_model': self.sudo()._name}
  322. ).open_dialog('amount_not_value', {
  323. 'message': '当前采购单价格为0,是否继续进行?',
  324. 'args': [vals],
  325. })
  326. # 采购预付款生成付款单
  327. money_order = self.generate_payment_order()
  328. self.buy_generate_receipt()
  329. self.approve_uid = self._uid
  330. self.write({
  331. 'money_order_id': money_order and money_order.id,
  332. 'state': 'done', # 为保证审批流程顺畅,否则,未审批就可审核
  333. })
  334. def amount_not_value(self, vals):
  335. for line in self.line_ids:
  336. line.onchange_price()
  337. self.buy_order_done()
  338. def buy_order_draft(self):
  339. '''撤销确认采购订单'''
  340. self.ensure_one()
  341. self._check_buy_order_manage_access()
  342. if self.state == 'draft':
  343. raise UserError('请不要重复撤销%s' % self._description)
  344. if any(r.state == 'done' for r in self.receipt_ids):
  345. raise UserError('该采购订单已经收货,不能撤销确认!')
  346. # 查找产生的发票并删除
  347. for inv in self.invoice_ids:
  348. if inv.state == 'done':
  349. raise UserError('该采购订单已经收票,不能撤销确认!')
  350. else:
  351. inv.unlink()
  352. for plan in self.pay_ids:
  353. plan.date_application = ''
  354. # 查找产生的入库单并删除
  355. self.receipt_ids.unlink()
  356. # 查找产生的付款单并撤销确认,删除
  357. for money_order_id in self.env['money.order'].search([('buy_id','=',self.id)]):
  358. if money_order_id.state == 'done':
  359. raise UserError('该采购订单已经付款,不能撤销确认!')
  360. money_order_id.unlink()
  361. self.approve_uid = False
  362. self.state = 'draft'
  363. def get_receipt_line(self, line, single=False):
  364. '''返回采购入库/退货单行'''
  365. self.ensure_one()
  366. qty = 0
  367. discount_amount = 0
  368. if single:
  369. qty = 1
  370. discount_amount = (line.discount_amount /
  371. ((line.quantity - line.quantity_in) or 1))
  372. else:
  373. qty = line.quantity - line.quantity_in
  374. discount_amount = line.discount_amount
  375. return {
  376. 'type': self.type == 'buy' and 'in' or 'out',
  377. 'buy_line_id': line.id,
  378. 'goods_id': line.goods_id.id,
  379. 'attribute_id': line.attribute_id.id,
  380. 'uos_id': line.goods_id.uos_id.id,
  381. 'goods_qty': qty,
  382. 'uom_id': line.uom_id.id,
  383. 'cost_unit': line.price,
  384. 'price': line.price,
  385. 'price_taxed': line.price_taxed,
  386. 'discount_rate': line.discount_rate,
  387. 'discount_amount': discount_amount,
  388. 'tax_rate': line.tax_rate,
  389. 'plan_date':self.planned_date,
  390. }
  391. def _generate_receipt(self, receipt_line):
  392. '''根据明细行生成入库单或退货单'''
  393. # 如果退货,warehouse_dest_id,warehouse_id要调换
  394. warehouse = (self.type == 'buy'
  395. and self.env.ref("warehouse.warehouse_supplier")
  396. or self.warehouse_dest_id)
  397. warehouse_dest = (self.type == 'buy'
  398. and self.warehouse_dest_id
  399. or self.env.ref("warehouse.warehouse_supplier"))
  400. rec = (self.type == 'buy' and self.with_context(is_return=False)
  401. or self.with_context(is_return=True))
  402. receipt_id = rec.env['buy.receipt'].create({
  403. 'partner_id': self.partner_id.id,
  404. 'warehouse_id': warehouse.id,
  405. 'warehouse_dest_id': warehouse_dest.id,
  406. 'date': self.planned_date,
  407. 'date_due': self.planned_date,
  408. 'order_id': self.id,
  409. 'ref': self.ref,
  410. 'origin': 'buy.receipt',
  411. 'discount_rate': self.discount_rate,
  412. 'discount_amount': self.discount_amount,
  413. 'invoice_by_receipt': self.invoice_by_receipt,
  414. 'currency_id': self.currency_id.id,
  415. 'currency_rate': self.env['res.currency'].get_rate_silent(
  416. self.date, self.currency_id.id) or 0,
  417. 'project_id': self.project_id.id,
  418. })
  419. if self.type == 'buy':
  420. receipt_id.write({'line_in_ids': [
  421. (0, 0, line) for line in receipt_line]})
  422. else:
  423. receipt_id.write({'line_out_ids': [
  424. (0, 0, line) for line in receipt_line]})
  425. return receipt_id
  426. def buy_generate_receipt(self):
  427. '''由采购订单生成采购入库/退货单'''
  428. self.ensure_one()
  429. receipt_line = [] # 采购入库/退货单行
  430. for line in self.line_ids:
  431. # 如果订单部分入库,则点击此按钮时生成剩余数量的入库单
  432. to_in = line.quantity - line.quantity_in
  433. if to_in <= 0:
  434. continue
  435. if line.goods_id.force_batch_one:
  436. i = 0
  437. while i < to_in:
  438. i += 1
  439. receipt_line.append(
  440. self.get_receipt_line(line, single=True))
  441. else:
  442. receipt_line.append(self.get_receipt_line(line, single=False))
  443. if not receipt_line:
  444. return {}
  445. self._generate_receipt(receipt_line)
  446. return {}
  447. def action_view_receipt(self):
  448. '''
  449. This function returns an action that display existing picking orders of given purchase order ids.
  450. When only one found, show the picking immediately.
  451. '''
  452. self.ensure_one()
  453. action = {
  454. 'name': '采购入库单',
  455. 'type': 'ir.actions.act_window',
  456. 'view_mode': 'form',
  457. 'res_model': 'buy.receipt',
  458. 'view_id': False,
  459. 'target': 'current',
  460. }
  461. #receipt_ids = sum([order.receipt_ids.ids for order in self], [])
  462. receipt_ids = [receipt.id for receipt in self.receipt_ids if not receipt.is_return]
  463. # choose the view_mode accordingly
  464. if len(receipt_ids) > 1:
  465. action['domain'] = "[('id','in',[" + \
  466. ','.join(map(str, receipt_ids)) + "])]"
  467. action['view_mode'] = 'list,form'
  468. elif len(receipt_ids) == 1:
  469. view_id = self.env.ref('buy.buy_receipt_form').id
  470. action['views'] = [(view_id, 'form')]
  471. action['res_id'] = receipt_ids and receipt_ids[0] or False
  472. return action
  473. def action_view_return(self):
  474. '''
  475. 该采购订单对应的退货单
  476. '''
  477. self.ensure_one()
  478. action = {
  479. 'name': '采购退货单',
  480. 'type': 'ir.actions.act_window',
  481. 'view_mode': 'form',
  482. 'res_model': 'buy.receipt',
  483. 'view_id': False,
  484. 'target': 'current',
  485. }
  486. receipt_ids = [receipt.id for receipt in self.receipt_ids if receipt.is_return]
  487. if len(receipt_ids) > 1:
  488. action['domain'] = "[('id','in',[" + \
  489. ','.join(map(str, receipt_ids)) + "])]"
  490. action['view_mode'] = 'list,form'
  491. elif len(receipt_ids) == 1:
  492. view_id = self.env.ref('buy.buy_return_form').id
  493. action['views'] = [(view_id, 'form')]
  494. action['res_id'] = receipt_ids and receipt_ids[0] or False
  495. return action
  496. def action_view_invoice(self):
  497. '''
  498. This function returns an action that display existing invoices of given purchase order ids( linked/computed via buy.receipt).
  499. When only one found, show the invoice immediately.
  500. '''
  501. self.ensure_one()
  502. if self.invoice_count == 0:
  503. return False
  504. action = {
  505. 'name': '结算单(供应商发票)',
  506. 'type': 'ir.actions.act_window',
  507. 'view_mode': 'form',
  508. 'res_model': 'money.invoice',
  509. 'view_id': False,
  510. 'target': 'current',
  511. }
  512. invoice_ids = self.invoice_ids.ids
  513. action['domain'] = "[('id','in',[" + \
  514. ','.join(map(str, invoice_ids)) + "])]"
  515. action['view_mode'] = 'list'
  516. return action
  517. class BuyOrderLine(models.Model):
  518. _name = 'buy.order.line'
  519. _description = '采购订单明细'
  520. # 根据采购商品的主单位数量,计算该商品的辅助单位数量
  521. @api.depends('quantity', 'goods_id')
  522. def _get_goods_uos_qty(self):
  523. for line in self:
  524. if line.goods_id and line.quantity:
  525. line.goods_uos_qty = line.quantity / line.goods_id.conversion
  526. else:
  527. line.goods_uos_qty = 0
  528. # 根据商品的辅助单位数量,反算出商品的主单位数量
  529. @api.onchange('goods_uos_qty', 'goods_id')
  530. def _inverse_quantity(self):
  531. for line in self:
  532. line.quantity = line.goods_uos_qty * line.goods_id.conversion
  533. @api.depends('goods_id')
  534. def _compute_using_attribute(selfs):
  535. '''返回订单行中商品是否使用属性'''
  536. for self in selfs:
  537. self.using_attribute = self.goods_id.attribute_ids and True or False
  538. @api.depends('quantity', 'price_taxed', 'discount_amount', 'tax_rate')
  539. def _compute_all_amount(selfs):
  540. for self in selfs:
  541. '''当订单行的数量、含税单价、折扣额、税率改变时,改变采购金额、税额、价税合计'''
  542. self.subtotal = self.price_taxed * self.quantity - self.discount_amount # 价税合计
  543. self.tax_amount = self.subtotal / (100 + self.tax_rate) * self.tax_rate # 税额
  544. self.amount = self.subtotal - self.tax_amount # 金额
  545. @api.onchange('price', 'tax_rate')
  546. def onchange_price(self):
  547. '''当订单行的不含税单价改变时,改变含税单价'''
  548. price = self.price_taxed / (1 + self.tax_rate * 0.01) # 不含税单价
  549. decimal = self.env.ref('core.decimal_price')
  550. if float_compare(price, self.price, precision_digits=decimal.digits) != 0:
  551. self.price_taxed = self.price * (1 + self.tax_rate * 0.01)
  552. order_id = fields.Many2one('buy.order',
  553. '订单编号',
  554. index=True,
  555. required=True,
  556. ondelete='cascade',
  557. help='关联订单的编号')
  558. partner_id = fields.Many2one(
  559. 'partner',
  560. string="供应商",
  561. related='order_id.partner_id',
  562. store=True)
  563. goods_id = fields.Many2one('goods',
  564. '商品',
  565. ondelete='restrict',
  566. help='商品')
  567. using_attribute = fields.Boolean('使用属性',
  568. compute=_compute_using_attribute,
  569. help='商品是否使用属性')
  570. attribute_id = fields.Many2one('attribute',
  571. '属性',
  572. ondelete='restrict',
  573. domain="[('goods_id', '=', goods_id)]",
  574. help='商品的属性,当商品有属性时,该字段必输')
  575. goods_uos_qty = fields.Float('辅助数量', digits='Quantity', compute='_get_goods_uos_qty',
  576. inverse='_inverse_quantity', store=True,
  577. help='商品的辅助数量')
  578. uos_id = fields.Many2one('uom', string='辅助单位', ondelete='restrict', readonly=True, help='商品的辅助单位')
  579. uom_id = fields.Many2one('uom',
  580. '单位',
  581. ondelete='restrict',
  582. help='商品计量单位')
  583. quantity = fields.Float('数量',
  584. default=1,
  585. required=True,
  586. digits='Quantity',
  587. help='下单数量')
  588. quantity_in = fields.Float('已执行数量',
  589. copy=False,
  590. digits='Quantity',
  591. help='采购订单产生的入库单/退货单已执行数量')
  592. price = fields.Float('采购单价',
  593. store=True,
  594. digits='Price',
  595. help='不含税单价,由含税单价计算得出')
  596. price_taxed = fields.Float('含税单价',
  597. digits='Price',
  598. help='含税单价,取自商品成本或对应供应商的采购价')
  599. discount_rate = fields.Float('折扣率%',
  600. help='折扣率')
  601. discount_amount = fields.Float('折扣额',
  602. digits='Amount',
  603. help='输入折扣率后自动计算得出,也可手动输入折扣额')
  604. amount = fields.Float('金额',
  605. compute=_compute_all_amount,
  606. store=True,
  607. digits='Amount',
  608. help='金额 = 价税合计 - 税额')
  609. tax_rate = fields.Float('税率(%)',
  610. default=lambda self: self.env.user.company_id.import_tax_rate,
  611. help='默认值取公司进项税率')
  612. tax_amount = fields.Float('税额',
  613. compute=_compute_all_amount,
  614. store=True,
  615. digits='Amount',
  616. help='由税率计算得出')
  617. subtotal = fields.Float('价税合计',
  618. compute=_compute_all_amount,
  619. store=True,
  620. digits='Amount',
  621. help='含税单价 乘以 数量')
  622. procure_date = fields.Date('计划交期', help='供应商计划交货日期。采购订单(要求交货日期)+ 料品(供应商备货周期)。')
  623. note = fields.Char('备注',
  624. help='本行备注')
  625. company_id = fields.Many2one(
  626. 'res.company',
  627. string='公司',
  628. change_default=True,
  629. default=lambda self: self.env.company)
  630. quantity_todo = fields.Float(
  631. '未执行数量', compute="_compute_quantity_todo",
  632. store=True, digits='Quantity')
  633. @api.depends('quantity_in')
  634. def _compute_quantity_todo(self):
  635. for s in self:
  636. s.quantity_todo = s.quantity - s.quantity_in
  637. @api.onchange('goods_id', 'quantity','order_id')
  638. def onchange_goods_id(self):
  639. '''当订单行的商品变化时,带出商品上的单位、成本价。
  640. 在采购订单上选择供应商,自动带出供货价格,没有设置供货价的取成本价格。'''
  641. if not self.order_id.partner_id:
  642. raise UserError('请先选择一个供应商!')
  643. if self.goods_id:
  644. self.uom_id = self.goods_id.uom_id
  645. self.uos_id = self.goods_id.uos_id
  646. if self.price == 0:
  647. self.price = self.goods_id.cost
  648. # 使用搜索使模型排序生效
  649. vendor_ids = self.env['vendor.goods'].search([
  650. ('goods_id', '=', self.goods_id.id )])
  651. for line in vendor_ids:
  652. if line.date and line.date > self.order_id.date:
  653. continue
  654. if line.vendor_id == self.order_id.partner_id \
  655. and self.quantity >= line.min_qty:
  656. if self.env.company.vendor_price_taxed:
  657. self.price_taxed = line.price
  658. else:
  659. self.price = line.price
  660. break
  661. self.tax_rate = self.goods_id.get_tax_rate(self.goods_id, self.order_id.partner_id, 'buy')
  662. @api.onchange('quantity', 'price_taxed', 'discount_rate')
  663. def onchange_discount_rate(self):
  664. '''当数量、单价或优惠率发生变化时,优惠金额发生变化'''
  665. price = self.price_taxed / (1 + self.tax_rate * 0.01)
  666. decimal = self.env.ref('core.decimal_price')
  667. if float_compare(price, self.price, precision_digits=decimal.digits) != 0:
  668. self.price = price
  669. self.discount_amount = (self.quantity * price *
  670. self.discount_rate * 0.01)
  671. @api.constrains('tax_rate')
  672. def _check_tax_rate(self):
  673. for record in self:
  674. if record.tax_rate > 100:
  675. raise UserError('税率不能输入超过100的数')
  676. if record.tax_rate < 0:
  677. raise UserError('税率不能输入负数')
  678. class Payment(models.Model):
  679. _name = "payment.plan"
  680. _description = '付款计划'
  681. name = fields.Char(string="付款阶段名称", required=True,
  682. help='付款计划名称')
  683. amount_money = fields.Float(string="金额", required=True,
  684. help='付款金额')
  685. date_application = fields.Date(string="申请日期", readonly=True,
  686. help='付款申请日期')
  687. buy_id = fields.Many2one("buy.order",
  688. help='关联的采购订单',
  689. ondelete='cascade'
  690. )
  691. def unlink(self):
  692. for p in self:
  693. if self.date_application:
  694. raise UserError('此付款计划已申请,不能删除。')
  695. return super().unlink()
  696. def request_payment(self):
  697. self.ensure_one()
  698. if not self.buy_id.company_id.bank_account_id.id:
  699. raise UserError('%s未设置开户行,不能申请付款,\n请联系系统管理员进行设置。' % self.buy_id.company_id.name)
  700. categ = self.env.ref('money.core_category_purchase')
  701. tax_rate = self.buy_id.line_ids[0].tax_rate
  702. tax_amount = self.amount_money * tax_rate / (100 + tax_rate)
  703. if not float_is_zero(self.amount_money, 2):
  704. source_id = self.env['money.invoice'].create({
  705. 'name': self.buy_id.name,
  706. 'partner_id': self.buy_id.partner_id.id,
  707. 'category_id': categ.id,
  708. 'date': fields.Date.context_today(self),
  709. 'amount': self.amount_money,
  710. 'tax_amount': tax_amount,
  711. 'reconciled': 0,
  712. 'to_reconcile': self.amount_money,
  713. 'date_due': fields.Date.context_today(self),
  714. 'state': 'draft',
  715. })
  716. # 避免付款单去核销一张未确认的结算单(公司按发票确认应收应付的场景下出现)
  717. if source_id.state == 'draft':
  718. source_id.money_invoice_done()
  719. self.with_context(type='pay').env["money.order"].create({
  720. 'partner_id': self.buy_id.partner_id.id,
  721. 'bank_name': self.buy_id.partner_id.bank_name,
  722. 'bank_num': self.buy_id.partner_id.bank_num,
  723. 'date': fields.Date.context_today(self),
  724. 'source_ids':
  725. [(0, 0, {'name': source_id.id,
  726. 'category_id': categ.id,
  727. 'date': source_id.date,
  728. 'amount': self.amount_money,
  729. 'reconciled': 0.0,
  730. 'to_reconcile': self.amount_money,
  731. 'this_reconcile': self.amount_money})],
  732. 'line_ids':
  733. [(0, 0, {'bank_id': self.buy_id.company_id.bank_account_id.id,
  734. 'amount': self.amount_money})],
  735. 'type': 'pay',
  736. 'amount': self.amount_money,
  737. 'reconciled': 0,
  738. 'to_reconcile': self.amount_money,
  739. 'state': 'draft',
  740. 'buy_id': self.buy_id.id,
  741. })
  742. self.date_application = datetime.now()
上海开阖软件有限公司 沪ICP备12045867号-1