TodoConvey.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class TodoConvey extends Base
  6. {
  7. public function sweepCode($data,$userId){
  8. $todo = Db::name('todo')
  9. ->where('id',$data['todoId'])
  10. ->where('del',0)
  11. ->find();
  12. if(!$todo || $todo['to_user_id'] != $userId){
  13. $this->error = '工单不存在';
  14. return false;
  15. }
  16. $orderConvey = Db::name('order_convey')
  17. ->where('order_id',$todo['order_id'])
  18. ->find();
  19. if(!$orderConvey){
  20. $this->error = '运送信息不存在';
  21. return false;
  22. }
  23. if($orderConvey && (new Config())->getConfig('org_scan_qrcode',$todo['org_id']) == 1){ // 强制扫码
  24. if(!$data['code']){
  25. $this->error = '请扫描二维码';
  26. return false;
  27. }else{
  28. $addrs = model('address')->checkAddrCode($data['code'],2,$todo['org_id']);
  29. if(!$addrs){
  30. $this->error = '二维码不正确';
  31. return false;
  32. }
  33. if($data['isStart'] == 0 && $addrs['id'] != $orderConvey['start']){
  34. $this->error = '二维码不正确';
  35. return false;
  36. }
  37. if($data['isStart'] == 1 && $addrs['id'] != $orderConvey['end']){
  38. $this->error = '二维码不正确';
  39. return false;
  40. }
  41. }
  42. if($data['isStart'] == 1 && $todo['work_type_mode'] == 3){
  43. /*// 判断是不是病人运送
  44. $convey = Db::name('order_convey')
  45. ->alias('a')
  46. ->join('convey_cate b','a.type = b.id')
  47. ->where('a.order_id',$todo['order_id'])
  48. ->where('b.cate',1)
  49. ->field('a.*')
  50. ->find();
  51. if($convey){ // 检查途径路线是否全部已扫码
  52. $ret = Db::name('order_convey_end')->where('order_id',$todo['order_id'])->where('scan',0)->find();
  53. if(!$ret){
  54. $this->error = '存在途经点未扫码,请确认';
  55. return false;
  56. }
  57. }*/
  58. }
  59. }else{
  60. if($data['code']){
  61. $addrs = model('address')->checkAddrCode($data['code'],2,$todo['org_id']);
  62. if(!$addrs){
  63. $this->error = '二维码不正确';
  64. return false;
  65. }
  66. if($data['isStart'] == 0 && $addrs['id'] != $orderConvey['start']){
  67. $this->error = '二维码不正确';
  68. return false;
  69. }
  70. if($data['isStart'] == 1 && $addrs['id'] != $orderConvey['end']){
  71. $this->error = '二维码不正确';
  72. return false;
  73. }
  74. }
  75. }
  76. //扫码开始
  77. if($data['isStart'] == 0 && $todo['work_type_mode'] == 3 ){
  78. $orderInfo = Db::name('orders')->where('id',$todo['order_id'])->where('del',0)->find();
  79. if($orderInfo['total_num'] > 0 && $data['num'] <= $orderInfo['total_num']){
  80. $this->error = '输入数量要大于当前已领取数量';
  81. return false;
  82. }
  83. }
  84. if($data['isStart'] == 1 && (new Config())->getConfig('org_scan_qrcode',$todo['org_id'])){
  85. $orderConeyEndList = Db::name('order_convey_end')
  86. ->where('order_id',$todo['order_id'])
  87. ->where('del',0)
  88. ->where('scan',0)
  89. ->select();
  90. if($orderConeyEndList){
  91. $this->error = '有途径点未扫码';
  92. return false;
  93. }
  94. }
  95. $startstatus = 0;
  96. $endstatus = 0;
  97. $todoconvey = Db::name('todo_convey')
  98. ->where('todo_id',$data['todoId'])
  99. ->find();
  100. if($todoconvey){
  101. if($todoconvey['start_time']){
  102. $startstatus = 1;
  103. }
  104. if($todoconvey['end_time']){
  105. $endstatus = 1;
  106. }
  107. }
  108. if($data['isStart'] == 1 && ( $startstatus == 0 || $endstatus == 1)){
  109. $this->error = '无权限操作';
  110. return false;
  111. }
  112. if($data['isStart'] == 0 && ( $startstatus == 1 || $endstatus == 1)){
  113. $this->error = '无权限操作';
  114. return false;
  115. }
  116. $ysConfigSignOff = (new Config())->getConfig('order_ys_sign',$todo['org_id']);
  117. if($ysConfigSignOff == 1 && empty($todoconvey['start_img']) && $data['isStart'] == 0){
  118. $this->error = '请上传开始地点签名';
  119. return false;
  120. }
  121. if($ysConfigSignOff == 1 && empty($todoconvey['end_img']) && $data['isStart'] == 1){
  122. $this->error = '请上传结束地点签名';
  123. return false;
  124. }
  125. $this->startTrans();
  126. try{
  127. if($todoconvey){
  128. if($data['isStart'] == 0){
  129. $d = [
  130. 'start' => $orderConvey['start'],
  131. 'start_time' => date('Y-m-d H:i:s')
  132. ];
  133. $ret = $this
  134. ->where('id',$todoconvey['id'])
  135. ->update($d);
  136. if(!$ret){
  137. \exception('操作失败');
  138. }
  139. }else{
  140. $d = [
  141. 'end' => $orderConvey['end'],
  142. 'end_time' => date('Y-m-d H:i:s')
  143. ];
  144. $ret = $this->where('id',$todoconvey['id'])->update($d);
  145. if(!$ret){
  146. \exception('操作失败');
  147. }
  148. $curTime = date('Y-m-d H:i:s');
  149. $sd = [
  150. 'todo_mode' => 3,
  151. 'images' => $data['img'],
  152. 'done_time' => date('Y-m-d H:i:s'),
  153. 'nodo_reason' => isset($data['content'])?$data['content']:'',
  154. 'wc_time' => strtotime($curTime) - strtotime($todo['confirm_time']),
  155. ];
  156. $ret = Db::name('todo')
  157. ->where('id',$data['todoId'])->update($sd);
  158. if(!$ret){
  159. \exception('操作失败');
  160. }
  161. $ret = (new Todo())->checkToDo($todo['order_id'],$data['todoId'],3);
  162. if(!$ret){
  163. \exception('订单修改失败');
  164. }
  165. }
  166. }else{
  167. $d = [
  168. 'start' => $orderConvey['start'],
  169. 'start_time' => date('Y-m-d H:i:s'),
  170. 'todo_id' => $data['todoId'],
  171. 'end' => $orderConvey['end']
  172. ];
  173. $res = $this->insert($d);
  174. if (!$res) {
  175. \exception('操作失败');
  176. }
  177. }
  178. $crdata = [
  179. 'org_id' => $data['orgId'],
  180. 'user_id' => $userId,
  181. 'create_time' => date('Y-m-d H:i:s'),
  182. 'create_yyyymmdd' => date('Ymd'),
  183. 'create_yyyymm' => date('Ym'),
  184. 'create_yyyy' => date('Y')
  185. ];
  186. if($data['isStart'] == 0){ //始发
  187. $crdata['addr_id'] = $orderConvey['start'];
  188. $crdata['type'] = 1;
  189. }else{
  190. $crdata['addr_id'] = $orderConvey['end'];
  191. $crdata['type'] = 2;
  192. }
  193. //位置签到表
  194. $res = Db::name('convey_plan_record')->insert($crdata);
  195. if (!$res) {
  196. \exception('操作失败');
  197. }
  198. if($data['isStart'] == 0 && $todo['work_type_mode'] == 3 ){
  199. Db::name('orders')->where('id',$todo['order_id'])->update(['total_num'=>$data['num']]);
  200. }
  201. $this->commit();
  202. }catch (Exception $e){
  203. $this->rollback();
  204. $this->error = $e->getMessage();
  205. return false;
  206. }
  207. return true;
  208. }
  209. // 保存签名
  210. public function saveSign($todoId,$type,$img,$userId){
  211. $todo = db('todo')
  212. ->where('id',$todoId)
  213. ->where('del',0)
  214. ->find();
  215. if(!$todo || $todo['to_user_id'] != $userId){
  216. $this->error = '工单不存在';
  217. return false;
  218. }
  219. $orderConvey = db('order_convey')
  220. ->where('order_id',$todo['order_id'])
  221. ->find();
  222. if(!$orderConvey){
  223. $this->error = '工单不存在';
  224. return false;
  225. }
  226. $this->startTrans();
  227. try{
  228. $todoconvey = db('todo_convey')
  229. ->where('todo_id',$todoId)
  230. ->find();
  231. if($todoconvey){
  232. if($type == 1){
  233. $d = [
  234. 'start_img' => $img
  235. ];
  236. }else{
  237. $d = [
  238. 'end_img' => $img
  239. ];
  240. }
  241. $ret = db('todo_convey')
  242. ->where('id',$todoconvey['id'])
  243. ->update($d);
  244. if(!$ret){
  245. \exception('操作失败');
  246. }
  247. }else{
  248. $d = [
  249. 'start' => $orderConvey['start'],
  250. 'todo_id' => $todoId,
  251. 'end' => $orderConvey['end'],
  252. 'start_img' => $type == 1?$img:'',
  253. 'end_img' => $type == 2?$img:'',
  254. ];
  255. $res = db('todo_convey')
  256. ->insert($d);
  257. if (!$res) {
  258. \exception('操作失败');
  259. }
  260. }
  261. $this->commit();
  262. }catch (Exception $e){
  263. $this->rollback();
  264. $this->error = $e->getMessage();
  265. return false;
  266. }
  267. return true;
  268. }
  269. }