public function exportcsvAction() { set_time_limit(0); $request = $this->getRequest()->getParams(); $fa = new Product_Model_Fa(); $son = new Product_Model_Son(); $db = $fa->getAdapter(); $explanded = array(); $whereSearch = "1=1"; foreach ($request as $k => $v) { if ($v) { if ("search_recordkey" == $k && $v) { $whereSearch .= " and t1.recordkey = '{$v}'"; } else { if ("explanded" == $k && $v) { $explanded = json_decode($v); } else { $col = str_replace('search_', '', $k); if ($col != $k) { // 查询条件 $whereSearch .= " and ifnull(t1." . $col . ",'') like '%" . $v . "%'"; } } } } } // 获取物料数据 $data = $fa->getList($whereSearch, null, null); print chr(0xef) . chr(0xbb) . chr(0xbf); // 获取物料数据 $data_csv = array(); $title = array('cnt' => '#', 'code' => '物料号', 'state' => '状态', 'name' => '物料名称', 'description' => '物料描述', 'low_cny' => '最低价格-人民币', 'high_cny' => '最高价格-人民币', 'average_cny' => '平均价格-人民币', 'low_usd' => '最低价格-美元', 'high_usd' => '最高价格-美元', 'average_usd' => '平均价格-美元', 'project_no_name' => '产品型号', 'qty' => '数量', 'replace' => '替代料', 'partposition' => '器件位置', 'remark' => '备注'); $date = date('YmdHsi'); // 文件名 if (count($data) == 1) { $name = $data[0]['code']; } else { $zip = new ZipArchive(); $zipFileName = "boms" . $date . ".zip"; $zipFile = "../temp/{$zipFileName}"; $zipPath = "../temp/boms" . $date . "/"; if (!file_exists($zipPath)) { mkdir($zipPath); } $name = "bom_list"; } $bomPrice = new Product_Model_BomPrice(); $currencyrate = new Erp_Model_Setting_Currencyrate(); $date = date('Y-m-d'); $rateCny = $currencyrate->getRateByCode('CNY', $date); $rateUsd = $currencyrate->getRateByCode('USD', $date); $rate = round($rateCny / $rateUsd, 4); $title = $this->object_array($title); $typeids = array(); $typenames = array(); $k = 0; for ($i = 0; $i < count($data); $i++) { $push_data = array(); $d = $data[$i]; $k++; $price = $bomPrice->calcBomPrice($d['recordkey'], 'CNY'); $info = array('cnt' => $k, 'code' => Helper::ifNull($d, 'code') . " V" . $d['ver'], 'state' => Helper::ifNull($d, 'state'), 'name' => Helper::ifNull($d, 'name'), 'description' => Helper::ifNull($d, 'description'), 'low_cny' => $price['low'], 'low_usd' => round($price['low'] * $rate, 4), 'high_cny' => $price['high'], 'high_usd' => round($price['high'] * $rate, 4), 'average_cny' => $price['average'], 'average_usd' => round($price['average'] * $rate, 4), 'qty' => $d['qty'], 'replace' => Helper::ifNull($d, 'replace'), 'partposition' => Helper::ifNull($d, 'partposition'), 'remark' => Helper::ifNull($d, 'remark')); $filename = $info['code'] . '-' . $date; if (isset($zipPath)) { $path = $zipPath . $filename . ".csv"; } else { $path = "../temp/" . $filename . ".csv"; } $file = fopen($path, "w"); fputcsv($file, $title); array_push($data_csv, $title); $info['count'] = 0; $push_data[] = $info; $push_data = $this->getBomInfo($push_data, $fa, $son, $d['recordkey'], 1, $explanded, $rate); foreach ($push_data as $bomdata) { $count = $bomdata['count']; $bomdata['count'] = ""; $prefix = ""; for ($ii = 0; $ii < $count; $ii++) { $prefix .= " "; } $bomdata['code'] = $prefix . $bomdata['code']; $bomd = $this->object_array($bomdata); fputcsv($file, $bomd); } fclose($file); } /* foreach($push_data as $data) { $count = $data['count']; $data['count'] = ""; $prefix = ""; for($i = 0;$i < $count; $i++){ $prefix .= " "; } $data['code'] = $prefix.$data['code']; $d = $this->object_array($data); fputcsv($file, $d); } fclose($file); */ $this->operate("BOM导出"); if (isset($zipPath)) { $zip = new ZipArchive(); $helper = new Application_Model_Helpers(); if ($zip->open($zipFile, ZipArchive::OVERWRITE) === TRUE) { $datalist = $this->list_dir($zipPath); foreach ($datalist as $val) { if (file_exists($val)) { $zip->addFile($val, basename($val)); //第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下 } } $zip->close(); //关闭 } echo $zipFileName; } else { echo $filename; } exit; }
public function exportcsvoneAction() { $request = $this->getRequest()->getParams(); $fa = new Product_Model_Fa(); $son = new Product_Model_Son(); $db = $fa->getAdapter(); $explanded = array(); $whereSearch = "1=1"; foreach ($request as $k => $v) { if ($v) { if ($k == 'search_key') { $whereSearch .= " and (ifnull(t1.remark,'') like '%{$v}%' or ifnull(t3.name,'') like '%{$v}%' or ifnull(t3.description,'') like '%{$v}%' or ifnull(t5.model_internal, '') like '%{$v}%')"; } else { if ("search_archive_date_from" == $k && $v) { $whereSearch .= " and t1.bom_upd_time >= '" . str_replace('T', ' ', $v) . "'"; } else { if ("search_archive_date_to" == $k && $v) { $whereSearch .= " and t1.bom_upd_time <= '" . str_replace('T00:00:00', ' 23:59:59', $v) . "'"; } else { if ("search_fa" == $k && $v) { $whereSearch .= " and t1.code like '%" . $v . "%'"; } else { if ("search_son" == $k && $v) { $recordkey = ""; $sonData = $db->query("select group_concat(recordkey) as recordkey from oa_product_bom_son where code like '%{$v}%'")->fetchObject(); if ($sonData && $sonData->recordkey) { $recordkey = $sonData->recordkey; } if (!$recordkey) { $recordkey = "0"; } $whereSearch .= " and t1.recordkey in ({$recordkey})"; } else { if ("search_recordkey" == $k && $v) { $whereSearch .= " and t1.recordkey = '{$v}'"; } else { if ("explanded" == $k && $v) { $explanded = json_decode($v); } else { $col = str_replace('search_', '', $k); if ($col != $k) { // 查询条件 $whereSearch .= " and ifnull(t1." . $col . ",'') like '%" . $v . "%'"; } } } } } } } } } } $updflg = false; if (isset($request['upd_flg']) && $request['upd_flg'] == 1) { // 只导出升版 $whereSearch .= " and t1.ver > 1.0"; $updflg = true; } if (isset($request['my']) && $request['my'] == 1) { $user_session = new Zend_Session_Namespace('user'); $user = $user_session->user_info['employee_id']; if (isset($request['step']) && $request['step'] == 'dev') { // 从升版里取 $data = $db->query("select group_concat(id) as nids from oa_product_bom_upd where create_user = {$user} and state = 'Active'")->fetchObject(); if ($data && $data->nids) { $nids = $data->nids; } } else { $data = $db->query("select group_concat(id) as nids from oa_product_bom_new where create_user = {$user} and state = 'Active'")->fetchObject(); if ($data && $data->nids) { $nids = $data->nids; } } if (isset($nids) && $nids) { $whereSearch .= " and t1.nid in ({$nids})"; } } // 获取物料数据 $data = $fa->getList($whereSearch, null, null); print chr(0xef) . chr(0xbb) . chr(0xbf); // 获取物料数据 $data_csv = array(); $title = array('cnt' => '#', 'code' => '物料号', 'state' => '状态', 'name' => '物料名称', 'description' => '物料描述', 'project_no_name' => '产品型号', 'qty' => '数量', 'replace' => '替代料', 'partposition' => '器件位置', 'remark' => '备注'); if ($updflg) { $title['upd_type'] = '升版类型'; $title['reason_type'] = '升版原因分类'; $title['upd_reason'] = '升版原因'; $title['description_upd'] = '升版描述'; } // 文件名 if (count($data) == 1) { $name = $data[0]['code']; } else { $name = "bom_list"; } $title = $this->object_array($title); $date = date('YmdHsi'); $filename = $name . '-' . $date; $path = "../temp/" . $filename . ".csv"; $file = fopen($path, "w"); fputcsv($file, $title); array_push($data_csv, $title); $typeids = array(); $typenames = array(); $k = 0; $push_data = array(); for ($i = 0; $i < count($data); $i++) { $d = $data[$i]; $k++; $info = array('cnt' => $k, 'code' => $this->ifNull($d, 'code') . " V" . $d['ver'], 'state' => $this->ifNull($d, 'state'), 'name' => $this->ifNull($d, 'name'), 'description' => $this->ifNull($d, 'description'), 'project_no_name' => $this->ifNull($d, 'project_no_name'), 'qty' => $d['qty'], 'replace' => $this->ifNull($d, 'replace'), 'partposition' => $this->ifNull($d, 'partposition'), 'remark' => $this->ifNull($d, 'remark')); if ($updflg) { $info['upd_type'] = $this->ifNull($d, 'upd_type'); $info['reason_type'] = $this->ifNull($d, 'reason_type_name'); $info['upd_reason'] = $this->ifNull($d, 'upd_reason'); $info['description_upd'] = $this->ifNull($d, 'description_upd'); } $info['count'] = 0; $push_data[] = $info; $push_data = $this->getBomInfo($push_data, $fa, $son, $d['recordkey'], 1, $explanded); } foreach ($push_data as $data) { $count = $data['count']; $data['count'] = ""; $prefix = ""; for ($i = 0; $i < $count; $i++) { $prefix .= " "; } $data['code'] = $prefix . $data['code']; $d = $this->object_array($data); fputcsv($file, $d); } fclose($file); $this->operate("BOM导出"); echo $filename; exit; }
public function exportcsvAction() { set_time_limit(0); $request = $this->getRequest()->getParams(); $fa = new Product_Model_Fa(); $son = new Product_Model_Son(); $db = $fa->getAdapter(); $explanded = array(); $whereSearch = "1=1"; foreach ($request as $k => $v) { if ($v) { if ($k == 'search_key') { $whereSearch .= " and (ifnull(t1.remark,'') like '%{$v}%' or ifnull(t3.name,'') like '%{$v}%' or ifnull(t3.description,'') like '%{$v}%' or ifnull(t5.model_internal, '') like '%{$v}%')"; } else { if ("search_archive_date_from" == $k && $v) { $whereSearch .= " and t1.bom_upd_time >= '" . str_replace('T', ' ', $v) . "'"; } else { if ("search_archive_date_to" == $k && $v) { $whereSearch .= " and t1.bom_upd_time <= '" . str_replace('T00:00:00', ' 23:59:59', $v) . "'"; } else { if ("search_fa" == $k && $v) { $whereSearch .= " and t1.code like '%" . $v . "%'"; } else { if ("search_son" == $k && $v) { $recordkey = ""; $sonData = $db->query("select group_concat(recordkey) as recordkey from oa_product_bom_son where code like '%{$v}%'")->fetchObject(); if ($sonData && $sonData->recordkey) { $recordkey = $sonData->recordkey; } if (!$recordkey) { $recordkey = "0"; } $whereSearch .= " and t1.recordkey in ({$recordkey})"; } else { if ("search_recordkey" == $k && $v) { $whereSearch .= " and t1.recordkey = '{$v}'"; } else { if ("explanded" == $k && $v) { $explanded = json_decode($v); } else { $col = str_replace('search_', '', $k); if ($col != $k) { // 查询条件 $whereSearch .= " and ifnull(t1." . $col . ",'') like '%" . $v . "%'"; } } } } } } } } } } // 获取物料数据 $data = $fa->getList($whereSearch, null, null); print chr(0xef) . chr(0xbb) . chr(0xbf); // 获取物料数据 $data_csv = array(); $title = array('cnt' => '#', 'code' => '物料号', 'state' => '状态', 'name' => '物料名称', 'description' => '物料描述', 'project_no_name' => '产品型号', 'bom_upd_time' => '归档时间', 'remark' => '备注'); $title = $this->object_array($title); $date = date('YmdHsi'); $filename = 'bomlist-' . $date; $path = "../temp/" . $filename . ".csv"; $file = fopen($path, "w"); fputcsv($file, $title); array_push($data_csv, $title); $typeids = array(); $typenames = array(); $k = 0; for ($i = 0; $i < count($data); $i++) { $d = $data[$i]; $k++; $info = array('cnt' => $k, 'code' => $this->ifNull($d, 'code') . " V" . $d['ver'], 'state' => $this->ifNull($d, 'state'), 'name' => $this->ifNull($d, 'name'), 'description' => $this->ifNull($d, 'description'), 'project_no_name' => $this->ifNull($d, 'project_no_name'), 'archive_time' => $d['bom_upd_time'], 'remark' => $this->ifNull($d, 'remark')); $bomd = $this->object_array($info); fputcsv($file, $bomd); } fclose($file); $this->operate("BOM导出"); echo $filename; exit; }