Changeset 0331028 in rattail


Ignore:
Timestamp:
07/25/2022 11:13:55 AM (6 months ago)
Author:
Lance Edgar <lance@…>
Branches:
master
Children:
9820a0f
Parents:
d094569
Message:

Update invoice parser for KeHE, per new format

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rattail/contrib/vendors/invoices/kehe.py

    rd094569 r0331028  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2021 Lance Edgar
     5#  Copyright © 2010-2022 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    3333import six
    3434
    35 from rattail.db import model
    36 from rattail.gpc import GPC
    3735from rattail.vendors.invoices import InvoiceParser
    3836
     
    4240    Vendor invoice parser for KeHE Distributors.
    4341
    44     This parser is actually capable of handling two different but similar
    45     formats.  CSV data is expected in both cases.  Basically the KeHE portal
    46     changed the format available for download, so the parser now needs to
    47     support both the "old" as well as "new" formats.  Note that the newer
    48     formats appeared circa 2020-02-11.
     42    KeHE has changed their format a few times; hence this parser is
     43    capable of handling a few similar but different formats.  All are
     44    essentially text/csv.
    4945
    50     See the :meth:`detect_version()` method for details of how the parser
    51     decides which "version" (format) of file it's dealing with.
     46    Format #1 is the original and is tab-separated.
     47
     48    Format #2 came circa 2020-02-11 and is comma-separated.
     49
     50    Format #3 is like #1 but changed a column header.
     51
     52    Format #4 is like #2 but changed column headers and data formats.
     53
     54    See the :meth:`detect_version()` method for details of how the
     55    parser decides which "version" (format) of file it's dealing with.
    5256    """
    5357    key = 'rattail.contrib.kehe'
     
    6569            line = f.readline()
    6670
    67         # if line has no tab characters, we can safely assume new version 2
    68         if '\t' not in line:
    69             return 2
     71        delimiter = str('\t') if '\t' in line else ','
    7072
    71         # the file *does* have tab characters, so we must check a column header
    7273        if six.PY3:
    7374            csv_file = open(path, 'rt')
    74             reader = csv.DictReader(csv_file, delimiter='\t')
     75            reader = csv.DictReader(csv_file, delimiter=delimiter)
    7576        else: # PY2
    7677            csv_file = open(path, 'rb')
    77             reader = csv.DictReader(csv_file, delimiter=b'\t')
     78            reader = csv.DictReader(csv_file, delimiter=delimiter)
    7879        data = next(reader)
    7980        csv_file.close()
    8081
    81         # check for old version 1 column header
    82         if 'Invoice Date' in data:
     82        if delimiter == ',': # comma-separated
     83            if 'OrderQuatity' in data:
     84                return 4
     85            return 2
     86
     87        else: # tab-separated
     88            if 'InvoiceDate' in data:
     89                return 3
    8390            return 1
    84 
    85         # okay, assume new version 3 then
    86         # assert 'InvoiceDate' in data
    87         return 3
    8891
    8992    def parse_invoice_date(self, path):
     
    104107        if version == 1:
    105108            return datetime.datetime.strptime(data['Invoice Date'], '%Y-%m-%d').date()
    106         else: # version in (2, 3)
     109        elif version in (2, 3):
    107110            return datetime.datetime.strptime(data['InvoiceDate'], '%m/%d/%Y %I:%M:%S %p').date()
     111        elif version == 4:
     112            return datetime.datetime.strptime(data['InvoiceDate'], '%m/%d/%Y').date()
    108113
    109114    def parse_rows(self, path):
     
    126131        }
    127132
    128         if version in (2, 3):
     133        if version > 1:
    129134            fields.update({
    130135                'upc': 'Upc',
     
    135140                'net_billable': 'NetBillable',
    136141                'pack_size': 'PackSize',
     142            })
     143
     144        if version == 4:
     145            fields.update({
     146                'order_quantity': 'OrderQuatity',
    137147            })
    138148
     
    146156        for data in reader:
    147157
    148             row = model.VendorInvoiceBatchRow()
     158            row = self.make_row()
    149159            row.item_entry = data[fields['upc']]
    150             row.upc = GPC(row.item_entry)
     160            row.upc = self.app.make_gpc(row.item_entry)
    151161            row.vendor_code = data[fields['ship_item']]
    152162            row.brand_name = data[fields['brand']]
Note: See TracChangeset for help on using the changeset viewer.