summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/download.py
blob: 54b093722f1b98a4589659061869079e796bef4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from .. import controller
from odoo import http
from odoo.http import request


class Download(controller.Controller):
    PREFIX = '/api/v1/'

    def _get_attachment(self, model, field, id):
        result = request.env['ir.attachment'].sudo().search_read([
            ('res_model', '=', model),
            ('res_field', '=', field),
            ('res_id', '=', id),
        ], ['datas', 'mimetype'])
        return result[0] if len(result) > 0 else None

    @http.route(PREFIX + 'download/invoice/<id>/<token>', auth='none', method=['GET'])
    def download_invoice(self, id, token):
        id = int(id)
        
        rest_api = request.env['rest.api']
        md5_valid = rest_api.md5_salt_valid(id, 'account.move', token)
        if not md5_valid:
            return self.response('Unauthorized')

        account_move = request.env['account.move'].sudo().search_read([('id', '=', id)], ['name'])
        pdf, type = request.env['ir.actions.report'].sudo().search([('report_name', '=', 'account.report_invoice')])._render_qweb_pdf([id])
        return rest_api.response_attachment({
            'content': pdf,
            'mimetype': 'application/pdf',
            'filename': account_move[0]['name'] + '.pdf'
        })

    @http.route(PREFIX + 'download/tax-invoice/<id>/<token>', auth='none', method=['GET'])
    def download_tax_invoice(self, id, token):
        id = int(id)

        rest_api = request.env['rest.api']
        md5_valid = rest_api.md5_salt_valid(id, 'account.move', token)
        if not md5_valid:
            return self.response('Unauthorized')

        account_move = request.env['account.move'].sudo().search_read([('id', '=', id)], ['name'])
        attachment = self._get_attachment('account.move', 'efaktur_document', id)
        if attachment and len(account_move) > 0:
            return rest_api.response_attachment({
                'content': attachment['datas'],
                'decode_content': True,
                'mimetype': attachment['mimetype'],
                'filename': account_move[0]['name'] + '.pdf',
            })
        return self.response('Dokumen tidak ditemukan', code=404)