A non well formed numeric value encountered in laravel 5.5 - php

I have this code
private function calculateDefferedRevenue($price, $sub)
{
$defferedrevenue = '0.00';
if ($sub->has('fulfillment') && is_object($sub->fulfillment)) {
$fulfillments = $sub->fulfillment;
foreach ($fulfillments as $key => $fulfillment) {
if ($fulfillment->has('shipments') && !is_null($fulfillment->shipments)) {
$counts = self::countShipments($fulfillment->shipments);
if (!empty($price) && $counts['shipmentCount'] > 0) {
$shippingFee = self::getShippingFee($sub);
$shippingFees = $counts['unshippedCount'] * $shippingFee;
$price = self::deductUsingCoupon($price, $sub);
$eachShipmentAmount = $price / $counts['shipmentCount'];
$revenue = $eachShipmentAmount * $counts['unshippedCount'];
$revenue = $revenue + $shippingFees;
$defferedrevenue = $defferedrevenue + $revenue;
$defferedrevenue = number_format($defferedrevenue, 2);
}
}
}
}
return $defferedrevenue;
}
and these are the set of values return from sentry after running lines aboove in the laravel queue jobs
{
counts: {
shipmentCount: 4,
shippedCount: 1,
unshippedCount: 3
},
defferedrevenue: 1,049.25,
eachShipmentAmount: 349.75,
key: 1,
price: 1399.00,
revenue: 1049.25,
shippingFee: 0.00,
shippingFees: 0,
sub: Object Subscription
}
Why php is saying A non well formed numeric value encountered on this line
$defferedrevenue = $defferedrevenue + $revenue;

You're using number_format() here, which might add commas:
$defferedrevenue = $defferedrevenue + $revenue;
$defferedrevenue = number_format($defferedrevenue, 2);
So on the next loop iteration, you're likely getting something like:
$defferedrevenue = "1,234" + 1;
This generates a notice because "1,234" is a string that can't be automatically interpreted as a number. Just get rid of the number_format() line from inside the loop and put it after the loop ends:
return number_format($defferedrevenue, 2);

Related

Laravel post data

I am adding a order on my database with diferent taxes and I was suprise with this function that work prefectly on local envoirement is not working wen I deploy the website $tva6, $tva10 and $tva20 still 0.
$tva6 = 0;
$tva10 = 0;
$tva20 = 0;
$cart = session('orderList.orderList');
foreach ($cart as $item){
if ($item['tva'] === 6){
$tva6 += ($item['price'] * $item['qty']);
}
if ($item['tva'] === 10){
$tva10 += ($item['price'] * $item['qty']);
}
if ($item['tva'] === 20){
$tva20 += ($item['price'] * $item['qty']);
}
}
$tva6 = $tva6 - ($tva6 / 1.055);
$tva10 = $tva10 - ($tva10 / 1.1);
$tva20 = $tva20 - ($tva20 / 1.2);
$total = session('orderList.deliSup') + session('DBcart.totalPrice');
$order = Order::create([
"user_id"=>Auth::id(),
"orderId"=>session('orderList.orderIdNaga'),
"orderList"=>session('orderList.orderList'),
"payMethod"=>session('orderList.payMethod'),
"utensils"=>session('orderList.utensils'),
"infoOrder"=>session('orderList.infoOrder'),
"totalPrice"=>$total,
"tva6"=>$tva6,
"tva10"=>$tva10,
"tva20"=>$tva20,
"deliSup"=>session('orderList.deliSup'),
"deliTime"=>session('orderList.deliTime'),
"deliDate"=>session('orderList.deliDate')
]);
Any idea ? thx
edit: $cart is a array with product infos
Maybe $item['tva'] is not integer, use intval for example
foreach ($cart as $item) {
if (intval($item['tva']) === 6) {
$tva6 += ($item['price'] * $item['qty']);
}
}
Try debugging $cart to see if the session is actually returning the array you expect.
Otherwise, check the script where the session is generated, as the problem may be there.
If I'm right in my thinking above. send us the script where the session is generated.

PHP Function call with parameters/variables

I have two functions in one Controller class and I want to call a getFunction with five variables. Here is the function call
public function editItem()
{
$reqNote = 1;
$reqTore = 2;
$reqTorvorlage = 3;
$reqGelbRot = 1;
$reqRot = 0;
$vPunkte = $this->getPoints($reqNote, $reqTore, $reqTorvorlage, $reqGelbRot, $reqRot);
}
And here is the function which should calculate a new value return.
public function getPoints()
{
$vNote = $reqNote;
$vTore = $reqTore;
$vTorvorlage = $reqTorvorlage;
$vRot = $reqRot;
$vGelbRot = $reqGelbRot;
$vPosition = 1;
$vPunkteResult = $vToreP + $vTorvorlageP + $vRotP + $vGelbRotP + $vNoteP;
return $vPunkteResult;
}
What I am doing wrong with my function call because at the moment I get $reqNote is not found?
simply add the variable in signature
public function getPoints($reqNote, $reqTore, $reqTorvorlage, $reqGelbRot, $reqRot)
{
$vNote = $reqNote;
$vTore = $reqTore;
$vTorvorlage = $reqTorvorlage;
$vRot = $reqRot;
$vGelbRot = $reqGelbRot;
$vPosition = 1;
$vPunkteResult = $vToreP + $vTorvorlageP + $vRotP + $vGelbRotP + $vNoteP;
return $vPunkteResult;
}
As your function currently looks as though the intention was to add the arguments (it is actually returning the sum of undefined variables i.e. $vToreP is never assigned, nor are the function arguments), you could take a short-cut:
<?php
function sumThings()
{
return array_sum(func_get_args());
}
var_dump(sumThings(1, 2, 3, 4, 5, 6));
Output:
21
Update the function signature. It should know that it is expecting 5 variables.
public function getPoints($reqNote, $reqTore, $reqTorvorlage, $reqGelbRot, $reqRot)
{
$vNote = $reqNote;
$vTore = $reqTore;
$vTorvorlage = $reqTorvorlage;
$vRot = $reqRot;
$vGelbRot = $reqGelbRot;
$vPosition = 1;
$vPunkteResult = $vToreP + $vTorvorlageP + $vRotP + $vGelbRotP + $vNoteP;
return $vPunkteResult;
}

Alternative to logicalNot() - not doing what expected

I made a query in a EntityRepository.php file, the query goes through and gives me a number of matching entities for example:
100
My new goal is to filter out all entities which have the property feedback.appointmentState.uid = 4.
and when I put in this following constraint:
$query->equals('feedback.appointmentState.uid', 4)
it gives me
0
Now comes the part that I don't understand -
When I change that part to:
$query->logicalNot($query->equals('feedback.appointmentState.uid', 4))
I get
90
It's not quite working this way since it should output 100 as there are no entities that match that filter - but how can I achieve this the best way?
For clarification here my code example (of course I didn't uncomment both commented parts at the same time)
public function countByCompany($company) {
$query = $this->createQuery();
$constraints[] = $query->equals('deleted', 0);
$constraints[] = $query->equals('hidden', 0);
$constraints[] = $query->equals('socialworker.company', $company);
$total = $query->matching($query->logicalAnd($constraints))->count();
// $total is 30
// $constraints[] = $query->equals('feedback.appointmentState.uid', 4);
// $totalCancelled = $query->matching($query->logicalAnd($constraints))->count();
// $totalCancelled is 0
// $constraints[] = $query->logicalNot($query->equals('feedback.appointmentState.uid', 4));
// $totalWithoutCancelled = $query->matching($query->logicalAnd($constraints))->count();
// $totalWithoutCancelled is 15
return false;
}
I dissolve your last query to
public function countByCompany($company) {
$query = $this->createQuery();
$totalWithoutCancelled = $query->matching(
$query->logicalAnd(
$query->equals('deleted', 0),
$query->equals('hidden', 0),
$query->equals('socialworker.company', $company),
$query->logicalNot(
$query->equals('feedback.appointmentState.uid', 4)
)
)
)->count();
return false;
}
Try mine. Does this the same?
public function countByCompany($company) {
$query = $this->createQuery();
$total = $query->matching(
$query->logicalAnd(
$query->equals('deleted', 0),
$query->equals('hidden', 0),
$query->equals('socialworker.company', $company)
),
$query->logicalNot(
$query->equals('feedback.appointmentState.uid', 4)
)
)->count();
return false;
}

code ignator update_batch error

While using update_batch function for codeigniter batch update I get this error
Undefined index: admission_no
function test_update(){
$updateArray = array();
$elefee = $this->input->post('effe');
$extra = $this->input->post('extra');
$messfe = $this->input->post("messfee");
$students = $this->Messmodel->get_each_student();
foreach ($students as $row){
$newdue = abs($row->amount - ($row->mess_fee + $row->ele_fee + $row->dues));
$finaldays = ($row->messdays - $row->messcut) * $messfe;
$messfee = $finaldays + $extra;
$updateArray[]=array('admission_no'=>$row->ADMISSION_NUMBER,'mess_fee' => $messfee);
}
$this->db->update_batch('mess',$updateArray,'admission_no');
}
this is my db snap
When i print update array I get this

PHP equivalent of Excel vlookup on array

After looking for a built in function in php I couldn't find a similar function to Excel's vlookup function.
I need a function that takes in an array and a lookup value and return the required info. So for example:
<?php
$baseCalculationPrice = [
0 => 50, //for values <=500 but >0
500 => 18, //for values <=3000 but >500
3000 => 15, //for values <=5000 but >3000
5000 => 14, //for values >5000
];
//Examples
$numPages = 499;
echo vlookup($numPages,$baseCalculationPrice); //should output 50
$numPages = 500;
echo vlookup($numPages,$baseCalculationPrice); //should output 50
$numPages = 501;
echo vlookup($numPages,$baseCalculationPrice); //should output 18
$numPages = 3000;
echo vlookup($numPages,$baseCalculationPrice); //should output 18
$numPages = 3001;
echo vlookup($numPages,$baseCalculationPrice); //should output 15
$numPages = 5000;
echo vlookup($numPages,$baseCalculationPrice); //should output 15
$numPages = 5001;
echo vlookup($numPages,$baseCalculationPrice); //should output 14
function vlookup($value,$array){
//magic code
return ....;
}
?>
I'm stuck even with the logic behind such a function, so any help would be great - thanks.
function vlookup($lookupValue,$array){
$result;
//test each set against the $lookupValue variable,
//and set/reset the $result value
foreach($array as $key => $value)
{
if($lookupValue > $key)
{
$result = $value;
}
}
return $result;
}
function testDeductionRate(){echo $this->deductionRate('ks',300);//zone and time are parameter for deductionRate() }
//deduction will be acted as vlookup (rangeLookup true)
function deductionRate($zone,$time){
$actualRate = 0;
$allRate = array(
'tg'=>array(0=>0,20=>200,30=>300,40=>400,50=>500),
'ks'=>array(0=>0,20=>100,30=>200,40=>300,50=>400),
'pc'=>array(0=>0,20=>50,30=>100,40=>200,50=>300)
);
if(isset($allRate[$zone])){
$rate = $allRate[$zone];
foreach($rate as $key=>$val){
if($time>=$key) $actualRate = $val;
}
}else{
return -1; //-1 means error
}
return $actualRate;
}
Try this if you would like to query on multi dimension associative array:
function vlookup($lookup_vakue, $lookup_array, $lookup_column, $result_column)
{
foreach ($lookup_array as $item) {
if ($item[$lookup_column] == $lookup_vakue) {
return $item[$result_column];
}
}
return false;
}
Sample Data
$data =
[
['id'=>1,'price'=>100],
['id'=>2,'price'=>200],
['id'=>3,'price'=>300]
];
Query Option
$result = vlookup('2',$data, 'id','price');
Result:
200
$getbase= function($amount) use ($baseCalculationPrice)
{
return (end(array_filter(
$baseCalculationPrice,function ($key) use ($amount)
{
return $key < $amount;
},
ARRAY_FILTER_USE_KEY
)));
};
amount 150 result 50
amount 1500 result 18
amount 25000 result 14

Categories