Sale.php 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\admin\model\ProcessOrder;
  5. use app\BaseController;
  6. use Exception;
  7. use think\App;
  8. use think\facade\Db;
  9. use app\admin\model\ActionLog;
  10. class Sale extends BaseController
  11. {
  12. public $post = "";
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->post = $this->request->post();
  17. }
  18. public function create()
  19. {
  20. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  21. $orderCode=makeNo("CX");
  22. $customer_code= isset($this->post['customer_code'])&& $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  23. if($customer_code==""){
  24. return error_show(1002,"客户customer_code不能为空");
  25. }
  26. $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  27. if($customer==false){
  28. return error_show(1004,"未找到客户数据");
  29. }
  30. $supplierNo= isset($this->post['supplierNo']) && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  31. if($supplierNo==""){
  32. return error_show(1002,"参数supplierNo不能为空");
  33. }
  34. $supplier=Db::name("business")->where(["companyNo"=>$supplierNo])->find();
  35. if($supplier==false){
  36. return error_show(1004,"未找到平台供应商数据");
  37. }
  38. $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"1";
  39. $order_type =isset($this->post['order_type']) && $this->post['order_type'] !=="" ?intval($this->post['order_type'])
  40. :"1";
  41. $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?intval($this->post['sendtype']):"";
  42. if($sendtype==''){
  43. return error_show(1004,"参数sendtype不能为空");
  44. }
  45. $platform_id =isset($this->post['platform_id']) && $this->post['platform_id'] !=="" ?intval($this->post['platform_id']):"";
  46. if($platform_id==''){
  47. return error_show(1004,"参数platform_id不能为空");
  48. }
  49. $zxinfoNo= isset($this->post['zxinfoNo'])&& $this->post['zxinfoNo'] !=="" ? trim($this->post['zxinfoNo']) :"";
  50. if($zxinfoNo=="" &&$order_type!==1){
  51. return error_show(1002,"参数 zxinfoNo 不能为空");
  52. }
  53. $platform_order =isset($this->post['platform_order']) && $this->post['platform_order'] !=="" ?trim($this->post['platform_order']):"";
  54. if($platform_order==''){
  55. return error_show(1004,"参数platform_order不能为空");
  56. }
  57. $good_code =isset($this->post['good_code']) && $this->post['good_code'] !=="" ?trim($this->post['good_code']):"";
  58. if($good_code==''){
  59. return error_show(1004,"参数good_code不能为空");
  60. }
  61. $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  62. if($good_num===''){
  63. return error_show(1004,"参数good_num不能为空");
  64. }
  65. if($order_type==3|| $order_type==4){
  66. $goodinfo = Db::name("consult_bids")->where(["spuCode"=>$good_code,"is_del"=>0])->find();
  67. if($goodinfo==false){
  68. return error_show(1004,"未找到商品数据");
  69. }
  70. $spuCode = $goodinfo['spuCode'];
  71. $skuCode = "";
  72. $is_stock=0;
  73. $cgd=[
  74. "supplierNo"=>$goodinfo['supplierNo'],
  75. "companyNo"=>$supplierNo,
  76. "spuCode"=>$good_code,
  77. "skuCode"=>$skuCode,
  78. "orderCode"=>$orderCode,
  79. "good_name"=>$goodinfo['good_name'],
  80. "sale_price"=>$goodinfo['sale_price'],
  81. "total_fee"=>$goodinfo['total_fee'],
  82. "pakge_fee"=>$goodinfo['pakge_fee'],
  83. "cert_fee"=>$goodinfo['cert_fee'],
  84. "open_fee"=>$goodinfo['open_fee'],
  85. "cost_fee"=>$goodinfo['cost_fee'],
  86. "mark_fee"=>$goodinfo['mark_fee'],
  87. "demo_fee"=>$goodinfo['demo_fee'],
  88. "good_num"=>$good_num,
  89. "good_type"=>$goodtype,
  90. "order_type"=>$order_type,
  91. ];
  92. }else{
  93. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  94. ->where(['a.skuCode' => $good_code])->find();
  95. if ($ct == false) {
  96. return error_show(1002, "未找到商品数据");
  97. }
  98. $goodinfo=$ct;
  99. $is_stock=$ct['is_stock'];
  100. $spuCode = $ct['spuCode'];
  101. $skuCode = $ct['skuCode'];
  102. $cgd=[
  103. "supplierNo"=>$ct['supplierNo'],
  104. "companyNo"=>$supplierNo,
  105. "orderCode"=>$orderCode,
  106. "spuCode"=>$ct['spuCode'],
  107. "skuCode"=>$ct['skuCode'],
  108. "good_name"=>$ct['good_name'],
  109. "sale_price"=>$ct['sale_price'],
  110. "total_fee"=>$ct['total_fee'],
  111. "pakge_fee"=>$ct['packing_fee'],
  112. "cert_fee"=>$ct['cert_fee'],
  113. "open_fee"=>$ct['open_fee'],
  114. "cost_fee"=>$ct['cost_fee'],
  115. "mark_fee"=>$ct['mark_fee'],
  116. "demo_fee"=>$ct['demo_fee'],
  117. "good_num"=>$good_num,
  118. "good_type"=>$goodtype,
  119. "order_type"=>$order_type,
  120. ];
  121. }
  122. $is_activity= isset($this->post['is_activity'])&&$this->post['is_activity']!=="" ?$this->post['is_activity']:"";
  123. if($order_type==1){
  124. if($is_activity===""){
  125. return error_show(1002, "参数is_activity不能为空");
  126. }
  127. }
  128. $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  129. $proofid= isset($this->post['proof_id'])&&$this->post['proof_id']===""?intval($this->post['proof_id']):"0";
  130. $apply_id =GetUserInfo($token);
  131. if(empty($apply_id)||$apply_id['code']!=0){
  132. return error_show(1002,"申请人数据不存在");
  133. }
  134. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  135. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  136. Db::startTrans();
  137. try {
  138. $data=[
  139. "orderCode"=>$orderCode,
  140. "good_code"=>$spuCode,
  141. "skuCode"=>$skuCode,
  142. "customer_code"=>$customer_code,
  143. "good_name"=>isset($goodinfo['good_name'])&&$goodinfo['good_name']!==''?$goodinfo['good_name']:'',
  144. "good_num"=>$good_num,
  145. "cat_id"=>$goodinfo['cat_id'],
  146. "apply_id"=>$rm,
  147. "apply_name"=>$ri,
  148. "origin_price"=>$goodinfo['total_fee'],
  149. "sale_price"=>$goodinfo['sale_price'],
  150. "post_fee"=>0,
  151. "status"=>0,
  152. "send_num"=>0,
  153. "wsend_num"=>$good_num,
  154. "send_status"=>1,
  155. "good_type"=>$goodtype,
  156. "send_type"=>$sendtype,
  157. "supplierNo"=>$supplierNo,
  158. "is_del"=>0,
  159. "zxNo"=>$zxinfoNo,
  160. "platform_order"=>$platform_order,
  161. "platform_id"=>$platform_id,
  162. "remark"=>$remark,
  163. "is_stock"=>$is_stock,
  164. "is_activity"=>$is_activity===""?0:$is_activity,
  165. "proof_id"=>$proofid,
  166. "order_type"=>$order_type,
  167. "addtime"=>date("Y-m-d H:i:s"),
  168. "updatetime"=>date("Y-m-d H:i:s"),
  169. 'total_price'=>round($goodinfo['sale_price']*$good_num,2),
  170. ];
  171. $datainfo = Db::name('sale')->insert($data,true);
  172. if($datainfo>0){
  173. if($is_stock==0){
  174. $bol=$this->createCgd($cgd);
  175. if($bol==false){
  176. Db::rollback();
  177. return error_show(1002,"咨询订单创建失败");
  178. }
  179. }
  180. if($order_type==3|| $order_type==4){
  181. $limt=[
  182. "spuCode"=>$goodinfo['spuCode'],
  183. "good_name"=>$goodinfo['good_name'],
  184. "brand_id"=>$goodinfo['brand_id'],
  185. "good_unit"=>$goodinfo['good_name'],
  186. "cat_id"=>$goodinfo['cat_id'],
  187. "good_type"=>0,
  188. "moq"=>1,
  189. "customized"=>$goodinfo['work_day'],
  190. "tax"=>$goodinfo['tax'],
  191. "supplierNo"=>$goodinfo["supplierNo"],
  192. "is_auth"=>0,
  193. "craft_desc"=>$goodinfo['good_name'],
  194. "good_remark"=>"",
  195. "good_img"=>$goodinfo['good_img'],
  196. "good_thumb_img"=>"",
  197. "good_info_img"=>"",
  198. "status"=>1,
  199. "is_del"=>0,
  200. "createrid"=>$rm,
  201. "creater"=>$ri,
  202. "addtime"=>date("Y-m-d H:i:s"),
  203. "updatetime"=>date("Y-m-d H:i:s")
  204. ];
  205. $good = Db::name("good_zixun")->insert($limt);
  206. if(!$good){
  207. Db::rollback();
  208. return error_show(1006,"咨询商品录入失败");
  209. }
  210. }
  211. Db::commit();
  212. return error_show(0,"咨询订单创建成功",["order_code"=>$orderCode]);
  213. }
  214. Db::rollback();
  215. return error_show(1002,"咨询订单创建失败");
  216. }catch(\Exception $e){
  217. Db::rollback();
  218. return error_show(1005,$e->getMessage());
  219. }
  220. }
  221. public function createCgd($data){
  222. $cgdCode = makeNo("CG");
  223. $supplier=Db::name("supplier")->where(["code"=>$data['supplierNo'],"is_del"=>0])->find();
  224. //echo Db::name("supplier")->getLastSql();
  225. if($supplier==false){
  226. return false;
  227. }
  228. $wsm= Db::name("warehouse_info")->where(["supplierNo"=>$data["supplierNo"],"companyNo"=>$data['companyNo'],"is_del"=>0])->find();
  229. if($wsm==false){
  230. $wsm_code = makeNo("WSM");
  231. $inwsm=[
  232. "wsm_code"=>$wsm_code,
  233. "name"=>$supplier['name'],
  234. "wsm_type"=>2,
  235. "supplierNo"=>$supplier['code'],
  236. "addr"=>"",
  237. "addrs_code"=>"",
  238. "contactor"=>0,
  239. "contactor_name"=>0,
  240. "mobile"=>"",
  241. "position"=>"",
  242. "companyNo"=>$data['companyNo'],
  243. "status"=>1,
  244. "is_del"=>0,
  245. "addtime"=>date("Y-m-d H:i:s"),
  246. "updatetime"=>date("Y-m-d H:i:s")
  247. ];
  248. $in = Db::name("warehouse_info")->insert($inwsm);
  249. if($in==false){
  250. return false;
  251. }
  252. }else{
  253. $wsm_code =$wsm['wsm_code'];
  254. }
  255. $cg =["cgdNo"=>$cgdCode,
  256. "bkcode"=>"",
  257. "wsm_code"=>$wsm_code,
  258. "cgder"=>$supplier['person'],
  259. "cgder_id"=>$supplier['personid'],
  260. "spuCode"=>$data['spuCode'],
  261. "skuCode"=>$data['skuCode'],
  262. "good_name"=>$data['good_name'],
  263. "good_num"=>$data['good_num'],
  264. "good_price"=>$data['sale_price'],
  265. "total_fee"=>$data['total_fee'],
  266. "pakge_fee"=>$data['pakge_fee'],
  267. "cert_fee"=>$data['cert_fee'],
  268. "open_fee"=>$data['open_fee'],
  269. "teach_fee"=>$data['cost_fee'],
  270. "mark_fee"=>$data['mark_fee'],
  271. "demo_fee"=>$data['demo_fee'],
  272. #"weight"=>$data['weight'],
  273. "diff_weight"=>"0",
  274. "diff_fee"=>"0",
  275. "gold_price"=>"0",
  276. "supplierNo"=>$data['supplierNo'],
  277. "supplier_name"=>$supplier['name'],
  278. "companyNo"=>$data['companyNo'],
  279. "send_status"=>1,
  280. "send_num"=>"0",
  281. "wsend_num"=>$data['good_num'],
  282. "remark"=>"",
  283. "status"=>0,
  284. "lasttime"=>date("Y-m-d H:i:s"),
  285. "is_del"=>0,
  286. "order_type"=>$data['order_type'],
  287. "good_type"=>$data['good_type'],
  288. "addtime"=>date("Y-m-d H:i:s"),
  289. "updatetime"=>date("Y-m-d H:i:s")
  290. ];
  291. $up =Db::name("purchease_order")->insert($cg);
  292. if($up){
  293. $rela=[
  294. "orderCode"=>$data['orderCode'],
  295. "cgdNo"=>$cgdCode,
  296. "spuCode"=>$data['spuCode'],
  297. "good_num"=>$data['good_num'],
  298. "wsend_num"=>$data['good_num'],
  299. "send_num"=>0,
  300. "wait_num"=>0,
  301. "status"=>1,
  302. "source"=>2
  303. ];
  304. $re =Db::name("order_num")->save($rela);
  305. if($re==false){
  306. return false;
  307. }else{
  308. return true;
  309. }
  310. }else{
  311. return false;
  312. }
  313. }
  314. public function addAddr(){
  315. $orderCode = isset($this->post['orderCode'])&&$this->post['orderCode']!=""? trim($this->post['orderCode']):"";
  316. $etid = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->find();
  317. if (empty($etid)) {
  318. return error_show(1002, "未找到数据");
  319. }
  320. $arrtime = isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
  321. if($arrtime==""){
  322. return error_show(1004,"参数arrtime不能为空");
  323. }
  324. $order =Db::name("order_num")->where(["orderCode"=>$orderCode,"status"=>1])->where([["wsend_num",">=",0]])
  325. ->select()->toArray();
  326. if(empty($order)){
  327. return error_show(1004,"未找到可以发货得采购单数据");
  328. }
  329. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  330. $apply_id =GetUserInfo($token);
  331. if(empty($apply_id)||$apply_id['code']!=0){
  332. return error_show(1002,"申请人数据不存在");
  333. }
  334. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  335. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  336. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  337. if($addrlist=="" || empty($addrlist) ||!is_array($addrlist)){
  338. return error_show(1004,"参数addrlist不能为空");
  339. }
  340. Db::startTrans();
  341. try{
  342. foreach ($addrlist as $value){
  343. $temp=[];
  344. $addrs=[];
  345. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  346. $addrs['provice_code'] = $value['addr_code'][0];
  347. $addrs['city_code'] = $value['addr_code'][1];
  348. $addrs['area_code'] = $value['addr_code'][2];
  349. $addr = json_encode($addrs);
  350. }else{
  351. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  352. }
  353. $temp['orderCode']=$orderCode;
  354. $temp['contactor']=$value['contactor'];
  355. $temp['mobile'] = $value['mobile'];
  356. $temp['addr'] = $value['addr'];
  357. $temp['addr_code']=$addr;
  358. $temp['customer_code'] =$etid['customer_code'];
  359. $temp['receipt_quantity']=$value['receipt_quantity'];
  360. $temp['post_fee'] =0;
  361. $temp['is_del'] =0;
  362. $temp['addtime'] =date("Y-m-d H:i:s");
  363. $temp['updatetime'] =date("Y-m-d H:i:s");
  364. $temp['arrive_time']=$arrtime;
  365. $vmp = Db::name('order_addr')->insert($temp,true);
  366. if($vmp>0){
  367. $outCode=makeNo("CK");
  368. $out=[
  369. "orderCode"=>$orderCode,
  370. "outCode"=>$outCode,
  371. "apply_id"=>$rm,
  372. "apply_name"=>$ri,
  373. "addrid"=>$vmp,
  374. "post_name"=>"",
  375. "post_code"=>"",
  376. "post_fee"=>0,
  377. "sendtime"=>date("Y-m-d H:i:s"),
  378. "send_num"=>$value['receipt_quantity'],
  379. "check_num"=>0,
  380. "error_num"=>0,
  381. "wsm_code"=>"",
  382. "order_type"=>$etid['order_type'],
  383. "status"=>0,
  384. "addtime"=>date("Y-m-d H:i:s"),
  385. "updatetime"=>date("Y-m-d H:i:s")
  386. ];
  387. $ou =Db::name("order_out")->insert($out);
  388. if($ou==false){
  389. Db::rollback();
  390. return error_show(1002,"发货地址添加创建失败");
  391. }
  392. $num = $value['receipt_quantity'];
  393. $orsend=[];
  394. foreach ($order as $val){
  395. $tep=[];$ornum=0;
  396. if($val['wsend_num']>=$num){
  397. $val['wsend_num']-=$num;
  398. $val['send_num']+=$num;
  399. $ornum =$num;
  400. $num=0;
  401. }else{
  402. $num-=$val['wsend_num'];
  403. $val['send_num']+=$val['wsend_num'];
  404. $ornum =$val['wsend_num'];
  405. $val['wsend_num']=0;
  406. }
  407. $or =Db::name("order_num")->save($val);
  408. if($or==false){
  409. Db::rollback();
  410. return error_show(1002,"发货地址添加创建失败");
  411. }
  412. $tep=[
  413. "cgdNo"=>$val['cgdNo'],
  414. "outCode"=>$outCode,
  415. "send_num"=>$ornum,
  416. "status"=>1,
  417. "addtime"=>date("Y-m-d H:i:s"),
  418. "updatetime"=>date("Y-m-d H:i:s")
  419. ];
  420. $sen=Db::name("order_send")->save($tep);
  421. if($sen==false){
  422. Db::rollback();
  423. return error_show(1002,"发货地址添加创建失败");
  424. }
  425. if($num==0){
  426. break;
  427. }
  428. }
  429. }else{
  430. Db::rollback();
  431. return error_show(1002,"发货地址添加创建失败");
  432. }
  433. }
  434. Db::commit();
  435. return app_show(0,"发货地址添加完成");
  436. }catch(\Exception $e){
  437. Db::rollback();
  438. return error_show(1002,$e->getMessage());
  439. }
  440. }
  441. public function consultOrder(){
  442. $bidNo =isset($this->post['bidNo'])&&$this->post['bidNo']!="" ? trim($this->post['bidNo']):"";
  443. if($bidNo==""){
  444. return error_show(1003,"参数bidNo不能为空");
  445. }
  446. $zxinfo = Db::name("consult_bid")->where(["bidNo"=>$bidNo,"is_del"=>0])->find();
  447. if($zxinfo==false){
  448. return error_show(1003,"未找到咨询单商品信息");
  449. }
  450. if($zxinfo['status']!=5){
  451. return error_show(1003,"咨询单状态有误无法转单");
  452. }
  453. $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->find();
  454. if($zx==false){
  455. return error_show(1003,"未找到咨询单信息");
  456. }
  457. if($zx['status']!=2){
  458. return error_show(1003,"咨询单任务未结束");
  459. }
  460. $zxorder =Db::name("consult_order")->where(["zxNo"=>$zx["zxNo"],"is_del"=>0])->find();
  461. if($zxorder==false){
  462. return error_show(1003,"未找到咨询单信息");
  463. }
  464. $good_num =isset($this->post['good_num'])&&$this->post['good_num']!="" ? intval($this->post['good_num']):"";
  465. if($good_num===""){
  466. return error_show(1003,"参数good_num不能为空");
  467. }
  468. $sendtype = isset($this->post['sendtype'])&&$this->post['sendtype']!="" ? intval($this->post['sendtype']):"";
  469. if($sendtype==""){
  470. return error_show(1003,"参数sendtype不能为空");
  471. }
  472. $orderCode = makeNo("QR");
  473. $spuCode = $zxinfo['spuCode'];
  474. $skuCode = "";
  475. $is_stock=0;
  476. $cgd=[
  477. "supplierNo"=>$zxinfo['supplierNo'],
  478. "companyNo"=>$zxorder['companyNo'],
  479. "spuCode"=>$spuCode,
  480. "skuCode"=>$skuCode,
  481. "orderCode"=>$orderCode,
  482. "good_name"=>$zxinfo['good_name'],
  483. "sale_price"=>$zxinfo['sale_price'],
  484. "total_fee"=>$zxinfo['total_fee'],
  485. "pakge_fee"=>$zxinfo['pakge_fee'],
  486. "cert_fee"=>$zxinfo['cert_fee'],
  487. "open_fee"=>$zxinfo['open_fee'],
  488. "cost_fee"=>$zxinfo['cost_fee'],
  489. "mark_fee"=>$zxinfo['mark_fee'],
  490. "demo_fee"=>$zxinfo['demo_fee'],
  491. "good_num"=>$good_num,
  492. "good_type"=>1,
  493. "is_diff"=>$zxinfo['is_diff'],
  494. "order_type"=>3,
  495. ];
  496. $token=isset($this->post['token'])&&$this->post['token']!=""? trim($this->post['token']):"";
  497. if($token==""){
  498. return error_show(102, "参数token不能为空");
  499. }
  500. $apply_id = GetUserInfo($token);
  501. if (empty($apply_id) || $apply_id['code'] != 0) {
  502. return error_show(102, "申请人数据不存在");
  503. }
  504. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  505. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  506. $va = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  507. if($sendtype==1){
  508. if ($va == "") {
  509. return error_show(1002, "参数order_addr不能为空");
  510. }
  511. }
  512. $arrtime=isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
  513. if($arrtime==""){
  514. return error_show(1002, "参数arrtime不能为空");
  515. }
  516. Db::startTrans();
  517. try{
  518. $data=[
  519. "orderCode"=>$orderCode,
  520. "good_code"=>$spuCode,
  521. "skuCode"=>$skuCode,
  522. "customer_code"=>$zx['khNo'],
  523. "good_name"=>isset($zxinfo['good_name'])&&$zxinfo['good_name']!==''?$zxinfo['good_name']:'',
  524. "good_num"=>$good_num,
  525. "cat_id"=>$zxinfo['cat_id'],
  526. "apply_id"=>$rm,
  527. "apply_name"=>$ri,
  528. "origin_price"=>$zxinfo['nake_fee'],
  529. "sale_price"=>$zxinfo['sale_price'],
  530. "post_fee"=>0,
  531. "status"=>0,
  532. "send_num"=>0,
  533. "wsend_num"=>$good_num,
  534. "send_status"=>1,
  535. "good_type"=>1,
  536. "send_type"=>$sendtype,
  537. "supplierNo"=>$zx['companyNo'],
  538. "is_del"=>0,
  539. "zxNo"=>$bidNo,
  540. "platform_order"=>"",
  541. "platform_id"=>0,
  542. "remark"=>"",
  543. "is_stock"=>0,
  544. "is_activity"=>0,
  545. "proof_id"=>0,
  546. "order_type"=>3,
  547. "addtime"=>date("Y-m-d H:i:s"),
  548. "updatetime"=>date("Y-m-d H:i:s"),
  549. 'total_price'=>round($zxinfo['sale_price']*$good_num,2),
  550. ];
  551. $datainfo = Db::name('sale')->insert($data, true);
  552. if($datainfo>0){
  553. $zxinfo['status']=6;
  554. $zxinfo['updatetime']=date("Y-m-d H:i:s");
  555. $sa=Db::name("consult_bids")->save($zxinfo);
  556. if($sa==false){
  557. Db::rollback();
  558. return error_show(1002,"咨询单修改失败");
  559. }
  560. $bol=$this->createCgd($cgd);
  561. if($bol==false){
  562. Db::rollback();
  563. return error_show(1002,"咨询订单创建失败");
  564. }
  565. $limt=[
  566. "spuCode"=>$zxinfo['spuCode'],
  567. "good_name"=>$zxinfo['good_name'],
  568. "brand_id"=>$zxinfo['brand_id'],
  569. "good_unit"=>$zxinfo['good_name'],
  570. "cat_id"=>$zxinfo['cat_id'],
  571. "good_type"=>0,
  572. "moq"=>1,
  573. "customized"=>$zxinfo['work_day'],
  574. "tax"=>$zxinfo['tax'],
  575. "supplierNo"=>$zxinfo["supplierNo"],
  576. "is_auth"=>0,
  577. "craft_desc"=>$zxinfo['good_name'],
  578. "good_remark"=>"",
  579. "good_img"=>$zxinfo['good_img'],
  580. "good_thumb_img"=>"",
  581. "good_info_img"=>"",
  582. "status"=>1,
  583. "is_del"=>0,
  584. "createrid"=>$rm,
  585. "creater"=>$ri,
  586. "addtime"=>date("Y-m-d H:i:s"),
  587. "updatetime"=>date("Y-m-d H:i:s")
  588. ];
  589. $good = Db::name("good_zixun")->insert($limt);
  590. if(!$good){
  591. Db::rollback();
  592. return error_show(1006,"咨询商品录入失败");
  593. }
  594. if(!empty($order_addr)){
  595. $order =Db::name("order_num")->where(["orderCode"=>$orderCode,"status"=>1])->where([["wsend_num",">=",0]])
  596. ->find();
  597. if($order==false){
  598. Db::rollback();
  599. return error_show(1006,"采购单关联数据未找到");
  600. }
  601. foreach ($order_addr as $value){
  602. $temp=[];
  603. $addrs=[];
  604. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  605. $addrs['provice_code'] = $value['addr_code'][0];
  606. $addrs['city_code'] = $value['addr_code'][1];
  607. $addrs['area_code'] = $value['addr_code'][2];
  608. $addr = json_encode($addrs);
  609. }else{
  610. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  611. }
  612. $temp['orderCode']=$orderCode;
  613. $temp['contactor']=$value['contactor'];
  614. $temp['mobile'] = $value['mobile'];
  615. $temp['addr'] = $value['addr'];
  616. $temp['addr_code']=$addr;
  617. $temp['customer_code'] =$zx['khNo'];
  618. $temp['receipt_quantity']=$value['receipt_quantity'];
  619. $temp['post_fee'] =0;
  620. $temp['is_del'] =0;
  621. $temp['addtime'] =date("Y-m-d H:i:s");
  622. $temp['updatetime'] =date("Y-m-d H:i:s");
  623. $temp['arrive_time']=$arrtime;
  624. $vmp = Db::name('order_addr')->insert($temp,true);
  625. if($vmp>0){
  626. $outCode=makeNo("CK");
  627. $out=[
  628. "orderCode"=>$orderCode,
  629. "outCode"=>$outCode,
  630. "apply_id"=>$rm,
  631. "apply_name"=>$ri,
  632. "addrid"=>$vmp,
  633. "post_name"=>"",
  634. "post_code"=>"",
  635. "post_fee"=>0,
  636. "sendtime"=>date("Y-m-d H:i:s"),
  637. "send_num"=>$value['receipt_quantity'],
  638. "check_num"=>0,
  639. "error_num"=>0,
  640. "wsm_code"=>"",
  641. "order_type"=>3,
  642. "status"=>0,
  643. "addtime"=>date("Y-m-d H:i:s"),
  644. "updatetime"=>date("Y-m-d H:i:s")
  645. ];
  646. $ou =Db::name("order_out")->insert($out);
  647. if($ou==false){
  648. Db::rollback();
  649. return error_show(1002,"发货地址添加创建失败");
  650. }
  651. $order['send_num']+=$value['receipt_quantity'];
  652. $order['wsend_num']-=$value['receipt_quantity'];
  653. if($order['wsend_num']<0){
  654. Db::rollback();
  655. return error_show(1002,"发货数量已超出总数");
  656. }
  657. $ups = Db::name("order_num")->save($order);
  658. if($ups){
  659. $tep=[
  660. "cgdNo"=>$order['cgdNo'],
  661. "outCode"=>$outCode,
  662. "send_num"=>$value['receipt_quantity'],
  663. "status"=>1,
  664. "addtime"=>date("Y-m-d H:i:s"),
  665. "updatetime"=>date("Y-m-d H:i:s")
  666. ];
  667. $sen=Db::name("order_send")->save($tep);
  668. if($sen==false){
  669. Db::rollback();
  670. return error_show(1002,"发货地址添加创建失败");
  671. }
  672. }
  673. }else{
  674. Db::rollback();
  675. return error_show(1002,"发货地址添加创建失败");
  676. }
  677. }
  678. }
  679. Db::commit();
  680. return error_show(0,"咨询订单创建成功",["order_code"=>$orderCode]);
  681. }
  682. Db::rollback();
  683. return error_show(1002,"咨询订单创建失败");
  684. }catch(\Exception $e){
  685. Db::rollback();
  686. return error_show(1003,$e->getMessage());
  687. }
  688. }
  689. /**
  690. * @return \think\response\Json|void
  691. * @throws \think\db\exception\DataNotFoundException
  692. * @throws \think\db\exception\DbException
  693. * @throws \think\db\exception\ModelNotFoundException
  694. */
  695. public function projectCreate(){
  696. $planNo =isset($this->post['planNo'])&&$this->post['planNo']!=""?trim($this->post['planNo']):"";
  697. if($planNo==""){
  698. return error_show(1004,"参数planNo不能为空");
  699. }
  700. $plan =Db::name("project_plan")->where(["planNo"=>$planNo,"is_del"=>0])->find();
  701. if($plan==false){
  702. return error_show(1004,"未找到方案信息");
  703. }
  704. $feedbackid = Db::name("project_plan_rela")->where(["planNo"=>$planNo,"is_del"=>0])->column("feedback_id");
  705. if($feedbackid==false){
  706. return error_show(1004,"未找到方案反馈信息");
  707. }
  708. $project = Db::name("project")->where(["projectNo"=>$plan['projectNo'],"is_del"=>0])->find();
  709. if($project==false){
  710. return error_show(1004,"未找到方案信息");
  711. }
  712. $arrtime=isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
  713. if($arrtime==""){
  714. return error_show(1002, "参数arrtime不能为空");
  715. }
  716. $planinfo =isset($this->post['planinfo'])&&!empty($this->post['planinfo']) ?$this->post['planinfo']:"";
  717. if($planinfo==""){
  718. return error_show(1002, "参数planinfo不能为空");
  719. }
  720. $token=isset($this->post['token'])&&$this->post['token']!=""? trim($this->post['token']):"";
  721. if($token==""){
  722. return error_show(102, "参数token不能为空");
  723. }
  724. $apply_id = GetUserInfo($token);
  725. if (empty($apply_id) || $apply_id['code'] != 0) {
  726. return error_show(102, "申请人数据不存在");
  727. }
  728. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  729. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  730. Db::startTrans();
  731. try {
  732. foreach ($planinfo as $value){
  733. $good=[];
  734. $feedback = Db::name("project_feedback")->where(["pgNo"=>$value['pgNo'],"projectNo"=>$plan['projectNo'], "is_del"=>0])->find();
  735. if($feedback==false){
  736. Db::rollback();
  737. return error_show(1002, "反馈数据未找到");
  738. }
  739. $good['spuCode'] = $feedback['spuCode'];
  740. $good['skuCode'] = $feedback['skuCode'];
  741. $good['good_num'] = $value['good_num'];
  742. $good['sendtype'] = $value['sendtype'];
  743. $good['sale_price']=$feedback['sale_price'];
  744. $good['companyNo']=$project['companyNo'];
  745. $good['customerNo']=$project['khNo'];
  746. $good['pgNo']=$feedback['pgNo'];
  747. $good['arrtime']=$arrtime;
  748. $good['apply_id']=$rm;
  749. $good['apply_name']=$ri;
  750. $good['sendtype']=$value['sendtype'];
  751. if($value['sendtype']==1){
  752. if(!isset($value['addrlist'])||empty($value['addrlist'])){
  753. Db::rollback();
  754. return error_show(1002, "订单地址不能为空");
  755. }
  756. $good['addrlist'] = $value['addrlist'];
  757. }
  758. if($feedback['data_source']==1){
  759. $feed=$this->project_good($good);
  760. }else{
  761. $feed=$this->project_zx($good);
  762. }
  763. if($feed==false){
  764. Db::rollback();
  765. return error_show(1002, "咨询订单生成失败");
  766. }
  767. $feedback['status']=2;
  768. $feedback['updatetime']=date("Y-m-d H:i:s");
  769. $fed =Db::name("project_feedback")->save($feedback);
  770. if($fed==false){
  771. Db::rollback();
  772. return error_show(1002, "项目订单生成失败");
  773. }
  774. }
  775. Db::commit();
  776. return app_show(0, "咨询订单生成成功");
  777. }catch (\Exception $e){
  778. Db::rollback();
  779. return error_show(1002, $e->getMessage());
  780. }
  781. }
  782. public function project_zx($data){
  783. $goodinfo = Db::name("consult_bids")->where(["spuCode"=>$data['spuCode'],"is_del"=>0])->find();
  784. if($goodinfo==false){
  785. return false;
  786. }
  787. $orderCode=makeNo("QR");
  788. $cgd=[
  789. "supplierNo"=>$goodinfo['supplierNo'],
  790. "companyNo"=>$data['companyNo'],
  791. "spuCode"=>$data['spuCode'],
  792. "skuCode"=>"",
  793. "orderCode"=>$orderCode,
  794. "good_name"=>$goodinfo['good_name'],
  795. "sale_price"=>$data['sale_price'],
  796. "total_fee"=>$goodinfo['total_fee'],
  797. "pakge_fee"=>$goodinfo['pakge_fee'],
  798. "cert_fee"=>$goodinfo['cert_fee'],
  799. "open_fee"=>$goodinfo['open_fee'],
  800. "cost_fee"=>$goodinfo['cost_fee'],
  801. "mark_fee"=>$goodinfo['mark_fee'],
  802. "demo_fee"=>$goodinfo['demo_fee'],
  803. "good_num"=>$data['good_num'],
  804. "good_type"=>1,
  805. "order_type"=>4,
  806. ];
  807. $datas=[
  808. "orderCode"=>$orderCode,
  809. "good_code"=>$data['spuCode'],
  810. "skuCode"=>"",
  811. "customer_code"=>$data['khNo'],
  812. "good_name"=>isset($goodinfo['good_name'])&&$goodinfo['good_name']!==''?$goodinfo['good_name']:'',
  813. "good_num"=>$data['good_num'],
  814. "cat_id"=>$goodinfo['cat_id'],
  815. "apply_id"=>$data['rm'],
  816. "apply_name"=>$data['ri'],
  817. "origin_price"=>$goodinfo['nake_fee'],
  818. "sale_price"=>$data['sale_price'],
  819. "post_fee"=>0,
  820. "status"=>0,
  821. "send_num"=>0,
  822. "wsend_num"=>$data['good_num'],
  823. "send_status"=>1,
  824. "good_type"=>1,
  825. "send_type"=>$data['sendtype'],
  826. "supplierNo"=>$data['companyNo'],
  827. "is_del"=>0,
  828. "zxNo"=>$data["pgNo"],
  829. "platform_order"=>"",
  830. "platform_id"=>0,
  831. "remark"=>"",
  832. "is_stock"=>$goodinfo['is_stock'],
  833. "is_activity"=>0,
  834. "proof_id"=>0,
  835. "order_type"=>4,
  836. "addtime"=>date("Y-m-d H:i:s"),
  837. "updatetime"=>date("Y-m-d H:i:s"),
  838. 'total_price'=>round($data['sale_price']*$data['good_num'],2),
  839. ];
  840. $datainfo = Db::name('sale')->insert($datas, true);
  841. if($datainfo>0){
  842. $bol = $this->createCgd($cgd);
  843. if ($bol == false) {
  844. return false;
  845. }
  846. $goodinfo['status']=6;
  847. $goodinfo['updatetime']=date("Y-m-d H:i:s");
  848. $sa=Db::name("consult_bids")->save($goodinfo);
  849. if($sa==false){
  850. return false;
  851. }
  852. if ($data['sendtype']==1&&!empty($data['addrlist'])) {
  853. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  854. ->find();
  855. if ($order == false) {
  856. return false;
  857. }
  858. foreach ($data['addrlist'] as $value) {
  859. $temp = [];
  860. $addrs = [];
  861. if ($value['addr_code'] !== '' && is_array($value['addr_code'])) {
  862. $addrs['provice_code'] = $value['addr_code'][0];
  863. $addrs['city_code'] = $value['addr_code'][1];
  864. $addrs['area_code'] = $value['addr_code'][2];
  865. $addr = json_encode($addrs);
  866. } else {
  867. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  868. }
  869. $temp['orderCode'] = $orderCode;
  870. $temp['contactor'] = $value['contactor'];
  871. $temp['mobile'] = $value['mobile'];
  872. $temp['addr'] = $value['addr'];
  873. $temp['addr_code'] = $addr;
  874. $temp['customer_code'] = $data['khNo'];
  875. $temp['receipt_quantity'] = $value['receipt_quantity'];
  876. $temp['post_fee'] = 0;
  877. $temp['is_del'] = 0;
  878. $temp['addtime'] = date("Y-m-d H:i:s");
  879. $temp['updatetime'] = date("Y-m-d H:i:s");
  880. $temp['arrive_time'] = $data['arrtime'];
  881. $vmp = Db::name('order_addr')->insert($temp, true);
  882. if ($vmp > 0) {
  883. $outCode = makeNo("CK");
  884. $out = [
  885. "orderCode" => $orderCode,
  886. "outCode" => $outCode,
  887. "apply_id"=>$data['rm'],
  888. "apply_name"=>$data['ri'],
  889. "addrid" => $vmp,
  890. "post_name" => "",
  891. "post_code" => "",
  892. "post_fee" => 0,
  893. "sendtime" => date("Y-m-d H:i:s"),
  894. "send_num" => $value['receipt_quantity'],
  895. "check_num" => 0,
  896. "error_num" => 0,
  897. "wsm_code" => "",
  898. "order_type" => 4,
  899. "status" => 0,
  900. "addtime" => date("Y-m-d H:i:s"),
  901. "updatetime" => date("Y-m-d H:i:s")
  902. ];
  903. $ou = Db::name("order_out")->insert($out);
  904. if ($ou == false) {
  905. return false;
  906. }
  907. $order['send_num'] += $value['receipt_quantity'];
  908. $order['wsend_num'] -= $value['receipt_quantity'];
  909. if ($order['wsend_num'] < 0) {
  910. return false;
  911. }
  912. $ups = Db::name("order_num")->save($order);
  913. if ($ups) {
  914. $tep = [
  915. "cgdNo" => $order['cgdNo'],
  916. "outCode" => $outCode,
  917. "send_num" => $value['receipt_quantity'],
  918. "status" => 1,
  919. "addtime" => date("Y-m-d H:i:s"),
  920. "updatetime" => date("Y-m-d H:i:s")
  921. ];
  922. $sen = Db::name("order_send")->save($tep);
  923. if ($sen == false) {
  924. return false;
  925. }
  926. }
  927. } else {
  928. return false;
  929. }
  930. }
  931. }
  932. }
  933. }
  934. public function project_good($data){
  935. $good=Db::name('good')->alias("a")->leftJoin("good_platform b","a.spuCode=b.spuCode")->where(["b
  936. .skuCode"=>$data['skuCode'],"a.is_del"=>0,"b.is_del"=>0])->find();
  937. if($good==false){
  938. return false;
  939. }
  940. $orderCode=makeNo("QR");
  941. $datas=[
  942. "orderCode"=>$orderCode,
  943. "good_code"=>$data['spuCode'],
  944. "skuCode"=>$data['skuCode'],
  945. "customer_code"=>$data['khNo'],
  946. "good_name"=>isset($good['good_name'])&&$good['good_name']!==''?$good['good_name']:'',
  947. "good_num"=>$data['good_num'],
  948. "cat_id"=>$good['cat_id'],
  949. "apply_id"=>$data['rm'],
  950. "apply_name"=>$data['ri'],
  951. "origin_price"=>$good['nake_fee'],
  952. "sale_price"=>$data['sale_price'],
  953. "post_fee"=>0,
  954. "status"=>0,
  955. "send_num"=>0,
  956. "wsend_num"=>$data['good_num'],
  957. "send_status"=>1,
  958. "good_type"=>1,
  959. "send_type"=>$data['sendtype'],
  960. "supplierNo"=>$data['companyNo'],
  961. "is_del"=>0,
  962. "zxNo"=>$data["pgNo"],
  963. "platform_order"=>"",
  964. "platform_id"=>0,
  965. "remark"=>"",
  966. "is_stock"=>$good['is_stock'],
  967. "is_activity"=>0,
  968. "proof_id"=>0,
  969. "order_type"=>$good['is_stock']==1?2:4,
  970. "addtime"=>date("Y-m-d H:i:s"),
  971. "updatetime"=>date("Y-m-d H:i:s"),
  972. 'total_price'=>round($data['sale_price']*$data['good_num'],2),
  973. ];
  974. $datainfo = Db::name('sale')->insert($datas, true);
  975. if($datainfo>0) {
  976. $cgd=[
  977. "supplierNo"=>$good['supplierNo'],
  978. "companyNo"=>$data['companyNo'],
  979. "orderCode"=>$orderCode,
  980. "spuCode"=>$good['spuCode'],
  981. "skuCode"=>$good['skuCode'],
  982. "good_name"=>$good['good_name'],
  983. "sale_price"=>$good['sale_price'],
  984. "total_fee"=>$good['total_fee'],
  985. "pakge_fee"=>$good['packing_fee'],
  986. "cert_fee"=>$good['cert_fee'],
  987. "open_fee"=>$good['open_fee'],
  988. "cost_fee"=>$good['cost_fee'],
  989. "mark_fee"=>$good['mark_fee'],
  990. "demo_fee"=>$good['demo_fee'],
  991. "good_num"=>$data['good_num'],
  992. "good_type"=>1,
  993. "order_type"=>2,
  994. ];
  995. if($good['is_stock']==0) {
  996. $bol = $this->createCgd($cgd);
  997. if ($bol == false) {
  998. return false;
  999. }
  1000. }
  1001. if ($data['sendtype']==1&&!empty($data['addrlist'])) {
  1002. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1003. ->find();
  1004. if ($order == false) {
  1005. return false;
  1006. }
  1007. foreach ($data['addrlist'] as $value) {
  1008. $temp = [];
  1009. $addrs = [];
  1010. if ($value['addr_code'] !== '' && is_array($value['addr_code'])) {
  1011. $addrs['provice_code'] = $value['addr_code'][0];
  1012. $addrs['city_code'] = $value['addr_code'][1];
  1013. $addrs['area_code'] = $value['addr_code'][2];
  1014. $addr = json_encode($addrs);
  1015. } else {
  1016. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1017. }
  1018. $temp['orderCode'] = $orderCode;
  1019. $temp['contactor'] = $value['contactor'];
  1020. $temp['mobile'] = $value['mobile'];
  1021. $temp['addr'] = $value['addr'];
  1022. $temp['addr_code'] = $addr;
  1023. $temp['customer_code'] = $data['khNo'];
  1024. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1025. $temp['post_fee'] = 0;
  1026. $temp['is_del'] = 0;
  1027. $temp['addtime'] = date("Y-m-d H:i:s");
  1028. $temp['updatetime'] = date("Y-m-d H:i:s");
  1029. $temp['arrive_time'] = $data['arrtime'];
  1030. $vmp = Db::name('order_addr')->insert($temp, true);
  1031. if ($vmp > 0) {
  1032. if($good['is_stock']!=1){
  1033. $outCode = makeNo("CK");
  1034. $out = [
  1035. "orderCode" => $orderCode,
  1036. "outCode" => $outCode,
  1037. "apply_id"=>$data['rm'],
  1038. "apply_name"=>$data['ri'],
  1039. "addrid" => $vmp,
  1040. "post_name" => "",
  1041. "post_code" => "",
  1042. "post_fee" => 0,
  1043. "sendtime" => date("Y-m-d H:i:s"),
  1044. "send_num" => $value['receipt_quantity'],
  1045. "check_num" => 0,
  1046. "error_num" => 0,
  1047. "wsm_code" => "",
  1048. "order_type" => 4,
  1049. "status" => 0,
  1050. "addtime" => date("Y-m-d H:i:s"),
  1051. "updatetime" => date("Y-m-d H:i:s")
  1052. ];
  1053. $ou = Db::name("order_out")->insert($out);
  1054. if ($ou == false) {
  1055. return false;
  1056. }
  1057. $order['send_num'] += $value['receipt_quantity'];
  1058. $order['wsend_num'] -= $value['receipt_quantity'];
  1059. if ($order['wsend_num'] < 0) {
  1060. return false;
  1061. }
  1062. $ups = Db::name("order_num")->save($order);
  1063. if ($ups) {
  1064. $tep = [
  1065. "cgdNo" => $order['cgdNo'],
  1066. "outCode" => $outCode,
  1067. "send_num" => $value['receipt_quantity'],
  1068. "status" => 1,
  1069. "addtime" => date("Y-m-d H:i:s"),
  1070. "updatetime" => date("Y-m-d H:i:s")
  1071. ];
  1072. $sen = Db::name("order_send")->save($tep);
  1073. if ($sen == false) {
  1074. return false;
  1075. }
  1076. }
  1077. }
  1078. } else {
  1079. return false;
  1080. }
  1081. }
  1082. }
  1083. }
  1084. return true;
  1085. }
  1086. public function list()
  1087. {
  1088. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  1089. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  1090. $where[] = ['is_del', "=", 0];
  1091. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  1092. if ($orderCode !== "") {
  1093. $where[] = ['orderCode', "like", "%$orderCode%"];
  1094. }
  1095. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name'])
  1096. : "";
  1097. if ($apply_name !== "") {
  1098. $where[] = ['apply_name', "like", "%$apply_name%"];
  1099. }
  1100. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name'])
  1101. : "";
  1102. if ($good_name !== "") {
  1103. $where[] = ['good_name', "like", "%$good_name%"];
  1104. }
  1105. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  1106. if ($good_code !== "") {
  1107. $where[] = ['good_code', "like", "%$good_code%"];
  1108. }
  1109. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  1110. if ($customer_code !== "") {
  1111. $where[] = ['customer_code', "like", "%$customer_code%"];
  1112. }
  1113. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  1114. if ($supplierNo !== "") {
  1115. $where[] = ['supplierNo', "like", "%$supplierNo%"];
  1116. }
  1117. $ordertype = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type'])
  1118. : "";
  1119. if ($ordertype !== "") {
  1120. $where[] = ['order_type', "=", $ordertype];
  1121. }
  1122. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  1123. if ($status !== "") {
  1124. $where[] = ['status', "=", $status];
  1125. }
  1126. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  1127. if ($start != "") {
  1128. $where[] = ["addtime", '>=', $start];
  1129. }
  1130. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  1131. if ($end != "") {
  1132. $where[] = ["addtime", '<=', $end];
  1133. }
  1134. $count = Db::name('sale')->where($where)->count();
  1135. $total = ceil($count / $size);
  1136. $page = $page >= $total ? $total : $page;
  1137. $list = Db::name('sale')->where($where)->order("addtime desc")->page($page, $size)->select();
  1138. $data = [];
  1139. foreach ($list as $value) {
  1140. if($value['order_type']==3|| $value['order_type']==4){
  1141. $var = Db::name("good_zixun")->where(["spuCode"=>$value['good_code'],"is_del"=>0])->find();
  1142. }else {
  1143. $var = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1144. ->where(['a.skuCode' => $value['sku_code']])->find();
  1145. }
  1146. $value['can'] = isset($var['cat_id']) && $var['cat_id'] != 0 ? made($var['cat_id']) : [];
  1147. $value['supplierName'] = '';
  1148. if ($value['supplierNo'] != "") {
  1149. $supplier = Db::name("business")->where(["companyNo" => $value['supplierNo']])->find();
  1150. $value['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  1151. }
  1152. $value['customerName'] = '';
  1153. if ($value['customer_code'] != "") {
  1154. $supplier = Db::name("customer_info")->where(["companyNo" => $value['customer_code']])->find();
  1155. $value['customerName'] = isset($supplier['companyName']) ? $supplier['companyName'] : "";
  1156. }
  1157. $data[] = $value;
  1158. }
  1159. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  1160. }
  1161. public function edit()
  1162. {
  1163. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  1164. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  1165. if ($id == "") {
  1166. return error_show(1002, "参数id不能为空");
  1167. }
  1168. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  1169. if (empty($etid)) {
  1170. return error_show(1002, "未找到数据");
  1171. }
  1172. // $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  1173. // if($orderCode==""){
  1174. // return error_show(1002,"销售单code不能为空");
  1175. // }
  1176. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  1177. if ($good_code == "") {
  1178. return error_show(1002, "商品code不能为空");
  1179. }
  1180. $ct = Db::name('good')->alias('a')->join('good_type b', 'b.good_code=a.good_code', 'left')
  1181. ->where(['b.type_code' => $good_code])->find();
  1182. if ($ct == "") {
  1183. return error_show(1002, "未找到商品数据");
  1184. }
  1185. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  1186. if ($customer_code == "") {
  1187. return error_show(1002, "客户code不能为空");
  1188. }
  1189. $customer = Db::name("customer_info")->where(["companyNo" => $customer_code])->find();
  1190. if ($customer == false) {
  1191. return error_show(1004, "未找到客户数据");
  1192. }
  1193. $supplierNo = $this->post['supplierNo'] && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  1194. if ($supplierNo == "") {
  1195. return error_show(1002, "参数supplierNo不能为空");
  1196. }
  1197. $supplier = Db::name("supplier")->where(["code" => $supplierNo])->find();
  1198. if ($supplier == false) {
  1199. return error_show(1004, "未找到平台供应商数据");
  1200. }
  1201. $apply_id = GetUserInfo($token);
  1202. if (empty($apply_id) || $apply_id['code'] != 0) {
  1203. return error_show(1002, "申请人数据不存在");
  1204. }
  1205. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1206. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1207. // $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim
  1208. //($this->post['good_name']) : "";
  1209. // $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval
  1210. //($this->post['good_num']) : "";
  1211. // $origin_price = isset($this->post['origin_price']) && $this->post['origin_price'] !== "" ? intval
  1212. // ($this->post['origin_price']) : "";
  1213. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] !== "" ? trim($this->post['sale_price']) : "";
  1214. // $total_price = isset($this->post['total_price']) && $this->post['total_price'] !== "" ? trim($this->post['total_price']) : "";
  1215. // $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? trim($this->post['post_fee']) : "";
  1216. // $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  1217. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  1218. if ($vr == "") {
  1219. return error_show(1002, "参数order_addr不能为空");
  1220. }
  1221. $vi = isset($this->post['good_stock']) && $this->post['good_stock'] !== "" ? $this->post['good_stock'] : "";
  1222. if ($vi == "") {
  1223. return error_show(1002, "参数good_stock不能为空");
  1224. }
  1225. $good_num = intval(array_sum(array_column($vi, "num")));
  1226. Db::startTrans();
  1227. try {
  1228. $datn = [
  1229. "id" => $id,
  1230. "good_code" => $good_code,
  1231. "customer_code" => $customer_code,
  1232. "good_num" => $good_num,
  1233. "supplierNo" => $supplierNo,
  1234. "apply_id" => $rm,
  1235. "apply_name" => $ri,
  1236. "is_del" => 0,
  1237. "updatetime" => date("Y-m-d H:i:s")
  1238. ];
  1239. $datninfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($datn);
  1240. if ($datninfo > 0) {
  1241. $stn = ["order_code"=>$etid['orderCode'],"status"=>$etid['status'],"action_remark"=>'',"action_type"=>"edit"];
  1242. ActionLog::logAdd($this->post['token'],$stn,"XSQRD",$etid['status'],$datn);
  1243. $order=["order_type"=>'XSQRD',"order_code"=>$etid['orderCode'],"order_id"=>$id,"order_status"=>$etid['status']];
  1244. ProcessOrder::AddProcess($this->post['token'],$order);
  1245. foreach ($vi as $ion) {
  1246. $iten = [];
  1247. isset($ion['id']) && $ion['id'] !== "" ? $iten['id'] = $ion['id'] : '';
  1248. $iten['orderCode'] = $etid['orderCode'];
  1249. $iten['wsm_code'] = $ion['wsm_code'];
  1250. $iten['stock_id'] = 0;
  1251. $iten['num'] = $ion['num'];
  1252. $iten['status'] = 1;
  1253. $iten['sale_price'] = $ct['original_price'];
  1254. isset($ion['id']) && $ion['id'] !== "" ? '' : $iten['addtime'] = date("Y-m-d H:i:s");
  1255. $iten['updatetime'] = date("Y-m-d H:i:s");
  1256. $vp = Db::name('sale_info')->save($iten);
  1257. if ($vp == false)
  1258. {
  1259. Db::rollback();
  1260. return error_show(1002, "更新失败");
  1261. }
  1262. }
  1263. foreach ($vr as $value) {
  1264. $temp = [];
  1265. isset($value['id']) && $value['id'] !== "" ? $temp['id'] = $value['id'] : '';
  1266. $temp['orderCode'] = $etid['orderCode'];
  1267. $temp['contactor'] = $value['contactor'];
  1268. $temp['mobile'] = $value['mobile'];
  1269. $temp['addr'] = $value['addr'];
  1270. $temp['addr_code'] = $value['addr_code'];
  1271. $temp['customer_code'] = $customer_code;
  1272. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1273. $temp['post_fee'] = 0;
  1274. $temp['is_del'] = $value['is_del'];
  1275. isset($value['id']) && $value['id'] !== "" ? '' : $temp['addtime'] = date("Y-m-d H:i:s");
  1276. $temp['updatetime'] = date("Y-m-d H:i:s");
  1277. $temp['arrive_time'] = date("Y-m-d H:i:s");
  1278. $dat = Db::name('order_addr')->save($temp);
  1279. if ($dat == false) {
  1280. Db::rollback();
  1281. return error_show(1002, "更新失败");
  1282. }
  1283. }
  1284. Db::commit();
  1285. return error_show(0, "更新成功");
  1286. }
  1287. Db::rollback();
  1288. return error_show(1002, "更新失败");
  1289. } catch (Exception $e) {
  1290. Db::rollback();
  1291. return error_show(1005, $e->getMessage());
  1292. }
  1293. }
  1294. public function info()
  1295. {
  1296. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  1297. if ($id == "") {
  1298. return error_show(1002, "参数id不能为空");
  1299. }
  1300. //->field('good_code,good_name,good_num,customer_code,orderCode')
  1301. $einfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  1302. if (empty($einfo)) {
  1303. return error_show(1002, "未找到销售订单数据");
  1304. }
  1305. $info = Db::name('sale_diff')->where(['orderCode' => $einfo['orderCode']])->select();
  1306. //$de = Db::name('order_out')->where(['orderCode' => $einfo['orderCode']])->select();
  1307. $dm = Db::name('order_return')->where(['orderCode' => $einfo['orderCode']])->select();
  1308. $dk = Db::name('order_back')->where(['orderCode' => $einfo['orderCode']])->select();
  1309. $sa = Db::name('sale_return')->where(['orderCode' => $einfo['orderCode']])->select();
  1310. if($einfo['order_type']==3|| $einfo['order_type']==4){
  1311. $goon = Db::name("good_zixun")->where(["spuCode"=>$einfo['good_code'],"is_del"=>0])->find();
  1312. }else {
  1313. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1314. ->where(['a.skuCode' => $einfo['sku_code']])->find();
  1315. }
  1316. if ($goon==false) {
  1317. return error_show(1003, "未找到商品数据");
  1318. }
  1319. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  1320. $in = Db::name('customer_info')->where(['companyNo' => $einfo['customer_code']])->field('companyName')->find();
  1321. // ->field('addr,contactor,mobile,receipt_quantity,arrive_time')
  1322. $einfo['supplierName'] = '';
  1323. if ($einfo['supplierNo'] != "") {
  1324. $supplier = Db::name("bussiness")->where(["companyNo" => $einfo['supplierNo']])->find();
  1325. $einfo['supplierName'] = isset($supplier['company']) ? $supplier['company'] : "";
  1326. }
  1327. $fo = Db::name('order_addr')->where(['orderCode' => $einfo['orderCode'], 'is_del' => 0])->select();
  1328. $bum = Db::name('order_out')->where(['orderCode' => $einfo['orderCode']])->select();
  1329. $ins = [];
  1330. if (!empty($bum)) {
  1331. foreach ($bum as $stn) {
  1332. $so = Db::name('order_addr')->where(['id' => $stn['addrid']])->find();
  1333. $addr = GetAddr($so['addr_code']);
  1334. $stn['addr_info'] = $addr;
  1335. $stn['add_code'] = json_decode($so['addr_code'], true);
  1336. $stn['addr'] = $so['addr'];
  1337. $stn['contact'] = $so['contactor'];
  1338. $stn['mobile'] = $so['mobile'];
  1339. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  1340. ->where(["a.wsm_code" => $stn['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  1341. $stn['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  1342. $stn['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  1343. $stn['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  1344. $ins[] = $stn;
  1345. }
  1346. }
  1347. $addrs = [];
  1348. if (!empty($fo)) {
  1349. foreach ($fo as $value) {
  1350. $value['addr_info'] = "";
  1351. $value['send_num'] = 0;
  1352. if ($value['addr_code'] != "") {
  1353. $addr = GetAddr($value['addr_code']);
  1354. $value['addr_info'] = $addr;
  1355. $value['addr_code'] = json_decode($value['addr_code'], true);
  1356. $send = Db::name("order_out")->where(['addrid' => $value['id'], 'orderCode' => $einfo['orderCode']])->sum("send_num");
  1357. $value['send_num'] = $send ?? 0;
  1358. }
  1359. $value['wsend_num'] = $value['receipt_quantity'] - $value['send_num'];
  1360. $addrs[] = $value;
  1361. }
  1362. }
  1363. //->field('num,sale_price,wsm_code,stock_id')
  1364. $st = Db::name('sale_info')->where(['orderCode' => $einfo['orderCode']])->select();
  1365. $data = [];
  1366. if (!empty($fo)) {
  1367. foreach ($st as $value) {
  1368. $value['wsm_name'] = "";
  1369. $value['wsm_supplier'] = "";
  1370. $value['wsm_supplierNo'] = "";
  1371. $value['usable_stock'] = 0;
  1372. $value['send_num'] = 0;
  1373. if ($value['wsm_code'] != "") {
  1374. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  1375. ->where(["a.wsm_code" => $value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  1376. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  1377. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  1378. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  1379. $dn = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $einfo['good_code']])->find();
  1380. $value['usable_stock'] = isset($dn['usable_stock']) ? $dn['usable_stock'] : "0";
  1381. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $einfo['orderCode']])->sum("send_num");
  1382. $value['send_num'] = $send ?? 0;
  1383. }
  1384. $value['wsend_num'] = $value['num'] - $value['send_num'];
  1385. $data[] = $value;
  1386. }
  1387. }
  1388. $einfo['info'] = $data;
  1389. $einfo['companyName'] = isset($in['companyName']) ? $in['companyName'] : "";
  1390. $einfo['addrs'] = $addrs;
  1391. $einfo['orderout'] = $ins;
  1392. $einfo['can'] = $int;
  1393. $einfo['salediff'] = $info;
  1394. //$einfo['orderout'] = $de;
  1395. $einfo['orderreturn'] = $dm;
  1396. $einfo['orderback'] = $dk;
  1397. $einfo['salereturn'] = $sa;
  1398. if (empty($einfo)) {
  1399. return error_show(1002, "未找到销售订单数据");
  1400. } else {
  1401. return app_show(0, "获取成功", $einfo);
  1402. }
  1403. }
  1404. public function del()
  1405. {
  1406. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  1407. if ($id === "") {
  1408. return error_show(1002, "参数id不能为空");
  1409. }
  1410. $ed = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  1411. if (empty($ed)) {
  1412. return error_show(1002, "未找到销售订单数据");
  1413. }
  1414. $str = Db::name('sale')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  1415. if ($str) {
  1416. $stc = ["order_code"=>$id,"status"=>$ed['status'],"action_remark"=>'',"action_type"=>"delete"];
  1417. ActionLog::logAdd($this->post['token'],$stc,"XSQRD",$ed['status'],['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  1418. $order=["order_type"=>'XSQRD',"order_code"=>$ed['orderCode'],"order_id"=>$id,"order_status"=>$ed['status']];
  1419. ProcessOrder::workdel($order);
  1420. return error_show(0, "删除成功");
  1421. } else {
  1422. return error_show(1002, "删除失败");
  1423. }
  1424. }
  1425. public function status()
  1426. {
  1427. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  1428. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  1429. if ($id == "") {
  1430. return error_show(1002, "参数id不能为空");
  1431. }
  1432. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  1433. if ($status == "") {
  1434. return error_show(1002, "订单状态不能为空");
  1435. }
  1436. $dio = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  1437. if (empty($dio)) {
  1438. return error_show(1002, "销售单信息未找到");
  1439. }
  1440. if ($status == 1 && $dio['order_type'] == 2) {
  1441. $zxinfo = Db::name("consult")->where(["zxNo" => $dio['zxNo'], "is_del" => 0])->find();
  1442. if ($zxinfo == false) {
  1443. return error_show(1004, "未找到咨询单数据");
  1444. }
  1445. $gold['price'] = 0;
  1446. if ($zxinfo['zx_type'] == 2) {
  1447. if ($zxinfo['metals'] == "") {
  1448. return error_show(1004, "未找到咨询单贵金属类型数据");
  1449. }
  1450. $metals = $zxinfo['metals'] == '18K' ? 1 : ($zxinfo['metals'] == '24K' ? 2 : 3);
  1451. $gold = Db::name("gold_price")->where(["type" => $metals])->order("addtime desc")->find();
  1452. if (empty($gold)) {
  1453. return error_show(1004, "未找到咨询单贵金属类型数据");
  1454. }
  1455. $dio['gold_price'] = $gold['price'];
  1456. }
  1457. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $zxinfo['gysNo'], "wsm_type" => 2])->find();
  1458. if (empty($wsm)) {
  1459. return error_show(1002, "供应商仓库未找到");
  1460. }
  1461. }
  1462. $apply_id = GetUserInfo($token);
  1463. if (empty($apply_id) || $apply_id['code'] != 0) {
  1464. return error_show(1002, "申请人数据不存在");
  1465. }
  1466. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1467. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1468. Db::startTrans();
  1469. try {
  1470. $vmp =$dio['status'];
  1471. $dio['status'] = $status;
  1472. $dio['updatetime'] = date("Y-m-d H:i:s");
  1473. $st = Db::name('sale')->save($dio);
  1474. if ($st) {
  1475. $stn = ["order_code"=>$dio['orderCode'],"status"=>$vmp,"action_remark"=>'',"action_type"=>"status"];
  1476. ActionLog::logAdd($this->post['token'],$stn, $dio['order_type'] == 2?"ZXQRD":"XSQRD",$dio['status'],$dio);
  1477. $order=["order_type"=>$dio['order_type'] == 2?"ZXQRD":"XSQRD","order_code"=>$dio['orderCode'],"order_id"=>$id,"order_status"=>$dio['status']];
  1478. ProcessOrder::AddProcess($this->post['token'],$order);
  1479. if ($status == 3 && $dio['order_type'] == 1) {
  1480. $dn = Db::name('sale_info')->where(['orderCode' => $st['orderCode']])->select();
  1481. foreach ($dn as $value) {
  1482. $dm = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $st['good_code']])->find();
  1483. if ($dm == "") {
  1484. Db::rollback();
  1485. return error_show(1003, "未找到商品数据");
  1486. }
  1487. if ($value['num'] > $dm['usable_stock']) {
  1488. Db::rollback();
  1489. return error_show(1002, "超出库存数量");
  1490. }
  1491. $dm['usable_stock'] -= $value['num'];
  1492. $dm['wait_out_stock'] += $value['num'];
  1493. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code']])->sum("send_num");
  1494. $value['send_num'] = $send ?? 0;
  1495. $value['wsend_num'] = $value['num'] - $value['send_num'];
  1496. $Db = Db::name('good_stock')->update($dm);
  1497. $Db['updatetime'] = date('Y-m-d H:i:s');
  1498. if ($Db == false) {
  1499. Db::rollback();
  1500. return error_show(1002, "状态更新失败");
  1501. }
  1502. }
  1503. }
  1504. if ($status == 1 && $dio['order_type'] == 2) {
  1505. $cgd = makeNo("CG");
  1506. $clll = [
  1507. "cgdNo" => $cgd,
  1508. "bkcode" => $dio['orderCode'],
  1509. "wsm_code" => $wsm['wsm_code'],
  1510. "cgder_id" => "0",
  1511. "cgder" => $zxinfo['saler'],
  1512. "good_code" => "GD-" . $zxinfo['cpNo'],
  1513. "good_name" => $zxinfo['cpName'],
  1514. "good_type_code" => $zxinfo['cpNo'],
  1515. "good_num" => $dio['good_num'],
  1516. "good_price" => $zxinfo['total_fee'],
  1517. "total_fee" => round($zxinfo['total_fee'] * $dio['good_num'], 2),
  1518. "pakge_fee" => $zxinfo['package_fee'],
  1519. "cert_fee" => $zxinfo['cert_fee'],
  1520. "open_fee" => $zxinfo['open_fee'],
  1521. "delivery_fee" => $zxinfo['delivery_fee'],
  1522. "mark_fee" => $zxinfo['mark_fee'],
  1523. "teach_fee" => $zxinfo['cost_fee'],
  1524. "demo_fee" => $zxinfo['demo_fee'],
  1525. "nake_fee" => $zxinfo['bare_fee'],
  1526. "weight" => isset($zxinfo['weight']) ? $zxinfo['weight'] : 0,
  1527. "supplierNo" => $zxinfo['gysNo'],
  1528. "supplier_name" => $zxinfo['gysname'],
  1529. "gold_price" => $gold['price'],
  1530. "send_num" => 0,
  1531. "wsend_num" => $dio['good_num'],
  1532. "remark" => '',
  1533. "lasttime" => date("Y-m-d H:i:s"),
  1534. "is_del" => 0,
  1535. "status" => $dio['send_type'] == 1 ? 3 : 0,
  1536. "order_type" => 2,
  1537. "addtime" => date("Y-m-d H:i:s"),
  1538. "updatetime" => date("Y-m-d H:i:s")
  1539. ];
  1540. $cgin = Db::name("purchease_order")->insert($clll);
  1541. // var_dump(Db::name("purchease_order")->getLastSql(),$clll);
  1542. if ($cgin == false) {
  1543. $stb = ["order_code"=>$cgd,"status"=> $clll['status'],"action_remark"=>'',"action_type"=>"create"];
  1544. ActionLog::logAdd($this->post['token'],$stb,"xsd", $clll['status'],$stb);
  1545. Db::rollback();
  1546. return error_show(1002, "咨询采购单创建商品失败");
  1547. }
  1548. if ($dio['send_type'] == 1) {
  1549. $addr = Db::name('order_addr')->where(["orderCode" => $dio['orderCode'], 'is_del' => 0])->select();
  1550. foreach ($addr as $value) {
  1551. $outCode = makeNo("CK");
  1552. $data = [
  1553. "wsm_code" => $wsm['wsm_code'],
  1554. "orderCode" => $dio['orderCode'],
  1555. "outCode" => $outCode,
  1556. "order_type" => $dio['order_type'],
  1557. "apply_id" => $rm,
  1558. "apply_name" => $ri,
  1559. "addrid" => $value['id'],
  1560. "post_name" => '',
  1561. "post_code" => '',
  1562. "post_fee" => '',
  1563. "sendtime" => date("Y-m-d H:i:s"),
  1564. "send_num" => $value['receipt_quantity'],
  1565. "check_num" => 0,
  1566. "error_num" => 0,
  1567. "status" => 0,
  1568. "addtime" => date("Y-m-d H:i:s"),
  1569. "updatetime" => date("Y-m-d H:i:s")
  1570. ];
  1571. $datainfo = Db::name('order_out')->insert($data);
  1572. if (!$datainfo) {
  1573. $sti = ["order_code"=>$dio['orderCode'],"status"=> 0,"action_remark"=>'',"action_type"=>"create"];
  1574. ActionLog::logAdd($this->post['token'],$sti,"xsd",0,$sti);
  1575. Db::rollback();
  1576. return error_show(1003, "创建失败");
  1577. }
  1578. }
  1579. $wsm_in_code = makeNo("IC");
  1580. $orin = [
  1581. "wsm_in_code" => $wsm_in_code,
  1582. "cgdNo" => $cgd,
  1583. "wsm_code" => $wsm['wsm_code'],
  1584. "wsm_reaper" => "",
  1585. "send_num" => $dio['good_num'],
  1586. "post_company" => '',
  1587. "post_code" => '',
  1588. "post_fee" => '',
  1589. "sendtime" => date("Y-m-d H:i:s"),
  1590. "apply_id" => $rm,
  1591. "apply_name" => $ri,
  1592. "wait_num" => 0,
  1593. "status" => 4,
  1594. "addtime" => date("Y-m-d H:i:s"),
  1595. "updatetime" => date("Y-m-d H:i:s")
  1596. ];
  1597. $win = Db::name("purchease_in")->insert($orin);
  1598. if (!$win){
  1599. $stv = ["order_code"=>$wsm_in_code,"status"=>4,"action_remark"=>'',"action_type"=>"create"];
  1600. ActionLog::logAdd($this->post['token'],$stv,"xsd",4,$stv);
  1601. Db::rollback();
  1602. return error_show(1003, "创建失败");
  1603. }
  1604. $good = Db::name("good_stock")->where(["wsm_code" => $wsm['wsm_code'], "good_type_code" => $zxinfo['cpNo'], "is_del" => 0])->find();
  1605. if (empty($good)) {
  1606. $good = [
  1607. "good_type_code" => $zxinfo['cpNo'],
  1608. "wsm_code" => $wsm['wsm_code'],
  1609. "usable_stock" => 0,
  1610. "wait_out_stock" => 0,
  1611. "total_stock" => 0,
  1612. "addtime" => date("Y-m-d H:i:s"),
  1613. "updatetime" => date("Y-m-d H:i:s"),
  1614. ];
  1615. }
  1616. $good['wait_out_stock'] += $dio['good_num'];
  1617. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  1618. $good['updatetime'] = date("Y-m-d H:i:s");
  1619. $upd = Db::name("good_stock")->save($good);
  1620. if (!$upd) {
  1621. $ste = ["order_code"=> $zxinfo['cpNo'],"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1622. ActionLog::logAdd($this->post['token'],$ste,"xsd",0,$ste);
  1623. Db::rollback();
  1624. return error_show(1003, "创建失败");
  1625. }
  1626. }
  1627. }
  1628. }
  1629. Db::commit();
  1630. return error_show(0, "订单状态更新成功");
  1631. } catch (Exception $e) {
  1632. Db::rollback();
  1633. return error_show(1005, $e->getMessage());
  1634. }
  1635. }
  1636. public function fee()
  1637. {
  1638. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  1639. if ($id == "") {
  1640. return error_show(1002, "参数id不能为空");
  1641. }
  1642. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  1643. if (empty($etid)) {
  1644. return error_show(1002, "未找到数据");
  1645. }
  1646. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  1647. if ($vr == "") {
  1648. return error_show(1002, "参数order_addr不能为空");
  1649. }
  1650. Db::startTrans();
  1651. try {
  1652. $data = [
  1653. "id" => $id,
  1654. "status" => 2,
  1655. "is_del" => 0,
  1656. "updatetime" => date("Y-m-d H:i:s")
  1657. ];
  1658. $datainfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($data);
  1659. if ($datainfo) {
  1660. $stx = ["order_code"=> $etid['orderCode'],"status"=>$etid['status'],"action_remark"=>'',
  1661. "action_type"=>"edit"];
  1662. ActionLog::logAdd($this->post['token'],$stx,$etid['order_type'] == 2?"ZXQRD":"XSQRD",2,$data);
  1663. $order=["order_type"=>$etid['order_type'] == 2?"ZXQRD":"XSQRD","order_code"=>$etid['orderCode'],"order_id"=>$id,"order_status"=>$data['status']];
  1664. ProcessOrder::AddProcess($this->post['token'],$order);
  1665. foreach ($vr as $value) {
  1666. $tm = [];
  1667. $tm['id'] = $value['id'];
  1668. $tm['orderCode'] = $etid['orderCode'];
  1669. $tm['post_fee'] = $value['post_fee'];
  1670. $tm['updatetime'] = date("Y-m-d H:i:s");
  1671. $dn = Db::name('order_addr')->save($tm);
  1672. if ($dn == false) {
  1673. Db::rollback();
  1674. return error_show(1002, "更新失败");
  1675. }
  1676. }
  1677. Db::commit();
  1678. return error_show(0, "更新成功");
  1679. }
  1680. Db::rollback();
  1681. return error_show(1003, "更新失败");
  1682. } catch (Exception $e) {
  1683. Db::rollback();
  1684. return error_show(1005, $e->getMessage());
  1685. }
  1686. }
  1687. public function out()
  1688. {
  1689. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  1690. $outCode = makeNo("CK");
  1691. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  1692. if ($orderCode == "") {
  1693. return error_show(1002, "参数orderCoder不能为空");
  1694. }
  1695. $der = Db::name('sale')->where(['orderCode' => $orderCode])->find();
  1696. if ($der == "") {
  1697. return error_show(1002, "未找到出库订单单号");
  1698. }
  1699. // $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  1700. // if ($wsm_code == "") {
  1701. // return error_show(1002, "参数wsm_code不能为空");
  1702. // }
  1703. // $wsm = Db::name('sale_info')->where(['wsm_code' => $wsm_code, 'orderCode' => $der['orderCode']])->find();
  1704. // if ($wsm == "") {
  1705. // return error_show(1002, "未找到仓库编码");
  1706. // }
  1707. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !== "" ? trim($this->post['post_name']) : "";
  1708. if ($post_name == "") {
  1709. return error_show(1002, "物流公司不能为空");
  1710. }
  1711. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
  1712. if ($post_code == "") {
  1713. return error_show(1002, "物流单号不能为空");
  1714. }
  1715. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
  1716. if ($post_fee == "") {
  1717. return error_show(1002, "物流费不能为空");
  1718. }
  1719. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  1720. if ($send_num == "") {
  1721. return error_show(1002, "发货数量不能为空");
  1722. }
  1723. if ($send_num > $der['wsend_num']) {
  1724. return error_show(1002, "超出可发货数量");
  1725. }
  1726. $apply_id = GetUserInfo($token);
  1727. if (empty($apply_id) || $apply_id['code'] != 0) {
  1728. return error_show(1002, "申请人数据不存在");
  1729. }
  1730. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1731. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1732. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !== "" ? intval($this->post['addrid']) : "";
  1733. $dr = Db::name('order_addr')->where(['id' => $addrid, 'is_del' => 0])->find();
  1734. if (empty($dr)) {
  1735. return error_show(1003, "收货人信息未找到");
  1736. }
  1737. if ($dr['orderCode'] !== $orderCode) {
  1738. return error_show(1002, "订单code不存在");
  1739. }
  1740. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  1741. // $check_num = isset($this->post['check_num']) && $this->post['check_num'] !=="" ? $this->post['check_num'] :"";
  1742. // $error_num = isset($this->post['error_num']) && $this->post['error_num'] !=="" ? $this->post['error_num'] :"";
  1743. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "1";
  1744. //$order_type = isset($this->post['order_type']) && $this->post['order_type'] !=="" ? $this->post['order_type'] :"1";
  1745. //$dn=Db::name('sale_info')->where(['wsm_code'=>$outCode])->find();
  1746. Db::startTrans();
  1747. try {
  1748. $data = [
  1749. "wsm_code" => "",
  1750. "orderCode" => $orderCode,
  1751. "outCode" => $outCode,
  1752. "order_type" => $der['order_type'],
  1753. "apply_id" => $rm,
  1754. "apply_name" => $ri,
  1755. "addrid" => $dr['id'],
  1756. "post_name" => $post_name,
  1757. "post_code" => $post_code,
  1758. "post_fee" => $post_fee,
  1759. "sendtime" => $sendtime,
  1760. "send_num" => $send_num,
  1761. "check_num" => 0,
  1762. "error_num" => 0,
  1763. "status" => $status,
  1764. "addtime" => date("Y-m-d H:i:s"),
  1765. "updatetime" => date("Y-m-d H:i:s")
  1766. ];
  1767. $datainfo = Db::name('order_out')->insert($data,true);
  1768. if ($datainfo>0) {
  1769. $stx = ["order_code"=>$outCode,"status"=>$status,"action_remark"=>'',"action_type"=>"create"];
  1770. ActionLog::logAdd($this->post['token'],$stx,$der['order_type']==1?"CKD":'ZXCKD',$status,$data);
  1771. $order=["order_type"=>$der['order_type']==1?"CKD":'ZXCKD',"order_code"=>$outCode,"order_id"=>$datainfo,"order_status"=>$data['status']];
  1772. ProcessOrder::AddProcess($this->post['token'],$order);
  1773. if ($status == 1) {
  1774. $var = Db::name('sale_info')->where(['orderCode' => $orderCode, 'wsm_code' => $wsm_code])->find();
  1775. if ($var == "") {
  1776. return error_show(1002, "未找到数据");
  1777. }
  1778. // $send = Db::name("order_out")->where(["wsm_code" => $wsm_code, 'orderCode' => $orderCode])->sum
  1779. //("send_num");
  1780. $item =$der['status'];
  1781. $der['send_num'] += $send_num;
  1782. $der['wsend_num'] -= $send_num;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  1783. $der['send_status'] = $der['send_num'] == 0 ? 1 : $der['wsend_num'] == 0 ? 3 : 2;
  1784. $der['status'] = $der['send_num'] == 0 ? 3 : $der['wsend_num'] == 0 ? 5 : 4;
  1785. $der['updatetime'] = date("Y-m-d H:i:s");
  1786. $si = Db::name('sale')->save($der);
  1787. if ($si == false) {
  1788. Db::rollback();
  1789. return error_show(1003, "更新失败");
  1790. }
  1791. $stx = ["order_code"=>$orderCode,"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  1792. ActionLog::logAdd($this->post['token'],$stx,$der['order_type'] == 2?"ZXQRD":"XSQRD",$der['status'],$der);
  1793. $order=["order_type"=>$der['order_type'] == 2?"ZXQRD":"XSQRD","order_code"=>$orderCode,"order_id"=>$der['id'],"order_status"=>$data['status']];
  1794. ProcessOrder::AddProcess($this->post['token'],$order);
  1795. $str = Db::name('good_stock')->where(['wsm_code' => $var['wsm_code'], 'good_type_code' => $der['good_code']])->find();
  1796. if ($str == false) {
  1797. Db::rollback();
  1798. return error_show(1002, "商品数据未找到");
  1799. }
  1800. if ($send_num > $str['wait_out_stock']) {
  1801. Db::rollback();
  1802. return error_show(1002, "超出库存数量");
  1803. }
  1804. $str['wait_out_stock'] -= $send_num;
  1805. $str['intra_stock'] += $send_num;
  1806. $str['updatetime'] = date("Y-m-d H:i:s");
  1807. $stre = Db::name('good_stock')->save($str);
  1808. if ($stre == false) {
  1809. Db::rollback();
  1810. return error_show(1002, "状态更新失败");
  1811. }
  1812. $good_data[]= ["stock_id"=>$str['id'],"type"=>2,'stock'=>$send_num,"stock_name"=>"wait_out_stock"];
  1813. $good_data[]= ["stock_id"=>$str['id'],"type"=>1,'stock'=>$send_num,"stock_name"=>"intra_stock"];
  1814. GoodLog::LogAdd($this->post['token'],$good_data,$der['order_type'] == 2?"ZXQRD":"XSQRD");
  1815. }
  1816. Db::commit();
  1817. return error_show(0, "创建成功");
  1818. } else {
  1819. Db::rollback();
  1820. return error_show(1003, "创建失败");
  1821. }
  1822. } catch (Exception $e) {
  1823. Db::rollback();
  1824. return error_show(1005, $e->getMessage());
  1825. }
  1826. }
  1827. public function customer()
  1828. {
  1829. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  1830. if (empty($outCode)) {
  1831. return error_show(1002, "销售订单编号不能为空");
  1832. }
  1833. $codeinfo = Db::name('order_out')->where(['outCode' => $outCode])->find();
  1834. if ($codeinfo == "") {
  1835. return error_show(1003, "未找到订单数据");
  1836. }
  1837. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  1838. // if($orderCode==""){
  1839. // return error_show(1002,"参数orderCoder不能为空");
  1840. // }
  1841. $dr = Db::name('sale')->where(['orderCode' => $codeinfo['orderCode']])->find();
  1842. if ($dr == "") {
  1843. return error_show(1002, "未找到销售订单");
  1844. }
  1845. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "";
  1846. if ($status == "") {
  1847. return error_show(10002, "状态不能为空");
  1848. }
  1849. Db::startTrans();
  1850. try {
  1851. $time = $codeinfo['status'];
  1852. $codeinfo['status'] = $status;
  1853. $codeinfo['updatetime'] = date("Y-m-d H:i:s");
  1854. $cust = Db::name('order_out')->save($codeinfo);
  1855. if ($cust) {
  1856. $stx = ["order_code"=>$outCode,"status"=>$time,"action_remark"=>'',"action_type"=>"status"];
  1857. ActionLog::logAdd($this->post['token'],$stx,$codeinfo['order_type'] == 2?"ZXCKD":"CKD",$codeinfo['status'],$codeinfo);
  1858. $order=["order_type"=>$codeinfo['order_type'] == 2?"ZXCKD":"CKD","order_code"=>$outCode,
  1859. "order_id"=>$codeinfo['id'],"order_status"=>$codeinfo['status']];
  1860. ProcessOrder::AddProcess($this->post['token'],$order);
  1861. if ($status == 1) {
  1862. $var = Db::name('sale_info')->where(['orderCode' =>$codeinfo['orderCode'], 'wsm_code' =>$codeinfo['wsm_code']])->find();
  1863. //var_dump(Db::name('sale_info')->getLastSql());
  1864. if ($var == "") {
  1865. return error_show(1002, "未找到数据");
  1866. }
  1867. // $send = Db::name("order_out")->where(["wsm_code" => $wsm_code, 'orderCode' => $orderCode])->sum
  1868. //("send_num");
  1869. $item =$dr['status'];
  1870. $dr['send_num'] +=$codeinfo['send_num'];
  1871. $dr['wsend_num'] -= $codeinfo['send_num'];//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  1872. $dr['send_status'] = $dr['send_num'] == 0 ? 1 : $dr['wsend_num'] == 0 ? 3 : 2;
  1873. $dr['status'] = $dr['send_num'] == 0 ? 3 : $dr['wsend_num'] == 0 ? 5 : 4;
  1874. $dr['updatetime'] = date("Y-m-d H:i:s");
  1875. $si = Db::name('sale')->save($dr);
  1876. if ($si == false) {
  1877. Db::rollback();
  1878. return error_show(1003, "更新失败");
  1879. }
  1880. $stx = ["order_code"=>$dr['orderCode'],"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  1881. ActionLog::logAdd($this->post['token'],$stx,$dr['order_type'] == 2?"ZXQRD":"XSQRD",$dr['status'],
  1882. $dr);
  1883. $order=["order_type"=>$dr['order_type'] == 2?"ZXQRD":"XSQRD","order_code"=>$dr['orderCode'],"order_id"=>$dr['id'],"order_status"=>$dr['status']];
  1884. ProcessOrder::AddProcess($this->post['token'],$order);
  1885. $str = Db::name('good_stock')->where(['wsm_code' => $var['wsm_code'], 'good_type_code' => $dr['good_code']])->find();
  1886. if ($str == false) {
  1887. Db::rollback();
  1888. return error_show(1002, "商品数据未找到");
  1889. }
  1890. if ($codeinfo['send_num'] > $str['wait_out_stock']) {
  1891. Db::rollback();
  1892. return error_show(1002, "超出库存数量");
  1893. }
  1894. $str['wait_out_stock'] -= $codeinfo['send_num'];
  1895. $str['intra_stock'] += $codeinfo['send_num'];
  1896. $str['updatetime'] = date("Y-m-d H:i:s");
  1897. $stre = Db::name('good_stock')->save($str);
  1898. if ($stre == false) {
  1899. Db::rollback();
  1900. return error_show(1002, "状态更新失败");
  1901. }
  1902. $good_data[]= ["stock_id"=>$str['id'],"type"=>2,'stock'=>$codeinfo['send_num'],"stock_name"=>"wait_out_stock"];
  1903. $good_data[]= ["stock_id"=>$str['id'],"type"=>1,'stock'=>$codeinfo['send_num'],"stock_name"=>"intra_stock"];
  1904. GoodLog::LogAdd($this->post['token'],$good_data,'CKD');
  1905. }
  1906. if ($status == 2) {
  1907. $ct = Db::name('sale_info')->where(['orderCode' => $dr['orderCode'], 'wsm_code' => $codeinfo['wsm_code']])->find();
  1908. if ($ct == false) {
  1909. return error_show(1002, "未找到数据");
  1910. }
  1911. $var = Db::name('good_stock')->where(['wsm_code' => $codeinfo['wsm_code'], 'good_type_code' => $dr['good_code']])->find();
  1912. if ($var == false) {
  1913. Db::rollback();
  1914. return error_show(1002, "未找到商品数据");
  1915. }
  1916. if ($ct['num'] > $var['intra_stock']) {
  1917. Db::rollback();
  1918. return error_show(1003, "超出库存数量");
  1919. }
  1920. $var['intra_stock'] -= $codeinfo['send_num'];
  1921. $var['updatetime'] = date("Y-m-d H:i:s");
  1922. $stre = Db::name('good_stock')->save($var);
  1923. $good_data[]= ["stock_id"=>$var['id'],"type"=>2,'stock'=>$codeinfo['send_num'],"stock_name"=>"intra_stock"];
  1924. GoodLog::LogAdd($this->post['token'],$good_data,$codeinfo['order_type'] == 2?"ZXCKD":"CKD");
  1925. if ($stre == false) {
  1926. Db::rollback();
  1927. return error_show(1002, "状态更新失败");
  1928. }
  1929. }
  1930. Db::commit();
  1931. return error_show(0, "出库订单更新成功");
  1932. } else {
  1933. Db::rollback();
  1934. return error_show(1003, "出库订单更新失败");
  1935. }
  1936. } catch (Exception $e) {
  1937. Db::rollback();
  1938. return error_show(1005, $e->getMessage());
  1939. }
  1940. }
  1941. public function difflist()
  1942. {
  1943. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  1944. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  1945. $where = [];
  1946. $cgdNo = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  1947. if ($cgdNo != "") {
  1948. $where[] = ['a.orderCode', "like", "%$cgdNo%"];
  1949. }
  1950. $diffNo = isset($this->post['diffCode']) && $this->post['diffCode'] != "" ? trim($this->post['diffCode']) : "";
  1951. if ($diffNo != "") {
  1952. $where[] = ['a.diffCode', "like", "%$diffNo%"];
  1953. }
  1954. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  1955. if ($status !== "") {
  1956. $where[] = ['a.status', "=", $status];
  1957. }
  1958. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  1959. if ($start != "") {
  1960. $where[] = ["a.addtime", '>=', $start];
  1961. }
  1962. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  1963. if ($end != "") {
  1964. $where[] = ["a.addtime", '<=', $end];
  1965. }
  1966. // $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  1967. // if($apply_name!==""){
  1968. // $where[]=['a.apply_name',"like","%$apply_name%"];
  1969. // }
  1970. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  1971. if ($supplierNo != "") {
  1972. $supplier = Db::name("supplier")->where([['code', "like", "%$supplierNo%"]])->column('code');
  1973. // if(empty($supplier)){
  1974. // return error_show(1004,"未找到供应商信息");
  1975. // }
  1976. $wsmcode = Db::name("sale")->where([
  1977. "is_del" => 0, "supplierNo" => $supplier])->column("orderCode");
  1978. $where[] = ['a.orderCode', "in", $wsmcode];
  1979. }
  1980. $companyNo =isset($this->post['companyNo']) &&$this->post['companyNo'] !=="" ? trim($this->post['companyNo']):"";
  1981. if($companyNo!=""){
  1982. $where[]=['c.customer_code',"like","%$companyNo%"];
  1983. }
  1984. $count = Db::name('sale_diff')->alias('a')
  1985. ->join("sale c", "c.orderCode=a.orderCode", "left")
  1986. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  1987. ->join("good b", "b.good_code=a.good_code", "left")
  1988. ->where($where)->count();
  1989. $total = ceil($count / $size);
  1990. $page = $page >= $total ? $total : $page;
  1991. //var_dump( Db::name('sale_diff')->getLastSql());
  1992. $list = Db::name('sale_diff')->alias('a')
  1993. ->join("sale c", "c.orderCode=a.orderCode", "left")
  1994. ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  1995. ->join("good b", "b.good_code=a.good_kode", "left")
  1996. ->where($where)->page($page, $size)->field("a.*,b.cat_id,c.customer_code,v.companyName")
  1997. ->order("a.addtime desc")->select();
  1998. $data = [];
  1999. foreach ($list as $value) {
  2000. $value['can'] = isset($value['cat_id']) && $value['cat_id'] != 0 ? made($value['cat_id']) : [];
  2001. $wsm = Db::name("sale")->alias("a")->Join("supplier b", "a.supplierNo=b.code", "left")->where
  2002. (['a.orderCode' => $value['orderCode'], "a.is_del" => 0])->field("b.code,b.name")->find();
  2003. $value['supplierNo'] = isset($wsm['code']) ? $wsm['code'] : "";
  2004. $value['supplierName'] = isset($wsm['name']) ? $wsm['name'] : "";
  2005. $data[] = $value;
  2006. }
  2007. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  2008. }
  2009. /**
  2010. * @return \think\response\Json|void
  2011. * @throws \think\db\exception\DataNotFoundException
  2012. * @throws \think\db\exception\DbException
  2013. * @throws \think\db\exception\ModelNotFoundException
  2014. */
  2015. public function diffcheck()
  2016. {
  2017. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2018. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2019. if ($id === "") {
  2020. return error_show(1004, "参数id不能为空");
  2021. }
  2022. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  2023. if (empty($info)) {
  2024. return error_show(1004, "订单数据未找到");
  2025. }
  2026. $is_act = isset($this->post['is_act']) && $this->post['is_act'] !== "" ? intval($this->post['is_act']) : "";
  2027. if ($is_act === '') {
  2028. return error_show(1004, "参数is_act不能为空");
  2029. }
  2030. $customer_remark = isset($this->post['customer_remark']) && $this->post['customer_remark'] !== "" ? trim($this->post['customer_remark']) : "";
  2031. if ($customer_remark === '') {
  2032. return error_show(1004, "参数customer_remark不能为空");
  2033. }
  2034. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "2";
  2035. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  2036. $item = $info['status'];
  2037. $info['is_act'] = $is_act;
  2038. $info['customer_remark'] = $customer_remark;
  2039. $info['status'] = $status;
  2040. $info['remark'] = $remark;
  2041. $info['updatetime'] = date("Y-m-d H:i:s");
  2042. $up = Db::name("sale_diff")->save($info);
  2043. if ($up) {
  2044. $stx = ["order_code"=>$info["orderCode"],"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  2045. ActionLog::logAdd($this->post['token'],$stx,"QRGCD",$info['status'],$info);
  2046. $order=["order_type"=>"QRGCD","order_code"=>$info["orderCode"],"order_id"=>$info['id'],"order_status"=>$info['status']];
  2047. ProcessOrder::AddProcess($this->post['token'],$order);
  2048. return app_show(0, "更新成功");
  2049. } else {
  2050. return error_show(1003, "更新失败");
  2051. }
  2052. }
  2053. /**
  2054. * @return \think\response\Json|void
  2055. * @throws \think\db\exception\DataNotFoundException
  2056. * @throws \think\db\exception\DbException
  2057. * @throws \think\db\exception\ModelNotFoundException
  2058. */
  2059. public function diffstatus()
  2060. {
  2061. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2062. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2063. if ($id === "") {
  2064. return error_show(1004, "参数id不能为空");
  2065. }
  2066. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  2067. if (empty($info)) {
  2068. return error_show(1004, "订单数据未找到");
  2069. }
  2070. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "3";
  2071. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  2072. $ite=$info['status'];
  2073. $info['status'] = $status;
  2074. $info['remark'] = $remark;
  2075. $info['updatetime'] = date("Y-m-d H:i:s");
  2076. $up = Db::name("sale_diff")->save($info);
  2077. if ($up) {
  2078. $stx = ["order_code"=>$info["orderCode"],"status"=>$ite,"action_remark"=>'',"action_type"=>"status"];
  2079. ActionLog::logAdd($this->post['token'],$stx,"QRGCD",$info['status'],$info);
  2080. $order=["order_type"=>"QRGCD","order_code"=>$info["orderCode"],"order_id"=>$info['id'],"order_status"=>$info['status']];
  2081. ProcessOrder::AddProcess($this->post['token'],$order);
  2082. return app_show(0, "更新成功");
  2083. } else {
  2084. return error_show(1003, "更新失败");
  2085. }
  2086. }
  2087. /**
  2088. * @return \think\response\Json|void
  2089. * @throws \think\db\exception\DataNotFoundException
  2090. * @throws \think\db\exception\DbException
  2091. * @throws \think\db\exception\ModelNotFoundException
  2092. */
  2093. public function diffinfo()
  2094. {
  2095. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2096. if ($id === "") {
  2097. return error_show(1004, "参数id不能为空");
  2098. }
  2099. $info = Db::name('sale_diff')->alias('a')->join("good b", "b.good_code=a.good_kode", "left")->where
  2100. (["a.id" => $id])->field("a.*,b.cat_id")->find();
  2101. if (empty($info)) {
  2102. return error_show(1004, "订单数据未找到");
  2103. }
  2104. $info['can'] = isset($info['cat_id']) && $info['cat_id'] !== 0 ? made($info['cat_id']) : [];
  2105. return app_show(0, "获取成功", $info);
  2106. }
  2107. public function saleout(){
  2108. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  2109. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  2110. $where= [];
  2111. $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode'])
  2112. :"";
  2113. if($orderCode !==""){
  2114. $where[]=['a.orderCode',"like","%$orderCode%"];
  2115. }
  2116. $order_type=isset($this->post['order_type']) && $this->post['order_type'] !=="" ? trim($this->post['order_type'])
  2117. :"";
  2118. if($order_type !==""){
  2119. $where[]=['a.order_type',"=",$order_type];
  2120. }
  2121. $outCode=isset($this->post['outCode']) && $this->post['outCode'] !=="" ? trim($this->post['outCode'])
  2122. :"";
  2123. if($outCode !==""){
  2124. $where[]=['a.outCode',"like","%$outCode%"];
  2125. }
  2126. $apply_name=isset($this->post['apply_name']) && $this->post['apply_name'] !=="" ? trim($this->post['apply_name'])
  2127. :"";
  2128. if($apply_name !==""){
  2129. $where[]=['a.apply_name',"like","%$apply_name%"];
  2130. }
  2131. $good_code=isset($this->post['good_code']) && $this->post['good_code'] !=="" ? trim($this->post['good_code'])
  2132. :"";
  2133. if($good_code !==""){
  2134. $where[]=['b.good_code',"like","%$good_code%"];
  2135. }
  2136. $good_name=isset($this->post['good_name']) && $this->post['good_name'] !=="" ? trim($this->post['good_name'])
  2137. :"";
  2138. if($good_name !==""){
  2139. $where[]=['b.good_name',"like","%$good_name%"];
  2140. }
  2141. $status=isset($this->post['status']) && $this->post['status'] !=="" ? trim($this->post['status']):"";
  2142. if($status !==""){
  2143. $where[]=['a.status',"=",$status];
  2144. }
  2145. $start = isset($this->post['start']) && $this->post['start'] !=="" ? $this->post['start'] :"";
  2146. if($start !=""){
  2147. $where[]= ["a.addtime",'>=',$start];
  2148. }
  2149. $end = isset($this->post['end']) && $this->post['end'] !=="" ? $this->post['end'] :"";
  2150. if($end !=""){
  2151. $where[]= ["a.addtime",'<=',$end];
  2152. }
  2153. $companyNo =isset($this->post['companyNo']) &&$this->post['companyNo'] !=="" ? trim($this->post['companyNo']):"";
  2154. if($companyNo!=""){
  2155. $where[]=['b.customer_code',"like","%$companyNo%"];
  2156. }
  2157. $count = Db::name('order_out')->alias('a')
  2158. ->join("sale b", "b.orderCode=a.orderCode", "left")
  2159. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  2160. //->join("good n","n.good_code=b.good_code","left")
  2161. ->where($where)->count();
  2162. $total = ceil($count / $size);
  2163. $page = $page >= $total ? $total : $page;
  2164. $list = Db::name('order_out')->alias('a')->join("sale b", "b.orderCode=a.orderCode", "left")
  2165. ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  2166. //->join("good n","n.good_code=b.good_code","left")
  2167. ->field("a.*,b.good_name,b.good_code,b.supplierNo,b.customer_code,v.companyName,b.origin_price,b.sale_price,b.total_price")
  2168. ->where($where)->order("addtime desc")->page($page, $size)->select();
  2169. $data=[];
  2170. foreach ($list as $value) {
  2171. $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  2172. ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  2173. $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find();
  2174. $good = Db::name("good_type")->alias("b")->join("good a", "a.good_code = b.good_code", "left")
  2175. ->where(["type_code" => $value['good_code']])->field('cat_id')->find();
  2176. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  2177. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  2178. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  2179. $value['addr'] = isset($addr['addr']) ? $addr['addr'] : "";
  2180. $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  2181. $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  2182. $value['can'] = isset($good['cat_id']) && $good['cat_id'] != 0 ? made($good['cat_id']) : [];
  2183. $data[] = $value;
  2184. }
  2185. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  2186. }
  2187. public function outinfo()
  2188. {
  2189. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  2190. if ($outCode == "") {
  2191. return error_show(1002, "参数outcode不能为空");
  2192. }
  2193. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode])->find();
  2194. if (empty($codeinfo)) {
  2195. return error_show(1002, "未找到出库数据");
  2196. }
  2197. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  2198. if($item['order_type']==3|| $item['order_type']==4){
  2199. // $good=Db::name("good_zixun")->where(["spuCode"=>$good_code])->find();
  2200. // if($good==false){
  2201. // return error_show(1004,"未找到商品数据");
  2202. // }
  2203. $goodinfo = Db::name("consult_bids")->where(["spuCode"=>$item['good_code'],"is_del"=>0])->find();
  2204. if($goodinfo==false){
  2205. return error_show(1004,"未找到商品数据");
  2206. }
  2207. }else{
  2208. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2209. ->where(['a.skuCode' =>$item['sku_code']])->find();
  2210. if ($goodinfo == false) {
  2211. return error_show(1002, "未找到商品数据");
  2212. }
  2213. }
  2214. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  2215. // $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  2216. // ->where(["k.wsm_code" => $codeinfo['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  2217. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  2218. //$item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  2219. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  2220. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  2221. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  2222. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  2223. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  2224. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  2225. // $codeinfo['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  2226. // $codeinfo['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  2227. // $codeinfo['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  2228. $codeinfo['addr'] = GetAddr($addr['addr_code']);
  2229. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  2230. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  2231. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  2232. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  2233. $codeinfo['can'] = $int;
  2234. return app_show(0, "获取成功", $codeinfo);
  2235. }
  2236. public function outadd(){
  2237. $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2238. $outCode = makeNo("CK");
  2239. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  2240. if ($orderCode == "") {
  2241. return error_show(1002, "参数orderCoder不能为空");
  2242. }
  2243. $der = Db::name('sale')->where(['orderCode' => $orderCode])->find();
  2244. if ($der == "") {
  2245. return error_show(1002, "未找到出库订单单号");
  2246. }
  2247. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  2248. if ($send_num == "") {
  2249. return error_show(1002, "发货数量不能为空");
  2250. }
  2251. if ($send_num > $der['wsend_num']) {
  2252. return error_show(1002, "超出可发货数量");
  2253. }
  2254. $contactor = isset($this->post['contactor'])&&$this->post['contactor']!="" ? trim($this->post['contactor']):"";
  2255. if($contactor==""){
  2256. return error_show(1002, "参数contactor不能为空");
  2257. }
  2258. $mobile = isset($this->post['mobile'])&&$this->post['mobile']!="" ? trim($this->post['mobile']):"";
  2259. if($mobile==""){
  2260. return error_show(1002, "参数mobile不能为空");
  2261. }
  2262. $addr = isset($this->post['addr'])&&$this->post['addr']!="" ? trim($this->post['addr']):"";
  2263. if($addr==""){
  2264. return error_show(1002, "参数addr不能为空");
  2265. }
  2266. $addr_code = isset($this->post['addr_code'])&&$this->post['addr_code']!="" ? trim($this->post['addr_code']):"";
  2267. if($addr_code==""){
  2268. return error_show(1002, "参数addr_code不能为空");
  2269. }
  2270. $apply_id = GetUserInfo($token);
  2271. if (empty($apply_id) || $apply_id['code'] != 0) {
  2272. return error_show(1002, "申请人数据不存在");
  2273. }
  2274. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2275. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2276. // $addrid = isset($this->post['addrid']) && $this->post['addrid'] !== "" ? intval($this->post['addrid']) : "";
  2277. // $dr = Db::name('order_addr')->where(['id' => $addrid, 'is_del' => 0])->find();
  2278. // if (empty($dr)) {
  2279. // return error_show(1003, "收货人信息未找到");
  2280. // }
  2281. // if ($dr['orderCode'] !== $orderCode) {
  2282. // return error_show(1002, "订单code不存在");
  2283. // }
  2284. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  2285. $order =Db::name("order_num")->where(["orderCode"=>$orderCode,"status"=>1])->where([["wsend_num",">=",0]])
  2286. ->select()->toArray();
  2287. if(empty($order)){
  2288. return error_show(1004,"未找到可以发货得采购单数据");
  2289. }
  2290. Db::startTrans();
  2291. try {
  2292. $addrlst =[
  2293. "orderCode"=>$orderCode,
  2294. "addr"=>$addr,
  2295. "addr_code"=>json_encode($addr_code),
  2296. "contactor"=>$contactor,
  2297. "mobile"=>$mobile,
  2298. "customer_code"=>$der['customer_code'],
  2299. "post_fee"=>$der['post_fee'],
  2300. "arrive_time"=>$sendtime,
  2301. "receipt_quantity"=>$send_num,
  2302. "is_del"=>0,
  2303. "addtime"=>date("Y-m-d H:i:s"),
  2304. "updatetime"=>date("Y-m-d H:i:s")
  2305. ];
  2306. $addrid = Db::name("order_addr")->insert($addrlst,true);
  2307. if($addrid>0){
  2308. $data = [
  2309. "wsm_code" => "",
  2310. "orderCode" => $orderCode,
  2311. "outCode" => $outCode,
  2312. "order_type" => $der['order_type'],
  2313. "apply_id" => $rm,
  2314. "apply_name" => $ri,
  2315. "addrid" => $addrid,
  2316. "post_name" => "",
  2317. "post_code" =>"",
  2318. "post_fee" => "",
  2319. "sendtime" => $sendtime,
  2320. "send_num" => $send_num,
  2321. "check_num" => 0,
  2322. "error_num" => 0,
  2323. "status" => 1,
  2324. "addtime" => date("Y-m-d H:i:s"),
  2325. "updatetime" => date("Y-m-d H:i:s")
  2326. ];
  2327. $datainfo = Db::name('order_out')->insert($data,true);
  2328. if($datainfo>0){
  2329. $num = $send_num;
  2330. foreach ($order as $val){
  2331. $tep=[];
  2332. $ornum=0;
  2333. if($val['wsend_num']>=$num){
  2334. $val['wsend_num']-=$num;
  2335. $val['send_num']+=$num;
  2336. $ornum =$num;
  2337. $num=0;
  2338. }else{
  2339. $num-=$val['wsend_num'];
  2340. $val['send_num']+=$val['wsend_num'];
  2341. $ornum =$val['wsend_num'];
  2342. $val['wsend_num']=0;
  2343. }
  2344. $or =Db::name("order_num")->save($val);
  2345. if($or==false){
  2346. Db::rollback();
  2347. return error_show(1002,"发货地址添加创建失败");
  2348. }
  2349. $tep=[
  2350. "cgdNo"=>$val['cgdNo'],
  2351. "outCode"=>$outCode,
  2352. "send_num"=>$ornum,
  2353. "status"=>1,
  2354. "addtime"=>date("Y-m-d H:i:s"),
  2355. "updatetime"=>date("Y-m-d H:i:s")
  2356. ];
  2357. $sen=Db::name("order_send")->save($tep);
  2358. if($sen==false){
  2359. Db::rollback();
  2360. return error_show(1002,"发货地址添加创建失败");
  2361. }
  2362. if($num==0){
  2363. break;
  2364. }
  2365. }
  2366. Db::commit();
  2367. return app_show(0,"出库单新建成功",["outCode"=>$outCode]);
  2368. }
  2369. }
  2370. Db::rollback();
  2371. return error_show(1004,"出库单新建失败");
  2372. } catch (Exception $e) {
  2373. Db::rollback();
  2374. return error_show(1005, $e->getMessage());
  2375. }
  2376. }
  2377. public function outSend(){
  2378. $outCode=isset($this->post['outCode'])&& $this->post['outCode']!=""? trim($this->post['outCode']):"";
  2379. if($outCode==""){
  2380. return error_show(1004,"参数outCode不能为空");
  2381. }
  2382. $outinfo = Db::name("order_out")->where(["outCode"=>$outCode])->find();
  2383. if($outinfo==false){
  2384. return error_show(1004,"发货数据未找到");
  2385. }
  2386. $post_name = isset($this->post['post_name']) &&$this->post['post_name']!="" ? trim($this->post['post_name']):"";
  2387. if($post_name==""){
  2388. return error_show(1004,"参数post_name不能为空");
  2389. }
  2390. $post_code = isset($this->post['post_code']) &&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  2391. if($post_code==""){
  2392. return error_show(1004,"参数post_code不能为空");
  2393. }
  2394. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee']!="" ? floatval($this->post['post_fee']):"";
  2395. $outinfo['post_name'] = $post_name;
  2396. $outinfo['post_code'] = $post_code;
  2397. $outinfo['post_fee'] = $post_fee;
  2398. $outinfo['sendtime'] = date("Y-m-d H:i:s");
  2399. $outinfo['status'] = 2;
  2400. $outinfo['updatetime'] = date("Y-m-d H:i:s");
  2401. $up =Db::name("order_out")->save($outinfo);
  2402. if($up){
  2403. $dbol =$this->RelaCgd($outinfo);
  2404. if($dbol==false){
  2405. Db::rollback();
  2406. return error_show(1004,"采购关联失败");
  2407. }
  2408. return app_show(0,"发货成功");
  2409. }else{
  2410. return error_show(1004,"发货失败");
  2411. }
  2412. }
  2413. public function RelaCgd($outinfo){
  2414. $der = Db::name('sale')->where(['orderCode' => $outinfo['orderCode']])->find();
  2415. if ($der == "") {
  2416. return false;
  2417. }
  2418. if($der['is_stock']==1){
  2419. $cgd = Db::name("order_bk")->where([["spuCode","=",$der['spuCode']],["is_del","=",0],["balance_num",">",0]])
  2420. ->select()->toArray();
  2421. if(empty($cgd)){
  2422. return false;
  2423. }
  2424. $send =$outinfo['send_num'];
  2425. $outsend=[];
  2426. foreach ($cgd as $value){
  2427. $temp=[];
  2428. if($value['balance_num']>=$send){
  2429. $value['balance_num']-=$send;
  2430. $value['merge_num']+=$send;
  2431. $send=0;
  2432. $temp['send_num'] =$send;
  2433. }else{
  2434. $value['balance_num'] =0;
  2435. $value['merge_num']+=$value['balance_num'];
  2436. $send -= $value['balance_num'];
  2437. $temp['send_num'] =$value['balance_num'];
  2438. }
  2439. $value['status'] = $value['balance_num']==0 ?2:$value['merge_num']==0?0:1 ;
  2440. $value['updatetime'] = date("Y-m-d H:i:s");
  2441. $bk = Db::name("order_bk")->save($value);
  2442. if($bk==false){
  2443. return false;
  2444. }
  2445. $temp['cgdNo'] = $value['cgdNo'];
  2446. $temp['outCode'] = $outinfo['outCode'];
  2447. $temp['status'] = 1;
  2448. $temp['addtime']=date("Y-m-d H:i:s");
  2449. $temp['updatetime']=date("Y-m-d H:i:s");
  2450. $outsend[]=$temp;
  2451. $order = Db::name("order_num")->where(["orderCode"=>$der['orderCode'],"cgdNo"=>$value['cgdNo'],"spuCode"=>$der['spuCode'],"status"=>1])->find();
  2452. if($order==false){
  2453. $order=[
  2454. "orderCode"=>$der['orderCode'],
  2455. "cgdNo"=>$value['cgdNo'],
  2456. "spuCode"=>$der['spuCode'],
  2457. "good_num"=>$der['good_num'],
  2458. "wsend_num"=>$der['good_num']-$temp['send_num'],
  2459. "send_num"=>$temp['send_num'],
  2460. "status"=>1,
  2461. "source"=>1
  2462. ];
  2463. }else{
  2464. $order['wsend_num']-=$temp['send_num'];
  2465. $order['send_num']+=$temp['send_num'];
  2466. }
  2467. if($order['wsend_num']<0){
  2468. return false;
  2469. }
  2470. $up =Db::name("order_num")->save($order);
  2471. if($up==false){
  2472. return false;
  2473. }
  2474. if($send==0){
  2475. break;
  2476. }
  2477. }
  2478. if(empty($temp)){
  2479. return false;
  2480. }
  2481. $insert = Db::name("order_send")->insertAll($outsend);
  2482. if($insert){
  2483. return true;
  2484. }else{
  2485. return false;
  2486. }
  2487. }else{
  2488. $order = Db::name("order_num")->where(["orderCode"=>$der['orderCode'],"spuCode"=>$der['spuCode'],
  2489. "status"=>1])->find();
  2490. if($order==false){
  2491. return false;
  2492. }else{
  2493. if($outinfo['send_num']> $order['wsend_num']){
  2494. return false;
  2495. }
  2496. $order['wsend_num']-=$outinfo['send_num'];
  2497. $order['send_num']+=$outinfo['send_num'];
  2498. if($order['wsend_num']<0){
  2499. return false;
  2500. }
  2501. $up =Db::name("order_num")->save($order);
  2502. if($up==false){
  2503. return false;
  2504. }
  2505. $temp=[];
  2506. $temp['cgdNo'] = $order['cgdNo'];
  2507. $temp['outCode'] = $outinfo['outCode'];
  2508. $temp['send_num'] =$outinfo['send_num'];
  2509. $temp['status'] = 1;
  2510. $temp['addtime']=date("Y-m-d H:i:s");
  2511. $temp['updatetime']=date("Y-m-d H:i:s");
  2512. $insert = Db::name("order_send")->insert($temp);
  2513. if($insert){
  2514. return true;
  2515. }else{
  2516. return false;
  2517. }
  2518. }
  2519. }
  2520. }
  2521. }