Ejemplo n.º 1
0
         if ($id) {
             $success = db_perform(TABLE_RMA, $sql_data_array, 'update', 'id = ' . $id);
             if ($success) {
                 gen_add_audit_log(RMA_LOG_USER_UPDATE . $rma_num);
             } else {
                 $error = true;
             }
         } else {
             // fetch the RMA number
             $result = $db->Execute("select next_rma_num from " . TABLE_CURRENT_STATUS);
             $rma_num = $result->fields['next_rma_num'];
             $sql_data_array['rma_num'] = $rma_num;
             $success = db_perform(TABLE_RMA, $sql_data_array, 'insert');
             if ($success) {
                 $id = db_insert_id();
                 $next_num = string_increment($sql_data_array['rma_num']);
                 $db->Execute("update " . TABLE_CURRENT_STATUS . " set next_rma_num = '" . $next_num . "'");
                 gen_add_audit_log(RMA_LOG_USER_ADD . $rma_num);
             } else {
                 $error = true;
             }
         }
         if (!$error) {
             $messageStack->add(($_POST['id'] ? RMA_MESSAGE_SUCCESS_UPDATE : RMA_MESSAGE_SUCCESS_ADD) . $rma_num, 'success');
         } else {
             $messageStack->add(RMA_MESSAGE_ERROR, 'error');
         }
     }
     break;
 case 'edit':
     $id = db_prepare_input($_POST['rowSeq']);
Ejemplo n.º 2
0
                 case '5':
                     $year_offset = ($i + 1) * 1;
                     break;
                     // Yearly
             }
             $glEntry->post_date = gen_specific_date($post_date, $day_offset, $month_offset, $year_offset);
             $glEntry->period = gen_calculate_period($glEntry->post_date, true);
             if (!$glEntry->period && $i < $glEntry->recur_id - 1) {
                 // recur falls outside of available periods, ignore last calculation
                 $messageStack->add(ORD_PAST_LAST_PERIOD, 'error');
                 $error = true;
                 break;
             }
             $glEntry->journal_main_array['post_date'] = $glEntry->post_date;
             $glEntry->journal_main_array['period'] = $glEntry->period;
             $glEntry->purchase_invoice_id = string_increment($glEntry->journal_main_array['purchase_invoice_id']);
         }
     }
     // restore the first values to continue with post process
     $glEntry->id = $first_id;
     $glEntry->journal_main_array['id'] = $first_id;
     $glEntry->post_date = $first_post_date;
     $glEntry->journal_main_array['post_date'] = $first_post_date;
     $glEntry->purchase_invoice_id = $first_purchase_invoice_id;
     $glEntry->journal_main_array['purchase_invoice_id'] = $first_purchase_invoice_id;
 } else {
     if (!$glEntry->validate_purchase_invoice_id()) {
         $error = true;
     } else {
         if (!$glEntry->Post($glEntry->id ? 'edit' : 'insert')) {
             $error = true;
Ejemplo n.º 3
0
 function post_ordr($action)
 {
     global $db, $messageStack;
     $this->journal_rows = array();
     // initialize ledger row(s) array
     $debit_total = 0;
     $credit_total = 0;
     switch ($this->journal_id) {
         // THE SEQUENCE IS IMPORTANT!
         case 6:
             // Purchase/Receive Journal
         // Purchase/Receive Journal
         case 13:
             // Customer Credit Memo Journal
             $this->closed = 0;
             // force the inv receipt/vcm open since it will be closed by the system, if necessary
             // continue like other payable prep
         // force the inv receipt/vcm open since it will be closed by the system, if necessary
         // continue like other payable prep
         case 3:
             // Purchase Quote Journal
         // Purchase Quote Journal
         case 4:
             // Purchase Order Journal
         // Purchase Order Journal
         case 21:
             // Inventory Direct Purchase Journal (POP)
             $debit_total += $this->add_item_journal_rows('debit');
             // read in line items and add to journal row array
             $debit_total += $this->add_freight_journal_row('debit');
             // put freight into journal row array
             $debit_total += $this->add_tax_journal_rows('debit');
             // fetch tax rates for tax calculation
             $credit_total += $this->add_discount_journal_row('credit');
             // put discount into journal row array
             $this->total_amount = $debit_total - $credit_total;
             $credit_total += $this->add_total_journal_row('credit');
             // put total value into ledger row array
             break;
         case 7:
             // Vendor Credit Memo Journal
         // Vendor Credit Memo Journal
         case 12:
             // Sales/Invoice Journal (Invoice)
             $this->closed = 0;
             // force the inv/cm open since it will be closed by the system, if necessary
             // continue like other receivable prep
         // force the inv/cm open since it will be closed by the system, if necessary
         // continue like other receivable prep
         case 9:
             // Sales Quote Journal
         // Sales Quote Journal
         case 10:
             // Sales Order Journal
         // Sales Order Journal
         case 19:
             // POS Journal
             $credit_total += $this->add_item_journal_rows('credit');
             // read in line items and add to journal row array
             $credit_total += $this->add_freight_journal_row('credit');
             // put freight into journal row array
             $credit_total += $this->add_tax_journal_rows('credit');
             // fetch tax rates for tax calculation
             $debit_total += $this->add_discount_journal_row('debit');
             // put discount into journal row array
             $this->total_amount = $credit_total - $debit_total;
             $debit_total += $this->add_total_journal_row('debit');
             // put total value into ledger row array
             break;
         default:
             return $this->fail_message('bad journal_id in pre-POST processing');
             // this should never happen, JOURNAL_ID is tested at script entry!
     }
     $this->journal_main_array = $this->build_journal_main_array();
     // build ledger main record
     // ***************************** START TRANSACTION *******************************
     $messageStack->debug("\n  started order post purchase_invoice_id = " . $this->purchase_invoice_id . " and id = " . $this->id);
     $db->transStart();
     // *************  Pre-POST processing *************
     // add/update address book
     if ($this->bill_add_update) {
         // billing address
         $this->bill_acct_id = $this->add_account($this->account_type . 'b', $this->bill_acct_id, $this->bill_address_id);
         if (!$this->bill_acct_id) {
             return false;
         }
     }
     if ($this->ship_add_update) {
         // shipping address
         if (!$this->ship_acct_id) {
             $this->ship_acct_id = $this->bill_acct_id;
         }
         // set to bill if adding contact and id not set.
         if ($this->bill_address_id == $this->ship_address_id) {
             $this->ship_address_id = '';
         }
         // force create new ship address, here from copy button
         $this->ship_acct_id = $this->add_account($this->account_type . 's', $this->ship_acct_id, $this->ship_address_id);
         if (!$this->ship_acct_id) {
             return false;
         }
     }
     // set the ship account id to bill account id if null (new accounts with ship update box not checked)
     // basically defaults the shipping same as billing if not specified
     if (!$this->journal_main_array['ship_acct_id']) {
         $this->journal_main_array['ship_acct_id'] = $this->journal_main_array['bill_acct_id'];
     }
     if (!$this->journal_main_array['ship_address_id']) {
         $this->journal_main_array['ship_address_id'] = $this->journal_main_array['bill_address_id'];
     }
     // ************* POST journal entry *************
     if ($this->recur_id > 0) {
         // if new record, will contain count, if edit will contain recur_id
         $first_id = $this->id;
         $first_post_date = $this->post_date;
         $first_purchase_invoice_id = $this->purchase_invoice_id;
         $first_terminal_date = $this->first_terminal_date;
         if ($this->id) {
             // it's an edit, fetch list of affected records to update if roll is enabled
             $affected_ids = $this->get_recur_ids($this->recur_id, $this->id);
             for ($i = 0; $i < count($affected_ids); $i++) {
                 $this->id = $affected_ids[$i]['id'];
                 $this->journal_main_array['id'] = $affected_ids[$i]['id'];
                 $this->remove_cogs_rows();
                 if ($i > 0) {
                     // Remove row id's for future posts, keep if re-posting single entry
                     for ($j = 0; $j < count($this->journal_rows); $j++) {
                         $this->journal_rows[$j]['id'] = '';
                     }
                     $this->post_date = $affected_ids[$i]['post_date'];
                     $this->terminal_date = $affected_ids[$i]['terminal_date'];
                     $this->purchase_invoice_id = $affected_ids[$i]['purchase_invoice_id'];
                 } else {
                     // for first entry, post date may be changed, use $_POST value
                     $this->post_date = $first_post_date;
                     $this->terminal_date = $first_terminal_date;
                     $this->purchase_invoice_id = $first_purchase_invoice_id;
                 }
                 $this->period = gen_calculate_period($this->post_date, true);
                 $this->journal_main_array['post_date'] = $this->post_date;
                 $this->journal_main_array['period'] = $this->period;
                 $this->journal_main_array['terminal_date'] = $this->terminal_date;
                 if (!$this->validate_purchase_invoice_id()) {
                     return false;
                 }
                 $messageStack->debug("\n\n  re-posting recur id = " . $this->id);
                 if (!$this->Post('edit')) {
                     return false;
                 }
                 // test for single post versus rolling into future posts, terminate loop if single post
                 if (!$this->recur_frequency) {
                     break;
                 }
             }
         } else {
             // it's an insert
             // fetch the next recur id
             $this->journal_main_array['recur_id'] = time();
             $day_offset = 0;
             $month_offset = 0;
             $year_offset = 0;
             $post_date = $this->post_date;
             for ($i = 0; $i < $this->recur_id; $i++) {
                 if (!$this->validate_purchase_invoice_id()) {
                     return false;
                 }
                 if (!$this->Post('insert')) {
                     return false;
                 }
                 $this->id = '';
                 $this->journal_main_array['id'] = $this->id;
                 $this->remove_cogs_rows();
                 for ($j = 0; $j < count($this->journal_rows); $j++) {
                     $this->journal_rows[$j]['id'] = '';
                 }
                 switch ($this->recur_frequency) {
                     default:
                     case '1':
                         $day_offset = ($i + 1) * 7;
                         break;
                         // Weekly
                     // Weekly
                     case '2':
                         $day_offset = ($i + 1) * 14;
                         break;
                         // Bi-weekly
                     // Bi-weekly
                     case '3':
                         $month_offset = ($i + 1) * 1;
                         break;
                         // Monthly
                     // Monthly
                     case '4':
                         $month_offset = ($i + 1) * 3;
                         break;
                         // Quarterly
                     // Quarterly
                     case '5':
                         $year_offset = ($i + 1) * 1;
                         break;
                         // Yearly
                 }
                 $this->post_date = gen_specific_date($post_date, $day_offset, $month_offset, $year_offset);
                 if ($this->terminal_date) {
                     $this->terminal_date = gen_specific_date($this->terminal_date, $day_offset, $month_offset, $year_offset);
                 }
                 $this->period = gen_calculate_period($this->post_date, true);
                 if (!$this->period && $i < $this->recur_id - 1) {
                     // recur falls outside of available periods, ignore last calculation
                     return $this->fail_message(ORD_PAST_LAST_PERIOD);
                 }
                 $this->journal_main_array['post_date'] = $this->post_date;
                 $this->journal_main_array['period'] = $this->period;
                 $this->journal_main_array['terminal_date'] = $this->terminal_date;
                 if (in_array($this->journal_id, array(4, 10, 12, 19)) && $first_purchase_invoice_id == '') {
                     $this->increment_purchase_invoice_id(true);
                 }
                 $this->purchase_invoice_id = string_increment($this->journal_main_array['purchase_invoice_id']);
             }
         }
         // restore the first values to continue with post process
         if (in_array($this->journal_id, array(4, 10, 12, 19)) && $first_purchase_invoice_id == '') {
             // special case for auto increment
             $first_purchase_invoice_id = $this->purchase_invoice_id;
         }
         $this->id = $first_id;
         $this->post_date = $first_post_date;
         $this->purchase_invoice_id = $first_purchase_invoice_id;
         $this->terminal_date = $first_terminal_date;
         $this->journal_main_array['id'] = $first_id;
         $this->journal_main_array['post_date'] = $first_post_date;
         $this->journal_main_array['purchase_invoice_id'] = $first_purchase_invoice_id;
         $this->journal_main_array['terminal_date'] = $first_terminal_date;
     } else {
         if (!$this->validate_purchase_invoice_id()) {
             return false;
         }
         if (!$this->Post($this->id ? 'edit' : 'insert')) {
             return false;
         }
     }
     // ************* post-POST processing *************
     switch ($this->journal_id) {
         case 3:
             // Purchase Quote Journal
         // Purchase Quote Journal
         case 4:
             // Purchase Order Journal
         // Purchase Order Journal
         case 7:
             // Vendor Credit Memo Journal
         // Vendor Credit Memo Journal
         case 9:
             // Sales Quote Journal
         // Sales Quote Journal
         case 10:
             // Sales Order Journal
         // Sales Order Journal
         case 12:
             // Sales/Invoice Journal
         // Sales/Invoice Journal
         case 13:
             // Customer Credit Memo Journal
             if ($this->purchase_invoice_id == '') {
                 // it's a new record, increment the po/so/inv to next number
                 if (!$this->increment_purchase_invoice_id()) {
                     return false;
                 }
             }
             break;
         case 6:
             // Purchase Journal
         // Purchase Journal
         default:
             break;
     }
     $messageStack->debug("\n  committed order post purchase_invoice_id = " . $this->purchase_invoice_id . " and id = " . $this->id);
     $db->transCommit();
     // finished successfully
     //echo 'committed transaction - bailing!'; exit();
     // ***************************** END TRANSACTION *******************************
     $messageStack->add(sprintf(TEXT_SUCCESSFULLY_POSTED, constant('ORD_HEADING_NUMBER_' . $this->journal_id) . ' ' . $this->purchase_invoice_id), 'success');
     return true;
 }
Ejemplo n.º 4
0
         $sql_data_array = array('qty' => $qty, 'priority' => $priority, 'notes' => $notes, 'closed' => $closed, 'close_date' => $close_date);
         $success = db_perform(TABLE_WO_JOURNAL_MAIN, $sql_data_array, 'update', 'id = ' . $id);
         if (!$success) {
             $error = true;
         }
     } else {
         $result = $db->Execute("select next_wo_num from " . TABLE_CURRENT_STATUS);
         $wo_num = $result->fields['next_wo_num'];
         $sql_data_array = array('wo_num' => $wo_num, 'sku_id' => $sku_id, 'qty' => $qty, 'wo_id' => $wo_id, 'priority' => $priority, 'wo_title' => $wo_title, 'post_date' => $post_date, 'notes' => $notes, 'closed' => $closed, 'close_date' => $close_date);
         $success = db_perform(TABLE_WO_JOURNAL_MAIN, $sql_data_array, 'insert');
         $id = db_insert_id();
         if (!$success) {
             $error = true;
         }
         if (!$error) {
             $result = $db->Execute("update " . TABLE_CURRENT_STATUS . " set next_wo_num = '" . string_increment($wo_num) . "'");
             $temp = $db->Execute("select allocate from " . TABLE_WO_MAIN . " where id = '" . $wo_id . "'");
             $allocate = $temp->fields['allocate'];
             if ($allocate) {
                 allocation_adjustment($sku_id, $qty);
             }
             // add allocation
         }
     }
 }
 // load and create the intial task list (only if new)
 if (!$error && !$_POST['id']) {
     $result = $db->Execute("select step, task_id  \n\t    from " . TABLE_WO_STEPS . " where ref_id = " . $wo_id . " order by step");
     $step_list = array();
     while (!$result->EOF) {
         $task = $db->Execute("select task_name, mfg, qa, data_entry from " . TABLE_WO_TASK . " where id = " . $result->fields['task_id'] . " limit 1");
Ejemplo n.º 5
0
 public function save_contact()
 {
     global $db;
     $fields = new contact_fields(false);
     $sql_data_array = $fields->what_to_save();
     $sql_data_array['type'] = $this->type;
     $sql_data_array['short_name'] = $this->short_name;
     $sql_data_array['inactive'] = isset($this->inactive) ? '1' : '0';
     $sql_data_array['contacts_level'] = $this->contacts_level;
     $sql_data_array['contact_first'] = $this->contact_first;
     $sql_data_array['contact_middle'] = $this->contact_middle;
     $sql_data_array['contact_last'] = $this->contact_last;
     $sql_data_array['store_id'] = $this->store_id;
     $sql_data_array['gl_type_account'] = is_array($this->gl_type_account) ? implode('', array_keys($this->gl_type_account)) : $this->gl_type_account;
     $sql_data_array['gov_id_number'] = $this->gov_id_number;
     $sql_data_array['dept_rep_id'] = $this->dept_rep_id;
     $sql_data_array['account_number'] = $this->account_number;
     $sql_data_array['special_terms'] = $this->special_terms;
     $sql_data_array['price_sheet'] = $this->price_sheet;
     $sql_data_array['tax_id'] = $this->tax_id;
     $sql_data_array['last_update'] = 'now()';
     if ($this->id == '') {
         //create record
         $sql_data_array['first_date'] = 'now()';
         db_perform(TABLE_CONTACTS, $sql_data_array, 'insert');
         $this->id = db_insert_id();
         //if auto-increment see if the next id is there and increment if so.
         if ($this->inc_auto_id) {
             // increment the ID value
             $next_id = string_increment($this->short_name);
             $db->Execute("update " . TABLE_CURRENT_STATUS . " set {$this->auto_field} = '{$next_id}'");
         }
         gen_add_audit_log(TEXT_CONTACTS . '-' . TEXT_ADD . '-' . constant('ACT_' . strtoupper($this->type) . '_TYPE_NAME'), $this->short_name);
     } else {
         // update record
         db_perform(TABLE_CONTACTS, $sql_data_array, 'update', "id = '{$this->id}'");
         gen_add_audit_log(TEXT_CONTACTS . '-' . TEXT_UPDATE . '-' . constant('ACT_' . strtoupper($this->type) . '_TYPE_NAME'), $this->short_name);
     }
 }
Ejemplo n.º 6
0
 if ($error == false) {
     $sql_data_array = array('type' => $type, 'short_name' => $short_name, 'inactive' => $inactive, 'contact_first' => $contact_first, 'contact_middle' => $contact_middle, 'contact_last' => $contact_last, 'store_id' => $store_id, 'gl_type_account' => $gl_type_account, 'gov_id_number' => $gov_id_number, 'dept_rep_id' => $dept_rep_id, 'account_number' => $account_number, 'special_terms' => $special_terms, 'price_sheet' => $price_sheet, 'last_update' => 'now()');
     // special case for contacts
     //		if ($type == 'i') { // convert dept_rep_id to record id
     //		  $result = $db->Execute("select id from " . TABLE_CONTACTS . " where short_name = '" . $dept_rep_id . "'");
     //		  $sql_data_array['dept_rep_id'] = ($result->RecordCount() > 0) ? $result->fields['id'] : 0;
     //		}
     if (!$id) {
         //create record
         $sql_data_array['first_date'] = 'now()';
         db_perform(TABLE_CONTACTS, $sql_data_array, 'insert');
         $id = db_insert_id();
         // if auto-increment see if the next id is there and increment if so.
         if ($inc_auto_id) {
             // increment the ID value
             $next_id = string_increment($short_name);
             $db->Execute("update " . TABLE_CURRENT_STATUS . " set " . $auto_field . " = '" . $next_id . "'");
         }
         gen_add_audit_log(ACT_LOG_ADD_ACCOUNT . constant('ACT_' . strtoupper($type) . '_TYPE_NAME'), $short_name);
     } else {
         // update record
         db_perform(TABLE_CONTACTS, $sql_data_array, 'update', "id = '" . $id . "'");
         gen_add_audit_log(ACT_LOG_UPDATE_ACCOUNT . constant('ACT_' . strtoupper($type) . '_TYPE_NAME'), $short_name);
     }
     // contact main record
     if ($type != 'i' && $i_short_name) {
         $sql_data_array = array('type' => 'i', 'short_name' => $i_short_name, 'contact_first' => $i_contact_first, 'contact_middle' => $i_contact_middle, 'contact_last' => $i_contact_last, 'gov_id_number' => $i_gov_id_number, 'account_number' => $i_account_number, 'dept_rep_id' => $id, 'last_update' => 'now()');
         if (!$i_id) {
             //create record
             $sql_data_array['first_date'] = 'now()';
             db_perform(TABLE_CONTACTS, $sql_data_array, 'insert');
Ejemplo n.º 7
0
         if ($id) {
             if ($success = db_perform(TABLE_CAPA, $sql_data_array, 'update', 'id = ' . $id)) {
                 gen_add_audit_log(CAPA_LOG_USER_UPDATE . $_POST['capa_num']);
                 $capa_num = $_POST['capa_num'];
             } else {
                 $error = true;
             }
         } else {
             // fetch the CAPA number
             $result = $db->Execute("select next_capa_num from " . TABLE_CURRENT_STATUS);
             $capa_num = $result->fields['next_capa_num'];
             $sql_data_array['capa_num'] = $capa_num;
             $success = db_perform(TABLE_CAPA, $sql_data_array, 'insert');
             if ($success) {
                 $id = db_insert_id();
                 $next_num = string_increment($capa_num);
                 $db->Execute("update " . TABLE_CURRENT_STATUS . " set next_capa_num = '" . $next_num . "'");
                 gen_add_audit_log(CAPA_LOG_USER_ADD . $capa_num);
             } else {
                 $error = true;
             }
         }
         if (!$error) {
             $messageStack->add(($_POST['rowSeq'] ? CAPA_MESSAGE_SUCCESS_UPDATE : CAPA_MESSAGE_SUCCESS_ADD) . $capa_num, 'success');
         } else {
             $messageStack->add(CAPA_MESSAGE_ERROR, 'error');
         }
     }
     break;
 case 'edit':
     $id = db_prepare_input($_POST['rowSeq']);
Ejemplo n.º 8
0
 function add_account($type, $acct_id = 0, $address_id = 0, $allow_overwrite = false)
 {
     global $db;
     $acct_type = substr($type, 0, 1);
     switch (substr($type, 1, 1)) {
         case 'b':
         case 'm':
             $add_type = 'bill';
             break;
         case 's':
             $add_type = 'ship';
             break;
         default:
             return $this->fail_message('Bad account type: ' . $type . ' passed to gen_ledger/classes/gen_ledger.php (add_account)');
     }
     if ($add_type == 'bill' || $this->drop_ship) {
         // update or insert new account record, else skip to add address
         $short_name = $add_type == 'bill' ? $this->short_name : $this->ship_short_name;
         $auto_type = false;
         $auto_field = '';
         if (!$short_name && (AUTO_INC_CUST_ID || AUTO_INC_VEND_ID)) {
             switch ($acct_type) {
                 case 'c':
                     // customers
                     $auto_type = AUTO_INC_CUST_ID;
                     $auto_field = 'next_cust_id_num';
                     break;
                 case 'v':
                     // vendors
                     $auto_type = AUTO_INC_VEND_ID;
                     $auto_field = 'next_vend_id_num';
                     break;
             }
             if ($auto_type) {
                 $result = $db->Execute("select " . $auto_field . " from " . TABLE_CURRENT_STATUS);
                 $short_name = $result->fields[$auto_field];
             }
         }
         if (!$short_name) {
             return $this->fail_message(ACT_ERROR_NO_ACCOUNT_ID);
         }
         // it id exists, fetch the data, else check for duplicates
         $sql = "select id, store_id, dept_rep_id from " . TABLE_CONTACTS . " where ";
         $sql .= $acct_id ? "id = " . (int) $acct_id : "short_name = '" . $short_name . "' and type = '" . $acct_type . "'";
         $result = $db->Execute($sql);
         if (!$acct_id && $result->RecordCount() > 0 && !$allow_overwrite) {
             // duplicate ID w/o allow_overwrite
             return $this->fail_message(ACT_ERROR_DUPLICATE_ACCOUNT);
         }
         $acct_id = $result->fields['id'];
         // will only change if no id was passed and allow_overwrite is true
         $sql_data_array = array();
         $sql_data_array['last_update'] = 'now()';
         $sql_data_array['store_id'] = isset($this->store_id) ? $this->store_id : $result->fields['store_id'];
         $sql_data_array['dept_rep_id'] = isset($this->dept_rep_id) ? $this->dept_rep_id : $result->fields['dept_rep_id'];
         if ($result->RecordCount() == 0) {
             // new account
             $sql_data_array['type'] = $acct_type;
             $sql_data_array['short_name'] = $short_name;
             $sql_data_array['gl_type_account'] = DEF_INV_GL_ACCT;
             $sql_data_array['first_date'] = 'now()';
             db_perform(TABLE_CONTACTS, $sql_data_array, 'insert');
             $acct_id = db_insert_id();
             $force_mail_address = true;
             if ($auto_type) {
                 $contact_id = $db->Execute("select " . $auto_field . " from " . TABLE_CURRENT_STATUS);
                 $auto_id = $contact_id->fields[$auto_field];
                 if ($auto_id == $short_name) {
                     // increment the ID value
                     $next_id = string_increment($auto_id);
                     $db->Execute("update " . TABLE_CURRENT_STATUS . " set " . $auto_field . " = '" . $next_id . "'");
                 }
             }
         } else {
             // duplicate ID with allow_overwrite
             db_perform(TABLE_CONTACTS, $sql_data_array, 'update', 'id = ' . (int) $acct_id);
             $force_mail_address = false;
         }
     }
     // address book fields
     $sql_data_array = array();
     if (!$address_id) {
         // check for the address already there using criteria_fields to match
         $criteria_fields = array('primary_name', 'address1', 'postal_code');
         $sql = "select address_id from " . TABLE_ADDRESS_BOOK . " where ";
         foreach ($criteria_fields as $name) {
             $field_to_test = $add_type . '_' . $name;
             $sql .= $name . " = '" . db_input($this->{$field_to_test}) . "' and ";
         }
         $sql .= "ref_id = " . $acct_id;
         $result = $db->Execute($sql);
         $address_id = $result->RecordCount() > 0 ? $result->fields['address_id'] : '';
     }
     $add_fields = array('primary_name', 'contact', 'address1', 'address2', 'city_town', 'state_province', 'postal_code', 'country_code', 'telephone1', 'telephone2', 'telephone3', 'telephone4', 'email', 'website');
     foreach ($add_fields as $name) {
         $field_to_test = $add_type . '_' . $name;
         if (isset($this->{$field_to_test})) {
             $sql_data_array[$name] = $this->{$field_to_test};
         }
     }
     $sql_data_array['ref_id'] = $acct_id;
     if (!$address_id) {
         // create new address
         $sql_data_array['type'] = $force_mail_address ? $acct_type . 'm' : $type;
         db_perform(TABLE_ADDRESS_BOOK, $sql_data_array, 'insert');
         $address_id = db_insert_id();
     } else {
         // then update address
         db_perform(TABLE_ADDRESS_BOOK, $sql_data_array, 'update', 'address_id = ' . (int) $address_id);
     }
     // update the journal_main array since we could have new id's
     switch ($add_type) {
         case 'mail':
         case 'bill':
             $this->journal_main_array['bill_acct_id'] = $acct_id;
             $this->journal_main_array['bill_address_id'] = $address_id;
             break;
         case 'ship':
             $this->journal_main_array['ship_acct_id'] = $acct_id;
             $this->journal_main_array['ship_address_id'] = $address_id;
             break;
         default:
     }
     return $acct_id;
     // should be either passed id or new id if record was created
 }