// Makes parent documents for direct delivery/invoice by recurent call.
// $policy - 0 or 1: writeoff/return for IV, back order/cancel for DN
function write($policy=0) {
-
+
+ global $SysPrefs, $Refs;
+
begin_transaction(); // prevents partial database changes in case of direct delivery/invoice
if ($this->reference != 'auto' && $this->trans_no == 0 && !is_new_reference($this->reference, $this->trans_type))
{
- commit_transaction();
- return -1;
+ if (!empty($SysPrefs->prefs['ref_no_auto_increase']))
+ $this->reference = $Refs->get_next($this->trans_type, null, array('date' => Today()));
+ if (!is_new_reference($this->reference, $this->trans_type))
+ {
+ commit_transaction();
+ return -1;
+ }
}
if (count($this->src_docs) == 0 && ($this->trans_type == ST_SALESINVOICE || $this->trans_type == ST_CUSTDELIVERY) && !$this->is_prepaid()) {
// this is direct document - first add parent