123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\model;
- use think\facade\Db;
- use think\Model;
- use think\model\concern\SoftDelete;
- /**
- * @mixin \think\Model
- */
- class DataChange extends Model
- {
- use SoftDelete;
- protected $createTime='createtime';
- protected $updateTime='updatetime';
- protected $deleteTime = 'delete_time';
- protected $append=["orderInfo"];
- /**
- * @param $v
- * @param $row
- * @return \app\admin\model\DataChange|array|mixed|\think\Model
- */
- public function getOrderInfoAttr($v,$row){
- $orderinfo=[];
- if(isset($row["type"])) {
- if ($row['type'] == 1) {
- $orderinfo=Db::name("purchease_order")->where(["cgdNo"=>$row['code']])->findOrEmpty();
- }
- if ($row['type'] == 2) {
- $orderinfo=Db::name('sale')->where(['orderCode'=>$row['code']])->findOrEmpty();
- }
- if ($row['type'] == 3) {
- $orderinfo=Db::name('good_zixun')->where(['spuCode'=>$row['code']])->json(["specinfo"])
- ->findOrEmpty();
- }
- }
- return $orderinfo;
- }
-
- public function updateOrder($data) {
- if($data['type']==1){
- $this->cgdUpdate($data);
- }
- if($data['type']==2){
- $this->qrdUpdate($data);
- }
- if($data['type']==3){
- $this->goodUpdate($data);
- }
- }
-
- /**
- * @param $row
- * @throws \Exception
- */
- public function cgdUpdate($row){
- $purchease = Db::name('purchease_order')->where(['cgdNo'=>$row['code']])->findOrEmpty();
- if(empty($purchease)) throw new \Exception("未找到采购单");
- $after= json_decode($row['after'],true);
- foreach ($after as $key=>$item){
- $purchease[$key]=$item;
- }
- $wsm=Db::name('warehouse_info')->where(['supplierNo' => $purchease["supplierNo"], 'companyNo' =>
- $purchease['companyNo'], 'wsm_type' => 2, 'is_del' => 0])->findOrEmpty();
- if(empty($wsm)){
- $wsm_code = makeNo('WSM');
- $inwsm = [
- 'wsm_code' => $wsm_code,
- 'name' =>$purchease['supplier_name'],
- 'wsm_type' => 2,
- 'supplierNo' =>$purchease['supplierNo'],
- 'supplierName' => $purchease['supplier_name'],
- 'addr' => '',
- 'addrs_code' => '',
- 'contactor' => $purchease['good_createrid'],
- 'contactor_name' =>$purchease['good_creater'],
- 'mobile' => '',
- 'position' => '',
- 'companyNo' => $purchease['companyNo'],
- 'companyName' => $purchease['companyName'],
- 'status' => 1,
- 'is_del' => 0,
- 'addtime' => date('Y-m-d H:i:s'),
- 'updatetime' => date('Y-m-d H:i:s'),
- 'createrid' => $row['apply_id'],
- 'creater' => $row['apply_name'],
- 'updaterid' => $row['apply_id'],
- 'updater' => $row['apply_name'],
- ];
- $sin= Db::name('warehouse_info')->save($inwsm);
- if($sin==false) throw new \Exception('采购单对应得仓库创建失败');
- }else $wsm_code = $wsm['wsm_code'];
- if($purchease['wsm_code']!=$wsm_code){
- $good_stock= Db::name("good_stock")->where(["spuCode"=>$purchease['spuCode'],
- "wsm_code"=>$purchease['wsm_code']])->findOrEmpty();
- if(empty(($good_stock))) throw new \Exception('商品库存数据未找到');
- $good_stock['wsm_code'] = $wsm_code;
- $goodup =Db::name('good_stock')->save($good_stock);
- if($goodup==false) throw new \Exception('商品库存数据更新失败');
- $purchease['wsm_code'] = $wsm_code;
- }
- $purchease['updatetime'] = date("Y-m-d H:i:s");
- $up=Db::name('purchease_order')->save($purchease);
- if($up==false) throw new \Exception('采购单数据更新失败');
- $this->upQrd($purchease);
- $good=Db::name("good_zixun")->where(["spuCode"=>$purchease['spuCode']])->findOrEmpty();
- if(empty($good))throw new \Exception('商品数据未找到');
- $good['supplierNo']=$purchease['supplierNo'];
- $good['supplierName']=$purchease['supplier_name'];
- $good['createrid']=$purchease['good_createrid'];
- $good['creater']=$purchease['good_creater'];
- $good['updatetime']=date('Y-m-d H:i:s');
- $goodup = Db::name("good_zixun")->save($good);
- if($goodup ==false) throw new \Exception('商品数据更新失败');
- }
-
- /**
- * @param $row
- * @throws \Exception
- */
- public function qrdUpdate($row){
- $sale =Db::name("sale")->where("orderCode",$row->code)->findOrEmpty();
- if(empty($sale)) throw new \Exception("销售单数据未找到");
- $after= json_decode($row->after,true);
- foreach ($after as $key=>$item){
- $sale[$key]=$item;
- }
- $sale['updatetime']=date('Y-m-d H:i:s');
- $saleup=Db::name('sale')->save($sale);
- if($saleup==false) throw new \Exception('销售单数据更新失败');
- $outchild =Db::name('order_out_child')->where(['orderCode'=>$sale['orderCode'],'is_del'=>0])->findOrEmpty();
-
- if(empty($outchild)==false){
- $outChidup=Db::name('order_out_child')->where(['orderCode'=>$sale['orderCode'],'is_del'=>0])->update
- ([
- 'customer_code'=>$sale['customer_code'],
- 'customer_name'=>$sale['customerName'],
- 'updatetime'=>date('Y-m-d H:i:s')]);
- if($outChidup==false) throw new \Exception('发货工单数据更新失败');
- }
- }
- /**
- * @param $cgd
- * @throws \think\Exception
- * @throws \think\db\exception\DbException
- */
- public function upQrd($cgd){
- $qrd = Db::name("sale")->alias("a")
- ->leftJoin("order_num b","a.orderCode=b.orderCode")
- ->where("b.cgdNo",$cgd['cgdNo'])
- ->field("a.*")->findOrEmpty();
- if(empty($qrd)) throw new \Exception('未找到对应得销售单');
- $data=[
- "supNo"=>$cgd['supplierNo'],
- "supName"=>$cgd['supplier_name'],
- "good_createrid"=>$cgd['good_createrid'],
- "good_creater"=>$cgd['good_creater'],
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $up=Db::name("sale")->where($qrd)->update($data);
- if($up==false) throw new \Exception('销售单数据更新失败');
- $out =Db::name("order_out")->where(["orderCode"=>$qrd['orderCode'],"is_del"=>0])->findOrEmpty();
- if(!empty($out)){
- $outUp= Db::name('order_out')->where(['orderCode'=>$qrd['orderCode'],'is_del'=>0])->update
- (["wsm_code"=>$cgd['wsm_code'],"updatetime"=>date('Y-m-d H:i:s')]);
- if($outUp==false) throw new \Exception('发货单数据更新失败');
- }
-
- $outchild =Db::name('order_out_child')->where(['orderCode'=>$qrd['orderCode'],'is_del'=>0])->findOrEmpty();
-
- if(!empty($outchild)){
- $outChidup=Db::name('order_out_child')->where(['orderCode'=>$qrd['orderCode'],'is_del'=>0])->update
- (['wsm_code'=>$cgd['wsm_code'],
- "supplierNo"=>$cgd['supplierNo'],
- "supplierName"=>$cgd['supplier_name'],
- 'updatetime'=>date('Y-m-d H:i:s')]);
- if($outChidup==false) throw new \Exception('发货工单数据更新失败');
- }
- }
- /**
- * @param $row
- */
- public function goodUpdate($row){
- $good = Db::name("good_zixun")->where("spuCode",$row->code)->findOrEmpty();
- if(empty($good))throw new \Exception('未找到对应得商品数据');
- $after= json_decode($row->after,true);
- foreach ($after as $key=>$item){
- $good[$key]=$item;
- }
- $good['updatetime']=date("Y-m-d H:i:s");
- $goodup= Db::name('good_zixun')->save($good);
- if($goodup==false) throw new \Exception('商品数据更新失败');
- $saleup =Db::name("sale")->where("good_code",$row->code)->update(["updatetime"=>date('Y-m-d H:i:s')]);
- if($saleup==false) throw new \Exception('商品数据销售单更新失败');
- $cgdup =Db::name("purchease_order")->where("spuCode",$row->code)->update(["updatetime"=>date('Y-m-d
- H:i:s')]);
- if($cgdup==false) throw new \Exception('商品数据采购单更新失败');
- }
- }
|