GoodERP
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

271 行
15KB

  1. # -*- coding: utf-8 -*-
  2. from odoo.exceptions import UserError
  3. from odoo import fields, models, api, _
  4. class PartnerStatementsReportWizard(models.TransientModel):
  5. _name = "partner.statements.report.wizard"
  6. _description = u"业务伙伴对账单向导"
  7. @api.model
  8. def _get_company_start_date(self):
  9. return self._get_company_start_date_impl()
  10. @api.model
  11. def _get_company_start_date_impl(self):
  12. ''' 获取当前登录用户公司的启用日期 '''
  13. return self.env.user.company_id.start_date
  14. partner_id = fields.Many2one('partner', string=u'业务伙伴', required=True,
  15. help=u'查看某一个业务伙伴的对账单报表')
  16. from_date = fields.Date(string=u'开始日期', required=True, default=_get_company_start_date,
  17. help=u'查看本次报表的开始日期') # 默认公司启用日期
  18. to_date = fields.Date(string=u'结束日期', required=True,
  19. default=lambda self: fields.Date.context_today(self),
  20. help=u'查看本次报表的结束日期') # 默认当前日期
  21. company_id = fields.Many2one(
  22. 'res.company',
  23. string=u'公司',
  24. change_default=True,
  25. default=lambda self: self.env.company)
  26. is_doc = fields.Boolean(u'打印word格式')
  27. def partner_statements_without_goods(self):
  28. """
  29. 业务伙伴对账单: 不带商品明细
  30. :return: action
  31. """
  32. for s in self:
  33. if s.from_date > s.to_date:
  34. raise UserError(_(u'结束日期不能小于开始日期!\n开始日期:%s 结束日期:%s ') %
  35. (s.from_date, s.to_date))
  36. if self.env.context.get('default_customer'): # 客户
  37. view = self.env.get('sell.order') != None \
  38. and self.env.ref('sell.customer_statements_report_list') \
  39. or self.env.ref('money.customer_statements_report_simple_list')
  40. name = _('客户对账单: %s', s.partner_id.name)
  41. res_model = 'customer.statements.report'
  42. else: # 供应商
  43. view = self.env.get('buy.order') != None \
  44. and self.env.ref('buy.supplier_statements_report_list') \
  45. or self.env.ref('money.supplier_statements_report_simple_list')
  46. name = _('供应商对账单: %s', s.partner_id.name)
  47. res_model = 'supplier.statements.report'
  48. # 打印word格式
  49. if s.is_doc:
  50. data = {
  51. 'partner_id': s.partner_id.id,
  52. 'from_date': s.from_date,
  53. 'to_date': s.to_date,
  54. }
  55. if res_model == 'supplier.statements.report':
  56. return self.env.ref('money.report_supplier_statements_report').report_action([], data=data)
  57. if res_model == 'customer.statements.report':
  58. return self.env.ref('money.report_customer_statements_report').report_action([], data=data)
  59. # 浏览器格式
  60. return {
  61. 'name': name,
  62. 'view_mode': 'list',
  63. 'res_model': res_model,
  64. 'view_id': False,
  65. 'views': [(view.id, 'list')],
  66. 'limit': 65535,
  67. 'type': 'ir.actions.act_window',
  68. 'target': 'main',
  69. 'domain': [('partner_id', '=', s.partner_id.id), ('date', '>=', s.from_date), ('date', '<=', s.to_date)]
  70. }
  71. def _create_statements_report_with_goods(self, partner_id, name, date, done_date, order_amount,
  72. benefit_amount, fee, amount, pay_amount, discount_money,
  73. balance_amount, note, move_id, ptype):
  74. """
  75. 生成无商品明细的对账单记录
  76. """
  77. if ptype == 'customer':
  78. model = self.env['customer.statements.report.with.goods']
  79. else:
  80. model = self.env['supplier.statements.report.with.goods']
  81. record_id = model.create({
  82. 'partner_id': partner_id,
  83. 'name': name,
  84. 'date': date,
  85. 'done_date': done_date,
  86. 'order_amount': order_amount,
  87. 'benefit_amount': benefit_amount,
  88. 'fee': fee,
  89. 'amount': amount,
  90. 'pay_amount': pay_amount,
  91. 'discount_money': discount_money,
  92. 'balance_amount': balance_amount,
  93. 'note': note,
  94. 'move_id': move_id}).id
  95. return record_id
  96. def _create_statements_report_with_goods_line(self, goods_code, goods_name, attribute_id, uom_id,
  97. quantity, price, discount_amount, without_tax_amount,
  98. tax_amount, order_amount, balance_amount, ptype):
  99. """
  100. 生成带商品明细的对账单记录
  101. """
  102. if ptype == 'customer':
  103. model = self.env['customer.statements.report.with.goods']
  104. else:
  105. model = self.env['supplier.statements.report.with.goods']
  106. record_id = model.create({
  107. 'goods_code': goods_code,
  108. 'goods_name': goods_name,
  109. 'attribute_id': attribute_id,
  110. 'uom_id': uom_id,
  111. 'quantity': quantity,
  112. 'price': price,
  113. 'discount_amount': discount_amount,
  114. 'without_tax_amount': without_tax_amount,
  115. 'tax_amount': tax_amount,
  116. 'order_amount': order_amount,
  117. 'balance_amount': balance_amount}).id
  118. return record_id
  119. def partner_statements_with_goods(self):
  120. """
  121. 业务伙伴对账单: 带商品明细
  122. :return: action
  123. """
  124. for s in self:
  125. res_ids = []
  126. if s.from_date > s.to_date:
  127. raise UserError(_(u'结束日期不能小于开始日期。\n开始日期:%s 结束日期:%s ') %
  128. (s.from_date, s.to_date))
  129. if self.env.context.get('default_customer'): # 客户
  130. reports = self.env['customer.statements.report'].search([('partner_id', '=', s.partner_id.id),
  131. ('date', '>=',
  132. s.from_date),
  133. ('date', '<=', s.to_date)])
  134. for report in reports:
  135. # 生成带商品明细的对账单记录
  136. record_id = self.sudo().with_context()._create_statements_report_with_goods(report.partner_id.id,
  137. report.name,
  138. report.date,
  139. report.done_date,
  140. report.sale_amount,
  141. report.benefit_amount,
  142. report.fee,
  143. report.amount,
  144. report.pay_amount,
  145. report.discount_money,
  146. report.balance_amount,
  147. report.note,
  148. report.move_id.id,
  149. 'customer')
  150. res_ids.append(record_id)
  151. # 生成带商品明细的对账单记录 明细
  152. if report.move_id:
  153. # report.amount<0时为销售退货单,否则为销售发货单
  154. line_ids = (report.amount < 0 and report.move_id.line_in_ids
  155. or report.move_id.line_out_ids)
  156. for line in line_ids:
  157. record_id = self.sudo().with_context()._create_statements_report_with_goods_line(line.goods_id.code,
  158. line.goods_id.name,
  159. line.attribute_id.id,
  160. line.uom_id.id,
  161. line.goods_qty,
  162. line.price,
  163. line.discount_amount,
  164. line.amount,
  165. line.tax_amount,
  166. line.subtotal,
  167. report.balance_amount,
  168. 'customer')
  169. res_ids.append(record_id)
  170. view = self.env.ref(
  171. 'sell.customer_statements_report_with_goods_list')
  172. return {
  173. 'name': _(u'客户对账单:') + s.partner_id.name,
  174. 'view_mode': 'list',
  175. 'res_model': 'customer.statements.report.with.goods',
  176. 'view_id': False,
  177. 'views': [(view.id, 'list')],
  178. 'limit': 65535,
  179. 'type': 'ir.actions.act_window',
  180. 'target': 'main',
  181. 'domain': [('id', 'in', res_ids)],
  182. 'context': {'is_customer': True, 'is_supplier': False},
  183. }
  184. else: # 供应商
  185. reports = self.env['supplier.statements.report'].search([('partner_id', '=', s.partner_id.id),
  186. ('date', '>=',
  187. s.from_date),
  188. ('date', '<=', s.to_date)])
  189. for report in reports:
  190. # 生成带商品明细的对账单记录
  191. record_id = self.sudo().with_context()._create_statements_report_with_goods(report.partner_id.id,
  192. report.name,
  193. report.date,
  194. report.done_date,
  195. report.purchase_amount,
  196. report.benefit_amount,
  197. 0,
  198. report.amount,
  199. report.pay_amount,
  200. report.discount_money,
  201. report.balance_amount,
  202. report.note,
  203. report.move_id.id,
  204. 'supplier')
  205. res_ids.append(record_id)
  206. # 生成带商品明细的对账单记录 明细
  207. if report.move_id:
  208. # report.amount<0时为采购退货单,否则为采购入库单
  209. line_ids = (report.amount < 0 and report.move_id.line_out_ids
  210. or report.move_id.line_in_ids)
  211. for line in line_ids:
  212. record_id = self.sudo().with_context()._create_statements_report_with_goods_line(line.goods_id.code,
  213. line.goods_id.name,
  214. line.attribute_id.id,
  215. line.uom_id.id,
  216. line.goods_qty,
  217. line.price,
  218. line.discount_amount,
  219. line.amount,
  220. line.tax_amount,
  221. line.subtotal,
  222. report.balance_amount,
  223. 'supplier')
  224. res_ids.append(record_id)
  225. view = self.env.ref(
  226. 'buy.supplier_statements_report_with_goods_list')
  227. return {
  228. 'name': _(u'供应商对账单:') + s.partner_id.name,
  229. 'view_mode': 'list',
  230. 'res_model': 'supplier.statements.report.with.goods',
  231. 'view_id': False,
  232. 'views': [(view.id, 'list')],
  233. 'limit': 65535,
  234. 'type': 'ir.actions.act_window',
  235. 'target': 'main',
  236. 'domain': [('id', 'in', res_ids)],
  237. 'context': {'is_customer': False, 'is_supplier': True},
  238. }
  239. @api.onchange('from_date')
  240. def onchange_from_date(self):
  241. """
  242. 客户对账单向导上过滤出是客户的业务伙伴,供应商上过滤出是供应商的业务伙伴
  243. :return: domain
  244. """
  245. if self.env.context.get('default_customer'):
  246. return {'domain': {'partner_id': [('c_category_id', '!=', False)]}}
  247. else:
  248. return {'domain': {'partner_id': [('s_category_id', '!=', False)]}}
上海开阖软件有限公司 沪ICP备12045867号-1