*** empty log message ***
[fa-stable.git] / sales / includes / db / custalloc_db.inc
1 <?php
2
3 //----------------------------------------------------------------------------------------
4
5 function add_cust_allocation($amount, $trans_type_from, $trans_no_from, 
6         $trans_type_to, $trans_no_to)
7 {
8         $sql = "INSERT INTO ".TB_PREF."cust_allocations (
9                 amt, date_alloc, 
10                 trans_type_from, trans_no_from, trans_no_to, trans_type_to) 
11                 VALUES ($amount, Now(), $trans_type_from, $trans_no_from, $trans_no_to, $trans_type_to)";
12                         
13         db_query($sql, "A customer allocation could not be added to the database");
14 }
15
16 //----------------------------------------------------------------------------------------
17
18
19 function delete_cust_allocation($trans_id)
20 {
21         $sql = "DELETE FROM ".TB_PREF."cust_allocations WHERE id = " . $trans_id;
22         return db_query($sql, "The existing allocation $trans_id could not be deleted");
23 }
24
25 //----------------------------------------------------------------------------------------
26
27 function get_DebtorTrans_allocation_balance($trans_type, $trans_no)
28 {
29         $sql = "SELECT (ov_amount+ov_gst+ov_freight-ov_discount-alloc) AS BalToAllocate 
30                 FROM ".TB_PREF."debtor_trans WHERE trans_no=$trans_no AND type=$trans_type";
31         $result = db_query($sql,"calculate the allocation");
32         $myrow = db_fetch_row($result); 
33         
34         return $myrow[0];
35 }
36
37 //----------------------------------------------------------------------------------------
38
39 function update_debtor_trans_allocation($trans_type, $trans_no, $alloc)
40 {
41         $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc = alloc + $alloc
42                 WHERE type=$trans_type AND trans_no = $trans_no";
43         db_query($sql, "The debtor transaction record could not be modified for the allocation against it");
44 }
45
46 //-------------------------------------------------------------------------------------------------------------
47
48 function void_cust_allocations($type, $type_no)
49 {
50         return clear_cust_alloctions($type, $type_no);
51 }
52
53 //-------------------------------------------------------------------------------------------------------------
54
55 function clear_cust_alloctions($type, $type_no)
56 {
57         // clear any allocations for this transaction
58         $sql = "SELECT * FROM ".TB_PREF."cust_allocations 
59                 WHERE (trans_type_from=$type AND trans_no_from=$type_no) 
60                 OR (trans_type_to=$type AND trans_no_to=$type_no)";
61         $result = db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
62         
63         while ($row = db_fetch($result))
64         {
65                 $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc=alloc - " . $row['amt'] . "
66                         WHERE (type= " . $row['trans_type_from'] . " AND trans_no=" . $row['trans_no_from'] . ") 
67                         OR (type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'] . ")";
68                 db_query($sql, "could not clear allocation");                                           
69         }
70                                                 
71
72         // remove any allocations for this transaction
73         $sql = "DELETE FROM ".TB_PREF."cust_allocations 
74                 WHERE (trans_type_from=$type AND trans_no_from=$type_no) 
75                 OR (trans_type_to=$type AND trans_no_to=$type_no)";
76                                 
77         db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");                                      
78 }
79
80 //-------------------------------------------------------------------------------------------------------------
81
82 function get_allocatable_from_cust_transactions($customer_id, $settled)
83 {
84         $settled_sql = "";
85         if (!$settled) 
86         {
87                 $settled_sql = " AND round(ABS(ov_amount+ov_gst+ov_freight+ov_discount)-alloc,6) > 0";
88         }
89         
90         $cust_sql = "";
91         if ($customer_id != null)
92                 $cust_sql = " AND ".TB_PREF."debtor_trans.debtor_no = $customer_id";            
93         
94         return get_customer_transactions("round(ABS(ov_amount+ov_gst+ov_freight+ov_discount)-alloc,6) <= 0 AS settled", 
95                 "(type=" . systypes::cust_payment(). " OR type=11 OR type=2) AND (".TB_PREF."debtor_trans.ov_amount < 0) " . $settled_sql . $cust_sql); 
96 }
97
98 //-------------------------------------------------------------------------------------------------------------
99
100 function get_allocatable_to_cust_transactions($customer_id, $trans_no=null, $type=null)
101 {       
102         if ($trans_no != null and $type != null) 
103         {
104                 return get_customer_transactions("amt", "".TB_PREF."debtor_trans.trans_no = ".TB_PREF."cust_allocations.trans_no_to 
105                         AND ".TB_PREF."debtor_trans.type = ".TB_PREF."cust_allocations.trans_type_to
106                         AND ".TB_PREF."cust_allocations.trans_no_from=$trans_no
107                         AND ".TB_PREF."cust_allocations.trans_type_from=$type 
108                         AND ".TB_PREF."debtor_trans.debtor_no=$customer_id", 
109                         "".TB_PREF."cust_allocations");                                         
110         } 
111         else 
112         {
113                 return get_customer_transactions(null, "round(ABS(ov_amount+ov_gst+ov_freight+ov_discount)-alloc,6) > 0
114                         AND ".TB_PREF."debtor_trans.type != " . systypes::cust_payment() . "
115                         AND ".TB_PREF."debtor_trans.debtor_no=$customer_id");
116         }                                                                                                                       
117 }
118
119
120 ?>