table 50075 "Sharepoint Documents"
{
    Caption = 'Sharepoint Documents';
    DataClassification = ToBeClassified;

    fields
    {
        field(1; "No."; Code[25])
        {
            DataClassification = ToBeClassified;

        }
        field(2; "Line No."; Integer)
        {
            DataClassification = ToBeClassified;
        }
        field(3; "SharePoint URL"; Text[2048])
        {
            DataClassification = ToBeClassified;
        }
        field(4; "File Name"; Text[250])
        {
            DataClassification = ToBeClassified;

        }
        field(5; "File Extension"; Text[50])
        {
            DataClassification = ToBeClassified;
        }
        field(6; "Table Id"; Integer)
        {
            DataClassification = ToBeClassified;
        }
        // field(7; "Document Type"; Option)
        // {
        //     OptionMembers = Quote,Order,Invoice,"Credit Memo","Blanket Order","Return Order";
        // }
        field(8; ID; Integer)
        {
            DataClassification = ToBeClassified;
            AutoIncrement = true;
        }
        field(9; "Document Code"; Code[20])
        {
            DataClassification = ToBeClassified;
        }
        field(10; "No. Series"; Code[20])
        {
            DataClassification = ToBeClassified;
        }
        field(11; "Document Type"; Option)
        {
            OptionMembers = Quote,Order,Invoice,"Credit Memo","Blanket Order","Return Order";
        }
        field(12; "Service Instruction Sheet"; boolean)
        {
            DataClassification = ToBeClassified;
        }
        field(13; "Exchange Rate Doc"; boolean)
        {
            DataClassification = ToBeClassified;
        }
        field(14; "Moved To Spoint"; Boolean)
        {
            DataClassification = ToBeClassified;
        }
        field(15; "Spoint Folder Relative Url"; Text[2048])
        {
            DataClassification = ToBeClassified;
        }

    }

    keys
    {
        key(Key1; "No.", "Line No.", "Table Id", ID, "Document Code")
        {
            Clustered = true;
        }
    }

    var
        CuNoSriemgmt: Codeunit NoSeriesManagement;

}


page 50159 "Sharepoint Documents1"
{
    PageType = List;
    ApplicationArea = All;
    UsageCategory = Lists;
    SourceTable = "Sharepoint Documents";


    layout
    {

        area(Content)
        {
            repeater(GroupName)
            {
                field("No."; "No.")
                {
                    ApplicationArea = All;

                }
                field("SharePoint URL"; "SharePoint URL")
                {
                    Editable = false;

                }
                field("File Name"; "File Name")
                {
                    trigger OnDrillDown()
                    var

                    begin
                        UploadDocumentForTheRecord("Table ID", "No.", "Document Code");
                    end;
                }
                field("File Extension"; "File Extension")
                {

                }
            }

        }
        area(Factboxes)
        {

        }
    }



    actions
    {
        area(processing)
        {
            action("View Document")
            {
                ApplicationArea = All;
                Image = Document;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction();
                begin
                    Hyperlink("SharePoint URL");
                end;
            }
            action("Download Document")
            {
                ApplicationArea = All;
                Image = Download;
                Promoted = true;
                PromotedCategory = Process;
                trigger OnAction();
                var
                    ObjICTSetup: Record "ICT Setup";
                    CuDocMgt: Codeunit "SharePoint Integration Handler";
                    bas64String: Text;
                    BcInstream: InStream;
                    BcOutStream: OutStream;
                    DialogTitle: Label 'Download File';
                    Cubase64: Codeunit "Base64 Convert";
                    Cutemplob: Codeunit "Temp Blob";
                    tofileName: Text;
                    tofolder: Label 'C:\';
                    toFilter: Label 'All Files (*.*)|*.*';
                begin
                    ObjICTSetup.Get();
                    if ObjICTSetup."Use Sharepoint" then begin
                        bas64String := CuDocMgt.fnDownloadDocument("Spoint Folder Relative Url", "File Name");
                        bas64String := DelChr(bas64String, '<', '"');
                        bas64String := DelChr(bas64String, '>', '"');
                        tofileName := "File Name";
                        Cutemplob.CreateOutStream(BcOutStream);
                        Cubase64.FromBase64(bas64String, BcOutStream);
                        Cutemplob.CreateInStream(BcInstream);
                        DownloadFromStream(BcInstream, DialogTitle, tofolder, toFilter, tofileName);
                    end;
                end;
            }
            action("Delete Document")
            {
                ApplicationArea = All;
                Image = Delete;
                Promoted = true;
                PromotedCategory = Process;
                ToolTip = 'Delete Document From Sharepoint';
                trigger OnAction();
                var
                    ObjICTSetup: Record "ICT Setup";
                    CuDocMgt: Codeunit "SharePoint Integration Handler";
                    res: text;
                begin
                    ObjICTSetup.Get();
                    if ObjICTSetup."Use Sharepoint" then begin
                        res := CuDocMgt.fnDeleteDocument("Spoint Folder Relative Url", "File Name");
                    end;
                    if res = '200' then begin
                        if Delete(true) then begin
                            Message('Document Deleted Successfully');
                        end;
                    end;
                    Commit();
                    CurrPage.Update();
                end;
            }

        }
    }

    trigger OnNewRecord(BelowxRec: Boolean)
    var

    begin
        "File Name" := SelectFileTxt;
    end;

    trigger OnAfterGetRecord()
    begin

    end;

    procedure UploadDocumentForTheRecord(TableId: Integer; RecordNo: code[25]; DocCode: Code[25])
    var
        FieldRef: FieldRef;
        DocType: Option;
        LineNo: Integer;
        ColumnPropertArray: array[6] of Text[250];
        i: Integer;
        ObjPurchInvHeader: Record "Purch. Inv. Header";
        ObjPurchRcptHeader: Record "Purch. Rcpt. Header";
        ObjPurchCrMemoHdr: Record "Purch. Cr. Memo Hdr.";
        ObjSalesHeader: Record "Sales Header";
        ObjBankAccRecon: Record "Bank Acc. Reconciliation";
        ObjEft: Record "EFT Generator";
        FoldersArray: array[5] of Text[250];
    begin
        ObjcompInfo.get;
        ObjSpStp.Reset();
        ObjSpStp.SetRange(ObjSpStp."Table Id", TableId);
        if ObjSpStp.FindFirst() then begin
            case
                TableId of
                Database::"Purchase Header":
                    begin
                        ObjPurchHeader.get(RecordNo);
                        FoldersArray[1] := ObjSpStp.Folder;
                        FoldersArray[2] := ObjSpStp.Module;
                        FoldersArray[3] := ObjSpStp."Financial Year";
                        FoldersArray[4] := Format(ObjPurchHeader."Document Type");
                        FoldersArray[5] := RecordNo;
                        ParseTheDocumentToSharepointHandler(FoldersArray);
                    end;               
            end;
        end;
    end;

    procedure ParseTheDocumentToSharepointHandler(FolderArray: array[5] of Text[250])
    var
        ServerFileName: Text[250];
        ObjIctSetup: Record "ICT Setup";
        ClientPath: Text[250];
        clientFileName: Text[250];
        TempFileName: Text[250];
        cuTemplob: Codeunit "Temp Blob";
        DialogTitle: Label 'Select File';
        base64String: Text;
        cuBase64: Codeunit "Base64 Convert";
        fromFilter: label 'All Files (*.*)|*.*';
        BcInstream: InStream;
        bcOutStream: OutStream;
        BcFile: File;
        Exfilter: label '(*.*)|*.*';
        FromFolder: label 'C:\';
        filename: Text[250];
    begin
        Commit();

        ObjIctSetup.Get();
        File.UploadIntoStream(DialogTitle, FromFolder, FromFilter, clientFileName, BcInstream);
        base64String := cuBase64.ToBase64(BcInstream);
        filename := FileManagement.GetFileName(clientFileName);
        "SharePoint URL" := CuDocMgmt.fnuploadDocument(base64String, FolderArray, filename);
        "File Name" := filename;
        "File Extension" := FileManagement.GetExtension(filename);
        "Spoint Folder Relative Url" := '/' + FolderArray[1] + '/' + FolderArray[2] + '/' + FolderArray[3] + '/' + FolderArray[4] + '/' + FolderArray[5];
    end;

    procedure OpenForRecReference(RecRef: RecordRef)

    var
        FieldRef: FieldRef;
        RecNo: Code[20];
        RecNo2: Code[20];
        DocType: Option;
        LineNo: Integer;
        DoCNo: Code[25];

    begin
        Reset();
        FromRecRef := RecRef;
        SetRange("Table ID", RecRef.Number);       
        CASE RecRef.NUMBER OF
            DATABASE::"Sales Header",
            DATABASE::"Sales Line",
            DATABASE::"Purchase Header",
            DATABASE::"Purchase Line":
                BEGIN
                    FieldRef := RecRef.FIELD(1);
                    DocType := FieldRef.VALUE;
                    SETRANGE("Document Type", DocType);

                    FieldRef := RecRef.FIELD(3);
                    RecNo := FieldRef.VALUE;
                    SETRANGE("No.", RecNo);

                    FlowFieldsEditable := FALSE;
                END;
        END;
        
    end;

    var
        FromRecRef: RecordRef;
        UrlText: Text[250];
        GLAccount: Record "G/L Account";
        SalesDocumentFlow: Boolean;
        PurchaseDocumentFlow: Boolean;
        FlowFieldsEditable: Boolean;
        ObjIctStupRec: Record "ICT Setup";

        Attachment: Page "Document Attachment Details";
        SelectFileTxt: TextConst ENU = 'Select File...';
        TbUserSetup: Record "User Setup";
        ObjSpStp: Record "Sharepoint Setup";
        CuDocMgmt: Codeunit "SharePoint Integration Handler";
        ObjcompInfo: Record "Company Information";
        ObjPayments: Record Payments;
        ObjPaylines: Record "Payment Lines";
        ObjPurchHeader: Record "Purchase Header";
        ObjStaffAdvance: Record Payments;
        storereq: Record "Store Requistion Header";
        prepaymentsheader: Record "Prepayments Header";
        ObjProposedBudget: Record "Proposed Budget Header";
        objBudgReallocation: Record "Budget Reallocation";
        ObjBudget: Record "Budget Approval Header";
        FileManagement: Codeunit "File Management";
        FileName: Text;
        ImportTxt: label 'Attach a document.';
        FileDialogTxt: TextConst ENU = 'Attachments (%1)|%1';
        FilterTxt: TextConst ENU = '*.jpg;*.jpeg;*.bmp;*.png;*.gif;*.tiff;*.tif;*.pdf;*.docx;*.doc;*.xlsx;*.xls;*.pptx;*.ppt;*.msg;*.xml;*.*';
        FolderArray: array[5] of Text[250];
        i: Integer;
}

table 50076 "Sharepoint Setup"
{
    Caption = 'Sharepoint Setup';
    DataClassification = ToBeClassified;

    fields
    {
        field(1; No; Integer)
        {
            DataClassification = ToBeClassified;

        }

        field(2; "Shortcut Dimension 2 Code"; Code[20])
        {
            CaptionClass = '1,1,2';
            TableRelation = "Dimension Value".Code WHERE("Global Dimension No." = CONST(2));

            trigger OnValidate()
            begin
            end;
        }
        field(3; "Sharepoint Url"; text[250])
        {
            DataClassification = ToBeClassified;
        }

        // field(4; "Document Type"; Enum "Sharepoint Document types")
        // {
        //     DataClassification = ToBeClassified;
        // }
        field(5; "Library"; Text[150])
        {
            DataClassification = ToBeClassified;
        }
        field(6; "Folder"; Text[150])
        {
            DataClassification = ToBeClassified;
        }
        field(7; "Financial Year"; Text[50])
        {
            DataClassification = ToBeClassified;
        }
        field(8; "Quarter"; Text[50])
        {
            DataClassification = ToBeClassified;
        }
        //Module
        field(9; "Module"; Text[50])
        {
            DataClassification = ToBeClassified;
        }
        //Sub-Module By
        field(10; "Sub-Module By"; Option)
        {
            DataClassification = ToBeClassified;
            OptionMembers = "Payment Type/Document Type",None;
        }
        //Sub-Module
        field(11; "Sub-Module"; Text[50])
        {
            DataClassification = ToBeClassified;
        }
        //table Id
        field(12; "Table Id"; Integer)
        {
            DataClassification = ToBeClassified;
            TableRelation = AllObjWithCaption."Object ID" where("Object Type" = CONST(Table));
            trigger OnValidate()
            var
                ObjAllObjWithCaption: Record "AllObjWithCaption";
            begin
                ObjAllObjWithCaption.Reset();
                ObjAllObjWithCaption.SetRange("Object ID", "Table Id");
                ObjAllObjWithCaption.SetRange("Object Type", ObjAllObjWithCaption."Object Type"::Table);
                if ObjAllObjWithCaption.FindFirst() then
                    "Table Name" := ObjAllObjWithCaption."Object Name";

            end;
        }
        //table Name
        field(13; "Table Name"; Text[50])
        {
            DataClassification = ToBeClassified;
        }

    }

    keys
    {
        key(Key1; No)
        {
            Clustered = true;
        }
    }

    var
        myInt: Integer;

    trigger OnInsert()
    begin

    end;

    trigger OnModify()
    begin

    end;

    trigger OnDelete()
    begin

    end;

    trigger OnRename()
    begin

    end;

}