public int SaveIssueDocument(IssueDocument model, int userID = 0)
        {
            try
            {
                model.tsUpdate       = DateTime.Now;
                model.tsUpdateUserID = userID;

                if (model.IssueDocumentID == 0)
                {
                    model.IssueNumber    = GetNextIssueDocumentNumber();
                    model.tsInsert       = DateTime.Now;
                    model.tsInsertUserID = userID;
                }

                model.Save();

                return(model.IssueDocumentID);
            }
            catch (Exception ex)
            {
                string error = "";
                CommonMethods.getError(ex, ref error);
                throw new Exception(CommonMethods.ConcatenateErrorIN_DB(DB_Exception.res_38, error, CommonMethods.GetCurrentMethodName()));
            }
        }
        public int CreateIssueDocumentFromMobileTransactions(List <int> mobileTransactionsID, int userID, int buyerID)
        {
            UnitOfWork uow = XpoHelper.GetNewUnitOfWork();
            XPQuery <MobileTransaction>   mobileTrans   = uow.Query <MobileTransaction>();
            XPQuery <InventoryDeliveries> invDeliveries = uow.Query <InventoryDeliveries>();

            var mobileTransactions = mobileTrans.Where(mt => mobileTransactionsID.Any(mti => mti == mt.MobileTransactionID)).ToList();

            if (mobileTransactions != null && mobileTransactions.Count > 0)
            {
                var buyer = clientRepo.GetClientByID(buyerID, session);
                //from locationID we get buyer
                IssueDocument document = new IssueDocument(session);

                document.IssueDocumentID  = 0;
                document.IssueNumber      = GetNextIssueDocumentNumber();
                document.BuyerID          = buyer;
                document.IssueStatus      = GetIssueDocumentStatusByCode(Enums.IssueDocumentStatus.DELOVNA);
                document.IssueDate        = DateTime.Now;
                document.Name             = $"Izdaja materiala za kupca: {buyer.Name}";
                document.Notes            = "Prenos iz mobilnih transkacij";
                document.InternalDocument = "000000";
                document.InvoiceNumber    = DateTime.Now.Year.ToString() + "/000000";
                document.tsInsert         = DateTime.Now;
                document.tsInsertUserID   = userID;
                document.tsUpdate         = DateTime.Now;
                document.tsUpdateUserID   = userID;

                document.Save();

                var issueDocument         = GetIssueDocumentByID(document.IssueDocumentID, uow);
                IssueDocumentPosition pos = null;
                foreach (var item in mobileTransactions)
                {
                    pos = new IssueDocumentPosition(uow);

                    pos.IssueDocumentID = issueDocument;
                    pos.SupplierID      = clientRepo.GetClientByID(item.SupplierID.ClientID, uow);

                    //Kosovna količina
                    pos.Quantity = item.Quantity <= 0 ? invDeliveries.Count(inv => inv.PackagesUIDs.Contains(item.UIDCode)) : item.Quantity;// če se še ni shranila količina na mobilnih transkacijah jo poiščemo v invnetoryDeliveries

                    pos.UID250              = item.UIDCode;
                    pos.Name                = item.ProductID.Name;
                    pos.Notes               = "Prenos iz mobilnih transakcij";
                    pos.tsInsert            = DateTime.Now;
                    pos.tsInsertUserID      = userID;
                    pos.tsUpdate            = DateTime.Now;
                    pos.tsUpdateUserID      = userID;
                    pos.ProductID           = productRepo.GetProductByID(item.ProductID.ProductID, uow);
                    pos.MobileTransactionID = item;
                }

                uow.CommitChanges();


                return(document.IssueDocumentID);
            }

            return(-1);
        }