Odoo / Openerp res.partner表上的多个创建在逐个成功时失败

时间:2015-10-07 10:33:34

标签: openerp odoo

以下是我要做的事情,我有一个CSV文件,其中包含一些我想在我的安装中导入的记录。当我在循环结束时放置return None语句时,以下代码有效。 (你会发现你已经注释掉了

点击一个按钮后,将执行此方法以循环CSV上的所有行,并根据条件创建新的res.partner记录。

(这已经通过XML-RPC来电实施,并且运行完美。)

问题是这样的:当我在导入一个CSV行后停止执行时,一切正常(创建res.partner上的记录),当我让它继续运行时,没有创建记录table res.partner(我没有错误,没有任何例外。我甚至在调用res_partner_obj.create()时获得新创建的记录的id。

注意:你会发现很多与我的问题无关的代码(这就是为什么没有创建res.partner上的记录)。我只是为了完整而把它放在那里)

def gms_import_test(self, cr, uid, vals, context=None, check=True):
        start_time = time.time()
        res_partner_obj = self.pool.get('res.partner')
        sales_teams_obj = self.pool.get('crm.case.section')
        res_partner_title_obj = self.pool.get('res.partner.title')
        customer_kunden_obj = self.pool.get('customer.kundengruppe')
        res_country_obj = self.pool.get('res.country')
        account_account_obj = self.pool.get('account.account')
        crm_phonecall_obj = self.pool.get('crm.phonecall')
        account_account_type_obj = self.pool.get('account.account.type')

        # sys.path[0] == module's path + /addons/gmsimport/+ file's name
        with open(sys.path[0] + '/addons/gmsimport/' + '21.9.2015.try8.csv') as csv_file:  # TODO THESE MUST CHANGE UPON DEPLOYMENT TO DIFFERENT MACHINES
            csv_reader = csv.reader(csv_file, delimiter='~', quotechar='^')

            # Get the teams
            sales_team_direct_sales_ID = sales_teams_obj.search(cr, uid, [('name', '=', 'Direct Sales')])
            sales_team_0_ID = sales_teams_obj.search(cr, uid, [('name', '=', '0')])
            sales_team_1_ID = sales_teams_obj.search(cr, uid, [('name', '=', '1')])
            sales_team_2_ID = sales_teams_obj.search(cr, uid, [('name', '=', '2')])
            sales_team_3_ID = sales_teams_obj.search(cr, uid, [('name', '=', '3')])
            sales_team_4_ID = sales_teams_obj.search(cr, uid, [('name', '=', '4')])
            sales_team_5_ID = sales_teams_obj.search(cr, uid, [('name', '=', '5')])
            sales_team_6_ID = sales_teams_obj.search(cr, uid, [('name', '=', '6')])
            sales_team_7_ID = sales_teams_obj.search(cr, uid, [('name', '=', '7')])
            sales_team_8_ID = sales_teams_obj.search(cr, uid, [('name', '=', '8')])
            sales_team_9_ID = sales_teams_obj.search(cr, uid, [('name', '=', '9')])

            # Search for the titles, create them if they do not exist
            damen_und_herren_title_ID = res_partner_title_obj.search(cr, uid, [('name', '=', 'Sehr geehrte Damen und Herren')])
            if not damen_und_herren_title_ID:
                damen_und_herren_title_ID = res_partner_title_obj.create(cr, uid, {'name':'Sehr geehrte Damen und Herren'})

            if type(damen_und_herren_title_ID) is list:
                damen_und_herren_title_ID = damen_und_herren_title_ID[0]

            frau_title_ID = res_partner_title_obj.search(cr, uid, [('name', '=', 'Sehr geehrte Frau')])
            if not frau_title_ID:
                frau_title_ID = res_partner_title_obj.create(cr, uid, {'name':'Sehr geehrte Frau'})

            if type(frau_title_ID) is list:
                frau_title_ID = frau_title_ID[0]

            herr_title_ID = res_partner_title_obj.search(cr, uid, [('name', '=', 'Sehr geehrter Herr')])
            if not herr_title_ID:
                herr_title_ID = res_partner_title_obj.create(cr, uid, {'name':'Sehr geehrter Herr'})

            if type(herr_title_ID) is list:
                herr_title_ID = herr_title_ID[0]

            account_type_id = account_account_type_obj.search(cr, uid, [('name', '=', 'Receivable')])
            # Checking to see whether there exists the "1200  - Forderungen aus Lieferungen und Leistungen account"
            forderungen_account = account_account_obj.search(cr, uid, [('code', '=', 1200)])
            if type(forderungen_account) is list and len(forderungen_account) > 0:
                forderungen_account = forderungen_account[0]

            account_payable_ID = account_account_obj.search(cr, uid, [('code', '=', 'Kunden - Diverse1')])
            if type(account_payable_ID) is list:
                account_payable_ID = account_payable_ID[0]


            next(csv_reader, None)  # To skip header row.
            row_counter = 2
            empty_name_counter = 0
            for row in csv_reader:
                print 'PROCESS IN ROW: ' + str(row_counter)

                final_customer_name = None

                if len(str(row[15]).strip()) > 0:
                    # If Firma is not empty, customer's name == Firma + Zusatz
                    final_customer_name = row[15] + ' ' + row[64]
                else:
                    # If Firma is empty, customer's name == Vorname + Name
                    final_customer_name = row[63] + ' ' + row[45]
                if final_customer_name:
                    empty_name_counter += 1
                logging.info("Customer name is " + str(final_customer_name))

                # search for the customer's existance, if exists do not add her
                customer_id = res_partner_obj.search(cr, uid, [('name', '=', final_customer_name)])
                print 'Searching with customer name ' + final_customer_name
                if not customer_id:
                    # Customer does not exist, only then make the calls
                    # Fields with relations that must be treated in a special way                
                    # x_kundengruppe, country_id, name
                    print 'customer.kundengruppe ' + str(row[6])
                    x_kundengruppe_id = customer_kunden_obj.search(cr, uid, [('name', '=', row[6].decode('utf8'))])
                    if not x_kundengruppe_id:
                        # kundergruppe does not exist (create a record on the model customer.kundengruppe and store its id)
                        print 'Creating kundengruppe'
                        x_kundengruppe_id = customer_kunden_obj.create(cr, uid, {'name':row[6]})
                    country_id = []
                    if str(row[27]).strip():
                        if str(row[27]) == 'Great Britain':
                            country_id = res_country_obj.search(cr, uid, [('name', '=', 'United Kingdom')])
                        else:
                            country_id = res_country_obj.search(cr, uid, [('name', '=', row[27])])


                    mittarbeitergrupe = None
                    if row[16] == '0 bis 4':
                        mittarbeitergrupe = '0_4'
                    elif row[16] == '5 bis 9':
                        mittarbeitergrupe = '5_9'
                    elif row[16] == '10 bis 19':
                        mittarbeitergrupe = '10_19'
                    elif row[16] == '20 bis 34':
                        mittarbeitergrupe = '20_34'
                    elif row[16] == '35 bis 49':
                        mittarbeitergrupe = '35_49'
                    elif row[16] == '50 bis 99':
                        mittarbeitergrupe = '50_99'
                    elif row[16] == 'über 100':
                        mittarbeitergrupe = 'uber_100'


                    final_customer_number_list = []
                    final_customer_number = None
                    print row[10]
                    if len(row[10]) < 8:
                        # Get row[10] length. Subtract it from 8. That's how many zeros must be put. Create a new string with zeros in front and number postfixed
                        zeros = 8 - len(row[10])
                        # final_customer_number_list.append('\'')
                        for y in range(0, zeros):
                            final_customer_number_list.append(str(0))
                        final_customer_number_list.append(str(row[10]))
                        # final_customer_number_list.append('\'')
                        final_customer_number = ''.join(final_customer_number_list)
                        print 'Customer\'s number length < 8. Prefixing ' + str(zeros) + ' zeros'
                        print 'final x_customer_number: ' + str(final_customer_number)
                    else:
                        final_customer_number = str(row[10])
                    # Make the country_id from an array to a single int variable
                    if len(country_id) > 0:
                        print 'SETTING COUNTRY ID TO ' + str(country_id)
                        country_id = country_id[0]
                    if type(x_kundengruppe_id) is list and len(x_kundengruppe_id) > 0:
                        x_kundengruppe_id = x_kundengruppe_id[0]
                    comment_section = self.assemble_internal_log(row)
                    final_title = self.assemble_customer_title(row, damen_und_herren_title_ID, frau_title_ID, herr_title_ID)            

                    # Check x_mitarbeiter 
                    if len(row[2].strip()) == 0:
                        row[2] = 0
                    else:
                        row[2] = int(row[2])
                    fields_to_be_inserted = {  'x_kundengruppe':x_kundengruppe_id,
                                         'x_customer_number':final_customer_number,  # (customer number cannot be less than 8 digits)
                                         'vat':row[12],
                                         'email':row[14],
                                         'name':final_customer_name,
                                         'x_mittarbeitergruppe':mittarbeitergrupe,
                                         'title':final_title,
                                         'x_a_kunde':row[23],
                                         'website':row[24],
                                         'country_id':country_id,
                                         'mobile':filter(lambda x: x.isdigit(), row[44]),
                                         'city':row[46],
                                         'zip':row[49],
                                         'function':str(row[50]),
                                         'street':row[57] + str(row[21]),  # street and House No
                                         'fax':filter(lambda x: x.isdigit(), row[59]),
                                         'phone':filter(lambda x: x.isdigit(), row[60]),
                                         'comment':comment_section,
                                         'x_mitarbeiter':row[2],
                                         'property_account_payable':account_payable_ID,
                                         }
                    log_entries = []
                    # column_index_list = [67, 68, 76, 77, 85, 86, 94, 95, 103, 104, 112, 113, 121, 122, 130, 131, 139, 140, 148, 149, 157, 158, 166, 167, 175, 176, 184, 185, 193, 194, 202, 203, 211, 212, 220, 221, 229, 230, 238, 239, 247, 248]
                    column_index_list = [67, 76, 85, 94, 103, 112, 121, 130, 139, 148, 157, 166, 175, 184, 193, 202, 211, 220, 229, 238, 247]
                    # search through the CSV to find those fields that contain (log note/date) and put them in internal log 
                    for x in column_index_list:
                        if len(row[x].strip()) > 0:
                            print 'Log entry found, adding to the list'
                            log_entries.append(row[x + 1] + '||||' + row[x])     

                if customer_id:
                    # Customer exists, do not add her
                    print 'Customer ' + final_customer_name + ' exists. We do not add this one.'
                    logging.info('Customer ' + final_customer_name + ' exists. We do not add this one.')

                else:
                    try:
                        logging.info('Creating customer ' + str(final_customer_name) + ', ' + str(row[15]))
                        created_customer_id = res_partner_obj.create(cr, uid, fields_to_be_inserted)

                        print 'CREATED CUSTOMER: ' + str(final_customer_name) + ' ID: ' + str(created_customer_id)
                        sales_team_id = None
                        # If PLZ field is not empty and is a 5 digit number
                        if row[49] and str(row[49]).isdigit() and len(str(row[49])) == 5:
                            # Check the first digit and assign to it a Sales Team
                            if int(str(row[49])[0]) == 1:
                                sales_team_id = sales_team_1_ID
                            elif int(str(row[49])[0]) == 2:
                                sales_team_id = sales_team_2_ID
                            elif int(str(row[49])[0]) == 3:
                                sales_team_id = sales_team_3_ID
                            elif int(str(row[49])[0]) == 4:
                                sales_team_id = sales_team_4_ID
                            elif int(str(row[49])[0]) == 5:
                                sales_team_id = sales_team_5_ID
                            elif int(str(row[49])[0]) == 6:
                                sales_team_id = sales_team_6_ID
                            elif int(str(row[49])[0]) == 7:
                                sales_team_id = sales_team_7_ID
                            elif int(str(row[49])[0]) == 8:
                                sales_team_id = sales_team_8_ID
                            elif int(str(row[49])[0]) == 9:
                                sales_team_id = sales_team_9_ID

                        # If the PLZ field is not empty and is a 4 digit number
                        elif row[49] and str(row[49]).isdigit() and len(str(row[49])) == 4:  # int(row[49]) >= 0 and int(row[49]) <= 9999:
                            sales_team_id = sales_team_0_ID
                        # Everything else goes to the Direct Sales team
                        else:
                            sales_team_id = sales_team_direct_sales_ID

                        if len(sales_team_id) > 0:  
                            print 'SECTION ID: ' + str(sales_team_id)
                            res_partner_obj.write(cr, uid, created_customer_id, {'section_id':str(sales_team_id[0])})
                        # personal account for each customer
                        # Check if account exists for certain customer
                        account_id = account_account_obj.search(cr, uid, [('name', '=', final_customer_name)])
                        if not account_id:
                            print 'Creating and linking new account for customer ' + final_customer_name + ' CODE: ' + str(row[10])

                            # Creating account (parent_id can be empty. If so, do not change the parent_id field; let it be)
                            account_id = account_account_obj.create(cr, uid, {'code': row[10],
                                                                            'name': final_customer_name,
                                                                            'type':'receivable',
                                                                            'user_type': account_type_id[0],
                                                                            'parent_id': forderungen_account
                                                                            })


                        # In any case assign the account to the customer
                        if type(account_id) is list:
                            account_id = account_id[0]
                        print 'ACCOUNT ID TO LINK' + str(account_id)
                        res_partner_obj.write(cr, uid, created_customer_id, {'property_account_receivable':account_id})
                    except Exception, e:
                        #pass
                        print '===> ERROR ' + str(e)  # If error on account creation, no problem keep going

                    for log_entry in log_entries:
                        log = log_entry.split('||||', 2)
                        crm_phonecall_obj.create(cr, uid, {'date':log[0], 'name':log[1], 'state':'done', 'partner_id':created_customer_id})



                if time.time() - start_time > 500:
                    print '500 secs passed'
                    return None                

                row_counter += 1
                #return None        

0 个答案:

没有答案