Intuit integration.
With create invoice. I am needing to get items from a loop when I create the invoice.
I also tried by creating the loop first and using $items in the invoice create but also get an error. "Format Error. Expect an element of an array or an object."
Not liking my loop. How can I accomplish this
?
$theResourceObj = Invoice::create([
"TotalAmt" => 200.00,
"Line" => [
$queryitems = 'SELECT * FROM mydatabse',
$query_items = mysqli_query($con,$queryitems),
while($row = mysqli_fetch_array($query_items)) {
[
"Description"=> "Loop Description",
"DetailType"=> "SalesItemLineDetail",
"SalesItemLineDetail"=> [
"TaxCodeRef"=> [
"value"=> "TAX"
],
"Qty"=> 1,
"UnitPrice"=> 200,
"ItemRef"=> [
"name"=> "Loop Name",
"value"=> "5"
]
],
"LineNum"=> 1,
"Amount"=> 200.00,
"Id"=> "1"
],
}
],
"CustomerRef"=> [
"value"=> 1
],
"BillEmail" => [
"Address" => "email#somewhere.com"
]
]);
You can't put loops inside array literals.
Construct the array incrementally outside the call to Invoice::create().
$invoice = [
"TotalAmt" => 200.00,
"CustomerRef"=> [
"value"=> 1
],
"BillEmail" => [
"Address" => "email#somewhere.com"
]
];
$lines = [];
$queryitems = 'SELECT * FROM mydatabse';
$query_items = mysqli_query($con,$queryitems);
while($row = mysqli_fetch_array($query_items)) {
$lines[] = [
"Description"=> "Loop Description",
"DetailType"=> "SalesItemLineDetail",
"SalesItemLineDetail"=> [
"TaxCodeRef"=> [
"value"=> "TAX"
],
"Qty"=> 1,
"UnitPrice"=> 200,
"ItemRef"=> [
"name"=> "Loop Name",
"value"=> "5"
]
],
"LineNum"=> 1,
"Amount"=> 200.00,
"Id"=> "1"
];
}
$invoice['Line'] = $lines;
$theResourceObj = Invoice::create($invoice);
Related
I have two variables like the following
$items1 =
[
[
"p_id" => 1000,
"value"=> 25
],
[
"p_id" => 2000,
"value"=> 15
],
[
"p_id" => 3000,
"value"=> 23
],
];
$items2 =
[
[
"p_id" => 1000,
"value"=> 5
],
[
"p_id" => 4000,
"value"=> 12
],
];
For a logging system, I want variable $items1 to be a reference and find its changes to the $items2 variable and finally have the following output:
$deleted = [
[
"p_id" => 2000,
"value"=> 15
]
];
$added = [
[
"p_id" => 4000,
"value"=> 12
],
];
$changed = [
[
"p_id" => 1000,
"value"=> 5
],
];
For a logging system may be a better suggested format, thank you for your help
working example
$items1 =
[
[
"p_id" => 1000,
"value"=> 25
],
[
"p_id" => 2000,
"value"=> 15
],
[
"p_id" => 3000,
"value"=> 23
],
];
$items2 =
[
[
"p_id" => 1000,
"value"=> 5
],
[
"p_id" => 4000,
"value"=> 12
],
];
function keyedByPidArray($items) {
$arr = [];
foreach($items as $item) {
$arr[$item['p_id']] = $item;
}
return $arr;
}
function changedItems($items1, $items2) {
$changed = [];
foreach($items1 as $item1) {
foreach($items2 as $k => $item2) {
if(($item1["p_id"] === $item2["p_id"]) && ($item1["value"] !== $item2["value"])) {
$changed[] = $item2;
unset($items2[$k]);
break;
}
}
}
return $changed;
}
$keyedByPidArr1 = keyedByPidArray($items1);
$keyedByPidArr2 = keyedByPidArray($items2);
$pids1 = array_column($items1, 'p_id');
$pids2 = array_column($items2, 'p_id');
$added = array_intersect_key($keyedByPidArr2, array_flip(array_diff($pids2, $pids1)));
$deleted = array_intersect_key($keyedByPidArr1, array_flip(array_diff($pids1, $pids2)));
$changed = changedItems($items1, $items2);
print_r($added);
print_r($deleted);
print_r($changed);
I am trying to modify single field value, but whenever I do it it erases all other fields leaving updated field only, how can I modify a single field witouth affecting the other ones?
so far this is what I'm doing:
//
$updateResult2 = $coll_rutas->updateOne(
/* LIMIT TO ROUTE ID */
[
"_id" => new \MongoDB\BSON\ObjectId($ruta_id)
],
/* UPDATE VALUE */
[
'$set' => [
'destinos.$[d]' => [
"horas" => "1",
"minutos" => "15",
"millas" => "150",
"orden" => "1",
"notas" => "demo notes",
],
],
],
/* WHERE FILTER */
[
"upsert" => true,
'arrayFilters' => [
['d.ciudad_id' => 47]
]
]
);
//var_dump($updateResult2); exit;
This is the original mongo Object:
"destinos": [
{
"ciudad_id": "47",
"nombre_destino": "Salt Lake City, Utah",
"horas": "0",
"minutos": "0",
"millas": "0",
"orden": 1,
"notas": null,
"origenes": [
{
"ciudad_id": "45",
"nombre_origen": "Provo, Utah",
"orden": 1,
"notas": null,
"base_price": 0
}
]
}
],
With my changes it erases origenes array, ciudad_id and nombre destino, leaving it this way:
"destinos": [
{
"horas": "1",
"minutos": "15",
"millas": "150",
"orden": "1",
"notas": "demo notes"
]
}
],
With this code:
'$set' => [
'destinos.$[d]' => [
"horas" => "1",
"minutos" => "15",
"millas" => "150",
"orden" => "1",
"notas" => "demo notes",
],
]
you are targeting the destinos.$[d] element and replacing all of its contents with the new data structure.
You need to use the .(dot) notation to target each element in the nested array. It's a bit verbose but this would work:
'$set' => [
'destinos.$[d].horas' => "1",
'destinos.$[d].minutos' => "15",
'destinos.$[d].millas' => "150",
'destinos.$[d].orden' => "1",
'destinos.$[d].notas' => "demo notes"
]
i have the created the following object array in php
$treeData[] = (object) array(
"name"=> "A",
"children" => [
[
"name"=> "A1",
"children"=> [
[
"name"=> "A1.1",
"children"=> [
[
"name"=> "A1.1.1",
"children"=> [
....
I'm trying to push new values inside the children of A1.1.1 based on below condition:
foreach ($treeData as $value) {
if ($value->name == 'A') {
$value->name[][] = (object) array(
"name"=> "ChildA",
"children"=> ""
);
break;
}
}
But it's giving me an error
Expected result should match as below example:
$treeData[] = (object) array(
"name"=> "A",
"children" => [
[
"name"=> "A1",
"children"=> [ [
"name"=> "A1.1",
"children"=> [ [
"name"=> "A1.1.1",
"children"=> [
[
"name"=> "ChildA",
"children"=> [ [
"name"=> "ChildA1"
] ]
],
[
"name"=> "ChildA",
"children"=> [ [
"name"=> "ChildA2"
] ]
],
]
] ]
] ]
]
]);
What I'm doing wrong here or any way to achieve this in different approach
The error I'm getting:
"Fatal error: Uncaught Error: [] operator not supported for strings "
First of all you getting the error as you do: $value->name[][]. Notice the name is a string so you ccannot use [] (array append operator) on it.
I would have take the recursive approach if I were you. Consider the following pseudo code:
function addChild($root, $addToName, $nameToAdd) {
if ($root->name == $addToName)
$root->children[] = (object) array("name"=> $nameToAdd, "children"=> []);
else
foreach($root->children as $child)
addChild($child, $addToName, $nameToAdd);
}
And now call is with: addChild($treeData, 'A', "ChildA")
I have tried implementing completion suggestor query in php as given here. My code is :
$params = [
"index" => $myIndex,
"body" => [
"try" => [
"text" => "ram",
"completion" => [ "value" => "suggest"]
]
]
];
$response = $client->suggest($params);
I have done indexing like this:
$params = [
"index" => $myIndex,
"body" => [
"settings"=> [
"analysis"=> [
"analyzer"=> [
"start_with_analyzer"=> [
"tokenizer"=> "my_edge_ngram",
"filter"=> [
"lowercase"
]
]
],
"tokenizer"=> [
"my_edge_ngram"=> [
"type"=> "edge_ngram",
"min_gram"=> 3,
"max_gram"=> 15
]
]
]
],
"mappings"=> [
"doc"=> [
"properties"=> [
"label"=> [
"type"=> "text",
"fields"=> [
"keyword"=> [
"type"=> "keyword"
],
"ngramed"=> [
"type"=> "text",
"analyzer"=> "start_with_analyzer"
]
]
]
]
]
]
]
];
$response = $client->indices()->create($params); // create an index
and I am getting the following error:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "[completion] unknown field [value], parser not found"
}
],
"type": "illegal_argument_exception",
"reason": "[completion] unknown field [value], parser not found"
},
"status": 400
}
I have tried changing value to value.keyword but it is showing same error. I am using elastic search 5.3.2 . How to resolve this error?
In the query you are using field 'value' inside the completion while it is not a field like this, that is the exact error is stating.
You can try the below solution:
$params = [
"index" => $myIndex,
"body" => [
"try" => [
"text" => "ram",
"completion" => [ "label" => "suggest"]
]
]
];
$response = $client->suggest($params);
Hope this will work.
I have two arrays that are the results of two different database queries. I want to merge those two arrays fin the way I explain in the example below
This is the code that executes the first query:
$query = $this->db->query("SELECT kec,pilihan,COUNT(pilihan) as total FROM votes GROUP BY pilihan");
$someArray = [];
foreach($query->result_array() as $row){
array_push($someArray, [
'data' => $row['pilihan'],
'total' => $row['total'],
'location' => $row['kec'],
]);
}
This is the resulting array:
[
{
"data": "1",
"total": "2",
"location": "3524230"
},
{
"data": "2",
"total": "3",
"location": "3524230"
},
{
"data": "3",
"total": "1",
"location": "3524230"
}
]
This is the code that executes the second query:
$query = $this->db->query("SELECT * FROM owl_dptmaster GROUP BY kecamatan");
$someArray = [];
foreach($query->result_array() as $row){
array_push($someArray, [
'location' => $row['kecamatan']
]);
}
And the second array looks like this:
[
{
"location": "3524230"
},
{
"location": "3524240"
},
{
"location": "3524250"
},
{
"location": "3524260"
}
]
I want to merge those two arrays so that the result would be this array:
[
{
"location": "3524230",
"data1": "2",
"data2": "3",
"data3": "1"
},
{
"location": "3524240",
"data1": null,
"data2": null,
"data3": null
},
{
"location": "3524250",
"data1": null,
"data2": null,
"data3": null
},
{
"location": "3524260",
"data1": null,
"data2": null,
"data3": null
}
]
How can I achieve this?
Instead of combining arrays, you could do the summarizing in sql with sub queries. Assuming you're using mysql.
$query = $this->db->query(
"select owl.kecamatan,
(select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 1) data1,
(select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 2) data2,
(select count(pilihan) from votes where kec = owl.kecamatan and pilihan = 3) data3,
from owl_dptmaster owl
order by owl.kecamatan"
);
You could do this query and then build an array of "data" . $pilihan => $totalPilihan which you push into the $someArray when the $location changes.
$query = $this->db->query(
"select owl.kecamatan as location, v.pilihan
from owl_dptmaster owl
left join votes v
on owl.kecamatan = v.kec"
order by 1, 2);
You have a results set with all the locations and votes.
// Made of query results...
$query = [
[ "location" => "3524230", "pilihan" => "1" ],
[ "location" => "3524230", "pilihan" => "1" ],
[ "location" => "3524230", "pilihan" => "2" ],
[ "location" => "3524230", "pilihan" => "3" ],
[ "location" => "3524230", "pilihan" => "3" ],
[ "location" => "3524230", "pilihan" => "3" ],
[ "location" => "3524230", "pilihan" => "4" ],
[ "location" => "3524230", "pilihan" => "4" ],
[ "location" => "3524230", "pilihan" => "5" ],
[ "location" => "3524240", "pilihan" => null ],
[ "location" => "3524250", "pilihan" => null ],
[ "location" => "3524260", "pilihan" => null ],
[ "location" => "3524270", "pilihan" => "1" ],
[ "location" => "3524270", "pilihan" => "1" ],
[ "location" => "3524270", "pilihan" => "1" ],
[ "location" => "3524270", "pilihan" => "20" ],
[ "location" => "3524270", "pilihan" => "20" ],
[ "location" => "3524270", "pilihan" => "30" ]
];
And here's a procedural routine to build $someArray[].
$someArray = [];
$xRow = [];
$oldPilihan = '';
$total = 0;
$oldLocation = '';
foreach ($query as $row) {
//echo $row['location'] . " - " . $row['pilihan'] . '<br>';
$location = $row['location'];
$pilihan = $row['pilihan'];
if ($pilihan !== $oldPilihan) {
if (! empty($oldPilihan) ) {
$dataName = 'data' . $oldPilihan;
$xRow[] = array($dataName => $total);
}
$total = 1;
$oldPilihan = $pilihan;
} else {
$total++;
}
if ($location !== $oldLocation) {
if (! empty($oldLocation)) {
array_push($someArray, $xRow);
}
$xRow = [ 'location' => $location ];
$oldLocation = $location;
}
}
$dataName = 'data' . $oldPilihan;
$xRow[] = array($dataName => $total);
array_push($someArray, $xRow);
var_dump($someArray);