~julie-w/unifield-wm/UTP-758

« back to all changes in this revision

Viewing changes to msf_doc_import/initial_stock_inventory.py

  • Committer: jf
  • Date: 2016-01-22 16:07:10 UTC
  • mfrom: (2703.19.11 unifield-wm)
  • Revision ID: jfb@tempo-consulting.fr-20160122160710-qph6khzlk74f2hao
US-256 [FIX] Physical inventory import: more accurate error message
lp:~unifield-team/unifield-wm/us-256

Show diffs side-by-side

added added

removed removed

Lines of Context:
101
101
        # ignore the first row
102
102
        reader.next()
103
103
        line_num = 1
 
104
        product_error = []
104
105
        for row in reader:
105
106
            line_num += 1
106
107
            # Check length of the row
119
120
            product_qty = 0.00
120
121
            product_uom = False
121
122
            comment = ''
 
123
            bad_expiry = None
 
124
            bad_batch_name = None
122
125
            to_correct_ok = False
123
126
 
124
127
            # Product code
145
148
            if not product_id:
146
149
                to_correct_ok = True
147
150
                import_to_correct = True
148
 
                raise osv.except_osv(_('Error'), _('The Product [%s] %s was not found in the list of the products') % (product_code or '', p_name or ''))
 
151
                product_error.append(line_num)
 
152
                continue
149
153
 
150
154
            # Location
151
155
            loc_id = row.cells[2].data
177
181
                    batch = str(batch)
178
182
                try:
179
183
                    batch = batch.strip()
180
 
                    batch_ids = batch_obj.search(cr, uid, [('product_id', '=', product_id), ('name', '=', batch)], context=context)
181
 
                    if not batch_ids:
182
 
                        batch_name = batch
 
184
                    if ',' in batch:
 
185
                        bad_batch_name = True
 
186
                        batch_name = False
183
187
                        batch = False
184
188
                        to_correct_ok = True
185
189
                    else:
186
 
                        batch = batch_ids[0]
 
190
                        batch_ids = batch_obj.search(cr, uid, [('product_id', '=', product_id), ('name', '=', batch)], context=context)
 
191
                        if not batch_ids:
 
192
                            batch_name = batch
 
193
                            batch = False
 
194
                            to_correct_ok = True
 
195
                        else:
 
196
                            batch = batch_ids[0]
187
197
                except Exception:
188
198
                    batch = False
189
199
 
192
202
                if row.cells[4].type == 'datetime':
193
203
                    expiry = row.cells[4].data.strftime('%Y-%m-%d')
194
204
                else:
 
205
                    bad_expiry = True
 
206
                    comment += _('Incorrectly formatted expiry date.')
195
207
                    to_correct_ok = True
196
208
                    import_to_correct = True
197
209
                if expiry and not batch:
271
283
                'location_id': location_id,
272
284
                'prod_lot_id': batch,
273
285
                'expiry_date': expiry,
 
286
                'bad_expiry': bad_expiry,
 
287
                'bad_batch_name': bad_batch_name,
274
288
                'product_qty': product_qty,
275
289
                'product_uom': product_uom,
276
290
                'hidden_batch_management_mandatory': hidden_batch_management_mandatory,
281
295
 
282
296
            vals['inventory_line_id'].append((0, 0, to_write))
283
297
 
 
298
        if product_error:
 
299
            raise osv.except_osv(
 
300
                _('Error'),
 
301
                _('Product not found in the database for %s line%s: %s') % (
 
302
                    len(product_error) > 1 and 'these' or 'this',
 
303
                    len(product_error) > 1 and 's' or '',
 
304
                    ' / '.join(str(x) for x in product_error)),
 
305
            )
 
306
 
284
307
        # write order line on Inventory
285
308
        context['import_in_progress'] = True
286
309
        vals.update({'file_to_import': False})
369
392
        batch = vals.get('prod_lot_id')
370
393
        expiry = vals.get('expiry_date')
371
394
        batch_name = vals.get('batch_name')
 
395
        bad_expiry = vals.get('bad_expiry')
 
396
        bad_batch_name = vals.get('bad_batch_name')
 
397
 
 
398
        if 'bad_expiry' in vals:
 
399
            del vals['bad_expiry']
 
400
 
 
401
        if 'bad_batch_name' in vals:
 
402
            del vals['bad_batch_name']
372
403
 
373
404
        if not location_id:
374
405
            comment += _('Location is missing.\n')
375
406
 
376
407
        if hidden_batch_management_mandatory and not batch:
377
 
            if batch_name:
 
408
            if bad_batch_name:
 
409
                comment += _('Incorrect batch number format.\n')
 
410
            elif batch_name and not bad_expiry and expiry:
378
411
                comment += _('Batch not found.\n')
 
412
            elif batch_name and not bad_expiry and not expiry:
 
413
                comment += _('Expiry date is missing.\n')
 
414
            elif batch_name and bad_expiry:
 
415
                comment += _('Incorrectly formatted expiry date. Batch not created.\n')
379
416
            else:
380
417
                comment += _('Batch is missing.\n')
381
418
                vals['expiry_date'] = False
382
419
 
383
420
        if hidden_perishable_mandatory and not expiry and not batch and batch_name:
384
 
            comment += _('Batch not found.\n')
 
421
            if bad_batch_name:
 
422
                comment += _('Incorrect batch number format.\n')
 
423
            elif bad_expiry:
 
424
                comment += _('Incorrectly formatted expiry date.\n')
 
425
            else:
 
426
                comment += _('Batch not found.\n')
385
427
        elif hidden_perishable_mandatory and not expiry:
386
428
            comment += _('Expiry date is missing.\n')
387
429
            vals['expiry_date'] = False
513
555
            raise osv.except_osv(_('Error'), _('Nothing to import.'))
514
556
 
515
557
        product_cache = {}
 
558
        product_error = []
516
559
 
517
560
        fileobj = SpreadsheetXML(xmlstring=base64.decodestring(obj.file_to_import))
518
561
 
536
579
            location_id = False
537
580
            batch = False
538
581
            expiry = False
 
582
            bad_expiry = None
 
583
            batch_name = None
 
584
            bad_batch_name = None
539
585
            product_qty = 0.00
540
586
            product_uom = False
541
587
            comment = ''
565
611
            if not product_id:
566
612
                to_correct_ok = True
567
613
                import_to_correct = True
568
 
                raise osv.except_osv(_('Error'), _('The Product [%s] %s was not found in the list of the products') % (product_code or '', p_name or ''))
 
614
                product_error.append(line_num)
 
615
                continue
569
616
 
570
617
            # Average cost
571
618
            cost = row.cells[2].data
608
655
                try:
609
656
                    batch = batch.strip()
610
657
                    batch_name = batch
 
658
                    if ',' in batch_name:
 
659
                        batch_name = False
 
660
                        batch = False
 
661
                        bad_batch_name = True
611
662
                except Exception:
612
663
                    pass
613
664
 
616
667
                if row.cells[5].type == 'datetime':
617
668
                    expiry = row.cells[5].data
618
669
                else:
 
670
                    bad_expiry = True
 
671
                    comment += _('Incorrectly formatted expiry date.')
619
672
                    to_correct_ok = True
620
673
                    import_to_correct = True
621
674
            else:
661
714
                'location_id': location_id,
662
715
                'prodlot_name': batch,
663
716
                'expiry_date': expiry and expiry.strftime('%Y-%m-%d') or False,
 
717
                'bad_expiry': bad_expiry,
 
718
                'bad_batch_name': bad_batch_name,
664
719
                'product_qty': product_qty,
665
720
                'product_uom': product_uom,
666
721
                'hidden_batch_management_mandatory': hidden_batch_management_mandatory,
671
726
 
672
727
            vals['inventory_line_id'].append((0, 0, to_write))
673
728
 
 
729
        if product_error:
 
730
            raise osv.except_osv(
 
731
                _('Error'),
 
732
                _('Product not found in the database for %s line%s: %s') % (
 
733
                    len(product_error) > 1 and 'these' or 'this',
 
734
                    len(product_error) > 1 and 's' or '',
 
735
                    ' / '.join(str(x) for x in product_error)),
 
736
            )
 
737
 
674
738
        # write order line on Inventory
675
739
        vals.update({'file_to_import': False})
676
740
        self.write(cr, uid, ids, vals, context=context)
758
822
            vals.update({'prodlot_name':batch})
759
823
        expiry = vals.get('expiry_date')
760
824
        batch_name = vals.get('batch_name')
 
825
        bad_expiry = vals.get('bad_expiry')
 
826
        bad_batch_name = vals.get('bad_batch_name')
 
827
 
 
828
        if 'bad_expiry' in vals:
 
829
            del vals['bad_expiry']
 
830
 
 
831
        if 'bad_batch_name' in vals:
 
832
            del vals['bad_batch_name']
761
833
 
762
834
        if not location_id:
763
835
            comment += _('Location is missing.\n')
764
836
 
765
837
        if hidden_batch_management_mandatory and not batch:
766
 
            if batch_name:
 
838
            if bad_batch_name:
 
839
                comment += _('Incorrect batch number format.\n')
 
840
            elif batch_name and not bad_expiry:
767
841
                comment += _('Batch not found.\n')
 
842
            elif batch_name and bad_expiry:
 
843
                comment += _('Incorrectly formatted expiry date. Batch not created.\n')
768
844
            else:
769
845
                comment += _('Batch is missing.\n')
770
846
        if hidden_perishable_mandatory and not expiry:
771
 
            comment += _('Expiry date is missing.\n')
 
847
            if bad_expiry:
 
848
                comment += _('Incorrectly formatted expiry date.\n')
 
849
            else:
 
850
                comment += _('Expiry date is missing.\n')
772
851
 
773
852
        if not comment:
774
853
            vals.update({'comment': comment, 'to_correct_ok': False})