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.

166 lines
5.6 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Robin Keunen <robin@coopiteasy.be>
  3. # Vincent Van Rossem <vincent@coopiteasy.be>
  4. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  5. from odoo import models, fields, api, _
  6. from odoo.exceptions import ValidationError
  7. class PurchaseOrderGenerator(models.Model):
  8. _description = "Purchase Order Generator"
  9. _name = "purchase.order.generator"
  10. _order = "id desc"
  11. name = fields.Char(string="POG Reference", default=_("New"))
  12. order_date = fields.Datetime(
  13. string="Purchase Order Date",
  14. default=fields.Datetime.now,
  15. help="Date at which the Quotation should be validated and "
  16. "converted into a purchase order.",
  17. )
  18. date_planned = fields.Datetime(
  19. string="Date Planned",
  20. default=fields.Datetime.now,
  21. )
  22. supplier_id = fields.Many2one(
  23. comodel_name="res.partner",
  24. string="Supplier",
  25. readonly=True,
  26. help="Supplier of the purchase order.",
  27. )
  28. pog_line_ids = fields.One2many(
  29. comodel_name="purchase.order.generator.line",
  30. inverse_name="cpo_id",
  31. string="Order Lines",
  32. )
  33. total_amount = fields.Float(
  34. string="Total Amount (w/o VAT)", compute="compute_pog_total"
  35. )
  36. generated_purchase_order_ids = fields.One2many(
  37. comodel_name="purchase.order",
  38. inverse_name="original_cpo_id",
  39. string="Generated Purchase Orders",
  40. )
  41. generated_po_count = fields.Integer(
  42. string="Generated Purchase Order count",
  43. compute="_compute_generated_po_count",
  44. )
  45. @api.multi
  46. @api.depends("pog_line_ids", "pog_line_ids.purchase_quantity")
  47. def compute_pog_total(self):
  48. for cpo in self:
  49. total_amount = sum(cpol.subtotal for cpol in cpo.pog_line_ids)
  50. cpo.total_amount = total_amount
  51. @api.model
  52. def _get_selected_supplier(self):
  53. product_ids = self.env.context.get("active_ids", [])
  54. products = self.env["product.template"].browse(product_ids)
  55. suppliers = products.mapped("main_supplier_id")
  56. if not suppliers:
  57. raise ValidationError("No supplier is set for selected articles.")
  58. elif len(suppliers) == 1:
  59. return suppliers
  60. else:
  61. raise ValidationError(
  62. "You must select article from a single supplier."
  63. )
  64. @api.model
  65. def generate_cpo(self):
  66. order_line_obj = self.env["purchase.order.generator.line"]
  67. product_ids = self.env.context.get("active_ids", [])
  68. supplier = self._get_selected_supplier()
  69. name = "POG {} {}".format(supplier.name, fields.Date.today())
  70. cpo = self.create({"name": name, "supplier_id": supplier.id})
  71. for product_id in product_ids:
  72. supplierinfo = self.env["product.supplierinfo"].search(
  73. [
  74. ("product_tmpl_id", "=", product_id),
  75. ("name", "=", supplier.id),
  76. ]
  77. )
  78. min_qty = supplierinfo.min_qty if supplierinfo else 0
  79. order_line_obj.create(
  80. {
  81. "cpo_id": cpo.id,
  82. "product_template_id": product_id,
  83. "purchase_quantity": min_qty,
  84. }
  85. )
  86. action = {
  87. "type": "ir.actions.act_window",
  88. "res_model": "purchase.order.generator",
  89. "res_id": cpo.id,
  90. "view_type": "form",
  91. "view_mode": "form,tree",
  92. "target": "current",
  93. }
  94. return action
  95. @api.multi
  96. def create_purchase_order(self):
  97. self.ensure_one()
  98. if sum(self.pog_line_ids.mapped("purchase_quantity")) == 0:
  99. raise ValidationError(
  100. "You need at least a product to generate " "a Purchase Order"
  101. )
  102. purchase_order = self.env["purchase.order"].create(
  103. {
  104. "date_order": self.order_date,
  105. "partner_id": self.supplier_id.id,
  106. "date_planned": self.date_planned,
  107. }
  108. )
  109. for cpo_line in self.pog_line_ids:
  110. if cpo_line.purchase_quantity > 0:
  111. pol = self.env["purchase.order.line"].create(
  112. {
  113. "name": cpo_line.name,
  114. "product_id": cpo_line.product_template_id.product_variant_id.id,
  115. "product_qty": cpo_line.purchase_quantity,
  116. "price_unit": cpo_line.product_price,
  117. "product_uom": cpo_line.uom_po_id.id,
  118. "order_id": purchase_order.id,
  119. "date_planned": self.date_planned,
  120. }
  121. )
  122. pol.compute_taxes_id()
  123. self.generated_purchase_order_ids += purchase_order
  124. action = {
  125. "type": "ir.actions.act_window",
  126. "res_model": "purchase.order",
  127. "res_id": purchase_order.id,
  128. "view_type": "form",
  129. "view_mode": "form,tree",
  130. "target": "current",
  131. }
  132. return action
  133. @api.multi
  134. @api.depends("generated_purchase_order_ids")
  135. def _compute_generated_po_count(self):
  136. for cpo in self:
  137. cpo.generated_po_count = len(cpo.generated_purchase_order_ids)
  138. @api.multi
  139. def get_generated_po_action(self):
  140. self.ensure_one()
  141. action = {
  142. "type": "ir.actions.act_window",
  143. "res_model": "purchase.order",
  144. "view_mode": "tree,form,kanban",
  145. "target": "current",
  146. "domain": [("id", "in", self.generated_purchase_order_ids.ids)],
  147. }
  148. return action