Activity.php 36 KB

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