|
@@ -1,14 +1,16 @@
|
|
|
<?php
|
|
|
namespace app\admin\model;
|
|
|
|
|
|
+use think\Model;use think\model\concern\SoftDelete;
|
|
|
class DataChange extends Base
|
|
|
{
|
|
|
//设置字段信息
|
|
|
+ use SoftDelete;
|
|
|
protected $schema = [
|
|
|
'id' =>'bigint',//
|
|
|
'code' =>'varchar',//数据编号
|
|
|
'oaCode' =>'varchar',//
|
|
|
- 'source' =>'tinyint',//1数据修正 2 二次议价咨询单
|
|
|
+ 'source' =>'tinyint',//1数据修正 2 二次议价咨询单 3 数据同步
|
|
|
'type' =>'tinyint',//数据类型 1 订单 2 采购单
|
|
|
'before' =>'varchar',//更新字段数据
|
|
|
'after' =>'varchar',//更新后数据字段
|
|
@@ -21,6 +23,185 @@ class DataChange extends Base
|
|
|
'updatetime' =>'datetime',//
|
|
|
'delete_time' =>'datetime',//
|
|
|
'remark' =>'varchar',//
|
|
|
-
|
|
|
];
|
|
|
+ protected $createTime = 'createtime';
|
|
|
+ protected $updateTime = 'updatetime';
|
|
|
+ protected $deleteTime = 'delete_time';
|
|
|
+
|
|
|
+ public function getBeforeAttr($value){
|
|
|
+ return json_decode($value,true);
|
|
|
+ }
|
|
|
+ public function getAfterAttr($value){
|
|
|
+ return json_decode($value,true);
|
|
|
+ }
|
|
|
+ public function setBeforeAttr($value){
|
|
|
+ return json_encode($value,JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+ public function setAfterAttr($value){
|
|
|
+ return json_encode($value,JSON_UNESCAPED_UNICODE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function onAfterWrite(Model $model) : void{
|
|
|
+ if($model->source==3 && $model->status==2){
|
|
|
+ self::upOrder($model);
|
|
|
+ }
|
|
|
+ $change = $model->getChangedData();
|
|
|
+ if(array_key_exists("status",$change) && $change['status']==2 && in_array($model->source,[1,2])){
|
|
|
+ self::updateOrder($model);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //数据同步
|
|
|
+ public static function upOrder($data){
|
|
|
+ if($data['type']==1){
|
|
|
+ PurcheaseOrder::where(['cgdNo'=>$data['code']])->update($data['after']);
|
|
|
+ }
|
|
|
+ if($data['type']==2){
|
|
|
+ Sale::where(['orderCode'=>$data['code']])->update($data['after']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //数据修正
|
|
|
+ public static function updateOrder($data) {
|
|
|
+ if($data['type']==1){
|
|
|
+ self::cgdUpdate($data);
|
|
|
+ }
|
|
|
+ if($data['type']==2){
|
|
|
+ self::qrdUpdate($data);
|
|
|
+ }
|
|
|
+ if($data['type']==3){
|
|
|
+ self::goodUpdate($data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param $row
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public static function cgdUpdate($row){
|
|
|
+ $purchease =PurcheaseOrder::where(['cgdNo'=>$row['code']])->findOrEmpty();
|
|
|
+ if($purchease->isEmpty()) throw new \Exception('未找到采购单');
|
|
|
+ foreach ($row['after'] as $key=>$item){
|
|
|
+ $purchease->$key=$item;
|
|
|
+ }
|
|
|
+ $wsm=WarehouseInfo::where(['supplierNo' => $purchease['supplierNo'], 'companyNo' =>
|
|
|
+ $purchease['companyNo'], 'wsm_type' => 2, 'is_del' => 0])->findOrEmpty();
|
|
|
+ if($wsm->isEmpty()){
|
|
|
+ $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=WarehouseInfo::create($inwsm);
|
|
|
+ if($sin==false) throw new \Exception('采购单对应得仓库创建失败');
|
|
|
+ }else $wsm_code = $wsm['wsm_code'];
|
|
|
+ if($purchease['wsm_code']!=$wsm_code){
|
|
|
+ $good_stock= GoodStock::where(['spuCode'=>$purchease['spuCode'],
|
|
|
+ 'wsm_code'=>$purchease['wsm_code']])->findOrEmpty();
|
|
|
+ if($good_stock->isEmpty()) throw new \Exception('商品库存数据未找到');
|
|
|
+ $good_stock->wsm_code = $wsm_code;
|
|
|
+ $goodup =$good_stock->save();
|
|
|
+ if($goodup==false) throw new \Exception('商品库存数据更新失败');
|
|
|
+ $purchease->wsm_code= $wsm_code;
|
|
|
+ }
|
|
|
+ $up=$purchease->save();
|
|
|
+ if($up==false) throw new \Exception('采购单数据更新失败');
|
|
|
+ self::upQrd($purchease);
|
|
|
+ $good=GoodZixun::where(['spuCode'=>$purchease['spuCode']])->findOrEmpty();
|
|
|
+ if($good->isEmpty())throw new \Exception('商品数据未找到');
|
|
|
+ $good->supplierNo=$purchease['supplierNo'];
|
|
|
+ $good->supplierName=$purchease['supplier_name'];
|
|
|
+ $good->createrid=$purchease['good_createrid'];
|
|
|
+ $good->creater=$purchease['good_creater'];
|
|
|
+ $goodup = $good->save();
|
|
|
+ if($goodup ==false) throw new \Exception('商品数据更新失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $row
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public static function qrdUpdate($row){
|
|
|
+ $sale =Sale::where('orderCode',$row->code)->findOrEmpty();
|
|
|
+ if($sale->isEmpty()) throw new \Exception('销售单数据未找到');
|
|
|
+ foreach ($row->after as $key=>$item){
|
|
|
+ $sale->$key=$item;
|
|
|
+ }
|
|
|
+ $saleup=$sale->save();
|
|
|
+ if($saleup==false) throw new \Exception('销售单数据更新失败');
|
|
|
+ $outchild=OrderOutChild::where(['orderCode'=>$sale['orderCode'],'is_del'=>0])->findOrEmpty();
|
|
|
+ if(!$outchild->isEmpty()){
|
|
|
+ $outchild->customer_code=$sale['customer_code'];
|
|
|
+ $outchild->customer_name=$sale['customerName'];
|
|
|
+ $save=$outchild->save();
|
|
|
+ if($save==false) throw new \Exception('销售单子表数据更新失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @param $cgd
|
|
|
+ * @throws \think\Exception
|
|
|
+ * @throws \think\db\exception\DbException
|
|
|
+ */
|
|
|
+ public static function upQrd($cgd){
|
|
|
+ $orderCode = OrderNum::where('cgdNo',$cgd['cgdNo'])->value("orderCode","");
|
|
|
+ if( $orderCode=="") throw new \Exception('未找到对应得销售单号');
|
|
|
+
|
|
|
+ $qrd = Sale::where('orderCode',$orderCode)->findOrEmpty();
|
|
|
+ if(empty($qrd)) throw new \Exception('未找到对应得销售单');
|
|
|
+ $qrd->supNo=$cgd['supplierNo'];
|
|
|
+ $qrd->supName=$cgd['supplier_name'];
|
|
|
+ $qrd->good_createrid=$cgd['good_createrid'];
|
|
|
+ $qrd->good_creater=$cgd['good_creater'];
|
|
|
+ $up=$qrd->save();
|
|
|
+ if($up==false) throw new \Exception('销售单数据更新失败');
|
|
|
+ $out =OrderOut::where(['orderCode'=>$orderCode,'is_del'=>0])->findOrEmpty();
|
|
|
+ if(!$out->isEmpty()){
|
|
|
+ $outUp=OrderOut::where(['orderCode'=>$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 =OrderOutChild::where(['orderCode'=>$orderCode,'is_del'=>0])->findOrEmpty();
|
|
|
+
|
|
|
+ if(!$outchild->isEmpty()){
|
|
|
+ $outChidup=OrderOutChild::where(['orderCode'=>$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 static function goodUpdate($row){
|
|
|
+ $good = GoodZixun::where('spuCode',$row->code)->findOrEmpty();
|
|
|
+ if(empty($good))throw new \Exception('未找到对应得商品数据');
|
|
|
+ foreach ($row->after as $key=>$item){
|
|
|
+ $good->$key=$item;
|
|
|
+ }
|
|
|
+ $goodup=$good->save();
|
|
|
+ if($goodup==false) throw new \Exception('商品数据更新失败');
|
|
|
+ $saleup =Sale::where('good_code',$row->code)->update(['updatetime'=>date('Y-m-d H:i:s')]);
|
|
|
+ if($saleup==false) throw new \Exception('商品数据销售单更新失败');
|
|
|
+ $cgdup =PurcheaseOrder::where('spuCode',$row->code)->update(['updatetime'=>date('Y-m-d H:i:s')]);
|
|
|
+ if($cgdup==false) throw new \Exception('商品数据采购单更新失败');
|
|
|
+ }
|
|
|
}
|