def get_digikey_supplier():
    dk = Company.list(API, name="Digikey")
    if len(dk) == 0:
        dk = Company.create(API, {
            'name': 'Digikey',
            'is_supplier': True,
            'description': 'Electronics Supply Store'
        })
        return dk
    else:
        return dk[0]
Exemple #2
0
def is_new_supplier_part(supplier_name: str, supplier_sku: str) -> bool:
    ''' Check if InvenTree supplier part exists to avoid duplicates '''
    global inventree_api

    # Fetch all companies
    cprint('[TREE]\tFetching suppliers', silent=settings.HIDE_DEBUG)
    company_list = Company.list(inventree_api, is_supplier=True, is_customer=False)
    companies = {}
    for company in company_list:
        companies[company.name] = company

    try:
        # Get all parts
        part_list = companies[supplier_name].getSuppliedParts()
    except:
        part_list = None

    if part_list is None:
        # Create
        cprint(f'[TREE]\tCreating new supplier "{supplier_name}"', silent=settings.SILENT)
        create_company(
            company_name=supplier_name,
            supplier=True,
        )
        # Get all parts
        part_list = []

    for item in part_list:
        if supplier_sku in item.SKU:
            cprint(f'[TREE]\t{item.SKU} ?= {supplier_sku} => True', silent=settings.HIDE_DEBUG)
            return False
        else:
            cprint(f'[TREE]\t{item.SKU} ?= {supplier_sku} => False', silent=settings.HIDE_DEBUG)

    return True
def create_manufacturer(name: str, is_supplier: bool=False):
    mfg = Company.create(API, {
        'name': name,
        'is_manufacturer': True,
        'is_supplier': is_supplier,
        'description': name
        })
    return mfg
Exemple #4
0
def get_company_id(company_name: str) -> int:
    ''' Get company (supplier/manufacturer) primary key (ID) '''
    global inventree_api

    company_list = Company.list(inventree_api)
    companies = {}
    for company in company_list:
        companies[company.name] = company.pk
    try:
        return companies[company_name]
    except:
        return 0
def find_manufacturer(dkpart: DigiPart):
    possible_manufacturers = Company.list(API, name=dkpart.manufacturer)
    if len(possible_manufacturers) == 0:
        mfg = create_manufacturer(dkpart.manufacturer)
        return mfg
    else:
        print("="*20)
        print("Choose a manufacturer")
        for idx, mfg in enumerate(possible_manufacturers):
            print("\t%d %s" %(idx, mfg.name, ))
        print("="*20)
        idx = int(input("> "))
        return possible_manufacturers[idx]
Exemple #6
0
def create_supplier(supplier_name: str) -> bool:
    ''' Create InvenTree supplier '''
    global inventree_api

    company = Company.create(
        inventree_api, {
            'name': supplier_name,
            'description': supplier_name,
            'is_customer': False,
            'is_supplier': True,
            'is_manufacturer': False,
        })

    return company
Exemple #7
0
def create_company(company_name: str, manufacturer=False, supplier=False) -> bool:
    ''' Create InvenTree company '''
    global inventree_api

    if not manufacturer and not supplier:
        return None

    company = Company.create(inventree_api, {
        'name': company_name,
        'description': company_name,
        'is_customer': False,
        'is_supplier': supplier,
        'is_manufacturer': manufacturer,
    })

    return company
Exemple #8
0
def is_new_manufacturer_part(manufacturer_name: str,
                             manufacturer_mpn: str) -> bool:
    ''' Check if InvenTree manufacturer part exists to avoid duplicates '''
    global inventree_api

    # Fetch all companies
    cprint(f'[TREE]\tFetching manufacturers', silent=settings.HIDE_DEBUG)
    company_list = Company.list(inventree_api,
                                is_manufacturer=True,
                                is_customer=False)
    companies = {}
    for company in company_list:
        companies[company.name] = company

    try:
        # Get all parts
        part_list = companies[manufacturer_name].getManufacturedParts()
    except:
        part_list = None

    if part_list == None:
        # Create
        cprint(f'[TREE]\tCreating new manufacturer "{manufacturer_name}"',
               silent=settings.SILENT)
        create_company(
            company_name=manufacturer_name,
            manufacturer=True,
        )
        # Get all parts
        part_list = []

    for item in part_list:
        try:
            if manufacturer_mpn in item.MPN:
                cprint(f'[TREE]\t{item.MPN} ?= {manufacturer_mpn} => True',
                       silent=settings.HIDE_DEBUG)
                return False
            else:
                cprint(f'[TREE]\t{item.MPN} ?= {manufacturer_mpn} => False',
                       silent=settings.HIDE_DEBUG)
        except TypeError:
            cprint(
                f'[TREE]\t{item.MPN} ?= {manufacturer_mpn} => *** SKIPPED ***',
                silent=settings.HIDE_DEBUG)

    return True
Exemple #9
0
        if mpn in MPN or len(MPN) >= 5:
            continue

        MPN.add(mpn)

        sku = product.digi_key_part_number

        man_name = product.manufacturer.value

        if man_name in manufacturers.keys():
            manufacturer = manufacturers[man_name]
        else:

            # Search InvenTree for manufacturer name
            query = Company.list(inventree, search=man_name)

            if len(query) == 0:

                print(f"Creating new manufacturer: '{man_name}'")

                manufacturer = Company.create(inventree,
                                              data={
                                                  'is_supplier': False,
                                                  'is_manufacturer': True,
                                                  'name': man_name,
                                              })

            else:
                manufacturer = query[0]
Exemple #10
0
def create_supplier_part(part_id: int, supplier_name: str, supplier_sku: str,
                         description: str, manufacturer_name: str,
                         manufacturer_pn: str, datasheet: str) -> bool:
    ''' Create InvenTree supplier part
	
		supplier: Company that supplies this SupplierPart object
		SKU: Stock keeping unit (supplier part number)
		manufacturer: Company that manufactures the SupplierPart (leave blank if it is the sample as the Supplier!)
		MPN: Manufacture part number
		link: Link to external website for this part
		description: Descriptive notes field 
	'''
    global inventree_api

    supplier_id = get_company_id(supplier_name)
    if not supplier_id:
        cprint(
            f'[TREE]\tError: Supplier "{supplier_name}" not found (failed to create supplier part)',
            silent=settings.SILENT)
        return False

    manufacturer_id = get_company_id(manufacturer_name)
    if not manufacturer_id:
        cprint(f'[TREE]\tCreating new manufacturer "{manufacturer_name}"',
               silent=settings.SILENT)
        '''
		name: Brief name of the company
		description: Longer form description
		is_customer: boolean value, is this company a customer
		is_supplier: boolean value, is this company a supplier
		is_manufacturer: boolean value, is this company a manufacturer
		'''
        manufacturer = Company.create(
            inventree_api, {
                'name': manufacturer_name,
                'description': manufacturer_name,
                'is_customer': False,
                'is_supplier': False,
                'is_manufacturer': True,
            })
        try:
            manufacturer_id = manufacturer.pk
        except AttributeError:
            manufacturer_id = None

    if manufacturer_id:
        # Validate datasheet link
        if not validators.url(datasheet):
            datasheet = ''

        supplier_part = SupplierPart.create(
            inventree_api, {
                'part': part_id,
                'supplier': supplier_id,
                'SKU': supplier_sku,
                'manufacturer': manufacturer_id,
                'MPN': manufacturer_pn,
                'link': datasheet,
                'description': description,
            })

        if supplier_part:
            return True

    return False