Activity.php 41 KB

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