Topic: Item_units_db.inc file gives error with this functions

We have some more similar functions in all over the FA, which will be going to give trouble for the users, those who buys new cpanel based host or server.  example.

 
function get_unit_dec($stock_id)
{
    $sql = "SELECT decimals FROM ".TB_PREF."item_units,    ".TB_PREF."stock_master
        WHERE abbr=units AND stock_id=".db_escape($stock_id)." LIMIT 1";
    $result = db_query($sql, "could not get unit decimals");
    $row = db_fetch_row($result);
    return $row[0];    
}

and it has to be replaced with

function get_unit_dec($stock_id)
{
    $sql = "SELECT decimals FROM ".TB_PREF."item_units,    ".TB_PREF."stock_master
        WHERE abbr=units AND stock_id=".db_escape($stock_id)." LIMIT 1";
    $result = db_query($sql, "could not get unit decimals");
    if(db_num_rows($result) > 0){
        $row = db_fetch_row($result);
        return $row[0];
    } else 
        return false;
}

This issue happens with many other files as well. @joe check them in all other files and update these functions.

Subscription service based on FA
HRM CRM POS batch Themes

Re: Item_units_db.inc file gives error with this functions

Thanks @kvvaradha

Nice job you have done.

As I am very overloaded with several tasks, RTL, Graphics etc. I would like you to help me and eventually make a list over places that we should fix.

I guess this is a PHP 8 problem, right? I have already taken some basic ones, but again it is good that we are helping each other detecting and fixing these problems.

/Joe

Re: Item_units_db.inc file gives error with this functions

Sure @joe, I will list them and get you the list of  changes.

Subscription service based on FA
HRM CRM POS batch Themes

Re: Item_units_db.inc file gives error with this functions

can somebody help, i am trying to depreciate asset with straight line, 5% rate
but when process depreciation, the amount is not correct. it's less than 5%
for example

i have an asset of 20,000 USD with 5% depreciation rate, when I process depreciation for the first year instead of 1000 i am getting something less than that.

function next_depreciation_date($depreciation_date) {

  $start = strtotime($depreciation_date);

  $y = date('Y', $start);
  $m = date('n', $start) + 1;

  if ($m > 12) {
    $m = 1;
    $y++;
  }

  return strtotime("$y-$m-1");
}

function depreciation_months($depreciation_start) {

  // assume that depreciation start is the same fiscal year
  //$start = strtotime($depreciation_start);
  //$end = strtotime($year['end']);

  $start = next_depreciation_date($depreciation_start);

  return 12 - date('n', $start) + 1;

  //$d1 = date('j', $start); // day of the month
  //$d2 = date('t', $start); // number of days in month

  //if ($d2 > $d1)
    //$months++;
}

function months_between_dates($start, $end) {
  $start = strtotime($start);
  $end = strtotime($end);

  $y1 = date('Y', $start);
  $m1 = date('n', $start);

  $y2 = date('Y', $end);
  $m2 = date('n', $end);

  return 12 * ($y2 - $y1) + $m2 - $m1;
}

function compute_gl_rows_for_depreciation($item, $no_months, $period) {
  $rows = array();

  $year = get_current_fiscalyear();
  $y = date('Y', strtotime($year['end']));

  switch ($item['depreciation_method']) {
    case 'D':
        $line_value = $item['purchase_cost']*$item['depreciation_rate']/100/12;
        $value = $item['material_cost'] * $item['depreciation_rate'] * $item['depreciation_factor']/100/12;
        if ($value < $line_value)
            $value = $line_value;
        break;

    case 'S':    // purchase_cost stores start cost of item
        $done_months = months_between_dates($item['depreciation_start'], $item['depreciation_date']);
        $remaining_months = 12.0 * 100.0/$item['depreciation_rate'] - $done_months;
        $value = $item['purchase_cost']*$item['depreciation_rate']/100/12;
        break;

    case 'N':
        $N = $item['depreciation_rate'];
        $done_years = months_between_dates($item['depreciation_start'], $item['depreciation_date'])/12;
        $value = $item['purchase_cost']* ($N-$done_years)/($N*($N+1)/2)/12;
        break;

    case 'O':
        $value = $item['material_cost'];
        break;
  }

  $next = next_depreciation_date($item['depreciation_date']);
  $m = date('n', $next);

  $total = 0;
  $cnt = 0;
  for ($i=$m; $i < $m + $no_months; $i++) {

    if ($item['depreciation_method'] == 'S') {
        if ($cnt >= $remaining_months)
            $value = 0;
    }

    $date = sql2date(date("$y-$i-t", strtotime("$y-$i-1")));

    $total += $value;

    if (FA_YEARLY == $period) {
      // yearly
      if ($i == $m + $no_months - 1)
        $rows[] = array('date' => $date, 'value' => $total);
      /*else
        $rows[] = array('date' => $date, 'value' => 0);
        */
    }
    else {
      // monthly
      $rows[] = array('date' => $date, 'value' => $value);
    }

    $cnt++;

    if ($item['depreciation_method'] == 'O') {
        // depreciate only in the first month
        $value = 0;
    }
}

  return $rows;
}

Re: Item_units_db.inc file gives error with this functions

Hello dude, May be it will be in the calculation issue, you need to describe, what comes and what you exactly need it with this code. describe that. We needs to understand it.

Subscription service based on FA
HRM CRM POS batch Themes

Re: Item_units_db.inc file gives error with this functions

kvvaradha wrote:

Hello dude, May be it will be in the calculation issue, you need to describe, what comes and what you exactly need it with this code. describe that. We needs to understand it.

Hello, just forget about my code I was trying to use the fixed asset module, to register my assets, but when I process depreciation, the value is not correct according to the depreciation rate,

for example: when you register an asset with purchase value of 30,000 USD, with Straight Line depreciation rate of 5%. for the first year I am supposed to get 1500 USD and my book value should be 28,500 USD,
So instead i am getting depreciation value of 650 USD which is 2.2%,... WHERE DO YOU FIX THIS IN THE CODE for it to get the exact 5%

Re: Item_units_db.inc file gives error with this functions

For straight line method, you need to use the purchase cost. so the purchase cost may be 15000 USD, this is a chance.

and probably you need to trace it with calculation from the compute_gl_rows_for_depreciation with some display errors placed in between locations i cant understand full concept of straight line method here programmed. it looks they are getting remaining months and process with new one. so in the each line place some display errors to make it readable for you.

Subscription service based on FA
HRM CRM POS batch Themes

Re: Item_units_db.inc file gives error with this functions

kvvaradha wrote:

For straight line method, you need to use the purchase cost. so the purchase cost may be 15000 USD, this is a chance.

and probably you need to trace it with calculation from the compute_gl_rows_for_depreciation with some display errors placed in between locations i cant understand full concept of straight line method here programmed. it looks they are getting remaining months and process with new one. so in the each line place some display errors to make it readable for you.

I am not very good at Ajax, how do I replace the calculation of % to get the exact percent value of and asset after depreciation.

e.g: if I purchased my asset with 5000 USD, with 5% depreciation rate, I am expecting to get 250 usd depreciation after 1 year. and my book value will be 4750 USD.

THanks

9 (edited by kvvaradha 07/03/2021 05:07:14 pm)

Re: Item_units_db.inc file gives error with this functions

kvvaradha wrote:

Sure @joe, I will list them and get you the list of  changes.

Here is the total list of changes @joe. it comes with more files, i put a search list here.

/var/www/FA/2410/admin/db/security_db.inc:
   65:     return $row[0];

/var/www/FA/2410/admin/db/tags_db.inc:
   68:     return $row[0];
  108:     return $row[0];

/var/www/FA/2410/dimensions/includes/dimensions_db.inc:
  185:     return $row[0];

/var/www/FA/2410/fixed_assets/includes/fa_classes_db.inc:
   30:     return $row[0];

/var/www/FA/2410/gl/includes/db/gl_db_account_types.inc:
  101:     return $row[0];
  158:     return $row[0];

/var/www/FA/2410/gl/includes/db/gl_db_accounts.inc:
   88:         return $row[0];

/var/www/FA/2410/gl/includes/db/gl_db_bank_trans.inc:
  137:     return $row[0];

/var/www/FA/2410/gl/includes/db/gl_db_banking.inc:
  138:     return $row[0];

/var/www/FA/2410/gl/includes/db/gl_db_rates.inc:
   35:         return $row[0];

/var/www/FA/2410/gl/includes/db/gl_db_trans.inc:
  368:     return $row[0];
  426:     return $row[0];

/var/www/FA/2410/includes/dashboard.inc:
  851:     return $row[0];
  909:     return $row[0];
 1197:     return $row[0];
 1205:     return $row[0];
 1213:     return $row[0];
 1227:     return $row[0];
 1235:     return $row[0];
 1244:     return $row[0];
 1252:     return $row[0];
 1262:     return $row[0];
 1270:     return $row[0];
 1278:     return $row[0];
 1287:     return $row[0];
 1315:     return $row[0];
 1326:     return $row[0];
 1334:     return $row[0];
 1342:     return $row[0];
 1350:     return $row[0];
 1359:     return $row[0];
 1368:     return $row[0];
 1376:     return $row[0];
 1384:     return $row[0];
 1392:     return $row[0];
 1400:     return $row[0];
 1436:         return $row[0];
 1451:         return $row[0];
 1460:     return $row[0];

/var/www/FA/2410/includes/db/connect_db.inc:
   24:     return $row[0];

/var/www/FA/2410/includes/db/crm_contacts_db.inc:
  245:     return $row[0];

/var/www/FA/2410/includes/db/inventory_db.inc:
  170:     return $row[0];
  348:     return $row[0];
  497:         return $row[0];

/var/www/FA/2410/includes/db_pager.inc:
  352:             $this->rec_count = $row[0];

/var/www/FA/2410/inventory/includes/db/items_category_db.inc:
  101:     return $row[0];

/var/www/FA/2410/inventory/includes/db/items_units_db.inc:
   51:     return $row[0];
   75:     return $row[0];

/var/www/FA/2410/purchasing/includes/db/grn_db.inc:
  208:     return $row[0];

/var/www/FA/2410/purchasing/includes/db/suppliers_db.inc:
  147:     return $row[0];

/var/www/FA/2410/reporting/rep103.php:
  100:     return $row[0];

/var/www/FA/2410/reporting/rep205.php:
   56:     return $row[0];

/var/www/FA/2410/reporting/rep306.php:
   89:         return $row[0];

/var/www/FA/2410/reporting/rep310.php:
   90:         return $row[0];

/var/www/FA/2410/reporting/rep451.php:
   36:     return $row[0];

/var/www/FA/2410/reporting/rep601.php:
   39:     return $row[0];

/var/www/FA/2410/reporting/rep602.php:
   40:     return $row[0];

/var/www/FA/2410/sales/includes/db/cust_trans_db.inc:
  247:     return $row[0];

/var/www/FA/2410/sales/includes/db/customers_db.inc:
  135:     return $row[0];

/var/www/FA/2410/sales/includes/db/recurrent_invoices_db.inc:
   88:     return !$row[0];

/var/www/FA/2410/sales/includes/db/sales_delivery_db.inc:
  214:       else $freight = $row[0];

/var/www/FA/2410/sales/includes/db/sales_groups_db.inc:
   52:     return $row[0];
   95:     return $row[0];
  154:     return $row[0];

/var/www/FA/2410/sales/includes/db/sales_invoice_db.inc:
  294:     return $row[0];

/var/www/FA/2410/sales/includes/db/sales_order_db.inc:
  647:         return $row == false ? false : $row[0];

/var/www/FA/2410/sales/includes/db/sales_points_db.inc:
   64:     return $row[0];

/var/www/FA/2410/sales/includes/db/sales_types_db.inc:
   53:     return $row[0];

/var/www/FA/2410/sql/alter2.2.php:
   90:         $max_order = $row[0];

/var/www/FA/2410/taxes/db/tax_types_db.inc:
   76:     return $row[0];
Subscription service based on FA
HRM CRM POS batch Themes

Re: Item_units_db.inc file gives error with this functions

All these

        return $row[0];

should be changed to

if(db_num_rows($result) > 0){
        $row = db_fetch_row($result);
        return $row[0];
    } else 
        return false;
Subscription service based on FA
HRM CRM POS batch Themes

Re: Item_units_db.inc file gives error with this functions

Ok I will have a look at this.

Joe

Re: Item_units_db.inc file gives error with this functions

@kvvaradha

Couldn't we just put these two lines after the query result:

if (!$result)
    return false;

Because if there is a $result then db_fetch will always return at least one array.

/Joe

Re: Item_units_db.inc file gives error with this functions

I tried this @joe, but the if condition didn't work.
though it goes to the next line and gives error.

Trying to access array offset on value of type bool in 
Subscription service based on FA
HRM CRM POS batch Themes

Re: Item_units_db.inc file gives error with this functions

Did you also try:

if ($result == false)
    return false;

Joe

Re: Item_units_db.inc file gives error with this functions

@Joe both are same conditions, right?

Though i checked it again, still it passes to the next, db_fetch_row and gives error.

and also while checking this i found another issue in the sales_kit.php
line no 49

qty_cell($myrow["quantity"], false,     $myrow["units"] == '' ? 0 : get_qty_dec($myrow["comp_name"]));

and it has to pass the stock id instead of comp_name

qty_cell($myrow["quantity"], false,     $myrow["units"] == '' ? 0 : get_qty_dec($myrow["stock_id"]));
Subscription service based on FA
HRM CRM POS batch Themes

Re: Item_units_db.inc file gives error with this functions

ok Thanks @kvvaradha.

I will just have a short discussion with Janusz (itronics) about this mysqli issue, before trying to fix it.

I will fix the qty_cell error.

/Joe

Re: Item_units_db.inc file gives error with this functions

After talking to Janusz, we can
change the line:

return $row[0];

to

return is_array($row) ? $row[0] : false;

This fixes the problem. But we don't have to do it all over the places. Only where there are NO aggregates as SUM, COUNT etc. These aggregates will always deliver at least one row with empty values.

I will start fixing the needed places tomorrow.

/Joe

Re: Item_units_db.inc file gives error with this functions

This mission is now fixed and committed to stable repo. I hope all of them are taken, but please report any remains.

/Joe

Re: Item_units_db.inc file gives error with this functions

I will take a look into the new version.

Subscription service based on FA
HRM CRM POS batch Themes