Activity.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\common\Ocr;
  4. use app\admin\model\ActionLog;
  5. use app\admin\model\DataGroup as DataGroupModel;
  6. use app\admin\model\ProcessOrder;
  7. use app\BaseController;
  8. use think\App;
  9. use think\facade\Db;
  10. class Activity extends Base
  11. {
  12. public $noble=[];
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->noble=\think\facade\Config::get("noble");
  17. }
  18. public function list(){
  19. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  20. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  21. $where = [["ga.is_del","=",0]];
  22. $activity_name = isset($this->post['activity_name']) && $this->post['activity_name'] !=="" ? trim($this->post['activity_name']):"";
  23. if($activity_name!=""){
  24. $where[]=['ga.activity_name',"like","%$activity_name%"];
  25. }
  26. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  27. if($status!==""){
  28. $where[]=['ga.status',"=",$status];
  29. }
  30. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !=="" ? trim($this->post['activity_code']):"";
  31. if($activity_code!==""){
  32. $where[]=['ga.activity_code',"like","%$activity_code%"];
  33. }
  34. $starttime = isset($this->post['starttime']) && $this->post['starttime']!=="" ? $this->post['starttime']:"";
  35. if($starttime!==""){
  36. $where[]=['ga.start',">=",date('Y-m-d H:i:s',strtotime($starttime))];
  37. }
  38. $endtime = isset($this->post['endtime']) && $this->post['endtime']!=="" ? $this->post['endtime']:"";
  39. if($endtime!==""){
  40. $where[]=['ga.end',"<",date('Y-m-d H:i:s',strtotime($endtime)+24*3600)];
  41. }
  42. $platform_code = isset($this->post['platform_code']) && $this->post['platform_code'] !=="" ? trim($this->post['platform_code']):"";
  43. if($platform_code!==""){
  44. $where[]=['ga.platform_code',"=",$platform_code];
  45. }
  46. $company_id = isset($this->post['company_id']) && $this->post['company_id'] !=="" ? trim($this->post['company_id']):"";
  47. if($company_id!==""){
  48. $where[]=['ga.company_id',"=",$company_id];
  49. }
  50. $creater = isset($this->post['creater']) && $this->post['creater'] !=="" ? trim($this->post['creater']) :"";
  51. if($creater!=""){
  52. $where[]=['ga.creater',"like","%$creater%"];
  53. }
  54. $createrid = isset($this->post['createrid']) && $this->post['createrid'] !=="" ? trim($this->post['createrid']) :"";
  55. if($createrid!==""){
  56. $where[]=['ga.createrid',"=",$createrid];
  57. }
  58. // $role=$this->checkRole();
  59. // if(!empty($role['write'])){
  60. // $where[]=["ga.createrid","in",$role['write']];
  61. // }
  62. $role = $this->checkDataShare();
  63. if (!empty($role[DataGroupModel::$type_全部])) $where[] = ['ga.createrid', 'in', $role[DataGroupModel::$type_全部]];
  64. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  65. if ($company_name !== "") $where[] = ["ga.createrid", 'in', get_company_item_user_by_name($company_name)];
  66. $count = Db::name('good_activity')
  67. ->alias('ga')
  68. ->where($where)
  69. ->count();
  70. $total = ceil($count / $size);
  71. $page = $page >= $total ? $total : $page;
  72. $list = Db::name('good_activity')
  73. ->alias('ga')
  74. ->field('ga.*,p.platform_name,u.itemid')
  75. ->leftJoin("depart_user u", "u.uid=ga.createrid AND u.is_del=0")
  76. ->leftJoin("platform p", "p.id=ga.platform_code")
  77. ->append(['company_name','is_allow_update'])
  78. ->withAttr('company_name',function($val,$data){
  79. return implode('/', array_column(GetPart($data['itemid']), 'name'));
  80. })->withAttr('is_allow_update',function($val,$data)use($role){
  81. return (in_array($this->roleid, [1, 33]) || in_array($data['createrid'], $role[DataGroupModel::$type_可编辑])) ? 1 : 0;//是否具有编辑权限
  82. })->where($where)
  83. ->page($page,$size)
  84. ->order("ga.addtime desc")
  85. ->select()
  86. ->toArray();
  87. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  88. }
  89. public function linst()
  90. {
  91. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  92. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  93. $where = [["a.is_del", "=", 0]];
  94. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] !== "" ? trim($this->post['skuCode']) : "";
  95. if ($skuCode !== "") {
  96. $where[] = ['a.skuCode', "like", "%$skuCode%"];
  97. }
  98. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  99. if ($good_name != "") {
  100. $where[] = ['a.good_name', "like", "%$good_name%"];
  101. }
  102. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !=="" ? trim($this->post['activity_code']):"";
  103. if($activity_code!==""){
  104. $where[]=['a.activity_code',"like","%$activity_code%"];
  105. }
  106. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  107. if($status!==""){
  108. $where[]=['a.status',"=",$status];
  109. }
  110. $activity_name = isset($this->post['activity_name']) && $this->post['activity_name'] !=="" ? trim($this->post['activity_name']):"";
  111. if($activity_name!=""){
  112. $where[]=['b.activity_name',"like","%$activity_name%"];
  113. }
  114. $platform_code = isset($this->post['platform_code']) && $this->post['platform_code'] !=="" ? trim($this->post['platform_code']):"";
  115. if($platform_code!=""){
  116. $where[]=['b.platform_code',"=",$platform_code];
  117. }
  118. $count = Db::name('activity_info')->alias("a")
  119. ->join("good_activity b","b.activity_code=a.activity_code","left")
  120. ->join("platform c","c.id=b.platform_code","left")
  121. ->where($where)->count();
  122. $total = ceil($count / $size);
  123. $page = $page >= $total ? $total : $page;
  124. $list = Db::name('activity_info')->alias("a")
  125. ->join("good_activity b","b.activity_code=a.activity_code","left")
  126. ->join("platform c","c.id=b.platform_code","left")
  127. ->where($where)->page($page,$size)->order("a.addtime desc")->field("a.*,b.activity_name,c.platform_name,c.platform_code
  128. ")->select();
  129. return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
  130. }
  131. public function zilist(){
  132. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  133. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  134. $where = [["a.is_del", "=", 0]];
  135. $spuCode = isset($this->post['spuCode']) && $this->post['spuCode'] !== "" ? trim($this->post['spuCode']) : "";
  136. if ($spuCode != "") $where[] = ['a.spuCode', "like", "%$spuCode%"];
  137. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  138. if ($good_name != "") $where[] = ['a.good_name', "like", "%$good_name%"];
  139. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  140. if($status!="") $where[]=['a.status',"=",$status];
  141. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  142. if ($supplierNo != "") $where[] = ['a.supplierNo', "like", "%$supplierNo%"];
  143. $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
  144. if ($name != "") $where[] = ['b.name', "like", "%$name%"];
  145. $start = isset($this->post['start']) && $this->post['start'] !== "" ? trim($this->post['start']) : "";
  146. if ($start != "") $where[] = ['a.addtime', ">=", $start];
  147. $end = isset($this->post['end']) && $this->post['end'] !== "" ? trim($this->post['end']) : "";
  148. if ($end != "") $where[] = ['a.addtime', "<=", $end.' 23:59:59'];
  149. $count = Db::name('good_zixun')
  150. ->alias("a")
  151. ->join("supplier b","b.code=a.supplierNo","left")
  152. ->where($where)
  153. ->count();
  154. $total = ceil($count / $size);
  155. $page = $page >= $total ? $total : $page;
  156. $list = Db::name('good_zixun')
  157. ->alias("a")
  158. ->join("supplier b","b.code=a.supplierNo","left")
  159. ->where($where)
  160. ->page($page,$size)
  161. ->order("a.addtime desc")
  162. ->field("a.*,b.name")
  163. ->select()
  164. ->toArray();
  165. //halt($list);
  166. $data=[];
  167. //分类/规格/品牌 等字段取出对应文字
  168. foreach ($list as $value){
  169. $value['cat_info']= made($value['cat_id'],[]);
  170. $platform = Db::name("platform")->where(["id"=>$value['platform_id']])->find();
  171. $value['platform_name'] = isset($platform['platform_name']) ? $platform['platform_name']:"";
  172. $value['platform_code_en'] = isset($platform['platform_code_en']) ? $platform['platform_code_en']:"";
  173. $supplier = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
  174. $value['supplier_name'] = isset($supplier['name']) ? $supplier['name']:"";
  175. $brand=Db::name("brand")->where(["id"=>$value['brand_id']])->find();
  176. $value["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  177. $unit = Db::name("unit")->where(["id"=>$value['good_unit']])->find();
  178. $value['unit'] =isset($unit['unit'])?$unit['unit']:"";
  179. $company = Db::name("business")->where(["companyNo"=>$value['companyNo']])->find();
  180. $value['company'] = isset($company['company'])?$company['company']:"";
  181. // $company = Db::name("business")->where(["companyNo"=>$value['companyNo']])->find();
  182. // $value['company'] = isset($company['company'])?$company['company']:"";
  183. $value['stock_total'] = Db::name("good_stock")->where(['spuCode'=>$value['spuCode'],"is_del"=>0])->sum("usable_stock");
  184. // $value['status']=$value['exam_status'];
  185. // $value['exclusive']=makeExcluse($value['is_exclusive']);
  186. $value['good_info_img']=$value['good_info_img'];
  187. $value['good_img']=$value['good_img'];
  188. $spec = Db::name("good_spec")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->select()->toArray();
  189. $speclist=[];
  190. if(!empty($spec)){
  191. foreach ($spec as $val){
  192. $temp=[];
  193. $temp['spec_id']=$val['spec_id'];
  194. $temp['spec_value_id']=$val['spec_value_id'];
  195. $sp = Db::name("specs")->where(["id"=>$val['spec_id']])->find();
  196. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  197. $spv = Db::name("spec_value")->where(["id"=>$val['spec_value_id']])->find();
  198. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  199. $speclist[]=$temp;
  200. }
  201. }
  202. $value['specinfo']=$speclist;
  203. $value['noble_name']=isset($value['noble_metal'])&&$value['noble_metal']!=0?$this->noble[$value['noble_metal']] :"";
  204. $data[]=$value;
  205. }
  206. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  207. }
  208. //创建活动
  209. public function create(){
  210. $activity_name = isset($this->post['activity_name'])&&$this->post['activity_name']!="" ? trim($this->post['activity_name']):"";
  211. if($activity_name==""){
  212. return error_show(1004,"参数activity_name不能为空");
  213. }
  214. $platform_code = isset($this->post['platform_code'])&&$this->post['platform_code']!="" ? trim($this->post['platform_code']):"";
  215. if($platform_code==""){
  216. return error_show(1004,"参数platform_code不能为空");
  217. }
  218. $company_id = isset($this->post['company_id'])&&$this->post['company_id']!="" ? trim($this->post['company_id']):"";
  219. if($company_id==""){
  220. return error_show(1004,"参数company_id不能为空");
  221. }
  222. $activity_start = isset($this->post['activity_start'])&&$this->post['activity_start']!="" ? $this->post['activity_start']:"";
  223. if($activity_start==""){
  224. return error_show(1004,"参数activity_start不能为空");
  225. }
  226. $activity_end = isset($this->post['activity_end'])&&$this->post['activity_end']!="" ? $this->post['activity_end']:"";
  227. if($activity_end==""){
  228. return error_show(1004,"参数activity_end不能为空");
  229. }
  230. $activity_desc = isset($this->post['activity_desc'])&&$this->post['activity_desc']!="" ?
  231. trim($this->post['activity_desc']):"";
  232. if($activity_desc==""){
  233. return error_show(1004,"参数activity_desc不能为空");
  234. }
  235. $is_stock = isset($this->post['is_stock'])&&$this->post['is_stock']!=="" ?
  236. intval($this->post['is_stock']):"";
  237. if($is_stock===""){
  238. return error_show(1004,"参数is_stock不能为空");
  239. }
  240. $good_list= isset($this->post['good_list'])&&$this->post['good_list']!=="" ?$this->post['good_list']:"";
  241. if($good_list==""){
  242. return error_show(1004,"参数good_list不能为空");
  243. }
  244. $token = isset($this->post['token'])&& $this->post['token']!='' ? trim($this->post['token']):"";
  245. if($token==''){
  246. return error_show(105,"参数token不能为空");
  247. }
  248. $user =GetUserInfo($token);
  249. if(empty($user)||$user['code']!=0){
  250. return error_show(102,"用户数据不存在");
  251. }
  252. $createrid= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  253. $creater= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  254. $activity_code = makeNo("AT");
  255. Db::startTrans();
  256. try{
  257. $data=[
  258. "activity_name"=>$activity_name,
  259. "activity_code"=>$activity_code,
  260. "is_stock"=>$is_stock,
  261. "start"=>$activity_start,
  262. "end"=>$activity_end,
  263. "platform_code"=>$platform_code,
  264. "activity_desc"=>$activity_desc,
  265. "company_id"=>$company_id,
  266. "creater"=>$creater,
  267. "createrid"=>$createrid,
  268. "status"=>1,
  269. "is_del"=>0,
  270. "addtime"=>date("Y-m-d H:i:s"),
  271. "updatetime"=>date("Y-m-d H:i:s")
  272. ];
  273. $inset = Db::name("good_activity")->insertGetId($data);
  274. if($inset>0){
  275. if($good_list!=""&&!empty($good_list)){
  276. $temp=[];
  277. $skuCode =array_column($good_list,"skuCode");
  278. $ist =Db::name("good_activity")->alias("a")
  279. ->leftJoin("activity_info b","a.activity_code = b.activity_code and b.is_del=0")
  280. ->where(["b.skuCode"=>$skuCode,"a.is_del"=>0])->where([["a.status","<>",8]])->where(function
  281. ($query)use($activity_end,$activity_start){
  282. return $query->whereOr([[["start","<=",$activity_start],["end",">=",$activity_end]],["start|end","between",[$activity_start, $activity_end]]]);
  283. })->select()->toArray();
  284. if(!empty($ist)){
  285. Db::rollback();
  286. $limit=[];
  287. foreach ($ist as $value){
  288. $limit[]="活动 ".$value['activity_name']. ' 已存在商品 '. $value['good_name'];
  289. }
  290. return app_show(1007,'活动商品已存在', $limit);
  291. }
  292. foreach ($good_list as $value){
  293. $lemp=[];
  294. $good = Db::name('good')->alias("a")->leftJoin("good_platform b","a.spuCode=b.spuCode")
  295. ->where(["skuCode"=>$value['skuCode'],"a.is_del"=>0])->find();
  296. $lemp['skuCode']=$value['skuCode'];
  297. $lemp['good_name']=isset($good['good_name']) ?$good['good_name']:"" ;
  298. $lemp['activity_code']=$activity_code;
  299. $lemp['activity_stock']=$value['activity_stock'];
  300. $lemp['moq_num']=0;
  301. $lemp['cost_price']=0;
  302. $lemp['sale_price']=0;
  303. $lemp['activity_price']=0;
  304. $lemp['status']=0;
  305. $lemp['remark']="";
  306. $lemp['is_del']=0;
  307. $lemp['addtime']=date("Y-m-d H:i:s");
  308. $lemp['updatetime']=date("Y-m-d H:i:s");
  309. $temp[]=$lemp;
  310. //当是库存品的时候校验可用库存数,不满足的时候不能添加
  311. if ($is_stock == 1) {
  312. $usable_stock = Db::name('good_stock')
  313. ->where(['spuCode' => $good['spuCode'], 'is_del' => 0])
  314. ->sum('usable_stock');
  315. if ($value['activity_stock'] > $usable_stock) {
  316. Db::rollback();
  317. // throw new \Exception($lemp['good_name'] . '的库存只有' . $usable_stock);
  318. return app_show(1005,'库存不足',[$lemp['good_name']. '的库存只有' . $usable_stock]);
  319. }
  320. }
  321. }
  322. $iall=Db::name("activity_info")->insertAll($temp);
  323. if($iall==0){
  324. Db::rollback();
  325. return error_show(1005,"新建失败");
  326. }
  327. }
  328. $stn = ["order_code" => $activity_code, "status" => 0, "action_remark" => '', "action_type" => "create"];
  329. ActionLog::logAdd($this->post['token'], $stn, "HD", $data['status'], $data);
  330. $process = ["order_code" => $activity_code, "order_id" => $inset, "order_status" => $data['status'], "order_type" => 'HD', "before_status" => 0, 'holder_id' => $data['createrid']];
  331. ProcessOrder::AddProcess($this->post['token'], $process);
  332. Db::commit();
  333. return app_show(0,"新建成功");
  334. }
  335. }catch (\Exception $e){
  336. Db::rollback();
  337. return error_show(1005,$e->getMessage());
  338. }
  339. }
  340. //采购定价
  341. public function cost(){
  342. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  343. if($good_list==""){
  344. return error_show(1004,"参数good_list不能为空");
  345. }
  346. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  347. if($activity_code==""){
  348. return error_show(1004,"参数activity_code不能为空");
  349. }
  350. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  351. if($activity==false){
  352. return error_show(1004,"未找到活动数据");
  353. }
  354. Db::startTrans();
  355. try{
  356. foreach ($good_list as $value){
  357. $temp=[];
  358. $ladd = Db::name("good_ladder")->where(["skuCode"=>$value['skuCode'],"is_del"=>0])->where
  359. ([["min_num","<=",$value['moq_num']]])->order("min_num desc")->find();
  360. $temp["id"]=$value['id'];
  361. $temp["cost_price"]=$value['cost_price'];
  362. $temp["moq_num"]=$value['moq_num'];
  363. $temp["sale_price"]=isset($ladd['sale_price'])?$ladd['sale_price']:0;
  364. $temp["updatetime"]=date("Y-m-d H:i:s");
  365. $up=Db::name("activity_info")->save($temp);
  366. if($up==false){
  367. Db::rollback();
  368. return error_show(1004,"商品数据更新失败");
  369. }
  370. }
  371. $old_activity_status = $activity['status'];
  372. $activity['status']=2;
  373. $activity['updatetime']=date("Y-m-d H:i:s");
  374. $act = Db::name("good_activity")->save($activity);
  375. if($act){
  376. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  377. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  378. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  379. ProcessOrder::AddProcess($this->post['token'], $process);
  380. Db::commit();
  381. return error_show(0,"商品数据更新成功");
  382. }else{
  383. Db::rollback();
  384. return error_show(1004,"商品数据更新失败");
  385. }
  386. }catch (\Exception $e){
  387. Db::rollback();
  388. return error_show(1004,$e->getMessage());
  389. }
  390. }
  391. //财务定价
  392. public function activity(){
  393. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  394. if($good_list==""){
  395. return error_show(1004,"参数good_list不能为空");
  396. }
  397. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  398. if($activity_code==""){
  399. return error_show(1004,"参数activity_code不能为空");
  400. }
  401. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  402. if($activity==false){
  403. return error_show(1004,"未找到活动数据");
  404. }
  405. Db::startTrans();
  406. try{
  407. foreach ($good_list as $value){
  408. $temp=[];
  409. $temp["id"]=$value['id'];
  410. $temp["activity_price"]=$value['activity_price'];
  411. $temp["updatetime"]=date("Y-m-d H:i:s");
  412. $up=Db::name("activity_info")->save($temp);
  413. if($up==false){
  414. Db::rollback();
  415. return error_show(1004,"商品数据更新失败");
  416. }
  417. }
  418. $old_activity_status = $activity['status'];
  419. $activity['status']=3;
  420. $activity['updatetime']=date("Y-m-d H:i:s");
  421. $act = Db::name("good_activity")->save($activity);
  422. if($act){
  423. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  424. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  425. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  426. ProcessOrder::AddProcess($this->post['token'], $process);
  427. Db::commit();
  428. return error_show(0,"商品数据更新成功");
  429. }else{
  430. Db::rollback();
  431. return error_show(1004,"商品数据更新失败");
  432. }
  433. }catch (\Exception $e){
  434. Db::rollback();
  435. return error_show(1004,$e->getMessage());
  436. }
  437. }
  438. //财务审核定价
  439. public function exam(){
  440. $good_list= isset($this->post['good_list'])&&!empty($this->post['good_list']) ?$this->post['good_list']:"";
  441. if($good_list==""){
  442. return error_show(1004,"参数good_list不能为空");
  443. }
  444. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  445. if($activity_code==""){
  446. return error_show(1004,"参数activity_code不能为空");
  447. }
  448. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  449. if($activity==false){
  450. return error_show(1004,"未找到活动数据");
  451. }
  452. Db::startTrans();
  453. try{
  454. foreach ($good_list as $value){
  455. $temp=[];
  456. $temp["id"]=$value['id'];
  457. $temp["status"]=$value['status'];
  458. $temp["remark"]=$value['remark'];
  459. $temp["updatetime"]=date("Y-m-d H:i:s");
  460. $up=Db::name("activity_info")->save($temp);
  461. if($up==false){
  462. Db::rollback();
  463. return error_show(1004,"商品数据更新失败");
  464. }
  465. }
  466. $old_activity_status = $activity['status'];
  467. $activity['status']=4;
  468. $activity['updatetime']=date("Y-m-d H:i:s");
  469. $act = Db::name("good_activity")->save($activity);
  470. if($act){
  471. $stn = ["order_code" => $activity_code, "status" => $old_activity_status, "action_remark" => '', "action_type" => "status"];
  472. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  473. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_activity_status, 'holder_id' => $activity['createrid']];
  474. ProcessOrder::AddProcess($this->post['token'], $process);
  475. Db::commit();
  476. return error_show(0,"商品数据审核成功");
  477. }else{
  478. Db::rollback();
  479. return error_show(1004,"商品数据审核失败");
  480. }
  481. }catch (\Exception $e){
  482. Db::rollback();
  483. return error_show(1004,$e->getMessage());
  484. }
  485. }
  486. //活动详情
  487. public function info()
  488. {
  489. $activity_code = isset($this->post['activity_code']) && $this->post['activity_code'] !== "" ? trim($this->post['activity_code']) : "";
  490. if ($activity_code == "") {
  491. return error_show(1004, "参数activity_code不能为空");
  492. }
  493. $activity = Db::name("good_activity")
  494. ->alias('ga')
  495. ->field('ga.*,p.platform_name')
  496. ->leftJoin('platform p', 'p.id=ga.platform_code AND p.is_del=0')
  497. ->where(["ga.activity_code" => $activity_code, "ga.is_del" => 0])
  498. ->find();
  499. if ($activity == false) {
  500. return error_show(1004, "未找到活动数据");
  501. }
  502. $activity_info = Db::name("activity_info")
  503. ->where(["activity_code" => $activity_code, "is_del" => 0])
  504. ->select()
  505. ->toArray();
  506. if (!empty($activity_info)) {
  507. //状态不是(0待产品部审核,1待采购定价),才会有起订量,才可以考虑计算售价
  508. if ($activity['status'] > 1) {
  509. //判断是否是启用实时金价的贵金属
  510. $rs = Db::name('good_platform')
  511. ->alias('a')
  512. ->join('good g', 'g.spuCode=a.spuCode')
  513. ->join('good_nake c', 'c.spuCode=a.spuCode')
  514. ->whereIn('a.skuCode', array_column($activity_info, 'skuCode'))
  515. ->column('g.cat_id,g.is_gold_price,g.demo_fee,g.open_fee,g.weight,c.cost_fee,c.package_fee,c.mark_fee,c.cert_fee,g.noble_metal,c.nake_fee,c.delivery_fee', 'a.skuCode');
  516. foreach ($activity_info as &$value) {
  517. //如果启用了实时金价,且是贵金属
  518. if (isset($rs[$value['skuCode']])) {
  519. $top_cat = made($rs[$value['skuCode']]['cat_id']);
  520. if (isset($top_cat[0]['id']) && $top_cat[0]['id'] == 6 && $rs[$value['skuCode']]['is_gold_price'] == 1) {
  521. $gold_price = Db::name("gold_price1")->where(["type" => $rs[$value['skuCode']]['noble_metal'], "is_del" => 0, "status" => 1])->order("addtime desc")->value('price');
  522. $value['sale_price'] = round($rs[$value['skuCode']]['demo_fee'] / $value['moq_num'] + $rs[$value['skuCode']]['open_fee'] / $value['moq_num'] + $rs[$value['skuCode']]['weight'] * $gold_price + $rs[$value['skuCode']]['cost_fee'] * $rs[$value['skuCode']]['weight'] + $rs[$value['skuCode']]['package_fee'] + $rs[$value['skuCode']]['mark_fee'] + $rs[$value['skuCode']]['cert_fee'] + $rs[$value['skuCode']]['nake_fee'] + $rs[$value['skuCode']]['delivery_fee'], 2);
  523. }
  524. }
  525. }
  526. }
  527. $activity['info'] = $activity_info;
  528. } else $activity['info'] = [];
  529. // $activity['info'] = empty($activity_info) ? [] : $activity_info;
  530. return app_show(0, "获取成功", $activity);
  531. }
  532. //修改活动状态
  533. public function actstatus(){
  534. $activity_code= isset($this->post['activity_code'])&&$this->post['activity_code']!=="" ?trim($this->post['activity_code']):"";
  535. if($activity_code==""){
  536. return error_show(1004,"参数activity_code不能为空");
  537. }
  538. $activity = Db::name("good_activity")->where(["activity_code"=>$activity_code,"is_del"=>0])->find();
  539. if($activity==false){
  540. return error_show(1004,"未找到活动数据");
  541. }
  542. $status = isset($this->post['status'])&&$this->post['status']!=="" ?intval($this->post['status']):"";
  543. if($status==""){
  544. return error_show(1004,"参数status不能为空");
  545. }
  546. $remark =isset($tihs->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  547. if($remark=="" &&$status==8){
  548. return error_show(1004,"参数remark不能为空");
  549. }
  550. $old_status = $activity['status'];
  551. $activity['status']=$status;
  552. $activity['remark']=$remark;
  553. $activity['updatetime']=date("Y-m-d H:i:s");
  554. $up = Db::name("good_activity")->save($activity);
  555. if($up){
  556. $stn = ["order_code" => $activity_code, "status" => $old_status, "action_remark" => '', "action_type" => "status"];
  557. ActionLog::logAdd($this->post['token'], $stn, "HD", $activity['status'], $activity);
  558. $process = ["order_code" => $activity_code, "order_id" => $activity['id'], "order_status" => $activity['status'], "order_type" => 'HD', "before_status" => $old_status, 'holder_id' => $activity['createrid']];
  559. ProcessOrder::AddProcess($this->post['token'], $process);
  560. return app_show(0,"修改成功");
  561. }else{
  562. return error_show(1004,"修改失败");
  563. }
  564. }
  565. public function query(){
  566. $skuCode= isset($this->post['skuCode'])&&$this->post['skuCode']!=""?trim($this->post['skuCode']):"";
  567. if($skuCode==""){
  568. return error_show(1004,"参数skuCode不能为空");
  569. }
  570. $where=['a.is_del'=>0,"b.is_del"=>0,"a.status"=>6,"b.status"=>1,"b.skuCode"=>$skuCode];
  571. $condition=['a.skuCode' =>$skuCode,"a.is_del"=>0,'a.exam_status'=>6];
  572. $company = isset($this->post['companyNo'])&&$this->post['companyNo']!=""?trim($this->post['companyNo']):"";
  573. if($company!=""){
  574. $where['a.company_id'] = $company;
  575. $condition['companyNo'] = $company;
  576. }
  577. $plat =isset($this->post['platform_id'])&&$this->post['platform_id']!=""?intval($this->post['platform_id'])
  578. :"";
  579. if($plat!==""){
  580. $where['a.platform_code'] = $plat;
  581. $condition['platform_code'] = $plat;
  582. }
  583. $good = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  584. ->where($condition)->select()->toArray();
  585. $goodlist=[];
  586. if(!empty($good)){
  587. foreach ($good as $value){
  588. $temp=[];
  589. $stock =Db::name("good_stock")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->sum('usable_stock');
  590. //如果是非库存商品,库存数改为最大(即将库存设为无限)
  591. $temp['stock_num'] = $value['is_stock'] == 1 ? intval($stock) : 9999;
  592. $temp['skuCode']=$value['skuCode'];
  593. $temp['spuCode']=$value['spuCode'];
  594. $temp['is_stock']=$value['is_stock'];
  595. $temp['good_name']=$value['good_name'];
  596. $temp['good_img']=$value['good_img'];
  597. $temp['good_info_img']=$value['good_info_img'];
  598. $temp['noble_metal']=$value['noble_metal'];
  599. $temp['noble_name']=$value['noble_metal']!=0?$this->noble[$value['noble_metal']]:"";
  600. $goodlist[]=$temp;
  601. }
  602. }
  603. $list = Db::name('good_activity')->alias("a")
  604. ->join("activity_info b","b.activity_code=a.activity_code","left")
  605. ->where($where)->field('b.id,a.activity_name,a.activity_code,a.is_stock,a.start,a.end,b.skuCode,b.good_name,b.activity_stock,
  606. b.cost_price,b.moq_num,b.sale_price,b.activity_price')->select()->toArray();
  607. return app_show(0,"获取成功",["act"=>$list,"good"=>$goodlist]);
  608. }
  609. //新增库存
  610. public function editStock(){
  611. $actCode=isset($this->post['actCode'])&&$this->post['actCode']!='' ? trim($this->post['actCode']):"";
  612. if($actCode==''){
  613. return error_show(1004,"参数 actCode 不能为空");
  614. }
  615. $skuCode=isset($this->post['skuCode'])&&$this->post['skuCode']!='' ? trim($this->post['skuCode']):"";
  616. if($skuCode==''){
  617. return error_show(1004,"参数 skuCode 不能为空");
  618. }
  619. $act_stock=isset($this->post['act_stock'])&&$this->post['act_stock']!='' ? intval($this->post['act_stock']):"";
  620. if($act_stock==''){
  621. return error_show(1004,"参数 act_stock 不能为空");
  622. }
  623. $act =Db::name("good_activity")->where(["activity_code"=>$actCode,"is_del"=>0])->findOrEmpty();
  624. if(empty($act)){
  625. return error_show(1005,"活动信息不存在");
  626. }
  627. if(!in_array($act['status'],[4,5,6]))return error_show(1005,"活动暂时不能修改");
  628. $goodinfo=Db::name("activity_info")->where(["activity_code"=>$actCode,"skuCode"=>$skuCode,"is_del"=>0])
  629. ->findOrEmpty();
  630. if(empty($goodinfo)){
  631. return error_show(1005,"活动商品信息不存在");
  632. }
  633. $good = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  634. ->where(['a.skuCode' =>$skuCode,"a.is_del"=>0,'a.exam_status'=>6])->findOrEmpty();
  635. if(empty($good)){
  636. return error_show(1005,"商品信息不存在或已下架");
  637. }
  638. if($good['is_stock']==1){
  639. $stock =Db::name("good_stock")->where(["spuCode"=>$good['spuCode'],"is_del"=>0])->sum('usable_stock');
  640. if($act_stock+$goodinfo['activity_stock']> $stock){
  641. return error_show(1005,"商品库存不足");
  642. }
  643. }
  644. $update =["activity_stock"=>$act_stock+$goodinfo['activity_stock'],"updatetime"=>date("Y-m-d H:i:s")];
  645. $up=Db::name("activity_info")->where($goodinfo)->update($update);
  646. return $up ? app_show(0,"活动库存增加成功"):error_show(1006,"活动库存增加失败");
  647. }
  648. }