from pyramid.view import view_config, view_defaults
from brewman.views.services.session import session_current_date
from brewman.views.services.voucher import blank_voucher

__author__ = 'tanshu'


@view_defaults(request_method='GET', route_name='api_voucher', renderer='json')
class blank_voucher_view(object):
    def __init__(self, request):
        self.request = request

    @view_config(request_param='type=Journal', permission='Journal')
    def journal(self):
        return self.get_blank()


    @view_config(request_param='type=Payment', permission='Payment')
    def payment(self):
        account = self.request.GET.get('a', None)
        return self.get_blank({'Account': account})


    @view_config(request_param='type=Receipt', permission='Receipt')
    def receipt(self):
        account = self.request.GET.get('a', None)
        return self.get_blank({'Account': account})


    @view_config(request_param='type=Purchase', permission='Purchase')
    def purchase(self):
        return self.get_blank()


    @view_config(request_param='type=Purchase Return', permission='Purchase Return')
    def purchase_return(self):
        return self.get_blank()


    @view_config(request_param='type=Salary Deduction', permission='Purchase Return')
    def purchase_return(self):
        return self.get_blank()


    @view_config(request_param='type=Issue', permission='Issue')
    def issue(self):
        voucher_type = self.request.GET.get('type', None)
        date = self.request.GET.get('Date', None)
        source = self.request.GET.get('Source', None)
        destination = self.request.GET.get('Destination', None)
        if date is not None and source is not None and destination is not None:
            return blank_voucher({'Type': voucher_type, 'Date': date, 'Source': source, 'Destination': destination})
        else:
            return self.get_blank()

    def get_blank(self, additional_info=None):
        voucher_type = self.request.GET.get('type', None)
        if additional_info is None:
            additional_info = {}
        additional_info['Date'] = session_current_date(self.request)
        additional_info['Type'] = voucher_type

        return blank_voucher(additional_info)
