src/Controller/StockControoler.php line 39

Open in your IDE?
  1. <?php
  2. /**
  3.  * 生産実績管理用コントローラー
  4.  * 
  5.  */
  6. namespace App\Controller;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use App\Controller\BaseController;
  12. use App\Service\KintoneService;
  13. use App\Service\CommonService;
  14. use Symfony\Component\Filesystem\Filesystem;
  15. class StockControoler extends BaseController
  16. {
  17.     protected $kintoneService;
  18.     protected $commonService;
  19.     protected $logger;
  20.     public function __construct(
  21.         KintoneService $kintoneService,
  22.         CommonService $commonService,
  23.         \Psr\Log\LoggerInterface $logger
  24.     )
  25.     {
  26.         parent::__construct();
  27.         $this->kintoneService $kintoneService;
  28.         $this->commonService $commonService;
  29.         $this->logger $logger;
  30.     }
  31.     /**
  32.      * @Route("/stock", name="stock")
  33.      */    
  34.     public function index(Request $request): Response
  35.     {
  36.         //検査者のリストを取得
  37.         $query "order by 表示順 asc";
  38.         $result $this->kintoneService->requestGets('staff'$query);
  39.         $records $result["records"];
  40.         $staff = [];
  41.         foreach($records as $record$staff[] = $record["検査者"]["value"];
  42.         //指定日の生産管理データ取得
  43.         $today $request->get('today'date("Y-m-d"));
  44.         //$query = sprintf('手配日 = "%s"', $today);
  45.         $query sprintf('手配日 = "%s" or (手配日 < "%s" and 全て完了 not in ("完了")) order by 製造号機 asc, 手配日 asc'$today$today);
  46.         $result $this->kintoneService->requestGets('production_management'$query);
  47.         $records $result["records"];
  48.         
  49.         $item_codes = [];
  50.         $arrangement_item = [];
  51.         foreach($records as $record){
  52.             $line $record["製造号機"]["value"];
  53.             $arrangement_list $record["手配状況"]["value"];
  54.             $all_complete $record["全て完了"]["value"];
  55.             $arrangement_date $record["手配日"]["value"];
  56.             
  57.             foreach($arrangement_list as $tmp_item) {
  58.                 $id $tmp_item["id"];
  59.                 $item $tmp_item["value"];
  60.                 $item_code $item["商品コード"]["value"];
  61.                 if(empty($item_code)) continue;
  62.                 $no $this->commonService->convertDispNo($item["No"]["value"], $item["完了No"]["value"]);
  63.                 $num = (int)$item["ケース"]["value"];
  64.                 $complete_num = (int)$item["完了数"]["value"];
  65.                 $key $item["key"]["value"];
  66.                 $complete $item["完了"]["value"];
  67.                 $quickly = !empty($item["急ぎ"]["value"]) ? true false;
  68.                 if(empty($complete)) {
  69.                     $arrangement_item[$line][$arrangement_date][] = [
  70.                         "id" => $id,
  71.                         "item_code" => $item_code,
  72.                         "num" => $num,
  73.                         "no" => $no,
  74.                         "complete_num" => $complete_num,
  75.                         "diff" => $num $complete_num,
  76.                         "key" => $key,
  77.                         "quickly" => $quickly,
  78.                     ];
  79.                 }
  80.                 if(!in_array($item_code$item_codes)) $item_codes[] = $item_code;
  81.             }
  82.         }
  83.         $data["arrangement"] = $arrangement_item;
  84.         $query sprintf('商品コード in (%s) order by レコード番号''"' join('","'$item_codes) . '"');
  85.         $result $this->kintoneService->requestGets('stock'$query);
  86.         $data["item"] = [];
  87.         foreach($result["records"] as $item) {
  88.             $item_name $item["商品名"]["value"];
  89.             $item_code $item["商品コード"]["value"];
  90.             //商品マスタ
  91.             $data["item"][$item_code] = [
  92.                 "code" => $item_code,
  93.                 "name" => $item_name,
  94.             ];
  95.         }
  96.         $search["machine"] = $request->get('search_machine');
  97.         return $this->render('stock.html.twig', [
  98.             "today" => $today,
  99.             "json_items" => json_encode($data["item"]),
  100.             "json_arrangement" => json_encode($data["arrangement"]),
  101.             "json_memo" => json_encode($this->getMemo(true)),
  102.             "json_log" => json_encode($this->getStockLog()),
  103.             "staff" => $staff,
  104.             'machine_list' => $this->getOriginalConfig("param""machines"),
  105.             "search" => $search,
  106.         ]);
  107.     }
  108.     /**
  109.      * @Route("/set_stock", name="set_stock")
  110.      */    
  111.     public function setStock(Request $request)
  112.     {
  113.         $search_date $request->get('search_date');
  114.         $line $request->get('line');
  115.         $key $request->get('key');
  116.         $qty $request->get('qty');
  117.         $no $request->get('no');
  118.         $staff $request->get('staff');
  119.         $add_complete_no $this->commonService->convertNo($no);
  120.         $this->logger->log("debug"sprintf("set stock date:%s line:%s key:%s qty:%s"$search_date$line$key$qty ));
  121.         //現状を取得
  122.         $query sprintf('手配日 = "%s" and 製造号機 in ("%s")'$search_date$line);
  123.         $result $this->kintoneService->requestGets('production_management'$query);
  124.         $stock_code "";
  125.         $add_qty $qty;
  126.         foreach($result["records"] as $record){
  127.             $kintone_id $record['$id']['value'];
  128.             $arrangement_list $record["手配状況"]["value"];
  129.             $all_complete true;
  130.             foreach($arrangement_list as $tmp_item) {
  131.                 $item $tmp_item["value"];
  132.                 $id $tmp_item["id"];
  133.                 if(empty($item["商品コード"]["value"])) continue;
  134.                 $stock_code $item["商品コード"]["value"];
  135.                 if($key == $item["key"]["value"]) {
  136.                     $num = (int)$item["ケース"]["value"];
  137.                     $complete_num = (int)$item["完了数"]["value"] + $qty;
  138.                     if($num <= $complete_num$item_complete = ["完了"];
  139.                     else {
  140.                         $item_complete = [];
  141.                         $all_complete false;
  142.                     }
  143.                     
  144.                     //既存の状態 + 追加分
  145.                     $complete_no $item["完了No"]["value"] ? $item["完了No"]["value"] . "," $add_complete_no $add_complete_no;
  146.                     $tmp = [
  147.                         "完了数" => $complete_num,
  148.                         "完了No" => $complete_no,
  149.                         "完了" => $item_complete
  150.                     ];
  151.                     $new_items[] = [ "id" => $id"value" => $this->kintoneService->setAttribute($tmp) ];
  152.                     //ログを残す
  153.                     $log_params = [
  154.                         "item_code" => $item["商品コード"]["value"],
  155.                         "item_name" => $item["商品名"]["value"],
  156.                         "qty" => $qty,
  157.                         "no" => $no,
  158.                         "staff" => $staff,
  159.                         "num" => $num,
  160.                         "complete_num" => $complete_num,
  161.                         "complete" => join(","$item_complete)
  162.                     ];
  163.                     $this->setStockLog($line$log_params);
  164.                     $update_stock_code $item["商品コード"]["value"];
  165.                 }else{
  166.                     if(empty($item["完了"]["value"])) $all_complete false;
  167.                     $new_items[] = [ "id" => $id ];
  168.                 }
  169.             }
  170.         }
  171.         if(!empty($new_items)) {
  172.             $management["手配状況"] = $new_items;
  173.             if($all_complete) {
  174.                 $management["全て完了"] = ["完了"];
  175.             }
  176.             $management_data $this->kintoneService->setAttribute($management);
  177.             $result $this->kintoneService->requestPut("production_management"$kintone_id$management_data);
  178.         }
  179.         //在庫のセット
  180.         if($update_stock_code){
  181.             $query sprintf('商品コード = "%s" order by レコード番号'$update_stock_code);
  182.             $result $this->kintoneService->requestGets('stock'$query);
  183.             foreach($result['records'] as $record) {
  184.                 $kintone_id $record['$id']['value'];
  185.                 $stock_data['在庫数']['value'] = $record['在庫数']['value'] + $add_qty;
  186.                 //$this->logger->log("debug", sprintf("record_id:%s: new stock_count: %d", $kintone_id, $stock_data['在庫数'] ));
  187.                 $result $this->kintoneService->requestPut("stock"$kintone_id$stock_data);
  188.             }
  189.         }
  190.         return new JsonResponse(["log" => $this->getStockLog()]);
  191.     }
  192.     /**
  193.      * @Route("/stock/setMemo", name="stock_set_memo")
  194.      */   
  195.     public function seMemo(Request $request){
  196.         $line_id $request->get('line_id');
  197.         $memo $request->get('memo');
  198.         $this->logger->info('POSTデータ', ["line_id" => $line_id"memo" => $memo]);
  199.         $path $this->getStockMemoFileName($line_id);
  200.         $fh fopen($path"w");
  201.         fputs($fh$memo);
  202.         fclose($fh);
  203.         return new JsonResponse($this->getMemo());
  204.     }
  205.     /**
  206.      * 保存されている製造号機ごとのメモを取得 function
  207.      *
  208.      * @param string $line_id
  209.      * @return array
  210.      */
  211.     private function getMemo($escape_flg false){
  212.         foreach($this->getOriginalConfig("param""machines") as $key => $value) {
  213.             $path $this->getStockMemoFileName($key);
  214.             $memo[$key] = is_file($path) ? file_get_contents($path) : "";
  215.             if($escape_flg) {
  216.                 $memo[$key] = preg_replace("/\n/","\\n"$memo[$key]);
  217.             }
  218.         }
  219.         return $memo;
  220.     }
  221.     
  222.     /**
  223.      * 製造号機ごとのメモファイルを取得
  224.      *
  225.      * @param [type] $machine_id
  226.      * @return string
  227.      */
  228.     private function getStockMemoFileName($machine_id){
  229.         $path __DIR__ "/../../data/stock/{$machine_id}.tsv";
  230.         return $path;
  231.     }
  232.     /**
  233.      * 製造号機の値からIDを取得
  234.      *
  235.      * @param [type] $line
  236.      * @return void
  237.      */
  238.     private function getMachineId($line){
  239.         foreach($this->getOriginalConfig("param""machines") as $key => $value) {
  240.             if($line == $value) return $key;
  241.         }
  242.     }
  243.     /**
  244.      * 製造号機ごとにログを残す
  245.      *
  246.      * @param [type] $line
  247.      * @param [type] $params
  248.      */
  249.     private function setStockLog($line$log_params) {
  250.         $machine_id $this->getMachineId($line);
  251.         $path $this->getStockLogFileName$machine_id );
  252.         $lock_handler $this->lfFileLock($path);
  253.         
  254.         $lines date("Y-m-d H:i:s") . "\t" serialize($log_params) . "\n";
  255.         $data is_file($path) ? file($path) : [];
  256.         //とりあえず100行分残す
  257.         for($i=0$i<count($data); $i++) {
  258.             $row trim($data[$i]);
  259.             if(empty($row)) continue;
  260.             $lines.= $row "\n";
  261.             if($i == 100) break;
  262.         }
  263.         $fh fopen($path"w");
  264.         fputs($fh$lines);
  265.         fclose($fh);
  266.         if(!$lock_handler) {
  267.             $this->logger->debug('ロックエラー', ["file" => $path"param" => $log_params]);
  268.         }else{
  269.             fclose($lock_handler);
  270.         }
  271.     }
  272.     /**
  273.      * 製造号機ごとのログファイルを取得
  274.      *
  275.      * @param [type] $machine_id
  276.      * @param [type] $date
  277.      * @return string
  278.      */
  279.     private function getStockLogFileName($machine_id$date null) {
  280.         if(is_null($date)) $date date("Y-m");
  281.         $path __DIR__ "/../../data/stock/{$machine_id}_log.tsv";
  282.         return $path;
  283.     }
  284.     /**
  285.      * 製造号機ごとの操作ログを取得
  286.      *
  287.      * @param [type] $date
  288.      * @return array
  289.      */
  290.     private function getStockLog($date null) {
  291.         $logs = [];
  292.         foreach($this->getOriginalConfig("param""machines") as $key => $value) {
  293.             $path $this->getStockLogFileName($key);
  294.             $rows is_file($path) ? file($path) : [];
  295.             $logs[$key] = [];
  296.             foreach($rows as $row){
  297.                 $row trim($row);
  298.                 if(empty($row)) continue;
  299.                 $log_data preg_split("/\t/"$row);
  300.                 $logs[$key][] = [
  301.                     "datetime" => $log_data[0],
  302.                     "log" => !empty($log_data[1]) ? unserialize($log_data[1]) : null,
  303.                 ];
  304.             }
  305.         }
  306.         return $logs;
  307.     }
  308. }