浏览代码

sql init tables engine changed

robert 8 年之前
父节点
当前提交
a258b2836c

+ 1 - 0
www/index.html

@@ -40,6 +40,7 @@
     <script src="js/factories/preferences.factory.js"></script>
     <script src="js/factories/preferences.factory.js"></script>
     <script src="js/factories/odoo.factory.js"></script>
     <script src="js/factories/odoo.factory.js"></script>
     <script src="js/factories/user.storage.factory.js"></script>
     <script src="js/factories/user.storage.factory.js"></script>
+    <script src="js/factories/database.factory.js"></script>
     <script src="js/factories/sales/customer.storage.factory.js"></script>
     <script src="js/factories/sales/customer.storage.factory.js"></script>
     <script src="js/factories/sales/customer.sync.factory.js"></script>
     <script src="js/factories/sales/customer.sync.factory.js"></script>
     <script src="js/factories/sales/lead.storage.factory.js"></script>
     <script src="js/factories/sales/lead.storage.factory.js"></script>

+ 12 - 1
www/init.sql

@@ -76,4 +76,15 @@ CREATE TABLE IF NOT EXISTS 'crm_stage' (
     'name' VARCHAR(35) DEFAULT NULL,
     'name' VARCHAR(35) DEFAULT NULL,
     'probability' REAL DEFAULT 0,
     'probability' REAL DEFAULT 0,
     'type' VARCHAR(100) DEFAULT 'both'
     'type' VARCHAR(100) DEFAULT 'both'
-);
+);
+
+// datetime
+// boolean
+// one2many
+// char
+// many2many
+// text
+// integer
+// many2one
+// float
+// selection

+ 2 - 14
www/js/app.js

@@ -13,8 +13,7 @@ angular.module(
     .run(function (
     .run(function (
         $ionicPlatform,
         $ionicPlatform,
         $state,
         $state,
-        $cordovaSQLite,
-        $cordovaFile,
+        databaseFactory,
         configFactory
         configFactory
     ) {
     ) {
 
 
@@ -32,18 +31,7 @@ angular.module(
             if (window.sqlitePlugin) {
             if (window.sqlitePlugin) {
                 db = sqlitePlugin.openDatabase({ name: 'odoo.db', location: 'default' });
                 db = sqlitePlugin.openDatabase({ name: 'odoo.db', location: 'default' });
 
 
-                $cordovaFile.readAsText(cordova.file.applicationDirectory, 'www/init.sql').then(function (query) {
-                    db.transaction(function (tx) {
-                        tx.executeSql(query);
-                    }, function (txErr) {
-                        console.log(txErr);
-                        ionic.Platform.exitApp();
-                    }, function () {
-                        console.log('DB ready')
-                    });
-                }).catch(function (readErr) {
-                    ionic.Platform.exitApp();
-                });
+                databaseFactory.initTables();
 
 
                 configFactory(function (configuration) {
                 configFactory(function (configuration) {
                     if (configuration) {
                     if (configuration) {

+ 1 - 12
www/js/controllers/sale.controller.js

@@ -1,15 +1,4 @@
 angular.module('odoo')
 angular.module('odoo')
 
 
-    .controller('SaleController', function ($scope, odooFactory, configFactory) {
-
-        configFactory(function (configuration) {
-            odooFactory.fields('res.partner', configuration, function (ok) {
-                console.log(ok);
-            }, function (fail) {
-                console.log(fail);
-            });
-        }, function (err) {
-
-        });
-
+    .controller('SaleController', function ($scope, databaseFactory) {
     });
     });

+ 115 - 0
www/js/factories/database.factory.js

@@ -0,0 +1,115 @@
+angular.module('odoo')
+
+    /**
+     *
+     */
+    .factory('databaseFactory', function (odooFactory, configFactory, asyncLoopFactory) {
+
+        var mappings = [
+            {
+                model: 'res.partner',
+                table: 'partner'
+            },
+            {
+                model: 'crm.lead',
+                table: 'crm_lead'
+            },
+            {
+                model: 'crm.case.stage',
+                table: 'stages'
+            }
+        ];
+
+        /**
+         *
+         */
+        var buildCreateStatement = function (table, model) {
+            var statement = 'CREATE TABLE IF NOT EXISTS ' + table + ' (\n';
+
+            for (var key in model) {
+                if (model.hasOwnProperty(key) && typeof model[key] == 'object') {
+                    if (model[key].type == 'float') {
+                        statement += key + ' REAL DEFAULT 0,\n';
+                    }
+
+                    if (model[key].type == 'char' || model[key].type == 'text' || model[key].type == 'datetime') {
+                        statement += key + ' TEXT DEFAULT NULL,\n';
+                    }
+
+                    if (model[key].type == 'integer' || model[key].type == 'selection' || model[key].type == 'boolean' || model[key].type == 'one2many' || model[key].type == 'many2many') {
+                        statement += key + ' INTEGER DEFAULT 0,\n';
+                    }
+
+                    if (model[key].type == 'many2one') {
+                        continue;
+                    }
+                }
+            }
+
+            statement += 'remote_id INTEGER DEFAULT 0,\n' +
+                'modified INTEGER DEFAULT 0,\n' +
+                'modified_date TEXT DEFAULT CURRENT_TIMESTAMP\n';
+
+            return statement + ');'
+        }
+
+        /**
+         *
+         */
+        var fetchMaps = function (success, error) {
+            var maps = [];
+
+            configFactory(function (configuration) {
+                asyncLoopFactory(mappings.length, function (loop) {
+                    var map = mappings[loop.iteration()];
+
+                    odooFactory.fields(map.model, configuration, function (response) {
+                        maps.push({ table: map.table, model: response });
+
+                        loop.next();
+                    }, function (odooErr) {
+                        console.log(odooErr);
+
+                        loop.next();
+                    });
+
+                }, function () {
+                    success(maps);
+                });
+            }, function (configErr) {
+                error(configErr);
+            });
+        }
+
+        /**
+         *
+         */
+        var initTables = function (success, error) {
+            fetchMaps(function (maps) {
+
+                asyncLoopFactory(maps.length, function (loop) {
+                    var map = maps[loop.iteration()];
+                    var sql = buildCreateStatement(map.table, map.model);
+
+                    db.executeSql(sql, [], function (result) {
+                        console.log(result);
+                        loop.next();
+                    }, function (err) {
+                        console.log(err);
+                        loop.next();
+                    });
+
+                }, function () {
+                    success(maps);
+                });
+
+            }, function (err) {
+                error(err);
+            });
+        }
+
+        return {
+            fetchMaps: fetchMaps,
+            initTables: initTables
+        }
+    });

+ 1 - 1
www/js/factories/odoo.factory.js

@@ -74,7 +74,7 @@ angular.module('odoo')
                 });
                 });
             },
             },
             fields: function (model, config, success, error) {
             fields: function (model, config, success, error) {
-                xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'fields_get', [], { 'attributes': ['string', 'type', 'length'] }]).then(function (response) {
+                xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'fields_get', [], { 'attributes': ['type', 'readonly', 'required', 'relation', 'relation_field'] }]).then(function (response) {
                     if (!response || response['faultCode']) {
                     if (!response || response['faultCode']) {
                         return error(response);
                         return error(response);
                     }
                     }

+ 214 - 200
www/js/factories/utils.factory.js

@@ -1,216 +1,230 @@
 angular.module('odoo')
 angular.module('odoo')
 
 
-/**
- * -----------------------------------------------------------------------------
- *  Description:    Native SQL util instructions
- * -----------------------------------------------------------------------------
- */
-.factory('sqlFactory', function () {
-    // Execute native SQL SELECT instruction
-    var select = function(tableName, success, error) {
-        var sql = 'SELECT * FROM ' + tableName;
-
-        db.executeSql(sql, [], function(result) {
-            success(result.rows);
-        }, function(err) {
-            error(err);
-        });
-    };
-
-    // Execute native SQL SELECT instruction with a constraint
-    var selectByConstraint = function(tableName, constraint, success, error) {
-        var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;
-
-        db.executeSql(sql, [], function(result) {
-            success(result.rows);
-        }, function(err) {
-            error(err);
-        });
-    };
-
-    // Execute native SQL SELECT instruction with count instruction
-    var count = function(tableName, success, error) {
-        var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
-
-        db.executeSql(sql, [], function(result) {
-            success(result.rows.item(0).total);
-        }, function(err) {
-            error(err);
-        });
-    };
-
-    return {
-        select: select,
-        selectByConstraint: selectByConstraint,
-        count: count
-    }
-})
-
-/**
- * -----------------------------------------------------------------------------
- *  Description:    Get user configuration from local database
- * -----------------------------------------------------------------------------
- */
-.factory('configFactory', function (sqlFactory) {
-    return function (success, error) {
-        sqlFactory.select('user', function (users) {
-            if (users.length == 0) {
-                success(0);
-            } else if (users.length == 1) {
-                success(users.item(0));
-            } else {
-                var configs = [];
-                for (var i = 0; i < users.length; i++) {
-                    configs.push(users.item(i))
-                }
+    /**
+     * -----------------------------------------------------------------------------
+     *  Description:    Native SQL util instructions
+     * -----------------------------------------------------------------------------
+     */
+    .factory('sqlFactory', function () {
+
+        /**
+         *
+         */
+        var count = function (tableName, success, error) {
+            var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
+
+            db.executeSql(sql, [], function (result) {
+                success(result.rows.item(0).total);
+            }, function (err) {
+                error(err);
+            });
+        };
 
 
-                success(configs);
-            }
-        }, function (err) {
-            error(err);
-        });
-    };
-})
-
-/**
- * -----------------------------------------------------------------------------
- *  Description:    Async loop util v2
- * -----------------------------------------------------------------------------
- */
-.factory('asyncLoopFactory', function () {
-    return function (iterations, func, callback) {
-                var index = 0;
-                var done = false;
-                var loop = {
-                    next: function  () {
-                        if (done) {
-                            return;
-                        }
-
-                        if (index < iterations) {
-                            index++;
-                            func(loop);
-
-                        } else {
-                            done = true;
-                            callback();
-                        }
-                    },
+        // Execute native SQL SELECT instruction
+        var select = function (tableName, success, error) {
+            var sql = 'SELECT * FROM ' + tableName;
 
 
-                    iteration: function() {
-                        return index - 1;
-                    },
+            db.executeSql(sql, [], function (result) {
+                success(result.rows);
+            }, function (err) {
+                error(err);
+            });
+        };
+
+        // Execute native SQL SELECT instruction with a constraint
+        var selectByConstraint = function (tableName, constraint, success, error) {
+            var sql = 'SELECT * FROM ' + tableName + ' WHERE ' + constraint;
+
+            db.executeSql(sql, [], function (result) {
+                success(result.rows);
+            }, function (err) {
+                error(err);
+            });
+        };
+
+        // Execute native SQL SELECT instruction with count instruction
+        var count = function (tableName, success, error) {
+            var sql = 'SELECT COUNT(*) AS total FROM ' + tableName;
+
+            db.executeSql(sql, [], function (result) {
+                success(result.rows.item(0).total);
+            }, function (err) {
+                error(err);
+            });
+        };
+
+        return {
+            select: select,
+            selectByConstraint: selectByConstraint,
+            count: count
+        }
+    })
+
+    /**
+     * -----------------------------------------------------------------------------
+     *  Description:    Get user configuration from local database
+     * -----------------------------------------------------------------------------
+     */
+    .factory('configFactory', function (sqlFactory) {
+        return function (success, error) {
+            sqlFactory.select('user', function (users) {
+                if (users.length == 0) {
+                    success(0);
+                } else if (users.length == 1) {
+                    success(users.item(0));
+                } else {
+                    var configs = [];
+                    for (var i = 0; i < users.length; i++) {
+                        configs.push(users.item(i))
+                    }
+
+                    success(configs);
+                }
+            }, function (err) {
+                error(err);
+            });
+        };
+    })
+
+    /**
+     * -----------------------------------------------------------------------------
+     *  Description:    Async loop util v2
+     * -----------------------------------------------------------------------------
+     */
+    .factory('asyncLoopFactory', function () {
+        return function (iterations, func, callback) {
+            var index = 0;
+            var done = false;
+            var loop = {
+                next: function () {
+                    if (done) {
+                        return;
+                    }
+
+                    if (index < iterations) {
+                        index++;
+                        func(loop);
 
 
-                    break: function() {
+                    } else {
                         done = true;
                         done = true;
                         callback();
                         callback();
                     }
                     }
-                };
+                },
 
 
-                loop.next();
-                return loop;
-            }
-})
-
-/**
- * -----------------------------------------------------------------------------
- *  Description:    Native camera manager
- * -----------------------------------------------------------------------------
- */
-.factory('cameraFactory', function ($cordovaCamera) {
-
-    // Take a picture using native camera
-    var takePicture = function (success, error) {
-        var options = {
-            quality: 75,
-            destinationType: Camera.DestinationType.DATA_URL,
-            sourceType: Camera.PictureSourceType.CAMERA,
-            allowEdit: true,
-            encodingType: Camera.EncodingType.JPEG,
-            targetWidth: 300,
-            targetHeight: 300,
-            popoverOptions: CameraPopoverOptions,
-            saveToPhotoAlbum: false,
-            correctOrientation:true
+                iteration: function () {
+                    return index - 1;
+                },
+
+                break: function () {
+                    done = true;
+                    callback();
+                }
+            };
+
+            loop.next();
+            return loop;
+        }
+    })
+
+    /**
+     * -----------------------------------------------------------------------------
+     *  Description:    Native camera manager
+     * -----------------------------------------------------------------------------
+     */
+    .factory('cameraFactory', function ($cordovaCamera) {
+
+        // Take a picture using native camera
+        var takePicture = function (success, error) {
+            var options = {
+                quality: 75,
+                destinationType: Camera.DestinationType.DATA_URL,
+                sourceType: Camera.PictureSourceType.CAMERA,
+                allowEdit: true,
+                encodingType: Camera.EncodingType.JPEG,
+                targetWidth: 300,
+                targetHeight: 300,
+                popoverOptions: CameraPopoverOptions,
+                saveToPhotoAlbum: false,
+                correctOrientation: true
+            };
+
+            $cordovaCamera.getPicture(options).then(function (imageData) {
+                success(imageData);
+            }, function (err) {
+                error(err);
+            });
         };
         };
 
 
-        $cordovaCamera.getPicture(options).then(function(imageData) {
-            success(imageData);
-        }, function(err) {
-            error(err);
-        });
-    };
-
-    return {
-        takePicture: takePicture
-    }
-})
-
-/**
- * -----------------------------------------------------------------------------
- *  Description:    Native contacts manager
- * -----------------------------------------------------------------------------
- */
-.factory('contactFactory', function ($cordovaContacts) {
-
-    // Save customer information to device contacts
-    var save = function (customer, success, error) {
-        if(!customer.mobile && !customer.phone && !customer.email) {
-            error();
-            return;
+        return {
+            takePicture: takePicture
         }
         }
+    })
+
+    /**
+     * -----------------------------------------------------------------------------
+     *  Description:    Native contacts manager
+     * -----------------------------------------------------------------------------
+     */
+    .factory('contactFactory', function ($cordovaContacts) {
+
+        // Save customer information to device contacts
+        var save = function (customer, success, error) {
+            if (!customer.mobile && !customer.phone && !customer.email) {
+                error();
+                return;
+            }
 
 
-        var contact = {
-            name: {
-                givenName: customer.name,
-                familyName: '',
-                formatted: ''
-            },
-            nickname: '',
-            phoneNumbers: [
-                {
-                    value: customer.phone,
-                    type: 'phone'
+            var contact = {
+                name: {
+                    givenName: customer.name,
+                    familyName: '',
+                    formatted: ''
                 },
                 },
-                {
-                    value: customer.mobile,
-                    type: 'mobile'
-                }
-            ],
-            emails: [
-                {
-                    value: customer.email,
-                    type: 'home'
-                }
-            ],
-            addresses: [
-                {
-                    type: 'home',
-                    formatted: '',
-                    streetAddress: customer.street,
-                    locality: customer.city,
-                    region: '',
-                    postalCode: '',
-                    country: 'Paraguay'
-                }
-            ],
-            ims: null,
-            organizations: null,
-            birthday: null,
-            note: null,
-            photos: null,
-            categories: null,
-            urls: null
+                nickname: '',
+                phoneNumbers: [
+                    {
+                        value: customer.phone,
+                        type: 'phone'
+                    },
+                    {
+                        value: customer.mobile,
+                        type: 'mobile'
+                    }
+                ],
+                emails: [
+                    {
+                        value: customer.email,
+                        type: 'home'
+                    }
+                ],
+                addresses: [
+                    {
+                        type: 'home',
+                        formatted: '',
+                        streetAddress: customer.street,
+                        locality: customer.city,
+                        region: '',
+                        postalCode: '',
+                        country: 'Paraguay'
+                    }
+                ],
+                ims: null,
+                organizations: null,
+                birthday: null,
+                note: null,
+                photos: null,
+                categories: null,
+                urls: null
+            };
+
+            $cordovaContacts.save(contact).then(function (result) {
+                success(result);
+            }, function (err) {
+                error(err);
+            });
         };
         };
 
 
-        $cordovaContacts.save(contact).then(function (result) {
-            success(result);
-        }, function (err) {
-            error(err);
-        });
-    };
-
-    return {
-        save: save
-    }
-});
+        return {
+            save: save
+        }
+    });