# 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_submit_claim_time(request):
#     tender = request.context
#     claim_submit_time = request.content_configurator.tender_claim_submit_time
#     if get_now() > calculate_business_date(tender.tenderPeriod.endDate, -claim_submit_time, tender):
#         raise_operation_error(request, 'Can submit claim not later than {0.days} days before tenderPeriod end'.format(claim_submit_time))
# 
# Path: openprocurement/tender/openua/constants.py
# CLAIM_SUBMIT_TIME = timedelta(days=10)
# 
# COMPLAINT_SUBMIT_TIME = timedelta(days=4)


from openprocurement.tender.belowthreshold.views.complaint import TenderComplaintResource
from openprocurement.api.utils import (
    context_unpack,
    json_view,
    set_ownership,
    get_now,
    raise_operation_error
)
from openprocurement.tender.openua.utils import (
    check_tender_status,
)
from openprocurement.tender.core.validation import (
    validate_complaint_data,
    validate_submit_complaint_time,
    validate_patch_complaint_data,
    validate_complaint_operation_not_in_active_tendering,
    validate_update_complaint_not_in_allowed_complaint_status
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch,
    optendersresource,
    calculate_business_date
)
from openprocurement.tender.openua.validation import validate_submit_claim_time
from openprocurement.tender.openua.constants import (
    CLAIM_SUBMIT_TIME, COMPLAINT_SUBMIT_TIME
)
                raise_operation_error(self.request, 'Can\'t update complaint: resolution too short')
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateAnswered = get_now()
        elif self.request.authenticated_role == 'tender_owner' and self.context.status in ['pending', 'accepted']:
            apply_patch(self.request, save=False, src=self.context.serialize())
        elif self.request.authenticated_role == 'tender_owner' and self.context.status == 'satisfied' and data.get('tendererAction', self.context.tendererAction) and data.get('status', self.context.status) == 'resolved':
            apply_patch(self.request, save=False, src=self.context.serialize())
        # aboveThresholdReviewers
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status in ['pending', 'accepted', 'stopping'] and data.get('status', self.context.status) == self.context.status:
            apply_patch(self.request, save=False, src=self.context.serialize())
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status in ['pending', 'stopping'] and data.get('status', self.context.status) in ['invalid', 'mistaken']:
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateDecision = get_now()
            self.context.acceptance = False
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status == 'pending' and data.get('status', self.context.status) == 'accepted':
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateAccepted = get_now()
            self.context.acceptance = True
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status in ['accepted', 'stopping'] and data.get('status', self.context.status) in ['declined', 'satisfied']:
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateDecision = get_now()
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status in ['pending', 'accepted', 'stopping'] and data.get('status', self.context.status) == 'stopped':
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateDecision = get_now()
            self.context.dateCanceled = self.context.dateCanceled or get_now()
        else:
            raise_operation_error(self.request, 'Can\'t update complaint')
        if self.context.tendererAction and not self.context.tendererActionDate:
            self.context.tendererActionDate = get_now()
        if self.context.status not in ['draft', 'claim', 'answered', 'pending', 'accepted', 'satisfied', 'stopping'] and tender.status in ['active.qualification', 'active.awarded']:

# 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_submit_claim_time(request):
#     tender = request.context
#     claim_submit_time = request.content_configurator.tender_claim_submit_time
#     if get_now() > calculate_business_date(tender.tenderPeriod.endDate, -claim_submit_time, tender):
#         raise_operation_error(request, 'Can submit claim not later than {0.days} days before tenderPeriod end'.format(claim_submit_time))
# 
# Path: openprocurement/tender/openua/constants.py
# CLAIM_SUBMIT_TIME = timedelta(days=10)
# 
# COMPLAINT_SUBMIT_TIME = timedelta(days=4)


# Path: openprocurement/tender/openua/views/complaint.py
from openprocurement.tender.belowthreshold.views.complaint import TenderComplaintResource
from openprocurement.api.utils import (
    context_unpack,
    json_view,
    set_ownership,
    get_now,
    raise_operation_error
)
from openprocurement.tender.openua.utils import (
    check_tender_status,
)
from openprocurement.tender.core.validation import (
    validate_complaint_data,
    validate_submit_complaint_time,
    validate_patch_complaint_data,
    validate_complaint_operation_not_in_active_tendering,
    validate_update_complaint_not_in_allowed_complaint_status
)
from openprocurement.tender.core.utils import (
    save_tender,
    apply_patch,
    optendersresource,
    calculate_business_date
)
from openprocurement.tender.openua.validation import validate_submit_claim_time
from openprocurement.tender.openua.constants import (
    CLAIM_SUBMIT_TIME, COMPLAINT_SUBMIT_TIME
)

                raise_operation_error(self.request, 'Can\'t update complaint: resolution too short')
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateAnswered = get_now()
        elif self.request.authenticated_role == 'tender_owner' and self.context.status in ['pending', 'accepted']:
            apply_patch(self.request, save=False, src=self.context.serialize())
        elif self.request.authenticated_role == 'tender_owner' and self.context.status == 'satisfied' and data.get('tendererAction', self.context.tendererAction) and data.get('status', self.context.status) == 'resolved':
            apply_patch(self.request, save=False, src=self.context.serialize())
        # aboveThresholdReviewers
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status in ['pending', 'accepted', 'stopping'] and data.get('status', self.context.status) == self.context.status:
            apply_patch(self.request, save=False, src=self.context.serialize())
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status in ['pending', 'stopping'] and data.get('status', self.context.status) in ['invalid', 'mistaken']:
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateDecision = get_now()
            self.context.acceptance = False
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status == 'pending' and data.get('status', self.context.status) == 'accepted':
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateAccepted = get_now()
            self.context.acceptance = True
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status in ['accepted', 'stopping'] and data.get('status', self.context.status) in ['declined', 'satisfied']:
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateDecision = get_now()
        elif self.request.authenticated_role == 'aboveThresholdReviewers' and self.context.status in ['pending', 'accepted', 'stopping'] and data.get('status', self.context.status) == 'stopped':
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateDecision = get_now()
            self.context.dateCanceled = self.context.dateCanceled or get_now()
        else:
            raise_operation_error(self.request, 'Can\'t update complaint')
        if self.context.tendererAction and not self.context.tendererActionDate:
            self.context.tendererActionDate = get_now()
        if self.context.status not in ['draft', 'claim', 'answered', 'pending', 'accepted', 'satisfied', 'stopping'] and tender.status in ['active.qualification', 'active.awarded']:

            check_tender_status(self.request)