Expected singleton: res.users() when create new invoice programmatically - invoice

I'm trying to create a new invoice in controller:
request.env['account.move'].sudo().create([
{
'move_type': 'out_invoice',
'journal_id': 1,
'invoice_date': '2020-01-10',
'invoice_line_ids': [(0, 0, {'product_id': 21, 'price_unit': 1000.0})],
}])
But i got this error
Expected singleton: res.users()
Please help, thanks.

I've found the answer with the help of my leader, here it is:
This issue is because we missing self.env.user, to do this, we must add .with_user(SUPERUSER_ID) after model call.
Example:
from odoo import SUPERUSER_ID
request.env['account.move'].with_user(SUPERUSER_ID).sudo().create([
{
'move_type': 'out_invoice',
'journal_id': 1,
'invoice_date': '2020-01-10',
'invoice_line_ids': [(0, 0, {'product_id': 21, 'price_unit': 1000.0})],
}])

Related

Find matching row(s) in other range

I'm trying to create a column containing a category based on a set of categories and a match/regex string from another range. My example is:
I can't find any answered questions here which explain to me how this can be done, if it can. So am asking here if anyone can help or point me toward an answer I've probably missed!
Neither the category or data ranges will be a static number of rows. I'd like to have a formula which simply produces something like this:
Any help very much appreciated!
try:
=ARRAYFORMULA(IFNA(REGEXEXTRACT(E2:E, "["&TEXTJOIN( , 1, B2:B)&"]")))
update 1:
=INDEX(IFNA(REGEXEXTRACT(E2:E, "["&TEXTJOIN( , 1,
REGEXREPLACE(C2:C, "[\.\*]", ))&"]")))
or:
=INDEX(IFNA(VLOOKUP(IFNA(REGEXEXTRACT(E2:E, "["&TEXTJOIN( , 1,
REGEXREPLACE(C2:C, "[\.\*]", ))&"]")), {
REGEXREPLACE(C2:C, "[\.\*]", ), B2:B}, 2, 0)))
update 2
first of all, learn your regex: https://github.com/google/re2/wiki/Syntax
change your C column to:
.*(A).*
.*(B).*
.*(C).*
then use:
=ARRAYFORMULA(IFNA(VLOOKUP(TRIM(FLATTEN(QUERY(TRANSPOSE(
IFNA(REGEXEXTRACT(E3:E, TEXTJOIN("|", 1, C3:C)))),,9^9))), {
IFNA(REGEXEXTRACT(C3:C, TEXTJOIN("|", 1, TRIM(FLATTEN(QUERY(TRANSPOSE(
IFNA(REGEXEXTRACT(E3:E, TEXTJOIN("|", 1, C3:C)))),,9^9)))))), B3:B}, 2, 0)))
or shorter:
=INDEX(IFNA(VLOOKUP(
IFNA(REGEXEXTRACT(E3:E, "["&TEXTJOIN("|", 1, C3:C)&"]")), {
IFNA(REGEXEXTRACT(C3:C, TEXTJOIN("|", 1,
IFNA(REGEXEXTRACT(E3:E, "["&TEXTJOIN("|", 1, C3:C)&"]"))))), B3:B}, 2, 0)))

Filter Active Record by relation but not the relation itself

I would like to filter a model with a has_many relation by a value of it's relation.
This is easily achievable by using Foo.include(:bars).where(bars: { foobar: 2 }).
If we had an object like this in our database:
{
id: 1,
bars: [
{ id: 4, foobar: 1 },
{ id: 5, foobar: 2 }
]
}
This query would only return this:
{
id: 1,
bars: [
{ id: 5, foobar: 2 }
]
}
I would like to still filter my records by this relation, but receive all records in the relation.
I was able to achieve this by using something like: Foo.include(:bars).joins('LEFT JOIN bars AS filtered_bars ON filtered_bars.foo_id = foos.id').where(filtered_bars: { foobar: 2 }) but this does not look very pretty.
Is there a better way to do this?
This is the magic of includes in action - since you are using bar in where, it decides to use a left join and will not preload anything else. You have to explicitly join and prelaod the association here:
Foo.joins(:bars).where(bars: {foobar: 2}).uniq.preload(:bars)
Some time ago I made a talk on those magic methods, you can find it here: https://skillsmatter.com/skillscasts/6731-activerecord-vs-n-1

How to structure falcor router to get all available IDs?

I'm experimenting with using Falcor to front the Guild Wars 2 API and want to use it to show game item details. I'm especially interested in building a router that can use multiple datasources to combine the results of different APIs.
The catch is, Item IDs in Guild Wars 2 aren't contiguous. Here's an example:
[
1,
2,
6,
11,
24,
56,
...
]
So I can't just write paths on the client like items[100..120].name because there's almost certainly going to be a bunch of holes in that list.
I've tried adding a route to my router so I can just request items, but that sends it into an infinite loop on the client. You can see that attempt on GitHub.
Any pointers on the correct way to structure this? As I think about it more maybe I want item.id instead?
You shouldn't find your self asking for ids from a Falcor JSON Graph object.
It seems like you want to build an array of game ids:
{
games: [
{ $type: "ref", value: ["gamesById", 352] },
{ $type: "ref", value: ["gamesById", 428] }
// ...
],
gamesById: {
352: {
gameProp1: ...,
},
428: {
gameProp2: ...
}
}
}
[games, {from: 5, to: 17 }, "gameProp1"]
Does that work?
You can use 'get' API of Falcor, It retrieves multiple values.. You can pass any number of required properties as shown below
var model=new falcor.Model({
cache:{
genereList:[
{name:"Recently Watched",
titles:[
{id:123,
name: "Ignatius",
rating: 4}
]
},
{name:"New Release",
titles:[
{id:124,
name: "Jessy",
rating: 3}
]
}
]
}
});
Getting single value
model.getValue('genereList[0].titles[0].name').
then(function(value){
console.log(value);
});
Getting multiple values
model.get('genereList[0..1].titles[0].name', 'genereList[0..1].titles[0].rating').
then(function(json){
console.log(JSON.stringify(json, null, 4));
})

Remove method of the List is not working

I am creating a session variable in one controller-:
List<Employee> emp = new ArrayList<Employee>()
emp = Employee.findAllByLevel(proj_lev.level_no)
session.employee_list = emp
render(view: "add_members", model: [proj_lev:proj_lev , proj_lev_right:proj_lev_right , employee:session.employee_list])
In another controller I am accessing the session variable and using the remove method of the List but the list is not changing-:
render "${session.employee_list}"
def emp_added = Employee.get(params.int('employee_id'))
session.employee_list.remove(emp_added)
render "${session.employee_list}"
The view in the GSP is-:
[tearp.Employee : 2, tearp.Employee : 5, tearp.Employee : 8, tearp.Employee : 9, tearp.Employee : 10][tearp.Employee : 2, tearp.Employee : 5, tearp.Employee : 8, tearp.Employee : 9, tearp.Employee : 10]
make a list of employee to be removed , and then remove this list from "added employees" and add this list to "all employees"
Have you override equals method on Employee domain object? I think that is the problem.
When you say this ( with out overriding the equals method):
session.employee_list.remove(emp_added)
It will first search the object in the list by its hashcode and every time you fetch an object from db using Employee.get(..) hibernate creates new object even for the same record, so every new object has new hashcode and list.remove() method wont work as far as the object is not found on the list.
Possible Solution:
add this method on your Employee domain class:
#Overrid
boolean equals(Employee employee){
return this.id == employee?.id
}

MagicalRecord keypath

I am using magicalrecord and have my properties mapped to the json details.slots.
My JSON looks like this
"details": {
"startTimestamp": "2014-01-13",
"endTimestamp": "2014-01-16",
"employeeId" : 176,
"slots": [
{
"numberOfAppointments": 0,
"numberOfSpots": 1,
"isReserved": 0,
"startTimestamp": "2014-01-13 08:00:00",
"endTimestamp": "2014-01-13 08:05:00"
},
{
"numberOfAppointments": 0,
"numberOfSpots": 1,
"isReserved": 0,
"startTimestamp": "2014-01-13 08:05:00",
"endTimestamp": "2014-01-13 08:10:00"
},
{
"numberOfAppointments": 0,
"numberOfSpots": 1,
"isReserved": 0,
"startTimestamp": "2014-01-13 08:10:00",
"endTimestamp": "2014-01-13 08:15:00"
},
....
I was wondering is there an easy way to map a field to a field up a level? IE I can use my.object.property to drill down but is there anyway if my import is importing the slots array to easily add the employeeId to each object? Slots can be as little as 600 records to up to a couple thousand.
The only solution I can think of is looping through every slot and manually adding it before I run the magical record import. Is anyone familiar with another, easier way to get this done?
One way you could achieve this with the current framework is to implement the method
- (BOOL) importDetails:(id)data;
In your entity. MagicalRecord importing will look for this method and call it based on your core data property name if it's implemented. This would mean that you're basically rewriting the import code for the entire array, but you'd have access to the data you're after. Not ideal, but still possible.

Resources