public function getlistAction() { $request = $this->getRequest()->getParams(); $fa = new Product_Model_Fa(); $fadev = new Product_Model_Fadev(); $db = $fa->getAdapter(); if (!isset($request['left_code']) || !isset($request['left_ver']) || !isset($request['right_code']) || !isset($request['right_ver']) || !$request['left_code'] || !$request['left_ver'] || !$request['right_code'] || !$request['right_ver']) { echo Zend_Json::encode(""); exit; } $left_code = $request['left_code']; $left_ver = $request['left_ver']; $right_code = $request['right_code']; $right_ver = $request['right_ver']; $whereleft = "code='{$left_code}' and ver='{$left_ver}'"; $whereright = "code='{$right_code}' and ver='{$right_ver}'"; $left_recordkey = ""; $right_recordkey = ""; $left_type = ""; $right_type = ""; if ($fa->getJoinCount($whereleft) || $fadev->getJoinCount($whereleft)) { $left_fa = $fa->getJoinList($whereleft, array(), array('recordkey')); if ($left_fa && count($left_fa) > 0) { $left_recordkey = $left_fa[0]['recordkey']; } else { $left_fa = $fadev->getJoinList($whereleft, array(), array('recordkey')); if ($left_fa && count($left_fa) > 0) { $left_recordkey = $left_fa[count($left_fa) - 1]['recordkey']; $left_type = "dev"; } } } if ($fa->getJoinCount($whereright) || $fadev->getJoinCount($whereright)) { $right_fa = $fa->getJoinList($whereright, array(), array('recordkey')); if ($right_fa && count($right_fa) > 0) { $right_recordkey = $right_fa[0]['recordkey']; } else { $right_fa = $fadev->getJoinList($whereright, array(), array('recordkey')); if ($right_fa && count($right_fa) > 0) { $right_recordkey = $right_fa[count($right_fa) - 1]['recordkey']; $right_type = "dev"; } } } if ($left_recordkey && $right_recordkey) { if ($left_type == 'dev') { $son_left = new Product_Model_Sondev(); } else { $son_left = new Product_Model_Son(); } if ($right_type == 'dev') { $son_right = new Product_Model_Sondev(); } else { $son_right = new Product_Model_Son(); } $leftData = $son_left->getSon($left_recordkey); $rightData = $son_right->getSon($right_recordkey); if ($leftData && $rightData) { $leftCode = array(); $rightCode = array(); foreach ($rightData as $d) { $rightCode[] = $d['code']; } foreach ($leftData as $d) { $leftCode[] = $d['code']; } $result = array(); for ($i = 0; $i < count($rightData); $i++) { $data = array(); $row = $rightData[$i]; $data['code'] = $row['code']; $data['code2'] = $row['code']; $data['name'] = $row['name']; $data['description'] = $row['description']; $data['qty2'] = $row['qty']; $data['partposition2'] = $row['partposition']; $data['replace2'] = $row['replace']; $left = $this->getRowFromArrayByCode($leftData, $row['code']); $leftData = $this->removeFromArray($leftData, $row['code']); $data['code1'] = $left['code']; $data['qty1'] = $left['qty']; $data['partposition1'] = $left['partposition']; $data['replace1'] = $left['replace']; $result[] = $data; } if (count($leftData) > 0) { foreach ($leftData as $row) { $data = array(); $data['code'] = $row['code']; $data['code1'] = $row['code']; $data['name'] = $row['name']; $data['description'] = $row['description']; $data['qty1'] = $row['qty']; $data['partposition1'] = $row['partposition']; $data['replace1'] = $row['replace']; $data['code2'] = ""; $data['qty2'] = ""; $data['partposition2'] = ""; $data['replace2'] = ""; $result[] = $data; } } // 将类别数据转为json格式并输出 echo Zend_Json::encode($result); exit; } else { echo Zend_Json::encode($leftData); exit; } } $msg = "BOM不存在"; if (!$left_recordkey) { $msg = $left_code . " V" . $left_ver . "不存在"; } else { if (!$right_recordkey) { $msg = $right_code . " V" . $right_ver . "不存在"; } } echo Zend_Json::encode(array("msg" => $msg)); exit; }
public function calcBomPrice($recordkey, $currency = 'CNY') { $low = $high = $average = 0.0; $fa = new Product_Model_Fa(); $son = new Product_Model_Son(); $price_list = new Erp_Model_Warehouse_Pricelist(); $data = $this->getJoinList('recordkey = ' . $recordkey); // 检查当前BOM是否已有价格 if (count($data) == 0) { // 计算价格 $sonData = $son->getJoinList("recordkey = " . $recordkey, array(), array('code', 'qty')); foreach ($sonData as $sonRow) { // 检查是否有BOM数据,如果没有,当作物料处理 $sonFaData = $fa->getJoinList("code = '" . $sonRow['code'] . "'", array(), array('recordkey'), array('ver desc')); if (count($sonFaData) == 0) { if (isset($_SESSION['mprice'][$sonRow['code']])) { // session 中存在 $price = $_SESSION['mprice'][$sonRow['code']]; } else { $price = $price_list->getMultiPrice($sonRow['code'], $currency); $_SESSION['mprice'][$sonRow['code']] = $price; } } else { $price = $this->calcBomPrice($sonFaData[0]['recordkey'], $currency); } $low += $price['low'] * $sonRow['qty']; $high += $price['high'] * $sonRow['qty']; $average += $price['average'] * $sonRow['qty']; } } else { $bomPrice = $data[0]; $low = $bomPrice['low_' . strtolower($currency)]; $high = $bomPrice['high_' . strtolower($currency)]; $average = $bomPrice['average_' . strtolower($currency)]; } return array('low' => round($low, 4), 'high' => round($high, 4), 'average' => round($average, 4)); }
public function importAction() { $result = array('success' => true, 'info' => '导入成功'); $request = $this->getRequest()->getParams(); $type = isset($request['type']) ? $request['type'] : null; $nid = isset($request['nid']) ? $request['nid'] : ""; if (isset($_FILES['csv'])) { $now = date('Y-m-d H:i:s'); $user_session = new Zend_Session_Namespace('user'); $user_id = $user_session->user_info['user_id']; $file = $_FILES['csv']; $file_extension = strrchr($file['name'], "."); $h = new Application_Model_Helpers(); $tmp_file_name = $h->getMicrotimeStr() . $file_extension; $savepath = "../temp/"; $tmp_file_path = $savepath . $tmp_file_name; move_uploaded_file($file["tmp_name"], $tmp_file_path); $materiel = new Product_Model_Materiel(); $db = $materiel->getAdapter(); if ($type == 'dev') { $fa = new Product_Model_Fadev(); $son = new Product_Model_Sondev(); $table = "oa_product_bom_fa_dev"; } else { if ($type == 'bom') { $fa = new Product_Model_Fa(); $son = new Product_Model_Son(); $table = "oa_product_bom_fa"; } } if ($type == 'dev' || $type == 'bom') { $file = fopen($tmp_file_path, "r"); $data = array(); while (!feof($file)) { array_push($data, fgetcsv($file)); } // 数据校验 if (count($data) <= 1) { $result['success'] = false; $result['info'] = "文件中无数据!"; fclose($file); echo Zend_Json::encode($result); exit; } for ($i = 1; $i < count($data); $i++) { $num = $i + 1; $row = $data[$i]; if ($i == 1 && !$row[1]) { $result['success'] = false; $result['info'] = "请以上级物料开头:第" . $num . "行!"; fclose($file); echo Zend_Json::encode($result); exit; } if (!$row[1] && !$row[2]) { continue; } if ($row[1] && $row[2]) { $result['success'] = false; $result['info'] = "上级物料和下级物料不能同时存在:第" . $num . "行!"; fclose($file); echo Zend_Json::encode($result); exit; } if ($row[1]) { if (!$materiel->checkExist($row[1])) { $result['success'] = false; $result['info'] = "物料“" . $row[1] . "”不存在:第" . $num . "行!"; fclose($file); echo Zend_Json::encode($result); exit; } $m = $materiel->getMaterielByCode($row[1]); } if ($row[2]) { if (!$materiel->checkExist($row[2])) { $result['success'] = false; $result['info'] = "物料“" . $row[2] . "”不存在:第" . $num . "行!"; fclose($file); echo Zend_Json::encode($result); exit; } } if ($row[2] && $row[7]) { $replace = explode(',', $row[7]); foreach ($replace as $r) { $r = iconv('utf-8', 'gbk//IGNORE', $r); if (!$materiel->checkExist($r)) { $result['success'] = false; $result['info'] = "物料“" . $r . "”不存在:第" . $i . "行!"; fclose($file); echo Zend_Json::encode($result); exit; } } } if ($i > 1 && $row[1] && $data[$i - 1][1]) { $result['success'] = false; $result['info'] = "BOM“" . $data[$i - 1][1] . "”不存在子物料:第{$i行!}"; fclose($file); echo Zend_Json::encode($result); exit; } } // 只取一次最大值,可能遇到重号 $maxkeys = $fa->getAdapter()->query("select ifnull(max(recordkey),0) as maxkey from {$table}")->fetchObject(); $recordkey = $maxkeys->maxkey; $pid = ""; $faArr = array(); $sonArr = array(); $obsoleteWhere = "1=0"; // 校验成功,开始保存数据 for ($i = 1; $i < count($data); $i++) { $row = $data[$i]; if ($row[1]) { $m = $materiel->getMaterielByCode($row[1]); $recordkey++; $pid = $m['id']; $faData = array('nid' => $nid, 'recordkey' => $recordkey, 'id' => $m['id'], 'code' => $row[1], 'qty' => 1, 'state' => $row[3] ? $row[3] : 'EBOM', 'ver' => $row[4] ? $row[4] : '1.0'); if ($faData['ver'] > '1.0') { $obsoleteWhere .= " or (code='" . $row[1] . "' and ver < '" . $faData['ver'] . "')"; } // 检查是否已经存在 if ($type == 'dev') { $list = $fa->getFaList("nid = {$nid} and ver = '" . $faData['ver'] . "' and id = " . $m['id']); } else { $list = $fa->getFaList("ver = '" . $faData['ver'] . "' and id = " . $m['id']); } if ($list && count($list) > 0) { $result['success'] = false; $result['info'] = "数据已存在:" . $faData['code'] . " " . $faData['ver'] . "!"; fclose($file); echo Zend_Json::encode($result); exit; } $faArr[] = $faData; } if ($row[2]) { $m = $materiel->getMaterielByCode($row[2]); $sonData = array('nid' => $nid, 'recordkey' => $recordkey, 'pid' => $pid, 'id' => $m['id'], 'code' => $row[2], 'qty' => $row[5] ? $row[5] : 1, 'partposition' => $row[6] ? $row[6] : '', 'replace' => $row[7] ? $row[7] : '', 'remark' => $row[8] ? $row[8] : ''); // 检查是否已经存在 if ($type == 'dev') { $list = $son->getList("nid = {$nid} and id = " . $m['id']); } else { $list = $son->getList("recordkey = {$recordkey} and id = " . $m['id']); } if ($list && count($list) > 0) { $result['success'] = false; $result['info'] = "数据已存在:" . $sonData['code'] . "!"; fclose($file); echo Zend_Json::encode($result); exit; } $sonArr[] = $sonData; } } $db->beginTransaction(); try { foreach ($faArr as $faData) { $fa->insert($faData); } foreach ($sonArr as $sonData) { $son->insert($sonData); } // 旧版作废 if ($type == 'bom') { $bosoleteData = array("state" => "Obsolete"); $fa->update($bosoleteData, $obsoleteWhere); } $db->commit(); //执行commit } catch (Exception $e) { $db->rollBack(); //如果出现错误,执行回滚操作 $result['result'] = false; $result['info'] = $e->getMessage(); fclose($file); echo Zend_Json::encode($result); exit; } fclose($file); } } echo Zend_Json::encode($result); exit; }
/** * @abstract 删除 * @return null */ public function removeAction() { // 返回值数组 $result = array('success' => true, 'result' => true, 'info' => '删除成功'); $request = $this->getRequest()->getParams(); $now = date('Y-m-d H:i:s'); $user_session = new Zend_Session_Namespace('user'); $user = $user_session->user_info['employee_id']; $json = json_decode($request['json']); $deleted = $json->deleted; $newbom = new Product_Model_Newbom(); $fa = new Product_Model_Fa(); $son = new Product_Model_Son(); $fadev = new Product_Model_Fadev(); $sondev = new Product_Model_Sondev(); $record = new Dcc_Model_Record(); $review = new Dcc_Model_Review(); if (count($deleted) > 0) { foreach ($deleted as $val) { $id = $val->sid; $recordkey = $val->recordkey; // 操作记录 $data = array('type' => "bom", 'table_name' => "oa_product_bom_new", 'table_id' => $id, 'handle_user' => $user, 'handle_time' => $now, 'action' => "删除", 'ip' => $_SERVER['REMOTE_ADDR']); try { // 增加record记录 $record->insert($data); // 删除bom表 $fa->delete("sid = {$id}"); $son->delete("recordkey = {$recordkey}"); if ($val->nid) { $fadev->delete("recordkey = {$recordkey}"); $sondev->delete("recordkey = {$recordkey}"); } } catch (Exception $e) { $result['result'] = false; $result['info'] = $e->getMessage(); echo Zend_Json::encode($result); exit; } } } echo Zend_Json::encode($result); exit; }