Ver Código Fonte

customer import

tum 8 meses atrás
pai
commit
f999eb3033

+ 53 - 0
app/core/management/commands/import_customer_templates.py

1
+import csv
2
+from django.core.management.base import BaseCommand
3
+from core.models import CustomerTemplateMapping
4
+from django.contrib.auth.models import User
5
+import json
6
+
7
+class Command(BaseCommand):
8
+    help = "Import CustomerTemplateMapping records from a CSV file"
9
+
10
+    def add_arguments(self, parser):
11
+        parser.add_argument('csv_path', type=str, help='Path to the CSV file')
12
+        parser.add_argument('--created_by', type=int, help='User ID to assign as created_by')
13
+
14
+    def handle(self, *args, **options):
15
+        csv_path = options['csv_path']
16
+        created_by = None
17
+
18
+        if options.get('created_by'):
19
+            try:
20
+                created_by = User.objects.get(id=options['created_by'])
21
+            except User.DoesNotExist:
22
+                self.stdout.write(self.style.ERROR(f"User with ID {options['created_by']} does not exist."))
23
+                return
24
+
25
+        with open(csv_path, newline='', encoding='utf-8') as csvfile:
26
+            reader = csv.DictReader(csvfile)
27
+            count = 0
28
+
29
+            for row in reader:
30
+                name = row.get("customer_name")
31
+                templates = row.get("template_names")
32
+
33
+                if not name or not templates:
34
+                    self.stderr.write(f"Skipping incomplete row: {row}")
35
+                    continue
36
+
37
+                try:
38
+                    template_list = json.loads(templates)
39
+                except json.JSONDecodeError:
40
+                    self.stderr.write(f"Skipping row with invalid JSON: {templates}")
41
+                    continue
42
+
43
+                CustomerTemplateMapping.objects.update_or_create(
44
+                    customer_name=name,
45
+                    defaults={
46
+                        "template_names": template_list,
47
+                        "created_by": created_by,
48
+                    }
49
+                )
50
+
51
+                count += 1
52
+
53
+        self.stdout.write(self.style.SUCCESS(f"Imported {count} customer template mappings."))

+ 1 - 0
app/core/utils.py

232
     'hardness_out_in': 'Hardness Out/In', 
232
     'hardness_out_in': 'Hardness Out/In', 
233
     'hardness_both_size': 'Hardness Both Size',
233
     'hardness_both_size': 'Hardness Both Size',
234
     'dimension': 'Dimension',
234
     'dimension': 'Dimension',
235
+    'dimension_weight_warp': 'Dimension/Weight/Warp',
235
     'dimension_app': 'Dimension Appearance',
236
     'dimension_app': 'Dimension Appearance',
236
     'dimension_app_drawing': 'Dimension Appearance + Drawing',
237
     'dimension_app_drawing': 'Dimension Appearance + Drawing',
237
     'dimension_bal_weight': 'Dimension Balance/Weight',
238
     'dimension_bal_weight': 'Dimension Balance/Weight',

+ 37 - 0
app/customer_templates.csv

1
+customer_name,template_names
2
+AIZEN,"[""dim_bal_app_hard""]"
3
+DYNAX,"[""dim_bal_app_hard""]"
4
+FSK,"[""dim_bal_app_hard""]"
5
+FUJI MACHINERY,"[""dimension""]"
6
+FUTABA KENNMA,"[""hardness_out""]"
7
+ITOU SEIKOU,"[""dim_bal_app_hard""]"
8
+JTEKT MACHINSYSTEMS,"[""dimension_app"", ""dimension_app_drawing""]"
9
+KARINO,"[""dimension"", ""dimension_weight_warp""]"
10
+KAWASAKI JUKOUGYOU,"[""dimension"", ""dimension_weight_warp""]"
11
+KOKI HOLDINGS,"[""dim_bal_app_hard""]"
12
+KUBOTA,"[""dim_bal_app_hard""]"
13
+KOMATSU SEISAKUSHO,"[""dim_bal_app_hard""]"
14
+MASAKI HAMONO,"[""hardness_out""]"
15
+MASTUDA KOUGYOU,"[""dim_bal_app_hard""]"
16
+MEIKOU HATSUJYO,"[""dim_bal_app_hard""]"
17
+MITSUBISHI JIDOUSHA,"[""dim_bal_app_hard""]"
18
+MIZUHO,"[""dim_bal_app_hard""]"
19
+MIZUNO TEKKOUSHO,"[""hardness_out_in""]"
20
+MUSASHINOSEIKI,"[""dim_bal_app_hard""]"
21
+NIPPON PISTON RING,"[""dim_bal_app_hard""]"
22
+NIWA TEKKOUSHO,"[""dimension"", ""dimension_weight_warp""]"
23
+NOBE SEISAKUSHO,"[""dim_bal_app_hard""]"
24
+NSK HARUNA,"[""hardness_out_in""]"
25
+NTN TADO,"[""dim_bal_app_hard""]"
26
+OKUMA,"[""dimension_app"", ""dimension_app_drawing""]"
27
+OSAKA SEIMITSU KOUGYO,"[""hardness_out""]"
28
+RIKEN,"[""dim_bal_app_hard""]"
29
+SANJO MCHINEWORKS,"[""dim_bal_app_hard""]"
30
+SANSIN,"[""hardness_out""]"
31
+SANYO,"[""dim_bal_app_hard""]"
32
+SHINNICHI KOGYO,"[""dim_bal_app_hard""]"
33
+SOUKA CENTERLESS,"[""dim_bal_app_hard""]"
34
+SUBARU,"[""dimension"", ""dimension_weight_warp""]"
35
+TOKYU,"[""dim_bal_app_hard""]"
36
+TOHSHIN SEIKI,"[""dim_bal_app_hard""]"
37
+NIPPISU IWATE,"[""hardness_out_in""]"

BIN
app/report/coi_templates.xlsx


+ 10 - 1
app/report/gen_report.py

137
 
137
 
138
 
138
 
139
 def gen_xlsx(template_file, selected_sheets, prefix_filename, data):
139
 def gen_xlsx(template_file, selected_sheets, prefix_filename, data):
140
+    pprint(f"5.0 = {template_file}, {selected_sheets}, {prefix_filename}, {data}")
140
     """
141
     """
141
     Generate an Excel file from a template, fill placeholders, and include only selected sheets.
142
     Generate an Excel file from a template, fill placeholders, and include only selected sheets.
142
 
143
 
179
                     # Determine value priority: `sheet_name.key` > `key`
180
                     # Determine value priority: `sheet_name.key` > `key`
180
                     value = None
181
                     value = None
181
                     sheet_specific_key = f"{sheet_name}.{placeholder}"
182
                     sheet_specific_key = f"{sheet_name}.{placeholder}"
183
+                    pprint(f"5.01 = {sheet_specific_key}")
184
+                    pprint(sheet_specific_key in data)
182
                     if sheet_specific_key in data:
185
                     if sheet_specific_key in data:
183
                         value = data[sheet_specific_key]
186
                         value = data[sheet_specific_key]
184
                     elif placeholder in data:
187
                     elif placeholder in data:
259
                             cell.value = value
262
                             cell.value = value
260
 
263
 
261
         for key, value in data.items():
264
         for key, value in data.items():
265
+            pprint(f"7 = {key} => {value}")
266
+            
262
             # if isinstance(value, str) and re.match(r"^\d+\[\d+:\d+\]$", value):
267
             # if isinstance(value, str) and re.match(r"^\d+\[\d+:\d+\]$", value):
263
             if isinstance(value, str) and re.match(r"^[0-9\s]+\[\d+:\d+\]$", value):
268
             if isinstance(value, str) and re.match(r"^[0-9\s]+\[\d+:\d+\]$", value):
264
                 # Parse the prefix and row range
269
                 # Parse the prefix and row range
267
                 
272
                 
268
                 # Hide rows if the prefix matches the condition
273
                 # Hide rows if the prefix matches the condition
269
                 # if prefix == "0":  # Adjust the condition as needed
274
                 # if prefix == "0":  # Adjust the condition as needed
275
+                pprint(f"5.1 = {sheet} {value} => {prefix} ({row_start}, {row_end})")
276
+                
270
                 if " " in prefix or prefix.strip() == "0":
277
                 if " " in prefix or prefix.strip() == "0":
271
-                    sheet.row_dimensions.group(row_start, row_end, hidden=True)
278
+                    if key.split('.')[0] == sheet.title:
279
+                        pprint(f"HIDE !!! {key.split('.')[0]} {sheet.title} = {row_start}, {row_end}")
280
+                        sheet.row_dimensions.group(row_start, row_end, hidden=True)
272
         
281
         
273
 
282
 
274
     # Generate the output filename with a timestamp
283
     # Generate the output filename with a timestamp

+ 1 - 1
app/report/templates/report/_tbl.html

1
 {% load legacy_filters %}
1
 {% load legacy_filters %}
2
 <div class="clear-both bg-white shadow rounded-lg overflow-x-auto my-6">
2
 <div class="clear-both bg-white shadow rounded-lg overflow-x-auto my-6">
3
-<h2 class="text-2xl md:text-2xl font-semibold text-gray-700 dark:text-gray-200  m-3 tracking-wide leading-snug">
3
+<h2 class="text-2xl md:text-2xl font-semibold text-gray-700   m-3 tracking-wide leading-snug">
4
   {{ header }}</h2>
4
   {{ header }}</h2>
5
 <table class="w-full border-collapse border border-gray-200">
5
 <table class="w-full border-collapse border border-gray-200">
6
     <thead>
6
     <thead>

+ 77 - 4
app/report/views.py

194
     # else:
194
     # else:
195
         # placeholders[f"v3_1"] = "0[25:28]"
195
         # placeholders[f"v3_1"] = "0[25:28]"
196
 
196
 
197
-    hide_con(placeholders, "v3_1", "25:28")
197
+    hide_con(placeholders, "v3_1", "26:29")
198
     
198
     
199
     placeholders['inspect_date'] = inspect_date.strftime('%Y/%m/%d') if inspect_date else "-"
199
     placeholders['inspect_date'] = inspect_date.strftime('%Y/%m/%d') if inspect_date else "-"
200
     return placeholders
200
     return placeholders
289
             placeholders[f'v6_2'] = r.hsize
289
             placeholders[f'v6_2'] = r.hsize
290
             placeholders[f'v6_3'] = r.hsizeok
290
             placeholders[f'v6_3'] = r.hsizeok
291
 
291
 
292
-    hide_con(placeholders, "v4_1", "24:28")
292
+    hide_con(placeholders, "v4_2", "25:29")
293
+    placeholders['inspect_date'] = inspect_date.strftime('%Y/%m/%d') if inspect_date else "-"
294
+    return placeholders
295
+
296
+def generate_dimension_weight_warp_values(lot_no, code):
297
+    """
298
+    Fetch dimension records from manualSize and DataMs models
299
+    and generate placeholder values for Standard, Actual, and Judgement.
300
+    Supports two row_no entries per lot.
301
+    """
302
+    # Fetch standard values from manualSize (limit to 2 rows)
303
+    manual_size_records = Manualsize.objects.filter(lotno=lot_no)
304
+    dimens = AllProductDimensionForInsProcess.objects.filter(ProductCode=code)
305
+
306
+    # Fetch actual and judgement values from DataMs ordered by row_no (limit to 2 rows)
307
+    data_ms_records = DataMs.objects.filter(lot_no=lot_no).order_by('row_no')
308
+    data_wb = DataWb.objects.filter(lot_no=lot_no).order_by('row_no')
309
+    # Prepare placeholders
310
+
311
+    # placeholders = {}
312
+    placeholders = clear_values(7,4)
313
+    # for i in range(1,7):
314
+        # for j in range(1,4):
315
+            # placeholders[f'v{i}_{j}'] = 0
316
+    
317
+    pprint(placeholders)
318
+    pprint(manual_size_records) 
319
+    for m in dimens:
320
+        if m.Size_Name == "D":
321
+            placeholders['v1_1'] = placeholders['v4_1'] = f'D{m.Std:.2f} +{m.TolUp:.2f} {m.TolUn:.2f}'
322
+        if m.Size_Name == "T":
323
+            placeholders['v2_1'] = placeholders['v5_1'] = f'T{m.Std:.2f} +{m.TolUp:.2f} {m.TolUn:.2f}'
324
+        if m.Size_Name == "H":
325
+            placeholders['v3_1'] = placeholders['v6_1'] = f'H{m.Std:.2f} +{m.TolUp:.2f} {m.TolUn:.2f}'
326
+
327
+
328
+
329
+    # Ensure that we map each manualSize entry to a corresponding DataMs entry
330
+    inspect_date = None
331
+    for r in data_ms_records:
332
+        if r.row_no == 1:
333
+            placeholders[f'v1_2'] = r.dsize
334
+            placeholders[f'v1_3'] = r.dsizeok
335
+            
336
+            placeholders[f'v2_2'] = r.tsize
337
+            placeholders[f'v2_3'] = r.tsizeok
338
+            
339
+            placeholders[f'v3_2'] = r.hsize
340
+            placeholders[f'v3_3'] = r.hsizeok
341
+            inspect_date = r.created_at
342
+        
343
+        if r.row_no == 2:
344
+            placeholders[f'v4_2'] = r.dsize
345
+            placeholders[f'v4_3'] = r.dsizeok
346
+            
347
+            placeholders[f'v5_2'] = r.tsize
348
+            placeholders[f'v5_3'] = r.tsizeok
349
+            
350
+            placeholders[f'v6_2'] = r.hsize
351
+            placeholders[f'v6_3'] = r.hsizeok
352
+
353
+
354
+    for r in data_wb:
355
+        if r.row_no == 1:
356
+            placeholders["v7_1"] = r.weight
357
+            placeholders["v7_2"] = r.judgement
358
+        if r.row_no == 2:
359
+            placeholders["v8_1"] = r.weight
360
+            placeholders["v8_2"] = r.judgement
361
+    
362
+    hide_con(placeholders, "v4_2", "29:37")
293
     placeholders['inspect_date'] = inspect_date.strftime('%Y/%m/%d') if inspect_date else "-"
363
     placeholders['inspect_date'] = inspect_date.strftime('%Y/%m/%d') if inspect_date else "-"
294
     return placeholders
364
     return placeholders
295
 
365
 
382
                 # placeholders[f'v8_1'] = 'NG'
452
                 # placeholders[f'v8_1'] = 'NG'
383
                 # placeholders[f'v8_2'] = 'NG'
453
                 # placeholders[f'v8_2'] = 'NG'
384
 
454
 
385
-    hide_con(placeholders, "v5_1", "26:32")
455
+    hide_con(placeholders, "v5_1", "29:37")
386
     placeholders['inspect_date'] = inspect_date.strftime('%Y/%m/%d') if inspect_date else "-"
456
     placeholders['inspect_date'] = inspect_date.strftime('%Y/%m/%d') if inspect_date else "-"
387
     return placeholders
457
     return placeholders
388
 
458
 
465
                 # placeholders[f'v8_1'] = 'NG'
535
                 # placeholders[f'v8_1'] = 'NG'
466
                 # placeholders[f'v8_2'] = 'NG'
536
                 # placeholders[f'v8_2'] = 'NG'
467
 
537
 
468
-    hide_con(placeholders, "v5_1", "26:32")
538
+    hide_con(placeholders, "v5_1", "27:33")
469
     
539
     
470
     drawing_data = ProductDrawing.objects.filter(
540
     drawing_data = ProductDrawing.objects.filter(
471
         Q(lot_no=lot_no) | Q(code_no=code)).first()
541
         Q(lot_no=lot_no) | Q(code_no=code)).first()
955
             sheet_data[sheet_name] = generate_hardness_both_size_values(lot_no, first_result, code)
1025
             sheet_data[sheet_name] = generate_hardness_both_size_values(lot_no, first_result, code)
956
         elif sheet_name == 'dimension':
1026
         elif sheet_name == 'dimension':
957
             sheet_data[sheet_name] = generate_dimension_values(lot_no, code)
1027
             sheet_data[sheet_name] = generate_dimension_values(lot_no, code)
1028
+
1029
+        elif sheet_name == 'dimension_weight_warp':
1030
+            sheet_data[sheet_name] = generate_dimension_weight_warp_values(lot_no, code)
958
         elif sheet_name == 'dimension_app':
1031
         elif sheet_name == 'dimension_app':
959
             sheet_data[sheet_name] = generate_dimension_app_values(lot_no, code)
1032
             sheet_data[sheet_name] = generate_dimension_app_values(lot_no, code)
960
         elif sheet_name == 'dimension_app_drawing':
1033
         elif sheet_name == 'dimension_app_drawing':

+ 8 - 0
app/result3.csv

1
+customer_name,template_names
2
+ABC,"[""hardness_out"", ""dimension_app"", ""dimension_bal_weight""]"
3
+CDEF,"[""hardness_out_in"", ""hardness_both_size"", ""dimension_app""]"
4
+FFF,"[""hardness_both_size"", ""dimension""]"
5
+CCC,"[""dimension"", ""dimension_app""]"
6
+TypeB,"[""hardness_out"", ""hardness_out_in"", ""dimension_bal_weight"", ""dim_bal_app_hard"", ""dim_bal_app_rot_hard""]"
7
+AIZEN,"[""dim_bal_app_hard""]"
8
+DYNAX,"[""dim_bal_app_hard""]"