src/Controller/ShippingController.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3.  * 出荷手配依頼コントローラー
  4.  */
  5. namespace App\Controller;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use App\Controller\BaseController;
  10. use App\Service\KintoneService;
  11. use App\Service\CommonService;
  12. use Exception;
  13. class ShippingController extends BaseController
  14. {
  15.     protected $kintoneService;
  16.     protected $commonService;
  17.     protected $logger;
  18.     public function __construct(
  19.         KintoneService $kintoneService,
  20.         CommonService $commonService,
  21.         \Psr\Log\LoggerInterface $logger
  22.     )
  23.     {
  24.         parent::__construct();
  25.         $this->kintoneService $kintoneService;
  26.         $this->commonService $commonService;
  27.         $this->logger $logger;
  28.     }
  29.     /**
  30.      * @Route("/shipping", name="shipping")
  31.      */    
  32.     public function shipping(Request $request): Response
  33.     {
  34.         
  35.         $today $request->get('today'date("Y-m-d"));
  36.         $category_relation $this->commonService->getCategoryRelation();
  37.         $org_top_categories $this->commonService->getTopCategoryList();
  38.         // カテゴリを配列に持たす
  39.         $top_categories = [];
  40.         foreach($this->getOriginalConfig("param""categories") as $tmp_cate) {
  41.             foreach($org_top_categories as $key => $category){
  42.                 if(preg_match("/$category/"$tmp_cate)) {
  43.                     $top_categories[$tmp_cate][] = $key;
  44.                 }
  45.             }
  46.         }
  47.         $records = [];
  48.         $check_flg false;
  49.         $limit 100;
  50.         $offset 0;
  51.         while(1) {
  52.             $query sprintf('出荷予定日_0 = "%s" offset %d'$today$offset);
  53.             $tmp $this->kintoneService->requestGets('order'$query);
  54.             $records array_merge($records$tmp["records"]);
  55.             $offset += $limit;
  56.             if($tmp["totalCount"] < $offset) {
  57.                 $totalCount $tmp["totalCount"];
  58.                 break;
  59.             }
  60.             if($offset >= 10000 ) break;
  61.         }
  62.         $order["records"] = $records;
  63.         $order["totalCount"] = $totalCount;
  64.         $result["category"] = $top_categories;
  65.         $result["order"] = $order;
  66.         $items = [];
  67.         $shipment_count = [=> 0=> 0=> 0=> 0];
  68.         foreach($result["order"]["records"] as $record) {
  69.             $delivery $record["運送会社"]["value"];
  70.             $sub_table $record["受注商品テーブル"]["value"];
  71.             foreach($sub_table as $row) {
  72.                 $item $row["value"];
  73.                 $item_code $item["商品コード"]["value"];
  74.                 $shipment $item["便番号"]["value"];
  75.                 $shipment_id str_replace("便"""$shipment);
  76.                 $qty $item["箱数"]["value"];
  77.                 if(empty($shipment_id) || empty($item_code)) continue;
  78.                 $item_name $item["受注商品名"]["value"];
  79.                 if(preg_match("/^◆_|^運賃_|_200入|_600入| 200入| 600入/"$item_name)) continue;
  80.                 try{
  81.                     $top_category_code $category_relation[$item_code];
  82.                 }catch(\Exception $e) {
  83.                     $this->logger->log("debug"json_encode($row));
  84.                     die("データエラー");
  85.                 }
  86.                 if(empty($items[$shipment_id])) $items[$shipment_id] = [];
  87.                 if(empty($items[$shipment_id][$delivery])) $items[$shipment_id][$delivery] = [];
  88.                 $okinawa_flg preg_match('/^沖縄県/'$record["お届け先住所1"]["value"]) ? true false;
  89.                 if(isset($items[$shipment_id][$delivery][$item_code])) {
  90.                     $items[$shipment_id][$delivery][$item_code]["qty"]+= (int)$item["箱数"]["value"];
  91.                     if($okinawa_flg) {
  92.                         $items[$shipment_id][$delivery][$item_code]["okinawa_qty"]+= (int)$item["箱数"]["value"];
  93.                     }
  94.                 }else{
  95.                     $items[$shipment_id][$delivery][$item_code] = [
  96.                         'name' => $item["受注商品名"]["value"],
  97.                         'qty' => (int)$item["箱数"]["value"],
  98.                         'category_code' => $top_category_code,
  99.                         'okinawa_flg' => $okinawa_flg true false,
  100.                         'okinawa_qty' => $okinawa_flg ? (int)$item["箱数"]["value"] : 0,
  101.                     ];
  102.                     try{
  103.                         $shipment_count[$shipment_id]++;
  104.                     }catch(Exception $e) {
  105.                         dd($e);
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.         // 対応済みの商品を配列に持たす
  111.         $checked_list $this->getComplete($today);
  112.         $checked = [];
  113.         foreach($checked_list as $val$checked[] = $val[1];
  114.         foreach($items as $shipment_id => $tmp_deliverys) {
  115.             foreach($tmp_deliverys as $delivery_id => $tmp_items) {
  116.                 foreach($tmp_items as $item_code => $item) {
  117.                     $item_key $shipment_id "-" $delivery_id "-" $item_code "-" $item["qty"];
  118.                     $items[$shipment_id][$delivery_id][$item_code]["item_key"] = $item_key;
  119.                     $items[$shipment_id][$delivery_id][$item_code]["checked"] = in_array($item_key$checked) ? true false;
  120.                 }
  121.             }
  122.         }
  123.         $search["shipment_id"] = $request->get('search_shipment_id');
  124.         $search["keyword"] = $request->get('search_keyword');
  125.         $search["okinawa_flg"] = $request->get('search_okinawa_flg');
  126.         $search["category_key"] = $request->get('search_category_key');
  127.         return $this->render('shipping.html.twig', [
  128.             'items' => $items,
  129.             'json_items' => json_encode($items),
  130.             'delivery_list' => $this->getOriginalConfig("param""delivery"),
  131.             'shipment_list' => $this->getOriginalConfig("param""shipment"),
  132.             'shipment_count' => $shipment_count,
  133.             'top_categories' => $top_categories,
  134.             'json_top_categories' => json_encode($top_categories),
  135.             'today' => $today,
  136.             'search' => $search,
  137.         ]);
  138.     }
  139.     /**
  140.      * @Route("/shipping/setComplete", name="shipping_set_complete")
  141.      */   
  142.     public function setComplete(Request $request){
  143.         //フォーマットの統一
  144.         $date $request->get('date');
  145.         $date date("Y-m-d"strtotime($date));
  146.         $key $request->get('key');
  147.         $checked $request->get('checked');
  148.         $this->logger->info('POSTデータ', ["date" => $date"key" => $key"checked" => $checked]);
  149.         $list $this->getComplete($datetrue);
  150.         $new_list = [];
  151.         foreach($list as $value){
  152.             if($checked == && $value[0] == $date && $value[1] == $key) continue;
  153.             $new_list[] = join("\t"$value);
  154.         }
  155.         if($checked == 1){
  156.             $new_list[] = $date "\t" $key;
  157.         }
  158.         $path $this->getCompleteFileName($date);
  159.         $fh fopen($path"w");
  160.         fputs($fhjoin("\n"$new_list));
  161.         fclose($fh);
  162.     }
  163.     /**
  164.      * 済みデータ取得 function
  165.      *
  166.      * @param [type] $date
  167.      * @param boolean $all_flg
  168.      * @return array
  169.      */
  170.     private function getComplete($date$all_flg false){
  171.         //フォーマットの統一
  172.         $date date("Y-m-d"strtotime($date));
  173.         //データは月単位で保存
  174.         $path $this->getCompleteFileName($date);
  175.         $list = [];
  176.         if(is_file($path)) {
  177.             $rows file($path);
  178.             foreach($rows as $row) {
  179.                 $values preg_split("/\t/"trim($row));
  180.                 if($all_flg || $values[0] == $date) {
  181.                     $list[] = $values;
  182.                 }
  183.             }
  184.         }
  185.         return $list;
  186.     }
  187.     
  188.     private function getCompleteFileName($date){
  189.         //データは月単位で保存
  190.         $yymm substr($date07);
  191.         $path __DIR__ "/../../data/shipping/{$yymm}.tsv";
  192.         return $path;
  193.     }
  194. }