示例#1
0
 function save()
 {
     global $db, $messageStack, $security_level, $currencies;
     $bom_list = array();
     for ($x = 0; $x < count($_POST['assy_sku']); $x++) {
         $bom_list[$x] = array('ref_id' => $this->id, 'sku' => db_prepare_input($_POST['assy_sku'][$x]), 'description' => db_prepare_input($_POST['assy_desc'][$x]), 'qty' => $currencies->clean_value(db_prepare_input($_POST['assy_qty'][$x])));
         $result = $db->Execute("select id from " . TABLE_INVENTORY . " where sku = '" . $_POST['assy_sku'][$x] . "'");
         if (($result->RecordCount() == 0 || $currencies->clean_value($_POST['assy_qty'][$x]) == 0) && ($_POST['assy_sku'][$x] = !'')) {
             // show error, bad sku, negative quantity. error check sku is valid and qty > 0
             $error = $messageStack->add(INV_ERROR_BAD_SKU . db_prepare_input($_POST['assy_sku'][$x]), 'error');
         } else {
             $prices = inv_calculate_sales_price(abs($this->bom[$x]['qty']), $result->fields['id'], 0, 'v');
             $bom_list[$x]['item_cost'] = strval($prices['price']);
             $prices = inv_calculate_sales_price(abs($this->bom[$x]['qty']), $result->fields['id'], 0, 'c');
             $bom_list[$x]['full_price'] = strval($prices['price']);
         }
     }
     $this->bom = $bom_list;
     $current_situation = $db->Execute("select * from " . TABLE_INVENTORY . " where id = '" . $this->id . "'");
     $sql_data_array = parent::save();
     if ($sql_data_array == false) {
         return false;
     }
     $result = $db->Execute("select last_journal_date, quantity_on_hand  from " . TABLE_INVENTORY . " where id = " . $this->id);
     $this->allow_edit_bom = ($result->fields['last_journal_date'] == '0000-00-00 00:00:00' || $result->fields['last_journal_date'] == '') && ($result->fields['quantity_on_hand'] == 0 || $result->fields['quantity_on_hand'] == '') ? true : false;
     if ($error) {
         return false;
     }
     if ($this->allow_edit_bom == true) {
         // only update if no posting has been performed
         $result = $db->Execute("delete from " . TABLE_INVENTORY_ASSY_LIST . " where ref_id = " . $this->id);
         foreach ($bom_list as $list_array) {
             unset($list_array['item_cost']);
             unset($list_array['full_price']);
             db_perform(TABLE_INVENTORY_ASSY_LIST, $list_array, 'insert');
         }
     }
     $sql_data_array['inventory_type'] = 'ia';
     // 	split attributes
     $attr0 = array();
     $attr1 = array();
     if ($this->ms_attr_0 != '') {
         $attr0 = explode(',', $this->ms_attr_0);
     }
     if ($this->ms_attr_1 != '') {
         $attr1 = explode(',', $this->ms_attr_1);
     }
     if (empty($attr0)) {
         $this->get_ms_list();
         return false;
         // no attributes, nothing to do
     }
     // build skus
     $sku_list = array();
     for ($i = 0; $i < count($attr0); $i++) {
         $idx0 = explode(':', $attr0[$i]);
         if (empty($attr1)) {
             if ($idx0[0] != '') {
                 $sku_list[] = $this->sku . '-' . $idx0[0];
                 $variables[$this->sku . '-' . $idx0[0]]['idx0'] = $idx0[1];
             }
         } else {
             for ($j = 0; $j < count($attr1); $j++) {
                 $idx1 = explode(':', $attr1[$j]);
                 if ($idx0[0] != '' && $idx1[0] != '') {
                     $sku_list[] = $this->sku . '-' . $idx0[0] . $idx1[0];
                     $variables[$this->sku . '-' . $idx0[0] . $idx1[0]]['idx0'] = $idx0[1];
                     $variables[$this->sku . '-' . $idx0[0] . $idx1[0]]['idx1'] = $idx1[1];
                 }
             }
         }
     }
     // either update, delete or insert sub skus depending on sku list
     $result = $db->Execute("select sku from " . TABLE_INVENTORY . " where inventory_type = 'ia' and sku like '" . $this->sku . "-%'");
     $existing_sku_list = array();
     while (!$result->EOF) {
         $existing_sku_list[] = $result->fields['sku'];
         $result->MoveNext();
     }
     $delete_list = array_diff($existing_sku_list, $sku_list);
     $update_list = array_intersect($existing_sku_list, $sku_list);
     $insert_list = array_diff($sku_list, $update_list);
     foreach ($insert_list as $sku) {
         // first insert new sku's with all fields
         $sql_data_array['sku'] = $sku;
         $sql_data_array['description_short'] = sprintf($this->description_short, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         $sql_data_array['description_purchase'] = sprintf($this->description_purchase, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         $sql_data_array['description_sales'] = sprintf($this->description_sales, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         db_perform(TABLE_INVENTORY, $sql_data_array, 'insert');
         $new_id = db_insert_id();
         foreach ($this->purchase_array as $purchase_row) {
             $purchase_data_array = array('sku' => $sku, 'vendor_id' => $purchase_row['vendor_id'], 'description_purchase' => sprintf($purchase_row['description_purchase'], $variables[$sku]['idx0'], $variables[$sku]['idx1']), 'item_cost' => $purchase_row['item_cost'], 'purch_package_quantity' => $purchase_row['purch_package_quantity'], 'purch_taxable' => $purchase_row['purch_taxable'], 'price_sheet_v' => $purchase_row['price_sheet_v']);
             db_perform(TABLE_INVENTORY_PURCHASE, $purchase_data_array, 'insert');
         }
         foreach ($bom_list as $list_array) {
             $list_array['ref_id'] = $new_id;
             unset($list_array['item_cost']);
             unset($list_array['full_price']);
             db_perform(TABLE_INVENTORY_ASSY_LIST, $list_array, 'insert');
         }
     }
     if ($this->id != '') {
         //only update fields that are changed otherwise fields in the child could be overwritten
         foreach ($current_situation->fields as $key => $value) {
             // remove fields where the parent is unchanged because the childeren could have different values in these fields.
             switch ($key) {
                 case 'description_short':
                     if ($this->description_short == $value) {
                         unset($sql_data_array[$key]);
                     }
                     break;
                 case 'description_purchase':
                     if ($this->description_purchase == $value) {
                         unset($sql_data_array[$key]);
                     }
                     break;
                 case 'description_sales':
                     if ($this->description_sales == $value) {
                         unset($sql_data_array[$key]);
                     }
                     break;
                 default:
                     if ($sql_data_array[$key] == $value) {
                         unset($sql_data_array[$key]);
                     }
             }
         }
     }
     foreach ($update_list as $sku) {
         //update with reduced number of fields.
         $sql_data_array['sku'] = $sku;
         if (isset($sql_data_array['description_short'])) {
             $sql_data_array['description_short'] = sprintf($this->description_short, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         }
         if (isset($sql_data_array['description_purchase'])) {
             $sql_data_array['description_purchase'] = sprintf($this->description_purchase, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         }
         if (isset($sql_data_array['description_sales'])) {
             $sql_data_array['description_sales'] = sprintf($this->description_sales, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         }
         db_perform(TABLE_INVENTORY, $sql_data_array, 'update', "sku = '" . $sku . "'");
         foreach ($this->backup_purchase_array as $backUpKey => $backUpRow) {
             $backUpRow['description_purchase'] = sprintf($backUpRow['description_purchase'], $variables[$sku]['idx0'], $variables[$sku]['idx1']);
             $purchase_data_array = null;
             if ($backUpRow['action'] == 'delete') {
                 $result = $db->Execute("delete from " . TABLE_INVENTORY_PURCHASE . " where sku = '" . $sku . "' and vendor_id = '" . $backUpRow['vendor_id'] . "'");
             } else {
                 if ($backUpRow['action'] == 'insert') {
                     $purchase_data_array = $backUpRow;
                     unset($purchase_data_array['id']);
                     unset($purchase_data_array['action']);
                     $purchase_data_array['sku'] = $sku;
                     db_perform(TABLE_INVENTORY_PURCHASE, $purchase_data_array, 'insert');
                 } else {
                     /*on purpose removed this part because iam not sure what to update and what not 
                     		 * $purchase_data_array = $backUpRow;
                     		unset($purchase_data_array['id']);
                     		unset($purchase_data_array['action']);
                     		foreach($backUpRow as $key => $value) {
                     			if($key == 'action' || $key == 'id' ) break;
                     			if($this->purchase_array[$backUpKey][$key] == $value){
                     				unset($purchase_data_array[$key]);
                     			}
                     		}
                     		db_perform(TABLE_INVENTORY_PURCHASE, $purchase_data_array, 'update', "sku = '" . $sku. "' and vendor_id = '".$backUpRow['vendor_id']."'");*/
                 }
             }
         }
         $result = $db->Execute("select id, last_journal_date, quantity_on_hand  from " . TABLE_INVENTORY . " where sku = '" . $sku . "'");
         $this->allow_edit_bom = ($result->fields['last_journal_date'] == '0000-00-00 00:00:00' || $result->fields['last_journal_date'] == '') && ($result->fields['quantity_on_hand'] == 0 || $result->fields['quantity_on_hand'] == '') ? true : false;
         if ($this->allow_edit_bom == true) {
             // only update if no posting has been performed
             $temp = $db->Execute("delete from " . TABLE_INVENTORY_ASSY_LIST . " where ref_id = " . $result->fields['id']);
             foreach ($bom_list as $list_array) {
                 $list_array['ref_id'] = $result->fields['id'];
                 unset($list_array['item_cost']);
                 unset($list_array['full_price']);
                 db_perform(TABLE_INVENTORY_ASSY_LIST, $list_array, 'insert');
             }
         }
     }
     if (count($delete_list) && $security_level < 4) {
         $messageStack->add(ERROR_NO_PERMISSION, 'error');
         $this->get_ms_list();
         return false;
     }
     foreach ($delete_list as $sku) {
         $temp = $this->ia_check_remove($sku);
         if ($temp === true) {
             $result = $db->Execute("delete from " . TABLE_INVENTORY . " where sku = '" . $sku . "'");
             $result = $db->Execute("delete from " . TABLE_INVENTORY_PURCHASE . " where sku = '" . $sku . "'");
             $result = $db->Execute("select id, last_journal_date, quantity_on_hand  from " . TABLE_INVENTORY . " where sku = '" . $sku . "'");
             $temp = $db->Execute("delete from " . TABLE_INVENTORY_ASSY_LIST . " where ref_id = " . $result->fields['id']);
         } elseif ($temp === false) {
             $result = $db->Execute("update " . TABLE_INVENTORY . " set inactive = '1' where sku = '" . $sku . "'");
         }
     }
     // update/insert into inventory_ms_list table
     $result = $db->Execute("select id from " . TABLE_INVENTORY_MS_LIST . " where sku = '" . $this->sku . "'");
     $exists = $result->RecordCount();
     $data_array = array('sku' => $this->sku, 'attr_0' => $this->ms_attr_0, 'attr_name_0' => $this->attr_name_0, 'attr_1' => $this->ms_attr_1, 'attr_name_1' => $this->attr_name_1);
     if ($exists) {
         db_perform(TABLE_INVENTORY_MS_LIST, $data_array, 'update', "id = " . $result->fields['id']);
     } else {
         db_perform(TABLE_INVENTORY_MS_LIST, $data_array, 'insert');
     }
     $this->get_ms_list();
     return true;
 }
示例#2
0
 public function create()
 {
     if ($this->data['users']->role != "admin") {
         exit;
     }
     $inventory = new inventory();
     $inventory->name = Input::get('name');
     $inventory->qty = Input::get('qty');
     $inventory->teacherId = Input::get('teacherId');
     $inventory->inventorydate = Input::get('inventorydate');
     $inventory->total = Input::get('total');
     $inventory->status = Input::get('status');
     $inventory->save();
     $teachername = User::where('role', 'teacher')->where('activated', '1')->where('id', $inventory->teacherId)->first();
     $inventory->teachername = $teachername->fullName;
     return $this->panelInit->apiOutput(true, 'Add Inventory', 'Inventory Added', $inventory->toArray());
     //return json_encode(array("jsTitle"=>'Add Inventory',"jsMessage"=>"Inventory Added","list"=>$this->listAll() ));
 }
示例#3
0
 function save()
 {
     global $db, $currencies, $messageStack;
     $bom_list = array();
     for ($x = 0; $x < count($_POST['assy_sku']); $x++) {
         $bom_list[$x] = array('ref_id' => $this->id, 'sku' => db_prepare_input($_POST['assy_sku'][$x]), 'description' => db_prepare_input($_POST['assy_desc'][$x]), 'qty' => $currencies->clean_value(db_prepare_input($_POST['assy_qty'][$x])));
         $result = $db->Execute("select id from " . TABLE_INVENTORY . " where sku = '" . $_POST['assy_sku'][$x] . "'");
         if (($result->RecordCount() == 0 || $currencies->clean_value($_POST['assy_qty'][$x]) == 0) && ($_POST['assy_sku'][$x] = !'')) {
             // show error, bad sku, negative quantity. error check sku is valid and qty > 0
             $error = $messageStack->add(INV_ERROR_BAD_SKU . db_prepare_input($_POST['assy_sku'][$x]), 'error');
         } else {
             $prices = inv_calculate_sales_price(abs($this->bom[$x]['qty']), $result->fields['id'], 0, 'v');
             $bom_list[$x]['item_cost'] = strval($prices['price']);
             $prices = inv_calculate_sales_price(abs($this->bom[$x]['qty']), $result->fields['id'], 0, 'c');
             $bom_list[$x]['full_price'] = strval($prices['price']);
         }
     }
     $this->bom = $bom_list;
     if (!parent::save()) {
         return false;
     }
     $result = $db->Execute("select last_journal_date, quantity_on_hand  from " . TABLE_INVENTORY . " where id = " . $this->id);
     $this->allow_edit_bom = ($result->fields['last_journal_date'] == '0000-00-00 00:00:00' || $result->fields['last_journal_date'] == '') && ($result->fields['quantity_on_hand'] == 0 || $result->fields['quantity_on_hand'] == '') ? true : false;
     if ($error) {
         return false;
     }
     if ($this->allow_edit_bom == true) {
         // only update if no posting has been performed
         $result = $db->Execute("delete from " . TABLE_INVENTORY_ASSY_LIST . " where ref_id = " . $this->id);
         while ($list_array = array_shift($bom_list)) {
             unset($list_array['item_cost']);
             unset($list_array['full_price']);
             db_perform(TABLE_INVENTORY_ASSY_LIST, $list_array, 'insert');
         }
     }
     return true;
 }
示例#4
0
 function save()
 {
     global $db, $messageStack, $security_level, $currencies;
     $current_situation = $db->Execute("select * from " . TABLE_INVENTORY . " where id = '{$this->id}'");
     $sql_data_array = parent::save();
     if ($sql_data_array == false) {
         return false;
     }
     $sql_data_array['inventory_type'] = 'mi';
     // 	split attributes
     $attr0 = array();
     $attr1 = array();
     if ($this->ms_attr_0 != '') {
         $attr0 = explode(',', $this->ms_attr_0);
     }
     if ($this->ms_attr_1 != '') {
         $attr1 = explode(',', $this->ms_attr_1);
     }
     if (empty($attr0)) {
         $this->get_ms_list();
         return false;
         // no attributes, nothing to do
     }
     // build skus
     $sku_list = array();
     for ($i = 0; $i < count($attr0); $i++) {
         $idx0 = explode(':', $attr0[$i]);
         if (empty($attr1)) {
             if ($idx0[0] != '') {
                 $sku_list[] = $this->sku . '-' . $idx0[0];
                 $variables[$this->sku . '-' . $idx0[0]]['idx0'] = $idx0[1];
             }
         } else {
             for ($j = 0; $j < count($attr1); $j++) {
                 $idx1 = explode(':', $attr1[$j]);
                 if ($idx0[0] != '' && $idx1[0] != '') {
                     $sku_list[] = $this->sku . '-' . $idx0[0] . $idx1[0];
                     $variables[$this->sku . '-' . $idx0[0] . $idx1[0]]['idx0'] = $idx0[1];
                     $variables[$this->sku . '-' . $idx0[0] . $idx1[0]]['idx1'] = $idx1[1];
                 }
             }
         }
     }
     // either update, delete or insert sub skus depending on sku list
     $result = $db->Execute("select sku from " . TABLE_INVENTORY . " where inventory_type = 'mi' and sku like '{$this->sku}-%'");
     $existing_sku_list = array();
     while (!$result->EOF) {
         $existing_sku_list[] = $result->fields['sku'];
         $result->MoveNext();
     }
     $delete_list = array_diff($existing_sku_list, $sku_list);
     $update_list = array_intersect($existing_sku_list, $sku_list);
     $insert_list = array_diff($sku_list, $update_list);
     foreach ($insert_list as $sku) {
         // first insert new sku's with all fields
         $sql_data_array['sku'] = $sku;
         $sql_data_array['description_short'] = sprintf($this->description_short, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         $sql_data_array['description_purchase'] = sprintf($this->description_purchase, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         $sql_data_array['description_sales'] = sprintf($this->description_sales, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         db_perform(TABLE_INVENTORY, $sql_data_array, 'insert');
         foreach ($this->purchase_array as $purchase_row) {
             $purchase_data_array = array('sku' => $sku, 'vendor_id' => $purchase_row['vendor_id'], 'description_purchase' => sprintf($purchase_row['description_purchase'], $variables[$sku]['idx0'], $variables[$sku]['idx1']), 'item_cost' => $purchase_row['item_cost'], 'purch_package_quantity' => $purchase_row['purch_package_quantity'], 'purch_taxable' => $purchase_row['purch_taxable'], 'price_sheet_v' => $purchase_row['price_sheet_v']);
             db_perform(TABLE_INVENTORY_PURCHASE, $purchase_data_array, 'insert');
         }
     }
     if ($this->id != '') {
         //only update fields that are changed otherwise fields in the child could be overwritten
         foreach ($current_situation->fields as $key => $value) {
             // remove fields where the parent is unchanged because the childeren could have different values in these fields.
             switch ($key) {
                 case 'description_short':
                     if ($this->description_short == $value) {
                         unset($sql_data_array[$key]);
                     }
                     break;
                 case 'description_purchase':
                     if ($this->description_purchase == $value) {
                         unset($sql_data_array[$key]);
                     }
                     break;
                 case 'description_sales':
                     if ($this->description_sales == $value) {
                         unset($sql_data_array[$key]);
                     }
                     break;
                 case 'minimum_stock_level':
                     if ($currencies->clean_value($this->minimum_stock_level) == $currencies->clean_value($value)) {
                         unset($sql_data_array[$key]);
                     }
                     break;
                 case 'reorder_quantity':
                     if ($currencies->clean_value($this->reorder_quantity) == $currencies->clean_value($value)) {
                         unset($sql_data_array[$key]);
                     }
                     break;
                 default:
                     if ($sql_data_array[$key] == $value) {
                         unset($sql_data_array[$key]);
                     }
             }
         }
     }
     foreach ($update_list as $sku) {
         //update with reduced number of fields.
         $sql_data_array['sku'] = $sku;
         if (isset($sql_data_array['description_short'])) {
             $sql_data_array['description_short'] = sprintf($this->description_short, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         }
         if (isset($sql_data_array['description_purchase'])) {
             $sql_data_array['description_purchase'] = sprintf($this->description_purchase, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         }
         if (isset($sql_data_array['description_sales'])) {
             $sql_data_array['description_sales'] = sprintf($this->description_sales, $variables[$sku]['idx0'], $variables[$sku]['idx1']);
         }
         db_perform(TABLE_INVENTORY, $sql_data_array, 'update', "sku = '" . $sku . "'");
         foreach ($this->backup_purchase_array as $backUpKey => $backUpRow) {
             $backUpRow['description_purchase'] = sprintf($backUpRow['description_purchase'], $variables[$sku]['idx0'], $variables[$sku]['idx1']);
             $purchase_data_array = null;
             if ($backUpRow['action'] == 'delete') {
                 $result = $db->Execute("delete from " . TABLE_INVENTORY_PURCHASE . " where sku = '{$sku}' and vendor_id = '{$backUpRow['vendor_id']}'");
             } else {
                 if ($backUpRow['action'] == 'insert') {
                     $purchase_data_array = $backUpRow;
                     unset($purchase_data_array['id']);
                     unset($purchase_data_array['action']);
                     $purchase_data_array['sku'] = $sku;
                     db_perform(TABLE_INVENTORY_PURCHASE, $purchase_data_array, 'insert');
                 } else {
                     /*on purpose removed this part because iam not sure what to update and what not 
                     		 * $purchase_data_array = $backUpRow;
                     		unset($purchase_data_array['id']);
                     		unset($purchase_data_array['action']);
                     		foreach($backUpRow as $key => $value) {
                     			if($key == 'action' || $key == 'id' ) break;
                     			if($this->purchase_array[$backUpKey][$key] == $value){
                     				unset($purchase_data_array[$key]);
                     			}
                     		}
                     		db_perform(TABLE_INVENTORY_PURCHASE, $purchase_data_array, 'update', "sku = '" . $sku. "' and vendor_id = '".$backUpRow['vendor_id']."'");*/
                 }
             }
         }
     }
     if (count($delete_list) && $security_level < 4) {
         $messageStack->add(ERROR_NO_PERMISSION, 'error');
         $this->get_ms_list();
         return false;
     }
     foreach ($delete_list as $sku) {
         $temp = $this->mi_check_remove($sku);
         if ($temp === true) {
             $result = $db->Execute("delete from " . TABLE_INVENTORY . " where sku = '{$sku}'");
             $result = $db->Execute("delete from " . TABLE_INVENTORY_PURCHASE . " where sku = '{$sku}'");
         } elseif ($temp === false) {
             $result = $db->Execute("update " . TABLE_INVENTORY . " set inactive = '1' where sku = '{$sku}'");
         }
     }
     // update/insert into inventory_ms_list table
     $result = $db->Execute("select id from " . TABLE_INVENTORY_MS_LIST . " where sku = '{$this->sku}'");
     $exists = $result->RecordCount();
     $data_array = array('sku' => $this->sku, 'attr_0' => $this->ms_attr_0, 'attr_name_0' => $this->attr_name_0, 'attr_1' => $this->ms_attr_1, 'attr_name_1' => $this->attr_name_1);
     if ($exists) {
         db_perform(TABLE_INVENTORY_MS_LIST, $data_array, 'update', "id = " . $result->fields['id']);
     } else {
         db_perform(TABLE_INVENTORY_MS_LIST, $data_array, 'insert');
     }
     $this->get_ms_list();
     return true;
 }