Laravel: How to get JSON objects with names separeted by "-"? - arrays

How can I get {{ $cSiacon->'NOME-CLIENTE' }} if it is dash separated? I´ve tried ['NOME-CLIENTE'] but Laravel gives me:
ErrorException (E_ERROR)
Cannot use object of type Illuminate\Http\JsonResponse as array
In my code:
$responseClienteSiacon = $clienteSiaconSOAP->ROS_ValidarCliente($paramClienteSiacon);
$cSiacon = response()->json(($responseClienteSiacon->ValidarCliente->Cliente));
return $cSiacon;
The result is:
[
{
"CODIGO-TIPO-PESSOA": "J",
"CPF-CNPJ": "00635344000177",
"CODIGO-GRUPO": "07384",
"NUMERO-SEQUENCIA": 0,
"NUMERO-COTA": 853,
"NOME-CLIENTE": "AUTO ESCOLA GUILHERMITTI E L LTDA",
"NUMERO-CONTRATO": 859866,
"DESCRICAO-BEM": "HONDA NXR 160 BROS",
"VALOR-BEM": 12975,
"NUMERO-TELEFONE": "017 32581859",
"DATA-PROXIMA-REUNIAO": "20190322",
"SITUACAO-COBRANCA": "N",
"DESCRICAO-SITUACAO-COBRANCA": "Normal",
"FASE-SITUACAO-COBRANCA": "N000",
"CODIGO-PLANO-COTA": 31,
"DATA-ENTREGA": "20180507",
"DATA-CONTEMPLACAO": "20170622",
"PERC-TOTAL-PAGO": 87.7196,
"PERC-TOTAL-PENDENTE": 3.1401,
"PERC-QUITACAO": 12.2804,
"CODIGO-FORMA-PAGAMENTO": 1,
"DATA-NASCIMENTO": "",
"DATA-CANCELAMENTO": "",
"CADASTRO-ATUALIZADO": "N",
"SEGMENTO-CADOC": 4,
"CEP": 15115000
},
{...
}
]
Also I´ve tried to get a higher position like: json(($responseClienteSiacon->ValidarCliente)); but the problem stils the same: the dash separated attribute name.
Should I convert each name before?

Like this
$object->{'NOME-CLIENTE'};
POC
https://3v4l.org/cTO7o
Example using blade
#php
$json = <<<JSON
[
{
"CODIGO-TIPO-PESSOA": "J",
"CPF-CNPJ": "00635344000177",
"CODIGO-GRUPO": "07384",
"NUMERO-SEQUENCIA": 0,
"NUMERO-COTA": 853,
"NOME-CLIENTE": "AUTO ESCOLA GUILHERMITTI E L LTDA",
"NUMERO-CONTRATO": 859866,
"DESCRICAO-BEM": "HONDA NXR 160 BROS",
"VALOR-BEM": 12975,
"NUMERO-TELEFONE": "017 32581859",
"DATA-PROXIMA-REUNIAO": "20190322",
"SITUACAO-COBRANCA": "N",
"DESCRICAO-SITUACAO-COBRANCA": "Normal",
"FASE-SITUACAO-COBRANCA": "N000",
"CODIGO-PLANO-COTA": 31,
"DATA-ENTREGA": "20180507",
"DATA-CONTEMPLACAO": "20170622",
"PERC-TOTAL-PAGO": 87.7196,
"PERC-TOTAL-PENDENTE": 3.1401,
"PERC-QUITACAO": 12.2804,
"CODIGO-FORMA-PAGAMENTO": 1,
"DATA-NASCIMENTO": "",
"DATA-CANCELAMENTO": "",
"CADASTRO-ATUALIZADO": "N",
"SEGMENTO-CADOC": 4,
"CEP": 15115000
}
]
JSON;
$json = json_decode($json);
#endphp
{{-- will echo "AUTO ESCOLA GUILHERMITTI E L LTDA" --}}
{{ $json[0]->{'NOME-CLIENTE'} }}

Related

Nested query and retrieve parent attributes for child filter

I was going through Yonik's blog.
I am using solr 5.3.0 and I have a scenario which I am trying to solve.
Any help will be appreciated.
Generalizing Yonik's example:
Old Document:
{
product_name : "Awesome T-Shirt",
category : "Clothing",
color : [ "Red", "Blue"],
size : [ "L", "M", "XL" ]
}
Parent:
{
product_name : "Awesome T-Shirt",
category : "Clothing",
}
Nested Children:
{
color : "Red",
size : "L",
quantity: 2
}
{
color : "Blue",
size : "M",
quantity: 3
}
{
color : "Blue",
size : "L",
quantity: 0
}
{
color : "Red",
size : "L",
quantity: 0
}
{
color : "Red",
size : "XL",
quantity: 1
}
Counting some scenarios:
1) If I put filter for color:Red and size:L I get the children documents only. Is that possible to get the parent fields on response (Since I have multiple products with color:Red and size:L).
2) If I search for color:Red and quantity greater than 0, I get multiple documents while this search is just to check if item in red color is available. Though group by is a work around, is there any other way.
If you want to get parent documents with child documents I'd do this:
/select?q=id:<parent id>
&fq=type:<parent type>
&fl=*,[child parentFilter="type:<parent type>" limit=10]
it will return a structure like this:
{
"type":"<parent type>",
....
"_childDocuments_":[{
<doc>..</doc>
...
<doc>..</doc>
}]
}
child document count is limited with limit=10 you can change that of course but default is 10 :)
I think Solr's block join parsers will get you what you need:
Block join

AngularJS ng-repeat duplicated error, when search is fired

I have a listing using ng-repeat with a search for two available parameters (month and state).
If I make a search, I get a ng-repeat error found duplicates.
Can not understand why if in both cases the JSON data have the same structure (just the values will change).
I have these ng-repeats: item in data and nested inside : uniqueitem in item
I've tried to use track by $index but it loops for every single character, and for item.index or item.label1 but triggers the found duplicates erros again.
Here is my loop using ng-repeat.
<tbody ng-repeat="item in data">
<tr ng-repeat="uniqueitem in item">
<td>
{{uniqueitem.label1 | number}}
</td>
<td>
{{uniqueitem.label2 | number}}
</td>
My JSON has this structure :
[
{
"index": 0,
"label1": "Initials",
"label2": "2",
"label3": "18",
"label4": "12",
"label5": 150,
"label6": "30",
"label7": 60,
"label5A": "v",
"label7A": "r"
},
{
"index": 1,
"label1": "Others",
"label2": 5485,
"label3": 27289,
"label4": 37776,
"label5": 72.23,
"label6": 91949,
"label7": 29.67,
"label5A": "r",
"label7A": "r"
},
....
]
Just works !
Inside
$http.post(ApiEndpoint.url,$scope.formData).success(function(data) {
Instead of this line :
$scope.data = JSON.stringify(data);
I add these lines :
var acp = {};
acp.resultdata = [ data ];
$scope.data = acp.resultdata;
I will replicate in a Plunkr, can not say why JSON.stringify causes this behavior.

in array, json data and put in MySql with php

I have a data under format 'JSON' like this one :
{
"email": "john#john.fr",
"line_items": [
{
"sku": "123456789",
"price": "0.67",
"price_with_tax": "4.00",
"tax_lines": [
{
"title": "tax010",
"rate": 0.01,
"price": "1.11"
},
{
"title": "tax00200",
"rate": 0.02,
"price": "2.22"
}
]
},
{
"sku": "012345666",
"price": "1.67",
"price_with_tax": "5.00",
"tax_lines": [
{
"title": "tax0003000",
"rate": 0.03,
"price": "3.33"
}
]
}
]
}
I want put it in my database (MySql) by PDO::prepare. My following sql query works but second line_items have not good value im Mysql :
1st item :::: good value
email:john#john.fr
sku:123456789
price:0.67
price_with_tax:4.00
price_tax1:1.11
price_tax2:2.22
2nd item ::::
email:john#john.fr
sku:012345666
price:1.67
price_with_tax:5.00
wrong value ::::
price_tax1:1.11
price_tax2:2.22
How can I put it good value ?
here is my code :
$dataDecode = json_decode($jsondata);
$email = $dataDecode->email;
try
{
$dtbs = new PDO($dsn_dev, $pdo_user, $pdo_password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch (Exception $e)
{
die('Error : ' . $e->getMessage());
}
try
{
foreach ($dataDecode->line_items as $obj)
{
$var_sku = $obj->sku;
$var_price = $obj->price;
$var_price_with_tax = $obj->price_with_tax;
$taxNewArray = array();
foreach ($dataDecode->line_items[0]->tax_lines as $obj2)
{
array_push($taxNewArray , $obj2);
}
$val1st = array_shift($taxNewArray);
$val2nd = array_pop ($taxNewArray);
$var_tax1 = $val1st->price;
$var_tax2 = $val2nd->price;
$stmt = $dtbs->prepare("INSERT INTO $tabledata ($mysql_email, $mysql_sku, $mysq_price, $mysql_price_with_tax, $mysql_price__tax1___line_items, $mysql_price__tax2___line_items)
VALUES (:email, :sku, :price, :price_with_tax, :price_tax1, :price_tax2)");
$stmt->execute(array(':email'=>$email,
':sku'=>$var_sku,
':price'=>$var_price,
':price_with_tax'=>$var_price_with_tax,
':price_tax1'=>$var_tax1,
':price_tax2'=>$var_tax2
));
}
}
catch(Exception $e)
{
throw $e;
}
Do you have a idée ?
If there's only one entry in tax_lines, your code will try to set $var_tax2 from the nonexistent second entry. You need to check for this and substitute some other value:
$var_tax2 = $val2nd ? $val2nd->price : '0.0';
The other problem is this line:
foreach ($dataDecode->line_items[0]->tax_lines as $obj2)
You're using the tax lines from the first line item every time. That should be:
foreach ($obj->tax_lines as $obj2)

Why AngularJS currency filter formats negative numbers with parenthesis?

Live Demo
Why this:
# Controller
$scope.price = -10;
# View
{{ price | currency }}
results in ($10.00) rather than -$10.00?
I know this is an old question, but the accepted answer is only answering why this happens, without a concrete solution to the problem. I think the "most correct way" of doing this, is to use a decorator like so:
angular
.module('app')
.config(['$provide', function($provide) {
$provide.decorator('$locale', ['$delegate', function($delegate) {
if($delegate.id == 'en-us') {
$delegate.NUMBER_FORMATS.PATTERNS[1].negPre = '-\u00A4';
$delegate.NUMBER_FORMATS.PATTERNS[1].negSuf = '';
}
return $delegate;
}]);
}]);
This is only called once, valid for any filter which depends on it, and you don't need a custom filter for your currency formatting.
This is a popular way to represent negative currencies. Wikipedia:
In bookkeeping, amounts owed are often represented by red numbers, or a number in parentheses, as an alternative notation to represent negative numbers.
You can see in the Angular source code where they do this (negSuf/negPre):
function $LocaleProvider(){
this.$get = function() {
return {
id: 'en-us',
NUMBER_FORMATS: {
DECIMAL_SEP: '.',
GROUP_SEP: ',',
PATTERNS: [
{ // Decimal Pattern
minInt: 1,
minFrac: 0,
maxFrac: 3,
posPre: '',
posSuf: '',
negPre: '-',
negSuf: '',
gSize: 3,
lgSize: 3
},{ //Currency Pattern
minInt: 1,
minFrac: 2,
maxFrac: 2,
posPre: '\u00A4',
posSuf: '',
negPre: '(\u00A4',
negSuf: ')',
gSize: 3,
lgSize: 3
}
],
CURRENCY_SYM: '$'
},
It works better for me by checking negative number:
var app = angular.module('myApp');
app.filter('customCurrency', ["$filter", function ($filter) {
return function(amount, currencySymbol){
var currency = $filter('currency');
if(amount < 0){
return currency(amount, currencySymbol).replace("-", "(") + ')'
}
return currency(amount, currencySymbol);
};
}]);
Do you mean display -$10.00 rather than ($10.00)?
The default, at least angularJs version 1.2.1 is to display with parentheses. Eg.: ($10.00)).
If so, this is my situation. I created a custom filter for that:
var app = angular.module('myApp');
app.filter('customCurrency', ["$filter", function ($filter) {
return function(amount, currencySymbol){
var currency = $filter('currency');
if(amount.charAt(0) === "-"){
return currency(amount, currencySymbol).replace("(", "-").replace(")", "");
}
return currency(amount, currencySymbol);
};
}]);
So it delegates to the built-in currency filter and "decorates" or "un-decorates" the parentheses.
I couldn't find a way to change $LocaleProvider on the fly. If someone knows please let me know.
cheers
Leonardo Correa
Update: Angular 1.4 no longer uses parentheses to indicate negative values but now uses the "-" symbol. Here is a link to a discussion: https://github.com/angular/angular.js/issues/12870
I used the decorator as described by marc to return the .negPre and .negSuf to use the parens.
If you don't mind keeping the parenthesis and just want a quick and easy way to achieve this eg: -($250.00) try the following:
<ul ng-repeat="data in customers">
<li>
Balance:
<span ng-if="data.balance<0">-</span>
<span ng-if="data.balance>0">+</span>
{{data.balance | currency}}
</li>
</ul>
If you want to remove the (), then you can create your own filter or try the other answers.
Edit your angular.js file around line number -36180
change negPre and negSuf by removing - and putting parenthesis
For Example
Change From :
"NUMBER_FORMATS": {
"CURRENCY_SYM": "$",
"DECIMAL_SEP": ".",
"GROUP_SEP": ",",
"PATTERNS": [
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 3,
"minFrac": 0,
"minInt": 1,
"negPre": "-",
"negSuf": "",
"posPre": "",
"posSuf": ""
},
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 2,
"minFrac": 2,
"minInt": 1,
"negPre": "-\u00a4",
"negSuf": "",
"posPre": "\u00a4",
"posSuf": ""
}
]
}
To
"NUMBER_FORMATS": {
"CURRENCY_SYM": "$",
"DECIMAL_SEP": ".",
"GROUP_SEP": ",",
"PATTERNS": [
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 3,
"minFrac": 0,
"minInt": 1,
"negPre": "-",
"negSuf": "",
"posPre": "",
"posSuf": ""
},
{
"gSize": 3,
"lgSize": 3,
"maxFrac": 2,
"minFrac": 2,
"minInt": 1,
"negPre": "(\u00a4",
"negSuf": ")",
"posPre": "\u00a4",
"posSuf": ""
}
]
}

Acessing inner element of json array from mapreduce

My sample Json object is as follows
{
"numRecommenders": 0,
"publicProfileUrl": "http://www.linkedin.com/pub/heena-vyas/16/786/826",
"positions": {
"total": 1,
"positionList": [
{
"id": "91286566",
"title": "senior executive",
"company": {
"name": "Reliance",
"industry": "Oil & Energy",
"type": "Public Company",
"size": "10,001+ employees"
},
"isCurrent": true
}
]
},
My mapreduce function is give below:
String map = "function() {"
+"for (index in this.positions.positionList) {"
+"emit(this.positions.positionList[index].company.name, {count: 1});"
+"}}";
String reduce = "function(key, values) {" + "var sum = 0;"
+ "values.forEach(function(value) {" + "sum += value['count'];"
+ "});" + "return {count: sum};" + "};";
MapReduceCommand mapReduceCommand = new MapReduceCommand(
mongoCollection, map, reduce.toString(), null,
MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput out = mongoCollection.mapReduce(mapReduceCommand);
But current I'm working with 1.5 million objects from mongoDb.
I'm getting the following exception..
Exception in thread "main" com.mongodb.CommandResult$CommandFailure: command failed [command failed [mapreduce] { "assertion" : "too much data for in memory map/reduce" , "assertionCode" : 13604 , "errmsg" : "db assertion failure" , "ok" : 0.0}
at com.mongodb.CommandResult.getException(CommandResult.java:70)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:116)
at com.mongodb.DBCollection.mapReduce(DBCollection.java:961)
at com.mongo.dboperations.MongoStorage.runGroupCommand(MongoStorage.java:126)
at com.mongo.dboperations.MongoStorage.main(MongoStorage.java:218)
How to resolve this exception?
Inline output for map/reduce in MongoDB is the fastest option, provided that your output fits within the 16 MB limit. Beyond that, you have to output to a collection.

Resources