d15deb80930d789fa22a8608c47f4e37554a8cda
[fa-stable.git] / 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 = $amount / $rate;
20                 $supp_discount = $discount / $rate;
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     $supplier_accounts = get_supplier_accounts($supplier_id);
34
35         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                 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                 add_gl_trans_supplier($trans_type, $payment_id, $date_, $bank_gl_account, 0, 0,
48                         -$supp_amount, $supplier_id, "", $rate);
49         }
50
51    /*now enter the bank_trans entry */
52         add_bank_trans($trans_type, $payment_id, $bank_account, $ref,
53                 $date_, -($amount), payment_person_types::supplier(),
54                 $supplier_id, $bank_account_currency,
55                 "Could not add the supplier payment bank transaction");
56
57         add_comments($trans_type, $payment_id, $date_, $memo_);
58
59         references::save_last($ref, $trans_type);
60
61         commit_transaction();
62
63         return $payment_id;
64 }
65
66 //------------------------------------------------------------------------------------------------
67
68 function void_supp_payment($type, $type_no)
69 {
70         begin_transaction();
71
72         void_bank_trans($type, $type_no, true);
73         void_gl_trans($type, $type_no, true);
74         void_supp_allocations($type, $type_no);
75         void_supp_trans($type, $type_no);
76
77         commit_transaction();
78 }
79
80
81 ?>