Show also Upcoming Purchase Invoices - in Dashboard. Fixed.
[fa-stable.git] / includes / dashboard.inc
index 43eb29df02c6438eea9fd3d7512f5f3560b1075e..57db155207a056a12c96ca085ac297548192441a 100644 (file)
 ***********************************************************************/
 //$colors = array('#008cc9','#4db625','#ef5500','#eef100','#05c6e6', '#5ee66a'); // Current in Use      
 $colors = Chart::$palette;
+$align = $_SESSION['language']->dir == 'rtl' ? 'right' : 'left';
 $style = "<style>
 div.square {
        display: inline-block;
        width: 200px;
        height:40px;
-       padding: 3px 0 0 5px;
+       padding: 3px 5px 0 5px;
        font-size:12px;
        font-weight:normal;
-       text-align: left;
+       text-align: $align;
        line-height: 1.2;
        color:#fff;
        border-radius:6px;
@@ -235,7 +236,7 @@ function display_supplier_topten()
        td('50%');
        source_graphic($today, $title[0], _("Supplier"), $pg);
        br(3);
-       echo "<div class='square square2'>"._('Purchable Total')."<p class='span1'>";
+       echo "<div class='square square2'>"._('Purchasable Total')."<p class='span1'>";
        echo number_format2($title[1])."</p></div>";
        br(3);
        echo "<div class='square square2'>"._('Aging Total')."<p class='span1'>";
@@ -445,25 +446,21 @@ function customer_top($today, $limit=10, $width='33', &$pg=null)
        headers($th);
        check_page_security('SA_SALESTRANSVIEW');
        $k = 0; //row colour counter
-       $i = 1;
        $names = $totals = array();
        $sales = 0;
        while ($myrow = db_fetch($result))
        {
                $sales += round($myrow['total']);
-               if ($i++ < $limit)
+               alt_table_row_color($k);
+               $name = $myrow["debtor_no"]." ".htmlspecialchars_decode($myrow["name"]);
+               label_cell($name);
+               amount_cell($myrow['total']);
+               if ($pg != null)
                {
-                       alt_table_row_color($k);
-                       $name = $myrow["debtor_no"]." ".htmlspecialchars_decode($myrow["name"]);
-                       label_cell($name);
-                       amount_cell($myrow['total']);
-                       if ($pg != null)
-                       {
-                               $names[] = $name;
-                               $totals[] = round($myrow['total']);
-                       }       
-                       end_row();
-               }
+                       $names[] = $name;
+                       $totals[] = round($myrow['total']);
+               }       
+               end_row();
        }
        if ($pg != null)
        {
@@ -491,25 +488,21 @@ function supplier_top($today, $limit=10, $width='33', &$pg=null)
        headers($th);
        check_page_security('SA_SUPPTRANSVIEW');
        $k = 0; //row colour counter
-       $i = 1;
        $names = $totals = array();
        $total = 0;
        while ($myrow = db_fetch($result))
        {
                $total += $myrow['total'];
-               if ($i++ < $limit)
+               alt_table_row_color($k);
+               $name = $myrow["supplier_id"]." ".htmlspecialchars_decode($myrow["supp_name"]);
+               label_cell($name);
+               amount_cell($myrow['total']);
+               if ($pg != null)
                {
-                       alt_table_row_color($k);
-                       $name = $myrow["supplier_id"]." ".htmlspecialchars_decode($myrow["supp_name"]);
-                       label_cell($name);
-                       amount_cell($myrow['total']);
-                       if ($pg != null)
-                       {
-                               $names[] = $name;
-                               $totals[] = round($myrow['total']);
-                       }       
-                       end_row();
-               }
+                       $names[] = $name;
+                       $totals[] = round($myrow['total']);
+               }       
+               end_row();
        }
        if ($pg != null)
        {
@@ -567,7 +560,6 @@ function stock_top($today, $limit=10, $width='33', $type=0, &$pg=null)
        headers($th);
        check_page_security($sec);
        $k = 0; //row colour counter
-       $i = 1;
        $names = $totals = $costs = $results = array();
        $sales = $cost = $res = 0;
        while ($myrow = db_fetch($result))
@@ -578,30 +570,27 @@ function stock_top($today, $limit=10, $width='33', $type=0, &$pg=null)
                        $cost += $myrow['costs'];
                        $res += ($myrow['total'] - $myrow['costs']);
                }
-               if ($i++ < $limit)
+               alt_table_row_color($k);
+               $name = $myrow["description"];
+               label_cell($name);
+               amount_cell($myrow['total']);
+               if ($type == 0)
                {
-                       alt_table_row_color($k);
-                       $name = $myrow["description"];
-                       label_cell($name);
-                       amount_cell($myrow['total']);
+                       amount_cell($myrow['costs']);
+                       amount_cell($myrow['total'] - $myrow['costs']);
+               }
+               qty_cell($myrow['qty']);
+               if ($pg != NULL)
+               {
+                       $names[] = $name;
+                       $totals[] = round($myrow['total']);     
                        if ($type == 0)
                        {
-                               amount_cell($myrow['costs']);
-                               amount_cell($myrow['total'] - $myrow['costs']);
+                               $costs[] = round($myrow['costs']);
+                               $results[] = round($myrow['total'] - $myrow['costs']);
                        }
-                       qty_cell($myrow['qty']);
-                       if ($pg != NULL)
-                       {
-                               $names[] = $name;
-                               $totals[] = round($myrow['total']);     
-                               if ($type == 0)
-                               {
-                                       $costs[] = round($myrow['costs']);
-                                       $results[] = round($myrow['total'] - $myrow['costs']);
-                               }
-                       }       
-                       end_row();
-               }
+               }       
+               end_row();
        }
        if ($pg != null)
        {
@@ -628,7 +617,7 @@ function dimension_top($today, $limit=10, $width='33', &$pg=null)
                LEFT JOIN ".TB_PREF."dimensions AS d ON (g.dimension_id = d.id OR g.dimension2_id = d.id)
                LEFT JOIN ".TB_PREF."chart_class AS c ON t.class_id = c.cid
                WHERE IF(c.ctype > 3, tran_date >= '$begin', tran_date >= '0000-00-00') 
-               AND tran_date <= '$today'  AND d.reference IS NOT NULL GROUP BY d.reference ORDER BY d.reference DESC LIMIT $limit";    
+               AND tran_date <= '$today'  AND d.reference IS NOT NULL GROUP BY d.reference ORDER BY result DESC LIMIT $limit";         
        $result = db_query($sql, "Transactions could not be calculated");
        $title = sprintf(_("Top %s Dimensions in fiscal year"), $limit);
        display_title($title);
@@ -748,7 +737,7 @@ function gl_week_performance($today, $width="33", $weeks=4)
        $pg->setLabels($week_names);
        $pg->addSerie(_('Sales'), $sales, 'bar');
        $pg->addSerie(_('Costs'), $costs, 'bar');
-       $pg->addSerie(_('Results'), $results, 8);
+       $pg->addSerie(_('Results'), $results, 'spline');
        source_graphic($today, $title, _("Week"), $pg, _("Weeks"), $weeks);
 }
 
@@ -787,7 +776,7 @@ function gl_month_performance($today, $width="33", $months=5)
        $pg->setLabels($month_names);
        $pg->addSerie(_('Sales'), $sales, 'bar');
        $pg->addSerie(_('Costs'), $costs, 'bar');
-       $pg->addSerie(_('Results'), $results, 8);
+       $pg->addSerie(_('Results'), $results, 'spline');
        source_graphic($today, $title, _("Month"), $pg, _("Months"), $months);
 }
 
@@ -799,10 +788,12 @@ function customer_aging($today, $width="33")
        $today1 = date2sql($today);
        $past1 = get_company_pref('past_due_days');
        $past2 = 2 * $past1;
-       // removed - debtor_trans.alloc from all summations
+
        $sign = "IF(`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, 1)";
-       $value = "IFNULL($sign*((trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - 
-               trans.alloc)*trans.rate),0)";
+
+       $value = "$sign*(IF(trans.prep_amount, trans.prep_amount,
+               ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount)) - trans.alloc)*trans.rate";
+
        $due = "IF (trans.type=".ST_SALESINVOICE.", trans.due_date, trans.tran_date)";
     $sql = "SELECT Sum($value) AS Balance,
                                Sum(IF ((TO_DAYS('$today1') - TO_DAYS($due)) > 0,$value,0)) AS Due,
@@ -815,7 +806,7 @@ function customer_aging($today, $width="33")
                        WHERE
                                        debtor.payment_terms = terms.terms_indicator
                                AND debtor.credit_status = credit_status.id";
-               $sql .= " AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - trans.alloc) > ".FLOAT_COMP_DELTA;
+               $sql .= " AND ABS(IF(trans.prep_amount, trans.prep_amount, ABS(trans.ov_amount) + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount) - trans.alloc) > ".FLOAT_COMP_DELTA;
     $result = db_query($sql,"The customer details could not be retrieved");
 
     $row = db_fetch($result);
@@ -839,13 +830,13 @@ function customer_balance($today)
 {
        $today = date2sql($today);
        $sign = "IF(`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, 1)";
-       $value = "IFNULL($sign*((trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - 
-               trans.alloc)*trans.rate),0)";
+       $value = "IFNULL($sign*((trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount -
+               trans.alloc)*trans.rate),0)";
        $due = "IF (trans.type=".ST_SALESINVOICE.", trans.due_date, trans.tran_date)";
     $sql = "SELECT Sum($value) AS Balance
                        FROM ".TB_PREF."debtors_master debtor
                                 LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.tran_date <= '$today' AND debtor.debtor_no = trans.debtor_no AND trans.type <> ".ST_CUSTDELIVERY."
-                       WHERE ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - trans.alloc) > ".FLOAT_COMP_DELTA;
+                       WHERE ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount) - trans.alloc > ".FLOAT_COMP_DELTA;
     $result = db_query($sql,"The customer details could not be retrieved");
     $row = db_fetch($result);
        return $row[0];
@@ -853,7 +844,7 @@ function customer_balance($today)
 
 function supplier_aging($today, $width="33")
 {
-       $pg = new chart('bar', 's3');
+       $pg = new chart('bar', 's3'); 
        if (isset($_POST['select_s3']))
                $pg->type = $_POST['select_s3'];
        $today1 = date2sql($today);
@@ -868,9 +859,8 @@ function supplier_aging($today, $width="33")
                                Sum(IF ((TO_DAYS('$today1') - TO_DAYS($due)) > 0,$value,0)) AS Due,
                                Sum(IF ((TO_DAYS('$today1') - TO_DAYS($due)) > $past1,$value,0)) AS Overdue1,
                                Sum(IF ((TO_DAYS('$today1') - TO_DAYS($due)) > $past2,$value,0)) AS Overdue2
-                       FROM ".TB_PREF."suppliers supplier, ".TB_PREF."supp_trans trans
-                               WHERE supplier.supplier_id = trans.supplier_id
-                       AND trans.tran_date <= '$today1'
+                       FROM ".TB_PREF."suppliers supp
+                        LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id AND trans.tran_date <= '$today1'
                        AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) > ".FLOAT_COMP_DELTA."
                        AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) - trans.alloc > ".FLOAT_COMP_DELTA;
     $result = db_query($sql,"The supplier details could not be retrieved");
@@ -894,7 +884,7 @@ function supplier_aging($today, $width="33")
 
 function supplier_balance($today)
 {
-       $today = date2sql($today);
+       $today = date2sql($today); 
        $value = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_BANKDEPOSIT.", 
        (trans.ov_amount + trans.ov_gst + trans.ov_discount - trans.alloc) * trans.rate,
        (trans.ov_amount + trans.ov_gst + trans.ov_discount + trans.alloc) * trans.rate)";
@@ -1098,19 +1088,25 @@ function supplier_trans($today)
                FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as s 
                WHERE s.supplier_id = trans.supplier_id
                        AND trans.type = ".ST_SUPPINVOICE." AND (ABS(trans.ov_amount + trans.ov_gst + 
-                               trans.ov_discount) - trans.alloc) > ".FLOAT_COMP_DELTA."
-                       AND DATEDIFF('$today', trans.due_date) > 0 ORDER BY days DESC";
+                               trans.ov_discount) - trans.alloc) > ".FLOAT_COMP_DELTA." ORDER BY days DESC";
        $result = db_query($sql);
-       $title = db_num_rows($result) . _(" overdue Purchase Invoices");
+       $title = db_num_rows($result) . _(" unpaid Purchase Invoices");
        display_title($title);
        $th = array("#", _("Ref."), _("Date"), _("Due Date"), _("Supplier"), _("Currency"), _("Total"), 
                _("Remainder"), _("Days"));
        start_table(TABLESTYLE, "width=90%");
        headers($th);
        $k = 0; //row colour counter
+       $due = false;
        while ($myrow = db_fetch($result))
        {
-               alt_table_row_color($k);
+               if ($myrow['days'] > 0)
+               {
+                       start_row("class='overduebg'");
+                       $due = true;
+               }
+               else
+                       alt_table_row_color($k);
                label_cell(get_trans_view_str(ST_SUPPINVOICE, $myrow["trans_no"]));
                label_cell($myrow['reference']);
                label_cell(sql2date($myrow['tran_date']));
@@ -1123,7 +1119,10 @@ function supplier_trans($today)
                label_cell($myrow['days'], "align='right'");
                end_row();
        }
-       end_table(2);
+       end_table();
+       if ($due)
+               display_note(_("Marked items are overdue."), 1, 0, "class='overduefg'");
+       br();
 }
 
 function stock_below_reorder($today, $type)