<?php
/**
* 出荷手配依頼コントローラー
*/
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Controller\BaseController;
use App\Service\KintoneService;
use App\Service\CommonService;
use Exception;
class ShippingController extends BaseController
{
protected $kintoneService;
protected $commonService;
protected $logger;
public function __construct(
KintoneService $kintoneService,
CommonService $commonService,
\Psr\Log\LoggerInterface $logger
)
{
parent::__construct();
$this->kintoneService = $kintoneService;
$this->commonService = $commonService;
$this->logger = $logger;
}
/**
* @Route("/shipping", name="shipping")
*/
public function shipping(Request $request): Response
{
$today = $request->get('today', date("Y-m-d"));
$category_relation = $this->commonService->getCategoryRelation();
$org_top_categories = $this->commonService->getTopCategoryList();
// カテゴリを配列に持たす
$top_categories = [];
foreach($this->getOriginalConfig("param", "categories") as $tmp_cate) {
foreach($org_top_categories as $key => $category){
if(preg_match("/$category/", $tmp_cate)) {
$top_categories[$tmp_cate][] = $key;
}
}
}
$records = [];
$check_flg = false;
$limit = 100;
$offset = 0;
while(1) {
$query = sprintf('出荷予定日_0 = "%s" offset %d', $today, $offset);
$tmp = $this->kintoneService->requestGets('order', $query);
$records = array_merge($records, $tmp["records"]);
$offset += $limit;
if($tmp["totalCount"] < $offset) {
$totalCount = $tmp["totalCount"];
break;
}
if($offset >= 10000 ) break;
}
$order["records"] = $records;
$order["totalCount"] = $totalCount;
$result["category"] = $top_categories;
$result["order"] = $order;
$items = [];
$shipment_count = [1 => 0, 2 => 0, 3 => 0, 4 => 0];
foreach($result["order"]["records"] as $record) {
$delivery = $record["運送会社"]["value"];
$sub_table = $record["受注商品テーブル"]["value"];
foreach($sub_table as $row) {
$item = $row["value"];
$item_code = $item["商品コード"]["value"];
$shipment = $item["便番号"]["value"];
$shipment_id = str_replace("便", "", $shipment);
$qty = $item["箱数"]["value"];
if(empty($shipment_id) || empty($item_code)) continue;
$item_name = $item["受注商品名"]["value"];
if(preg_match("/^◆_|^運賃_|_200入|_600入| 200入| 600入/", $item_name)) continue;
try{
$top_category_code = $category_relation[$item_code];
}catch(\Exception $e) {
$this->logger->log("debug", json_encode($row));
die("データエラー");
}
if(empty($items[$shipment_id])) $items[$shipment_id] = [];
if(empty($items[$shipment_id][$delivery])) $items[$shipment_id][$delivery] = [];
$okinawa_flg = preg_match('/^沖縄県/', $record["お届け先住所1"]["value"]) ? true : false;
if(isset($items[$shipment_id][$delivery][$item_code])) {
$items[$shipment_id][$delivery][$item_code]["qty"]+= (int)$item["箱数"]["value"];
if($okinawa_flg) {
$items[$shipment_id][$delivery][$item_code]["okinawa_qty"]+= (int)$item["箱数"]["value"];
}
}else{
$items[$shipment_id][$delivery][$item_code] = [
'name' => $item["受注商品名"]["value"],
'qty' => (int)$item["箱数"]["value"],
'category_code' => $top_category_code,
'okinawa_flg' => $okinawa_flg ? true : false,
'okinawa_qty' => $okinawa_flg ? (int)$item["箱数"]["value"] : 0,
];
try{
$shipment_count[$shipment_id]++;
}catch(Exception $e) {
dd($e);
}
}
}
}
// 対応済みの商品を配列に持たす
$checked_list = $this->getComplete($today);
$checked = [];
foreach($checked_list as $val) $checked[] = $val[1];
foreach($items as $shipment_id => $tmp_deliverys) {
foreach($tmp_deliverys as $delivery_id => $tmp_items) {
foreach($tmp_items as $item_code => $item) {
$item_key = $shipment_id . "-" . $delivery_id . "-" . $item_code . "-" . $item["qty"];
$items[$shipment_id][$delivery_id][$item_code]["item_key"] = $item_key;
$items[$shipment_id][$delivery_id][$item_code]["checked"] = in_array($item_key, $checked) ? true : false;
}
}
}
$search["shipment_id"] = $request->get('search_shipment_id');
$search["keyword"] = $request->get('search_keyword');
$search["okinawa_flg"] = $request->get('search_okinawa_flg');
$search["category_key"] = $request->get('search_category_key');
return $this->render('shipping.html.twig', [
'items' => $items,
'json_items' => json_encode($items),
'delivery_list' => $this->getOriginalConfig("param", "delivery"),
'shipment_list' => $this->getOriginalConfig("param", "shipment"),
'shipment_count' => $shipment_count,
'top_categories' => $top_categories,
'json_top_categories' => json_encode($top_categories),
'today' => $today,
'search' => $search,
]);
}
/**
* @Route("/shipping/setComplete", name="shipping_set_complete")
*/
public function setComplete(Request $request){
//フォーマットの統一
$date = $request->get('date');
$date = date("Y-m-d", strtotime($date));
$key = $request->get('key');
$checked = $request->get('checked');
$this->logger->info('POSTデータ', ["date" => $date, "key" => $key, "checked" => $checked]);
$list = $this->getComplete($date, true);
$new_list = [];
foreach($list as $value){
if($checked == 0 && $value[0] == $date && $value[1] == $key) continue;
$new_list[] = join("\t", $value);
}
if($checked == 1){
$new_list[] = $date . "\t" . $key;
}
$path = $this->getCompleteFileName($date);
$fh = fopen($path, "w");
fputs($fh, join("\n", $new_list));
fclose($fh);
}
/**
* 済みデータ取得 function
*
* @param [type] $date
* @param boolean $all_flg
* @return array
*/
private function getComplete($date, $all_flg = false){
//フォーマットの統一
$date = date("Y-m-d", strtotime($date));
//データは月単位で保存
$path = $this->getCompleteFileName($date);
$list = [];
if(is_file($path)) {
$rows = file($path);
foreach($rows as $row) {
$values = preg_split("/\t/", trim($row));
if($all_flg || $values[0] == $date) {
$list[] = $values;
}
}
}
return $list;
}
private function getCompleteFileName($date){
//データは月単位で保存
$yymm = substr($date, 0, 7);
$path = __DIR__ . "/../../data/shipping/{$yymm}.tsv";
return $path;
}
}