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 editAction() { // 返回值数组 $result = array('success' => true, 'info' => '编辑成功'); $request = $this->getRequest()->getParams(); $now = date('Y-m-d H:i:s'); $user_session = new Zend_Session_Namespace('user'); $user_id = $user_session->user_info['user_id']; $json = json_decode($request['json']); $updated = $json->updated; $inserted = $json->inserted; $deleted = $json->deleted; $pricelist = new Erp_Model_Warehouse_Pricelist(); if (count($updated) > 0) { foreach ($updated as $val) { $data = array('type' => $val->type, 'supplier_id' => $val->supplier_id, 'price' => $val->price, 'currency' => $val->currency, 'remark' => $val->remark, 'update_time' => $now, 'update_user' => $user_id); if ($val->type == 1) { $code = $val->product_code; $data['product_code'] = $val->product_code; } else { $code = $val->code; $data['code'] = $val->code; } if ($pricelist->fetchAll("id != " . $val->id . " and type = " . $val->type . " and supplier_id = " . $val->supplier_id . " and code = '" . $code . "'")->count() > 0) { $result['success'] = false; $result['info'] = '当前业务伙伴物料[' . $val->code . ']已存在,请勿重复添加!'; } else { try { $pricelist->update($data, "id = " . $val->id); } catch (Exception $e) { $result['success'] = false; $result['info'] = $e->getMessage(); echo Zend_Json::encode($result); exit; } } } } if (count($inserted) > 0) { foreach ($inserted as $val) { $data = array('type' => $val->type, 'supplier_id' => $val->supplier_id, 'price' => $val->price, 'currency' => $val->currency, 'remark' => $val->remark, 'create_time' => $now, 'create_user' => $user_id, 'update_time' => $now, 'update_user' => $user_id); if ($val->type == 1) { $code = $val->product_code; $data['product_code'] = $val->product_code; } else { $code = $val->code; $data['code'] = $val->code; } if ($pricelist->fetchAll("type = " . $val->type . " and supplier_id = " . $val->supplier_id . " and code = '" . $code . "'")->count() > 0) { $result['success'] = false; $result['info'] = '当前业务伙伴物料[' . $val->code . ']已存在,请勿重复添加!'; } else { try { $pricelist->insert($data); } catch (Exception $e) { $result['success'] = false; $result['info'] = $e->getMessage(); echo Zend_Json::encode($result); exit; } } } } if (count($deleted) > 0) { $ladder = new Erp_Model_Warehouse_Ladder(); $qty = new Erp_Model_Warehouse_Ladderqty(); foreach ($deleted as $val) { try { $ladderRes = $ladder->fetchAll("pricelist_id = " . $val->id); if ($ladderRes->count() > 0) { $ladderData = $ladderRes->toArray(); foreach ($ladderData as $ld) { $qty->delete("ladder_id = " . $ld['id']); } $ladder->delete("pricelist_id = " . $val->id); } $pricelist->delete("id = " . $val->id); } catch (Exception $e) { $result['success'] = false; $result['info'] = $e->getMessage(); echo Zend_Json::encode($result); exit; } } } echo Zend_Json::encode($result); exit; }
private function getBomInfo($push_data, $fa, $son, $recordkey, $count, $explanded, $rate) { $price_list = new Erp_Model_Warehouse_Pricelist(); $bomPrice = new Product_Model_BomPrice(); $data = $son->getSon($recordkey); for ($i = 0; $i < count($data); $i++) { $fadata = $fa->getFa($data[$i]['code'], null); $faRow = ""; if ($fadata && count($fadata) > 0) { $faRow = $fadata[0]; } $row = array(); $row['cnt'] = ""; $row['code'] = $data[$i]['code']; if ($faRow) { $row['code'] = $data[$i]['code'] . ' V' . $faRow['ver']; $row['state'] = $faRow['state']; $row['name'] = $data[$i]['name']; $row['description'] = $data[$i]['description']; $price = $bomPrice->calcBomPrice($faRow['recordkey'], 'CNY'); $row['low_cny'] = round($price['low'] * $data[$i]['qty'], 4); $row['low_usd'] = round($price['low'] * $rate * $data[$i]['qty'], 4); $row['high_cny'] = round($price['high'] * $data[$i]['qty'], 4); $row['high_usd'] = round($price['high'] * $rate * $data[$i]['qty'], 4); $row['average_cny'] = round($price['average'] * $data[$i]['qty'], 4); $row['average_usd'] = round($price['average'] * $rate * $data[$i]['qty'], 4); $row['project_no_name'] = $faRow['project_no_name']; $row['qty'] = $data[$i]['qty']; $row['replace'] = $data[$i]['replace']; $row['partposition'] = $data[$i]['partposition']; $row['remark'] = $data[$i]['remark']; $row['count'] = $count; $push_data[] = $row; if (count($explanded) == 0 || in_array($data[$i]['code'], $explanded)) { $push_data = $this->getBomInfo($push_data, $fa, $son, $faRow['recordkey'], ++$count, $explanded, $rate); $count--; } } else { $row['state'] = $data[$i]['mstate']; $row['name'] = $data[$i]['name']; $row['description'] = $data[$i]['description']; if (isset($_SESSION['mprice'][$data[$i]['code']])) { // session 中存在 $price = $_SESSION['mprice'][$data[$i]['code']]; } else { $price = $price_list->getMultiPrice($data[$i]['code'], 'CNY'); $_SESSION['mprice'][$data[$i]['code']] = $price; } $row['low_cny'] = round($price['low'] * $data[$i]['qty'], 4); $row['low_usd'] = round($price['low'] * $rate * $data[$i]['qty'], 4); $row['high_cny'] = round($price['high'] * $data[$i]['qty'], 4); $row['high_usd'] = round($price['high'] * $rate * $data[$i]['qty'], 4); $row['average_cny'] = round($price['average'] * $data[$i]['qty'], 4); $row['average_usd'] = round($price['average'] * $rate * $data[$i]['qty'], 4); $row['project_no_name'] = ""; $row['qty'] = $data[$i]['qty']; $row['replace'] = $data[$i]['replace']; $row['partposition'] = $data[$i]['partposition']; $row['remark'] = $data[$i]['remark']; $row['count'] = $count; $push_data[] = $row; } } return $push_data; }