+
+//--------------------------------------------------------------------------------------------------
+function update_prepaid_so_line($line_id, $qty_invoiced)
+{
+ $sql = "UPDATE ".TB_PREF."sales_order_details
+ SET invoiced = invoiced + ".(float)$qty_invoiced."
+ WHERE id=".db_escape($line_id);
+
+ db_query($sql, "The document detail record could not be updated with invoiced qty");
+ return true;
+}
+/*
+ Returns array of all issued invoices to sales order $order_no, optinally up to trans_no==$up_to
+*/
+function get_sales_order_invoices($order_no)
+{
+ $sql = "SELECT trans_no, dt.type as type, tran_date, reference, prep_amount
+ FROM ".TB_PREF."debtor_trans dt
+ LEFT JOIN ".TB_PREF."voided v ON v.type=dt.type AND v.id=dt.trans_no
+ WHERE ISNULL(v.id) AND dt.type=".ST_SALESINVOICE." AND dt.order_=".db_escape($order_no)
+ ." ORDER BY dt.tran_date, dt.reference, dt.trans_no";
+
+ return db_query($sql, "cannot retrieve sales invoices for sales order");
+}
+
+function is_sales_order_started($order_no)
+{
+ $sql = "SELECT count(*) FROM ".TB_PREF."sales_order_details WHERE order_no=".db_escape($order_no)." AND trans_type=".ST_SALESORDER
+ ." AND (invoiced!=0 OR qty_sent!=0)";
+
+ $result = db_fetch(db_query($sql, "cannot retrieve sales invoices for sales order"));
+ return $result[0];
+}
+
+//---------------------------------------------------------------------------------------------
+//
+// Mark/unmark sales order as template.
+//
+function sales_order_set_template($id, $status)
+{
+ $sql = "UPDATE ".TB_PREF."sales_orders SET type = ".db_escape($status)." WHERE order_no=".db_escape($id);
+ db_query($sql, "Can't change sales order type");
+}
+
+/*
+ Check whether sales order is issued in prepaid mode and already opened
+*/
+
+function is_prepaid_order_open($order_no)
+{
+ $sql = "SELECT count(*)
+ FROM ".TB_PREF."sales_orders o,
+ ((SELECT trans_no_to FROM ".TB_PREF."cust_allocations
+ WHERE trans_type_to=".ST_SALESORDER." AND trans_no_to=".db_escape($order_no).")
+ UNION
+ (SELECT order_ FROM ".TB_PREF."debtor_trans
+ WHERE type=".ST_SALESINVOICE." AND order_=".db_escape($order_no).")) related
+ WHERE
+ o.prep_amount>0
+ AND o.trans_type = ".ST_SALESORDER."
+ AND o.order_no = " . db_escape($order_no);
+
+ $result = db_fetch(db_query($sql, "cannot check prepaid order open"));
+
+ return $result[0];
+}