Sale.php 128 KB

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