Coede reorganization to reuse sql query by db_pager.
[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
30         $sql = "SELECT (ov_amount+ov_gst+ov_freight+ov_freight_tax-ov_discount-alloc) AS BalToAllocate
31                 FROM ".TB_PREF."debtor_trans WHERE trans_no=$trans_no AND type=$trans_type";
32         $result = db_query($sql,"calculate the allocation");
33         $myrow = db_fetch_row($result);
34
35         return $myrow[0];
36 }
37
38 //----------------------------------------------------------------------------------------
39
40 function update_debtor_trans_allocation($trans_type, $trans_no, $alloc)
41 {
42         $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc = alloc + $alloc
43                 WHERE type=$trans_type AND trans_no = $trans_no";
44         db_query($sql, "The debtor transaction record could not be modified for the allocation against it");
45 }
46
47 //-------------------------------------------------------------------------------------------------------------
48
49 function void_cust_allocations($type, $type_no, $date="")
50 {
51         return clear_cust_alloctions($type, $type_no, $date);
52 }
53
54 //-------------------------------------------------------------------------------------------------------------
55
56 function clear_cust_alloctions($type, $type_no, $date="")
57 {
58         // clear any allocations for this transaction
59         $sql = "SELECT * FROM ".TB_PREF."cust_allocations
60                 WHERE (trans_type_from=$type AND trans_no_from=$type_no)
61                 OR (trans_type_to=$type AND trans_no_to=$type_no)";
62         $result = db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
63
64         while ($row = db_fetch($result))
65         {
66                 $sql = "UPDATE ".TB_PREF."debtor_trans SET alloc=alloc - " . $row['amt'] . "
67                         WHERE (type= " . $row['trans_type_from'] . " AND trans_no=" . $row['trans_no_from'] . ")
68                         OR (type=" . $row['trans_type_to'] . " AND trans_no=" . $row['trans_no_to'] . ")";
69                 db_query($sql, "could not clear allocation");
70                 // 2008-09-20 Joe Hunt
71                 if ($date != "")
72                         exchange_variation($type, $type_no, $row['trans_type_to'], $row['trans_no_to'], $date,
73                                 $row['amt'], payment_person_types::customer(), true);
74                 //////////////////////
75         }
76
77
78         // remove any allocations for this transaction
79         $sql = "DELETE FROM ".TB_PREF."cust_allocations
80                 WHERE (trans_type_from=$type AND trans_no_from=$type_no)
81                 OR (trans_type_to=$type AND trans_no_to=$type_no)";
82
83         db_query($sql, "could not void debtor transactions for type=$type and trans_no=$type_no");
84 }
85 //----------------------------------------------------------------------------------------
86
87 function get_alloc_trans_sql($extra_fields=null, $extra_conditions=null, $extra_tables=null)
88 {
89         $sql = "SELECT
90                 trans.type,
91                 trans.trans_no,
92                 trans.reference,
93                 trans.tran_date,
94                 debtor.name AS DebtorName, 
95                 debtor.curr_code, 
96                 ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount AS Total,
97                 trans.alloc,
98                 trans.due_date,
99                 debtor.address,
100                 trans.version ";
101
102         if ($extra_fields)
103                 $sql .= ", $extra_fields ";
104
105         $sql .= " FROM ".TB_PREF."debtor_trans as trans, "
106                                 .TB_PREF."debtors_master as debtor";
107         if ($extra_tables)
108                 $sql .= ",$extra_tables ";
109
110         $sql .= " WHERE trans.debtor_no=debtor.debtor_no";
111
112         if ($extra_conditions)
113                 $sql .= " AND $extra_conditions ";
114         
115         return $sql;
116 }
117
118
119 //-------------------------------------------------------------------------------------------------------------
120
121 function get_allocatable_from_cust_sql($customer_id, $settled)
122 {
123         $settled_sql = "";
124         if (!$settled)
125         {
126                 $settled_sql = " AND (round(ov_amount+ov_gst+ov_freight+ov_freight_tax-ov_discount-alloc,6) > 0)";
127         }
128         $cust_sql = "";
129         if ($customer_id != null)
130                 $cust_sql = " AND trans.debtor_no = $customer_id";
131
132         $sql = get_alloc_trans_sql("round(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) <= 0 AS settled",
133                 "(type=12 OR type=11 OR type=2) AND (trans.ov_amount > 0) " . $settled_sql . $cust_sql);
134
135         return $sql;
136 }
137
138 //-------------------------------------------------------------------------------------------------------------
139
140 function get_allocatable_to_cust_transactions($customer_id, $trans_no=null, $type=null)
141 {
142         if ($trans_no != null and $type != null)
143         {
144                 $sql = get_alloc_trans_sql("amt", "trans.trans_no = alloc.trans_no_to
145                         AND trans.type = alloc.trans_type_to
146                         AND alloc.trans_no_from=$trans_no
147                         AND alloc.trans_type_from=$type
148                         AND trans.debtor_no=$customer_id",
149                         "".TB_PREF."cust_allocations as alloc");
150         }
151         else
152         {
153                 $sql = get_alloc_trans_sql(null, "round(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc,6) > 0
154                         AND trans.type != " . systypes::cust_payment() . "
155                         AND trans.type != " . systypes::bank_deposit() . "
156                         AND trans.type != 11
157                         AND trans.type != 13
158                         AND trans.debtor_no=$customer_id");
159         }
160
161         return db_query($sql." ORDER BY trans_no", "Cannot retreive alloc to transactions");
162 }
163
164
165 ?>