DataChange.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\facade\Db;
  5. use think\Model;
  6. use think\model\concern\SoftDelete;
  7. /**
  8. * @mixin \think\Model
  9. */
  10. class DataChange extends Model
  11. {
  12. use SoftDelete;
  13. protected $createTime='createtime';
  14. protected $updateTime='updatetime';
  15. protected $deleteTime = 'delete_time';
  16. protected $append=["orderInfo"];
  17. /**
  18. * @param $v
  19. * @param $row
  20. * @return \app\admin\model\DataChange|array|mixed|\think\Model
  21. */
  22. public function getOrderInfoAttr($v,$row){
  23. $orderinfo=[];
  24. if(isset($row["type"])) {
  25. if ($row['type'] == 1) {
  26. $orderinfo=Db::name("purchease_order")->where(["cgdNo"=>$row['code']])->findOrEmpty();
  27. }
  28. if ($row['type'] == 2) {
  29. $orderinfo=Db::name('sale')->where(['orderCode'=>$row['code']])->findOrEmpty();
  30. }
  31. if ($row['type'] == 3) {
  32. $orderinfo=Db::name('good_zixun')->where(['spuCode'=>$row['code']])->json(["specinfo"])
  33. ->findOrEmpty();
  34. }
  35. }
  36. return $orderinfo;
  37. }
  38. public function updateOrder($data) {
  39. if($data['type']==1){
  40. $this->cgdUpdate($data);
  41. }
  42. if($data['type']==2){
  43. $this->qrdUpdate($data);
  44. }
  45. if($data['type']==3){
  46. $this->goodUpdate($data);
  47. }
  48. }
  49. /**
  50. * @param $row
  51. * @throws \Exception
  52. */
  53. public function cgdUpdate($row){
  54. $purchease = Db::name('purchease_order')->where(['cgdNo'=>$row['code']])->findOrEmpty();
  55. if(empty($purchease)) throw new \Exception("未找到采购单");
  56. $after= json_decode($row->after,true);
  57. foreach ($after as $key=>$item){
  58. $purchease[$key]=$item;
  59. }
  60. $wsm=Db::name('warehouse_info')->where(['supplierNo' => $purchease["supplierNo"], 'companyNo' =>
  61. $purchease['companyNo'], 'wsm_type' => 2, 'is_del' => 0])->findOrEmpty();
  62. if(empty($wsm)){
  63. $wsm_code = makeNo('WSM');
  64. $inwsm = [
  65. 'wsm_code' => $wsm_code,
  66. 'name' =>$purchease['supplier_name'],
  67. 'wsm_type' => 2,
  68. 'supplierNo' =>$purchease['supplierNo'],
  69. 'supplierName' => $purchease['supplier_name'],
  70. 'addr' => '',
  71. 'addrs_code' => '',
  72. 'contactor' => $purchease['good_createrid'],
  73. 'contactor_name' =>$purchease['good_creater'],
  74. 'mobile' => '',
  75. 'position' => '',
  76. 'companyNo' => $purchease['companyNo'],
  77. 'companyName' => $purchease['companyName'],
  78. 'status' => 1,
  79. 'is_del' => 0,
  80. 'addtime' => date('Y-m-d H:i:s'),
  81. 'updatetime' => date('Y-m-d H:i:s'),
  82. 'createrid' => $row->apply_id,
  83. 'creater' => $row->apply_name,
  84. 'updaterid' => $row->apply_id,
  85. 'updater' => $row->apply_name,
  86. ];
  87. $sin= Db::name('warehouse_info')->save($inwsm);
  88. if($sin==false) throw new \Exception('采购单对应得仓库创建失败');
  89. }else $wsm_code = $wsm['wsm_code'];
  90. if($purchease['wsm_code']!=$wsm_code){
  91. $good_stock= Db::name("good_stock")->where(["spuCode"=>$purchease['spuCode'],
  92. "wsm_code"=>$purchease['wsm_code']])->findOrEmpty();
  93. if(empty(($good_stock))) throw new \Exception('商品库存数据未找到');
  94. $good_stock['wsm_code'] = $wsm_code;
  95. $goodup =Db::name('good_stock')->save($good_stock);
  96. if($goodup==false) throw new \Exception('商品库存数据更新失败');
  97. $purchease['wsm_code'] = $wsm_code;
  98. }
  99. $purchease['updatetime'] = date("Y-m-d H:i:s");
  100. $up=Db::name('purchease_order')->save($purchease);
  101. if($up==false) throw new \Exception('采购单数据更新失败');
  102. $this->upQrd($purchease);
  103. $good=Db::name("good_zixun")->where(["spuCode"=>$purchease['spuCode']])->findOrEmpty();
  104. if(empty($good))throw new \Exception('商品数据未找到');
  105. $good['supplierNo']=$purchease['supplierNo'];
  106. $good['supplierName']=$purchease['supplier_name'];
  107. $goodup = Db::name("good_zixun")->save($good);
  108. if($goodup ==false) throw new \Exception('商品数据更新失败');
  109. }
  110. /**
  111. * @param $row
  112. * @throws \Exception
  113. */
  114. public function qrdUpdate($row){
  115. $sale =Db::name("sale")->where("orderCode",$row->code)->findOrEmpty();
  116. if(empty($sale)) throw new \Exception("销售单数据未找到");
  117. $after= json_decode($row->after,true);
  118. foreach ($after as $key=>$item){
  119. $sale[$key]=$item;
  120. }
  121. $sale['updatetime']=date('Y-m-d H:i:s');
  122. $saleup=Db::name('sale')->save($sale);
  123. if($saleup==false) throw new \Exception('销售单数据更新失败');
  124. $outchild =Db::name('order_out_child')->where(['orderCode'=>$sale['orderCode'],'is_del'=>0])->findOrEmpty();
  125. if(empty($outchild)==false){
  126. $outChidup=Db::name('order_out_child')->where(['orderCode'=>$sale['orderCode'],'is_del'=>0])->update
  127. ([
  128. 'customer_code'=>$sale['customer_code'],
  129. 'customer_name'=>$sale['customerName'],
  130. 'updatetime'=>date('Y-m-d H:i:s')]);
  131. if($outChidup==false) throw new \Exception('发货工单数据更新失败');
  132. }
  133. }
  134. /**
  135. * @param $cgd
  136. * @throws \think\Exception
  137. * @throws \think\db\exception\DbException
  138. */
  139. public function upQrd($cgd){
  140. $qrd = Db::name("sale")->alias("a")
  141. ->leftJoin("order_num b","a.orderCode=b.orderCode")
  142. ->where("b.cgdNo",$cgd['cgdNo'])
  143. ->field("a.*")->findOrEmpty();
  144. if(empty($qrd)) throw new \Exception('未找到对应得销售单');
  145. $data=["supNo"=>$cgd['supplierNo'],"supName"=>$cgd['supplier_name'],"updatetime"=>date("Y-m-d H:i:s")];
  146. $up=Db::name("sale")->where($qrd)->update($data);
  147. if($up==false) throw new \Exception('销售单数据更新失败');
  148. $out =Db::name("order_out")->where(["orderCode"=>$qrd['orderCode'],"is_del"=>0])->findOrEmpty();
  149. if(!empty($out)){
  150. $outUp= Db::name('order_out')->where(['orderCode'=>$qrd['orderCode'],'is_del'=>0])->update
  151. (["wsm_code"=>$cgd['wsm_code'],"updatetime"=>date('Y-m-d H:i:s')]);
  152. if($outUp==false) throw new \Exception('发货单数据更新失败');
  153. }
  154. $outchild =Db::name('order_out_child')->where(['orderCode'=>$qrd['orderCode'],'is_del'=>0])->findOrEmpty();
  155. if(!empty($outchild)){
  156. $outChidup=Db::name('order_out_child')->where(['orderCode'=>$qrd['orderCode'],'is_del'=>0])->update
  157. (['wsm_code'=>$cgd['wsm_code'],
  158. "supplierNo"=>$cgd['supplierNo'],
  159. "supplierName"=>$cgd['supplier_name'],
  160. 'updatetime'=>date('Y-m-d H:i:s')]);
  161. if($outChidup==false) throw new \Exception('发货工单数据更新失败');
  162. }
  163. }
  164. /**
  165. * @param $row
  166. */
  167. public function goodUpdate($row){
  168. $good = Db::name("good_zixun")->where("spuCode",$row->code)->findOrEmpty();
  169. if(empty($good))throw new \Exception('未找到对应得商品数据');
  170. $after= json_decode($row->after,true);
  171. foreach ($after as $key=>$item){
  172. $good[$key]=$item;
  173. }
  174. $good['updatetime']=date("Y-m-d H:i:s");
  175. $goodup= Db::name('good_zixun')->save($good);
  176. if($goodup==false) throw new \Exception('商品数据更新失败');
  177. $saleup =Db::name("sale")->where("good_code",$row->code)->update(["updatetime"=>date('Y-m-d H:i:s')]);
  178. if($saleup==false) throw new \Exception('商品数据销售单更新失败');
  179. $cgdup =Db::name("purchease_order")->where("spuCode",$row->code)->update(["updatetime"=>date('Y-m-d
  180. H:i:s')]);
  181. if($cgdup==false) throw new \Exception('商品数据采购单更新失败');
  182. }
  183. }