Worker.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\service\auth\MysqlAdapter;
  4. use app\common\model\ScanLog;
  5. use app\common\model\StockConfig;
  6. use app\api\service\StockService;
  7. use app\common\model\StockLog;
  8. use think\exception\HttpResponseException;
  9. use think\exception\ValidateException;
  10. use app\api\validate\Worker as WorkerValidate;
  11. use think\facade\Cache;
  12. use think\facade\Db;
  13. use think\Response;
  14. use app\common\model\GroupUser as GroupUserModel;
  15. use app\api\controller\GroupUser as GroupUserController;
  16. use app\common\model\ShopDelivery as ShopDeliveryModel;
  17. use app\common\model\WorkerOut as WorkerOutModel;
  18. use app\common\model\PackSpecs as PackSpecsModel;
  19. /**
  20. * 打包工人相关接口
  21. */
  22. class Worker extends Base
  23. {
  24. protected $shopDeliveryModel = null;
  25. protected $groupUserController = null;
  26. protected $quantity_sum = 0;
  27. protected $labor_cost_money_sum = 0;
  28. protected $quantity_avg = 0;
  29. protected $labor_cost_money_avg = 0;
  30. protected $worker_num = 0;
  31. protected $worker_list = [];
  32. protected $time = null;
  33. /**
  34. * 判断权限
  35. * @access protected
  36. */
  37. protected function _initialize()
  38. {
  39. parent::_initialize();
  40. $this->shopDeliveryModel = new ShopDeliveryModel();
  41. $this->groupUserController = new GroupUserController();
  42. if (!str_contains($this->userinfo['role'], "1")) {
  43. $this->error(__('没有权限' . $this->userinfo['role']));
  44. }
  45. }
  46. //首页
  47. public function index(StockConfig $stockConfig, ScanLog $scanLog)
  48. {
  49. $data = [];
  50. $data['today'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'today')->count();
  51. $data['yesterday'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'yesterday')->count();
  52. $todayTime = date("Y-m-d");
  53. $startTime = strtotime($todayTime . ' 00:00:00');
  54. $startTime = date('Y-m-d H:i:s', $startTime);
  55. $endTime = strtotime($todayTime . ' 23:59:59');
  56. $endTime = date('Y-m-d H:i:s', $endTime);
  57. $time = $startTime . ',' . $endTime;
  58. $arr = explode(',', $time);
  59. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  60. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  61. $pid = $this->userinfo['id'];
  62. $where[] = ['sl.user_id', '=', $pid];
  63. $list = $scanLog->alias('sl')
  64. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  65. ->where('sl.order_status', 1)
  66. ->where('sl.spec_id', '<>', null)
  67. ->where($where)
  68. ->field("sl.code,sl.spec_id,SUM(COALESCE(sd.num,0) * 1) as pack_count,SUM(COALESCE(sd.num,0) * COALESCE(sd.labor_cost_money,0)) as pack_money")
  69. ->group('sl.spec_id')
  70. ->select()->each(function ($item, $key) {
  71. $spec_name = $this->get_order_info($item['code']);
  72. $item['pack_name'] = $spec_name;
  73. unset($item['code']);
  74. });
  75. $data['pack_specs'] = $list;
  76. // $data['pack_specs'] = [
  77. // [
  78. // 'pack_name' => '三斤',
  79. // 'pack_count' => '120',
  80. // 'pack_money' => '100'
  81. // ],
  82. // [
  83. // 'pack_name' => '五斤',
  84. // 'pack_count' => '120',
  85. // 'pack_money' => '100'
  86. // ],
  87. // [
  88. // 'pack_name' => '五斤保温',
  89. // 'pack_count' => '120',
  90. // 'pack_money' => '100'
  91. // ],
  92. // ];
  93. $this->success('ok', $data);
  94. }
  95. //添加出入库
  96. public function scan(ScanLog $scanLog)
  97. {
  98. $data = $this->request->post();
  99. $result = false;
  100. Db::startTrans();
  101. $insert_data = [];
  102. try {
  103. validate(WorkerValidate::class)->scene('scan')->check($data);
  104. //$resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  105. $time = time();
  106. $insert_data = [
  107. 'user_id' => $this->userinfo['id'],
  108. 'code' => $data['code'],
  109. 'order_status' => 0,
  110. 'remark' => '未找到单号',
  111. 'order_data' => [],
  112. 'createtime' => date('Y-m-d H:i:s', $time)
  113. ];
  114. $sql_data = $this->shopDeliveryModel->where('waybill_no', $data['code'])->find();
  115. if (!empty($sql_data)) {
  116. switch ($sql_data['order_status']) {
  117. case 'TRADE_CLOSED':
  118. $insert_data['order_status']=3;
  119. $insert_data['remark'] = '已退款';
  120. break;
  121. default:
  122. if (!empty($sql_data['user_id']) && $sql_data['user_id'] != 0) {
  123. $insert_data['order_status'] = 2;
  124. $insert_data['remark'] = '请勿重复录入';
  125. } else {
  126. $result = $sql_data->save([
  127. 'user_id' => $this->userinfo['id'],
  128. 'entry_time' => $time,
  129. 'updatetime' => $time
  130. ]);
  131. if ($result) {
  132. $insert_data['order_status'] = 1;
  133. $insert_data['remark'] = '录入成功';
  134. $insert_data['spec_id'] = $sql_data['spec_id'];
  135. } else {
  136. $insert_data['remark'] = '录入失败';
  137. }
  138. }
  139. $entry_time = $sql_data['entry_time'];
  140. if (!empty($sql_data['entry_time'])) {
  141. $entry_time = date('Y-m-d H:i:s', $sql_data['entry_time']);
  142. }
  143. switch ($sql_data['incubator']) {
  144. case 1:
  145. $sql_data['incubator_title'] = '单层保温';
  146. break;
  147. case 2:
  148. $sql_data['incubator_title'] = '双层保温';
  149. break;
  150. default:
  151. $sql_data['incubator_title'] = '不是保温箱';
  152. break;
  153. }
  154. $sql_data['entry_time'] = $entry_time;
  155. $insert_data['order_data'] = $sql_data;
  156. break;
  157. }
  158. }
  159. $result = $scanLog->save($insert_data);
  160. Db::commit();
  161. } catch (ValidateException $e) {
  162. return $this->error($e->getError());
  163. } catch (\Exception $e) {
  164. Db::rollback();
  165. $this->error($e->getMessage());
  166. }
  167. if ($result === false) {
  168. $this->error(__('没有新增任何数据'));
  169. }
  170. $this->success('', $insert_data);
  171. }
  172. //扫描记录
  173. public function scanlog(ScanLog $scanLog)
  174. {
  175. $where = [];
  176. $limit = $this->request->post('limit/d', 10); //条数
  177. $time = $this->request->post('create_time/s'); //日期
  178. // if (!empty($type)) $where[] = ['a.type', '=', $type];
  179. // if (!empty($type_id)) $where[] = ['a.type_id', '=', $type_id];
  180. // if (!empty($spec_id)) $where[] = ['a.variety_id', '=', $spec_id];
  181. if (!empty($time)) {
  182. $arr = explode(',', $time);
  183. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  184. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  185. }
  186. $list = $scanLog->alias('sl')
  187. ->where('user_id', $this->userinfo['id'])
  188. ->whereIn('order_status', [0, 1, 2])
  189. ->where($where)
  190. ->order('createtime desc')
  191. ->paginate($limit)
  192. ->each(function ($item, $key) {
  193. $order_data = $this->shopDeliveryModel->where('waybill_no', $item['code'])->find();
  194. $item['order_data'] = [];
  195. if (!empty($order_data['waybill_no'])) {
  196. $incubator_title = '不是保温箱';
  197. switch ($order_data['incubator']) {
  198. case 1:
  199. $incubator_title = '单层保温';
  200. break;
  201. case 2:
  202. $incubator_title = '双层保温';
  203. break;
  204. }
  205. $item['order_data'] = $order_data;
  206. $item['order_data']['incubator_title'] = $incubator_title;
  207. }
  208. return $item;
  209. });
  210. $this->success('ok', $list);
  211. }
  212. //数据统计,今日昨日,本周
  213. public function pack_data_statistics(ScanLog $scanLog, GroupUserModel $groupUserModel)
  214. {
  215. $where = [];
  216. $limit = $this->request->post('limit/d', 10); //条数
  217. $todayTime = date("Y-m-d");
  218. $startTime = strtotime($todayTime . ' 00:00:00');
  219. $startTime = date('Y-m-d H:i:s', $startTime);
  220. $endTime = strtotime($todayTime . ' 23:59:59');
  221. $endTime = date('Y-m-d H:i:s', $endTime);
  222. $time = $startTime . ',' . $endTime;
  223. $time = $this->request->post('create_time/s', $time); //日期
  224. $this->time = $time;
  225. if (!empty($time)) {
  226. $arr = explode(',', $time);
  227. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  228. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  229. }
  230. $pid = $this->userinfo['id'];
  231. $where[] = ['sl.user_id', '=', $pid];
  232. $list = $scanLog->alias('sl')
  233. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  234. ->where('sl.order_status', 1)
  235. ->where('sl.spec_id', '<>', null)
  236. ->where($where)
  237. ->field("sl.code,sl.spec_id,SUM(COALESCE(sd.num,0) * 1) as spec_quantity_sum,SUM(COALESCE(sd.num,0) * COALESCE(sd.labor_cost_money,0)) as spec_labor_cost_money_sum")
  238. ->group('sl.spec_id')
  239. ->paginate($limit)->each(function ($item, $key) {
  240. $spec_name = $this->get_order_info($item['code']);
  241. $item['spec_name'] = $spec_name;
  242. unset($item['code']);
  243. });
  244. $list_sum = $scanLog->alias('sl')
  245. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  246. ->where('sl.order_status', 1)
  247. ->where('sl.spec_id', '<>', null)
  248. ->where($where)
  249. ->field("sl.code,sl.spec_id,SUM(COALESCE(sd.num,0) * 1) as spec_quantity_sum,SUM(COALESCE(sd.num,0) * COALESCE(sd.labor_cost_money,0)) as spec_labor_cost_money_sum")
  250. ->group('sl.spec_id')
  251. ->select();
  252. $quantity_sum = 0;
  253. $labor_cost_money_sum = 0;
  254. foreach ($list_sum as $item) {
  255. $spec_quantity_sum = $item['spec_quantity_sum'];
  256. $spec_labor_cost_money_sum = $item['spec_labor_cost_money_sum'];
  257. $quantity_sum = bcadd(strval($quantity_sum), strval($spec_quantity_sum), 0);
  258. $labor_cost_money_sum = bcadd(strval($labor_cost_money_sum), strval($spec_labor_cost_money_sum), 4);
  259. $labor_cost_money_sum = round((float)$labor_cost_money_sum, 2);
  260. }
  261. $data = [
  262. 'quantity_sum' => $quantity_sum,
  263. 'labor_cost_money_sum' => $labor_cost_money_sum,
  264. 'list' => empty($list) ? [] : $list
  265. ];
  266. $this->success('ok', $data);
  267. }
  268. //获取数量、总价
  269. public function quantity_labor_cost_money(ScanLog $scanLog)
  270. {
  271. $where = [];
  272. $todayTime = date("Y-m-d");
  273. $startTime = strtotime($todayTime . ' 00:00:00');
  274. $startTime = date('Y-m-d H:i:s', $startTime);
  275. $endTime = strtotime($todayTime . ' 23:59:59');
  276. $endTime = date('Y-m-d H:i:s', $endTime);
  277. $time = $startTime . ',' . $endTime;
  278. $time = $this->request->post('create_time/s', $time); //日期
  279. $this->time = $time;
  280. if (!empty($time)) {
  281. $arr = explode(',', $time);
  282. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  283. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  284. }
  285. $pid = $this->userinfo['id'];
  286. $where[] = ['sl.user_id', '=', $pid];
  287. $list_sum = $scanLog->alias('sl')
  288. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  289. ->where('sl.order_status', 1)
  290. ->where('sl.spec_id', '<>', null)
  291. ->where($where)
  292. ->field("sl.code,sl.spec_id,SUM(COALESCE(sd.num,0) * 1) as spec_quantity_sum,SUM(COALESCE(sd.num,0) * COALESCE(sd.labor_cost_money,0)) as spec_labor_cost_money_sum")
  293. ->group('sl.spec_id')
  294. ->select();
  295. $quantity_sum = 0;
  296. $labor_cost_money_sum = 0;
  297. foreach ($list_sum as $item) {
  298. $spec_quantity_sum = $item['spec_quantity_sum'];
  299. $spec_labor_cost_money_sum = $item['spec_labor_cost_money_sum'];
  300. $quantity_sum = bcadd(strval($quantity_sum), strval($spec_quantity_sum), 0);
  301. $labor_cost_money_sum = bcadd(strval($labor_cost_money_sum), strval($spec_labor_cost_money_sum), 4);
  302. $labor_cost_money_sum = round((float)$labor_cost_money_sum, 2);
  303. }
  304. $data = [
  305. 'quantity_sum' => $quantity_sum,
  306. 'labor_cost_money_sum' => $labor_cost_money_sum
  307. ];
  308. return $data;
  309. }
  310. //获取今天出工人员状态,0=未出个,1=出工
  311. public function get_worker_out_state($id, $pid, $time)
  312. {
  313. $groupUserModel = new GroupUserModel();
  314. $count = $groupUserModel->where('id', $id)->where('pid', $pid)->count();
  315. if ($count == 0) return 1;
  316. $workerOutModel = new WorkerOutModel();
  317. $todayTime = strtotime('today');
  318. $where = [];
  319. $where[] = ['user_id', '=', $id];
  320. $where[] = ['pid', '=', $pid];
  321. $where[] = ['state', '=', 1];
  322. if (!empty($time)) {
  323. $arr = explode(',', $time);
  324. $where[] = ['createtime', '>=', strtotime($arr[0])];
  325. $where[] = ['createtime', '<=', strtotime($arr[1])];
  326. }
  327. return $workerOutModel->where($where)->count();
  328. }
  329. public function get_order_info($waybill_no)
  330. {
  331. return $this->shopDeliveryModel->where('waybill_no', $waybill_no)->value('spec_name');
  332. }
  333. }