+
+function get_sales_child_lines($trans_type, $trans_no, $lines=true)
+{
+ if (!($ctype = get_child_type($trans_type)))
+ return false;
+ if (!is_array($trans_no)) {
+ $trans_no = array($trans_no);
+ }
+
+ $par_tbl = $trans_type == ST_SALESORDER ? "sales_order_details" : "debtor_trans_details";
+ $par_no = $trans_type == ST_SALESORDER ? "trans.order_no" : "trans.debtor_trans_no";
+
+ foreach($trans_no as $n => $trans) {
+ $trans_no[$n] = db_escape($trans);
+ }
+ $sql = "SELECT child.*
+ FROM
+ ".TB_PREF."debtor_trans_details child
+ LEFT JOIN ".TB_PREF."$par_tbl trans
+ ON child.src_id=trans.id AND child.debtor_trans_type=$ctype
+ WHERE $par_no IN(". implode(',', array_values($trans_no)).")";
+
+ if (!$lines)
+ $sql .= " GROUP BY child.debtor_trans_no";
+
+ $sql .= " ORDER BY child.debtor_trans_no";
+
+ return db_query($sql, "can't retrieve child trans");
+}
+
+function get_sales_child_numbers($trans_type, $trans_no)
+{
+ $trans = array();
+ $res = get_sales_child_lines($trans_type, $trans_no, false);
+ while ($line = db_fetch($res)) {
+ $trans[] = $line['debtor_trans_no'];
+ }
+ return $trans;
+}
+
+function get_sales_parent_lines($trans_type, $trans_no, $lines=true)
+{
+ $partype = get_parent_type($trans_type);
+
+ if (!$partype)
+ return false;
+
+ $par_tbl = $partype == ST_SALESORDER ? "sales_order_details" : "debtor_trans_details";
+ $par_no = $partype == ST_SALESORDER ? "parent.order_no" : "parent.debtor_trans_no";
+ $sql = "SELECT parent.*
+ FROM
+ ".TB_PREF."$par_tbl parent
+ LEFT JOIN ".TB_PREF."debtor_trans_details trans
+ ON trans.src_id=parent.id
+ WHERE
+ trans.debtor_trans_type=".db_escape($trans_type)
+ ." AND trans.debtor_trans_no=".db_escape($trans_no);
+ if (!$lines)
+ $sql .= " GROUP BY $par_no";
+
+ $sql .= " ORDER BY $par_no";
+
+ return db_query($sql, "can't retrieve child trans");
+
+}
+
+function get_sales_parent_numbers($trans_type, $trans_no)
+{
+ $trans = array();
+ $res = get_sales_parent_lines($trans_type, $trans_no, false);
+ while ($line = db_fetch($res))
+ $trans[] = $line[$trans_type==ST_CUSTDELIVERY ? 'order_no' : 'debtor_trans_no'];
+ return $trans;
+}
+
+//----------------------------------------------------------------------------------------
+
+function get_sales_child_documents($trans_type, $trans_no)
+{
+ // FIXME - credit notes retrieved here should be those linked to invoices containing
+ // at least one line from related invoice
+
+ if (!count($trans_no))
+ return false;
+ $childs = get_sales_child_numbers($trans_type, $trans_no, false);
+ if (!count($childs))
+ return false;
+
+ $sql = "SELECT * FROM ".TB_PREF."debtor_trans
+ WHERE type=".get_child_type($trans_type)." AND trans_no IN(". implode(',', array_values($childs)).")";
+
+ return db_query($sql,"The related credit notes could not be retreived");
+}
+