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.

182 lines
5.0 KiB

  1. # Copyright 2020 Iván Todorovich <ivan.todorovich@gmail.com>
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import api, models, fields, tools
  4. class PosOrderPaymentReport(models.Model):
  5. _name = 'report.pos.order.payment'
  6. _description = "Point of Sale Order Payments Report"
  7. _auto = False
  8. _order = 'date desc'
  9. date = fields.Datetime(
  10. string='Order Date',
  11. readonly=True,
  12. )
  13. state = fields.Selection(
  14. [
  15. ('draft', 'New'),
  16. ('paid', 'Paid'),
  17. ('done', 'Posted'),
  18. ('invoiced', 'Invoiced'),
  19. ('cancel', 'Cancelled'),
  20. ],
  21. string='Status',
  22. readonly=True,
  23. )
  24. order_id = fields.Many2one(
  25. 'pos.order',
  26. string='Order',
  27. readonly=True,
  28. )
  29. session_id = fields.Many2one(
  30. 'pos.session',
  31. string='Session',
  32. readonly=True,
  33. )
  34. partner_id = fields.Many2one(
  35. 'res.partner',
  36. string='Customer',
  37. readonly=True,
  38. )
  39. pricelist_id = fields.Many2one(
  40. 'product.pricelist',
  41. string='Pricelist',
  42. readonly=True,
  43. )
  44. user_id = fields.Many2one(
  45. 'res.users',
  46. string='Salesperson',
  47. readonly=True,
  48. )
  49. location_id = fields.Many2one(
  50. 'stock.location',
  51. string='Location',
  52. readonly=True,
  53. )
  54. company_id = fields.Many2one(
  55. 'res.company',
  56. string='Company',
  57. readonly=True,
  58. )
  59. journal_id = fields.Many2one(
  60. 'account.journal',
  61. string='Journal',
  62. readonly=True,
  63. )
  64. config_id = fields.Many2one(
  65. 'pos.config',
  66. string='Point of Sale',
  67. readonly=True,
  68. )
  69. pos_categ_id = fields.Many2one(
  70. 'pos.category',
  71. string='PoS Category',
  72. readonly=True,
  73. )
  74. product_id = fields.Many2one(
  75. 'product.product',
  76. string='Product',
  77. readonly=True,
  78. )
  79. product_tmpl_id = fields.Many2one(
  80. 'product.template',
  81. string='Product Template',
  82. readonly=True,
  83. )
  84. product_categ_id = fields.Many2one(
  85. 'product.category',
  86. string='Product Category',
  87. readonly=True,
  88. )
  89. payment_journal_id = fields.Many2one(
  90. "account.journal",
  91. string="Payment Journal",
  92. readonly=True,
  93. )
  94. payment_amount = fields.Float(
  95. string="Paid Amount",
  96. readonly=True,
  97. )
  98. invoiced = fields.Boolean(
  99. readonly=True,
  100. )
  101. def _select(self):
  102. return """
  103. SELECT
  104. ROW_NUMBER() OVER() AS id,
  105. s.date_order AS date,
  106. s.id as order_id,
  107. s.partner_id AS partner_id,
  108. s.state AS state,
  109. s.user_id AS user_id,
  110. s.location_id AS location_id,
  111. s.company_id AS company_id,
  112. s.sale_journal AS journal_id,
  113. l.product_id AS product_id,
  114. pt.categ_id AS product_categ_id,
  115. p.product_tmpl_id,
  116. ps.config_id,
  117. pt.pos_categ_id,
  118. s.pricelist_id,
  119. s.session_id,
  120. s.invoice_id IS NOT NULL AS invoiced,
  121. st.journal_id AS payment_journal_id,
  122. st.amount AS payment_amount
  123. """
  124. def _from(self):
  125. return """
  126. FROM pos_order_line AS l
  127. LEFT JOIN pos_order s ON (s.id = l.order_id)
  128. LEFT JOIN product_product p ON (l.product_id = p.id)
  129. LEFT JOIN product_template pt ON (p.product_tmpl_id = pt.id)
  130. LEFT JOIN uom_uom u ON (u.id = pt.uom_id)
  131. LEFT JOIN pos_session ps ON (s.session_id = ps.id)
  132. INNER JOIN (%s) st ON (st.pos_line_id = l.id)
  133. """ % (self._payment_query())
  134. def _payment_query(self):
  135. """
  136. Returns a query that distributes the pos.order payments
  137. among the pos.order.lines
  138. """
  139. return """
  140. SELECT
  141. pol.id AS pos_line_id,
  142. sl.id AS statement_line_id,
  143. st.journal_id AS journal_id,
  144. (
  145. sl.amount / po.amount_total * pol.price_subtotal_incl
  146. ) AS amount
  147. FROM pos_order_line AS pol
  148. INNER JOIN pos_order AS po
  149. ON pol.order_id = po.id
  150. INNER JOIN account_bank_statement_line AS sl
  151. ON pol.order_id = sl.pos_statement_id
  152. INNER JOIN account_bank_statement AS st
  153. ON st.id = sl.statement_id
  154. """
  155. def _group_by(self):
  156. return ""
  157. def _having(self):
  158. return ""
  159. @api.model_cr
  160. def init(self):
  161. tools.drop_view_if_exists(self._cr, self._table)
  162. self._cr.execute("""
  163. CREATE OR REPLACE VIEW %s AS (
  164. %s
  165. %s
  166. %s
  167. %s
  168. )
  169. """ % (
  170. self._table,
  171. self._select(), self._from(), self._group_by(), self._having(),
  172. ))