Intechnity-com / OdooJsonRpcClient

Odoo Client Json Rpc
MIT License
70 stars 33 forks source link

Account.Move.Line insertion failed #1

Closed KeepCar closed 3 years ago

KeepCar commented 3 years ago

I try to insert data in Account.Move.Line but I have : Cannot create unbalanced journal entry. Ids: [68]\nDifferences debit - credit: [-199.5]

So that the balance of accounting entries is balanced. You must insert 3 lines at the same time.

Or find a way to temporarily deactivate the balance control.

Do you have a solution ?

patricoos commented 3 years ago

it's a problem with validation in Odoo. Try add JournalId to Create Invoice model. That's working example. BTW U can add invoice in one call with lines:

`
var journalResult = await _journalOdooRepository.GetSaleTypeByCompanyIdAsync(odooCompanyId.Value); if (journalResult.Failed || journalResult.Value.Length == 0) return Result.FailedResult($"Journal with id: '{companyId}' has no sale journal");

        var journal = journalResult.Value.First();

        Result<CreateOrUpdateInvoiceLineOdoo>[] invoiceLineIdsResults;
        invoiceLineIdsResults = invoiceLines.Select(x =>
        {
            var productId = _odooMappingRepository.GetOdooArticleId(x.ArticleId);
            if (!productId.HasValue)
                return Result<CreateOrUpdateInvoiceLineOdoo>.FailedResult($"Article with id: '{x.ArticleId}' does't exist in Odoo");

            var ilvoiceLine = new CreateOrUpdateInvoiceLineOdoo
            {
                ProductId = productId.GetValueOrDefault(),
                AccountId = journal.DefaultAccountId,
                CurrencyId = 1,
                Quantity = x.Quantity,
                PriceUnit = x.Price,
            };
            return Result<CreateOrUpdateInvoiceLineOdoo>.SucceedResult(ilvoiceLine);
        }
        ).ToArray();

        if (invoiceLineIdsResults.Any(x => x.Failed))
            return Result<long>.FailedResult(string.Join("; ", invoiceLineIdsResults.Where(x => x.Failed).Select(x => x.Message)));

        var model = new CreateOrUpdateInvoiceOdoo
        {
            InvoiceDate = invoicDate.ToString(ODOO_DATE_FORMAT),
            InvoiceDateDue = invoicDate.AddDays(INVOICE_DATE_DUE_DAYS).ToString(ODOO_DATE_FORMAT),
            Date = invoicDate.ToString(ODOO_DATE_FORMAT),
            CurrencyId = 1,
            CompanyId = odooCompanyId.Value,
            ExtractState = "done",
            JournalId = journal.Id,
            MoveType = "out_invoice",
            Narration = comment,
            PartnerId = odooCustoremId.Value,
            State = "draft",
            InvoiceLineIds = invoiceLineIdsResults.Select(x => x.Value).ToArray()
        };

        var result = await _invoiceOdooRepository.CreateAsync(model);

`

`
public class JournalOdooRepository : OdooRepository { public JournalOdooRepository(IOptions licensesSettings) : base(new
OdooConfig(licensesSettings.Value.OdooUrl, licensesSettings.Value.OdooDbName, licensesSettings.Value.OdooUserName, licensesSettings.Value.OdooPassword)) { }

    public async Task<OdooResult<JournalOdooEntity[]>> GetSaleTypeByCompanyIdAsync(int odooCompanyId)
    {
        return await Query()
            .Where(x => x.CompanyId, OdooOperator.EqualsTo, odooCompanyId)
            .Where(x => x.Type, OdooOperator.EqualsTo, "sale")
            .ToListAsync();
    }
}

`

KeepCar commented 3 years ago

Thank you for quick answer :)

Please, Can you send me code of classes: CreateOrUpdateInvoiceLineOdoo, CreateOrUpdateInvoiceOdoo and JournalOdooEntity

patricoos commented 3 years ago

Journal U can take from "account.journal". CreateOrUpdate models have only this property that U can see in example that I send. They are based on "account.move.line", "account.move"

` [OdooTableName("account.move")] [JsonConverter(typeof(OdooModelConverter))] public class CreateOrUpdateInvoiceOdoo : IOdooCreateModel { [JsonProperty("currency_id")] public int CurrencyId { get; set; }

    [JsonProperty("date")]
    public string Date { get; set; }

    [JsonProperty("invoice_date")]
    public string InvoiceDate { get; set; }

    [JsonProperty("invoice_date_due")]
    public string InvoiceDateDue { get; set; }

    [JsonProperty("extract_state")]
    public string ExtractState { get; set; }

    [JsonProperty("journal_id")]
    public long JournalId { get; set; }

    [JsonProperty("move_type")]
    public string MoveType { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }
    [JsonProperty("partner_id")]
    public int PartnerId { get; set; }

    [JsonProperty("company_id")]
    public int CompanyId { get; set; }

    [JsonProperty("invoice_line_ids")]
    public CreateOrUpdateInvoiceLineOdoo[] InvoiceLineIds { get; set; }

    [JsonProperty("narration")]
    public string Narration { get; set; }
}

[OdooTableName("account.move.line")] [JsonConverter(typeof(OdooModelConverter))] public class CreateOrUpdateInvoiceLineOdoo : IOdooCreateModel { [JsonProperty("product_id")] public int ProductId { get; set; }

    [JsonProperty("quantity")]
    public double Quantity { get; set; }

    [JsonProperty("price_unit")]
    public double PriceUnit { get; set; }

    [JsonProperty("account_id")]
    public int AccountId { get; set; }

    [JsonProperty("currency_id")]
    public int CurrencyId { get; set; }
}

`