Tax Inquiry: fixed refrences to non-existing database fields.
[fa-stable.git] / purchasing / includes / db / invoice_db.inc
index 8193f0ddb573c6fd15a852225c7e4fbc4035dc36..11ce9f931ca33d0c4835a8c9184c5600055ab0fa 100644 (file)
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
 include_once($path_to_root . "/purchasing/includes/db/invoice_items_db.inc");
+include_once($path_to_root . "/admin/db/attachments_db.inc");
 
 //--------------------------------------------------------------------------------------------------
 
 function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
 {
        $sql = "SELECT supp.supp_name, terms.terms, terms.days_before_due,
-               terms.day_in_following_month, supp.tax_included, supp.tax_algorithm,
+               terms.day_in_following_month, supp.tax_included,
                supp.tax_group_id, tax_group.name AS tax_group_name,
                supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1) 
                        * (ov_amount + ov_gst + ov_discount),0)) as cur_credit
@@ -44,7 +45,6 @@ function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
 
     $supp_trans->supplier_id = $supplier_id;
     $supp_trans->tax_included = $myrow['tax_included'];
-    $supp_trans->tax_algorithm = $supp_trans->stored_algorithm = $myrow['tax_algorithm'];
     $supp_trans->supplier_name = $myrow['supp_name'];
        $supp_trans->terms = array( 
                'description' => $myrow['terms'],
@@ -119,14 +119,15 @@ function get_diff_in_home_currency($supplier, $old_date, $date, $amount1, $amoun
 }
 //----------------------------------------------------------------------------------------
 
-function add_supp_invoice(&$supp_trans) // do not receive as ref because we change locally
+function add_supp_invoice(&$supp_trans)
 {
        global $Refs;
 
        //$company_currency = get_company_currency();
        $trans_no = $supp_trans->trans_no;
        $trans_type = $supp_trans->trans_type;
-       /*Start an sql transaction */
+       $supplier = get_supplier($supp_trans->supplier_id);
+
        begin_transaction();
        hook_db_prewrite($supp_trans, $trans_type);
        $tax_total = 0;
@@ -138,8 +139,6 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
        } else
                $allocs = get_po_prepayments($supp_trans);
 
-       $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)
@@ -173,9 +172,10 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
     /*First insert the invoice into the supp_trans table*/
        $invoice_id = write_supp_trans($trans_type, 0, $supp_trans->supplier_id, $date_, $supp_trans->due_date,
                $supp_trans->reference, $supp_trans->supp_reference,
-               $invoice_items_total, $item_added_tax, $supp_trans->ov_discount, "", 0, $supp_trans->tax_included,
-               $supp_trans->tax_algorithm);
+               $invoice_items_total, $item_added_tax, $supp_trans->ov_discount, "", 0, $supp_trans->tax_included);
 
+       if ($trans_no)
+               move_trans_attachments($trans_type, $trans_no, $invoice_id);
 
        $supp_trans->trans_no = $invoice_id;
 
@@ -198,7 +198,7 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                        }
                        add_trans_tax_details($trans_type, $invoice_id, 
                                $taxitem['tax_type_id'], $taxitem['rate'], $supp_trans->tax_included, $taxitem['Value'],
-                               $taxitem['Net'], $ex_rate, $date_, $supp_trans->supp_reference);
+                               $taxitem['Net'], $ex_rate, $date_, $supp_trans->supp_reference, TR_INPUT);
 
                        if (isset($taxitem['purchasing_gl_code']))
                        {
@@ -234,8 +234,8 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $entered_gl_code->gl_code,
                        $entered_gl_code->gl_dim, $entered_gl_code->gl_dim2, $entered_gl_code->amount, $supp_trans->supplier_id, "", 0, $memo_);
 
-               add_supp_invoice_gl_item($trans_type, $invoice_id, $entered_gl_code->gl_code,
-                       $entered_gl_code->amount, $memo_);
+               add_supp_invoice_gl_item($trans_type, $invoice_id, $entered_gl_code->gl_code, $entered_gl_code->amount, $memo_,
+                       $entered_gl_code->gl_dim, $entered_gl_code->gl_dim2);
 
                // store tax details if the gl account is a tax account
                if ($trans_type == ST_SUPPCREDIT)
@@ -270,8 +270,10 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
 
                $stock_gl_code = get_stock_gl_code($entered_grn->item_code);
 
-               $dim = $supplier['dimension_id'] ? $supplier['dimension_id'] : $stock_gl_code['dimension_id'];
-               $dim2 = $supplier['dimension2_id'] ? $supplier['dimension2_id'] : $stock_gl_code['dimension2_id'];
+               $dim = !empty($supp_trans->dimension) ? $supp_trans->dimension :
+                       ($supplier['dimension_id'] ? $supplier['dimension_id'] : $stock_gl_code['dimension_id']);
+               $dim2 = !empty($supp_trans->dimension2) ? $supp_trans->dimension2 :
+                       ($supplier['dimension2_id'] ? $supplier['dimension2_id'] : $stock_gl_code['dimension2_id']);
        if ($trans_type == ST_SUPPCREDIT)
                {
                        $iv_act = (is_inventory_item($entered_grn->item_code) ? $stock_gl_code["inventory_account"] : 
@@ -306,7 +308,7 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                                $currency = get_supplier_currency($supp_trans->supplier_id);
 
                                $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
-                                       $dim, $dim2, $entered_grn->this_quantity_inv * $old_price, $supp_trans->supplier_id, "", $ex_rate);
+                                       $dim, $dim2, $old_value, $supp_trans->supplier_id, "", $ex_rate);
                                $diff = get_diff_in_home_currency($supp_trans->supplier_id, $old_date, $date_, $old_value, 
                                        $taxfree_line);
 
@@ -328,7 +330,8 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                                                global $Refs;
 
                                                $id = get_next_trans_no(ST_JOURNAL);
-                                               $ref = $Refs->get_next(ST_JOURNAL);
+                                               $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
+                                               add_journal(ST_JOURNAL, $id, $diff, $date_, get_company_currency(), $ref);
                                                $stock_id = $entered_grn->item_code;
                                                $stock_gl_code = get_stock_gl_code($stock_id);
                                                $memo = _("Supplier invoice adjustment for zero inventory of ").$stock_id." "._("Invoice")." ".$supp_trans->reference;
@@ -377,9 +380,9 @@ function add_supp_invoice(&$supp_trans) // do not receive as ref because we chan
                        if ($allocate_amount != 0) 
                        {
                                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);
+                                       $supp_trans->supplier_id, $date_);
+                               update_supp_trans_allocation(ST_SUPPINVOICE, $invoice_no, $supp_trans->supplier_id);
+                               update_supp_trans_allocation(ST_SUPPCREDIT, $invoice_id, $supp_trans->supplier_id);
 
                                exchange_variation(ST_SUPPCREDIT, $invoice_id, ST_SUPPINVOICE, $supp_trans->src_docs, $date_,
                                        $allocate_amount, PT_SUPPLIER);
@@ -466,7 +469,6 @@ function read_supp_invoice($trans_no, $trans_type, &$supp_trans)
                $supp_trans->ov_discount = $trans_row["ov_discount"];
                $supp_trans->ov_gst = $trans_row["ov_gst"];
                $supp_trans->tax_included = $trans_row["tax_included"];
-               $supp_trans->stored_algorithm = $supp_trans->tax_algorithm = $trans_row["tax_algorithm"];
 
                $id = $trans_row["trans_no"];
 
@@ -490,7 +492,7 @@ 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_"]);
+                                       $details_row["FullUnitPrice"], $details_row["memo_"], $details_row["dimension_id"], $details_row["dimension2_id"]);
                }
             }
                        $supp_trans->tax_overrides = get_tax_overrides($trans_type, $trans_no);
@@ -595,7 +597,8 @@ function void_supp_invoice($type, $type_no)
 
                                                //Chaitanya : Post a journal entry
                                                $id = get_next_trans_no(ST_JOURNAL);
-                                               $ref = $Refs->get_next(ST_JOURNAL);
+                                               $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
+                                               add_journal(ST_JOURNAL, $id, $details_row["quantity"] * $diff, $old_date, get_company_currency(), $ref);
                                                $stock_id = $details_row["stock_id"];
                                                $stock_gl_code = get_stock_gl_code($stock_id);
                                                $memo = "Reversing Supplier invoice adjustment for zero inventory of ".$stock_id." Invoice: ".$trans['reference'];
@@ -610,16 +613,9 @@ function void_supp_invoice($type, $type_no)
                                                
                                                add_audit_trail(ST_JOURNAL, $id, $old_date);
                                                add_comments(ST_JOURNAL, $id, $old_date, $memo);
-                                               $Refs->save(ST_JOURNAL, $id, $ref);                                             
-                                       }       
-                               }
-                               /*$deliveries = get_deliveries_between($details_row["stock_id"], $old_date, $date_);
-                               if ($deliveries[0] != 0) // have deliveries been done during the period?
-                               {
-                                       update_stock_move_pid(ST_CUSTDELIVERY, $details_row["stock_id"], $old_date, $date_, 0, $mat_cost);
+                                               $Refs->save(ST_JOURNAL, $id, $ref);
+                                       }
                                }
-                               update_stock_move_pid(ST_SUPPRECEIVE, $details_row["stock_id"], $old_date, $old_date, $grn['supplier_id'], $mat_cost);
-                               */
                }
         }
        }
@@ -679,7 +675,7 @@ function remove_not_invoice_item($id)
         $price = get_tax_free_price_for_item($myrow['item_code'], $myrow['unit_price'], $supp['tax_group_id'], $supp['tax_included']);        
 
     add_stock_move(ST_SUPPRECEIVE, $myrow["item_code"], $myrow['grn_batch_id'], $grn['loc_code'], sql2date($grn["delivery_date"]), "",
-        -$myrow["QtyOstdg"], $myrow['std_cost_unit'], $grn["supplier_id"], 1, $price);
+        -$myrow["QtyOstdg"], $myrow['std_cost_unit'], $price);
         
     $clearing_act = get_company_pref('grn_clearing_act');
     if ($clearing_act) {    // otherwise GRN clearing account is not used