public function delete_selected()
 {
     if (!$this->checkParams('session_data_key')) {
         $this->dataError();
         sendBack();
     }
     $flash = Flash::Instance();
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     $session_data_key = $this->_data['session_data_key'];
     $page_data = new SessionData($session_data_key);
     foreach ($this->_data[$this->modeltype] as $id => $fields) {
         if (!isset($fields['select']) && isset($fields['_checkbox_exists_select'])) {
             $page_data->deletePageData($id);
         } else {
             $page_data->updatePageData($id, $fields, $errors);
         }
     }
     $data = $page_data->getPageData();
     // Could do with a progress bar here as the number of records could be large
     $delete_count = 0;
     if (count($data) > 0) {
         $progressBar = new Progressbar('soproductline_delete_unused');
         $callback = function ($fields, $id) use(&$delete_count) {
             if ($fields['select'] == 'on') {
                 $productline = DataObjectFactory::Factory('SOProductLine');
                 $productline->load($id);
                 if (!$productline->isLoaded() || !$productline->delete($id, $errors)) {
                     return FALSE;
                 }
                 $delete_count++;
             }
         };
         if ($progressBar->process($data, $callback) === FALSE) {
             $errors[] = 'Failed to delete product line';
         }
     } else {
         $flash->addWarning('Nothing selected to delete');
     }
     // reset timeout to 30 seconds to allow time to redisplay the page
     // hopefully, it will be quicker than this!
     set_time_limit(30);
     if (count($errors) > 0) {
         $flash->addErrors($errors);
         $flash->addError($db->ErrorMsg());
         $db->FailTrans();
         $db->CompleteTrans();
         $this->refresh();
     } else {
         $page_data->clear();
         $db->CompleteTrans();
         $flash->addMessage($delete_count . ' record' . get_plural_string($delete_count) . ' archived successfully');
         sendTo($this->name, 'unused', $this->_modules);
     }
 }
Example #2
0
 public function recalcLatestCosts()
 {
     $flash = Flash::Instance();
     $db = DB::Instance();
     $db->StartTrans();
     $errors = array();
     $stitems_done = array();
     $stitem_ids = array_keys(STItem::nonObsoleteItems());
     $max_depth = 5;
     $max_parents = 5;
     $progressBar = new Progressbar('recalclatestcosts');
     $callback = function ($stitem_id, $id) use(&$stitems_done, &$errors) {
         if (in_array($stitem_id, $stitems_done)) {
             return;
         }
         $stitem = DataObjectFactory::Factory('STItem');
         if (!$stitem->load($stitem_id)) {
             return FALSE;
         }
         $parent = null;
         $num_parents = 0;
         do {
             if ($parent) {
                 $stitem = $parent;
             }
             $parent = null;
             $parents = $stitem->getParents();
             if (count($parents) > 0) {
                 list($parent) = $parents;
             }
             $num_parents++;
         } while ($parent && $num_parents <= $max_parents);
         $tree_array = $stitem->getTreeArray($max_depth);
         // Gets child nodes first
         $array_iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($tree_array), 2);
         foreach ($array_iterator as $id => $children) {
             if (in_array($id, $stitems_done)) {
                 return;
             }
             $stitem = DataObjectFactory::Factory('STItem');
             if (!$stitem->load($id)) {
                 return FALSE;
             }
             $stitems_done[] = $id;
             $old_costs = array($stitem->latest_cost, $stitem->latest_mat, $stitem->latest_lab, $stitem->latest_osc, $stitem->latest_ohd);
             $stitem->calcLatestCost();
             $new_costs = array($stitem->latest_cost, $stitem->latest_mat, $stitem->latest_lab, $stitem->latest_osc, $stitem->latest_ohd);
             $equal_costs = true;
             $total_costs = count($old_costs);
             for ($i = 0; $i < $total_costs; $i++) {
                 if (bccomp($old_costs[$i], $new_costs[$i], $stitem->cost_decimals) != 0) {
                     $equal_costs = false;
                     break;
                 }
             }
             if ($equal_costs) {
                 return;
             }
             if (!$stitem->saveCosts() || !STCost::saveItemCost($stitem)) {
                 return FALSE;
             }
         }
     };
     if ($progressBar->process($stitem_ids, $callback) === FALSE) {
         $errors[] = 'Could not re-calculate stock item costs';
         $db->FailTrans();
     }
     $db->CompleteTrans();
     if (count($errors) == 0) {
         $flash->addMessage('Stock item costs re-calculated');
         sendTo('Index', 'index', $this->_modules);
     } else {
         $flash->addErrors($errors);
         sendBack();
     }
 }