def save_or_update(distribution_details: Dict, dist_id: int = None): """Save distribution.""" current_app.logger.debug('<save_or_update') dist_code_svc = DistributionCode() if dist_id is not None: dist_code_dao = DistributionCodeModel.find_by_id(dist_id) dist_code_svc._dao = dist_code_dao # pylint: disable=protected-access if distribution_details.get('endDate', None): dist_code_svc.end_date = parser.parse( distribution_details.get('endDate')) if distribution_details.get('startDate', None): dist_code_svc.start_date = parser.parse( distribution_details.get('startDate')) else: dist_code_svc.start_date = date.today() _has_code_changes: bool = dist_code_svc.client != distribution_details.get('client', None) \ or dist_code_svc.responsibility_centre != distribution_details.get('responsibilityCentre', None) \ or dist_code_svc.service_line != distribution_details.get('serviceLine', None) \ or dist_code_svc.project_code != distribution_details.get('projectCode', None) \ or dist_code_svc.service_fee_distribution_code_id != \ distribution_details.get('serviceFeeDistributionCodeId', None) dist_code_svc.client = distribution_details.get('client', None) dist_code_svc.name = distribution_details.get('name', None) dist_code_svc.responsibility_centre = distribution_details.get( 'responsibilityCentre', None) dist_code_svc.service_line = distribution_details.get( 'serviceLine', None) dist_code_svc.stob = distribution_details.get('stob', None) dist_code_svc.project_code = distribution_details.get( 'projectCode', None) dist_code_svc.service_fee_distribution_code_id = distribution_details.get( 'serviceFeeDistributionCodeId', None) dist_code_svc.disbursement_distribution_code_id = distribution_details.get( 'disbursementDistributionCodeId', None) dist_code_svc.account_id = distribution_details.get('accountId', None) if _has_code_changes and dist_id is not None: # Update all invoices which used this distribution for updating revenue account details # If this is a service fee distribution, then find all distribution which uses this and update them. InvoiceModel.update_invoices_for_revenue_updates(dist_id) for dist in DistributionCodeModel.find_by_service_fee_distribution_id( dist_id): InvoiceModel.update_invoices_for_revenue_updates( dist.distribution_code_id) # Reset stop jv for every dave. dist_code_svc.stop_ejv = False dist_code_dao = dist_code_svc.save() distribution_code_schema = DistributionCodeSchema() current_app.logger.debug('>save_or_update') return distribution_code_schema.dump(dist_code_dao, many=False)
def save_or_update(distribution_details: Dict, dist_id: int = None): """Save distribution.""" current_app.logger.debug('<save_or_update') dist_code_svc = DistributionCode() if dist_id is not None: dist_code_dao = DistributionCodeModel.find_by_id(dist_id) dist_code_svc._dao = dist_code_dao # pylint: disable=protected-access if distribution_details.get('endDate', None): dist_code_svc.end_date = parser.parse( distribution_details.get('endDate')) if distribution_details.get('startDate', None): dist_code_svc.start_date = parser.parse( distribution_details.get('startDate')) else: dist_code_svc.start_date = date.today() dist_code_svc.client = distribution_details.get('client', None) dist_code_svc.responsibility_centre = distribution_details.get( 'responsibilityCentre', None) dist_code_svc.service_line = distribution_details.get( 'serviceLine', None) dist_code_svc.stob = distribution_details.get('stob', None) dist_code_svc.project_code = distribution_details.get( 'projectCode', None) dist_code_svc.service_fee_client = distribution_details.get( 'serviceFeeClient', None) dist_code_svc.service_fee_responsibility_centre = distribution_details.get( 'serviceFeeResponsibilityCentre', None) dist_code_svc.service_fee_line = distribution_details.get( 'serviceFeeLine', None) dist_code_svc.service_fee_stob = distribution_details.get( 'serviceFeeStob', None) dist_code_svc.service_fee_project_code = distribution_details.get( 'serviceFeeProjectCode', None) if dist_id is not None: # Update all invoices which used this distribution for updating revenue account details InvoiceModel.update_invoices_for_revenue_updates(dist_id) dist_code_dao = dist_code_svc.save() distribution_code_schema = DistributionCodeSchema() current_app.logger.debug('>save_or_update') return distribution_code_schema.dump(dist_code_dao, many=False)