+//----------------------------------------------------------------------------------------
+
+function get_alloc_order_sql($extra_fields=null, $extra_conditions=null, $extra_tables=null)
+{
+ $sql = "SELECT
+ so.trans_type as type,
+ so.order_no as trans_no,
+ so.reference,
+ so.ord_date as tran_date,
+ debtor.name AS DebtorName,
+ debtor.curr_code,
+ Sum(line.unit_price*line.quantity*(1-line.discount_percent))+freight_cost as Total,
+ Sum(alloc.amt) as alloc,
+ so.delivery_date as due_date,
+ debtor.address,
+ so.version,
+
+ Sum(line.qty_sent) AS TotDelivered,
+ Sum(line.quantity) AS TotQuantity";
+
+ if ($extra_fields)
+ $sql .= ", $extra_fields ";
+
+ $sql .= " FROM ".TB_PREF."sales_orders as so
+ LEFT JOIN ".TB_PREF."cust_allocations as alloc
+ ON alloc.trans_type_to=".ST_SALESORDER
+ ." AND so.order_no = alloc.trans_no_to,"
+ .TB_PREF."sales_order_details as line,"
+ .TB_PREF."debtors_master as debtor";
+
+ if ($extra_tables)
+ $sql .= ",$extra_tables ";
+
+ $sql .= " WHERE so.debtor_no=debtor.debtor_no
+ AND so.trans_type = ".ST_SALESORDER."
+ AND so.order_no = line.order_no
+ AND so.trans_type = line.trans_type";
+
+ if ($extra_conditions)
+ $sql .= " AND $extra_conditions ";
+
+ $sql .= " GROUP BY so.order_no,
+ so.debtor_no,
+ so.branch_code,
+ so.customer_ref,
+ so.ord_date,
+ so.deliver_to";
+ return $sql;
+}
+
+//-------------------------------------------------------------------------------------------------------------
+
+function get_allocatable_sales_orders($customer_id, $trans_no=null, $type=null)
+{
+ if ($trans_no != null and $type != null)
+ {
+ $sql = get_alloc_order_sql("alloc.amt", "alloc.trans_no_from=$trans_no
+ AND alloc.trans_type_from=$type
+ AND so.debtor_no=".db_escape($customer_id));
+ }
+ else
+ {
+ $sql = get_alloc_order_sql(null, "so.debtor_no=".db_escape($customer_id));
+ }
+//display_notification($sql);
+ return db_query($sql." ORDER BY so.order_no", "Cannot retreive alloc to orders");
+}
+