Jelajahi Sumber

database.factory bux fixed

robert 8 tahun lalu
induk
melakukan
be0e981990

+ 1 - 1
www/js/controllers/configuration.controller.js

@@ -28,7 +28,7 @@ angular.module('odoo')
         $scope.auth = function () {
             $scope.loading = true;
 
-            odooFactory.auth($scope.configuration, function (id) {
+            odooFactory.auth(function (id) {
                 $scope.configuration.id = id;
 
                 databaseFactory.initTables(function (result) {

+ 28 - 12
www/js/controllers/customer.controller.js

@@ -1,5 +1,8 @@
 angular.module('odoo')
 
+    /**
+     *
+     */
     .controller('CustomersController', function (
         $scope,
         $ionicModal,
@@ -34,7 +37,9 @@ angular.module('odoo')
             $scope.customer = {};
         });
 
-        // Fill view list from data readed from local database
+        /**
+         *
+         */
         $scope.fill = function (refresh = false) {
             $scope.loading = !refresh;
 
@@ -45,7 +50,6 @@ angular.module('odoo')
                     $scope.$broadcast('scroll.refreshComplete');
                     $scope.loading = false;
                 }, function (getCustomersError) {
-                    console.log(getCustomersError);
 
                     $scope.$broadcast('scroll.refreshComplete');
                     $scope.loading = false;
@@ -53,14 +57,12 @@ angular.module('odoo')
                     $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
                 });
             }, function (syncErr) {
-                console.log(syncErr);
 
                 $scope.getCustomers(function () {
                     $scope.$broadcast('scroll.refreshComplete');
                     $scope.loading = false;
                 }, function (getCustomersError) {
-                    console.log(getCustomersError);
-                    
+
                     $scope.$broadcast('scroll.refreshComplete');
                     $scope.loading = false;
 
@@ -69,7 +71,9 @@ angular.module('odoo')
             });
         }
 
-        // Get filtered customers for correct data visualization
+        /**
+         *
+         */
         $scope.getCustomers = function (success, error) {
             sqlFactory.selectByConstraint('partner', 'customer = 1 AND modified != 2', function (customers) {
                 $scope.customers = [];
@@ -85,7 +89,9 @@ angular.module('odoo')
             });
         }
 
-        // Show action sheet for customer options
+        /**
+         *
+         */
         $scope.openOptions = function (index) {
             if (index == -1) {
                 $scope.customer = {};
@@ -131,7 +137,9 @@ angular.module('odoo')
             });
         }
 
-        // Save a customer data on local database
+        /**
+         *
+         */
         $scope.save = function () {
             customersStorageFactory.save($scope.customer, function (customerId) {
                 if (!$scope.customer.id) {
@@ -148,7 +156,9 @@ angular.module('odoo')
             });
         }
 
-        // Delete a customer from local database
+        /**
+         *
+         */
         $scope.delete = function () {
             $ionicPopup.confirm({
                 title: 'Confirmar',
@@ -171,12 +181,16 @@ angular.module('odoo')
             });
         }
 
-        // Show customer data in modal
+        /**
+         *
+         */
         $scope.show = function () {
             $scope.customerModal.show();
         }
 
-        // Take a picture from camera for customer profile
+        /**
+         *
+         */
         $scope.takePicture = function () {
             cameraFactory.takePicture(function (imageData) {
                 $scope.customer.image_small = imageData;
@@ -186,7 +200,9 @@ angular.module('odoo')
             });
         }
 
-        // Add customer data contact to device mobile contacts
+        /**
+         *
+         */
         $scope.addContact = function () {
             contactFactory.save($scope.customer, function (result) {
                 $ionicPopup.alert({ title: 'Contacto guardado' });

+ 209 - 183
www/js/controllers/lead.controller.js

@@ -1,213 +1,239 @@
 angular.module('odoo')
 
-.controller('LeadsController', function (
-    $scope,
-    $ionicModal,
-    $ionicActionSheet,
-    $ionicFilterBar,
-    $ionicPopup,
-    $cordovaVibration,
-    leadsRemoteFactory,
-    leadsStorageFactory,
-    opportunitiesStorageFactory,
-    sqlFactory
-) {
-
-    $scope.loading = false;
-    $scope.leads = [];
-    $scope.lead = {};
-    $scope.conversion = {
-        action: 'convert',
-        customer: 'create'
-    };
-    $scope.search = null;
-
-
-    $ionicModal.fromTemplateUrl('templates/sales/lead.html', {
-        scope: $scope,
-        animation: 'slide-in-up'
-    }).then(function(modal) {
-        $scope.leadModal = modal;
-    });
+    /**
+     *
+     */
+    .controller('LeadsController', function (
+        $scope,
+        $ionicModal,
+        $ionicActionSheet,
+        $ionicFilterBar,
+        $ionicPopup,
+        $cordovaVibration,
+        leadsRemoteFactory,
+        leadsStorageFactory,
+        opportunitiesStorageFactory,
+        sqlFactory
+    ) {
+
+        $scope.loading = false;
+        $scope.leads = [];
+        $scope.lead = {};
+        $scope.conversion = {
+            action: 'convert',
+            customer: 'create'
+        };
+        $scope.search = null;
+
+
+        $ionicModal.fromTemplateUrl('templates/sales/lead.html', {
+            scope: $scope,
+            animation: 'slide-in-up'
+        }).then(function (modal) {
+            $scope.leadModal = modal;
+        });
 
-    $ionicModal.fromTemplateUrl('templates/sales/leadToOpportunity.html', {
-        scope: $scope,
-        animation: 'slide-in-up'
-    }).then(function(modal) {
-        $scope.leadToOpportunityModal = modal;
-    });
+        $ionicModal.fromTemplateUrl('templates/sales/leadToOpportunity.html', {
+            scope: $scope,
+            animation: 'slide-in-up'
+        }).then(function (modal) {
+            $scope.leadToOpportunityModal = modal;
+        });
 
-    $scope.$on('$ionicView.enter', function () {
-        $scope.fill();
-    });
+        $scope.$on('$ionicView.enter', function () {
+            $scope.fill();
+        });
 
-    $scope.$on('$destroy', function() {
-        $scope.leadModal.remove();
-        $scope.leadToOpportunityModal.remove();
-    });
+        $scope.$on('$destroy', function () {
+            $scope.leadModal.remove();
+            $scope.leadToOpportunityModal.remove();
+        });
 
-    $scope.$on('modal.hidden', function() {
-        $scope.lead = {};
-    });
+        $scope.$on('modal.hidden', function () {
+            $scope.lead = {};
+        });
 
-    $scope.fill = function (refresh = false) {
-        $scope.loading = !refresh;
+        /**
+         *
+         */
+        $scope.fill = function (refresh = false) {
+            $scope.loading = !refresh;
 
-        leadsRemoteFactory.sync(function (leads) {
-            console.log('Leads sync correctly and receive ' + leads.length + ' items');
+            leadsRemoteFactory.sync(function (leads) {
+                console.log('Leads sync correctly and receive ' + leads.length + ' items');
 
-            $scope.getLeads(function () {
-                $scope.$broadcast('scroll.refreshComplete');
-                $scope.loading = false;
-            }, function (getLeadsErr) {
-                $scope.$broadcast('scroll.refreshComplete');
-                $scope.loading = false;
+                $scope.getLeads(function () {
+                    $scope.$broadcast('scroll.refreshComplete');
+                    $scope.loading = false;
+                }, function (getLeadsErr) {
+                    $scope.$broadcast('scroll.refreshComplete');
+                    $scope.loading = false;
 
-                $ionicPopup.alert({ title: 'No se pudo obtener las iniciativas' });
+                    $ionicPopup.alert({ title: 'No se pudo obtener las iniciativas' });
+                });
+            }, function (syncErr) {
+                $scope.getLeads(function () {
+                    $scope.$broadcast('scroll.refreshComplete');
+                    $scope.loading = false;
+                }, function (getLeadsErr) {
+                    $scope.$broadcast('scroll.refreshComplete');
+                    $scope.loading = false;
+
+                    $ionicPopup.alert({ title: 'No se pudo obtener las iniciativas' });
+                });
             });
-        }, function (syncErr) {
+        }
 
-            $scope.getLeads(function () {
-                $scope.$broadcast('scroll.refreshComplete');
-                $scope.loading = false;
-            }, function (getLeadsErr) {
-                $scope.$broadcast('scroll.refreshComplete');
-                $scope.loading = false;
+        /**
+         *
+         */
+        $scope.getLeads = function (success, error) {
+            sqlFactory.selectByConstraint('crm_lead', "type = 'lead' AND modified != 2", function (leads) {
+                console.log(leads);
+                $scope.leads = [];
 
-                $ionicPopup.alert({ title: 'No se pudo obtener las iniciativas' });
-            });
-        });
-    }
+                for (var i = 0; i < leads.length; i++) {
+                    $scope.leads.push(leads.item(i));
+                }
 
-    $scope.getLeads = function (success, error) {
-        sqlFactory.selectByConstraint('lead', "type = 'lead' AND modified != 2", function (leads) {
-            $scope.leads = [];
+                $scope.$apply();
+                success();
+            }, function (sqlFactoryError) {
+                error(sqlFactoryError);
+            });
+        }
 
-            for (var i = 0; i < leads.length; i++) {
-                $scope.leads.push(leads.item(i));
-            }
+        /**
+         *
+         */
+        $scope.show = function (event) {
+            $scope.leadModal.show();
+        }
 
-            $scope.$apply();
-            success();
-        }, function (sqlFactoryError) {
-            error(sqlFactoryError);
-        });
-    }
+        /**
+         *
+         */
+        $scope.save = function () {
+            leadsStorageFactory.save($scope.lead, function (leadId) {
+                if (!$scope.lead.id) {
+                    $scope.lead.id = leadId;
+                    $scope.leads.push($scope.lead);
+                }
 
-    $scope.show = function (event) {
-        $scope.leadModal.show();
-    }
+                $scope.lead = {};
+                $scope.leadModal.hide();
+                console.log('Lead saved');
+            }, function (error) {
+                $ionicPopup.alert({ title: 'No se ha podido guardar la iniciativa', template: JSON.stringify(error) });
+                console.log(JSON.stringify(error));
+            });
+        }
 
-    $scope.save = function () {
-        leadsStorageFactory.save($scope.lead, function (leadId) {
-            if (!$scope.lead.id) {
-                $scope.lead.id = leadId;
-                $scope.leads.push($scope.lead);
-            }
+        /**
+         *
+         */
+        $scope.delete = function () {
+            $ionicPopup.confirm({
+                title: 'Confirmar',
+                template: 'Estás seguro que quieres eliminar esta iniciativa?'
+            }).then(function (confirmation) {
+                if (confirmation) {
+                    leadsStorageFactory.remove($scope.lead, function (affected) {
+                        if (affected != 0) {
+                            var index = $scope.leads.indexOf($scope.lead);
+                            $scope.leads.splice(index, 1);
+                            $scope.lead = {};
+
+                            $scope.$apply();
+                        }
+                    }, function (error) {
+                        $ionicPopup.alert({ title: 'No se puedo eliminar la iniciativa', template: JSON.stringify(error) });
+                        console.log(JSON.stringify(error));
+                    });
+                }
+            });
+        }
 
-            $scope.lead = {};
-            $scope.leadModal.hide();
-            console.log('Lead saved');
-        }, function (error) {
-            $ionicPopup.alert({ title: 'No se ha podido guardar la iniciativa', template: JSON.stringify(error) });
-            console.log(JSON.stringify(error));
-        });
-    }
-
-    $scope.delete = function () {
-        $ionicPopup.confirm({
-            title: 'Confirmar',
-            template: 'Estás seguro que quieres eliminar esta iniciativa?'
-        }).then(function (confirmation) {
-            if(confirmation) {
-                leadsStorageFactory.remove($scope.lead, function (affected) {
-                    if (affected != 0) {
-                        var index = $scope.leads.indexOf($scope.lead);
-                        $scope.leads.splice(index, 1);
-                        $scope.lead = {};
-
-                        $scope.$apply();
-                    }
-                }, function (error) {
-                    $ionicPopup.alert({ title: 'No se puedo eliminar la iniciativa', template: JSON.stringify(error) });
-                    console.log(JSON.stringify(error));
-                });
-            }
-        });
-    }
+        /**
+         *
+         */
+        $scope.convertToOpportunity = function () {
+            opportunitiesStorageFactory.save($scope.lead, function (opportunityId) {
+                $scope.leadToOpportunityModal.hide();
 
-    $scope.convertToOpportunity = function () {
-        console.log('ok');
-        opportunitiesStorageFactory.save($scope.lead, function (opportunityId) {
-            $scope.leadToOpportunityModal.hide();
+                var index = $scope.leads.indexOf($scope.lead);
+                $scope.leads.splice(index, 1);
+                $scope.lead = {};
 
-            var index = $scope.leads.indexOf($scope.lead);
-            $scope.leads.splice(index, 1);
-            $scope.lead = {};
+                $scope.$apply();
+            }, function (saveErr) {
+                $scope.leadToOpportunityModal.hide();
+                $ionicPopup.alert({
+                    title: 'No se puedo convertir la iniciativa',
+                    template: JSON.stringify(saveErr)
+                });
+            });
+        }
 
-            $scope.$apply();
-        }, function (saveErr) {
-            $scope.leadToOpportunityModal.hide();
-            $ionicPopup.alert({
-                title: 'No se puedo convertir la iniciativa',
-                template: JSON.stringify(saveErr)
+        /**
+         *
+         */
+        $scope.toggleSearch = function () {
+            $scope.search = $ionicFilterBar.show({
+                items: $scope.leads,
+                update: function (filtered, text) {
+                    $scope.leads = filtered;
+                }
             });
-        });
-    }
+        }
 
-    $scope.toggleSearch = function () {
-        $scope.search = $ionicFilterBar.show({
-            items: $scope.leads,
-            update: function (filtered, text) {
-                $scope.leads = filtered;
-            }
-        });
-    }
+        /**
+         *
+         */
+        $scope.openOptions = function (index) {
+            $cordovaVibration.vibrate(100);
 
-    $scope.openOptions = function (index) {
-        $cordovaVibration.vibrate(100);
-        
-        if (index == -1) {
-            $scope.lead = {};
-        } else {
-            $scope.lead = $scope.leads[index];
-        }
+            if (index == -1) {
+                $scope.lead = {};
+            } else {
+                $scope.lead = $scope.leads[index];
+            }
 
             console.log('Customer selected => ' + JSON.stringify($scope.lead));
 
-        $ionicActionSheet.show({
-            titleText: 'Acciones',
-            buttons: [
-                {
-                    text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
+            $ionicActionSheet.show({
+                titleText: 'Acciones',
+                buttons: [
+                    {
+                        text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
+                    },
+                    {
+                        text: '<i class="icon ion-wand positive"></i> Convertir a oportunidad'
+                    }
+                ],
+                destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
+                cancel: function () {
+                    $scope.lead = {};
+                    console.log('ActionSheet canceled');
                 },
-                {
-                    text: '<i class="icon ion-wand positive"></i> Convertir a oportunidad'
-                }
-            ],
-            destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
-            cancel: function() {
-                $scope.lead = {};
-                console.log('ActionSheet canceled');
-            },
-            buttonClicked: function(index) {
-                switch (index) {
-                    case 0:
-                        $scope.show();
-                        break;
-                    case 1:
-                        $scope.leadToOpportunityModal.show();
-                        break;
-                    default:
-                        $scope.show();
-                }
-                return true;
-            },
-            destructiveButtonClicked: function() {
-                $scope.delete();
+                buttonClicked: function (index) {
+                    switch (index) {
+                        case 0:
+                            $scope.show();
+                            break;
+                        case 1:
+                            $scope.leadToOpportunityModal.show();
+                            break;
+                        default:
+                            $scope.show();
+                    }
+                    return true;
+                },
+                destructiveButtonClicked: function () {
+                    $scope.delete();
 
-                return true;
-            }
-        });
-    }
-});
+                    return true;
+                }
+            });
+        }
+    });

+ 4 - 2
www/js/factories/database.factory.js

@@ -39,11 +39,11 @@ angular.module('odoo')
                         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') {
+                    if (model[key].type == 'integer' || model[key].type == 'selection' || model[key].type == 'boolean' || model[key].type == 'many2one' || model[key].type == 'many2many') {
                         statement += key + ' INTEGER DEFAULT 0,\n';
                     }
 
-                    if (model[key].type == 'many2one') {
+                    if (model[key].type == 'one2many') {
                         continue;
                     }
                 }
@@ -52,6 +52,8 @@ angular.module('odoo')
             statement += 'remote_id INTEGER DEFAULT 0,\n' +
                 'modified INTEGER DEFAULT 0,\n' +
                 'modified_date TEXT DEFAULT CURRENT_TIMESTAMP\n';
+            
+            console.log(statement);
 
             return statement + ');'
         }

+ 12 - 10
www/js/factories/sales/lead.storage.factory.js

@@ -20,17 +20,18 @@ angular.module('odoo')
                 .set('fax', data.fax)
                 .set('street', data.street)
                 .set('street2', data.street2)
-                .set('meeting_count', data.meeting_coun)
+                .set('meeting_count', data.meeting_count)
                 .set('message_bounce', data.message_bounce)
                 .set('planned_cost', data.planned_cost)
                 .set('planned_revenue', data.planned_revenue)
                 .set('priority', data.priority)
                 .set('probability', data.probability)
                 .set('type', data.type)
-                .set('stage_id', data.stage_id)
+                .set('stage_id', typeof data.stage_id == 'object' ? data.stage_id[0] : data.stage_id)
                 .set('user_id', data.user_id)
                 .set('partner_id', data.partner_id)
-                .where('id', data.id);
+                .where('id', data.id)
+                .toParam();
         } else {
             query = squel.insert()
                 .into('crm_lead')
@@ -51,16 +52,17 @@ angular.module('odoo')
                 .set('priority', data.priority)
                 .set('probability', data.probability)
                 .set('type', data.type)
-                .set('stage_id', typeof data.stage_id == 'number' ? data.stage_id : parseInt(data.stage_id.split(',')[0]))
+                .set('stage_id', typeof data.stage_id == 'object' ? data.stage_id[0] : data.stage_id)
                 .set('user_id', data.user_id)
-                .set('partner_id', data.partner_id);
+                .set('partner_id', data.partner_id)
+                .toParam();
         }
-
-        query = query.toParam();
        
         db.executeSql(query.text, query.values, function (result) {
-            success(query.text.startsWith('INSERT') ? result.insertId : lead.id);
+            console.log(result);
+            success(query.text.startsWith('INSERT') ? result.insertId : data.id);
         }, function (err) {
+            console.log(err);
             error(err);
         });
     }
@@ -96,11 +98,11 @@ angular.module('odoo')
      */
     var removeAll = function (success, error) {
         var query = squel.delete()
-            .from('lead')
+            .from('crm_lead')
             .where('type', 'lead')
             .toParam();
 
-        db.executeSql(query.text, query.values, function(result) {
+        db.executeSql(query.text, query.values, function (result) {
             success(result.rowsAffected);
         }, function(err) {
             error(err);

+ 1 - 2
www/js/factories/sales/lead.sync.factory.js

@@ -107,7 +107,7 @@ angular.module('odoo')
          *
          */
         var get = function (contraint, success, error) {
-            sqlFactory.selectByConstraint('lead', contraint, function (leads) {
+            sqlFactory.selectByConstraint('crm_lead', contraint, function (leads) {
                 success(leads);
             }, function (err) {
                 error(err);
@@ -190,7 +190,6 @@ angular.module('odoo')
          */
         var downloadSyncData = function (success, error) {
             pull(null, function (leads) {
-
                 leadsStorageFactory.removeAll(function () {
                     asyncLoopFactory(leads.length, function (loop) {
                         var data = leads[loop.iteration()];