Ir al contenido
Menú
Se marcó esta pregunta
113 Vistas

<?xml version="1.0" encoding="UTF-8"?>

<odoo>

    <data>

        <record id="res_partner_form_view_inherit" model="ir.ui.view">

            <field name="name">res.partner.form.view.inherit</field>

            <field name="model">res.partner</field>

            <field name="inherit_id" ref="base.view_partner_form" />

            <field name="arch" type="xml">

                <xpath expr="//form" position="attributes">

                    <attribute name="js_class">distribution_location_partner_form_view</attribute>

                </xpath>

            </field>

        </record>

    </data>

</odoo>



/** @odoo-module */


import { rpc } from '@web/core/network/rpc'

import { registry } from "@web/core/registry"

import { useService } from "@web/core/utils/hooks"

import { FormController } from "@web/views/form/form_controller"

import { formView } from "@web/views/form/form_view"


class ResPartnerFormController extends FormController {

    setup() {

        super.setup()

        this.notification = useService("notification")

    }


    // 📸 Existing feature: location + photo upload

    async onLocationClick() {

        const currentRecord = this.model.root.data

        const partnerName = currentRecord.name


        if (navigator.geolocation) {

            navigator.geolocation.getCurrentPosition(

                async (position) => {

                    const lat = position.coords.latitude

                    const lon = position.coords.longitude


                    const fileInput = document.createElement("input")

                    fileInput.type = "file"

                    fileInput.accept = "image/*"


                    fileInput.onchange = async (event) => {

                        const file = event.target.files[0]

                        if (!file) {

                            this.notification.add("Photo is required!", {

                                title: "Error",

                                type: "danger",

                            })

                            return

                        }


                        const reader = new FileReader()

                        reader.onload = async () => {

                            const photoBase64 = reader.result.split(",")[1]


                            const result = await rpc("/partner/checkin", {

                                partner_name: partnerName,

                                lat: lat,

                                lon: lon,

                                photo: photoBase64,

                            })


                            if (result.success) {

                                this.notification.add(result.message, {

                                    title: "Success",

                                    type: "success",

                                })

                            } else {

                                this.notification.add(result.message, {

                                    title: "Error",

                                    type: "danger",

                                })

                            }

                        }

                        reader.readAsDataURL(file)

                    }

                    fileInput.click()

                },

                () => {

                    this.notification.add("Location retrieval failed", {

                        title: "Error",

                        type: "danger",

                    })

                }

            )

        }

    }


    // 📍 New feature: only save location to partner fields

    async onSaveLocationClick() {

        const currentRecord = this.model.root.data

        const partnerId = this.model.root.resId  // Use resId instead of data.id

        const partnerName = currentRecord.name


        console.log("Debug - Partner ID:", partnerId, "Partner Name:", partnerName)


        // Check if record is saved

        if (!partnerId || partnerId === false) {

            this.notification.add("Please save the partner record first before capturing location", {

                title: "Warning",

                type: "warning",

            })

            return

        }


        if (!navigator.geolocation) {

            this.notification.add("Geolocation is not supported by your browser", {

                title: "Error",

                type: "danger",

            })

            return

        }


        navigator.geolocation.getCurrentPosition(

            async (position) => {

                const lat = position.coords.latitude

                const lon = position.coords.longitude


                try {

                    const result = await rpc("/partner/save_location", {

                        partner_id: partnerId,

                        lat: lat,

                        lon: lon,

                    })


                    if (result.success) {

                        this.notification.add(result.message, {

                            title: "Success",

                            type: "success",

                        })

                        await this.model.root.load() // refresh form to show updated values

                    } else {

                        this.notification.add(result.message, {

                            title: "Error",

                            type: "danger",

                        })

                    }

                } catch (error) {

                    console.error("RPC Error:", error)

                    this.notification.add("Failed to save location: " + error.message, {

                        title: "Error",

                        type: "danger",

                    })

                }

            },

            (error) => {

                let errorMsg = "Failed to get location"

                switch (error.code) {

                    case error.PERMISSION_DENIED:

                        errorMsg = "Location permission denied. Please enable location access."

                        break

                    case error.POSITION_UNAVAILABLE:

                        errorMsg = "Location information unavailable."

                        break

                    case error.TIMEOUT:

                        errorMsg = "Location request timed out."

                        break

                }

                this.notification.add(errorMsg, {

                    title: "Error",

                    type: "danger",

                })

            }

        )

    }

}


ResPartnerFormController.template = "distribution_location.ResPartnerFormView"


export const resPartnerFormView = {

    ...formView,

    Controller: ResPartnerFormController,

}


registry.category("views").add(

    "distribution_location_partner_form_view",

    resPartnerFormView,

)

It works fine on the Contacts page, but when I try to add a new customer from the POS page, I get an error


Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
sept 25
487
2
oct 24
2850
1
jun 23
2465
1
may 23
2392
1
oct 22
4572