Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
1231 Vistas

Hi all. I create a center module to creates appointments using calendar widget.

In my appointment model i have all the restriction when the appointment create (i check open/close, holidays and days when center open) and all works perfect but always when i click the appointment create (i can click wherever i want on the calendar view)

I want to add this controls at the calendar view. I mean, i want to block the click create event appointment on the calendar when when center close or when holidays. I read that with JavaScript this could be done but i dont know how to do it. check some javascript code but they are too old and i dont know what methods have to override to do it. Im literally new at odoo and python hehe. Could some one give a tips on how to do it? Thanks a lot!



Avatar
Descartar
Mejor respuesta

Hi Matias,


You can do using changing code  extend the web_calendar widget in your module's JavaScript file. You can override the onCreate method to include your restrictions.


odoo.define('your_module_name.CalendarViewRestrictions', function (require) {

    "use strict";


    const CalendarController = require('web.CalendarController');

    const rpc = require('web.rpc');


    CalendarController.include({

        async onCreateEvent(event) {

            const date = event.data.date; // The clicked date


            // Call the backend method to check restrictions

            const is_allowed = await rpc.query({

                model: 'your.model.name',

                method: 'check_calendar_restrictions',

                args: [date],

            });


            if (!is_allowed) {

                this.do_warn('Restriction', 'You cannot create an appointment on this date.');

                return;

            }


            // If allowed, proceed with the default behavior

            this._super.apply(this, arguments);

        },

    });

});


Avatar
Descartar
Autor

First, thanks for the reply. It helps me a lot to understand more about Odoo.
When i try it, i have this error when open my odoo (I restart odoo server with --dev=assets flag)

"The following modules are needed by other modules but have not been defined, they may not be present in the correct asset bundle:
web.CalendarController
web.rpc
The following modules could not be loaded because they have unmet dependencies, this is a secondary error which is likely caused by one of the above problems:
@cng_appointments/js/appointment"

JavaScript file is added on __manifest__.py file

'assets': {
'web.assets_backend': [
'cng_appointments/static/src/js/calendar_restriction.js',
],
}

Mejor respuesta

Hello matias,

can you be more specific

If you need to create the appointments via backend/erp you need to pass validations in there, else if you have created the widget in website then can you provide me a screenshot of how it looks based on that i can give you some reference of js for this validations in calender you are asking.

Avatar
Descartar
Autor

Hello! I cant provide a screenshot (button disabled) but this is what i have.

<record id="action_appointment_calendar" model="ir.actions.act_window">
<field name="name">Calendario de Citas</field>
<field name="res_model">cng_appointments.appointment</field>
<field name="view_mode">calendar,tree</field>
</record>

<record id="view_appointment_calendar" model="ir.ui.view">
<field name="name">cng_appointments.appointment.calendar</field>
<field name="model">cng_appointments.appointment</field>
<field name="arch" type="xml">
<calendar string="Calendario de Citas" mode="day" date_start="date_start" date_stop="date_end" color="box_id">
<field name="patient_id"/>
<field name="service_id"/>
<field name="box_id"/>
<field name="center_id"/>
</calendar>
</field>
</record>

<record id="view_appointment_calendar" model="ir.ui.view">
<field name="name">cng_appointments.appointment.calendar</field>
<field name="model">cng_appointments.appointment</field>
<field name="arch" type="xml">
<calendar string="Calendario de Citas" mode="day" date_start="date_start" date_stop="date_end" color="box_id">
<field name="patient_id"/>
<field name="service_id"/>
<field name="box_id"/>
<field name="center_id"/>
</calendar>
</field>
</record>

<menuitem id="menu_appointment_root" sequence="1" name="Gestión de Citas"/>
<menuitem id="menu_calendar_appointments"
name="Ver Citas"
parent="menu_appointment_root"
action="action_appointment_calendar"/>

I validate all on appointment form (After select a hour/day and confirm the popup create buttom). But i want the validations on the calendar. I mean, disable click or info the person that center is closed before the popup create button appears. @Sandeep Mishra answer i think works well but i have an error when try it.

Sorry with my english, is not good. Hope you understand what i want :')

Hello matias
thanks for sharing the code this did me help for understanding your need and your problem.
you need to create a separate model named off days in which youre storing dates which are off dates and also passing a method in the calender model whcih will check the date is an off date and pass then show an warning/validation (depends on that youre passing on the offday) that the day with an off day should not be selected/ your string for the off day choosen and also you can create an boolean which will check the saturday sunday off and add those days in the off days model i believe this would help if it doesnt you can still ask me here or ping me here by upvoting i will surely respond you with your need.
Thanks
and yes this is an view (Calender view) adding constraint/validation via js doesnt make sense doing via backend/python does thus do it via python if you have this form on the portal end/website module then you need to create a validation via js.

Autor

I try what Sandeep Mishra but i have this error when restart odoo server.

"The following modules are needed by other modules but have not been defined, they may not be present in the correct asset bundle:
web.CalendarController
web.rpc
The following modules could not be loaded because they have unmet dependencies, this is a secondary error which is likely caused by one of the above problems:
@cng_appointments/js/appointment"

Do you have any idea what can this error be?

Hello matias,
the sandeep mishra thing need to be covered in the asset as in the manifest you need to add the file name under the folder with the path given so the compiler will register your file and thus will need compile your code and give you an needed response you needed thus for the sandeep code you need to add that in manifest file which i will give you an example below.
If this doesnt solve your problem give me your whole js code and then give me your whole error in the message thus i can see that and respond you properly with that i have your calender thing code just give me manifest code andf the js code which you have written for the validation feature.
I am providing you the code of the js file register in the manifest below.
Thanks

#This you need to put below the data file in the manifest file
# "assets": {
# "web.assets_frontend": [
# # assets for frontend
# ],
# },

Publicaciones relacionadas Respuestas Vistas Actividad
0
feb 25
702
0
dic 21
1918
1
jul 25
377
2
jun 25
751
1
may 25
2372