GoodERP
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

270 líneas
15KB

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