# Path: openprocurement/tender/openua/utils.py
# PKG = get_distribution(__package__)
# LOGGER = getLogger(PKG.project_name)
# def calculate_normalized_date(dt, tender, ceil=False):
# def check_bids(request):
# def check_complaint_status(request, complaint):
# def check_status(request):
# def add_next_award(request, reverse=False, awarding_criteria_key='amount'):
# 
# Path: openprocurement/tender/openua/validation.py
# def validate_contract_update_with_accepted_complaint(request):
#     tender = request.validated['tender']
#     if any([any([c.status == 'accepted' for c in i.complaints]) for i in tender.awards if i.lotID in [a.lotID for a in tender.awards if a.id == request.context.awardID]]):
#         raise_operation_error(request, 'Can\'t update contract with accepted complaint')


from openprocurement.tender.belowthreshold.views.contract import TenderAwardContractResource
from openprocurement.api.utils import (
    context_unpack,
    json_view,
    get_now,
    raise_operation_error
)
from openprocurement.tender.openua.utils import check_tender_status
from openprocurement.tender.core.validation import (
    validate_contract_signing,
    validate_patch_contract_data,
    validate_update_contract_value,
    validate_update_contract_only_for_active_lots,
    validate_contract_operation_not_in_allowed_status
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch,
    optendersresource
)
from openprocurement.tender.openua.validation import validate_contract_update_with_accepted_complaint
# -*- coding: utf-8 -*-


@optendersresource(name='aboveThresholdUA:Tender Contracts',
                   collection_path='/tenders/{tender_id}/contracts',
                   path='/tenders/{tender_id}/contracts/{contract_id}',
                   procurementMethodType='aboveThresholdUA',
                   description="Tender contracts")
class TenderUaAwardContractResource(TenderAwardContractResource):

    @json_view(content_type="application/json", permission='edit_tender', validators=(validate_patch_contract_data, validate_contract_operation_not_in_allowed_status,
               validate_update_contract_only_for_active_lots, validate_contract_update_with_accepted_complaint, validate_update_contract_value, validate_contract_signing))
    def patch(self):
        """Update of contract
        """
        contract_status = self.request.context.status
        apply_patch(self.request, save=False, src=self.request.context.serialize())
        if contract_status != self.request.context.status and (contract_status != 'pending' or self.request.context.status != 'active'):
            raise_operation_error(self.request, 'Can\'t update contract status')
        if self.request.context.status == 'active' and not self.request.context.dateSigned:
            self.request.context.dateSigned = get_now()

# Path: openprocurement/tender/openua/utils.py
# PKG = get_distribution(__package__)
# LOGGER = getLogger(PKG.project_name)
# def calculate_normalized_date(dt, tender, ceil=False):
# def check_bids(request):
# def check_complaint_status(request, complaint):
# def check_status(request):
# def add_next_award(request, reverse=False, awarding_criteria_key='amount'):
# 
# Path: openprocurement/tender/openua/validation.py
# def validate_contract_update_with_accepted_complaint(request):
#     tender = request.validated['tender']
#     if any([any([c.status == 'accepted' for c in i.complaints]) for i in tender.awards if i.lotID in [a.lotID for a in tender.awards if a.id == request.context.awardID]]):
#         raise_operation_error(request, 'Can\'t update contract with accepted complaint')


# Path: openprocurement/tender/openua/views/contract.py
from openprocurement.tender.belowthreshold.views.contract import TenderAwardContractResource
from openprocurement.api.utils import (
    context_unpack,
    json_view,
    get_now,
    raise_operation_error
)
from openprocurement.tender.openua.utils import check_tender_status
from openprocurement.tender.core.validation import (
    validate_contract_signing,
    validate_patch_contract_data,
    validate_update_contract_value,
    validate_update_contract_only_for_active_lots,
    validate_contract_operation_not_in_allowed_status
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch,
    optendersresource
)
from openprocurement.tender.openua.validation import validate_contract_update_with_accepted_complaint

# -*- coding: utf-8 -*-


@optendersresource(name='aboveThresholdUA:Tender Contracts',
                   collection_path='/tenders/{tender_id}/contracts',
                   path='/tenders/{tender_id}/contracts/{contract_id}',
                   procurementMethodType='aboveThresholdUA',
                   description="Tender contracts")
class TenderUaAwardContractResource(TenderAwardContractResource):

    @json_view(content_type="application/json", permission='edit_tender', validators=(validate_patch_contract_data, validate_contract_operation_not_in_allowed_status,
               validate_update_contract_only_for_active_lots, validate_contract_update_with_accepted_complaint, validate_update_contract_value, validate_contract_signing))
    def patch(self):
        """Update of contract
        """
        contract_status = self.request.context.status
        apply_patch(self.request, save=False, src=self.request.context.serialize())
        if contract_status != self.request.context.status and (contract_status != 'pending' or self.request.context.status != 'active'):
            raise_operation_error(self.request, 'Can\'t update contract status')
        if self.request.context.status == 'active' and not self.request.context.dateSigned:
            self.request.context.dateSigned = get_now()

        check_tender_status(self.request)