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.

165 lines
5.6 KiB

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