(function () {
    "use strict";

    angular.module('pos').factory('inventarioService', factory);
    factory.$inject = ['$http', '$q'];

    function factory($http, $q) {
        var service = {}
        service.get = function (codigo, filial) {
            var deferred = $q.defer();
            var args = { 'codigo': codigo, 'filial': filial }
            $http.post('/inventario/get', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        service.getAll = function (filtro) {
            var deferred = $q.defer();
            var args = {
                'chaves': filtro.chaves,
                'codigo': filtro.codigo,
                'filial': filtro.filial,
                'descricao': filtro.descricao,
                'emissaoDe': filtro.emissao.de,
                'emissaoAte': filtro.emissao.ate,
                'skip': filtro.skip,
                'take': filtro.take
            }
            $http.post('/inventario/getall', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        service.count = function (filtro) {
            var deferred = $q.defer();
            var args = {
                'chaves': filtro.chaves,
                'codigo': filtro.codigo,
                'filial': filtro.filial,
                'descricao': filtro.descricao,
                'emissaoDe': filtro.emissao.de,
                'emissaoAte': filtro.emissao.ate
            }
            $http.post('/inventario/count', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        service.getMovimentacoes = function (inventario) {
            var deferred = $q.defer();
            var args = {
                'inventario': inventario
            }
            $http.post('/inventario/getmovimentacoes', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        service.addNewItem = function (filial, inventario, item) {
            var deferred = $q.defer();
            var args = { 'filial': filial, 'codigoInventario': inventario, 'item': item };
            $http.post('/inventario/addnewitem', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        service.delete = function ( item) {
            var deferred = $q.defer();
            var args = { 'item': item};
            $http.post('/inventario/delete', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        service.UpdateItemInventario = function (item) {
            var deferred = $q.defer();
            var args = { 'item': item };
            $http.post('/inventario/UpdateItemInventario', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        

        service.prepareNewItem = function (filial, inventario, item) {
            var deferred = $q.defer();
            var args = { 'filial': filial, 'inventario': inventario, 'codigo': item };
            $http.post('/inventario/PrepareNewItem', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        service.save = function (inventario) {
            var deferred = $q.defer();
            var args = { 'inventario': inventario }
            $http.post('/inventario/save', inventario)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        return service;
    }

})();