Purch.php 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\DataGroup as DataGroupModel;
  5. use app\admin\model\ProcessOrder;
  6. use think\App;
  7. use think\Exception;
  8. use think\facade\Db;
  9. use think\facade\Validate;
  10. //采购单相关功能
  11. class Purch extends Base
  12. {
  13. public $noble=[];
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. $this->noble=\think\facade\Config::get("noble");
  18. }
  19. //采购单列表
  20. public function list()
  21. {
  22. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  23. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  24. $where = [['po.is_del', "=", 0]];
  25. $bkcode = isset($this->post['bk_code']) && $this->post['bk_code'] != "" ? trim($this->post['bk_code']) : "";
  26. if ($bkcode !== "") {
  27. $where[] = ['po.bkcode', "like", "%{$bkcode}%"];
  28. }
  29. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  30. if ($status !== "") {
  31. // $where['status'] = $status;
  32. $where[] = ['po.status', "=", $status];
  33. }
  34. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
  35. if ($cgdNo !== "") {
  36. //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
  37. $where[] = ['po.cgdNo', "like", "%{$cgdNo}%"];
  38. }
  39. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] != "" ? trim($this->post['apply_name']) : "";
  40. if ($apply_name !== "") {
  41. // $where['cgder'] =Db::Raw("like '%{$apply_name}%'");
  42. $where[] = ['po.cgder', "like", "%{$apply_name}%"];
  43. }
  44. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] != "" ? trim($this->post['wsm_code']) : "";
  45. if ($wsm_code !== "") {
  46. // $where['wsm_code'] = $wsm_code;
  47. $where[] = ['po.wsm_code', "=", $wsm_code];
  48. }
  49. $wsm_in_code = isset($this->post['wsm_in_code']) && $this->post['wsm_in_code'] != "" ? trim($this->post['wsm_in_code']) : "";
  50. if ($wsm_in_code !== "") {
  51. $incode = Db::name("purchease_in")->where(["wsm_in_code" => Db::raw(" like %{$wsm_in_code}%"), "is_del" => 0])->column("cgdNo");
  52. if (empty($incode)) {
  53. return error_show(1004, "未找到有关入库单信息");
  54. }
  55. $where[] = ['po.cgdNo', "in", $incode];
  56. }
  57. $good_name = isset($this->post['good_name']) && $this->post['good_name'] != "" ? trim($this->post['good_name']) : "";
  58. if ($good_name !== "") {
  59. //$where['good_name'] = Db::raw(" like %{$good_name}%");
  60. $where[] = ['po.good_name', "like", "%{$good_name}%"];
  61. }
  62. $good_code = isset($this->post['good_code']) && $this->post['good_code'] != "" ? trim($this->post['good_code']) : "";
  63. if ($good_code !== "") {
  64. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  65. $where[] = ['po.spuCode', "like", "%{$good_code}%"];
  66. }
  67. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
  68. if ($supplierNo !== "") {
  69. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  70. $where[] = ['po.supplierNo', "=", $supplierNo];
  71. }
  72. $wsm_supplierNo = isset($this->post['wsm_supplierNo']) && $this->post['wsm_supplierNo'] != "" ? trim($this->post['wsm_supplierNo']) : "";
  73. if ($wsm_supplierNo !== "") {
  74. // $supplier = Db::name("supplier")->where(["code"=>$wsm_supplierNo])->find();
  75. // if(empty($supplier)){
  76. // return error_show(1004,"未找到供应商信息");
  77. // }
  78. $wsmcode = Db::name("warehouse_info")->where(["is_del" => 0, "supplierNo" => $wsm_supplierNo])->column("wsm_code");
  79. $where[] = ['po.wsm_code', "in", $wsmcode];
  80. }
  81. $start = isset($this->post['start']) && $this->post['start'] != "" ? $this->post['start'] : "";
  82. if ($start !== "") {
  83. // $where['addtime'] = Db::raw(" >= '{$start}'");
  84. $where[] = ['po.addtime', ">=", $start . " 00:00:00"];
  85. }
  86. $end = isset($this->post['end']) && $this->post['end'] != "" ? $this->post['end'] : "";
  87. if ($end !== "") {
  88. $where[] = ['po.addtime', "<=", $end . " 23:59:59"];
  89. }
  90. $last_start = isset($this->post['last_start']) && $this->post['last_start'] != "" ? $this->post['last_start'] : "";
  91. if ($last_start !== "") {
  92. //$where['lasttime'] = Db::raw(" >= '{$last_start}'");
  93. $where[] = ['po.lasttime', ">=", $last_start . " 00:00:00"];
  94. }
  95. $last_end = isset($this->post['last_end']) && $this->post['last_end'] != "" ? $this->post['last_end'] : "";
  96. if ($last_end !== "") {
  97. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  98. $where[] = ['po.lasttime', "<=", $last_end . " 23:59:59"];
  99. }
  100. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? $this->post['orderCode'] : "";
  101. if ($orderCode !== "") {
  102. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  103. $where[] = ['on.orderCode', "like", '%' . $orderCode . '%'];
  104. }
  105. //筛选供应商名称
  106. $supplier_name = isset($this->post['supplier_name']) && $this->post['supplier_name'] != "" ? $this->post['supplier_name'] : "";
  107. if ($supplier_name != '') $where[] = ['po.supplier_name', 'like', '%' . $supplier_name . '%'];
  108. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] != "" ? trim($this->post['relaComNo']) : "";
  109. if ($relaComNo !== "") $where[] = ['po.companyNo', '=', $relaComNo];
  110. // $whereor=[];
  111. // if(!empty($role['write'])){
  112. //// $whereor[]=["po.cgder_id","in",$role['write']];
  113. //// $whereor[]=["sip.personid","=",$this->uid];
  114. // $whereor[]=["sip.personid","in",$role['write']];//最后的筛选条件
  115. //// $whereor[]=["po.cgder_id","=",$this->uid];
  116. // }
  117. // $role = $this->checkDataShare();
  118. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ["sip.personid", "in", $role[DataGroupModel::$type_全部]];
  119. $userCommon = \app\admin\common\User::getIns();
  120. //只有level2的账号过滤数据权限
  121. if ($this->level == 2) {
  122. $role = $this->checkDataShare();
  123. $hand = resign_hand_user($this->uid, 0);
  124. if (!empty($role[DataGroupModel::$type_全部])) {
  125. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  126. $supplierNos = Db::connect('mysql_sys')
  127. ->name('supplier')
  128. ->where(['is_del' => 0, 'personid' => $arr])
  129. ->column('code');
  130. $where[] = ['po.supplierNo', 'in', $supplierNos];
  131. }
  132. }
  133. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  134. if ($company_name !== "") $where[] = ["po.cgder_id", 'in', get_company_item_user_by_name($company_name)];
  135. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : "";
  136. if ($order_type !== "") $where[] = ["po.order_type", '=', $order_type];
  137. $order_source = isset($this->post['order_source']) && $this->post['order_source'] !== "" ? intval($this->post['order_source']) : "";
  138. if ($order_source !== "") $where[] = ["po.order_source", '=', $order_source];
  139. // if(!empty($role['platform']) ){
  140. // $where[]=["po.platform_id","in",$role['platform']];
  141. // }
  142. $count = Db::name("purchease_order")
  143. ->alias('po')
  144. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  145. ->leftJoin("depart_user u", "u.uid=po.cgder_id AND u.is_del=0")
  146. // ->leftJoin('supplier sip', 'sip.code=po.supplierNo')
  147. ->where($where)
  148. ->where(function ($query) use ($where) {
  149. $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
  150. })->count();
  151. $total = ceil($count / $size);
  152. $page = $page >= $total ? $total : $page;
  153. $list = Db::name("purchease_order")
  154. ->alias('po')
  155. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  156. // ->leftJoin('supplier sip', 'sip.code=po.supplierNo')
  157. ->field('po.*,on.orderCode,"" supplier_cgderid,"" supplier_cgder')
  158. ->where($where)
  159. ->where(function ($query) use ($where) {
  160. $query->whereOr([['po.order_type', '<>', 1], ['po.order_source', '=', 0]]);
  161. })
  162. ->page($page, $size)
  163. ->order("po.addtime desc")
  164. ->select()
  165. ->toArray();
  166. $account = checkHasAccountBySupplierNos(array_column($list, 'supplierNo'));
  167. $all_createrid = array_column($list, 'cgder_id');
  168. $item = get_company_name_by_uid($all_createrid);
  169. $data = [];
  170. $wsmCodes = array_column($list, 'wsm_code');
  171. $wsmcode = Db::name("warehouse_info")
  172. ->alias("a")
  173. ->where(["a.wsm_code" => $wsmCodes])
  174. ->column("a.name as wsm_name,a.supplierNo as code", "wsm_code");
  175. // $wsmsupplier=[];
  176. // if(!empty($wsmcode)){
  177. //
  178. //
  179. // $wsmsupplier = $names['data']??[];
  180. // }
  181. $names = $userCommon->handle('getCodeAndName', ['code' => array_unique(array_column($wsmcode, "code"))]);
  182. //补充供应商负责人(sip.personid as supplier_cgderid,sip.person as supplier_cgder)
  183. $person_list = $userCommon->handle('sGetList', ['more_code' => array_unique(array_column($list, 'supplierNo')), 'size' => $size]);
  184. $person_list = array_column($person_list['data']['list'], null, 'code');
  185. $goon_3_4 = Db::name('good_zixun')
  186. ->where(['spuCode' => array_column($list, 'spuCode'), 'is_del' => 0])
  187. ->column('specinfo,cat_id,createrid,creater', 'spuCode');
  188. $goon_1_2 = Db::name('good_basic')
  189. ->where(['is_del' => 0, 'spuCode' => array_column($list, 'spuCode')])
  190. ->column('cat_id,createrid,creater', 'spuCode');
  191. $cat_temp = [];
  192. foreach ($list as $value) {
  193. // $value['wsm_name'] = isset($wsmcode[$value['wsm_code']]['wsm_name']) ? $wsmcode[$value['wsm_code']]['wsm_name'] : "";
  194. // $value['wsm_supplier'] =isset($wsmcode[$value['wsm_code']]['code'])? $person_list[$wsmcode[$value['wsm_code']]['code']]['name'] ?? "":"";
  195. // $value['wsm_supplierNo'] = $wsmcode[$value['wsm_code']]['code'] ?? "";
  196. $value['wsm_name'] = $value['wsm_code'] != '' ? $wsmcode[$value['wsm_code']]['wsm_name'] ?? '' : "";
  197. $value['wsm_supplier'] = $value['wsm_code'] != '' ? $names[$wsmcode[$value['wsm_code']]['code']] ?? '' : '';
  198. $value['wsm_supplierNo'] = $value['wsm_code'] != '' ? $wsmcode[$value['wsm_code']]['code'] ?? "" : '';
  199. $value['supplier_cgderid'] = $person_list[$value['supplierNo']]['personid'] ?? '';
  200. $value['supplier_cgder'] = $person_list[$value['supplierNo']]['person'] ?? '';
  201. // $value['companyName'] = $names['data'][$value['companyNo']] ?? '';
  202. // $value['wsm_name']="";
  203. // if($value['wsm_code']!=""){
  204. //// $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  205. //// ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  206. // $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  207. // $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  208. // $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  209. // }
  210. // $supplierCgder =Db::name("supplier")->where(["code"=>$value['supplierNo']])->findOrEmpty();
  211. // $value['supplier_cgderid'] = $supplierCgder['personid'] ??'';
  212. // $value['supplier_cgder'] = $supplierCgder['person'] ??'';
  213. // $cat_id = 0;
  214. if ($value['order_type'] == 3 || $value['order_type'] == 4) {
  215. // $goon = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  216. $value["speclist"] = json_decode($goon_3_4[$value['spuCode']]['specinfo'],true);//isset($goon['specinfo'])&&$goon['specinfo']!=""? json_decode($goon['specinfo'],true):"";
  217. //为了格式统一,缺了一个spec_value
  218. if ($value['speclist'] != '') {
  219. foreach ($value['speclist'] as &$v) {
  220. $v['spec_value'] = $v['spec_value_name'];
  221. }
  222. }
  223. $cat_id = $goon_3_4[$value['spuCode']]['cat_id'];
  224. } else {
  225. // $goon =Db::name('good_basic')->field('cat_id')->where(['spuCode'=>$value['spuCode']])->find();
  226. $spec = Db::name("good_spec")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();
  227. $speclist = [];
  228. if (!empty($spec)) {
  229. foreach ($spec as $val) {
  230. $temp = [];
  231. $temp['id'] = $val['id'];
  232. $temp['spuCode'] = $val['spuCode'];
  233. $temp['spec_id'] = $val['spec_id'];
  234. $temp['spec_value_id'] = $val['spec_value_id'];
  235. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  236. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  237. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  238. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  239. $speclist[] = $temp;
  240. }
  241. }
  242. $value["speclist"] = empty($speclist) ? [] : $speclist;
  243. $cat_id = $goon_1_2[$value['spuCode']]['cat_id'];
  244. }
  245. if (!isset($cat_temp[$cat_id])) $cat_temp[$cat_id] = made($cat_id);
  246. $value['can'] = $cat_temp[$cat_id];
  247. //采购单详情字段order_type 为1或者2时,取线上商品库 商品创建人 order_type 为3或者4时 取采购反馈的商品库 商品反馈人
  248. // $value['cgder_id'] = $goon['createrid'];
  249. // $value['cgder'] = $goon['creater'];
  250. $inorder = Db::name("purchease_in")
  251. ->where(['cgdNo' => $value['cgdNo'], "is_del" => 0])
  252. ->select()
  253. ->toArray();
  254. $value['child'] = empty($inorder) ? [] : $inorder;
  255. $value['send_num'] -= $value['th_num'];
  256. $value['total_fee'] = round($value['total_fee'] - $value['th_fee'], 2);
  257. //补充orderCode
  258. // $value['orderCode']=isset($all_orderCodes[$value['cgdNo']])?$all_orderCodes[$value['cgdNo']]:'';
  259. $value['company_name'] = $item[$value['cgder_id']] ?? '';
  260. $value['has_account'] = (int)isset($account[$value['supplierNo']]);
  261. //是否具有编辑权限
  262. // $value['is_allow_update'] = 0;
  263. // if (in_array($this->roleid, [1, 33]) || in_array($value['cgder_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  264. $data[] = $value;
  265. }
  266. return app_show(0, "获取成功", ["list" => $data, "count" => $count]);
  267. }
  268. public function info()
  269. {
  270. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? trim($this->post['cgdNo']) : "";
  271. if ($cgdNo == "") {
  272. return error_show(1004, "参数cgdNo 不能为空");
  273. }
  274. //采购单
  275. $data = Db::name("purchease_order")
  276. ->alias('a')
  277. ->field('a.*,b.orderCode')
  278. ->leftJoin('order_num b', 'b.cgdNo=a.cgdNo')
  279. ->where(["a.cgdNo" => $cgdNo, "a.is_del" => 0])
  280. ->findOrEmpty();
  281. if (empty($data)) {
  282. return error_show(1004, "未找到数据");
  283. }
  284. //采购退货单
  285. $im = Db::name('purchease_back')->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->select();
  286. $var = [];
  287. foreach ($im as $value) {
  288. if (in_array($data['order_type'], [3, 4])) {
  289. $goo = Db::name("good_zixun")->field('cat_id')->where(["spuCode" => $value['spuCode'], "is_del" => 0])->findOrEmpty();
  290. } else {
  291. $goo = Db::name('good_basic')->field('cat_id')->where(['spuCode' => $value['spuCode']])->findOrEmpty();
  292. }
  293. if (empty($goo)) {
  294. return error_show(1002, "未找到商品数据");
  295. }
  296. $cat = isset($goo['cat_id']) && $goo['cat_id'] != 0 ? made($goo['cat_id']) : [];
  297. $value['cant'] = $cat;
  298. $var[] = $value;
  299. }
  300. //采购工差单
  301. $dom = Db::name("purchease_diff")->where(['cgdNo' => $data['cgdNo'], 'is_del' => 0])->find();
  302. if (in_array($data['order_type'], [3, 4])) {
  303. $goon = Db::name("good_zixun")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->find();
  304. } else {
  305. $goon = Db::name('good_basic')->where(['spuCode' => $data['spuCode']])->find();
  306. }
  307. if (empty($goon)) {
  308. return error_show(1002, "未找到商品数据");
  309. } else {
  310. $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
  311. $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
  312. $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  313. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  314. $speclist = [];
  315. if (!empty($spec)) {
  316. foreach ($spec as $value) {
  317. $temp = [];
  318. $temp['id'] = $value['id'];
  319. $temp['spuCode'] = $value['spuCode'];
  320. $temp['spec_id'] = $value['spec_id'];
  321. $temp['spec_value_id'] = $value['spec_value_id'];
  322. $temp['is_del'] = $value['is_del'];
  323. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  324. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  325. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  326. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  327. $speclist[] = $temp;
  328. }
  329. }
  330. $goon["speclist"] = empty($speclist) ? [] : $speclist;
  331. $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  332. $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
  333. $goon['origin_place_cn'] = "";
  334. $goon['delivery_place_cn'] = "";
  335. if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
  336. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  337. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
  338. $goon['delivery_place_cn'] = GetAddr(json_encode($place));
  339. }
  340. if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
  341. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  342. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
  343. $goon['origin_place_cn'] = GetAddr(json_encode($place));
  344. }
  345. if ($goon['brand_id'] != 0) {
  346. $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
  347. $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  348. } else {
  349. $goon["brand_name"] = "";
  350. $goon["brand_id"] = "";
  351. }
  352. $userCommon = \app\admin\common\User::getIns();
  353. $supplier = $userCommon->handle('sInfo', ['code' => $goon['supplierNo']]);
  354. // $supplier = Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  355. $goon['supplier_cgderid'] = $supplier['data']['personid'] ?? '';
  356. $goon['supplier_cgder'] = $supplier['data']['person'] ?? '';
  357. $goon['supplierName'] = isset($supplier['data']['name']) ? $supplier['data']['name'] : "";
  358. $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
  359. //补充客户名称
  360. if (empty($data['orderCode'])) $data['customer'] = ['companyNo' => '', 'companyName' => ''];
  361. else {
  362. $customer_code = Db::name('sale')
  363. ->alias('s')
  364. // ->field('c.companyNo,c.companyName')
  365. // ->leftJoin('customer_info c', 'c.companyNo=s.customer_code AND c.is_del=0')
  366. ->where(['s.orderCode' => $data['orderCode']])
  367. ->value('s.customer_code', '');
  368. }
  369. $tmp = $userCommon->handle('getCodeAndName', ['code' => [
  370. $goon['companyNo'],
  371. $data['wsm_code'],
  372. $data['companyNo'],
  373. $customer_code ?? ''
  374. ]]);
  375. if (!empty($data['orderCode'])) {
  376. $data['customer']['companyNo'] = $customer_code;
  377. $data['customer']['companyName'] = $tmp['data'][$customer_code] ?? '';
  378. }
  379. if (isset($goon['companyNo']) && $goon['companyNo'] != "") {
  380. // $company = Db::name("business")->where(["companyNo"=>$goon['companyNo']])->find();
  381. }
  382. $goon['company'] = $tmp['data'][$goon['companyNo']] ?? '';
  383. }
  384. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  385. $data['wsm_name'] = "";
  386. if ($data['wsm_code'] != "") {
  387. $wsmcode = Db::name("warehouse_info")
  388. ->alias("a")
  389. // ->leftJoin("supplier b","a.supplierNo=b.code")
  390. ->leftJoin("warehouse_addr c", "a.wsm_code=c.wsm_code and c.is_del=0")
  391. ->where(["a.wsm_code" => $data['wsm_code']])
  392. ->field("a.name as wsm_name,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code")
  393. ->find();
  394. $data['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  395. $data['wsm_supplier'] = isset($wsmcode['data'][$data['wsm_code']]) ? $wsmcode['data'][$data['wsm_code']] : "";
  396. $data['wsm_supplierNo'] = isset($wsmcode['data'][$data['wsm_code']]) ? $wsmcode['data'][$data['wsm_code']] : "";
  397. $data['wsm_contactor'] = isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor'] : "";
  398. $data['wsm_mobile'] = isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile'] : "";
  399. // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  400. $data['addr_code'] = isset($wsmcode['addr_code']) ? $wsmcode['addr_code'] : "";
  401. $data['wsm_addr'] = isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']) : "";
  402. $data['wsm_addr'] .= isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr'] : "";
  403. $inorder = Db::name("purchease_in")->where(['cgdNo' => $data['cgdNo'], "is_del" => 0])->select();
  404. $data['child'] = empty($inorder) ? [] : $inorder;
  405. }
  406. $data['can'] = $int;
  407. $data['goodinfo'] = $goon;
  408. $data['purcheasediff'] = $dom;
  409. $data['send_num'] -= $data['th_num'];
  410. $data['total_fee'] = round($data['total_fee'] - $data['th_fee'], 2);
  411. $data['company'] = '';
  412. if (isset($data['companyNo']) && $data['companyNo'] != "") {
  413. // $company = Db::name("business")->where(["companyNo"=>$data['companyNo']])->find();
  414. $data['company'] = $tmp['data'][$data['companyNo']] ?? '';
  415. }
  416. //$data['company'] = isset($company['company'])?$company['company']:"";
  417. //$data['info'] = $var;
  418. $data['purcheaseback'] = $var;
  419. //补充orderCode
  420. // $data['orderCode'] = Db::name('order_num')
  421. // ->where('cgdNo', $data['cgdNo'])
  422. // ->value('orderCode', '');
  423. // //补充收货地址
  424. // $addr = Db::name('order_addr')
  425. // ->field('id,addr,addr_code,contactor,mobile,receipt_quantity')
  426. // ->where(['orderCode' => $data['orderCode'], 'is_del' => 0])
  427. // ->select()
  428. // ->toArray();
  429. // if (!empty($addr)) {
  430. // foreach ($addr as &$vv){
  431. // $temp = explode(',', $vv['addr_code']);
  432. // $temp_ = GetAddr(json_encode(['provice_code' => isset($temp[0]) ? $temp[0] : '', 'city_code' => isset($temp[1]) ? $temp[1] : '', 'area_code' => isset($temp[2]) ? $temp[2] : '']));
  433. // $vv['addr_code'] = $temp_ . $vv['addr'];
  434. // }
  435. // }else
  436. $addr = [];
  437. $data['addr_info'] = $addr;
  438. $data['has_account'] = (int)isset(checkHasAccountBySupplierNos([$data['supplierNo']])[$data['supplierNo']]);
  439. //补充商品创建人字段
  440. $data['good_createrid'] = $goon['createrid'] ?? '';
  441. $data['good_creater'] = $goon['creater'] ?? '';
  442. return app_show(0, "获取成功", $data);
  443. }
  444. public function edit(){
  445. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  446. if($cgdNo==""){
  447. return error_show(1004,"参数cgdNo 不能为空");
  448. }
  449. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  450. if(empty($data)){
  451. return error_show(1004,"未找到数据");
  452. }
  453. $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$this->post['remark'],"action_type"=>"edit"];
  454. $wsm_code = isset($this->post['wsm_code'])&&$this->post['wsm_code']!=""? trim($this->post['wsm_code']):"";
  455. if($wsm_code!=""){
  456. $ware = Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code,"is_del"=>0])->find();
  457. if(empty($ware)){
  458. return error_show(1004,"未找到仓库信息");
  459. }
  460. // $good = Db::name("good_stock")->where(["wsm_code"=>$wsm_code,"good_type_code"=>$data['good_type_code'],"is_del"=>0])->find();
  461. // if(empty($good)){
  462. // return error_show(1004,"未找到仓库下商品信息");
  463. // }
  464. $data['wsm_code'] = $wsm_code;
  465. }
  466. $cgdid = isset($this->post['cgder_id'])&&$this->post['cgder_id']!=""?intval($this->post['cgder_id']):"";
  467. if($cgdid!=''){
  468. $cgduser = GetInfoById($this->post['token'],["id"=>$cgdid]);
  469. if((!empty($cgduser) && $cgduser['code']!=0) ||empty($cgduser) ){
  470. return error_show($cgduser['code'],$cgduser['message']);
  471. }
  472. $userinfo = $cgduser['data'];
  473. $data['cgder_id'] = $cgdid;
  474. $data['cgder'] = $userinfo['nickname'];
  475. }
  476. $good_num = isset($this->post['good_num'])&&$this->post['good_num']!=""?intval($this->post['good_num']):"";
  477. if($good_num!=""){
  478. $data['good_num'] = $good_num;
  479. }
  480. $good_price = isset($this->post['good_price'])&&$this->post['good_price']!=""?$this->post['good_price']:"";
  481. if($good_price!=""){
  482. $data['good_price'] = $good_price;
  483. }
  484. // $total_fee = isset($this->post['total_fee'])&&$this->post['total_fee']!=""?$this->post['total_fee']:"";
  485. // if($total_fee!=""){
  486. // $data['total_fee'] = $total_fee;
  487. // }
  488. $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!=""?$this->post['pakge_fee']:"";
  489. if($pakge_fee!=""){
  490. $data['pakge_fee'] = $pakge_fee;
  491. }
  492. $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!=""?$this->post['cert_fee']:"";
  493. if($cert_fee!=""){
  494. $data['cert_fee'] = $cert_fee;
  495. }
  496. $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!=""?$this->post['open_fee']:"";
  497. if($open_fee!=""){
  498. $data['open_fee'] = $open_fee;
  499. }
  500. $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!=""?$this->post['delivery_fee']:"";
  501. if($delivery_fee!=""){
  502. $data['delivery_fee'] = $delivery_fee;
  503. }
  504. $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!=""?$this->post['mark_fee']:"";
  505. if($mark_fee!=""){
  506. $data['mark_fee'] = $mark_fee;
  507. }
  508. $teach_fee = isset($this->post['teach_fee'])&&$this->post['teach_fee']!=""?$this->post['teach_fee']:"";
  509. if($teach_fee!=""){
  510. $data['teach_fee'] = $teach_fee;
  511. }
  512. $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!=""?$this->post['nake_fee']:"";
  513. if($nake_fee!=""){
  514. $data['nake_fee'] = $nake_fee;
  515. }
  516. $weight = isset($this->post['weight'])&&$this->post['weight']!=""?$this->post['weight']:"";
  517. if($weight!=""){
  518. $data['weight'] = $weight;
  519. }
  520. $diff_weight = isset($this->post['diff_weight'])&&$this->post['diff_weight']!=""?$this->post['diff_weight']:"";
  521. if($diff_weight!=""){
  522. $data['diff_weight'] = $diff_weight;
  523. }
  524. $diff_fee = isset($this->post['diff_fee'])&&$this->post['diff_fee']!=""?$this->post['diff_fee']:"";
  525. if($diff_fee!=""){
  526. $data['diff_fee'] = $diff_fee;
  527. }
  528. $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!=""?trim($this->post['supplierNo']) :"";
  529. if($supplierNo!=""){
  530. $userCommon = \app\admin\common\User::getIns();
  531. $supplier_temp = $userCommon->handle('getCodeAndName', ['code' => $supplierNo]);
  532. // $supplier =Db::name("supplier")->where(['code'=>$supplierNo])->find();
  533. if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1004,"未找到供应商信息");
  534. // if(empty($supplier)){
  535. // return error_show(1004,"未找到供应商信息");
  536. // }
  537. $data['supplierNo'] = $supplierNo;
  538. $data['supplier_name'] = $supplier_temp['data'][$supplierNo]??"";
  539. }
  540. $remark = isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  541. if($remark!=""){
  542. $data['remark'] = $remark;
  543. }
  544. $data['good_price'] = round(($data['pakge_fee']+$data['weight']*$data['gold_price']+$data['nake_fee']+$data['mark_fee']+$data['cert_fee']+$data['open_fee']/$data['good_num']+$data['teach_fee']*$data['weight']+$data['delivery_fee']),2);
  545. $data['total_fee'] = round($data['good_price']*$data['good_num'],2);
  546. $data['updatetime'] =date("Y-m-d H:i:s");
  547. $upd=Db::name("purchease_order")->save($data);
  548. if($upd){
  549. // ActionLog::logAdd($this->post['token'],$order,'cgd',$data['status'],$this->post);
  550. //修改状态,添加待办
  551. ActionLog::logAdd($this->post['token'], $order, "CGD", $data['status'], $this->post);
  552. // ProcessOrder::AddProcess($this->post['token'], [
  553. // "order_type" => 'CGD',
  554. // "order_code" => $cgdNo,//销售单code
  555. // "order_id" => $data['id'],
  556. // "order_status" => $data['status'],"before_status"=>1
  557. // ]);
  558. return app_show(0,"更新成功");
  559. }else{
  560. return error_show(1004,'更新失败');
  561. }
  562. }
  563. //修改采购单
  564. public function editNew()
  565. {
  566. $param = $this->request->only(['cgdNo', 'supplierNo', 'token', 'wsm_code' => ''], 'post', 'trim');
  567. $val = Validate::rule([
  568. 'cgdNo|采购单号' => 'require',
  569. 'supplierNo|供应商编号' => 'require',
  570. 'token' => 'require',
  571. ]);
  572. if (!$val->check($param)) return error_show(1004, $val->getError());
  573. Db::startTrans();
  574. try {
  575. $rs = Db::name('purchease_order')
  576. ->alias('po')
  577. ->field('po.id,po.order_type,po.order_source,po.status,po.cgder_id,po.cgder,po.supplierNo,po.supplier_name,po.wsm_code,wi.name wsm_name')
  578. ->leftJoin('warehouse_info wi', 'wi.wsm_code=po.wsm_code AND wi.is_del=0')
  579. ->where([
  580. 'po.cgdNo' => $param['cgdNo'],
  581. 'po.is_del' => 0,
  582. 'po.status' => 0
  583. ])->find();
  584. if (empty($rs)) throw new Exception('采购单不存在或不允许修改');
  585. $update = ['updatetime' => date('Y-m-d H:i:s')];
  586. //order_type==1备库
  587. if ($rs['order_type'] == 1 && $rs['order_source']==0) {
  588. if (empty($param['wsm_code'])) throw new Exception('备库单请传入仓库编码');
  589. else {
  590. $wsm_rs = Db::name('warehouse_info')
  591. ->field('id,wsm_code,name wsm_name')
  592. ->where([
  593. 'wsm_code' => $param['wsm_code'],
  594. 'is_del' => 0,
  595. ])->find();
  596. if (empty($wsm_rs)) throw new Exception('查不到该仓库的记录');
  597. else $update['wsm_code'] = $wsm_rs['wsm_code'];
  598. }
  599. }
  600. //供应商信息
  601. // $supplier_rs = Db::name('supplier')
  602. // ->field('id,name supplierName,code supplierNo,person,personid')
  603. // ->where(['is_del' => 0, 'code' => $param['supplierNo']])
  604. // ->find();
  605. $userCommon= \app\admin\common\User::getIns();
  606. $supplier_temp =$userCommon->handle("sInfo",["code"=>$param['supplierNo']]);
  607. if ($supplier_temp['code']!=0 || empty($supplier_temp['data'])) throw new Exception('查不到该供应商的记录');
  608. else {
  609. $supplier_rs=$supplier_temp['data'];
  610. $update['cgder_id'] = $supplier_rs['personid'];
  611. $update['cgder'] = $supplier_rs['person'];
  612. $update['supplierNo'] = $supplier_rs['code'];
  613. $update['supplier_name'] = $supplier_rs['name'];
  614. }
  615. //是否修改内容确定
  616. if (isset($update['wsm_code'])) {
  617. if ($update['wsm_code'] == $rs['wsm_code'] && $update['supplierNo'] == $rs['supplierNo']) throw new Exception('没有修改的内容,不能提交');
  618. } elseif ($update['supplierNo'] == $rs['supplierNo']) throw new Exception('没有修改的内容,不能提交');
  619. if ($update['supplierNo'] == $rs['supplierNo'] && $param['wsm_code'] == $rs['wsm_code']) throw new Exception('没有修改的内容');
  620. //修改
  621. Db::name('purchease_order')
  622. ->where([
  623. 'id' => $rs['id'],
  624. 'is_del' => 0,
  625. 'status' => 0
  626. ])->update($update);
  627. $user = GetUserInfo($param['token']);
  628. $creater_id = isset($user['data']['id']) ? $user['data']['id'] : 0;
  629. $creater = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  630. //添加修改记录
  631. Db::name('purchease_change_supplier_log')
  632. ->insert([
  633. 'cgdNo' => $param['cgdNo'],
  634. 'cgder_id_old' => $rs['cgder_id'],
  635. 'cgder_id' => $update['cgder_id'],
  636. 'cgder_old' => $rs['cgder'],
  637. 'cgder' => $update['cgder'],
  638. 'supplierNo_old' => $rs['supplierNo'],
  639. 'supplierNo' => $update['supplierNo'],
  640. 'supplier_name_old' => $rs['supplier_name'],
  641. 'supplier_name' => $update['supplier_name'],
  642. 'wsm_code_old' => $rs['wsm_code'],
  643. 'wsm_code' => isset($update['wsm_code']) ? $update['wsm_code'] : $rs['wsm_code'],
  644. 'wsm_name_old' => $rs['wsm_name'],
  645. 'wsm_name' => isset($wsm_rs['wsm_name']) ? $wsm_rs['wsm_name'] : $rs['wsm_name'],
  646. 'addtime' => date('Y-m-d H:i:s'),
  647. 'updatetime' => date('Y-m-d H:i:s'),
  648. 'creater_id' => $creater_id,
  649. 'creater' => $creater,
  650. ]);
  651. //记录日志
  652. ActionLog::logAdd(['id' => $creater_id, 'nickname' => $creater], $rs, "CGD", $rs['status'], $param);
  653. // ProcessOrder::AddProcess($this->post['token'], [
  654. // "order_type" => 'CGD',
  655. // "order_code" => $param['cgdNo'],//销售单code
  656. // "order_id" => $rs['id'],
  657. // "order_status" => $rs['status']
  658. // ]);
  659. //维护台账
  660. Db::name('standing_book')
  661. ->where('cgdNo', $param['cgdNo'])
  662. ->update(['supplierNo' => $param['supplierNo'], 'updatetime' => date('Y-m-d H:i:s')]);
  663. Db::commit();
  664. return app_show(0, '修改成功');
  665. } catch (Exception $exception) {
  666. Db::rollback();
  667. return error_show(1005, $exception->getMessage());
  668. }
  669. }
  670. public function status()
  671. {
  672. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
  673. if (empty($cgdNo)) {
  674. return error_show(1004, "参数cgdNo 不能为空");
  675. }
  676. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  677. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  678. if ($status === "") {
  679. return error_show(1004, "参数status 不能为空");
  680. }
  681. Db::startTrans();
  682. try {
  683. if ($status == 0) {
  684. $data = Db::name("purchease_order")
  685. ->field('id,cgdNo,spuCode')
  686. ->whereIn('cgdNo', $cgdNo)
  687. ->where(["is_del" => 0])
  688. ->where('send_status', '>', 1)
  689. ->find();
  690. if (!empty($data)) throw new Exception($data['cgdNo'] . "采购单发货中无法取消");
  691. }
  692. // if($data['send_status']>1 && $status==0){
  693. // return error_show(1004,"采购单发货中无法取消");
  694. // }
  695. // $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$remark,"action_type"=>"status"];
  696. // $data['status'] = $status;
  697. // $data['remark'] = $remark;
  698. // $data['updatetime'] =date("Y-m-d H:i:s");
  699. //原始数据
  700. $info = Db::name("purchease_order")
  701. ->whereIn('cgdNo', $cgdNo)
  702. ->column('id,cgdNo,status,supplierNo,spuCode,order_type', 'cgdNo');
  703. $upd = Db::name("purchease_order")
  704. ->whereIn('cgdNo', $cgdNo)
  705. ->save([
  706. 'status' => $status,
  707. 'remark' => $remark,
  708. 'updatetime' => date("Y-m-d H:i:s"),
  709. ]);
  710. if ($upd) {
  711. //获取供应商的负责人
  712. $userCommon = \app\admin\common\User::getIns();
  713. $supplier = $userCommon->handle('sGetList',['more_code'=>array_column($info, 'supplierNo')]);
  714. $supplier_cgderid = array_column($supplier['data']['list'],'personid','code');
  715. // $supplier_cgderid = Db::name('supplier')
  716. // ->where('is_del', 0)
  717. // ->whereIn('code', array_column($info, 'supplierNo'))
  718. // ->column('personid', 'code');
  719. // ->leftJoin('supplier sip', 'sip.code=po.supplierNo')
  720. // ->field('po.*,on.orderCode,u.itemid,sip.personid as supplier_cgderid,sip.person as supplier_cgder')
  721. // $user=GetUserInfo($this->post['token']);
  722. $uid = $this->uid;
  723. $uname = $this->uname;
  724. $all_has_supplier_account = checkHasAccountBySupplierNos(array_column($info, 'supplierNo'));
  725. foreach ($cgdNo as $vlue) {
  726. $isreturn = cgd_sale_return($vlue);
  727. if($isreturn){
  728. throw new Exception("{$vlue}关联销售单存在退货单正在进行");
  729. }
  730. if($status == 1){
  731. //如果开通了供应商账号,则只允许level3账号操作,level2账号不允许操作
  732. if (isset($all_has_supplier_account[$info[$vlue]['supplierNo']]) && $this->level == 2) throw new Exception('已开通供应商账号,当前账号不能操作');
  733. // if($this->level == 2 && $uid != $supplier_cgderid[$info[$vlue]['supplierNo']]) throw new Exception('只能由供应商负责人来操作');//与供应商确认的这个操作,只能由供应商负责人来操作
  734. }
  735. if ($status == 1 || $status == 2) {
  736. // $orderinfo = Db::name('sale')
  737. // ->alias('a')
  738. // ->field('a.id,a.orderCode,a.is_stock,a.order_type,a.cgderid')
  739. // ->leftJoin('order_num b','b.orderCode=a.orderCode')
  740. // ->where([
  741. // 'a.is_del'=>0,
  742. // 'b.cgdNo'=>$vlue,
  743. // ])->findOrEmpty();
  744. // $is_stock = Db::name('good_basic')
  745. // ->where(['is_del' => 0, 'spuCode' => $info[$vlue]['spuCode']])
  746. // ->value('is_stock');
  747. // if ($is_stock == 1) {
  748. // //库存品,推给31库管人员、41库管-张凯旋
  749. // $uids = Db::name('user_role')
  750. // ->where([
  751. // ['is_del', '=', 0],
  752. // ['roleid', 'in', [31, 41]],
  753. // ['status', '=', 1]
  754. // ])->column('uid');
  755. //// if (!in_array($this->uid, $uids)) throw new Exception('库存品订单只能由库管人员操作');
  756. // $process = ["order_code" => $vlue, "order_id" => isset($info[$vlue]['id']) ? $info[$vlue]['id'] : 0, "order_status" => $status, "order_type" => 'CGD', "before_status" => isset($info[$vlue]['status']) ? $info[$vlue]['status'] : 0, 'holder_id' => isset($supplier_cgderid[$info[$vlue]['supplierNo']]) ? $supplier_cgderid[$info[$vlue]['supplierNo']] : 0, 'handle_user_list' => implode(',', $uids)];
  757. // } elseif ($is_stock == 0 || $info[$vlue]['order_type'] == 3) {
  758. //非库存品和采返商品,推给供应商负责人
  759. // if ($this->uid != $supplier_cgderid[$info[$vlue]['supplierNo']]) throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  760. $process = ["order_code" => $vlue, "order_id" => isset($info[$vlue]['id']) ? $info[$vlue]['id'] : 0, "order_status" => $status, "order_type" => 'CGD', "before_status" => isset($info[$vlue]['status']) ? $info[$vlue]['status'] : 0, 'holder_id' => isset($supplier_cgderid[$info[$vlue]['supplierNo']]) ? $supplier_cgderid[$info[$vlue]['supplierNo']] : 0, 'handle_user_list' => isset($supplier_cgderid[$info[$vlue]['supplierNo']]) ? $supplier_cgderid[$info[$vlue]['supplierNo']] : 0];
  761. // }
  762. } else $process = ["order_code" => $vlue, "order_id" => isset($info[$vlue]['id']) ? $info[$vlue]['id'] : 0, "order_status" => $status, "order_type" => 'CGD', "before_status" => isset($info[$vlue]['status']) ? $info[$vlue]['status'] : 0, 'holder_id' => isset($supplier_cgderid[$info[$vlue]['supplierNo']]) ? $supplier_cgderid[$info[$vlue]['supplierNo']] : 0];
  763. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], $process);
  764. $order = ["order_code" => $vlue, "status" => '', "action_remark" => $remark, "action_type" => "status"];
  765. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], $order, 'CGD', $status, $this->post);
  766. }
  767. // return app_show(0, "更新成功");
  768. } else throw new Exception('更新失败');
  769. // return error_show(1004, '更新失败');
  770. Db::commit();
  771. return app_show(0, "更新成功");
  772. } catch (Exception $exception) {
  773. Db::rollback();
  774. return error_show(1004, '更新失败,' . $exception->getMessage());
  775. }
  776. }
  777. //创建采购工差单
  778. public function diffcreat(){
  779. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  780. //$cgdNo=makeNo("CG");
  781. $cgdNo = $this->post['cgdNo'] && $this->post['cgdNo'] !=="" ? trim($this->post['cgdNo']) :"";
  782. if($cgdNo==""){
  783. return error_show(1002,"参数good_code不能为空");
  784. }
  785. $cg = Db::name('purchease_order')->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
  786. if($cg==""){
  787. return error_show(1002,"未找到采购单数据");
  788. }
  789. if (in_array($cg['order_type'], [3, 4])) {
  790. $gd = Db::name("good_zixun")->where(["spuCode"=>$cg['spuCode']])->find();
  791. }else {
  792. $gd =Db::name('good_basic')->where(['spuCode'=>$cg['spuCode']])->find();
  793. }
  794. if($gd==""){
  795. return error_show(1002,"未找到商品数据");
  796. }
  797. $diff_weight = isset($this->post['diff_weight']) && $this->post['diff_weight'] !=="" ? floatval($this->post['diff_weight']) :"";
  798. if($diff_weight===""){
  799. return error_show(1002,"参数diff_weight不能为空");
  800. }
  801. $orderCode = Db::name("order_num")->where([["cgdNo","=",$cg['cgdNo']],["status","=",1]])->findOrEmpty();
  802. $order=[];
  803. if(!empty($orderCode)){
  804. $order = Db::name("sale")->where("orderCode", '=', $orderCode['orderCode'])->findOrEmpty();
  805. if(empty($order)){
  806. return error_show(1002,"未找到确认单信息");
  807. }
  808. }
  809. $userCommon = \app\admin\common\User::getIns();
  810. $supp =$userCommon->handle("getCodeAndName",["code"=>[$cg['companyNo'],$order['customer_code']??"",$order['supplierNo']??""]]);
  811. $SuppArr= $supp['data']??[];
  812. $rid= $this->uid;
  813. $rname= $this->uname;
  814. $diff_price = round($diff_weight*$cg['gold_price']+$diff_weight*$cg['teach_fee'],2);
  815. //如果有未处理的工差单,不允许创建新的
  816. $wait_check = Db::name('purchease_diff')
  817. ->field('id')
  818. ->where(['apply_id'=>$rid,'status'=>1,'is_del'=>0,"cgdNo"=>$cgdNo])
  819. ->find();
  820. if(!empty($wait_check)) return error_show('1005','您还有未处理的工差单,请先处理');
  821. $data=[
  822. "cgdNo"=>$cgdNo,
  823. "good_code"=>$cg['spuCode'],
  824. "good_name"=>$cg['good_name'],
  825. "sale_price"=>$cg['good_price'],
  826. "good_weight"=>$cg['weight']*$cg['good_num'],
  827. "good_num"=>$cg['good_num'],
  828. "apply_id"=>$rid,
  829. "apply_name"=>$rname,
  830. "diff_weight"=>$diff_weight,
  831. "diff_price"=>$diff_price,
  832. "gold_price"=>$cg['good_price'],
  833. "supplierName"=>$cg['supplier_name'],
  834. "supplierNo"=>$cg['supplierNo'],
  835. "companyNo"=>$cg['companyNo'],
  836. "companyName"=>$SuppArr[$cg['companyNo']]??'',
  837. "status"=>1,
  838. "is_del"=>0,
  839. "addtime"=>date("Y-m-d H:i:s"),
  840. "updatetime"=>date("Y-m-d H:i:s")
  841. ];
  842. Db::startTrans();
  843. try{
  844. $item = Db::name("purchease_diff")->insert($data,true);
  845. if($item>0){
  846. $sto = ["order_code"=>$cgdNo,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  847. ActionLog::logAdd(['id'=>$rid,'nickname'=>$rname],$sto,"CGGCD",1,$data);
  848. $process=["order_code"=>$cgdNo,"order_id"=>$item,"order_status"=>1,"order_type"=>'CGGCD',"before_status"=>1,'holder_id'=>$rid];
  849. ProcessOrder::AddProcess(['id'=>$rid,'nickname'=>$rname],$process);
  850. $update_standing_book_data = [
  851. 'cgd_diff_id' => $item,//采购工差单id
  852. 'updatetime' => date("Y-m-d H:i:s")
  853. ];
  854. if(!empty($order)){
  855. $data=[
  856. "cgd_diffid"=>$item,
  857. "orderCode"=>$orderCode['orderCode'],
  858. "good_code"=>$cg['spuCode'],
  859. "good_name"=>$cg['good_name'],
  860. "diff_weight"=>$diff_weight,
  861. "diff_price"=>round($diff_weight*$order['gold_price']+$diff_weight*$order['cost_price'],2),
  862. "gold_price"=>$order['sale_price'],
  863. "customerName"=>$SuppArr[$order['customer_code']]??"",
  864. "customerNo"=>$order['customer_code'],
  865. "companyNo"=>$order['supplierNo'],
  866. "companyName"=>$SuppArr[$order['supplierNo']]??'',
  867. "supplierName"=>$cg['supplier_name'],
  868. "supplierNo"=>$cg['supplierNo'],
  869. "status"=>1,
  870. "addtime"=>date("Y-m-d H:i:s"),
  871. "updatetime"=>date("Y-m-d H:i:s")
  872. ];
  873. $cgd = Db::name('sale_diff')->insert($data,true);
  874. if($cgd==0){
  875. Db::rollback();
  876. return error_show(1002,"新建失败");
  877. }
  878. $sto = ["order_code"=>$orderCode['orderCode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  879. ActionLog::logAdd($this->post['token'],$sto,"XSGCD",1,$data);
  880. $process=["order_code"=>$orderCode['orderCode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'XSGCD',"before_status"=>1,'holder_id'=>$order['apply_id']];
  881. ProcessOrder::AddProcess($this->post['token'],$process);
  882. $update_standing_book_data['sale_diff_id']=$cgd;//工差单id
  883. }
  884. //维护台账记录
  885. Db::name('standing_book')
  886. ->where('cgdNo', $cgdNo)
  887. ->update($update_standing_book_data);
  888. Db::commit();
  889. return error_show(0,"新建成功");
  890. }else{
  891. Db::rollback();
  892. return error_show(1002,"新建失败");
  893. }
  894. }catch (\Exception $e){
  895. Db::rollback();
  896. return error_show(1002,$e->getMessage());
  897. }
  898. }
  899. public function difflist(){
  900. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  901. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  902. $where = [['a.is_del',"=",0]];
  903. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
  904. if($cgdNo!=""){
  905. $where[]=['a.cgdNo',"like", "%$cgdNo%"];
  906. }
  907. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  908. if($status!==""){
  909. $where[]=['a.status',"=",$status];
  910. }
  911. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  912. if ($start !="") {
  913. $where[]= ["a.addtime",'>=',$start];
  914. }
  915. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  916. if($end !=""){
  917. $where[]= ["a.addtime",'<=',$end];
  918. }
  919. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  920. if($apply_name!==""){
  921. $where[]=['a.apply_name',"like","%$apply_name%"];
  922. }
  923. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  924. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  925. $order_source = $order_source = isset($this->post['order_source']) && $this->post['order_source'] != "" ? intval($this->post['order_source']) : "";
  926. if ($order_source !== "") $where[] = ['c.order_source', "=", $order_source];
  927. $order_type = $order_source = isset($this->post['order_type']) && $this->post['order_type'] != "" ? intval($this->post['order_type']) : "";
  928. if ($order_type !== "") $where[] = ['c.order_type', "=", $order_type];
  929. // $role = $this->checkDataShare();
  930. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ['a.apply_id', 'in', $role[DataGroupModel::$type_全部]];
  931. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] != "" ? trim($this->post['relaComNo']) : "";
  932. if ($relaComNo != '') $where[] = ['a.companyNo', '=', $relaComNo];
  933. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
  934. if ($supplierNo != '') $where[] = ['b.supplierNo', '=', $supplierNo];
  935. if ($this->level == 2) {
  936. $role = $this->checkDataShare();
  937. $hand = resign_hand_user($this->uid, 0);
  938. if (!empty($role[DataGroupModel::$type_全部])) {
  939. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  940. $where[] = ['a.apply_id', 'in', $arr];
  941. }
  942. // if($relaComNo!='') $where[]=['b.companyNo','=', $relaComNo];
  943. }
  944. // if($this->level==3){
  945. // if($relaComNo!='') $where[]=['b.supplierNo','=', $relaComNo];
  946. // }
  947. $count = Db::name('purchease_diff')
  948. ->alias('a')
  949. ->leftJoin("good b","b.spuCode=a.good_code")
  950. ->leftJoin("purchease_order c","c.cgdNo=a.cgdNo")
  951. ->where($where)
  952. ->count();
  953. $total = ceil($count/$size);
  954. $page = $page >= $total ? $total : $page;
  955. $list = Db::name('purchease_diff')
  956. ->alias('a')
  957. ->leftJoin("good b","b.spuCode=a.good_code")
  958. ->leftJoin("purchease_order c","c.cgdNo=a.cgdNo")
  959. ->where($where)
  960. ->page($page,$size)
  961. ->order("a.addtime desc")
  962. ->field("a.*,b.cat_id,c.order_source,c.order_type")
  963. ->select()
  964. ->toArray();
  965. $all_createrid = array_column($list,'apply_id');
  966. $item = get_company_name_by_uid($all_createrid);
  967. $data=[];
  968. foreach ($list as $value){
  969. $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
  970. $value['company_name'] = $item[$value['apply_id']]??'';
  971. //是否具有编辑权限
  972. $value['is_allow_update'] = 0;
  973. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  974. $data[]=$value;
  975. }
  976. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  977. }
  978. public function diffinfo(){
  979. $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
  980. if($id==""){
  981. return error_show(1002,"参数id 不能为空");
  982. }
  983. $idinf = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  984. if($idinf==false){
  985. return error_show(1002,"未找到采购工差单数据");
  986. }
  987. $dn = Db::name('purchease_order')->where(['cgdNo'=>$idinf['cgdNo'],'is_del'=>0])->find();
  988. if (in_array($dn['order_type'], [3, 4])) {
  989. $goon = Db::name("good_zixun")->where(["spuCode"=>$dn['spuCode'],"is_del"=>0])->find();
  990. }else {
  991. $goon =Db::name('good_basic')->where(['spuCode'=>$dn['spuCode']])->find();
  992. }
  993. // $goon = Db::name('good')->where(['spuCode'=>$idinf['good_code']])->find();
  994. if(empty($goon)){
  995. return error_show(1002,"未找到商品数据");
  996. }
  997. $idinf['wsm_name'] ="";
  998. $idinf['wsm_supplier'] ="";
  999. $idinf['wsm_supplierNo'] ="";
  1000. $idinf['wsm_contactor'] ="";
  1001. $idinf['wsm_mobile'] ="";
  1002. $idinf['addr_code'] ="";
  1003. $idinf['wsm_addr'] ="";
  1004. if($dn['wsm_code']!=""){
  1005. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  1006. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  1007. ->where(["a.wsm_code"=>$dn['wsm_code']])->field("a.name as wsm_name,b.name,b.code,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code")->find();
  1008. $idinf['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  1009. $idinf['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  1010. $idinf['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  1011. $idinf['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  1012. $idinf['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  1013. $idinf['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  1014. $idinf['addr_cn'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  1015. $idinf['wsm_addr']=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  1016. }
  1017. $idinf['info'] =$dn;
  1018. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  1019. $idinf['can']=$int;
  1020. $idinf['order_type']=$dn['order_type'];
  1021. $idinf['order_source']=$dn['order_source'];
  1022. //取出销售的工差单信息
  1023. $diff = Db::name('sale_diff')->field('id,customer_remark,is_act,remark')->where('cgd_diffid',$id)->find();
  1024. $idinf['diff_customer_remark']=$diff['customer_remark'];
  1025. $idinf['diff_is_act']=$diff['is_act'];
  1026. $idinf['diff_remark']=$diff['remark'];
  1027. return app_show(0,"获取成功",$idinf);
  1028. }
  1029. public function diffstatu(){
  1030. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  1031. if($id==""){
  1032. return error_show(1002,"参数id不能为空");
  1033. }
  1034. $dio = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  1035. if(empty($dio)){
  1036. return error_show(1002,"工差订单未找到");
  1037. }
  1038. $cgd = Db::name("purchease_order")->where(['cgdNo'=>$dio['cgdNo']])->find();
  1039. if(empty($cgd)){
  1040. return error_show(1002,"采购单未找到");
  1041. }
  1042. $salediff=Db::name("sale_diff")->where(["cgd_diffid"=>$dio['id']])->find();
  1043. if($salediff==false){
  1044. return error_show(1002,"销售工差单未找到");
  1045. }
  1046. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  1047. if($status===""){
  1048. return error_show(1002,"参数status不能为空");
  1049. }
  1050. Db::startTrans();
  1051. try{
  1052. $temp = $dio['status'];
  1053. $dio['status']=$status;
  1054. $dio['updatetime']=date("Y-m-d H:i:s");
  1055. $item = Db::name("purchease_diff")->save($dio);
  1056. if ($item){
  1057. if($status==2){
  1058. $cgd['diff_fee'] =$dio['diff_price'];
  1059. $cgd['diff_weight'] =$dio['diff_weight'];
  1060. $cgd['updatetime'] =date("Y-m-d H:i:s");
  1061. $cgp=Db::name("purchease_order")->save($cgd);
  1062. if($cgp==false){
  1063. Db::rollback();
  1064. return error_show(1002,"采购单更新失败");
  1065. }
  1066. if($salediff['is_act']==1){
  1067. $saled=[
  1068. "diff_fee"=>$salediff['diff_price'],
  1069. "diff_weight"=>$salediff['diff_weight'],
  1070. "updatetime"=>date("Y-m-d H:i:s")
  1071. ];
  1072. $up =Db::name("sale")->where(["orderCode"=>$salediff['orderCode']])->save($saled);
  1073. if($up==false){
  1074. Db::rollback();
  1075. return error_show(1002,"销售单更新失败");
  1076. }
  1077. }
  1078. }
  1079. $sto = ["order_code" => $dio['cgdNo'], "status" => 1, "action_remark" => '', "action_type" => "create"];
  1080. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $sto, "CGGCD", $status, $this->post);
  1081. $process = ["order_code" => $dio['cgdNo'], "order_id" => $dio['id'], "order_status" => $status, "order_type" => 'CGGCD', "before_status" => 1, 'holder_id' => $dio['apply_id']];
  1082. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  1083. Db::commit();
  1084. return error_show(0,"更新成功");
  1085. }else{
  1086. Db::rollback();
  1087. return error_show(1002,"更新失败");
  1088. }
  1089. }catch (\Exception $e){
  1090. Db::rollback();
  1091. return error_show(1002,$e->getMessage()."|".$e->getLine());
  1092. }
  1093. }
  1094. //修改采购单的实时金价
  1095. public function editGoldPrice()
  1096. {
  1097. $param = $this->request->only(['cgdNo', 'gold_price'], 'post', 'trim');
  1098. $val = Validate::rule(['cgdNo|采购单号' => 'require', 'gold_price|实时金价' => 'require|float']);
  1099. if (!$val->check($param)) return error_show(1004, $val->getError());
  1100. //采购单
  1101. $data = Db::name("purchease_order")
  1102. ->field('id,demo_fee,good_num,weight,open_fee,teach_fee,pakge_fee,mark_fee,cert_fee,nake_fee,delivery_fee,status')
  1103. ->where(["cgdNo" => $param['cgdNo'], "is_del" => 0])
  1104. ->find();
  1105. if (empty($data)) return error_show(1004, "未找到该采购单数据");
  1106. if ($data['status'] != 0) return error_show(1005, '该状态下不允许修改实时金价');
  1107. $sale_price = round($data['demo_fee'] / $data['good_num'] + $data['open_fee'] / $data['good_num'] +$data['weight'] * $param["gold_price"] + $data['teach_fee'] * $data['weight'] +
  1108. $data['pakge_fee'] + $data['mark_fee'] + $data['cert_fee'] +$data['nake_fee'] + $data['delivery_fee'],2);
  1109. $total_price = round($data['good_num']*$sale_price,2);
  1110. $rs = Db::name("purchease_order")
  1111. ->where('id', $data['id'])
  1112. ->update(['updatetime' => date('Y-m-d H:i:s'), 'gold_price' => $param['gold_price'],"good_price"=>$sale_price,"total_fee"=>$total_price]);
  1113. return $rs ? app_show(0, '修改采购单实时金价成功') : error_show(1005, '修改采购单实时金价失败');
  1114. }
  1115. //采购单导出
  1116. public function exportCgdList()
  1117. {
  1118. $cgdNos = $this->request->post('cgdNos', [], 'trim');
  1119. if (empty($cgdNos)) return error_show(1004, '要导出的采购单编号不能为空');
  1120. $i = 1;
  1121. $send_type = [1 => '直接发货', 2 => '延时发货'];
  1122. $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
  1123. $list = Db::name("purchease_order")
  1124. ->alias('po')
  1125. ->field('"" as 序号,po.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,s.orderCode as 确认单号,s.addtime as 确认单时间,po.spuCode as 产品编号,po.good_name as 产品名称,"" as 规格,"" as 单位,po.supplier_name as 供应商名称,po.supplierNo as 供应商编号,po.nake_fee as 裸价,po.delivery_fee as 物流费,s.send_type as 发货方式,po.good_price as 采购单价,s.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,"" as 购买方公司,s.arrive_time as 到货时间,"" 税率,oa.addr 收货地址,oa.mobile 电话,oa.contactor 联系人,s.platform_order 平台订单编号,oa.receipt_quantity 地址发货数量, oa.addr_code,po.order_type,s.supplierNo')
  1126. ->whereIn('po.cgdNo', $cgdNos)
  1127. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  1128. ->leftJoin('sale s', 's.orderCode=on.orderCode AND s.is_del=0')
  1129. ->leftJoin('order_addr oa', 'oa.orderCode=on.orderCode AND oa.is_del=0')
  1130. // ->leftJoin('business b', 'b.companyNo=s.supplierNo AND b.is_del=0')
  1131. ->order("po.addtime desc")
  1132. ->withAttr('序号', function () use (&$i) {
  1133. return $i++;
  1134. })->withAttr('采购单状态', function ($val) use ($status) {
  1135. return isset($status[$val]) ? $status[$val] : '';
  1136. })->withAttr('发货方式', function ($val) use ($send_type) {
  1137. return isset($send_type[$val]) ? $send_type[$val] : '';
  1138. })->withAttr('发货方式', function ($val) use ($send_type) {
  1139. return isset($send_type[$val]) ? $send_type[$val] : '';
  1140. })
  1141. ->select()
  1142. ->toArray();
  1143. $all_supplierNo = array_column($list, 'supplierNo');
  1144. $userCommon = \app\admin\common\User::getIns();
  1145. $tmp = $userCommon->handle('getCodeAndName', ['code' => $all_supplierNo]);
  1146. foreach ($list as &$value) {
  1147. if (in_array($value['order_type'], [3, 4])) {
  1148. $temp = Db::name("good_zixun")
  1149. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  1150. ->field('id,specinfo,good_unit,tax')
  1151. ->find();
  1152. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  1153. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  1154. $speclist = [];
  1155. foreach ($specinfo as $val) {
  1156. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  1157. }
  1158. $value['税率'] = $temp['tax'] . '%';
  1159. } else {
  1160. $good_unit = Db::name('good_basic')
  1161. ->field('id,good_unit,tax')
  1162. ->where(['spuCode' => $value['产品编号']])
  1163. ->find();
  1164. $spec = Db::name("good_spec")
  1165. ->field('id,spec_id,spec_value_id')
  1166. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  1167. ->select()
  1168. ->toArray();
  1169. $speclist = [];
  1170. if (!empty($spec)) {
  1171. foreach ($spec as $val) {
  1172. $speclist[] = Db::name("specs")->where(["id" => $val['spec_id']])->value('spec_name', '') . ':' . Db::name("spec_value")->where(["id" => $val['spec_value_id']])->value('spec_value', '');
  1173. }
  1174. }
  1175. $value['税率'] = isset($good_unit['tax']) ? $good_unit['tax'] . '%' : '';
  1176. }
  1177. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  1178. $value['单位'] = isset($good_unit['good_unit']) ? Db::name('unit')->where(['id' => $good_unit['good_unit'], 'is_del' => 0])->value('unit', '') : '';
  1179. if (!empty($value['addr_code'])) {
  1180. $temp = explode(',', $value['addr_code']);
  1181. $temp_ = GetAddr(json_encode(['provice_code' => $temp[0], 'city_code' => $temp[1], 'area_code' => $temp[2]]));
  1182. $value['收货地址'] = $temp_ . $value['收货地址'];
  1183. }
  1184. $value['购买方公司'] = $tmp['data'][$value['supplierNo']] ?? '';
  1185. unset($value['addr_code']);
  1186. unset($value['order_type']);
  1187. unset($value['supplierNo']);
  1188. }
  1189. $headerArr = array_keys($list[0]);
  1190. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  1191. }
  1192. //获取采购单修改供应商的记录列表
  1193. public function getChangeSupplierList()
  1194. {
  1195. $param = $this->request->only(['cgdNo' => '', 'start' => '', 'end' => '', 'creater' => '', 'page' => 1, 'size' => 15, 'company_name' => ''], 'post', 'trim');
  1196. $where = [['a.is_del', '=', 0]];
  1197. if (!empty($param['cgdNo'])) $where[] = ['a.cgdNo', 'like', '%' . $param['cgdNo'] . '%'];
  1198. if (!empty($param['start'])) $where[] = ['a.addtime', '>=', $param['start']];
  1199. if (!empty($param['end'])) $where[] = ['a.addtime', '<=', $param['end'] . ' 23:59:59'];
  1200. if (!empty($param['creater'])) $where[] = ['a.creater', 'like', '%' . $param['creater'] . '%'];
  1201. if (!empty($param['company_name'])) $where[] = ["a.creater_id", 'in', get_company_item_user_by_name($param['company_name'])];
  1202. $count = Db::name('purchease_change_supplier_log')
  1203. ->alias('a')
  1204. ->where($where)
  1205. ->count('a.id');
  1206. $list = Db::name('purchease_change_supplier_log')
  1207. ->alias('a')
  1208. ->where($where)
  1209. ->order('a.id', 'desc')
  1210. ->page($param['page'], $param['size'])
  1211. ->select()
  1212. ->toArray();
  1213. $all_createrid = array_column($list,'createrid');
  1214. $item = get_company_name_by_uid($all_createrid);
  1215. foreach ($list as &$value){
  1216. $val['company_name']=$item[$value['createrid']]??'';
  1217. }
  1218. return app_show(0, '获取成功', ['list' => $list, 'count' => $count]);
  1219. }
  1220. // public function create(){
  1221. // $customer = isset($this->post['customer_code'])&&$this->post['customer_code']!="" ?trim($this->post['customer_code']):"";
  1222. // if($customer==""){
  1223. // return error_show(1004,"参数customer_code不能为空");
  1224. // }
  1225. // $supplier = isset($this->post['supplier_code'])&&$this->post['supplier_code']!="" ?trim($this->post['supplier_code']):"";
  1226. // if($supplier==""){
  1227. // return error_show(1004,"参数supplier_code不能为空");
  1228. // }
  1229. // $good_code = isset($this->post['good_code']) && $this->post['good_code']!=""?trim($this->post['good_code']):"";
  1230. // if($good_code==""){
  1231. // return error_show(1004,"参数good_code不能为空");
  1232. // }
  1233. // $good_num = isset($this->post['good_num'])&& $this->post['good_num']!==""? intval($this->post['good_num']):"";
  1234. // if($good_num===""){
  1235. // return error_show(1004,"参数good_code不能为空");
  1236. // }
  1237. // $file_url = isset($this->post['file_url'])&& $this->post['file_url']!==""? trim($this->post['file_url']):"";
  1238. // if($file_url===""){
  1239. // return error_show(1004,"参数file_url不能为空");
  1240. // }
  1241. // $mark =isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  1242. // if($mark==""){
  1243. // return error_show(1004,"参数remark不能为空");
  1244. // }
  1245. //
  1246. // $data=[
  1247. // ""
  1248. // ];
  1249. // }
  1250. }