function processInventory($upload_name)
 {
     global $coa, $db, $currencies, $messageStack;
     if (!$this->cyberParse($upload_name)) {
         return false;
     }
     $post_date = gen_specific_date(date('Y-m-d'), $day_offset = -1);
     $glEntry = new journal();
     $sku_list = array();
     $coa_list = array();
     $affected_accounts = array();
     for ($row_id = 0, $j = 2; $row_id < count($this->records); $row_id++, $j++) {
         $row = $this->records[$row_id];
         $total_amount = $currencies->clean_value($row['total_amount']);
         $qty = $currencies->clean_value($row['quantity']);
         // check for errors and report/exit if error found
         if (!gen_validate_sku($row['sku'])) {
             $messageStack->add(GL_ERROR_UPDATING_INVENTORY_STATUS . $row['sku'] . GL_BEG_BAL_ERROR_0 . $j, 'error');
             return false;
         }
         if (!in_array($row['inv_gl_acct'], $coa) || !in_array($row['gl_acct'], $coa)) {
             $messageStack->add(GL_BEG_BAL_ERROR_6 . $j, 'error');
             return false;
         }
         if ($qty == 0) {
             $messageStack->add(GL_BEG_BAL_ERROR_7 . $j, 'caution');
         } else {
             $affected_accounts[$row['inv_gl_acct']] = true;
             // need list of accounts to update history
             $affected_accounts[$row['gl_acct']] = true;
             // both credit and debit
             $sku_list[$row['sku']]['qty'] += $qty;
             // load quantity indexed by sku
             $sku_list[$row['sku']]['total'] += $total_amount;
             // load total_value indexed by sku
             $coa_list[$row['inv_gl_acct']] += $total_amount;
             // add to debit total by coa
             $coa_list[$row['gl_acct']] -= $total_amount;
             // add to credit total by coa
         }
     }
     if (is_array($sku_list)) {
         $glEntry->affected_accounts = $affected_accounts;
         // *************** START TRANSACTION *************************
         $db->transStart();
         // update inventory balances on hand
         foreach ($sku_list as $sku => $details) {
             $sql = "update " . TABLE_INVENTORY . " set quantity_on_hand = quantity_on_hand + {$details['qty']} where sku = '{$sku}'";
             $result = $db->Execute($sql);
             if ($result->AffectedRows() != 1) {
                 $messageStack->add(sprintf(GL_BEG_BAL_ERROR_8, $sku), 'error');
                 $db->transRollback();
                 return false;
             }
             $history_array = array('ref_id' => 0, 'sku' => $sku, 'qty' => $details['qty'], 'remaining' => $details['qty'], 'unit_cost' => $details['total'] / $details['qty'], 'avg_cost' => $details['total'] / $details['qty'], 'post_date' => $post_date);
             $result = db_perform(TABLE_INVENTORY_HISTORY, $history_array, 'insert');
         }
         // update chart of account beginning balances for period 1
         foreach ($coa_list as $account => $amount) {
             $sql = "update " . TABLE_CHART_OF_ACCOUNTS_HISTORY . " set beginning_balance = beginning_balance + {$amount}\n\t\t\twhere account_id = '{$account}' and period = 1";
             $result = $db->Execute($sql);
             if ($result->AffectedRows() != 1) {
                 $messageStack->add(sprintf(GL_BEG_BAL_ERROR_9, $account), 'error');
                 $db->transRollback();
                 return false;
             }
         }
         // update the chart of accounts history through the existing periods
         if (!$glEntry->update_chart_history_periods($period = 1)) {
             if (DEBUG) {
                 $messageStack->write_debug();
             }
             return $glEntry->fail_message(GL_ERROR_UPDATE_COA_HISTORY);
         }
         $db->transCommit();
         // post the chart of account values
         if (DEBUG) {
             $messageStack->write_debug();
         }
         // *************** END TRANSACTION *************************
     }
     $this->line_count = $row_id;
     return true;
 }
 if ($inventory_type == 'ms') {
     $attributes = array('attr_name_0' => db_prepare_input($_POST['attr_name_0']), 'ms_attr_0' => substr(db_prepare_input($_POST['ms_attr_0']), 0, -1), 'attr_name_1' => db_prepare_input($_POST['attr_name_1']), 'ms_attr_1' => substr(db_prepare_input($_POST['ms_attr_1']), 0, -1));
     save_ms_items($sql_data_array, $attributes);
 }
 // if it's an assembly, retrieve the BOM (if no journal_entries have been posted)
 $update_bom = false;
 if ($inventory_type == 'as' || $inventory_type == 'sa') {
     $result = $db->Execute("select last_journal_date from " . TABLE_INVENTORY . " where id = " . $id);
     if ($result->fields['last_journal_date'] == '0000-00-00 00:00:00') {
         // only update if no posting has been performed
         $x = 1;
         $bom_array = array();
         for ($x = 0; $x < count($_POST['assy_sku']); $x++) {
             $assy_sku = db_prepare_input($_POST['assy_sku'][$x]);
             $assy_qty = $currencies->clean_value(db_prepare_input($_POST['assy_qty'][$x]));
             if (gen_validate_sku($assy_sku) && $assy_qty > 0) {
                 // error check sku is valid and qty > 0
                 $bom_array[] = array('ref_id' => $id, 'sku' => $assy_sku, 'description' => db_prepare_input($_POST['assy_desc'][$x]), 'qty' => $assy_qty);
             } elseif ($assy_sku != '' || $assy_qty < 0) {
                 // show error, bad sku, negative quantity, skip the blank lines
                 $messageStack->add(INV_ERROR_BAD_SKU . $assy_sku, 'error');
             }
         }
         $update_bom = true;
     }
 }
 $img = $db->Execute("select image_with_path from " . TABLE_INVENTORY . " where id = " . $id);
 $file_path = DIR_FS_MY_FILES . $_SESSION['company'] . '/inventory/images';
 if ($remove_image) {
     // update the image with relative path
     if ($img->fields['image_with_path'] && file_exists($file_path . '/' . $img->fields['image_with_path'])) {
Beispiel #3
0
 function rename($id, $newSku)
 {
     global $db, $messageStack;
     if (!$newSku) {
         $newSku = $this->next_sku;
     }
     if (!$newSku) {
         $messageStack->add(INV_ERROR_SKU_BLANK, 'error');
         return false;
     }
     if (gen_validate_sku($newSku)) {
         $messageStack->add(INV_ERROR_DUPLICATE_SKU, 'error');
         return false;
     }
     if (isset($id)) {
         $this->get_item_by_id($id);
     }
     $sku_list = array($this->sku);
     if (isset($this->edit_ms_list) && $this->edit_ms_list == true) {
         // build list of sku's to rename (without changing contents)
         $result = $db->Execute("select sku from " . TABLE_INVENTORY . " where sku like '" . $this->sku . "-%'");
         while (!$result->EOF) {
             $sku_list[] = $result->fields['sku'];
             $result->MoveNext();
         }
     }
     // start transaction (needs to all work or reset to avoid unsyncing tables)
     $db->transStart();
     // rename the afffected tables
     for ($i = 0; $i < count($sku_list); $i++) {
         $new_sku = str_replace($this->sku, $newSku, $sku_list[$i], $count = 1);
         $result = $db->Execute("update " . TABLE_INVENTORY . " set sku = '" . $new_sku . "' where sku = '" . $sku_list[$i] . "'");
         $result = $db->Execute("update " . TABLE_INVENTORY_ASSY_LIST . " set sku = '" . $new_sku . "' where sku = '" . $sku_list[$i] . "'");
         $result = $db->Execute("update " . TABLE_INVENTORY_COGS_OWED . " set sku = '" . $new_sku . "' where sku = '" . $sku_list[$i] . "'");
         $result = $db->Execute("update " . TABLE_INVENTORY_HISTORY . " set sku = '" . $new_sku . "' where sku = '" . $sku_list[$i] . "'");
         $result = $db->Execute("update " . TABLE_INVENTORY_MS_LIST . " set sku = '" . $new_sku . "' where sku = '" . $sku_list[$i] . "'");
         $result = $db->Execute("update " . TABLE_JOURNAL_ITEM . " set sku = '" . $new_sku . "' where sku = '" . $sku_list[$i] . "'");
         $result = $db->Execute("update " . TABLE_INVENTORY_PURCHASE . " set sku = '" . $new_sku . "' where sku = '" . $sku_list[$i] . "'");
     }
     $db->transCommit();
     return true;
 }
     break;
 case 'create':
     require DIR_FS_MODULES . 'install/functions/install.php';
     if ($security_level < 2) {
         $messageStack->add_session(ERROR_NO_PERMISSION, 'error');
         gen_redirect(html_href_link(FILENAME_DEFAULT, gen_get_all_get_params(array('action')), 'SSL'));
         break;
     }
     $asset_id = db_prepare_input($_POST['asset_id']);
     $asset_type = db_prepare_input($_POST['asset_type']);
     if (!$asset_id) {
         $messageStack->add(ASSETS_ERROR_SKU_BLANK, 'error');
         $action = 'new';
         break;
     }
     if (gen_validate_sku($asset_id)) {
         $messageStack->add(ASSETS_ERROR_DUPLICATE_SKU, 'error');
         $action = 'new';
         break;
     }
     $sql_data_array = array('asset_id' => $asset_id, 'asset_type' => $asset_type, 'acquisition_date' => 'now()');
     switch ($asset_type) {
         case 'vh':
             $sql_data_array['account_asset'] = best_acct_guess(8, TEXT_VEHICLE, '');
             $sql_data_array['account_depreciation'] = best_acct_guess(10, TEXT_VEHICLE, '');
             $sql_data_array['account_maintenance'] = best_acct_guess(34, TEXT_VEHICLE, '');
             break;
         case 'bd':
             $sql_data_array['account_asset'] = best_acct_guess(8, TEXT_BUILDING, '');
             $sql_data_array['account_depreciation'] = best_acct_guess(10, TEXT_BUILDING, '');
             $sql_data_array['account_maintenance'] = best_acct_guess(34, TEXT_BUILDING, '');