Exemple #1
0
    def __set_raw_value(line, field_id, field_data):
        """
        Set simple value of Order Line

        :param line: sale.order.line
        :param field_id: str
        :param field_data: mixed
        """

        from odoo.addons.splashsync.helpers import TaxHelper, SettingsManager, M2MHelper
        tax_field_id = "tax_id" if OrderLinesHelper.is_order_line(
            line) else "invoice_line_tax_ids"

        # ==================================================================== #
        # [CORE] Order Line Fields
        # ==================================================================== #

        # ==================================================================== #
        # Linked Product ID
        if field_id == "product_id" and isinstance(
                ObjectsHelper.id(field_data), (int, str)):
            line.product_id = int(ObjectsHelper.id(field_data))
        # ==================================================================== #
        # Description
        # Qty Ordered | Qty Shipped/Delivered | Qty Invoiced
        # Delivery Lead Time | Line Status
        # Line Unit Price Reduction (Percent)
        if field_id in OrderLinesHelper.__generic_fields:
            setattr(line, field_id, field_data)
        # ==================================================================== #
        # Line Unit Price (HT)
        if field_id == "price_unit":
            line.price_unit = PricesHelper.extract(field_data, "ht")
            if not SettingsManager.is_sales_adv_taxes():
                setattr(
                    line, tax_field_id,
                    TaxHelper.find_by_rate(
                        PricesHelper.extract(field_data, "vat"), 'sale'))
        # ==================================================================== #
        # Sales Taxes
        if field_id == "tax_name" and SettingsManager.is_sales_adv_taxes():
            field_data = '["' + field_data + '"]' if isinstance(
                field_data, str) else "[]"
            M2MHelper.set_names(line,
                                tax_field_id,
                                field_data,
                                domain=TaxHelper.tax_domain,
                                filters=[("type_tax_use", "=", 'sale')])
        if field_id == "tax_names" and SettingsManager.is_sales_adv_taxes():
            M2MHelper.set_names(line,
                                tax_field_id,
                                field_data,
                                domain=TaxHelper.tax_domain,
                                filters=[("type_tax_use", "=", 'sale')])

        # ==================================================================== #
        # [EXTRA] Order Line Fields
        # ==================================================================== #

        return True
Exemple #2
0
    def complete_values(line_data):
        """
        Complete Order Line values with computed Information
        - Detect Product ID based on Line Name


        :param line_data: dict
        :rtype: dict
        """
        from odoo.addons.splashsync.helpers import M2OHelper

        # ====================================================================#
        # Detect Wrong or Empty Product ID
        # ====================================================================#
        try:
            if not M2OHelper.verify_id(
                    ObjectsHelper.id(line_data["product_id"]),
                    'product.product'):
                raise Exception("Invalid Product ID")
        except Exception:
            # ====================================================================#
            # Try detection based on Line Description
            try:
                product_id = M2OHelper.verify_name(line_data["name"],
                                                   'default_code',
                                                   'product.product')
                if int(product_id) > 0:
                    line_data["product_id"] = ObjectsHelper.encode(
                        "Product", str(product_id))
            except Exception:
                pass

        return line_data
Exemple #3
0
    def detect_variant_template(self):
        """
        Detect if Product Template for New Product
        :return: None|int
        """
        # ====================================================================#
        # Check if Inputs Contains Variants Ids
        if "variants" not in self._in.keys() or not isinstance(
                self._in["variants"], dict):
            return None
        # ====================================================================#
        # Walk on Variants Items
        for key, variant in self._in["variants"].items():
            # Variant Object Splash Id is Here
            if "id" not in variant.keys() or not isinstance(
                    variant["id"], str):
                continue
            # Variant Object Splash Id is Valid
            product_id = ObjectsHelper.id(variant["id"])
            if not isinstance(product_id, str) or int(product_id) <= 0:
                continue
            # Load Variant Product
            product = self.load(int(product_id))
            if product is False or not isinstance(
                    product.product_tmpl_id[0].id, int):
                continue
            # Return product Template Id
            return product.product_tmpl_id[0].id

        return None
Exemple #4
0
    def add_invoice_line(invoice, line_data):
        """
        Add a New Line to an Invoice

        :param invoice: account.invoice
        :param line_data: dict
        :return: account.invoice.line
        """
        # ====================================================================#
        # Load Account Id from Configuration
        account_id = OrderLinesHelper.detect_sales_account_id(invoice)
        # ====================================================================#
        # Safety Check
        if account_id is None or int(account_id) <= 0:
            Framework.log().error(
                "Unable to detect Account Id, Add Invoice Line skipped.")
            Framework.log().error("Please check your configuration.")
            return None
        # ====================================================================#
        # Prepare Minimal Order Line Data
        req_fields = {
            "invoice_id": invoice.id,
            "account_id": account_id,
            "sequence": 10 + len(invoice.invoice_line_ids),
        }
        # ====================================================================#
        # Link to Product
        try:
            req_fields["product_id"] = int(
                ObjectsHelper.id(line_data["product_id"]))
        except:
            pass
        # ==================================================================== #
        # Description
        # Qty Invoiced
        for field_id in OrderLinesHelper.__generic_fields:
            try:
                req_fields[field_id] = line_data[field_id]
            except:
                pass
        # ====================================================================#
        # Unit Price
        try:
            req_fields["price_unit"] = PricesHelper.extract(
                line_data["price_unit"], "ht")
        except:
            pass
        # ====================================================================#
        # Create Order Line
        try:
            return http.request.env["account.invoice.line"].create(req_fields)
        except Exception as exception:
            Framework.log().error(
                "Unable to create Invoice Line, please check inputs.")
            Framework.log().fromException(exception, False)
            Framework.log().dump(req_fields, "New Invoice Line")
            return None
Exemple #5
0
 def add_order_line(order, line_data):
     """
     Add a New Line to an Order
     :param order: sale.order
     :return: sale.order.line
     """
     # ====================================================================#
     # Prepare Minimal Order Line Data
     req_fields = {
         "order_id": order.id,
         "sequence": 10 + len(order.order_line),
         "qty_delivered_method": 'manual',
     }
     # ====================================================================#
     # Link to Product
     try:
         req_fields["product_id"] = int(
             ObjectsHelper.id(line_data["product_id"]))
     except:
         Framework.log().error(
             "Unable to create Order Line, Product Id is Missing")
         return None
     # ==================================================================== #
     # Description
     # Qty Ordered | Qty Shipped/Delivered | Qty Invoiced
     # Delivery Lead Time | Line Status
     for field_id in OrderLinesHelper.__generic_fields:
         try:
             req_fields[field_id] = line_data[field_id]
         except:
             pass
     # ====================================================================#
     # Unit Price
     try:
         req_fields["price_unit"] = PricesHelper.extract(
             line_data["price_unit"], "ht")
     except:
         pass
     # ====================================================================#
     # Create Order Line
     try:
         return http.request.env["sale.order.line"].create(req_fields)
     except Exception as exception:
         Framework.log().error(
             "Unable to create Order Line, please check inputs.")
         Framework.log().fromException(exception, False)
         Framework.log().dump(req_fields, "New Order Line")
         return None
Exemple #6
0
    def collectRequiredFields(self):
        """
        Collect All Required Fields from Inputs

        :return: False|hash
        """
        from splashpy import Framework
        # ====================================================================#
        # Safety Check - All required fields are there
        if self.is_ready_for_create() is not True:
            return Framework.log().error(self.is_ready_for_create())
        # ====================================================================#
        # Safety Check - Tracking Policy is Required
        if "picking_policy" not in self._in:
            self._in["picking_policy"] = "one"
        # ====================================================================#
        # Collect Basic Core Fields
        req_fields = self.collectRequiredCoreFields()
        self._in.__delitem__("picking_policy")
        if req_fields is False or req_fields.__len__() < 1:
            return False
        # ====================================================================#
        # Collect Order Core Fields
        for field_id in OrderCore.__core_fields_ids:
            # ====================================================================#
            # Setup Order Date
            if field_id == "date_order":
                req_fields[field_id] = self._in[field_id]
                continue
            # ====================================================================#
            # Setup Order Relations
            req_fields[field_id] = int(ObjectsHelper.id(self._in[field_id]))
            object_filters = PartnersHelper.thirdparty_filter(
            ) if field_id is "partner_id" else PartnersHelper.address_filter()
            if not M2OHelper.verify_id(req_fields[field_id], "res.partner",
                                       object_filters):
                return Framework.log().error(
                    "Unable to Identify Pointed Object: " +
                    str(self._in[field_id]))
        # ====================================================================#
        # Safety Check
        return req_fields
Exemple #7
0
    def collectRequiredFields(self):
        """
        Collect All Required Fields from Inputs

        :return: False|hash
        """
        from splashpy import Framework
        # ====================================================================#
        # Safety Check - All required fields are there
        if self.is_ready_for_create() is not True:
            return Framework.log().error(self.is_ready_for_create())
        # ====================================================================#
        # Collect Basic Core Fields
        req_fields = self.collectRequiredCoreFields()
        if req_fields is False:
            return False
        # ====================================================================#
        # Collect Order Core Fields
        for field_id in InvoiceCore.__core_fields_ids:
            # ====================================================================#
            # Setup Order Date
            if field_id in ['date_invoice']:
                req_fields[field_id] = self._in[field_id]
                continue
            # ====================================================================#
            # Setup Order Relations
            req_fields[field_id] = int(ObjectsHelper.id(self._in[field_id]))
            object_filters = PartnersHelper.thirdparty_filter(
            ) if field_id is "partner_id" else PartnersHelper.address_filter()
            if not M2OHelper.verify_id(req_fields[field_id], "res.partner",
                                       object_filters):
                return Framework.log().error(
                    "Unable to Identify Pointed Object: " +
                    str(self._in[field_id]))
        # ====================================================================#
        # Safety Check
        return req_fields