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