Changeset - 693308a7a801
[Not reviewed]
0 2 0
Lance Edgar (lance) - 3 years ago 2022-03-09 18:25:39
lance@edbob.org
Add basic UNFI catalog parser for tab-separated format
2 files changed with 50 insertions and 8 deletions:
0 comments (0 inline, 0 general)
rattail/contrib/vendors/catalogs/unfi.py
Show inline comments
 
@@ -2,7 +2,7 @@
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2021 Lance Edgar
 
#  Copyright © 2010-2022 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
@@ -28,7 +28,7 @@ from __future__ import unicode_literals, absolute_import
 

	
 
import re
 
import datetime
 
from decimal import Decimal
 
import decimal
 

	
 
import six
 
import xlrd
 
@@ -36,6 +36,7 @@ import xlrd
 
from rattail.db import model
 
from rattail.gpc import GPC
 
from rattail.vendors.catalogs import CatalogParser
 
from rattail.csvutil import UnicodeDictReader
 

	
 

	
 
class UNFICatalogParser(CatalogParser):
 
@@ -43,7 +44,7 @@ class UNFICatalogParser(CatalogParser):
 
    Vendor catalog parser for UNFI, version 1.
 
    """
 
    key = 'rattail.contrib.unfi'
 
    display = "United Natural Foods (UNFI), v1"
 
    display = "United Natural Foods (UNFI), Excel v1"
 
    vendor_key = 'unfi'
 

	
 
    effective_date_pattern = re.compile(r'effective from (\d{2}/\d{2}/\d{4})-')
 
@@ -85,8 +86,8 @@ class UNFICatalogParser(CatalogParser):
 
            row.description = sheet.cell_value(r, 3)
 
            row.size = sheet.cell_value(r, 5)
 
            row.case_size = int(sheet.cell_value(r, 4))
 
            row.case_cost = Decimal(six.text_type(sheet.cell_value(r, 8)))
 
            row.unit_cost = Decimal(six.text_type(sheet.cell_value(r, 9)))
 
            row.case_cost = decimal.Decimal(six.text_type(sheet.cell_value(r, 8)))
 
            row.unit_cost = decimal.Decimal(six.text_type(sheet.cell_value(r, 9)))
 
            yield row
 

	
 

	
 
@@ -95,7 +96,7 @@ class UNFICatalogParser2(UNFICatalogParser):
 
    Vendor catalog parser for UNFI, version 2.
 
    """
 
    key = 'rattail.contrib.unfi.2'
 
    display = "United Natural Foods (UNFI), v2"
 
    display = "United Natural Foods (UNFI), Excel v2"
 

	
 
    def parse_rows(self, path, progress=None):
 
        sheet = self.open_sheet(path)
 
@@ -121,6 +122,46 @@ class UNFICatalogParser2(UNFICatalogParser):
 
            row.description = sheet.cell_value(r, 4)
 
            row.size = sheet.cell_value(r, 7)
 
            row.case_size = int(sheet.cell_value(r, 6))
 
            row.case_cost = Decimal(six.text_type(sheet.cell_value(r, 10)))
 
            row.unit_cost = Decimal(six.text_type(sheet.cell_value(r, 11)))
 
            row.case_cost = decimal.Decimal(six.text_type(sheet.cell_value(r, 10)))
 
            row.unit_cost = decimal.Decimal(six.text_type(sheet.cell_value(r, 11)))
 
            yield row
 

	
 

	
 
class UNFITabSeparatedCatalogParser(CatalogParser):
 
    """
 
    Vendor catalog parser for UNFI, tab-separated values format
 
    """
 
    key = 'rattail.contrib.unfi.tsv'
 
    display = "United Natural Foods (UNFI), tab-separated"
 
    vendor_key = 'unfi'
 

	
 
    def parse_rows(self, csv_path, progress=None):
 
        if six.PY3:
 
            csv_file = open(csv_path, 'rt', encoding='latin_1')
 
            # nb. skip first "message" line
 
            next(csv_file)
 
            reader = UnicodeDictReader(csv_file, dialect='excel-tab')
 
        else: # PY2
 
            csv_file = open(csv_path, 'rb')
 
            # nb. skip first "message" line
 
            next(csv_file)
 
            reader = UnicodeDictReader(csv_file, encoding='latin_1',
 
                                       dialect='excel-tab')
 

	
 
        for csvrow in reader:
 
            row = self.make_row()
 

	
 
            row.item_entry = csvrow['UPRD']
 
            row.item_id = csvrow['UPCC']
 
            row.upc = self.app.make_gpc(csvrow['UPCC'])
 
            row.vendor_code = csvrow['UPRD']
 
            row.brand_name = csvrow['BRND']
 
            row.description = csvrow['DESC']
 
            row.size = csvrow['SIZE']
 
            row.case_size = self.decimal(csvrow['PACK'])
 
            row.case_cost = int(csvrow['CAVD']) / decimal.Decimal('100.00')
 
            row.unit_cost = row.case_cost / row.case_size
 

	
 
            yield row
 

	
 
        csv_file.close()
setup.py
Show inline comments
 
@@ -276,6 +276,7 @@ setup(
 
            'rattail.contrib.lotuslight = rattail.contrib.vendors.catalogs.lotuslight:LotusLightCatalogParser',
 
            'rattail.contrib.unfi = rattail.contrib.vendors.catalogs.unfi:UNFICatalogParser',
 
            'rattail.contrib.unfi.2 = rattail.contrib.vendors.catalogs.unfi:UNFICatalogParser2',
 
            'rattail.contrib.unfi.tsv = rattail.contrib.vendors.catalogs.unfi:UNFITabSeparatedCatalogParser',
 
        ],
 

	
 
        'rattail.vendors.invoices.parsers': [
0 comments (0 inline, 0 general)