Changeset 0331028 in rattail
- Timestamp:
- 07/25/2022 11:13:55 AM (6 months ago)
- Branches:
- master
- Children:
- 9820a0f
- Parents:
- d094569
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
rattail/contrib/vendors/invoices/kehe.py
rd094569 r0331028 3 3 # 4 4 # Rattail -- Retail Software Framework 5 # Copyright © 2010-202 1Lance Edgar5 # Copyright © 2010-2022 Lance Edgar 6 6 # 7 7 # This file is part of Rattail. … … 33 33 import six 34 34 35 from rattail.db import model36 from rattail.gpc import GPC37 35 from rattail.vendors.invoices import InvoiceParser 38 36 … … 42 40 Vendor invoice parser for KeHE Distributors. 43 41 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. 49 45 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. 52 56 """ 53 57 key = 'rattail.contrib.kehe' … … 65 69 line = f.readline() 66 70 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 ',' 70 72 71 # the file *does* have tab characters, so we must check a column header72 73 if six.PY3: 73 74 csv_file = open(path, 'rt') 74 reader = csv.DictReader(csv_file, delimiter= '\t')75 reader = csv.DictReader(csv_file, delimiter=delimiter) 75 76 else: # PY2 76 77 csv_file = open(path, 'rb') 77 reader = csv.DictReader(csv_file, delimiter= b'\t')78 reader = csv.DictReader(csv_file, delimiter=delimiter) 78 79 data = next(reader) 79 80 csv_file.close() 80 81 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 83 90 return 1 84 85 # okay, assume new version 3 then86 # assert 'InvoiceDate' in data87 return 388 91 89 92 def parse_invoice_date(self, path): … … 104 107 if version == 1: 105 108 return datetime.datetime.strptime(data['Invoice Date'], '%Y-%m-%d').date() 106 el se: # version in (2, 3)109 elif version in (2, 3): 107 110 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() 108 113 109 114 def parse_rows(self, path): … … 126 131 } 127 132 128 if version in (2, 3):133 if version > 1: 129 134 fields.update({ 130 135 'upc': 'Upc', … … 135 140 'net_billable': 'NetBillable', 136 141 'pack_size': 'PackSize', 142 }) 143 144 if version == 4: 145 fields.update({ 146 'order_quantity': 'OrderQuatity', 137 147 }) 138 148 … … 146 156 for data in reader: 147 157 148 row = model.VendorInvoiceBatchRow()158 row = self.make_row() 149 159 row.item_entry = data[fields['upc']] 150 row.upc = GPC(row.item_entry)160 row.upc = self.app.make_gpc(row.item_entry) 151 161 row.vendor_code = data[fields['ship_item']] 152 162 row.brand_name = data[fields['brand']]
Note: See TracChangeset
for help on using the changeset viewer.