[0000078] Opened Exchange rate for editing (without storing) in sales/purchasing...
[fa-stable.git] / purchasing / includes / db / supp_payment_db.inc
1 <?php
2
3 function add_supp_payment($supplier_id, $date_, $payment_type, $bank_account,
4         $amount, $discount, $ref, $memo_, $rate=0)
5 {
6         begin_transaction();
7
8         $supplier_currency = get_supplier_currency($supplier_id);
9     $bank_account_currency = get_bank_account_currency($bank_account);
10
11         if ($rate == 0)
12         {
13                 $supp_amount = exchange_from_to($amount, $bank_account_currency, $supplier_currency, $date_);
14                 $supp_discount = exchange_from_to($discount, $bank_account_currency, $supplier_currency, $date_);
15         }
16         else
17         {
18                 $supp_amount = $amount / $rate;
19                 $supp_discount = $discount / $rate;
20         }
21         
22
23         // it's a supplier payment
24         $trans_type = 22;
25
26         /* Create a supp_trans entry for the supplier payment */
27         $payment_id = add_supp_trans($trans_type, $supplier_id, $date_, $date_,
28                 $ref, "", -$supp_amount, 0, -$supp_discount, "", $rate);
29
30         // Now debit creditors account with payment + discount
31
32     $supplier_accounts = get_supplier_accounts($supplier_id);
33
34         add_gl_trans_supplier($trans_type, $payment_id, $date_, $supplier_accounts["payable_account"], 0, 0,
35                 $supp_amount + $supp_discount, $supplier_id, "", $rate);
36
37         // Now credit discount received account with discounts
38         if ($supp_discount != 0)
39         {
40                 add_gl_trans_supplier($trans_type, $payment_id, $date_, $supplier_accounts["payment_discount_account"], 0, 0,
41                         -$supp_discount, $supplier_id, "", $rate);
42         }
43
44         if ($supp_amount != 0)
45         {
46                 add_gl_trans_supplier($trans_type, $payment_id, $date_, $bank_account, 0, 0,
47                         -$supp_amount, $supplier_id, "", $rate);
48         }
49
50    /*now enter the bank_trans entry */
51         add_bank_trans($trans_type, $payment_id, $bank_account, $ref,
52                 $date_, $payment_type, -($amount), payment_person_types::supplier(),
53                 $supplier_id, $bank_account_currency,
54                 "Could not add the supplier payment bank transaction");
55
56         add_comments($trans_type, $payment_id, $date_, $memo_);
57
58         references::save_last($ref, $trans_type);
59
60         commit_transaction();
61
62         return $payment_id;
63 }
64
65 //------------------------------------------------------------------------------------------------
66
67 function void_supp_payment($type, $type_no)
68 {
69         begin_transaction();
70
71         void_bank_trans($type, $type_no, true);
72         void_gl_trans($type, $type_no, true);
73         void_supp_allocations($type, $type_no);
74         void_supp_trans($type, $type_no);
75
76         commit_transaction();
77 }
78
79
80 ?>