Customer Payment, Supplier Payment: early discount support.
[fa-stable.git] / purchasing / includes / purchasing_db.inc
index 9ab331fe819bf33e0969f66eb4d0059013f1b0b0..fa8b3a497c392a2374938c4181708d9ab4a88b9e 100644 (file)
@@ -1,12 +1,22 @@
 <?php
-
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
 include_once($path_to_root . "/purchasing/includes/supp_trans_class.inc");
 
 include_once($path_to_root . "/includes/banking.inc");
+include_once($path_to_root . "/includes/inventory.inc");
 
 include_once($path_to_root . "/includes/date_functions.inc");
 
-include_once($path_to_root . "/includes/db/inventory_db.inc");
+include_once($path_to_root . "/includes/db/allocations_db.inc");
 
 include_once($path_to_root . "/purchasing/includes/db/supp_trans_db.inc");
 include_once($path_to_root . "/purchasing/includes/db/po_db.inc");
@@ -23,14 +33,11 @@ include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
 // $amount is in SUPPLIERS'S currency
 
 function add_gl_trans_supplier($type, $type_no, $date_, $account, $dimension, $dimension2,  
-       $amount, $supplier_id, $err_msg="")
+       $amount, $supplier_id, $rate=0, $memo="")
 {
-       if ($err_msg == "")
-               $err_msg = "The supplier GL transaction could not be inserted"; 
-               
-       return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, "", 
+       return add_gl_trans($type, $type_no, $date_, $account, $dimension, $dimension2, $memo, 
                $amount, get_supplier_currency($supplier_id), 
-               payment_person_types::supplier(), $supplier_id, $err_msg);
+               PT_SUPPLIER, $supplier_id, $rate);
 }
 
 //----------------------------------------------------------------------------------------
@@ -38,8 +45,8 @@ function add_gl_trans_supplier($type, $type_no, $date_, $account, $dimension, $d
 function get_purchase_price($supplier_id, $stock_id)
 {
        $sql = "SELECT price, conversion_factor FROM ".TB_PREF."purch_data 
-               WHERE supplier_id = '" . $supplier_id . "' 
-               AND stock_id = '". $stock_id . "'";
+               WHERE supplier_id = ".db_escape($supplier_id) . " 
+               AND stock_id = ".db_escape($stock_id);
        $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
 
        if (db_num_rows($result) == 1)
@@ -53,7 +60,226 @@ function get_purchase_price($supplier_id, $stock_id)
        }       
 }
 
+function get_purchase_conversion_factor($supplier_id, $stock_id)
+{
+       $sql = "SELECT conversion_factor FROM ".TB_PREF."purch_data 
+               WHERE supplier_id = ".db_escape($supplier_id)." 
+               AND stock_id = ".db_escape($stock_id);
+       $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
+
+       if (db_num_rows($result) == 1)
+       {
+               $myrow = db_fetch($result);
+               return $myrow['conversion_factor'];
+       } 
+       else 
+       {
+               return 1;
+       }       
+}
 //----------------------------------------------------------------------------------------
 
+function get_purchase_data($supplier_id, $stock_id)
+{
+       $sql = "SELECT * FROM ".TB_PREF."purch_data 
+               WHERE supplier_id = ".db_escape($supplier_id) . "
+               AND stock_id = ".db_escape($stock_id);
+       $result = db_query($sql, "The supplier pricing details for " . $stock_id . " could not be retrieved");    
+
+       return db_fetch($result);
+}
+
+function add_or_update_purchase_data($supplier_id, $stock_id, $price, $description="", $uom="")
+{
+       $data = get_purchase_data($supplier_id, $stock_id);
+       if ($data === false)
+       {
+               $sql = "INSERT INTO ".TB_PREF."purch_data (supplier_id, stock_id, price, suppliers_uom,
+                       conversion_factor, supplier_description) VALUES (".db_escape($supplier_id)
+                       .", ".db_escape($stock_id).", ".db_escape($price).", "
+                       .db_escape($uom).", 1, ".db_escape($description).")";
+               db_query($sql,"The supplier purchasing details could not be added");
+               return;
+       }       
+       $price = round($price * $data['conversion_factor'], user_price_dec());
+       $sql = "UPDATE ".TB_PREF."purch_data SET price=".db_escape($price);
+       if ($uom != "")
+               $sql .= ",suppliers_uom=".db_escape($uom);
+       if ($description != "") 
+               $sql .= ",supplier_description=".db_escape($description);
+       $sql .= " WHERE stock_id=".db_escape($stock_id)." AND supplier_id=".db_escape($supplier_id);
+       db_query($sql,"The supplier purchasing details could not be updated");
+       return true;
+}
+
+function get_po_prepayments($supp_trans)
+{
+       // collect purchase order line ids
+       $allocations = array();
+       $line_ids = array();
+       foreach($supp_trans->grn_items as $item)
+               if ($item->po_detail_item)
+                       $line_ids[] = $item->po_detail_item;
+
+       if (!count($line_ids))
+               return $allocations;
+
+       $sql = "SELECT DISTINCT allocs.*
+               FROM ".TB_PREF."supp_allocations allocs 
+                       LEFT JOIN ".TB_PREF."purch_order_details line ON line.order_no=allocs.trans_no_to AND trans_type_to=".ST_PURCHORDER."
+               WHERE line.po_detail_item IN(".implode(',', array_values($line_ids)).")";
+
+       $result = db_query($sql, "Cannot retrieve po prepayments");
+
+       while($dat = db_fetch($result))
+       {
+               $allocations[] = $dat;
+       }
+
+       return $allocations;
+}
+
+
+//------------------------------------ Backend interfaces--------------------------------------------
+
+//---------------------------------------------------------------------------------------------------
+//
+//     Add Purchase Order, GRN or Purchase Invoice with parent auto documents (if any)
+//
+function add_direct_supp_trans($cart)
+{
+       global $Refs, $type_shortcuts;
+
+       begin_transaction(__FUNCTION__, func_get_args());
+
+       if ($cart->trans_type != ST_PURCHORDER) {
+               // for direct grn/invoice set same dates for lines as for whole document
+               foreach ($cart->line_items as $line_no =>$line)
+                       $cart->line_items[$line_no]->req_del_date = $cart->tran_date;
+       }
+
+       $ref = $cart->reference;
+       if ($cart->trans_type != ST_PURCHORDER) {
+               $cart->reference = 'auto';
+       }
+       $order_no = add_po($cart);
+       $cart->order_no = $order_no;
+
+       if ($cart->trans_type == ST_PURCHORDER) {
+               commit_transaction();
+               return $order_no;
+       }
+
+       //Direct GRN
+       if ($cart->trans_type == ST_SUPPRECEIVE)
+               $cart->reference = $ref;
+       if ($cart->trans_type != ST_SUPPINVOICE)
+               $cart->Comments = $cart->reference; //grn does not hold supp_ref
+
+       $grn_no = write_grn($cart);
+       if ($cart->trans_type == ST_SUPPRECEIVE) {
+               commit_transaction(); // save PO+GRN
+               return $grn_no;
+       }
+       //      Direct Purchase Invoice
+       $inv = new supp_trans(ST_SUPPINVOICE);
+       $inv->Comments = $cart->Comments;
+       $inv->supplier_id = $cart->supplier_id;
+       $inv->tran_date = $cart->tran_date;
+       $inv->due_date = $cart->due_date;
+       $inv->dimension = $cart->dimension;
+       $inv->dimension2 = $cart->dimension2;
+       $inv->reference = $ref;
+       $inv->supp_reference = $cart->supp_ref;
+       $inv->tax_included = $cart->tax_included;
+       $supp = get_supplier($cart->supplier_id);
+       $inv->tax_group_id = $supp['tax_group_id'];
+       $inv->ov_amount = $inv->ov_gst = $inv->ov_discount = 0;
+       $total = 0;
+               foreach($cart->line_items as $key => $line) {
+               $inv->add_grn_to_trans($line->grn_item_id, $line->po_item_id, $line->stock_id,
+                       $line->item_description, $line->quantity, 0, $line->quantity,
+                       $line->price, $line->price, true, get_unit_cost($line->stock_id), '');
+
+               $total += round2(($line->quantity * $line->price), user_price_dec());
+       }
+       $inv->tax_overrides = $cart->tax_overrides;
+       if (!$inv->tax_included) {
+               $taxes = $inv->get_taxes();
+               foreach( $taxes as $taxitem) {
+                       $total += isset($taxitem['Override']) ? $taxitem['Override'] : $taxitem['Value'];
+               }
+       }
+       $inv->ex_rate = $cart->ex_rate;
+
+       $inv_no = add_supp_invoice($inv);
+
+       if ($cart->cash_account) {
+               $pmt_no = write_supp_payment(0, $inv->supplier_id, $cart->cash_account, $inv->tran_date, $Refs->get_next(ST_SUPPAYMENT, null, $inv->tran_date), 
+                       $total, 0, _('Payment for:').$inv->supp_reference .' ('.$type_shortcuts[ST_SUPPINVOICE].$inv_no.')');
+               add_supp_allocation($total, ST_SUPPAYMENT, $pmt_no, ST_SUPPINVOICE, $inv_no, $inv->supplier_id, $inv->tran_date);
+               update_supp_trans_allocation(ST_SUPPINVOICE, $inv_no, $inv->supplier_id);
+               update_supp_trans_allocation(ST_SUPPAYMENT, $pmt_no, $inv->supplier_id);
+       }
+       commit_transaction(); // save PO+GRN+PI(+SP)
+       return $inv_no;
+}
+
+function write_supplier($supplier_id, $supp_name, $ref, $address, $supp_address, $gst_no,
+                               $website, $supp_account_no, $bank_account, $credit_limit = 0, $dim1, $dim2, $curr_code,
+                               $payment_terms, $payable_account, $purchase_account, $payment_discount_account,
+                               $notes, $tax_group_id, $tax_included, $contact, $phone, $phone2, $fax, $email, $lang, $inactive=null)
+{
+               begin_transaction(__FUNCTION__, func_get_args());
+               if ($supplier_id) {
+                       update_supplier($supplier_id, $supp_name, $ref, $address, $supp_address, $gst_no,
+                                       $website, $supp_account_no, $bank_account, $credit_limit = 0, $dim1, $dim2, $curr_code,
+                                       $payment_terms, $payable_account, $purchase_account, $payment_discount_account,
+                                       $notes, $tax_group_id, $tax_included);
+
+                       if ( isset($inactive) )
+                               update_record_status($supplier_id, $inactive, 'suppliers', 'supplier_id');
+               } else {
+                       add_supplier($supp_name, $ref, $address, $supp_address, $gst_no,
+                                       $website, $supp_account_no, $bank_account, $credit_limit = 0, $dim1, $dim2, $curr_code,
+                                       $payment_terms, $payable_account, $purchase_account, $payment_discount_account,
+                                       $notes, $tax_group_id, $tax_included);
+                       $supplier_id = db_insert_id();
+
+                       add_crm_person($ref, $contact, '', $address, $phone, $phone2, $fax, $email, $lang, '');
+
+                       add_crm_contact('supplier', 'general', $supplier_id, db_insert_id());
+               }
+               commit_transaction();
+               return $supplier_id;
+}
+
+function write_supp_allocation($cart)
+{
+       begin_transaction(__FUNCTION__, func_get_args());
+               $result = $cart->write();
+       commit_transaction();
+       return $result;
+}
+
+function save_supp_payment($cart, $supplier_id, $bank_account, $trans_date, $ref, $amount, $memo, $charge, $bank_amount)
+{
+       begin_transaction(__FUNCTION__, func_get_args());
+
+       $discount = 0;
+       foreach($cart->allocs as $alloc)
+               if ($alloc->discount_confirmed)
+                       $discount += $alloc->early_discount*$alloc->amount;
+
+       $payment_no = write_supp_payment($cart->trans_no, $supplier_id, $bank_account, $trans_date, $ref, $amount,
+               $discount, $memo, $charge, $bank_amount);
+
+       $cart->trans_no = $payment_no;
+       $cart->date_ = $trans_date;
+       $cart->write();
+
+       commit_transaction();
+
+       return $payment_no;
+}
 
-?>
\ No newline at end of file