Repository: inventree/InvenTree

Issue:
V0.8.4 Missing parameters in BOM export CSV format
### Please verify that this bug has NOT been raised before.

- [X] I checked and didn't find similar issue

### Describe the bug*

When exporting a BOM, CSV and XLSX output files are different.
In the CSV, parameters from the last component are missing (missing SKU and other empty parameters):

```
1,117,44,CSBRD22005,LINKY DEVELOPMENT BOARD,27,CS00026,DIGITAL_ISOLATOR_ADuM3402,QUAD-CHANNEL DIGITAL ISOLATORS,False,1.0,0,,U5,,0,0,,,,,+105,-40,SOIC16,,7.12€,,,,,,ANALOG DEVICES,ADUM3402BRWZ,MOUSER,584-ADUM3402BRWZ,,,,,,,,
1,118,44,CSBRD22005,LINKY DEVELOPMENT BOARD,40,CS00039,SINGLE_OPTO_DIGITAL_LTV-M601,High Speed 10MBit/s TTL Compatible Optocouplers,False,2.0,0,,"U6, U7",,0,0,,,,,+85,-40,SO4-5,,0.7€,,,,,,LITE-ON,LTV-M601,LCSC,C123201,MOUSER,859-LTV-M601,,,,,,
1,119,44,CSBRD22005,LINKY DEVELOPMENT BOARD,32,CS00031,QZ_12MHz_3.2x2.5_15ppm,Quartz CRYSTAL 12MHZ 8PF SMD,False,1.0,0,,Y1,,0,0,,,,12MHz,85,-40,3.2mmx2.5mm,,0.5€,,,,,,NDK,NX3225SA-12MHz-STD-CSR-6,LCSC,C481400,MOUSER


```
The save BOM as a XLSX file:

![image](https://user-images.githubusercontent.com/1468818/200411159-ccb6d1d2-747f-42e8-82c6-211585098c56.png)






### Steps to Reproduce

Export BOM as CSV and XLSX file.
I don't have any other BOM to test against another set of data.

### Expected behavior

Both files must contain the same parameters

### Deployment Method

- [X] Docker
- [ ] Bare metal

### Version Information

# Version Information:
InvenTree-Version: 0.8.4
Django Version: 3.2.15
Commit Hash: ca1fbf9
Commit Date: 20/10/2022
Database: postgresql
Debug-Mode: False
Deployed using Docker: True


### Relevant log output

_No response_


Discussion:
@fjullien would you be happy to provide the two output files for analysis? I cannot trivially reproduce this on my end.
Here are the two files:

https://e1.pcloud.link/publink/show?code=XZBIeQZ6wGWJnn0pg0xTbikdJ2KW5GhEn0k
https://e1.pcloud.link/publink/show?code=XZ2IeQZoSAhixFdPi7pTudQwmFjPQiu38JV
@fjullien I have tried to replicate this on my end, but cannot. It may be specific to the data being exported. Are you perhaps able to check if you can reproduce this with a different BOM?
@fjullien can you reproduce this on the demo instance https://inventree.org/demo.html ? It might be a bug related to a special character or a specific combination of characters.
I'll try to reproduce it or at least do some tests to check if it's because of a special character.
Thank you, that helps out a lot!
This problem doesn't happen with another BOM.
If I manually add a component to the board, this last component gets cut, I can't see the manufacturer and suppliers data.

I don't know what I can do to help. I now use XLSX output to post-process the BOM so I'm fine.

Feel free to close this issue.
@fjullien would you feel comfortable sharing a database dump through private channels so we can maybe try to reproduce that way? If yes please connect via code <AT> mjmair <DOT> COM

I would really love to get to the bottom of the problem rather then just ignore it.
This issue seems stale. Please react to show this is still important.
This is a hard one, I tried with multiple versions and environments. Sometimes it reproduces - sometimes not. The crux is that it is not stable and I never get it to reproduce with debug=true.
ok, I have found the error and a repeatable test with 0.11.0dev - working on a solution
So I have not found a solution but I have the problem down and reproducible well: in the specific case the data passed to `DownloadFile` - which wraps the response to be streamed - has a length of 5521 characters. If I set the Content-Length header to that, I am missing characters in the last line. The correct Content-Length would be 5560. I found that out while creating a test case, for which I wrapped the StreamingResponse object returned from DownloadFile and binarywrote the content to disk.
@inventree/triage any idea where this mismatch in length might come from?
@matmair can you point out in the code where you think this is happening?

Resolving patch:
diff --git a/InvenTree/InvenTree/helpers.py b/InvenTree/InvenTree/helpers.py
--- a/InvenTree/InvenTree/helpers.py
+++ b/InvenTree/InvenTree/helpers.py
@@ -525,6 +525,7 @@ def DownloadFile(data, filename, content_type='application/text', inline=False)
         A StreamingHttpResponse object wrapping the supplied data
     """
     filename = WrapWithQuotes(filename)
+    length = len(data)
 
     if type(data) == str:
         wrapper = FileWrapper(io.StringIO(data))
@@ -532,7 +533,9 @@ def DownloadFile(data, filename, content_type='application/text', inline=False)
         wrapper = FileWrapper(io.BytesIO(data))
 
     response = StreamingHttpResponse(wrapper, content_type=content_type)
-    response['Content-Length'] = len(data)
+    if type(data) == str:
+        length = len(bytes(data, response.charset))
+    response['Content-Length'] = length
 
     disposition = "inline" if inline else "attachment"
 


Test patch: