Announcement

Collapse
No announcement yet.

Auto attach file or PDF in create document modal form

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Auto attach file or PDF in create document modal form

    Hi Tanya,

    Good day!

    My question is in conjunction with below question:

    http://forum.espocrm.com/forum/devel...quote-template


    I am now being able to create a child view modal of the create document (views/modal/edit). But, I am having issues on how to attach the pdf or file to the form after selecting a Quote template for printing PDF.

    How would I be able to modify the View of the modal with attachments already? I already tried to attach the attachmentsIds and attachmentsNames in to the model by passing the attributes from the ajaxPostRequest for getting Email attributes that contains the PDF attachments.

    Code:
    actionSaveAsDocument: function () {      
        this.createView('pdfTemplate', 'Modals.SelectTemplate', {        
            entityType: this.model.name      
        }, function (view) {        
            view.render();                 
            this.listenToOnce(view, 'select', function (model) {          
                this.notify('Loading...');          
                this.ajaxPostRequest('Quote/action/getAttributesForEmail', {            
                    quoteId: this.model.id,
                                templateId: model.id          
                }).done(function (attributes) {            
                    alert('done');            
                    console.log(attributes);            
                    this.createView('quickCreate', 'views/modals/edit', {              
                        scope: 'Document',
                                      attributes: attributes,
                                      keepAttachmentsOnSelectTemplate: true            
                    },             function (view) {              
                        alert('rendering');              
                        var attributes = view.attributes;              
                        model.set({                  
                            attachmentsIds: attributes.attachmentsIds,
                                              attachmentsNames: attributes.attachmentsNames                
                        });              
                        console.log(view);              
                        console.log(model);              
                        this.notify(false);              
                        view.render();            
                    });          
                }.bind(this));        
            }, this);      
        }, this);    
    },

  • #2
    It is ok now. The reason is the field is not matched.

    Document has [file] field while..
    Email has [attachments] field.

    So therefore, whenever I push the attributes fetched from a Service to the child view 'quickCreate' it is not showing since the field is not matched with the attribute.

    I come up with below code:

    Code:
    actionSaveAsDocument: function () {
        this.createView('pdfTemplate', 'Modals.SelectTemplate', {
            entityType: this.model.name
        }, function (view) {
            view.render();
            this.listenToOnce(view, 'select', function (model) {
                this.notify('Loading...');
                this.ajaxPostRequest('Quote/action/getAttributesForEmail', {
                    quoteId: this.model.id,
                    templateId: model.id
                }).done(function (attributes) {
                    var fileId = attributes.attachmentsIds[0];
                    attributes.fileId = fileId;
                    attributes.fileName = attributes.attachmentsNames[fileId];
                    this.createView('quickCreate', 'custom:views/document/modals/create-attachment', {  // optional could be 'views/modals/edit'
                            scope: 'Document',
                            attributes: attributes,
                            keepAttachmentsOnSelectTemplate: true
                        },
                        function (view) {
                            this.notify(false);
                            view.render();
                        });
                }.bind(this));
            }, this);
        }, this);
    }
    I changed the attribute before it comes to the child view. I also thought that may I could change the Service that caters the attributes away from the getAttributesForEmail and put it in custom.

    Code:
    actionSaveAsDocument: function () {
        this.createView('pdfTemplate', 'Modals.SelectTemplate', {
            entityType: this.model.name
        }, function (view) {
            view.render();
            this.listenToOnce(view, 'select', function (model) {
                this.notify('Loading...');
                this.ajaxPostRequest('Quote/action/getAttributesForDocument', {
                    quoteId: this.model.id,
                    templateId: model.id
                }).done(function (attributes) {
                    this.createView('quickCreate', 'custom:views/document/modals/create-attachment', {  // optional could be 'views/modals/edit'
                            scope: 'Document',
                            attributes: attributes,
                            keepAttachmentsOnSelectTemplate: true
                        },
                        function (view) {
                            this.notify(false);
                            view.render();
                        });
                }.bind(this));
            }, this);
        }, this);
    }
    With the new code above, I removed the fileId and fileName in the attribute and changed the Service method. This Service method would be the one to give the attribute data of [fileId] and [fileName] needed by the Document.

    Below were the list of files I created and changed to extend it in Custom and upgrade safe:

    /client/custom/src/views/quote/record/detail
    /custom/Espo/Custom/Resources/metadata/clientDef/Quote.json
    /custom/Espo/Custom/Controller/Quote.php
    /custom/Espo/Custom/Service/Quote.php
    optional: if you want to have own implementation in the UI of the modal. (upgrade safe)
    /client/custom/src/views/document/modals/create-attachment
    My reference files for implementation:

    /client/modules/advanced/src/views/quote/record/detail
    /custom/Espo/Custom/Resources/metadata/clientDef/Quote.json
    /application/Espo/Modules/Advanced/Controller/Quote.php
    /application/Espo/Modules/Advanced/Service/Quote.php
    for the modal:
    ​​​
    /client/src/views/modals/edit
    Last edited by brianpunzalan; 12-05-2017, 02:50 AM. Reason: finishing

    Comment

    Working...
    X