# -*- coding: utf-8 -*-
from ppms.migration import PacketCategory, MaxVersionDependency, VersionType, MinVersionDependency, \
    ChangeDTPPacket


class AddFormatToAvailableCapDataField(ChangeDTPPacket):
    """Change the customizing of the period fields to include a default format"""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 14080

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.6', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        for di_number, old_format in (('001337', '000015'), ('002454', ''), ('001240', '')):
            record = self.get_record(412, [di_number], ['format_id'])
            if not self.change_value(record=record, attribute='format_id', old_value=old_format, new_value='000069'):
                return self.fail('Could not add format for di "%s" - was it individualized?' % di_number)
        
        for da, df_number in (('045580', 17480), ('045580', 27740)):
            record = self.get_record(411, [da, df_number], ['format_id'])
            if not self.change_value(record=record, attribute='format_id', old_value='000015', new_value=''):
                return self.fail('Could not add format for DF "%s" - was it individualized?' % df_number)

        return self.success()
