/*
* Linux 自动任务
* 质检组收货及积压报表 --- 收件人邮箱
*
* $date 0000-00-00 日期 当天的前一天
* $start_date 2017-09-01 00:00:00 开始日期 每月 1 号
* $warehouseId Integer 仓库
*/
require_once( "config.php" );
$flagFile = dirname(__FILE__) . "/qc-group-receiving-and-backlog-report";
if (@file_exists($flagFile)) {
Common_Service_Common::autoRunFlag($flagFile, __FILE__, 2);
die("程序正在运行!");
}
ini_set("memory_limit", "-1");
set_time_limit(0);
define("RUNTIME", "[ " . date("Y-m-d H:i:s") . " ] ");
echo RUNTIME . "Starting!\n";
$Object = new Product_Service_QualityReceivingBacklog();
$warehouseId = array(11, 4);
$date = date("Y-m-d", strtotime("-1 day"));
foreach ($warehouseId as $value) {
$Object->addQualityReceivingBacklog($date, $value);
}
//休眠 5 秒后等待数据同步
sleep(5);
$start_date = date("Y-m-d", mktime(0, 0, 0, date("m"), 1, date("Y")));
foreach ($warehouseId as $valu) {
$Object->addresseeEmail($start_date, $date, $valu);
}
echo "[ " . date('Y-m-d H:i:s') . " ] 结束运行\r\n";
@unlink($flagFile);
//Service
class Product_Service_QualityReceivingBacklog {
/*
* $combination array() 结合(字段,值)
*/
public static function add($combination) {
$Object = new Product_Model_QualityReceivingBacklog();
return $Object->add($combination);
}
/*
* $combination array() 条件
* $type * || COUNT(*)
* $orderBy DESC || ASC 排序
* $page Integer 页
* $pageSize Integer 条数
*/
public static function getCombination($combination, $type = "*", $orderBy = "qrb_id desc", $page = 0, $pageSize = 0) {
$Object = new Product_Model_QualityReceivingBacklog();
return $Object->getCombination($combination, $type, $orderBy, $page, $pageSize);
}
/*
* 质检组收货及积压报表
*
* $date 0000-00-00 日期 当天的前一天
* $warehouseId Integer 仓库
*/
public static function addQualityReceivingBacklog($date, $warehouseId) {
try {
$list = array();
//收货数据
$combination = array(
"warehouse_id" => $warehouseId,
"rp_receiving_time_from" => $date . " 00:00:00",
"rp_receiving_time_to" => $date . " 23:59:59",
"asn_has" => "0",
);
$receivingIds = Product_Service_ReceivingPre::getByleftJoin($combination, "*", "rp_receiving_time desc");
if ($receivingIds) {
$row = array();
foreach ($receivingIds as $key => $value) {
if ($value['warehouse_id'] == 11) {
if ($value['ref_id'] != "") {
//用采购单做为键(客户参考号)
$key = $value['ref_id'];
$row[$key] = $value;
}
} else {
if ($value['receiving_code'] != "") {
//用 ASN 单号作为键
$key = $value['receiving_code'];
$row[$key] = $value;
}
}
}
foreach ($row as $ke => $valu) {
//用收货时间作为键
$rpReceivingTime = substr($valu['rp_receiving_time'], 0, 10);
$list[$rpReceivingTime]['count_po'][] = $ke;
$list[$rpReceivingTime]['qrb_po'] = count($list[$rpReceivingTime]['count_po']);
if ($valu['plat_form'] == "CSP") {
$list[$rpReceivingTime]['count_csp'][] = $ke;
$list[$rpReceivingTime]['qrb_csp'] = count($list[$rpReceivingTime]['count_csp']);
}
if (isset($list[$rpReceivingTime]['qrb_csp']) != 0 && $list[$rpReceivingTime]['qrb_po'] != 0) {
$list[$rpReceivingTime]['qrb_csp_proportion'] = round($list[$rpReceivingTime]['qrb_csp'] / $list[$rpReceivingTime]['qrb_po'], 4) * 100;
}
}
}
//未绑定 PO 票数
$combinat = array(
"warehouse_id" => $warehouseId,
"rp_receiving_time_from" => $date,
"rp_receiving_time_to_2" => $date . " 21:00:00",
"asn_has" => 1,
"rp_status" => "0",
);
$rpIds = Product_Service_ReceivingPre::getByCondition($combinat, "*", "rp_receiving_time desc");
if ($rpIds) {
foreach ($rpIds as $val) {
//用收货时间作为键
$rpReceivingTime = substr($val['rp_receiving_time'], 0, 10);
$list[$rpReceivingTime]['rp_tracking_number'][] = $val['rp_tracking_number'];
$list[$rpReceivingTime]['qrb_no_po'] = count(array_unique($list[$rpReceivingTime]['rp_tracking_number']));
}
}
//积压数据
$combin = array(
"warehouse_id" => $warehouseId,
"rp_receiving_time_from" => $date . " 00:00:00",
"rp_receiving_time_to" => $date . " 21:00:00",
"asn_has" => "0",
"rp_status" => "0",
);
$receivingIdInfo = Product_Service_ReceivingPre::getByleftJoin($combin, "*", "rp_receiving_time desc");
if ($receivingIdInfo) {
$arr = array();
foreach ($receivingIdInfo as $k => $va) {
if ($va['ref_id'] != "" && $va['customer_id'] == "STK01") {
//用采购单做为键(客户参考号)
$k = $va['ref_id'];
$arr[$k] = $va;
}
}
foreach ($arr as $ky => $vl) {
//用收货时间和品类作为键
$rpReceivingTime = substr($vl['rp_receiving_time'], 0, 10);
$category = $vl['category'];
if ($vl['plat_form'] == "CSP") {
$list[$rpReceivingTime]['count_is_csp'][] = $ky;
$list[$rpReceivingTime]['qrb_is_csp'] = count($list[$rpReceivingTime]['count_is_csp']);
} else {
$list[$rpReceivingTime]['count_no_csp'][] = $ky;
$list[$rpReceivingTime]['qrb_no_csp'] = count($list[$rpReceivingTime]['count_no_csp']);
}
if ($category != 0) {
if (!isset($list[$rpReceivingTime]['category'])) {
$catearray = Product_Service_Receiving::$productCategory;
foreach ($catearray as $kk => $v) {
$list[$rpReceivingTime]['category'][$kk] = 0;
}
}
$list[$rpReceivingTime]['category'][$category] += 1;
}
}
}
//写入数据
$warehouseNameCn = Warehouse_Service_Warehouse::getById($warehouseId);
if ($list) {
foreach ($list as $kkk => $vv) {
$comb = array(
"qrb_po" => !isset($list[$kkk]['qrb_po']) ? "" : $list[$kkk]['qrb_po'],
"qrb_csp" => !isset($list[$kkk]['qrb_csp']) ? "" : $list[$kkk]['qrb_csp'],
"qrb_csp_proportion" => !isset($list[$kkk]['qrb_csp_proportion']) ? "" : $list[$kkk]['qrb_csp_proportion'],
"qrb_no_po" => !isset($list[$kkk]['qrb_no_po']) ? "" : $list[$kkk]['qrb_no_po'],
"qrb_no_csp" => !isset($list[$kkk]['qrb_no_csp']) ? "" : $list[$kkk]['qrb_no_csp'],
"qrb_is_csp" => !isset($list[$kkk]['qrb_is_csp']) ? "" : $list[$kkk]['qrb_is_csp'],
"qrb_category" => !isset($list[$kkk]['category']) ? "" : serialize($list[$kkk]['category']),
"warehouse_id" => $warehouseId,
"qrb_date" => $date,
"qrb_create_time" => date("Y-m-d H:i:s"),
);
if (!self::add($comb)) {
echo $warehouseNameCn['warehouse_name_cn'] . "写入数据失败";
}
}
}
} catch (Exception $e) {
echo "[ " . date("Y-m-d H:is") . " ] 出现异常,异常信息为:" . $e->getMessage() . "\r\n";
}
}
/*
* 发送质检组收货及积压报表
*
* $start_date 2017-09-01 开始日期 每月 1 号
* $date 0000-00-00 结束日期 当天的前一天
* $warehouseId Integer 仓库
*/
public static function addresseeEmail($start_date, $date, $warehouseId) {
//获取数据
$combination = array(
"warehouse_id" => $warehouseId,
"start_date" => $start_date,
"end_date" => $date,
);
$qrbIds = self::getCombination($combination, "*", "qrb_date desc");
$week = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
$catearray = Product_Service_Receiving::$productCategory;
//正文内容
$text = "<style>.tab-report{border-top:1px solid #cccccc;border-left:1px solid #cccccc;} .tab-report td, .tab-report th{border-bottom:1px solid #cccccc;border-right:1px solid #cccccc; text-align:center;} .tab-report .yellow{background-color:yellow} .tab-report .blue{background-color:#82A3F8} .tab-report .orange{background-color:#E6F3F9} .tab-report .event-tr{background-color:#E6F3F9}</style>";
$text .= "<table width='100%' border='0' cellspacing='0' cellpadding='0' class='tab-report'>";
$text .= "<tr class='o0range'>";
$text .= "<th colspan='2'>时间</th>";
$text .= "<th colspan='3'>收货数据</th>";
$text .= "<th colspan='100%'>积压数据</th>";
$text .= "</tr>";
$text .= "<tr class='even-tr'>";
$text .= "<th rowspan='2'>日期</th>";
$text .= "<th rowspan='2'>星期</th>";
$text .= "<th rowspan='2'>PO 收货量</th>";
$text .= "<th rowspan='2'>CSP 收货量</th>";
$text .= "<th rowspan='2'>CSP 占比</th>";
$text .= "<th rowspan='2'>未绑定 PO 票数</th>";
$text .= "<th colspan='2'>合计</th>";
foreach ($catearray as $key => $value) {
$text .= "<th rowspan='2' value='$key'>$value</th>";
}
$text .= "</tr>";
$text .= "<tr class='even-tr'>";
$text .= "<th>非 CSP</th>";
$text .= "<th>CSP</th>";
$text .= "</tr>";
foreach ($qrbIds as $valu) {
$valu['qrb_category'] == "" ? $qrbCategory = array() : $qrbCategory = unserialize($valu['qrb_category']);
$text .= "<tr>"
. "<td>" . $valu['qrb_date'] . "</td>"
. "<td>" . $week[date("w", strtotime($valu['qrb_date']))] . "</td>"
. "<td>" . $valu['qrb_po'] . "</td>"
. "<td>" . $valu['qrb_csp'] . "</td>"
. "<td>" . $valu['qrb_csp_proportion'] . "%</td>"
. "<td>" . $valu['qrb_no_po'] . "</td>"
. "<td>" . $valu['qrb_no_csp'] . "</td>"
. "<td>" . $valu['qrb_is_csp'] . "</td>";
if ($qrbCategory) {
foreach ($qrbCategory as $ke => $val) {
$text .= "<td>" . $val . "</td>";
}
} else {
for ($i = 0; $i < count($catearray); $i++) {
$text .= "<td></td>";
}
}
$text .= "</tr>";
}
$text .= "</table>";
//获取收件人邮箱
$configId = Common_Service_Config::getByAttribute("RECIPIENT_MAILBOX_ADDRESS");
if ($configId && $configId['config_value']) {
$emails = explode("&", $configId['config_value']);
if ($warehouseId == 11) {
$email = explode(";", $emails[0]);
} else {
$email = explode(";", $emails[1]);
}
//弹出数组最后一个单元(出栈)
array_pop($email);
} else {
die("请配置收件人邮箱地址!");
}
//发送邮件对应处理
$warehouseNameCn = Warehouse_Service_Warehouse::getById($warehouseId);
$mailSubject = $warehouseNameCn['warehouse_name_cn'] . "质检组收货及积压预警监控报表" . date("Y-m-d");
$mail = Product_Service_ReceivingItem::setupMail();
$mail->setBodyHtml($text);
$mail->addTo($email, $mailSubject);
$mail->setSubject($mailSubject);
$path = APPLICATION_PATH . "/../public/file/attachment/" . $mailSubject . ".csv";
if ($path) {
$handle = fopen($path, "a");
if ($handle) {
fwrite($handle, $text);
fclose($handle);
}
}
if ($mail->send()) {
if(is_array($email)){
$mailUser = implode(";", $email);
}
$combinat = array(
"eq_to" => $mailUser,
"eq_subject" => $mailSubject,
"eq_body" => $text,
"eq_attachment" => $path,
"eq_add_time" => date("Y-m-d H:i:s"),
"eq_send_time" => date("Y-m-d H:i:s"),
"eq_status" => 1
);
Common_Service_MailRecord::addMailRecord($combinat);
}
}
}
//Model
class Product_Model_QualityReceivingBacklog {
private $_table;
public function __construct() {
$this->_table = new Product_Model_DbTable_QualityReceivingBacklog();
}
/*
* $combination array() 结合(字段,值)
*/
public function add($combination) {
return $this->_table->insert($combination);
}
/*
* $combination array() 条件
* $type * || COUNT(*)
* $orderBy DESC || ASC 排序
* $page Integer 页
* $pageSize Integer 条数
*/
public function getCombination($combination, $type = "*", $orderBy = "qrb_id desc", $page = 0, $pageSize = 0) {
$select = $this->_table->getAdapter()->select();
$table = $this->_table->info("name");
$select->from($table, $type);
$select->where("1 = ?", 1);
if (isset($combination['warehouse_id']) && $combination['warehouse_id'] != "") {
$select->where("warehouse_id = ?", $combination['warehouse_id']);
}
if (isset($combination['start_date']) && $combination['start_date'] != "") {
$select->where("qrb_date >= ?", $combination['start_date']);
}
if (isset($combination['end_date']) && $combination['end_date'] != "") {
$select->where("qrb_date <= ?", $combination['end_date']);
}
if ($type == "count(*)") {
return $this->_table->getAdapter()->fetchOne($select);
} else {
$select->order($orderBy);
if ($pageSize > 0 and $page > 0) {
$start = ($page - 1) * $pageSize;
$select->limit($pageSize, $start);
}
$sql = $select->__toString();
return $this->_table->getAdapter()->fetchAll($sql);
}
}
}
//Model_DbTable
class Product_Model_DbTable_QualityReceivingBacklog extends Zend_Db_Table_Abstract {
//数据表名称
protected $_name = "quality_receiving_backlog";
//主键
protected $_primary = "qrb_id";
protected $_sequence = true;
}