예제 #1
0
def update_coa(ctx):
    company_id = ctx['company_id']
    model = 'account.account'
    if len(clodoo.searchL8(ctx, model,
                           [('company_id', '=', company_id)])) < 100:
        return
    company = clodoo.browseL8(ctx, 'res.company', company_id)
    print "- Processing company %s" % company.name
    for code in ('__', 'A', 'P', 'R', 'S'):
        ids = clodoo.searchL8(ctx, model, [('code', '=', code),
                                           ('company_id', '=', company_id)])
        if len(ids) > 2:
            print "Warning: invalid account '%s'" % code
            sys.exit(1)
        elif ids:
            vals = get_code_values(ctx, code)
            try:
                clodoo.writeL8(ctx, model, ids[0], vals)
            except BaseException:
                pass
        else:
            vals = get_code_values(ctx, code)
            clodoo.createL8(ctx, model, vals)
    aprs = {}
    for code in ('__', 'A', 'P', 'R', 'S'):
        ids = clodoo.searchL8(ctx, model, [('code', '=', code),
                                           ('company_id', '=', company_id)])
        aprs[code] = ids[0]
    root_id = get_company_account(ctx)
    account_ids = clodoo.searchL8(ctx, model,
                                  [('type', '=', 'view'),
                                   ('parent_id', '=', root_id),
                                   ('id', 'not in', aprs.values())])
    for account_id in account_ids:
        ids = clodoo.searchL8(ctx, model, [('parent_id', '=', account_id)])
        if ids:
            account = clodoo.browseL8(ctx, model, ids[0])
            while account.type == 'view':
                ids = clodoo.searchL8(ctx, model, [('parent_id', '=', ids[0])])
                if ids:
                    account = clodoo.browseL8(ctx, model, ids[0])
                else:
                    account.type = 'asset'
            if account.user_type.code in ('asset', 'bank',
                                          'cash', 'receivable'):
                parent_id = aprs['A']
            elif account.user_type.code in ('equity', 'liability',
                                            'payable', 'tax'):
                parent_id = aprs['P']
            elif account.user_type.code == 'income':
                parent_id = aprs['R']
            elif account.user_type.code == 'expense':
                parent_id = aprs['S']
            else:
                parent_id = False
            if parent_id:
                vals = {}
                vals['parent_id'] = parent_id
                clodoo.writeL8(ctx, model, account_id, vals)
예제 #2
0
 def drop_out_original_field(ctx, model, id, vals, name):
     do_ids = clodoo.searchL8(ctx, model, [(name, '=', vals[name])])
     if do_ids:
         for do_id in do_ids:
             if ctx['_cr']:
                 table = model.replace('.', '_')
                 sql = "update %s set %s='(id=%d) =>%d' where id=%d;" % (
                     table, name, do_id, id, do_id)
                 try:
                     ctx['_cr'].execute(sql)
                 except BaseException:
                     pass
             else:
                 try:
                     clodoo.writeL8(ctx, model, do_ids,
                                    {name: '(id=%d) =>%d' % (do_id, id)})
                 except BaseException:
                     pass
예제 #3
0
def write_no_dup(ctx, model, ids, vals, rec_id):
    try_again = False
    try:
        clodoo.writeL8(ctx, model, ids, vals)
    except BaseException:
        try_again = True
        drop_out_originals(ctx, model, ids[0], vals)
    if try_again:
        try:
            try_again = False
            clodoo.writeL8(ctx, model, ids, vals)
        except BaseException:
            try_again, vals = set_tmp_keys(ctx, model, ids[0], vals)
    if try_again:
        try:
            try_again = False
            clodoo.writeL8(ctx, model, ids, vals)
        except BaseException:
            print 'Error writing record %d of %s' % (rec_id, model)
            manage_error()
예제 #4
0
def add_elem(row, ctx, MYDICT):
    def cvt_val(ctx, field_name, value):
        x = 'trx_%s' % field_name
        if x in ctx:
            trx_dict = ctx[x]
            if value in trx_dict:
                value = trx_dict[value]
            else:
                value = value.strip()
        elif field_name == 'company_id':
            value = get_company_id(ctx)
        else:
            value = value.strip()
        return value

    def add_val(row, ctx, MYDICT, field_name):
        def_field_name = 'default_%s' % field_name
        if field_name in MYDICT:
            idx = MYDICT[field_name]
            if idx >= len(row):
                print 'Invalid translation field %d' % idx
                return
            vals[field_name] = cvt_val(ctx, field_name, row[idx])
        elif def_field_name in ctx:
            vals[field_name] = ctx[def_field_name]

    msg_burst('Reading %s ...' % row[MYDICT['name']])
    model = 'product.product'
    vals = {}
    add_val(row, ctx, MYDICT, 'name')
    for field_id in clodoo.searchL8(ctx, 'ir.model.fields',
                                    [('model', '=', model)]):
        field_name = clodoo.browseL8(ctx, 'ir.model.fields', field_id).name
        add_val(row, ctx, MYDICT, field_name)

    if 'name' not in vals:
        vals['name'] = 'PRODUCT'
    if 'company_id' not in vals:
        vals['company_id'] = get_company_id(ctx)

    ids = []
    if vals.get('code'):
        ids = clodoo.searchL8(ctx, model,
                              [('default_code', '=', vals['code'])])
    if not ids and vals.get('default_code'):
        ids = clodoo.searchL8(ctx, model,
                              [('default_code', '=', vals['default_code'])])
    if not ids:
        ids = clodoo.searchL8(ctx, model, [('name', '=', vals['name'])])
    if ids:
        product_id = ids[0]
        try:
            clodoo.writeL8(ctx, model, [product_id], vals)
        except BaseException:
            write_log("Cannot import %s" % vals['name'])
            try:
                clodoo.writeL8(ctx, model, [product_id], vals)
            except BaseException:
                pass
    else:
        try:
            product_id = clodoo.createL8(ctx, model, vals)
        except BaseException:
            write_log("Cannot create %s" % vals['name'])
            try:
                product_id = clodoo.createL8(ctx, model, vals)
            except BaseException:
                pass
예제 #5
0
def add_elem(row, ctx, MYDICT_C, MYDICT_S):
    def add_val(row, ctx, MYDICT, field_name):
        def_field_name = 'default_%s' % field_name
        if field_name in MYDICT:
            idx = MYDICT[field_name]
            if idx >= len(row):
                print 'Invalid translation field %d' % idx
                # continue
            vals[field_name] = cvt_val(ctx, field_name, row[idx])
            # if field_name == 'state_id':
            #     if row[idx]:
            #         state_id = row[idx]
        elif def_field_name in ctx:
            vals[field_name] = ctx[def_field_name]

    if ctx['customers']:
        MYDICT = MYDICT_C
    else:
        MYDICT = MYDICT_S
    msg_burst('Reading %s ...' % row[MYDICT['name']])
    ids = clodoo.searchL8(ctx, 'res.country',
                          [('code', '=', 'IT')])
    if ids:
        country_IT = ids[0]
    model = 'res.partner'
    vals = {}
    country_code = False
    state_id = False
    for field_id in clodoo.searchL8(ctx, 'ir.model.fields',
                                    [('model', '=', model)]):
        field_name = clodoo.browseL8(ctx, 'ir.model.fields', field_id).name
        add_val(row, ctx, MYDICT, field_name)

    if 'is_company' not in vals:
        vals['is_company'] = True
    if 'name' not in vals:
        vals['name'] = 'UNKNOWN'
    elif 'name2' in MYDICT:
        vals['name'] = '%s %s' % (vals['name'], row[MYDICT['name2']])
        vals['name'] = vals['name'].strip()
    if 'company_id' not in vals:
        vals['company_id'] = get_company_id(ctx)
    if 'country_id' not in vals:
        vals['country_id'] = country_IT
        country_code = 'IT'
    if state_id:
        if ctx.get('IN_state_id') == 'code':
            ids = clodoo.searchL8(ctx, 'res.country.state',
                                  [('country_id', '=', vals['country_id']),
                                   ('code', '=', state_id.upper())])
        else:
            ids = clodoo.searchL8(ctx, 'res.country.state',
                                  [('country_id', '=', vals['country_id']),
                                   ('name', 'ilike', state_id)])
        if ids:
            vals['state_id'] = ids[0]
    if vals.get('vat'):
        if country_code and country_code != vals['vat'][0:2]:
            if country_code == 'IT' and vals['vat'].isdigit():
                vals['vat'] = 'IT%011d' % int(vals['vat'])
            else:
                vals['vat'] = country_code + vals['vat']

    if country_code in ('AT', 'BE', 'BG', 'CY', 'HR', 'DK',
                        'EE', 'FI', 'FR', 'DE', 'GB', 'EL', 'IE',
                        'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT',
                        'CZ', 'SK', 'RO', 'SI', 'ES', 'SE', 'HU'):
        fiscal = 'Regime%UE'
    elif country_code and country_code != 'IT':
        fiscal = 'Extra%UE'
    else:
        fiscal = 'Italia'
    ids = clodoo.searchL8(ctx, 'account.fiscal.position',
                          [('company_id', '=', vals['company_id']),
                           ('name', 'ilike', fiscal)])
    if ids:
        vals['propert_account_position'] = ids[0]

    ids = []
    if vals.get('old_customer_id'):
        ids = clodoo.searchL8(ctx, model, [('old_customer_id',
                                            '=',
                                            vals['old_customer_id'])])
    if not ids and vals.get('old_supplier_id'):
        ids = clodoo.searchL8(ctx, model, [('old_supplier_id',
                                            '=',
                                            vals['old_supplier_id'])])
    if not ids and vals.get('vat'):
        ids = clodoo.searchL8(ctx, model, [('vat', '=', vals['vat'])])
    if not ids:
        ids = clodoo.searchL8(ctx, model, [('name', '=', vals['name'])])
    if not ids:
        vals['customer'] = False
        vals['supplier'] = False
    if ctx['customers']:
        vals['customer'] = True
    if ctx['suppliers']:
        vals['supplier'] = True
    if ids:
        partner_id = ids[0]
        try:
            clodoo.writeL8(ctx, model, [partner_id], vals)
        except BaseException:
            write_log("Cannot import %s" % vals['name'])
            if vals.get('vat'):
                del vals['vat']
            try:
                clodoo.writeL8(ctx, model, [partner_id], vals)
            except BaseException:
                pass
    else:
        try:
            partner_id = clodoo.createL8(ctx, model, vals)
        except BaseException:
            write_log("Cannot create %s" % vals['name'])
            if vals.get('vat'):
                del vals['vat']
            try:
                partner_id = clodoo.createL8(ctx, model, vals)
            except BaseException:
                pass
예제 #6
0
def upd_invoice(ctx, tmp_num=False, cur_num=False, cur_dt=False):
    move_name = get_name_by_ver(ctx, 'move_name')
    if not tmp_num and not cur_num:
        print ">> Missing parameters"
        return
    company_id = ctx['company_id']
    tmp_inv_id = False
    cur_inv_id = False
    if tmp_num:
        inv = clodoo.searchL8(ctx,
                              'account.invoice',
                              [('company_id', '=', company_id),
                               (move_name, '=', tmp_num)])
        if len(inv):
            tmp_inv_id = inv[0]
        else:
            return
        print ">>> tmp_inv=%d" % tmp_inv_id
    if cur_num:
        inv = clodoo.search(ctx,
                            'account.invoice',
                            [('company_id', '=', company_id),
                             (move_name, '=', cur_num)])
        if len(inv):
            cur_inv_id = inv[0]
        else:
            return
        print ">>> cur_inv=%d" % cur_inv_id
    if not tmp_inv_id and not cur_inv_id:
        print ">> No invoice found ", tmp_num, cur_num
        return
    if tmp_inv_id:
        rec_ids = [tmp_inv_id]
        tag = tmp_num
    else:
        rec_ids = [cur_inv_id]
        tag = cur_num
    print ">> Get info tmp invoice %s (%s)" % (str(rec_ids), tag)
    reconcile_dict, move_dict = clodoo.get_reconcile_from_invoices(
        rec_ids, ctx)
    print ">> Unreconcile tmp invoice"
    clodoo.unreconcile_invoices(reconcile_dict, ctx)
    if tmp_inv_id and cur_inv_id and tmp_inv_id != cur_inv_id:
        print ">> Delete tmp invoice"
        try:
            clodoo.writeL8(ctx,
                           'account.invoice',
                           tmp_inv_id,
                           {'state': 'cancel',
                            'number': '', move_name: ''})
        except BaseException:                                # pragma: no cover
            pass
        clodoo.unlinkL8(ctx, 'account.invoice', [tmp_inv_id])
    if cur_inv_id:
        rec_ids = [cur_inv_id]
        tag = cur_num
    else:
        rec_ids = [tmp_inv_id]
        tag = tmp_num
    if cur_inv_id and tmp_inv_id and tmp_inv_id != cur_inv_id:
        print ">> Get info cur invoice %s (%s)" % (str(rec_ids), tag)
        reconcile_dict, move_dict = clodoo.get_reconcile_from_invoices(
            rec_ids, ctx)
        print ">> Unreconcile cur invoices"
        clodoo.unreconcile_invoices(reconcile_dict, ctx)
    print ">> Draft cur invoices"
    clodoo.upd_invoices_2_draft(move_dict, ctx)
    inv_id = rec_ids[0]
    vals = {}
    if cur_dt:
        dt_s = str(cur_dt)
        period_ids = clodoo.executeL8(ctx, 'account.period', 'find', dt_s)
        period_id = period_ids and period_ids[0] or False
        vals['date_invoice'] = dt_s
        vals['registration_date'] = dt_s
        vals['period_id'] = period_id
    if cur_num and tmp_num and tmp_num != cur_num:
        vals[move_name] = tmp_num
    if len(vals):
        print ">> Update values ", inv_id, vals
        clodoo.writeL8(ctx, 'account.invoice', inv_id, vals)
    print ">> Posted"
    clodoo.upd_invoices_2_posted(move_dict, ctx)
    reconciles = reconcile_dict[inv_id]
    if len(reconciles):
        print ">> Reconcile "
        cur_reconciles, cur_reconcile_dict = clodoo.refresh_reconcile_from_inv(
            inv_id, reconciles, ctx)
        clodoo.reconcile_invoices(cur_reconcile_dict, ctx)
    return
예제 #7
0
                                                           reconcile_dict,
                                                           move_dict))
                fd.close()
                print ">> Unreconcile cur invoices"
                clodoo.unreconcile_invoices(reconcile_dict, ctx)
                print ">> Draft cur invoices"
                clodoo.upd_invoices_2_draft(move_dict, ctx)
    if action[1] == '?':
        continue

    if action[0] == 'C':
        print ">> Cancel invoice number"
        for inv_id in rec_ids:
            print "Cancelling Invoice number of %d" % inv_id
        clodoo.writeL8(ctx,
                       'account.invoice',
                       rec_ids,
                       {move_name: ''})
    elif action[0] == 'N':
        for inv_id in rec_ids:
            print_invoice_info(inv_id)
            number = raw_input('New invoice number? ')
            clodoo.writeL8(ctx, MODEL['IH'], [inv_id],
                           {move_name: number})
    elif action[0] == 'R' and action != 'RB':
        for inv_id in rec_ids:
            print_invoice_info(inv_id)
            ids = get_ids_from_params(MODEL['ID'],
                                      company_id,
                                      P1=('invoice_id', '=', inv_id)
                                      )
            clodoo.writeL8(ctx, MODEL['ID'], ids,