自动任务发送邮件

/*
 * Linux 自动任务
 * 质检组收货及积压报表 --- 收件人邮箱
 *
 * $start_date              2017-09-01                 开始时间默认日期为每月 1 号
 * $end_dates               0000-00-00                 结束时间运行的前一天
 * $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";
$warehouseId = array(11, 4);
$start_date = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y")));
//$end_dates = date("Y-m-d", strtotime("-1 day"));
$end_dates = date("Y-m-d");
$Object = new Product_Service_Receiving();
foreach ($warehouseId as $value) {
    $Object->addresseeEmail($start_date, $end_dates, $value);
}
echo "[ " . date('Y-m-d H:i:s') . " ] 结束运行\r\n";
@unlink($flagFile);


//Service
class Product_Service_Receiving {
    /*
     * 质检组收货及积压报表
     *
     * $start_date     2017-09-01                 开始时间默认日期为每月 1 号
     * $end_dates      0000-00-00                 结束时间运行的前一天
     * $warehouseId             Integer                    仓库
     */

    public static function addresseeEmail($start_date, $end_dates, $warehouseId) {
        $list = array();

        for ($i = strtotime($start_date); $i <= strtotime($end_dates); $i+=86400) {
            $date = date("Y-m-d", $i);
            //收货数据
            $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'][] = $ke;
                    $list[$rpReceivingTime]['count_po'] = count($list[$rpReceivingTime]['count']);
                    if ($valu['plat_form'] == "CSP") {
                        $list[$rpReceivingTime]['csp'][] = $ke;
                        $list[$rpReceivingTime]['count_csp'] = count($list[$rpReceivingTime]['csp']);
                    }
                    if (isset($list[$rpReceivingTime]['count_csp']) != 0 && $list[$rpReceivingTime]['count_po'] != 0) {
                        $list[$rpReceivingTime]['csp_proportion'] = round($list[$rpReceivingTime]['count_csp'] / $list[$rpReceivingTime]['count_po'], 4) * 100 . "%";
                    }
                }
            }

            //未绑定 PO 票数
            $combinat = array(
                "warehouse_id" => $warehouseId,
                "rp_receiving_time_from" => $date,
                "rp_receiving_time_to" => $date,
                "asn_has" => 1,
            );
            $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]['in_po'] = count(array_unique($list[$rpReceivingTime]['rp_tracking_number']));
                }
            }

            //积压数据
            $comb = 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($comb, "*", "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]['totUpCsp'][] = $ky;
                        $list[$rpReceivingTime]['tot_up_csp'] = count($list[$rpReceivingTime]['totUpCsp']);
                    } else {
                        $list[$rpReceivingTime]['totUp'][] = $ky;
                        $list[$rpReceivingTime]['tot_up'] = count($list[$rpReceivingTime]['totUp']);
                    }
                    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) {
            echo("未获取到" . $warehouseNameCn['warehouse_name_cn'] . "对应数据!");
        }

        $week = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
        foreach ($list as $kkk => $vv) {
            $list[$kkk]['week'] = $week[date("w", strtotime($kkk))];
            $list[$kkk]['count_po'] = !isset($list[$kkk]['count_po']) ? "" : $list[$kkk]['count_po'];
            $list[$kkk]['count_csp'] = !isset($list[$kkk]['count_csp']) ? "" : $list[$kkk]['count_csp'];
            $list[$kkk]['csp_proportion'] = !isset($list[$kkk]['csp_proportion']) ? "" : $list[$kkk]['csp_proportion'];
            $list[$kkk]['in_po'] = !isset($list[$kkk]['in_po']) ? "" : $list[$kkk]['in_po'];
            $list[$kkk]['tot_up'] = !isset($list[$kkk]['tot_up']) ? "" : $list[$kkk]['tot_up'];
            $list[$kkk]['tot_up_csp'] = !isset($list[$kkk]['tot_up_csp']) ? "" : $list[$kkk]['tot_up_csp'];
            $list[$kkk]['category'] = !isset($list[$kkk]['category']) ? 0 : $list[$kkk]['category'];
        }
        krsort($list);

        //附件内容
        $content = "";
        $content .= "时间, , 收货数据, , , , 合计, , 积压数据\n";
        $content .= "日期, 星期, PO 收货量, CSP 收货量, CSP 占比, 未绑定 PO 票数, 非 CSP, CSP, 手机电子, 汽摩配, 家居装饰, 运动, 服装, 珠宝, 玩具, 婚庆, 手工, 园艺, 爱好, 美妆, 电脑电子, 宠物灯饰, 骑行渔具, 户外, 摩配, 消费电子\n";
        foreach ($list as $kkkk => $vvv) {
            if ($vvv['category'] == 0) {
                $categorys = "";
            } else {
                $categorys = implode(",", $vvv['category']);
            }
            $content .= $kkkk . ","
                    . $vvv['week'] . ","
                    . $vvv['count_po'] . ","
                    . $vvv['count_csp'] . ","
                    . $vvv['csp_proportion'] . ","
                    . $vvv['in_po'] . ","
                    . $vvv['tot_up'] . ","
                    . $vvv['tot_up_csp'] . ","
                    . $categorys . "\n";
        }

        //正文内容
        $mail_subject = "质检组收货及积压报表 ";
        $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>日期</th>';
        $text .= '<th>文件名</th>';
        $text .= '<th>仓库</th>';
        $text .= '</tr>';
        $text .= '<tr>'
                . '<td>' . date("Y-m-d") . '</td>'
                . '<td>' . $mail_subject . '</td>'
                . '<td>' . $warehouseNameCn['warehouse_name_cn'] . '</td>'
                . '</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);
            for ($i = 0; $i <= count($email); $i++) {
                $email[$i];
            }
        } else {
            die("请配置收件人邮箱地址!");
        }
        //发送邮件对应处理
        $mail = Product_Service_ReceivingItem::setupMail();
        $mail->setBodyHtml($text);
        $mail->addTo($email, $mail_subject);
        $mail->setSubject($mail_subject);
        $at = $mail->createAttachment(iconv("UTF-8", "GBK", $content));
        $at->type = "application/vnd.ms-excel";
        $at->disposition = Zend_Mime::DISPOSITION_INLINE;
        $at->encoding = Zend_Mime::ENCODING_8BIT;
        $at->filename = iconv("UTF-8", "GBK//TRANSLIT//IGNORE", $mail_subject . date("Y-m-d") . ".csv");
        $mail->send();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值