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.

171 lines
5.7 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(
  57. _("No supplier is set for selected articles.")
  58. )
  59. elif len(suppliers) == 1:
  60. return suppliers
  61. else:
  62. raise ValidationError(
  63. _("You must select article from a single supplier.")
  64. )
  65. @api.model
  66. def generate_cpo(self):
  67. order_line_obj = self.env["purchase.order.generator.line"]
  68. product_ids = self.env.context.get("active_ids", [])
  69. supplier = self._get_selected_supplier()
  70. name = "POG {} {}".format(supplier.name, fields.Date.today())
  71. cpo = self.create({"name": name, "supplier_id": supplier.id})
  72. for product_id in product_ids:
  73. supplierinfo = self.env["product.supplierinfo"].search(
  74. [
  75. ("product_tmpl_id", "=", product_id),
  76. ("name", "=", supplier.id),
  77. ]
  78. )
  79. min_qty = supplierinfo.min_qty if supplierinfo else 0
  80. order_line_obj.create(
  81. {
  82. "cpo_id": cpo.id,
  83. "product_template_id": product_id,
  84. "purchase_quantity": min_qty,
  85. }
  86. )
  87. action = {
  88. "type": "ir.actions.act_window",
  89. "res_model": "purchase.order.generator",
  90. "res_id": cpo.id,
  91. "view_type": "form",
  92. "view_mode": "form,tree",
  93. "target": "current",
  94. }
  95. return action
  96. @api.multi
  97. def create_purchase_order(self):
  98. self.ensure_one()
  99. if sum(self.pog_line_ids.mapped("purchase_quantity")) == 0:
  100. raise ValidationError(
  101. _(
  102. "You need at least a product to generate "
  103. "a Purchase Order"
  104. )
  105. )
  106. purchase_order = self.env["purchase.order"].create(
  107. {
  108. "date_order": self.order_date,
  109. "partner_id": self.supplier_id.id,
  110. "date_planned": self.date_planned,
  111. }
  112. )
  113. for cpo_line in self.pog_line_ids:
  114. if cpo_line.purchase_quantity > 0:
  115. product = cpo_line.product_template_id.product_variant_id
  116. pol = self.env["purchase.order.line"].create(
  117. {
  118. "name": cpo_line.name,
  119. "product_id": product.id,
  120. "product_qty": cpo_line.purchase_quantity,
  121. "price_unit": cpo_line.product_price,
  122. "product_uom": cpo_line.uom_po_id.id,
  123. "order_id": purchase_order.id,
  124. "date_planned": self.date_planned,
  125. }
  126. )
  127. pol.compute_taxes_id()
  128. self.generated_purchase_order_ids += purchase_order
  129. action = {
  130. "type": "ir.actions.act_window",
  131. "res_model": "purchase.order",
  132. "res_id": purchase_order.id,
  133. "view_type": "form",
  134. "view_mode": "form,tree",
  135. "target": "current",
  136. }
  137. return action
  138. @api.multi
  139. @api.depends("generated_purchase_order_ids")
  140. def _compute_generated_po_count(self):
  141. for cpo in self:
  142. cpo.generated_po_count = len(cpo.generated_purchase_order_ids)
  143. @api.multi
  144. def get_generated_po_action(self):
  145. self.ensure_one()
  146. action = {
  147. "type": "ir.actions.act_window",
  148. "res_model": "purchase.order",
  149. "view_mode": "tree,form,kanban",
  150. "target": "current",
  151. "domain": [("id", "in", self.generated_purchase_order_ids.ids)],
  152. }
  153. return action