Browse Source

image avatar in form added

robert2206 8 years ago
parent
commit
1ddc8c949e
5 changed files with 213 additions and 158 deletions
  1. 10 3
      www/css/app.css
  2. 1 0
      www/index.html
  3. 46 69
      www/js/controllers.js
  4. 154 86
      www/js/factories.js
  5. 2 0
      www/templates/customer.html

+ 10 - 3
www/css/app.css

@@ -1,5 +1,12 @@
 /* Empty. Add your own CSS if you like */
-
-.capitalize {
-    text-transform: capitalize;
+.profile {
+    width: 150px;
+    height: 150px;
+    background-size: cover;
+    display: block;
+    margin-left: auto;
+    margin-right: auto;
+    margin-top: 10px;
+    margin-bottom: 10px;
+    border-radius: 50px;
 }

+ 1 - 0
www/index.html

@@ -7,6 +7,7 @@
     <title></title>
 
     <link href="css/ionic.app.min.css" rel="stylesheet">
+    <link href="css/app.css" rel="stylesheet">
 
     <script src="lib/ionic/js/ionic.bundle.js"></script>
     <script src="lib/ngCordova/dist/ng-cordova.min.js"></script>

+ 46 - 69
www/js/controllers.js

@@ -28,14 +28,40 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
         $scope.fill();
     });
 
+    $scope.$on('$destroy', function() {
+        $scope.customerModal.remove();
+    });
+
+    $scope.$on('modal.hidden', function() {
+        $scope.customer = {};
+    });
+
     // Fill view list
     $scope.fill = function () {
-        // sync.syncCustomers(function (customers) {
-        //     console.log(customers);
-        // }, function (error) {
-        //     console.log(error);
-        // });
+        $ionicLoading.show();
 
+        sync.syncCustomers(function (customers) {
+            $log.info(customers);
+
+            $scope.getCustomers(function () {
+                $scope.$broadcast('scroll.refreshComplete');
+                $ionicLoading.hide();
+            }, function (getCustomersError) {
+                $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
+            });
+        }, function (syncCustomersError) {
+            $log.error(syncCustomersError);
+
+            $scope.getCustomers(function () {
+                $scope.$broadcast('scroll.refreshComplete');
+                $ionicLoading.hide();
+            }, function (getCustomersError) {
+                $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
+            });
+        });
+    }
+
+    $scope.getCustomers = function (success, error) {
         storage.getByConstraint('partner', 'customer = 1', function (customers) {
             $scope.customers = [];
 
@@ -44,41 +70,10 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
             }
 
             $scope.$apply();
-            $scope.$broadcast('scroll.refreshComplete');
-        }, function (error) {
-            $log.error(error);
+            success();
+        }, function (customerGetByConstraintError) {
+            error(customerGetByConstraintError);
         });
-
-        // $ionicLoading.show();
-        //
-        // storage.get('user', function (users) {
-        //     if (users.length == 1) {
-        //         var userConfig = users.item(0);
-        //
-        //         odoo.fetch('res.partner', [[['customer', '=', true]]], userConfig, function (customers) {
-        //             $ionicLoading.hide();
-        //             $scope.customers = customers;
-        //             $scope.$broadcast('scroll.refreshComplete');
-        //         }, function (error) {
-        //             $ionicLoading.hide();
-        //             console.error(error);
-        //
-        //             var message = 'Configuración no válida';
-        //             if (angular.isObject(error)) {
-        //                 message = 'Credenciales no válidas';
-        //             }
-        //
-        //             $ionicPopup.alert({ title: 'No se pudo establecer una conexión al servidor', template: message });
-        //         });
-        //     } else {
-        //         $ionicLoading.hide();
-        //         $ionicPopup.alert({ title: 'Nada que hacer' });
-        //     }
-        // }, function (error) {
-        //     $ionicLoading.hide();
-        //     console.error(error);
-        //     $ionicPopup.alert({ title: 'Atención', template: 'No se pudo ejecutar la instrucción SQL' });
-        // });
     }
 
     $scope.openOptions = function(index) {
@@ -127,40 +122,17 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
     }
 
     $scope.save = function() {
-        var data = [0,  // remote_id
-                    $scope.customer.name,
-                    $scope.customer.city,
-                    $scope.customer.mobile,
-                    $scope.customer.phone,
-                    $scope.customer.fax,
-                    $scope.customer.email,
-                    $scope.customer.street,
-                    $scope.customer.street2,
-                    $scope.customer.image_medium,
-                    $scope.customer.image_small,
-                    $scope.customer.comment,
-                    1,  // customer
-                    0,  // employee
-                    0,  // is_company
-                    0,  // debit
-                    0,  // debit_limit
-                    0,  // opportunity_count
-                    0,  // contracts_count
-                    0,  // journal_item_count
-                    0,  // meeting_count
-                    0,  // phonecall_count
-                    0,  // sale_order_count
-                    0]; // total_invoiced
-
-        storage.saveCustomer(data, function (customerId) {
-            $scope.customer.id = customerId;
-            $scope.customers.push($scope.customer);
+        storage.saveCustomer($scope.customer, function (customerId) {
+            if (!$scope.customer.id) {
+                $scope.customer.id = customerId;
+                $scope.customers.push($scope.customer);
+            }
 
             $scope.customer = {};
             $scope.customerModal.hide();
             $log.info('Customer saved');
         }, function (error) {
-            $ionicPopup.alert({ title: 'No se puedo guardar el cliente', template: JSON.stringify(error) });
+            $ionicPopup.alert({ title: 'No se ha podido guardar el cliente', template: JSON.stringify(error) });
             $log.error(JSON.stringify(error));
         });
     }
@@ -170,7 +142,7 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
             quality: 75,
             destinationType: Camera.DestinationType.DATA_URL,
             sourceType: Camera.PictureSourceType.CAMERA,
-            allowEdit: false,
+            allowEdit: true,
             encodingType: Camera.EncodingType.JPEG,
             targetWidth: 300,
             targetHeight: 300,
@@ -214,6 +186,11 @@ angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
     }
 
     $scope.addContact = function () {
+        if(!$scope.customer.mobile && !$scope.customer.phone && !$scope.customer.email) {
+            $ionicPopup.alert({ title: 'No se puede guardar el contacto', template: 'El cliente no tiene registrado un teléfono, celular y email'});
+            return;
+        }
+
         var contact = {
             name: {
                 givenName: $scope.customer.name,

+ 154 - 86
www/js/factories.js

@@ -1,5 +1,42 @@
 angular.module('odoo.factories', ['xml-rpc'])
 
+.factory('util', function () {
+    return {
+        asyncLoop : 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();
+                    }
+                },
+
+                iteration: function() {
+                    return index - 1;
+                },
+
+                break: function() {
+                    done = true;
+                    callback();
+                }
+            };
+
+            loop.next();
+            return loop;
+        }
+    }
+})
+
 .factory('odoo', function (xmlrpc, methodCallManager) {
     return {
         auth: function (config, success, error) {
@@ -10,9 +47,11 @@ angular.module('odoo.factories', ['xml-rpc'])
                 }
 
                 success({ id: response, username: config.username, password: config.password });
+            }, function (xmlrpcError) {
+                error(xmlrpcError);
             });
         },
-        push: function (model, domain, config, success, error) {
+        read: function (model, domain, config, success, error) {
             xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'search_read', domain]).then(function (response) {
                 if (!response || response['faultCode']) {
                     error(response);
@@ -20,20 +59,44 @@ angular.module('odoo.factories', ['xml-rpc'])
                 }
 
                 success(response);
-            }, function (err) {
-                error(err);
+            }, function (xmlrpcError) {
+                error(xmlrpcError);
             });
         },
-        pull: function (model, data, config, success, error) {
-            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, 'create', data]).then(function (response) {
+        create: function (model, data, config, success, error) {
+            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'create', data]).then(function (response) {
                 if (!response || response['faultCode']) {
                     error(response);
                     return;
                 }
 
                 success(response);
-            }, function (err) {
-                error(response);
+            }, function (xmlrpcError) {
+                error(xmlrpcError);
+            });
+        },
+        write: function (model, data, config, success, error) {
+            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'write', data], function (response) {
+                if (!response || response['faultCode']) {
+                    error(response);
+                    return;
+                }
+
+                success(response);
+            }, function (xmlrpcError) {
+                error(xmlrpcError);
+            });
+        },
+        unlink: function (model, id, config, success, error) {
+            xmlrpc.callMethod(methodCallManager.call('execute_kw', config), [config.database, config.remote_id, config.password, model, 'unlink', id], function (response) {
+                if (!response || response['faultCode']) {
+                    error(response);
+                    return;
+                }
+
+                success(response);
+            }, function (xmlrpcError) {
+                error(xmlrpcError);
             });
         }
     }
@@ -59,39 +122,90 @@ angular.module('odoo.factories', ['xml-rpc'])
     }
 })
 
-.factory('sync', function (storage, odoo) {
+.factory('sync', function (storage, odoo, util) {
     return {
         syncCustomers: function (success, error) {
             storage.get('user', function (users) {
                 if (users.length == 1) {
                     var userConfig = users.item(0);
 
-                    storage.getByConstraint('partner', 'remote_id = 0', function(partners) {
-                        var data = storage.resultToJSON(partners);
+                    // 1. Transfer all new data from mobile to server
+                    storage.getByConstraint('partner', 'remote_id = 0', function(newPartners) {
+                        util.asyncLoop(newPartners.length, function (loop) {
+                            var data = [newPartners.item(loop.iteration())];
+
+                            odoo.create('res.partner', data, userConfig, function (response) {
+                                loop.next();
+                            }, function (odooCreateError) {
+                                loop.break();
+                            });
+
+                        // End loop
+                        }, function() {
+                            // 2. Transfer all modified data from mobile to server
+                            storage.getByConstraint('partner', '', function (modifiedPartners) {
+                                util.asyncLoop(modifiedPartners.length, function (loop) {
+                                    var data = [modifiedPartners.item(loop.iteration())];
+
+                                    odoo.write('res.partner', data, userConfig, function (response) {
+                                        loop.next();
+                                    }, function (odooWriteError) {
+                                        loop.break();
+                                    });
 
-                        odoo.pull('res.partner', data, userConfig, function (response) {
-                            success(response);
-                        }, function(e3) {
-                            error(e3);
+                                // End loop
+                                }, function () {
+                                    // 3. Delete server data from mobile
+                                    storage.getByConstraint('partner', '', function (deletedPartners) {
+                                        util.asyncLoop(deletedPartners.length, function (loop) {
+                                            var id = deletedPartners.item(loop.iteration()).remote_id;
+
+                                            odoo.unlink('res.partner', id, userConfig, function (response) {
+                                                loop.next();
+                                            }, function(odooUnlinkError) {
+                                                loop.break();
+                                            });
+
+                                        // End loop
+                                        }, function () {
+                                            // 4. Download updated data from server to mobile
+                                            odoo.read('res.partner', [[['active' = True]]], userConfig, function (updatedPartners) {
+                                                storage.deleteAllCustomers(function () {
+                                                    util.asyncLoop(updatedPartners.length, funtion (loop) {
+                                                        var data = updatedPartners.item(loop.iteration());
+
+                                                        storage.saveCustomer(data, function () {
+                                                            loop.next();
+                                                        }, function (saveCustomerError) {
+                                                            loop.break();
+                                                        })
+
+                                                    // End loop
+                                                    }, function () {
+                                                        success(updatedPartners);
+                                                    });
+                                                }, function (deleteAllCustomersError) {
+                                                    error(deleteAllCustomersError);
+                                                });
+                                            }, function (odooReadError) {
+                                                error(odooReadError);
+                                            });
+                                        });
+                                    }, function (getDeletedPartnersError) {
+                                        error(getDeletedPartnersError);
+                                    });
+                                });
+                            }, function (getModifiedPartnersError) {
+                                error(getModifiedPartnersError);
+                            });
                         });
-                    }, function (e2) {
-                        error(e2);
+                    }, function (partnerGetByConstraintError) {
+                        error(partnerGetByConstraintError);
                     });
                 }
-            }, function(e1) {
-                error(e1);
+            }, function(userGetError) {
+                error(userGetError);
             });
-            // var sql = 'SELECT * FROM partner WHERE remote_id = 0';
-            //
-            // db.executeSql(sql, [], function (result) {
-            //     for (var i = 0; i < result.rows.length; i++) {
-            //         var customer = result.rows.item(i);
-            //
-            //         s
-            //     }
-            // }, function(err) {
-            //     error(err);
-            // });
         }
     }
 })
@@ -99,63 +213,17 @@ angular.module('odoo.factories', ['xml-rpc'])
 .factory('storage', function () {
     return {
         // Customer
-        saveCustomer(data, success, error) {
-            console.log(data);
-
-            var sql = 'INSERT INTO partner(\n\
-                            remote_id,\n\
-                            name,\n\
-                            city,\n\
-                            mobile,\n\
-                            phone,\n\
-                            fax,\n\
-                            email,\n\
-                            street,\n\
-                            street2,\n\
-                            image_medium,\n\
-                            image_small,\n\
-                            comment,\n\
-                            customer,\n\
-                            employee,\n\
-                            is_company,\n\
-                            debit,\n\
-                            debit_limit,\n\
-                            opportunity_count,\n\
-                            contracts_count,\n\
-                            journal_item_count,\n\
-                            meeting_count,\n\
-                            phonecall_count,\n\
-                            sale_order_count,\n\
-                            total_invoiced\n\
-                        ) VALUES(\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?,\n\
-                            ?\n\
-                        )';
-
-            db.executeSql(sql, data, function(result) {
-                success(result.insertId);
+        saveCustomer(customer, success, error) {
+            var sql = '';
+
+            if (customer.id) {
+                sql = `UPDATE partner SET remote_id = ${ customer.remote_id ? customer.remote_id : 0  }, name = ${ customer.name ? '"' + customer.name + '"' : null }, city = ${ customer.city ? '"' + customer.city + '"' : null }, mobile = ${ customer.mobile ? '"' + customer.mobile + '"' : null }, phone = ${ customer.phone ? '"' + customer.phone + '"' : null }, fax = ${ customer.fax ? '"' + customer.fax + '"' : null }, email = ${ customer.email ? '"' + customer.email + '"' : null }, street = ${ customer.street ? '"' + customer.street + '"' : null }, street2 = ${ customer.street2 ? '"' + customer.street2 + '"' : null }, image_medium = ${ customer.image_medium ? '"' + customer.image_medium + '"' : null }, image_small = ${ customer.image_small ? '"' + customer.image_small + '"' : null }, comment = ${ customer.comment ? '"' + customer.comment + '"' : null }, customer = ${ customer.customer ? customer.customer : 1 }, employee = ${ customer.employee ? customer.employee : 0 }, is_company = ${ customer.is_company ? customer.is_company : 0 }, debit = ${ customer.debit ? customer.debit : 0 }, debit_limit = ${ customer.debit_limit ? customer.debit_limit : 0 }, opportunity_count = ${ customer.opportunity_count ? customer.opportunity_count : 0 }, contracts_count = ${ customer.contracts_count ? customer.contracts_count : 0 }, journal_item_count = ${ customer.journal_item_count ? customer.journal_item_count : 0 }, meeting_count = ${ customer.meeting_count ? customer.meeting_count : 0 }, phonecall_count = ${ customer.phonecall_count ? customer.phonecall_count : 0 }, sale_order_count = ${ customer.sale_order_count ? customer.sale_order_count : 0 }, total_invoiced = ${ customer.total_invoiced ? customer.total_invoiced : 0 } WHERE id = ${ customer.id }`;
+            } else {
+                sql = `INSERT INTO partner(remote_id, name, city, mobile, phone, fax, email, street, street2, image_medium, image_small, comment, customer, employee, is_company, debit, debit_limit, opportunity_count, contracts_count, journal_item_count, meeting_count, phonecall_count, sale_order_count, total_invoiced) VALUES (0, ${ customer.name ? '"' + customer.name + '"' : null }, ${ customer.city ? '"' + customer.city + '"' : null }, ${ customer.mobile ? '"' + customer.mobile + '"' : null }, ${ customer.phone ? '"' + customer.phone + '"' : null }, ${ customer.fax ? '"' + customer.fax + '"' : null }, ${ customer.email ? '"' + customer.email + '"' : null }, ${ customer.street ? '"' + customer.street + '"' : null }, ${ customer.street2 ? '"' + customer.street2 + '"' : null }, ${ customer.image_medium ? '"' + customer.image_medium + '"' : null }, ${ customer.image_small ? '"' + customer.image_small + '"' : null }, ${ customer.comment ? '"' + customer.comment + '"' : null }, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)`;
+            }
+
+            db.executeSql(sql, [], function(result) {
+                success(sql.startsWith('INSERT') ? result.insertId : customer.id);
             }, function(err) {
                 error(err);
             });

File diff suppressed because it is too large
+ 2 - 0
www/templates/customer.html


Some files were not shown because too many files changed in this diff