Dimension is missing in Supplier Invoice View, GL Rows.
[fa-stable.git] / purchasing / includes / db / invoice_db.inc
index 1d729ea53a117b5c31d95cf788e74bc7fef01363..a1268d693e490f6132bf6edb77305a8860acf110 100644 (file)
@@ -124,6 +124,8 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
 
        $supplier = get_supplier($supp_trans->supplier_id);
 
+       add_new_exchange_rate($supplier['curr_code'], $supp_trans->tran_date, $supp_trans->ex_rate);
+
     foreach ($taxes as $n => $taxitem)
     {
                $taxes[$n]['Value'] =  round2($taxitem['Value'], user_price_dec());
@@ -343,24 +345,24 @@ function add_supp_invoice($supp_trans, $invoice_no=0) // do not receive as ref b
                if ($invoice_alloc_balance > 0) 
                {       //the invoice is not already fully allocated 
 
-                       $trans = get_supp_trans($invoice_no, ST_SUPPINVOICE);
-                       $total = $trans['Total'];
+                       $trans = get_supp_trans($invoice_id, ST_SUPPCREDIT);
+                       $total = -$trans['Total'];
 
                        $allocate_amount = ($invoice_alloc_balance > $total) ? $total : $invoice_alloc_balance;
+
                        /*Now insert the allocation record if > 0 */
                        if ($allocate_amount != 0) 
                        {
-                               update_supp_trans_allocation(ST_SUPPINVOICE, $invoice_no, $allocate_amount);
-                               update_supp_trans_allocation(ST_SUPPCREDIT, $invoice_id, $allocate_amount);
                                add_supp_allocation($allocate_amount, ST_SUPPCREDIT, $invoice_id, ST_SUPPINVOICE, $invoice_no,
                                        $date_);
+                               update_supp_trans_allocation(ST_SUPPINVOICE, $invoice_no);
+                               update_supp_trans_allocation(ST_SUPPCREDIT, $invoice_id);
 
                                exchange_variation(ST_SUPPCREDIT, $invoice_id, ST_SUPPINVOICE, $invoice_no, $date_,
                                        $allocate_amount, PT_SUPPLIER);
                        }
                }
        }
-
        $supp_trans->trans_no = $invoice_id;
        hook_db_postwrite($supp_trans, $supp_trans->trans_type);
     commit_transaction();
@@ -440,10 +442,10 @@ function read_supp_invoice($trans_no, $trans_type, &$supp_trans)
 
                if (db_num_rows($result) > 0)
                {
-
+                       $counter = 0;
+                       $sql2 = "";
             while ($details_row = db_fetch($result))
             {
-
                if ($details_row["gl_code"] == 0)
                {
                        $supp_trans->add_grn_to_trans($details_row["grn_item_id"], $details_row["po_detail_item_id"], $details_row["stock_id"],
@@ -452,8 +454,16 @@ function read_supp_invoice($trans_no, $trans_type, &$supp_trans)
                }
                else
                {
-                       $supp_trans->add_gl_codes_to_trans($details_row["gl_code"], get_gl_account_name($details_row["gl_code"]), 0, 0,
-                                       $details_row["FullUnitPrice"], $details_row["memo_"]);
+                       $sql2 .= "$counter";                            
+                                       $sql = "SELECT gl.counter, gl.dimension_id, gl.dimension2_id, gl.account FROM ".TB_PREF."gl_trans gl
+                                               WHERE type_no = ".db_escape($trans_no)." AND type = ".db_escape($trans_type)." 
+                                               AND gl.account = ".$details_row["gl_code"]." AND gl.counter NOT IN ($sql2)";
+                                       $res = db_query($sql, "Cannot retreive a gl_trans");
+                                       $row = db_fetch($res);
+                       $supp_trans->add_gl_codes_to_trans($details_row["gl_code"], get_gl_account_name($details_row["gl_code"]), 
+                               $row['dimension_id'], $row['dimension2_id'], $details_row["FullUnitPrice"], $details_row["memo_"]);
+                                       $sql2 .= ",";
+                                       $counter = $row['counter'];
                }
             }
                        $supp_trans->tax_overrides = get_tax_overrides($trans_type, $trans_no);