Purch.php 63 KB

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