public static FunctionModel GetFunctionModel(string function, FunctionUsage functionUsage)
        {
            if (functionsModels == null)
            {
                functionsModels = new Dictionary <string, string>();

                StreamReader __model__ = File.OpenText(PathData.Models + "/openerp_scripts.py.model");

                string content = __model__.ReadToEnd();
                __model__.Close();

                string[] functs = content.Trim().Split(new string[] { "-- new script --" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string funct in functs)
                {
                    string[] lines = funct.Trim().Split(new char[] { '\n' });

                    string name  = (lines[0].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries))[1].Trim();
                    string usage = (lines[1].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries))[1].Trim();

                    string cont = "";
                    for (int i = 2; i < lines.Length; i++)
                    {
                        cont += lines[i];
                    }

                    functionsModels[name + ":" + usage] = cont.Trim();
                }
            }

            FunctionModel functionModel = new FunctionModel(functionUsage);

            string usefor = functionUsage.ToString().ToLower();

            if (functionsModels.ContainsKey(function + ":" + usefor))
            {
                functionModel.Content = functionsModels[function + ":" + usefor];
                functionModel.ScanAllVariables();

                return(functionModel);
            }
            else
            {
                functionModel.Content = functionsModels["empty_function:" + usefor];
                functionModel.ScanAllVariables();
                functionModel.Default = true;

                return(functionModel);
            }
        }
Ejemplo n.º 2
0
        public void Create()
        {
            Name = Name.Replace(".", "_");

            List <PropertyData> listOfPropertyData_Code   = new List <PropertyData>();
            List <PropertyData> listOfPropertyData_Name   = new List <PropertyData>();
            List <PropertyData> listOfPropertyData_Others = new List <PropertyData>();

            foreach (PropertyData propertyData in ListOfProperties)
            {
                if (propertyData.Name.Equals("code"))
                {
                    listOfPropertyData_Code.Add(propertyData);
                }
                else if (propertyData.Name.Equals("name"))
                {
                    listOfPropertyData_Name.Add(propertyData);
                }
                else
                {
                    listOfPropertyData_Others.Add(propertyData);
                }
            }

            ListOfProperties = new List <PropertyData>();
            ListOfProperties.AddRange(listOfPropertyData_Code);
            ListOfProperties.AddRange(listOfPropertyData_Name);
            ListOfProperties.AddRange(listOfPropertyData_Others);


            ListOfGroups = new Dictionary <string, GroupData>();

            ListOfGroupsReport     = new Dictionary <string, GroupData>();
            ListOfPropertiesReport = new List <PropertyData>();

            GroupData rootGroupData = new GroupData("default");

            rootGroupData.Root      = true;
            rootGroupData.ClassData = this;
            rootGroupData.Report    = false;
            ListOfGroups.Add("default", rootGroupData);

            GroupData rootReportGroupData = new GroupData("default");

            rootReportGroupData.Root      = true;
            rootReportGroupData.ClassData = this;
            rootReportGroupData.Report    = true;


            string sel = "";
            List <PropertyData> listOfDates = new List <PropertyData>();

            foreach (PropertyData propertyData in ListOfProperties)
            {
                if (propertyData.Typage.Equals("date") || propertyData.Typage.Equals("datetime") || propertyData.Typage.Equals("time"))
                {
                    listOfDates.Add(propertyData);
                    ListOfPropertiesReport.Add(propertyData);
                }
            }

            if (listOfDates.Count > 0)
            {
                sel += "('date', 'Date'),";
            }

            if (!sel.Equals(""))
            {
                PropertyData property = new PropertyData();
                property.Name   = "filtrerpar";
                property.Alias  = "Filtrer par";
                property.Typage = "selection";

                sel = sel.Substring(0, sel.Length - 1);
                property.Selection = "[" + sel + "]";


                PageData pageFilter = new PageData("Filtres");
                pageFilter.ListOfGroups["default"]           = new GroupData("default");
                pageFilter.ListOfGroups["default"].ClassData = this;
                pageFilter.ListOfGroups["default"].ListOfProperties.Add(property);
                ListOfPropertiesReport.Add(property);

                if (listOfDates.Count > 0)
                {
                    pageFilter.ListOfGroups["default"].ListOfGroups.Add("dates", new GroupData("Dates", this));
                    pageFilter.ListOfGroups["default"].ListOfGroups["dates"].ListOfProperties = listOfDates;
                }

                rootReportGroupData.ListOfPages.Add("filter", pageFilter);
            }


            rootReportGroupData.ListOfPages.Add("avances", new PageData("Avancés"));
            rootReportGroupData.ListOfPages.Add("options", new PageData("Options"));

            ListOfGroupsReport.Add("default", rootReportGroupData);

            foreach (PropertyData propertyData in ListOfProperties)
            {
                bool dontthread = false;

                if (!propertyData.Name.Equals(Name + "_id"))
                {
                    propertyData.Update();


                    if ((propertyData.Name.Equals("state")) && (propertyData.Typage.Equals("selection")))
                    {
                        dontthread    = true;
                        StateWorkflow = true;
                        StateProperty = propertyData;

                        propertyData.Readonly = "1";
                        propertyData.Select   = "1";

                        ListOfWorkFlowButton   = new Dictionary <string, ButtonData>();
                        ListOfWorkFlowFunction = new Dictionary <string, OperationData>();
                        foreach (KeyValuePair <string, string> KeyValue in propertyData.SelectionData.ListOfValues)
                        {
                            OperationData operationData = GetOperation(propertyData.TypageInitial, KeyValue.Key);

                            if (operationData != null)
                            {
                                ButtonData buttonData = new ButtonData();
                                buttonData.States = KeyValue.Key;

                                if (operationData.CallBy.Equals("button"))
                                {
                                    buttonData.Text = KeyValue.Value;
                                    buttonData.Name = operationData.Name;
                                }
                                else if (operationData.CallBy.Equals(""))
                                {
                                    buttonData.RenderXML = false;
                                    ListOfWorkFlowFunction.Add(KeyValue.Key, operationData);

                                    operationData.ReturnType = "boolean";
                                }

                                ListOfWorkFlowButton.Add(KeyValue.Key, buttonData);
                            }
                        }
                    }
                    else if (propertyData.Name.Equals("name"))
                    {
                        //
                    }
                    else if (propertyData.Name.Equals("code"))
                    {
                        if (!Code.Equals(""))
                        {
                            CodeProperty      = propertyData;
                            propertyData.Uniq = true;
                        }
                    }
                    else if (propertyData.Name.Equals("sequence"))
                    {
                        //
                    }
                    else if (propertyData.Name.Equals("parent_id"))
                    {
                        //
                    }



                    if (!dontthread)
                    {
                        if (propertyData.Typage.Equals("text"))
                        {
                            if (propertyData.Group.Equals("") && propertyData.Page.Equals(""))
                            {
                                propertyData.Page      = propertyData.Alias;
                                propertyData.ColSpan   = "6";
                                propertyData.ShowLabel = false;
                            }
                        }
                        else if (propertyData.Typage.Equals("selection") || (propertyData.LinkData != null))
                        {
                            ListOfGroupsReport["default"].ListOfProperties.Add(propertyData);
                            ListOfPropertiesReport.Add(propertyData);
                        }


                        if (propertyData.Group.Equals(string.Empty) && propertyData.Page.Equals(string.Empty))
                        {
                            ListOfGroups["default"].ListOfProperties.Add(propertyData);
                        }
                        else if (!propertyData.Group.Equals(string.Empty) && propertyData.Page.Equals(string.Empty))
                        {
                            if (!ListOfGroups["default"].ListOfGroups.ContainsKey(propertyData.Group))
                            {
                                ListOfGroups["default"].ListOfGroups.Add(propertyData.Group, new GroupData(propertyData.Group, this));
                            }

                            ListOfGroups["default"].ListOfGroups[propertyData.Group].ListOfProperties.Add(propertyData);
                        }
                        else if (propertyData.Group.Equals(string.Empty) && !propertyData.Page.Equals(string.Empty))
                        {
                            if (!ListOfGroups["default"].ListOfPages.ContainsKey(propertyData.Page))
                            {
                                ListOfGroups["default"].ListOfPages.Add(propertyData.Page, new PageData(propertyData.Page));
                                ListOfGroups["default"].ListOfPages[propertyData.Page].ListOfGroups.Add("default", new GroupData("default", this));
                            }

                            ListOfGroups["default"].ListOfPages[propertyData.Page].ListOfGroups["default"].ListOfProperties.Add(propertyData);
                        }
                        else if (!propertyData.Group.Equals(string.Empty) && !propertyData.Page.Equals(string.Empty))
                        {
                            if (!ListOfGroups["default"].ListOfPages.ContainsKey(propertyData.Page))
                            {
                                ListOfGroups["default"].ListOfPages.Add(propertyData.Page, new PageData(propertyData.Page));
                                ListOfGroups["default"].ListOfPages[propertyData.Page].ListOfGroups.Add("default", new GroupData("default", this));
                            }

                            if (!ListOfGroups["default"].ListOfPages[propertyData.Page].ListOfGroups.ContainsKey(propertyData.Group))
                            {
                                ListOfGroups["default"].ListOfPages[propertyData.Page].ListOfGroups.Add(propertyData.Group, new GroupData(propertyData.Group, this));
                            }

                            ListOfGroups["default"].ListOfPages[propertyData.Page].ListOfGroups[propertyData.Group].ListOfProperties.Add(propertyData);
                        }
                    }
                    else
                    {
                        if (propertyData.Typage.Equals("selection") || (propertyData.LinkData != null))
                        {
                            ListOfGroupsReport["default"].ListOfProperties.Add(propertyData);
                            ListOfPropertiesReport.Add(propertyData);
                        }
                    }
                }
            }

            foreach (PropertyData propertyData in ListOfProperties)
            {
                if (propertyData.Typage.Equals("one2many"))
                {
                    foreach (PropertyData property in ListOfProperties)
                    {
                        if (!propertyData.Equals(property))
                        {
                            if (!property.ShareTo.Equals(""))
                            {
                                string[] shareto = property.ShareTo.Split(new char[] { '.' });

                                string clssppt = Parent.ClassicName + "." + shareto[0];

                                if (clssppt.Equals(propertyData.LinkManyClass))
                                {
                                    ClassData clssData = Parent.GetClass(propertyData.LinkManyClass.Split(new char[] { '.' })[1]);

                                    PropertyData propData = clssData.GetProperty(shareto[1]);

                                    propData.MaxUse = Name + "." + propData.Name;
                                }
                            }
                        }
                    }
                }
            }

            Dictionary <string, PageData> ListOfPages  = new Dictionary <string, PageData>();
            Dictionary <string, PageData> ListOfPages2 = new Dictionary <string, PageData>();

            foreach (KeyValuePair <string, PageData> pageData in ListOfGroups["default"].ListOfPages)
            {
                if ((pageData.Value.ListOfGroups != null) && (pageData.Value.ListOfGroups.Count > 0) &&
                    (pageData.Value.ListOfGroups.ContainsKey("default")) && (pageData.Value.ListOfGroups["default"].ListOfProperties != null) &&
                    (pageData.Value.ListOfGroups["default"].ListOfProperties.Count > 0) && (pageData.Value.ListOfGroups["default"].ListOfProperties[0].Typage.Equals("text")))
                {
                    ListOfPages2.Add(pageData.Value.Label, pageData.Value);
                }
                else
                {
                    ListOfPages.Add(pageData.Value.Label, pageData.Value);
                }
            }

            foreach (KeyValuePair <string, PageData> pageData in ListOfPages2)
            {
                ListOfPages.Add(pageData.Value.Label, pageData.Value);
            }

            ListOfGroups["default"].ListOfPages = ListOfPages;

            ListOfButton = new List <ButtonData>();
            foreach (KeyValuePair <string, OperationData> operationDataKV in ListOfOperations)
            {
                OperationData operationData = operationDataKV.Value;

                if ((operationData.CallBy.Equals("button")) && (operationData.State.Equals("")))
                {
                    ButtonData buttonData = new ButtonData(operationData);

                    ListOfButton.Add(buttonData);
                }
                else if ((operationData.CallBy.Equals("button")) && (!operationData.State.Equals("")))
                {
                    FunctionModel pythonCode = Utils.GetFunctionModel("function_caller_action_wkf", FunctionUsage.Classic);

                    string[] states = operationData.State.Split(new char[] { '.' });

                    string state = states[1].Trim(); //GetNextState(states[1].Trim());

                    if (!state.Equals(""))
                    {
                        /*Dictionary<string, string> pythonCode_values = new Dictionary<string, string>();
                         * pythonCode_values["function_name"] = operationData.Name;
                         * pythonCode_values["action_wkf"] = "action_" + state + "_wkf";
                         *
                         * pythonCode.Content = Utils.replaceValues(pythonCode.Content, pythonCode_values) + "\r\n\t";*/

                        pythonCode.ReplaceValues(operationData.Name, "action_" + state + "_wkf");

                        operationData.PythonCode = pythonCode.Content;
                    }
                }
                else if (!operationData.CallBy.Equals("button") && !operationData.CallBy.Equals(""))
                {
                    PropertyData propertyData = GetProperty(operationData.CallBy);

                    string param = "";
                    foreach (ParameterData parameterData in operationData.ListOfParameter)
                    {
                        param += parameterData.Name + ", ";
                    }

                    if (!param.Equals(""))
                    {
                        param = param.Substring(0, param.Length - 2);
                    }

                    propertyData.OnChange = operationData.Name + "(" + param + ")";
                }
            }

            ListOfGroups["default"].ListOfButtons = ListOfButton;

            PyGenerator.CreateClass(this);
            PyGenerator.CreateReportModel(this);
            PyGenerator.CreateReportWizardModel(this);

            XmlGenerator.CreateView(this);
            XmlGenerator.CreateReportView(this);
            XmlGenerator.CreateWizardReportView(this, MenuRapport);

            if (CodeProperty != null)
            {
                XmlGenerator.CreateDatas(this);
            }

            if (StateWorkflow)
            {
                XmlGenerator.CreateWorkFlow(this);
            }

            XslGenerator.CreateReport(this);
        }
Ejemplo n.º 3
0
        public static string CreateClass(ClassData classData)
        {
            Dictionary <string, string> openerp_classValues = new Dictionary <string, string>();

            string fields   = "";
            string defaults = "";

            string constraints           = "";
            string sql_constraints       = "";
            string constraints_functions = "";

            string inherit = "# delete this line";

            string functions        = "";
            string functions_before = "# delete this line";


            foreach (PropertyData propertyData in classData.ListOfProperties)
            {
                if (!propertyData.Typage.Equals("many2many") && !propertyData.Typage.Equals("one2many"))
                {
                    RenderPropertyPy(classData, propertyData, ref fields, ref defaults, ViewKind.Form);

                    if (propertyData.Uniq)
                    {
                        sql_constraints += ((sql_constraints.Equals(""))?"":"\r\n\t\t") + "('" + propertyData.Name + "_" + classData.Name + "_uniq', 'unique(" + propertyData.Name + ")', 'La valeur du champ [" + propertyData.Alias + "] doit être unique !'),";
                    }

                    if (!propertyData.Function.Equals(string.Empty))
                    {
                        functions_before += (functions_before.Equals(string.Empty)) ? "" : "\r\n\r\n\t";

                        string fieldFunctionType = Utils.GetFunctionName(propertyData.Function);

                        FunctionModel pythonCode = Utils.GetFunctionModel("function_" + fieldFunctionType, FunctionUsage.Field);

                        string result = "";
                        if (pythonCode.Default)
                        {
                            if ((new string[] { "int" }).Contains <string>(propertyData.Typage))
                            {
                                result = "0";
                            }

                            if ((new string[] { "float" }).Contains <string>(propertyData.Typage))
                            {
                                result = "0.0";
                            }

                            if ((new string[] { "char", "string", "text" }).Contains <string>(propertyData.Typage))
                            {
                                result = "''";
                            }
                        }

                        List <string> pythonCode_values = new List <string>();
                        pythonCode_values.Add(propertyData.Name + "_" + fieldFunctionType);

                        switch (fieldFunctionType)
                        {
                        case "sum":
                        {
                            string property_zero = "0";
                            if (propertyData.Typage.Equals("float"))
                            {
                                property_zero = "0.0";
                            }

                            pythonCode_values.Add(property_zero);

                            pythonCode_values.Add(propertyData.Name);

                            List <string> arguments = Utils.GetFunctionArguments(propertyData.Function);

                            pythonCode_values.Add(arguments[1]);
                            pythonCode_values.Add(arguments[0]);

                            break;
                        }

                        default:
                        {
                            pythonCode_values.Add(result);

                            break;
                        }
                        }

                        /*Dictionary<string, string> pythonCode_values = new Dictionary<string, string>();
                         * pythonCode_values["function_name"] = propertyData.Name + "_" + fieldFunctionType;
                         * pythonCode_values["property_name"] = propertyData.Name;
                         * pythonCode_values["property_to_sum"] = "";
                         * pythonCode_values["children"] = "";
                         *
                         * pythonCode.Content = Utils.replaceValues(pythonCode.Content, pythonCode_values) + "\r\n\t";*/

                        pythonCode.ReplaceValues(pythonCode_values);

                        functions_before += pythonCode;
                    }
                }
            }

            foreach (PropertyData propertyData in classData.ListOfProperties)
            {
                if (!propertyData.MaxUse.Equals(""))
                {
                    functions_before += (functions_before.Equals(string.Empty)) ? "" : "\r\n\r\n\t";

                    FunctionModel pythonCode = Utils.GetFunctionModel("function_label", FunctionUsage.Classic);

                    Dictionary <string, string> pythonCode_values = new Dictionary <string, string>();

                    pythonCode_values.Add("module", classData.Parent.ClassicName);

                    pythonCode_values.Add("classname", classData.Name);
                    pythonCode_values.Add("property_name", propertyData.Name);

                    string[] maxuse = propertyData.MaxUse.Split(new char[] { '.' });

                    pythonCode_values.Add("classlink", maxuse[0]);
                    pythonCode_values.Add("property_link", maxuse[1]);

                    pythonCode.ReplaceValues(pythonCode_values);

                    functions_before += pythonCode;



                    functions += (functions.Equals(string.Empty)) ? "" : "\r\n\r\n\t";

                    FunctionModel pythonCode2 = Utils.GetFunctionModel("function_onchange_maxuse", FunctionUsage.Classic);

                    Dictionary <string, string> pythonCode2_values = new Dictionary <string, string>();

                    pythonCode2_values.Add("property_name", propertyData.Name);
                    pythonCode2_values.Add("classlink", maxuse[0]);

                    pythonCode2.ReplaceValues(pythonCode2_values);

                    functions += pythonCode2;
                }
            }

            foreach (PropertyData propertyData in classData.ListOfProperties)
            {
                if (propertyData.Typage.Equals("many2many"))
                {
                    RenderPropertyPy(classData, propertyData, ref fields, ref defaults, ViewKind.Form);
                }
            }

            foreach (PropertyData propertyData in classData.ListOfProperties)
            {
                if (propertyData.Typage.Equals("one2many"))
                {
                    RenderPropertyPy(classData, propertyData, ref fields, ref defaults, ViewKind.Form);
                }
            }


            Dictionary <string, OperationData> listOfOperationsData = classData.ListOfOperations;

            if (classData.StateProperty != null)
            {
                int i = 1;

                foreach (KeyValuePair <string, string> KeyValue in classData.StateProperty.SelectionData.ListOfValues)
                {
                    string key = KeyValue.Key;

                    OperationData operationData = new OperationData();
                    operationData.Parent          = classData;
                    operationData.ListOfParameter = new List <ParameterData>();

                    FunctionModel pythonCode = new FunctionModel(FunctionUsage.Classic);

                    if (i == 1)
                    {
                        pythonCode = Utils.GetFunctionModel("function_first_action_wkf", FunctionUsage.Classic);
                    }
                    else
                    {
                        pythonCode = Utils.GetFunctionModel("function_action_wkf", FunctionUsage.Classic);
                    }

                    /*Dictionary<string, string> pythonCode_values = new Dictionary<string, string>();
                     * pythonCode_values["function_name"] = "action_" + key + "_wkf";
                     * pythonCode_values["state"] = key;
                     *
                     * pythonCode.Content = Utils.replaceValues(pythonCode.Content, pythonCode_values) + "\r\n\t";*/

                    pythonCode.ReplaceValues("action_" + key + "_wkf", key);

                    operationData.Name       = "action_" + key + "_wkf";
                    operationData.PythonCode = pythonCode.Content;

                    listOfOperationsData.Add(operationData.Name, operationData);

                    i++;
                }
            }

            Dictionary <string, string> accountArgs = Utils.FunctionArgs(classData.AccountingArgs);

            //string state = Utils.GetArg(accountArgs, "state", "");
            //string button = Utils.GetArg(accountArgs, "button", "");


            foreach (KeyValuePair <string, OperationData> operationDataKV in listOfOperationsData)
            {
                OperationData operationData = operationDataKV.Value;

                functions += (functions.Equals(string.Empty)) ? "" : "\r\n\r\n\t";

                string parameters = "";
                int    i          = 0;
                foreach (ParameterData param in operationData.ListOfParameter)
                {
                    if (!param.Name.Equals(""))
                    {
                        parameters += ", " + param.Name;
                    }
                    else
                    {
                        parameters += ", arg" + i.ToString();

                        i++;
                    }
                }

                if (operationData.PythonCode.Equals(""))
                {
                    functions += "def " + operationData.Name + "(self, cr, uid, ids" + parameters + ", context=None):\r\n";
                    functions += "\t\t# contenu généré, à modifier si besoin\r\n";
                    functions += "\t\treturn " + Utils.CorrectReturn(operationData.ReturnType);
                }
                else
                {
                    functions += operationData.PythonCode;
                }
            }


            //string add_after = "";
            foreach (KeyValuePair <string, string> constraint in classData.ListOfConstraints)
            {
                if (constraints_functions.Equals(""))
                {
                    constraints_functions = "\r\n\t";
                    //add_after += "\r\n";
                }

                string[] contrainte = constraint.Value.Split(new char[] { '\n' });

                string[] contrainte_property   = contrainte[0].Trim().Split(new char[] { ':' });
                string[] contrainte_constraint = contrainte[1].Trim().Split(new char[] { ':' });
                string[] contrainte_message    = contrainte[2].Trim().Split(new char[] { ':' });


                string[] con_properties        = contrainte_property[1].Split(new char[] { ',' });
                string   contrainte_properties = "";
                string   virgule = "";
                foreach (string con_property in con_properties)
                {
                    contrainte_properties += virgule + "'" + con_property.Trim() + "'";
                    virgule = ",";
                }

                string[] operandes = contrainte_constraint[1].Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                string operande1 = operandes[0];
                string oprator   = operandes[1];
                string operande2 = operandes[2];

                FunctionModel function_content = Utils.GetFunctionModel("function_constraint_operator", FunctionUsage.Classic);

                /*Dictionary<string, string> constraint_content_values = new Dictionary<string, string>();
                 * constraint_content_values["constraint_name"] = constraint.Key;
                 * constraint_content_values["operande1"] = operande1;
                 * constraint_content_values["operator"] = Utils.TrueString(oprator);
                 * constraint_content_values["operande2"] = operande2;
                 *
                 * function_content.Content = Utils.replaceValues(function_content.Content, constraint_content_values);*/

                function_content.ReplaceValues(constraint.Key, operande1, Utils.TrueString(oprator), operande2);

                constraints_functions += function_content + "\r\n\t";

                constraints += "(_" + constraint.Key + ", \"" + contrainte_message[1].Trim() + "\", [" + contrainte_properties + "]),\r\n\t\t";
            }

            if (!constraints.Equals(""))
            {
                constraints = constraints.Substring(0, constraints.Length - 4);
            }

            //constraints_functions += add_after;

            openerp_classValues["name"] = classData.ModuleName + "." + classData.Name;
            if (classData.Inherit != null)
            {
                openerp_classValues["name"] = "# delete this line";
                inherit = "_inherit = \"" + classData.Inherit.ModuleName + "." + classData.Inherit.Name + "\"";
            }

            openerp_classValues["classname"]             = classData.Parent.ClassicName + "_" + classData.Name;
            openerp_classValues["description"]           = classData.Documentation;
            openerp_classValues["inherit"]               = inherit;
            openerp_classValues["fields"]                = fields;
            openerp_classValues["defaults"]              = defaults;
            openerp_classValues["constraints_functions"] = constraints_functions;
            openerp_classValues["constraints"]           = constraints;
            openerp_classValues["sql_constraints"]       = sql_constraints;
            openerp_classValues["functions"]             = functions;
            openerp_classValues["functions_before"]      = functions_before;



            string filePy = classData.Parent.DirectoryInfo_Base.FullName + "/" + classData.Parent.ClassicName + "_" + classData.Name + ".py";

            Utils.MakeFileFromModel("openerp_class.py.model", filePy, openerp_classValues);

            return(filePy);
        }
        public static string CreateClass(ClassData classData)
        {
            Dictionary<string, string> openerp_classValues = new Dictionary<string, string>();

            string fields = "";
            string defaults = "";

            string constraints = "";
            string sql_constraints = "";
            string constraints_functions = "";

            string inherit = "# delete this line";

            string functions = "";
            string functions_before = "# delete this line";
            

            foreach (PropertyData propertyData in classData.ListOfProperties)
            {
                if (!propertyData.Typage.Equals("many2many") && !propertyData.Typage.Equals("one2many"))
                {
                    RenderPropertyPy(classData, propertyData, ref fields, ref defaults, ViewKind.Form);

                    if (propertyData.Uniq)
                    {
                        sql_constraints += ((sql_constraints.Equals(""))?"":"\r\n\t\t") + "('" + propertyData.Name + "_" + classData.Name + "_uniq', 'unique(" + propertyData.Name + ")', 'La valeur du champ [" + propertyData.Alias + "] doit être unique !'),";
                    }

                    if (!propertyData.Function.Equals(string.Empty))
                    {
                        functions_before += (functions_before.Equals(string.Empty)) ? "" : "\r\n\r\n\t";

                        string fieldFunctionType = Utils.GetFunctionName(propertyData.Function);

                        FunctionModel pythonCode = Utils.GetFunctionModel("function_" + fieldFunctionType, FunctionUsage.Field);

                        string result = "";
                        if (pythonCode.Default)
                        {
                            if ((new string[] { "int" }).Contains<string>(propertyData.Typage))
                            {
                                result = "0";
                            }

                            if ((new string[] { "float" }).Contains<string>(propertyData.Typage))
                            {
                                result = "0.0";
                            }

                            if ((new string[] { "char", "string", "text" }).Contains<string>(propertyData.Typage))
                            {
                                result = "''";
                            }
                        }

                        List<string> pythonCode_values = new List<string>();
                        pythonCode_values.Add(propertyData.Name + "_" + fieldFunctionType);
                        
                        switch (fieldFunctionType)
                        {
                            case "sum":
                            {
                                string property_zero = "0";
                                if (propertyData.Typage.Equals("float"))
                                {
                                    property_zero = "0.0";
                                }

                                pythonCode_values.Add(property_zero);

                                pythonCode_values.Add(propertyData.Name);

                                List<string> arguments = Utils.GetFunctionArguments(propertyData.Function);

                                pythonCode_values.Add(arguments[1]);
                                pythonCode_values.Add(arguments[0]);

                                break;
                            }

                            default:
                            {
                                pythonCode_values.Add(result);

                                break;
                            }
                        }

                        /*Dictionary<string, string> pythonCode_values = new Dictionary<string, string>();
                        pythonCode_values["function_name"] = propertyData.Name + "_" + fieldFunctionType;
                        pythonCode_values["property_name"] = propertyData.Name;
                        pythonCode_values["property_to_sum"] = "";
                        pythonCode_values["children"] = "";

                        pythonCode.Content = Utils.replaceValues(pythonCode.Content, pythonCode_values) + "\r\n\t";*/

                        pythonCode.ReplaceValues(pythonCode_values);
                        
                        functions_before += pythonCode;
                    }
                }
            }

            foreach (PropertyData propertyData in classData.ListOfProperties)
            {
                if (!propertyData.MaxUse.Equals(""))
                {
                    functions_before += (functions_before.Equals(string.Empty)) ? "" : "\r\n\r\n\t";

                    FunctionModel pythonCode = Utils.GetFunctionModel("function_label", FunctionUsage.Classic);

                    Dictionary<string, string> pythonCode_values = new Dictionary<string, string>();

                    pythonCode_values.Add("module", classData.Parent.ClassicName);

                    pythonCode_values.Add("classname", classData.Name);
                    pythonCode_values.Add("property_name", propertyData.Name);

                    string[] maxuse = propertyData.MaxUse.Split(new char[] { '.' });

                    pythonCode_values.Add("classlink", maxuse[0]);
                    pythonCode_values.Add("property_link", maxuse[1]);

                    pythonCode.ReplaceValues(pythonCode_values);

                    functions_before += pythonCode;



                    functions += (functions.Equals(string.Empty)) ? "" : "\r\n\r\n\t";

                    FunctionModel pythonCode2 = Utils.GetFunctionModel("function_onchange_maxuse", FunctionUsage.Classic);

                    Dictionary<string, string> pythonCode2_values = new Dictionary<string, string>();

                    pythonCode2_values.Add("property_name", propertyData.Name);
                    pythonCode2_values.Add("classlink", maxuse[0]);

                    pythonCode2.ReplaceValues(pythonCode2_values);

                    functions += pythonCode2;
                }
            }

            foreach (PropertyData propertyData in classData.ListOfProperties)
            {
                if (propertyData.Typage.Equals("many2many"))
                {
                    RenderPropertyPy(classData, propertyData, ref fields, ref defaults, ViewKind.Form);
                }
            }

            foreach (PropertyData propertyData in classData.ListOfProperties)
            {
                if (propertyData.Typage.Equals("one2many"))
                {
                    RenderPropertyPy(classData, propertyData, ref fields, ref defaults, ViewKind.Form);
                }
            }


            Dictionary<string, OperationData> listOfOperationsData = classData.ListOfOperations;
            
            if (classData.StateProperty != null)
            {
                int i = 1;

                foreach (KeyValuePair<string, string> KeyValue in classData.StateProperty.SelectionData.ListOfValues)
                {
                    string key = KeyValue.Key;

                    OperationData operationData = new OperationData();
                    operationData.Parent = classData;
                    operationData.ListOfParameter = new List<ParameterData>();

                    FunctionModel pythonCode = new FunctionModel(FunctionUsage.Classic);

                    if (i == 1)
                    {
                        pythonCode = Utils.GetFunctionModel("function_first_action_wkf", FunctionUsage.Classic);
                    }
                    else
                    {
                        pythonCode = Utils.GetFunctionModel("function_action_wkf", FunctionUsage.Classic);
                    }

                    /*Dictionary<string, string> pythonCode_values = new Dictionary<string, string>();
                    pythonCode_values["function_name"] = "action_" + key + "_wkf";
                    pythonCode_values["state"] = key;

                    pythonCode.Content = Utils.replaceValues(pythonCode.Content, pythonCode_values) + "\r\n\t";*/

                    pythonCode.ReplaceValues("action_" + key + "_wkf", key);

                    operationData.Name = "action_" + key + "_wkf";
                    operationData.PythonCode = pythonCode.Content;

                    listOfOperationsData.Add(operationData.Name, operationData);

                    i++;
                }
            }

            Dictionary<string, string> accountArgs = Utils.FunctionArgs(classData.AccountingArgs);

            //string state = Utils.GetArg(accountArgs, "state", "");
            //string button = Utils.GetArg(accountArgs, "button", "");


            foreach (KeyValuePair<string, OperationData> operationDataKV in listOfOperationsData)
            {
                OperationData operationData = operationDataKV.Value;

                functions += (functions.Equals(string.Empty)) ? "" : "\r\n\r\n\t";

                string parameters = "";
                int i = 0;
                foreach (ParameterData param in operationData.ListOfParameter)
                {
                    if (!param.Name.Equals(""))
                    {
                        parameters += ", " + param.Name;
                    }
                    else
                    {
                        parameters += ", arg" + i.ToString();

                        i++;
                    }
                }

                if (operationData.PythonCode.Equals(""))
                {
                    functions += "def " + operationData.Name + "(self, cr, uid, ids" + parameters + ", context=None):\r\n";
                    functions += "\t\t# contenu généré, à modifier si besoin\r\n";
                    functions += "\t\treturn " + Utils.CorrectReturn(operationData.ReturnType);
                }
                else
                {
                    functions += operationData.PythonCode;
                }
            }


            //string add_after = "";
            foreach (KeyValuePair<string, string> constraint in classData.ListOfConstraints)
            {
                if (constraints_functions.Equals(""))
                {
                    constraints_functions = "\r\n\t";
                    //add_after += "\r\n";
                }

                string[] contrainte = constraint.Value.Split(new char[] { '\n' });

                string[] contrainte_property = contrainte[0].Trim().Split(new char[] { ':' });
                string[] contrainte_constraint = contrainte[1].Trim().Split(new char[] { ':' });
                string[] contrainte_message = contrainte[2].Trim().Split(new char[] { ':' });


                string[] con_properties = contrainte_property[1].Split(new char[] { ',' });
                string contrainte_properties = "";
                string virgule = "";
                foreach (string con_property in con_properties)
                {
                    contrainte_properties += virgule + "'" + con_property.Trim() + "'";
                    virgule = ",";
                }

                string[] operandes = contrainte_constraint[1].Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                string operande1 = operandes[0];
                string oprator = operandes[1];
                string operande2 = operandes[2];

                FunctionModel function_content = Utils.GetFunctionModel("function_constraint_operator", FunctionUsage.Classic);

                /*Dictionary<string, string> constraint_content_values = new Dictionary<string, string>();
                constraint_content_values["constraint_name"] = constraint.Key;
                constraint_content_values["operande1"] = operande1;
                constraint_content_values["operator"] = Utils.TrueString(oprator);
                constraint_content_values["operande2"] = operande2;

                function_content.Content = Utils.replaceValues(function_content.Content, constraint_content_values);*/

                function_content.ReplaceValues(constraint.Key, operande1, Utils.TrueString(oprator), operande2);

                constraints_functions += function_content + "\r\n\t";

                constraints += "(_" + constraint.Key + ", \"" + contrainte_message[1].Trim() + "\", [" + contrainte_properties + "]),\r\n\t\t";
            }

            if (!constraints.Equals("")) constraints = constraints.Substring(0, constraints.Length - 4);

            //constraints_functions += add_after;

            openerp_classValues["name"] = classData.ModuleName + "." + classData.Name;
            if (classData.Inherit != null)
            {
                openerp_classValues["name"] = "# delete this line";
                inherit = "_inherit = \"" + classData.Inherit.ModuleName + "." + classData.Inherit.Name + "\"";
            }

            openerp_classValues["classname"] = classData.Parent.ClassicName +"_" + classData.Name;
            openerp_classValues["description"] = classData.Documentation;
            openerp_classValues["inherit"] = inherit;
            openerp_classValues["fields"] = fields;
            openerp_classValues["defaults"] = defaults;
            openerp_classValues["constraints_functions"] = constraints_functions;
            openerp_classValues["constraints"] = constraints;
            openerp_classValues["sql_constraints"] = sql_constraints;
            openerp_classValues["functions"] = functions;
            openerp_classValues["functions_before"] = functions_before;



            string filePy = classData.Parent.DirectoryInfo_Base.FullName + "/" + classData.Parent.ClassicName + "_" + classData.Name + ".py";
            
            Utils.MakeFileFromModel("openerp_class.py.model", filePy, openerp_classValues);

            return filePy;
        }
        public static FunctionModel GetFunctionModel(string function, FunctionUsage functionUsage)
        {
            if (functionsModels == null)
            {
                functionsModels = new Dictionary<string, string>();

                StreamReader __model__ = File.OpenText(PathData.Models + "/openerp_scripts.py.model");

                string content = __model__.ReadToEnd();
                __model__.Close();

                string[] functs = content.Trim().Split(new string[] { "-- new script --" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string funct in functs)
                {
                    string[] lines = funct.Trim().Split(new char[] { '\n' });

                    string name = (lines[0].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries))[1].Trim();
                    string usage = (lines[1].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries))[1].Trim();

                    string cont = "";
                    for (int i = 2; i < lines.Length; i++)
                    {
                        cont += lines[i];
                    }

                    functionsModels[name + ":" + usage] = cont.Trim();
                }
            }

            FunctionModel functionModel = new FunctionModel(functionUsage);

            string usefor = functionUsage.ToString().ToLower();

            if (functionsModels.ContainsKey(function + ":" + usefor))
            {
                functionModel.Content = functionsModels[function + ":" + usefor];
                functionModel.ScanAllVariables();

                return functionModel;
            }
            else
            {
                functionModel.Content = functionsModels["empty_function:" + usefor];
                functionModel.ScanAllVariables();
                functionModel.Default = true;

                return functionModel;
            }
        }