def import_mecalux_ZCS(self, file_id):
        res = False
        pick_obj = self.env['stock.picking']
        sga_file_obj = self.env['sga.file'].browse(file_id)
        sga_file = open(sga_file_obj.sga_file, 'r')
        sga_file_lines = sga_file.readlines()
        sga_file.close()
        str_error = ''
        n_line = 0
        sgavar = self.env['sgavar.file'].search([('code', '=', 'CSO')])
        pick = False
        if not sgavar:
            raise ValidationError("Modelo no encontrado")
        create = False
        LEN_HEADER = 470
        LEN_LINE = 344
        LEN_DETAIL_LINE = 436
        sga_ops_exists = False
        do_pick = False
        pool_ids = []
        for line in sga_file_lines:
            n_line += 1

            if len(line) == LEN_HEADER:
                if pick:
                    pick.do_pick(sga_ops_exists)
                sga_ops_exists = False
                st = 10
                en = st + 50
                sorder_code = line[st:en].strip()
                pick = pick_obj.search([('name', '=', sorder_code)])

                if not pick:
                    str_error += "Codigo de albaran %s no encontrado o estado incorrecto en linea ...%s " % (sorder_code, n_line)
                    error_message =  u'Albarán %s no encontrado o en estado incorrecto.' % (
                                      sorder_code)
                    self.create_sga_file_error(sga_file_obj, n_line, 'ZCS', pick, 'Pick no válido', error_message)

                    sga_file_obj.write_log(str_error)
                    continue

                pool_ids.append(pick.id)
                st = 60
                en = st + 10
                pick_status = line[st:en].strip()
                if pick_status == "CANCELED":
                    pick.sga_state = 'NE'
                    pick.message_post(body="Pick <em>%s</em> <b>ha sido cancelado en Mecalux</b>." % (pick.name))
                    pick = False
                    continue
                if pick.sga_state != "PM":
                    pick.message_post(body="Pick <em>%s</em> <b>ha sido realizado en Odoo antes que en Mecalux</b>." % (pick.name))
                    error_message = u'Albarán %s en estado incorrecto (%s)' % (
                                      sorder_code, pick.state)
                    self.create_sga_file_error(sga_file_obj, n_line,'ZCS',pick,'Pick no válido', error_message)

                    pick = False
                    continue
                #print "###############\nde mecalux %s\n##############"%pick.name

                st = 378
                en = st + 10
                pick_weight = sga_file_obj.format_from_mecalux_number(line[st:en].strip() or 0, (10, 10, 0))

                st = 388
                en = st + 10
                pick_packages = sga_file_obj.format_from_mecalux_number(line[st:en].strip() or 0, (10, 10, 0))

                st = 398
                en = st + 50
                carrier_operario = line[st:en].strip()

                if ',' in carrier_operario:
                    carrier_code, oper_code = carrier_operario.split(',')
                elif '.' in carrier_operario:
                    carrier_code, oper_code = carrier_operario.split('.')
                else:
                    carrier_code = carrier_operario
                    oper_code = False

                domain = [('carrier_code', '=', carrier_code)]
                carrier = self.env['delivery.carrier'].search(domain)

                carrier_id = carrier.id if carrier else False

                if oper_code:
                    domain = [('ref', '=', oper_code)]
                    user_id = self.env['res.users'].search(domain)
                    operator = user_id.display_name or oper_code
                else:
                    operator = ''

                st = 90
                en = st + 14
                date_done = sga_file_obj.format_from_mecalux_date(line[st:en].strip())
                vals = {'sga_state': 'MT',
                        'pick_weight': pick_weight,
                        'pick_packages': pick_packages,
                        'carrier_id': carrier_id,
                        'operator': operator,
                        'date_done': date_done}
                pick.write(vals)
            elif len(line) == LEN_LINE and pick:
                #Buscamos la operacion relacionada
                st = 0
                en = st + 10
                op_id = sga_file_obj.format_from_mecalux_number(line[st:en].strip() or 0, (10, 10, 0))
                op = self.env['stock.pack.operation'].search([('id', '=', op_id), ('picking_id', '=', pick.id)])
                if op_id and not op:

                    error_message = u'Op %s no encontrada en el albarán %s' % (op_id, pick.name)
                    self.create_sga_file_error(sga_file_obj, n_line,'ZCS',pick,'Op no encontrada', error_message)
                    bool_error = False
                    continue
                # cantidad a realizar
                st = 284
                en = st + 12
                qty_done = sga_file_obj.format_from_mecalux_number(line[st:en].strip() or 0, (12, 7, 5))
                if op:
                    op.write({'qty_done': qty_done, 'sga_changed': True})
                    sga_ops_exists = True
            else:
                continue

        if pick:
            pick.do_pick(sga_ops_exists)
        return pool_ids
    def import_mecalux_CRP(self, file_id):

        pick_obj = self.env['stock.picking']
        sga_file_obj = self.env['sga.file'].browse(file_id)
        sga_file = open(sga_file_obj.sga_file, 'r')
        sga_file_lines = sga_file.readlines()
        sga_file.close()
        bool_error = True
        LEN_HEADER = 460
        LEN_LINE = 362
        LEN_DETAIL_LINE = 88
        pool_ids = []
        n_line = 0
        res = False
        create = False
        pick = self.env['stock.picking']
        sga_ops_exists = False
        for line in sga_file_lines:
            line = line.strip()
            n_line += 1
            if len(line) == LEN_HEADER:
                if pick:
                    bool_error = pick.do_pick(sga_ops_exists, bool_error)
                sga_ops_exists = False
                pick = False
                #Busco pick
                st = 40
                en = st + 30
                rec_order_code = line[st:en].strip()
                pick = pick_obj.search([('name', '=', rec_order_code), ('sga_state', 'in', ('MC', 'PM'))])
                #if not pick:
                #    pick = pick_obj.search([('backorder_id.name', '=', rec_order_code), ('sga_state', 'in', ('PM', 'EI'))])
                if pick:
                    pool_ids.append(pick.id)
                    st = 70
                    en = st+30
                    sga_state = line[st:en].strip()
                    pick.sga_state = "MT" if sga_state == "CLOSE" else "MC"

                    st = 100
                    en = st + 14
                    date_done = line[st:en].strip()
                    pick.date_done = sga_file_obj.format_from_mecalux_date(date_done)
                    if sga_state == 'CANCEL':
                        pick.sga_state = "MC"
                        pick.message_post(body="Pick <em>%s</em> <b>ha sido cancelado en Mecalux</b>." % (pick.name))
                        pick = False
                        continue

                    pick.sga_state = "MT"
                else:
                    bool_error = False
                    str_error = "Codigo de albaran %s no encontrado o estado incorrecto en linea ...%s " % (rec_order_code, n_line)


                    error_message = u'Albarán %s no encontrado o en estado incorrecto.' % (rec_order_code)
                    self.create_sga_file_error(sga_file_obj, n_line, 'CRP', pick, 'Pick no válido', error_message)

                    sga_file_obj.write_log(str_error)

            elif len(line) == LEN_LINE and pick:
                # cantidad a realizada
                st = 186
                en = st + 12
                qty_done = sga_file_obj.format_from_mecalux_number(line[st:en].strip() or 0, (12, 7, 5))

                st = 210
                en = st + 10
                op_id = sga_file_obj.format_from_mecalux_number(line[st:en].strip() or 0, (10, 10, 0))
                op = self.env['stock.pack.operation'].search([('id', '=', op_id)])
                if op_id and not op:
                    bool_error = False

                    error_message = u'Op %s no encontrada en el albarán %s'%(op_id, pick.name)
                    self.create_sga_file_error(sga_file_obj, n_line, 'CRP', pick, 'Op no encontrada', error_message)

                # Si op existe, escribo qty_done, si no creo una linea de operacion con lo recibido
                if op:
                    op.qty_done = qty_done
                    op.sga_changed = True
                    sga_ops_exists = True

            else:
                continue
        if pick:
            bool_error = pick.do_pick(sga_ops_exists, bool_error)

        return pool_ids