# 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
)
        set_ownership(complaint, self.request)
        tender.complaints.append(complaint)
        if save_tender(self.request):
            self.LOGGER.info('Created tender complaint {}'.format(complaint.id),
                        extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_complaint_create'}, {'complaint_id': complaint.id}))
            self.request.response.status = 201
            self.request.response.headers['Location'] = self.request.route_url('{}:Tender Complaints'.format(tender.procurementMethodType), tender_id=tender.id, complaint_id=complaint.id)
            return {
                'data': complaint.serialize(tender.status),
                'access': {
                    'token': complaint.owner_token
                }
            }

    @json_view(content_type="application/json", validators=(validate_patch_complaint_data, validate_complaint_operation_not_in_active_tendering, validate_update_complaint_not_in_allowed_complaint_status), permission='edit_complaint')
    def patch(self):
        """Post a complaint resolution
        """
        tender = self.request.validated['tender']
        data = self.request.validated['data']
        # complaint_owner
        if self.request.authenticated_role == 'complaint_owner' and self.context.status in ['draft', 'claim', 'answered'] and data.get('status', self.context.status) == 'cancelled':
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateCanceled = get_now()
        elif self.request.authenticated_role == 'complaint_owner' and self.context.status in ['pending', 'accepted'] and data.get('status', self.context.status) == 'stopping':
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateCanceled = get_now()
        elif self.request.authenticated_role == 'complaint_owner' and tender.status == 'active.tendering' and self.context.status == 'draft' 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 == 'complaint_owner' and tender.status == 'active.tendering' and self.context.status == 'draft' and data.get('status', self.context.status) == 'claim':

# 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
)

        set_ownership(complaint, self.request)
        tender.complaints.append(complaint)
        if save_tender(self.request):
            self.LOGGER.info('Created tender complaint {}'.format(complaint.id),
                        extra=context_unpack(self.request, {'MESSAGE_ID': 'tender_complaint_create'}, {'complaint_id': complaint.id}))
            self.request.response.status = 201
            self.request.response.headers['Location'] = self.request.route_url('{}:Tender Complaints'.format(tender.procurementMethodType), tender_id=tender.id, complaint_id=complaint.id)
            return {
                'data': complaint.serialize(tender.status),
                'access': {
                    'token': complaint.owner_token
                }
            }

    @json_view(content_type="application/json", validators=(validate_patch_complaint_data, validate_complaint_operation_not_in_active_tendering, validate_update_complaint_not_in_allowed_complaint_status), permission='edit_complaint')
    def patch(self):
        """Post a complaint resolution
        """
        tender = self.request.validated['tender']
        data = self.request.validated['data']
        # complaint_owner
        if self.request.authenticated_role == 'complaint_owner' and self.context.status in ['draft', 'claim', 'answered'] and data.get('status', self.context.status) == 'cancelled':
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateCanceled = get_now()
        elif self.request.authenticated_role == 'complaint_owner' and self.context.status in ['pending', 'accepted'] and data.get('status', self.context.status) == 'stopping':
            apply_patch(self.request, save=False, src=self.context.serialize())
            self.context.dateCanceled = get_now()
        elif self.request.authenticated_role == 'complaint_owner' and tender.status == 'active.tendering' and self.context.status == 'draft' 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 == 'complaint_owner' and tender.status == 'active.tendering' and self.context.status == 'draft' and data.get('status', self.context.status) == 'claim':

            if get_now() > calculate_business_date(tender.tenderPeriod.endDate, -CLAIM_SUBMIT_TIME, tender):