"""Logic for Vendor Contracts.""" from owsrequest import request as requests from contracts import response from contracts.constants import service_name from contracts.models import ( booked_vendor_contract, ows_account, ows_product, product_split, product_territory_split, track_split, vendor_contract, ) def get_booked_contracts(vendor_id, sort_order, page_limit, page_offset): """Get all booked contracts for provided vendor_id. Args: vendor_id (int): Vendor Id. sort_order (str): Order of sorting: asc or desc. page_limit (int): Count of records to fetch. page_offset (int): count of records to skip. Returns: response.Response: List of booked contracts or error in Response object. """ contracts_count_data = booked_vendor_contract.get_booked_contracts_count(vendor_id) if not contracts_count_data: return contracts_count_data items = [] contracts_count = contracts_count_data.message if contracts_count > 0: contracts = booked_vendor_contract.get_booked_contracts( vendor_id=vendor_id, sort_order=sort_order, page_limit=page_limit, page_offset=page_offset, ) if not contracts: return contracts items = [item.as_dict() for item in contracts.message] pagination = { 'type': 'standard', 'offset': page_offset, 'limit': page_limit, 'total_records': contracts_count, 'sort_order': sort_order, } result = {'items': items, 'pagination': pagination} return response.Response(result) def get_booked_contracts_by_period_range( vendor_id, first_period, last_period, sort_order ): """Get all booked contracts for provided vendor_id and period range. Args: vendor_id (int): Vendor Id. first_period (int): The first period of the range last_period (int): The last period of the range sort_order (str): Order of sorting: asc or desc. Returns: response.Response: List of booked contracts or error in Response object. """ contracts = booked_vendor_contract.get_booked_contracts_by_period_range( vendor_id=vendor_id, first_period=first_period, last_period=last_period, sort_order=sort_order, ) return contracts def get_active_vendor_contract(vendor_id): """Get active vendor contract information for vendor. Args: vendor_id: unique identifier of the vendor. Returns: response.Response: active vendor contract information. """ active_contract = vendor_contract.get_active_contract(vendor_id) return active_contract def get_vendor_contract(contract_id): """Get vendor contract information. Args: contract_id: unique identifier of the contract. Returns: response.Response: vendor contract information. """ contract = vendor_contract.get_contract(contract_id) return contract def get_contract_service_type(contract_id): """Get the service type for a given contract. Args: contract_id: unique identifier of the contract. Returns: response.Response: service type. """ return vendor_contract.get_contract_service_type(contract_id) def get_product_split(upc, page_offset=None, page_limit=None): """Get a product split a given upc. Args: upc: unique identifier of the product. Returns: response.Response: list of upcs and product split. """ return product_split.get_product_split( upc, page_offset=page_offset, page_limit=page_limit ) def check_product_ownership(json_data, vendor_id, account_type): """Check product ownership. Args: json_data: json of upcs. vendor_id: Vendor Id account_type: Grass Account Type Returns: response.Response: status """ acc_type = account_type if account_type else 'vendor' return requests.post( service_name.OWS_PRODUCT, '/{}/{}/upcs'.format(acc_type, vendor_id), json=json_data, ) def set_product_split(data, vendor_id, account_type): """Set a product split for a given upc. Args: data: a list of dictionaries that contains upc and product_split_rate. vendor_id: vendor_id account_type: Grass Account Type Returns: response.Response: status """ json_data = {'upcs': [item['upc'] for item in data]} products_response = check_product_ownership(json_data, vendor_id, account_type) if products_response.status_code == 200: for item in data: if not ows_product.validate_upc(item['upc']): return response.create_not_found_response() return product_split.set_product_split(data, vendor_id) return response.Response( status=products_response.status_code, message=products_response.json() ) def update_product_split(data, vendor_id, account_type): """Set a product split for a given upc. Args: data: a list of dictionaries that contains upc and product_split_rate vendor_id: vendor_id account_type: Grass Account Type Returns: response.Response: status """ json_data = {'upcs': [item['upc'] for item in data]} products_response = check_product_ownership(json_data, vendor_id, account_type) if products_response.status_code == 200: return product_split.update_product_split(data) return response.Response( status=products_response.status_code, message=products_response.json() ) def delete_product_split(upc, data): """Set a product split for a given upc. Args: upc: unique identifier of the product. Returns: response.Response: status """ return product_split.delete_product_split(upc, data) def get_track_split(vendor_id, page_offset=None, page_limit=None): """Get a track split for a given vendor id. Args: vendor_id: unique identifier of the vendor. Returns: response.Response: list of isrc and track split. """ return track_split.get_track_split( vendor_id, page_offset=page_offset, page_limit=page_limit ) def set_track_split(vendor_id, data): """Set a track split for a given vendor_id and isrc. Args: vendor_id (int): vendor_id. data (list): a list of dictionaries of request parameters. Returns: response.Response: status """ return track_split.set_track_split(vendor_id, data) def update_track_split(vendor_id, data): """Update a track split for a given upc. Args: data: a list of dictionaries that contains isrc and track_split_rate Returns: response.Response: status """ return track_split.update_track_split(vendor_id, data) def delete_track_split(vendor_id, data): """Delete a track split for provided ISRC and vendor id. Args: vendor_id (int): unique identifier of the vendor. data (dict): dictionary of request parameters. Returns: response.Response: status """ return track_split.delete_track_split(vendor_id, data) def get_product_territory_split(vendor_id, page_offset=None, page_limit=None): """Get a product territory split for a given vendor id. Args: vendor_id: unique identifier of the vendor. Returns: response.Response: list of product_ids and split_rate """ return product_territory_split.get_product_territory_split( vendor_id, page_offset=page_offset, page_limit=page_limit ) def delete_product_territory_split(upc, data): """Delete a product territory split for provided UPC and country id. Args: upc (int): unique product code data (dict): dictionary of request parameters. Returns: response.Response: status """ return product_territory_split.delete_product_territory_split(upc, data) def set_product_territory_split(data, vendor_id, account_type): """Set a product territory split for a given vendor_id. Args: data: a list of dictionaries that contains upc and product_territory_split_rate. vendor_id: vendor_id account_type: Grass Account Type Returns: response.Response: status """ json_data = {'upcs': [item['upc'] for item in data]} products_response = check_product_ownership(json_data, vendor_id, account_type) if products_response.status_code == 200: for item in data: if not ows_product.validate_upc(item['upc']): return response.create_not_found_response() return product_territory_split.set_product_territory_split(data, vendor_id) return response.Response( status=products_response.status_code, message=products_response.json() ) def update_product_territory_split(data, vendor_id, account_type): """Set a product territory split for a given upc and country_id. Args: data: a list of dictionaries that contains upc and product_split_rate vendor_id: vendor_id account_type: Grass Account Type Returns: response.Response: status """ json_data = {'upcs': [item['upc'] for item in data]} products_response = check_product_ownership(json_data, vendor_id, account_type) if products_response.status_code == 200: return product_territory_split.update_product_territory_split(data) return response.Response( status=products_response.status_code, message=products_response.json() ) def get_vendor_currency(vendor_id): """Get vendor currency. Args: vendor_id (int): the vendor's unique identifier Returns: response.Response: status """ return vendor_contract.get_vendor_currency(vendor_id) def get_contract_royalties(contract_id): """Get contract royalties. Args: contract_id(int): the contract's unique identifier Returns: response.Response: royalty info """ return vendor_contract.get_contract_royalties(contract_id) def update_service_type_by_vendor_id(vendor_id, service_type_id): """Update service type ID for active vendor contract. Args: vendor_id(int): unique identifier for the vendor service_type_id(int): service type id of a contract Returns: response.Response: vendor contract update status """ vendor_info = ows_account.get_vendor_uuid_by_vendor_id(vendor_id) if vendor_info is None: return response.create_not_found_response( f'Vendor not found for vendor id: {vendor_id}' ) active_vendor_contract = get_active_vendor_contract(vendor_id) if not active_vendor_contract: return response.create_not_found_response( f'No active contract found for vendor_id {vendor_id}.' ) vendor_contract_id = active_vendor_contract.message['vendor_contract_id'] try: vendor_contract.update_vendor_contract_service_type_id( service_type_id, vendor_id, vendor_contract_id ) return vendor_info except Exception as e: return response.create_fatal_response( f'Failed to update service type for vendor {vendor_id}: {str(e)}' )