Example #1
0
        void AttachDocuments()
        {
            if (fileBrowser == null)
            {
                fileBrowser = new FileBrowseControl();
            }
            else
            {
                fileBrowser.SelectedFileInfos = null;
            }
            fileBrowser.IsMultiSelect = true;
            fileBrowser.BrowseFile();
            var fileList = fileBrowser.SelectedFileInfos;

            if (fileList != null)
            {
                if (documents == null)
                {
                    documents = new List <TableAddOnData>();
                }
                foreach (var file in fileList)
                {
                    documents.Add(new TableAddOnData
                    {
                        _Text         = System.IO.Path.GetFileNameWithoutExtension(file.FileName),
                        _DocumentType = DocumentConvert.GetDocumentType(file.FileExtension),
                        _Data         = file.FileBytes
                    });
                }
            }

            if (documents?.Count > 0)
            {
                var rb = (RibbonBase)localMenu.DataContext;
                attachDocMenu         = UtilDisplay.GetMenuCommandByName(rb, "AttachDoc");
                attachDocMenu.Caption = string.Format("{0} ({1})", string.Format(Uniconta.ClientTools.Localization.lookup("AttachOBJ"), Uniconta.ClientTools.Localization.lookup("Documents")), documents.Count);
            }
        }
        async void SaveIn2Steps()
        {
            // This here will first save the record without attachment. Then it will update with attachement.
            // The update will be done without await, so it will be done in the background.
            // This way it is fast to upload a document, since the user will not have to wait.
            var multiVouchers = this.multiVouchers;

            if (multiVouchers != null && browseControl.IsMultiSelect)
            {
                int l = multiVouchers.Length;
                if (l > 1)
                {
                    var buffers = new byte[l][];
                    for (int i = 0; (i < l); i++)
                    {
                        var voucher = multiVouchers[i];
                        if (voucher._Data != null)
                        {
#if !SILVERLIGHT
                            if (voucher._Fileextension == FileextensionsTypes.JPEG)
                            {
                                var imageBytes = FileBrowseControl.ImageResize(voucher._Data, ".jpg");
                                if (imageBytes != null)
                                {
                                    voucher._Data = imageBytes;
                                }
                            }
#endif
                            buffers[i]    = voucher._Data;
                            voucher._Data = null;
                        }
                    }
                    var err = await api.Insert(multiVouchers);

                    if (err != ErrorCodes.Succes)
                    {
                        for (int i = 0; (i < l); i++)
                        {
                            multiVouchers[i]._Data = buffers[i];
                        }
                        ClearBusy();
                        UtilDisplay.ShowErrorCode(err);
                    }
                    else
                    {
                        Utility.UpdateBuffers(api, buffers, multiVouchers);
                        ClosePage(4); // full refresh and clearBusy inside
                        CloseDockItem();
                    }
                    return;
                }
                voucherClientRow = multiVouchers[0];
            }
            byte[] buf = null;
            if (LoadedRow == null)
            {
                buf = voucherClientRow._Data;
#if !SILVERLIGHT
                if (buf != null && voucherClientRow._Fileextension == FileextensionsTypes.JPEG)
                {
                    buf = FileBrowseControl.ImageResize(buf, ".jpg");
                    if (buf != null)
                    {
                        voucherClientRow._Data = buf;
                    }
                    else
                    {
                        buf = voucherClientRow._Data;
                    }
                }
#endif
                if (buf != null && buf.Length > 200000)
                {
                    voucherClientRow._Data = null;
                }
                else
                {
                    buf = null;
                }
            }

            await saveForm();

            ClearBusy();

            if (buf != null)
            {
                if (voucherClientRow.RowId != 0)
                {
                    Utility.UpdateBuffers(api, new[] { buf }, new[] { voucherClientRow });
                }
                else
                {
                    voucherClientRow._Data = buf;
                }
            }
            else
            {
                VoucherCache.SetGlobalVoucherCache(voucherClientRow);
            }
        }
        async public override Task <ErrorCodes> SaveData()
        {
            SelectedItem = null;

            /*
             * Awaiting the save data as we need to have rowid
             * With row id we would be able to save the buffers seperately
             * So first we save the records and then update the buffers
             */
            var addedRows     = AddedRows;
            var addedRowCount = addedRows.Count();

            if (addedRowCount > 0)
            {
                var userDocs = new UserDocsClient[addedRowCount];
                var buffers  = new byte[addedRowCount][];
                int iCtr     = 0;

                foreach (var row in addedRows)
                {
                    var doc = row.DataItem as UserDocsClient;
                    userDocs[iCtr] = doc;
                    if (doc._Data != null)
                    {
#if !SILVERLIGHT
                        if (doc._DocumentType == FileextensionsTypes.JPEG ||
                            doc._DocumentType == FileextensionsTypes.BMP ||
                            doc._DocumentType == FileextensionsTypes.GIF ||
                            doc._DocumentType == FileextensionsTypes.TIFF)
                        {
                            var imageBytes = FileBrowseControl.ImageResize(doc._Data, ".jpg");
                            if (imageBytes != null)
                            {
                                doc._Data          = imageBytes;
                                doc._NoCompression = (doc._DocumentType == FileextensionsTypes.JPEG);
                                doc._DocumentType  = FileextensionsTypes.JPEG;
                            }
                        }
#endif
                        buffers[iCtr] = doc._Data;
                        doc._Data     = null;
                        iCtr++;
                    }
                }

                var result = await base.SaveData();

                if (result != ErrorCodes.Succes)
                {
                    return(result);
                }

                UpdateDocsBuffer(api, buffers, userDocs, iCtr);
                this.RefreshData();

                return(0);
            }
            else
            {
                return(await base.SaveData());
            }
        }