MerchantForm.php 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. // use app\models\Form;
  9. // use app\models\Option;
  10. use app\jobs\CreatedStbzOrderTranJob;
  11. use app\models\Address;
  12. use app\models\AgentFrontBind;
  13. use app\models\AgentFrontErpInventory;
  14. use app\models\AgentFrontErpInventoryLog;
  15. use app\models\AgentGoodsInstallLog;
  16. use app\models\CloudGoodsUpdateLog;
  17. use app\models\DeliveryInfo;
  18. use app\models\District;
  19. use app\models\FrontDeliveryOrder;
  20. use app\models\FrontExpressOrder;
  21. use app\models\LocalDeliveryFreight;
  22. use app\models\Md;
  23. use app\models\OrderGoodsCancel;
  24. use app\models\OrderRefund;
  25. use app\models\SaasUser;
  26. use app\models\Store;
  27. use app\models\StoreForbiddenDirectory;
  28. use app\models\StoreShareMoney;
  29. use app\modules\client\models\v1\order\OrderForm;
  30. use app\utils\CurlHelper;
  31. use app\utils\PrintOrder;
  32. use Yii;
  33. use Exception;
  34. use yii\base\Model;
  35. use app\models\Admin;
  36. use app\models\Supplier;
  37. use app\models\StoreCloud;
  38. use app\models\Goods;
  39. use app\models\Option;
  40. use app\models\CloudGoodsBind;
  41. use app\models\OrderDetail;
  42. use app\models\Order;
  43. use app\models\OrderTransit;
  44. use app\models\Share;
  45. use app\models\Attr;
  46. use yii\base\BaseObject;
  47. use yii\helpers\Json;
  48. use app\jobs\BatchDownGoodsJob;
  49. use app\constants\OptionSetting;
  50. use yii\data\Pagination;
  51. use app\models\Purchase;
  52. use app\models\PurchaseOrder;
  53. use app\models\PurchaseOrderDetail;
  54. class MerchantForm extends Model
  55. {
  56. public $name;
  57. public $password;
  58. public $tel;
  59. public $logo;
  60. public $type;
  61. public $certificate;
  62. public $id_card_reverse;
  63. public $id_card_front;
  64. public $page;
  65. public $limit;
  66. public $token_stroe_cloud_id = 0;
  67. public $supplier_id; //云库供货商id
  68. public $cloud_user_id; //云库会员id
  69. public $price; //变动金额
  70. public $desc; //详情
  71. public $status; //状态
  72. public $cash_id; //提现ID
  73. public $pay_type; //打款方式 1 微信打款 2 支付宝打款 3 银行卡打款 4 其他
  74. public $service_charge; //手续费
  75. public $user_id;
  76. public $cloud_store_id; //云仓商户id
  77. public $attr_id; //属性id 多规格'12-21'
  78. public $goods_id; //商品id
  79. public $province_id; //省
  80. public $city_id; //市
  81. public $district_id; //区
  82. public $address; //详细地址
  83. public $goods_info; //商品详情
  84. public $mch_id; //商户id
  85. public $start_time;
  86. public $end_time;
  87. public $mch_order_no; //订单号
  88. public $order_no; //订单号
  89. public $order_id; //订单id
  90. public $cloud_goods_id; //云仓商品id
  91. public $cloud_attr_id; //云仓属性id
  92. public $cloud_supplier_id; //云仓供货商id
  93. public $apply_data;
  94. public $user_send_express;
  95. public $user_send_express_no;
  96. public $sort;
  97. public $origin;
  98. public $mobile;
  99. public $goods_name;
  100. public $sale_sort;
  101. public $price_sort;
  102. public $is_distribution;
  103. public $cat_id;
  104. public $parent_id;
  105. public $store_id;
  106. public $pic_list;
  107. public $low_price;
  108. public $high_price;
  109. public $send_type;
  110. private $domain = '';
  111. /**
  112. * @var mixed
  113. */
  114. public $order_type;
  115. public $ids;
  116. public $is_pay;
  117. public $order_refund_id;//退款订单id
  118. public $attr;
  119. public $mch_cancel_id;
  120. public $goods_num;
  121. public $md_id;
  122. public function __construct($config = [])
  123. {
  124. parent::__construct($config);
  125. $this->domain = (new OptionSetting)->getCloudDomainName();
  126. }
  127. /**
  128. * @return array the validation rules.
  129. */
  130. public function rules()
  131. {
  132. return [
  133. [['name', 'certificate', 'id_card_reverse','id_card_front','logo','desc','address', 'ids'], 'string', 'max' => 255],
  134. [['password'], 'string', 'max'=>32],
  135. ['is_pay', 'default', 'value' => 1],
  136. [['tel','price','service_charge', 'low_price', 'high_price'],'number'],
  137. [['order_id','district_id','city_id','province_id','goods_id','user_id','pay_type','cash_id','type','page','limit','cloud_user_id','cloud_store_id','status','cloud_goods_id','cloud_attr_id','cloud_supplier_id', 'sort', 'origin', 'mobile', 'sale_sort', 'price_sort', 'is_distribution', 'cat_id', 'order_type', 'parent_id', 'store_id', 'send_type', 'is_pay'],'integer'],
  138. [['goods_info','order_no','mch_order_no','start_time','end_time','attr_id', 'user_send_express', 'user_send_express_no', 'goods_name', 'pic_list'],'string'],
  139. [['apply_data', 'supplier_id', 'order_refund_id'], 'safe']
  140. ];
  141. }
  142. //订单确认收货
  143. public function mchOrderConfirm(){
  144. if (!$this->validate()) {
  145. return $this->getErrorSummary(false)[0];
  146. }
  147. $message = null;
  148. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  149. if (!$cloud_store_token && $message) {
  150. return $message;
  151. }
  152. $mch_order_confirm_url = "/cloud/mch/mchOrderConfirm";
  153. $mch_order_confirm_data = [];
  154. $mch_order_confirm_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  155. $mch_order_confirm_data['order_id'] = $this->order_id; //订单id
  156. $mchOrderConfirmInfo = cloud_post($this->domain.$mch_order_confirm_url,$mch_order_confirm_data);
  157. $mchOrderConfirmInfo = json_decode($mchOrderConfirmInfo,true);
  158. if($mchOrderConfirmInfo['code'] != 0){
  159. $code = 1;//失败
  160. return $this->outPutData($code,$mchOrderConfirmInfo['msg']);
  161. }else{
  162. return $this->outPutData($mchOrderConfirmInfo['code'],$mchOrderConfirmInfo['msg']);
  163. }
  164. }
  165. //订单取消申请
  166. public function mchOrderCancel(){
  167. if (!$this->validate()) {
  168. return $this->getErrorSummary(false)[0];
  169. }
  170. $message = null;
  171. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  172. if (!$cloud_store_token && $message) {
  173. return $message;
  174. }
  175. $mch_order_cancel_url = "/cloud/mch/mchOrderCancel";
  176. $mch_order_cancel_data = [];
  177. $mch_order_cancel_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  178. $mch_order_cancel_data['order_id'] = $this->order_id; //订单id
  179. $this->order_type = 1;
  180. $res = $this->stbzRrfundOrderHandle($cloud_store_token);
  181. debug_log('订单未完成,胜天半子售后' . json_encode($res));
  182. if ($res['code'] === 0) {
  183. $mch_order_cancel_data['stbz_refund_id'] = $res['data'];
  184. }
  185. if ($res['code'] === 1) {
  186. return $res;
  187. }
  188. $mchOrderCancelInfo = cloud_post($this->domain.$mch_order_cancel_url,$mch_order_cancel_data);
  189. $mchOrderCancelInfo = json_decode($mchOrderCancelInfo,true);
  190. if($mchOrderCancelInfo['code'] != 0){
  191. $code = 1;//失败
  192. return $this->outPutData($code,$mchOrderCancelInfo['msg']);
  193. }else{
  194. return $this->outPutData($mchOrderCancelInfo['code'],$mchOrderCancelInfo['msg']);
  195. }
  196. }
  197. //供货单汇总
  198. public function mchOrderSummary(){
  199. if (!$this->validate()) {
  200. return $this->getErrorSummary(false)[0];
  201. }
  202. $message = null;
  203. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  204. if (!$cloud_store_token && $message) {
  205. return $message;
  206. }
  207. $mch_order_list_url = "/cloud/mch/orderShow";
  208. $mch_order_list_data = [];
  209. $mch_order_list_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  210. $mch_order_list_data['page'] = $this->page; //默认为 1
  211. $mch_order_list_data['limit'] = $this->limit; //默认20
  212. $mch_order_list_data['start_time'] = strtotime($this->start_time); //开始时间
  213. $mch_order_list_data['end_time'] = strtotime($this->end_time); //结束时间
  214. $mchOrderListInfo = cloud_post($this->domain.$mch_order_list_url,$mch_order_list_data);
  215. $mchOrderListInfo = json_decode($mchOrderListInfo,true);
  216. if($mchOrderListInfo['code'] != 0){
  217. $code = 1;//失败
  218. return $this->outPutData($code,$mchOrderListInfo['msg']);
  219. }else{
  220. $msg = "获取成功";
  221. return $this->outPutData($mchOrderListInfo['code'],$msg,$mchOrderListInfo['data']);
  222. }
  223. }
  224. //供货单列表
  225. public function mchOrderList(){
  226. if (!$this->validate()) {
  227. return $this->getErrorSummary(false)[0];
  228. }
  229. $message = null;
  230. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  231. if (!$cloud_store_token && $message) {
  232. return $message;
  233. }
  234. $mch_order_list_url = "/cloud/mch/mchOrderList";
  235. $mch_order_list_data = [];
  236. $mch_order_list_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  237. $mch_order_list_data['page'] = $this->page; //默认为 1
  238. $mch_order_list_data['limit'] = $this->limit; //默认20
  239. $mch_order_list_data['status'] = $this->status; //-1 全部 0 待供货商确认 1 供货商已发货 2 已确认收货
  240. $mch_order_list_data['mch_id'] = $this->mch_id; //商户ID
  241. $mch_order_list_data['start_time'] = strtotime($this->start_time); //开始时间
  242. $mch_order_list_data['end_time'] = strtotime($this->end_time); //结束时间
  243. $mchOrderListInfo = cloud_post($this->domain.$mch_order_list_url,$mch_order_list_data);
  244. $mchOrderListInfo = json_decode($mchOrderListInfo,true);
  245. if($mchOrderListInfo['code'] != 0){
  246. $code = 1;//失败
  247. return $this->outPutData($code,$mchOrderListInfo['msg']);
  248. }else{
  249. $msg = "获取成功";
  250. return $this->outPutData($mchOrderListInfo['code'],$msg,$mchOrderListInfo['data']);
  251. }
  252. }
  253. //创建供货单
  254. public function mchSetSubmitOrder(){
  255. if (!$this->validate()) {
  256. return $this->getErrorSummary(false)[0];
  257. }
  258. $message = null;
  259. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  260. if (!$cloud_store_token && $message) {
  261. return $message;
  262. }
  263. $mch_set_submit_order_url = "/cloud/mch/mchSetSubmitOrder";
  264. $mch_set_submit_order_data = [];
  265. $mch_set_submit_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  266. $mch_set_submit_order_data['province_id'] = $this->province_id; //省
  267. $mch_set_submit_order_data['city_id'] = $this->city_id; //市
  268. $mch_set_submit_order_data['district_id'] = $this->district_id; //区
  269. $mch_set_submit_order_data['address'] = $this->address; //地址详情
  270. $mch_set_submit_order_data['name'] = $this->name; //联系人
  271. $mch_set_submit_order_data['mobile'] = $this->tel; //联系电话
  272. $mch_set_submit_order_data['goods_info'] = $this->goods_info; //商品数据
  273. $mchSetSubmitOrderInfo = cloud_post($this->domain.$mch_set_submit_order_url,$mch_set_submit_order_data);
  274. $mchSetSubmitOrderInfo = json_decode($mchSetSubmitOrderInfo,true);
  275. if($mchSetSubmitOrderInfo['code'] != 0){
  276. $code = 1;//失败
  277. return $this->outPutData($code,$mchSetSubmitOrderInfo['msg']);
  278. }else{
  279. $msg = "创建成功";
  280. return $this->outPutData($mchSetSubmitOrderInfo['code'],$msg,$mchSetSubmitOrderInfo['data']);
  281. }
  282. }
  283. //订单确认收货
  284. public function mchPurchaseOrderConfirm($token_store_cloud_id = 0, $store_id = 0, $order_id = 0){
  285. if (!$this->validate()) {
  286. return $this->getErrorSummary(false)[0];
  287. }
  288. $message = null;
  289. $cloud_store_token = get_merchant_token($token_store_cloud_id ?: $this->token_stroe_cloud_id , $store_id ?: 0, $message);
  290. if (!$cloud_store_token && $message) {
  291. return $message;
  292. }
  293. $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
  294. $platform_profit_strategy = json_decode($platform_profit_strategy, true);
  295. $change_rate = 0;
  296. if (!empty($platform_profit_strategy)) {
  297. if (intval($platform_profit_strategy['service_charge_model'])) {
  298. $change_rate = 1;
  299. }
  300. }
  301. $mch_purchase_order_confirm_url = "/cloud/mch/mchPurchaseOrderConfirm";
  302. $mch_purchase_order_confirm_data = [];
  303. $mch_purchase_order_confirm_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  304. $mch_purchase_order_confirm_data['order_id'] = $order_id ?: $this->order_id; //订单id
  305. $mch_purchase_order_confirm_data['change_rate'] = $change_rate;
  306. $mchPurchaseOrderConfirmInfo = cloud_post($this->domain.$mch_purchase_order_confirm_url,$mch_purchase_order_confirm_data);
  307. $mchPurchaseOrderConfirmInfo = json_decode($mchPurchaseOrderConfirmInfo,true);
  308. if($mchPurchaseOrderConfirmInfo['code'] != 0){
  309. $code = 1;//失败
  310. return $this->outPutData($code,$mchPurchaseOrderConfirmInfo['msg']);
  311. }else{
  312. return $this->outPutData($mchPurchaseOrderConfirmInfo['code'],$mchPurchaseOrderConfirmInfo['msg']);
  313. }
  314. }
  315. //订单取消申请
  316. public function mchPurchaseSendcancel(){
  317. if (!$this->validate()) {
  318. return $this->getErrorSummary(false)[0];
  319. }
  320. $message = null;
  321. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  322. if (!$cloud_store_token && $message) {
  323. return $message;
  324. }
  325. $mch_purchase_send_cancel_url = "/cloud/mch/mchPurchaseSendcancel";
  326. $mch_purchase_send_cancel_data = [];
  327. $mch_purchase_send_cancel_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  328. $mch_purchase_send_cancel_data['order_id'] = $this->order_id; //订单id
  329. $mchPurchaseSendcancelInfo = cloud_post($this->domain.$mch_purchase_send_cancel_url,$mch_purchase_send_cancel_data);
  330. $mchPurchaseSendcancelInfo = json_decode($mchPurchaseSendcancelInfo,true);
  331. if($mchPurchaseSendcancelInfo['code'] != 0){
  332. $code = 1;//失败
  333. return $this->outPutData($code,$mchPurchaseSendcancelInfo['msg']);
  334. }else{
  335. return $this->outPutData($mchPurchaseSendcancelInfo['code'],$mchPurchaseSendcancelInfo['msg']);
  336. }
  337. }
  338. //订单申请售后
  339. public function mchOrderRefundApply()
  340. {
  341. if (!$this->validate()) {
  342. return $this->getErrorSummary(false)[0];
  343. }
  344. $order_refund_id = $this->order_refund_id;//商城售后订单ID
  345. $message = null;
  346. // var_dump($this->token_stroe_cloud_id, $this->store_id);die;
  347. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, $this->store_id, $message);
  348. if (!$cloud_store_token && $message) {
  349. return $message;
  350. }
  351. $mch_order_refund_apply_url = "/cloud/mch/mchOrderRefundApply";
  352. $mch_order_refund_apply_data = [];
  353. $data = $this->apply_data;
  354. if (is_string($data['pic_list'])) {
  355. $data['pic_list'] = explode(',', $data['pic_list']);
  356. }
  357. $refund_data = [];
  358. if (!intval($this->order_type)) {
  359. $purchaseOrder = OrderTransit::findOne(['cloud_order_id' => $this->order_id]);
  360. if ($purchaseOrder) {
  361. $cancel_examine_info = json_decode($purchaseOrder->cancel_examine_info, true) ?? [];
  362. $cancel_examine_detail_id = array_column($cancel_examine_info, 'store_order_detail_id');
  363. if ($order_refund_id) {
  364. $orderRefund = OrderRefund::findOne($order_refund_id);
  365. $order_detail_id_arr = json_decode($orderRefund->order_detail_id, true);
  366. foreach ($order_detail_id_arr as $order_detail_id) {
  367. $orderDetail = OrderDetail::findOne($order_detail_id);
  368. if (in_array($order_detail_id, $cancel_examine_detail_id)) {
  369. continue;
  370. }
  371. if ($orderDetail) {
  372. $goods_info = json_decode($orderDetail->goods_info, true);
  373. $attr = json_decode($orderDetail->attr, true);
  374. $attr_id = array_column($attr, 'attr_id');
  375. sort($attr_id);
  376. $goods_info['attr'] = json_decode($goods_info['attr'], true);
  377. foreach ($goods_info['attr'] as $attr_item) {
  378. $item_attr_id = array_column($attr_item['attr_list'], 'attr_id');
  379. sort($item_attr_id);
  380. if (!array_diff($item_attr_id, $attr_id)) {
  381. $refund_data[] = [
  382. 'cloud_goods_id' => $goods_info['cloud_goods_id'],
  383. 'goods_no' => $attr_item['no'],
  384. 'store_order_detail_id' => $order_detail_id
  385. ];
  386. break;
  387. }
  388. }
  389. }
  390. }
  391. $purchaseOrder->cancel_examine_info = json_encode(array_merge($cancel_examine_info, $refund_data));
  392. // $purchaseOrder->cancel_examine_info = json_encode($refund_data);
  393. $purchaseOrder->save();
  394. // $refund_data = json_decode($purchaseOrder->cancel_examine_info, true);
  395. }
  396. }
  397. }
  398. $mch_order_refund_apply_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  399. $mch_order_refund_apply_data['order_id'] = $this->order_id;
  400. $mch_order_refund_apply_data['refund_data'] = json_encode($refund_data);
  401. $mch_order_refund_apply_data['type'] = $data['type'];
  402. $mch_order_refund_apply_data['desc'] = $data['desc'];
  403. $mch_order_refund_apply_data['pic_list'] = json_encode($data['pic_list']);
  404. $mch_order_refund_apply_data['video'] = $data['video'];
  405. // $mch_order_refund_apply_data['refund_price'] = $data['refund_price'];//老板说取消自定义退款金额
  406. $mch_order_refund_apply_data['order_type'] = $this->order_type;
  407. $res = $this->stbzRrfundOrderHandle($cloud_store_token, $data);
  408. // $res = ['code' => 0, 'data' => 52859];
  409. if ($res['code'] === 0) {
  410. $mch_order_refund_apply_data['stbz_refund_id'] = $res['data'];
  411. }
  412. if ($res['code'] === 1) {
  413. return $res;
  414. }
  415. $mchOrderRefundApplyInfo = cloud_post($this->domain.$mch_order_refund_apply_url,$mch_order_refund_apply_data);
  416. $mchOrderRefundApplyInfo = json_decode($mchOrderRefundApplyInfo,true);
  417. if($mchOrderRefundApplyInfo['code'] != 0){
  418. $code = 1;//失败
  419. return $this->outPutData($code,$mchOrderRefundApplyInfo['msg']);
  420. }else{
  421. if ((int)$this->order_type === 1) {
  422. $PurchaseOrder = PurchaseOrder::findOne(['cloud_order_id' => $this->order_id]);
  423. } else {
  424. $PurchaseOrder = OrderTransit::findOne(['cloud_order_id' => $this->order_id]);
  425. }
  426. $PurchaseOrder->cancel_examine = 1;
  427. $PurchaseOrder->cancel_examine_time = (string)time();
  428. if (!$PurchaseOrder->save()) {
  429. return $this->outPutData(1, '订单状态更新失败' . json_encode($PurchaseOrder->errors));
  430. };
  431. return $this->outPutData($mchOrderRefundApplyInfo['code'],$mchOrderRefundApplyInfo['msg']);
  432. }
  433. }
  434. //订单申请售后(邮寄产品)
  435. public function mchOrderRefundSend($order_refund_detail_info = '')
  436. {
  437. if (!$this->validate()) {
  438. return $this->getErrorSummary(false)[0];
  439. }
  440. $message = null;
  441. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  442. if (!$cloud_store_token && $message) {
  443. return $message;
  444. }
  445. $refund_data = [];
  446. if ($order_refund_detail_info) {
  447. $order_refund_detail_info = json_decode($order_refund_detail_info, true);
  448. foreach ($order_refund_detail_info as $order_refund_detail_item) {
  449. $orderTransit = OrderTransit::findOne(['cloud_order_id' => $this->order_id, 'is_delete' => 0]);
  450. $cancel_examine_info = json_decode($orderTransit->cancel_examine_info, true);
  451. $store_order_detail_arr = array_column($cancel_examine_info, 'store_order_detail_id');
  452. $result = array_search($order_refund_detail_item, $store_order_detail_arr);
  453. if ($result !== false) {
  454. $refund_data[] = $cancel_examine_info[$result];
  455. }
  456. }
  457. }
  458. $mch_order_refund_send_url = "/cloud/mch/mchOrderRefundSend";
  459. $mch_order_refund_send_data = [];
  460. $data = $this->apply_data;
  461. $mch_order_refund_send_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  462. $mch_order_refund_send_data['order_id'] = $this->order_id;
  463. $mch_order_refund_send_data['refund_data'] = json_encode($refund_data);
  464. $mch_order_refund_send_data['user_send_express'] = $this->user_send_express;
  465. $mch_order_refund_send_data['user_send_express_no'] = $this->user_send_express_no;
  466. $mch_order_refund_send_data['order_type'] = $this->order_type;
  467. $mch_order_refund_send_data['send_pic_list'] = json_encode($this->pic_list);
  468. $order_data = [
  469. 'order_id' => $this->order_id,
  470. 'status' => $this->order_type
  471. ];
  472. $order_url = "/user/getRefundOrder";
  473. $domain = $this->domain;
  474. $orderInfo = cloud_post($domain. $order_url,
  475. $order_data);
  476. $orderInfo = json_decode($orderInfo,true);
  477. // if(input_params('debug')){
  478. // var_dump($orderInfo);die;
  479. // }
  480. if($orderInfo['code'] == 0){
  481. if ($orderInfo['data']['stbz_refund_id']) {
  482. $urls = [];
  483. $pic_lists = $this->pic_list;
  484. if (!empty($pic_lists)) {
  485. $order_url = "/user/getOrder";
  486. $orderInfo = cloud_post($domain. $order_url,
  487. $order_data);
  488. $orderInfo = json_decode($orderInfo,true);
  489. if($orderInfo['code'] !== 0){
  490. return $orderInfo['msg'];
  491. }
  492. //上传凭证
  493. foreach ($pic_lists as &$pic_list) {
  494. $save_path = $this->saveTempImage($pic_list);
  495. $image = file_get_contents($save_path);
  496. $pic_list = base64_encode($image);
  497. }
  498. $params = array_merge($order_data, [
  499. 'pictures' => $pic_lists
  500. ]);
  501. $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale/picture', $params);
  502. $checkData = \json_decode($checkData, true);
  503. if ((int)$checkData['code'] === 1) {
  504. $urls = $checkData['data']['urls'];
  505. }
  506. }
  507. $company = '';
  508. $logistic = \stbz_client()->getApiResponse('get', '/v2/logistic/firms');
  509. $returnData = \json_decode($logistic, true);
  510. if ($returnData['code'] == 1) {
  511. if($returnData['data']){
  512. foreach($returnData['data'] as $gitem){
  513. if($this->user_send_express == $gitem['name']){
  514. $company = $gitem['code'];
  515. break;
  516. }
  517. }
  518. }
  519. }
  520. $params = [
  521. 'id' => $orderInfo['data']['stbz_refund_id'],
  522. 'sn' => $this->user_send_express_no,
  523. 'company' => $company,
  524. 'vouchers' => $urls
  525. ];
  526. $checkData = \stbz_client()->getApiResponse('patch', '/v2/afterSale', $params);
  527. $checkData = \json_decode($checkData, true);
  528. debug_log('邮寄产品' . json_encode($params) . json_encode($checkData));
  529. if ((int)$checkData['code'] !== 1) {
  530. return [
  531. 'code' => 1,
  532. 'msg' => $checkData['msg'],
  533. 'd' => [$checkData, $params],
  534. ];
  535. }
  536. }
  537. }
  538. $mchOrderRefundSendInfo = cloud_post($this->domain.$mch_order_refund_send_url,$mch_order_refund_send_data);
  539. $mchOrderRefundSendInfo = json_decode($mchOrderRefundSendInfo,true);
  540. if($mchOrderRefundSendInfo['code'] != 0){
  541. $code = 1;//失败
  542. return $this->outPutData($code,$mchOrderRefundSendInfo['msg']);
  543. }else{
  544. return $this->outPutData($mchOrderRefundSendInfo['code'],$mchOrderRefundSendInfo['msg']);
  545. }
  546. }
  547. //售后列表
  548. public function mchOrderRefundList()
  549. {
  550. try {
  551. if (!$this->validate()) {
  552. return $this->getErrorSummary(false)[0];
  553. }
  554. $message = null;
  555. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  556. if (!$cloud_store_token && $message) {
  557. return $message;
  558. }
  559. $mch_order_refund_send_url = "/cloud/mch/mchOrderRefundList";
  560. $mch_order_refund_send_data['access_token'] = $cloud_store_token;
  561. $mch_order_refund_send_data['id'] = $this->order_id;
  562. $mch_order_refund_send_data['page'] = $this->page; //默认为 1
  563. $mch_order_refund_send_data['limit'] = $this->limit; //默认20
  564. $mch_order_refund_send_data['status'] = $this->status;
  565. $mchOrderRefundSendInfo = cloud_post($this->domain.$mch_order_refund_send_url,$mch_order_refund_send_data);
  566. $mchOrderRefundSendInfo = json_decode($mchOrderRefundSendInfo,true);
  567. if($mchOrderRefundSendInfo['code'] != 0){
  568. $code = 1;//失败
  569. return $this->outPutData($code,$mchOrderRefundSendInfo['msg']);
  570. }else{
  571. return $this->outPutData($mchOrderRefundSendInfo['code'],$mchOrderRefundSendInfo['msg'],$mchOrderRefundSendInfo['data']);
  572. }
  573. } catch (\Exception $e) {
  574. return [
  575. 'code' => 1,
  576. 'msg' => $e->getMessage()
  577. ];
  578. }
  579. }
  580. public function mchGetOrderGoods()
  581. {
  582. if (!$this->validate()) {
  583. return $this->getErrorSummary(false)[0];
  584. }
  585. $message = null;
  586. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  587. if (!$cloud_store_token && $message) {
  588. return $message;
  589. }
  590. $mch_purchase_order_list_url = "/cloud/mch/mchGetOrderGoods";
  591. $mch_purchase_order_list_data = [];
  592. $mch_purchase_order_list_data['access_token'] = $cloud_store_token;//获取供货商的token信息
  593. $mch_purchase_order_list_data['status'] = $this->status;//-1 全部 0 待供货商确认 1 供货商已发货 2 已确认收货
  594. //$mch_purchase_order_list_data['mch_id'] = $this->mch_id; //商户ID
  595. if($this->page) $mch_purchase_order_list_data['page'] = $this->page; //默认为 1
  596. if($this->limit) $mch_purchase_order_list_data['limit'] = $this->limit; //默认20
  597. if($this->start_time) $mch_purchase_order_list_data['start_time'] = strtotime($this->start_time); //开始时间
  598. if($this->end_time) $mch_purchase_order_list_data['end_time'] = strtotime($this->end_time); //结束时间
  599. if($this->order_no) $mch_purchase_order_list_data['order_no'] = $this->order_no; //订单号
  600. if($this->origin) $mch_purchase_order_list_data['origin'] = $this->origin; //订单号
  601. if($this->mobile) $mch_purchase_order_list_data['mobile'] = $this->mobile; //订单号
  602. if($this->goods_name) $mch_purchase_order_list_data['goods_name'] = $this->goods_name; //订单号
  603. $mchPurchaseOrderListInfo = cloud_post($this->domain.$mch_purchase_order_list_url,$mch_purchase_order_list_data);
  604. $mchPurchaseOrderListInfo = json_decode($mchPurchaseOrderListInfo,true);
  605. if($mchPurchaseOrderListInfo['code'] != 0){
  606. $code = 1;//失败
  607. return $this->outPutData($code,$mchPurchaseOrderListInfo['msg']);
  608. }else{
  609. $msg = "获取成功";
  610. return $this->outPutData($mchPurchaseOrderListInfo['code'],$msg,$mchPurchaseOrderListInfo['data']);
  611. }
  612. }
  613. //转单列表(无页码限制)
  614. public function getPurchaseOrderList(){
  615. if (!$this->validate()) {
  616. return $this->getErrorSummary(false)[0];
  617. }
  618. $order_id = $this->order_id;
  619. $orderTransit = OrderTransit::find()->where(['order_id' => $order_id])->select('cloud_order_id')->asArray()->all();
  620. $cloud_order_id = array_column($orderTransit, 'cloud_order_id');
  621. $order_data_url = "/user/getPurchaseOrder";
  622. $order_data = [];
  623. if (!$cloud_order_id) {
  624. return [
  625. 'code' => 1,
  626. 'msg' => '未查找到订单信息'
  627. ];
  628. }
  629. $order_data['order_ids'] = implode(',', $cloud_order_id);
  630. $order_data_info = cloud_post($this->domain . $order_data_url, $order_data);
  631. $order_data_info = json_decode($order_data_info, true);
  632. if($order_data_info['code'] != 0){
  633. $code = 1;//失败
  634. return $this->outPutData($code, $order_data_info['msg']);
  635. }else{
  636. $msg = "获取成功";
  637. return $this->outPutData($order_data_info['code'], $msg, $order_data_info['data']);
  638. }
  639. }
  640. //转单列表
  641. public function mchPurchaseOrderList(){
  642. if (!$this->validate()) {
  643. return $this->getErrorSummary(false)[0];
  644. }
  645. $message = null;
  646. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  647. if (!$cloud_store_token && $message) {
  648. return $message;
  649. }
  650. $mch_purchase_order_list_url = "/cloud/mch/mchPurchaseOrderList";
  651. $mch_purchase_order_list_data = [];
  652. $mch_purchase_order_list_data['access_token'] = $cloud_store_token;//获取供货商的token信息
  653. $mch_purchase_order_list_data['status'] = $this->status;//-1 全部 0 待供货商确认 1 供货商已发货 2 已确认收货
  654. //$mch_purchase_order_list_data['mch_id'] = $this->mch_id; //商户ID
  655. if($this->page) $mch_purchase_order_list_data['page'] = $this->page; //默认为 1
  656. if($this->limit) $mch_purchase_order_list_data['limit'] = $this->limit; //默认20
  657. if($this->start_time) $mch_purchase_order_list_data['start_time'] = strtotime($this->start_time); //开始时间
  658. if($this->end_time) $mch_purchase_order_list_data['end_time'] = strtotime($this->end_time); //结束时间
  659. if($this->order_no) $mch_purchase_order_list_data['order_no'] = $this->order_no; //订单号
  660. if($this->origin) $mch_purchase_order_list_data['origin'] = $this->origin; //订单号
  661. if($this->mobile) $mch_purchase_order_list_data['mobile'] = $this->mobile; //订单号
  662. if($this->goods_name) $mch_purchase_order_list_data['goods_name'] = $this->goods_name; //订单号
  663. if($this->mch_order_no){
  664. $orderIds = Order::find()->where(['store_id' => $this->store_id])->andWhere(['like', 'order_no', $this->mch_order_no])->select('id')->column();
  665. if($orderIds){
  666. $mch_purchase_order_list_data['mch_order_ids'] = implode(',', $orderIds);
  667. }
  668. }
  669. $mchPurchaseOrderListInfo = cloud_post($this->domain.$mch_purchase_order_list_url,$mch_purchase_order_list_data);
  670. $mchPurchaseOrderListInfo = json_decode($mchPurchaseOrderListInfo,true);
  671. if($mchPurchaseOrderListInfo['code'] != 0){
  672. $code = 1;//失败
  673. return $this->outPutData($code,$mchPurchaseOrderListInfo['msg']);
  674. }else{
  675. $msg = "获取成功";
  676. foreach ($mchPurchaseOrderListInfo['data']['list'] as &$item) {
  677. //TODO 胜天半子
  678. $token = get_merchant_token();
  679. $result = $this->stbzRrfundOrderHandleInfo($token, $item['id']);
  680. $reasonsType = null;
  681. $returnType = null;
  682. $packageType = null;
  683. $serviceType = null;
  684. if ($result['code'] === 0) {
  685. $reasonsType = $result['data']['reasonsType'];
  686. $returnType = $result['data']['returnType'];
  687. $packageType = $result['data']['packageType'];
  688. $serviceType = $result['data']['serviceType'];
  689. }
  690. $item['reasonsType'] = $reasonsType;
  691. $item['returnType'] = $returnType;
  692. $item['packageType'] = $packageType;
  693. $item['serviceType'] = $serviceType;
  694. $item['is_can_cancel'] = 0;
  695. $item['order'] = Order::findOne($item['platform_order_no']);
  696. //
  697. $order_transit = OrderTransit::findOne(['cloud_order_id' => $item['id'], 'is_delete' => 0]);
  698. if ($order_transit) {
  699. $order_refund = OrderRefund::findOne(['order_id' => $order_transit->order_id]);
  700. if (!$order_refund) {
  701. $open = false;
  702. //显示可售后按钮 且 用户未申请售后的不显示取消售后按钮
  703. if ($item['is_show_refund'] === 1) {
  704. $open = true;
  705. $item['is_show_refund'] = 0;
  706. }
  707. $order = Order::findOne($order_transit->order_id);
  708. if ($order) {
  709. if (intval($order->trade_status) === 1 && $open && intval($item['is_show_refund']) === 0 && in_array($item['status'], [0, 1])) {
  710. $item['is_can_cancel'] = 1;
  711. if (intval($item['is_cancel']) === 1) {
  712. $item['is_can_cancel'] = 0;
  713. $item['is_show_refund'] = 0;
  714. $item['status'] = '-1';
  715. }
  716. }
  717. }
  718. }
  719. }
  720. }
  721. return $this->outPutData($mchPurchaseOrderListInfo['code'],$msg,$mchPurchaseOrderListInfo['data']);
  722. }
  723. }
  724. //检测转单
  725. public function mchGetPurchaseOrder($type = 0){
  726. if(!$this->validate()) {
  727. return $this->getErrorSummary(false)[0];
  728. }
  729. $message = null;
  730. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, $this->store_id, $message);
  731. if (!$cloud_store_token && $message) {
  732. return $message;
  733. }
  734. $order = Order::findOne($this->order_id);
  735. if (empty($order)) {
  736. return [
  737. 'code' => 1,
  738. 'msg' => '订单不存在'
  739. ];
  740. }
  741. if (intval($order->apply_delete) === 1) {
  742. return [
  743. 'code' => 1,
  744. 'msg' => '订单正在申请取消 不可转单'
  745. ];
  746. }
  747. if (intval($order->is_offline) === 1) {
  748. //如果订单自提就发货到商家
  749. $store = Store::findOne($order->store_id);
  750. if ($store) {
  751. $this->province_id = $store->province_id;
  752. $this->city_id = $store->city_id;
  753. $this->district_id = $store->district_id;
  754. $this->address = $store->address;
  755. $this->name = $store->name;
  756. $this->tel = $store->contact_tel;
  757. }
  758. if ($order->md_id > 0) {
  759. $md = Md::findOne(['id' => $order->md_id, 'is_delete' => 0]);
  760. if ($md) {
  761. $this->province_id = $md->province;
  762. $this->city_id = $md->city;
  763. $this->district_id = $md->district;
  764. $this->address = $md->address;
  765. $this->name = $md->name;
  766. $this->tel = $md->mobile;
  767. }
  768. }
  769. }
  770. $OrderDetail = OrderDetail::find()->alias('od')->where(['od.order_id' => $this->order_id])
  771. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  772. ->select('od.id, od.id order_d_id, od.goods_id, od.num, od.total_price price, od.pic pic_url, od.attr, g.attr attrs,
  773. g.name, g.is_front_delivery, g.is_front_centralize, g.cloud_supplier_id, g.cloud_goods_id')
  774. ->asArray()->all();
  775. $checkMdFrontBindArr = [];
  776. $checkMdFrontBindArr['send'] = 'express';
  777. if ($order->is_offline) {
  778. $checkMdFrontBindArr['send'] = 'shop';
  779. }
  780. if ($order->is_delivery) {
  781. $checkMdFrontBindArr['send'] = 'delivery';
  782. }
  783. $goods_info = [];
  784. $price = 0;
  785. foreach ($OrderDetail as $index => $item) {
  786. $item['id'] = $item['goods_id'];
  787. $attr = array_column(json_decode($item['attr'], true), 'attr_id');
  788. sort($attr);
  789. $attrs = json_decode($item['attrs'], true);
  790. foreach ($attrs as $value) {
  791. $goods_attr = array_column($value['attr_list'], 'attr_id');
  792. sort($goods_attr);
  793. if (!array_diff($goods_attr, $attr)) {
  794. $item['attr'] = json_encode($value);
  795. continue;
  796. }
  797. }
  798. unset($OrderDetail[$index]['attrs'], $OrderDetail[$index]['goods_id']);
  799. $goods = CloudGoodsBind::find()->where(['goods_id' => $item['id']])->select('cloud_goods_id id, cloud_supplier_id')->asArray()->one();
  800. if (!empty($goods)) {
  801. $item['id'] = $goods['id'];
  802. $goods_info[] = $item;
  803. $order_detail_ids[] = $item['order_d_id'];
  804. $price += $item['price'];
  805. $supplier = \app\models\Supplier::findOne(['cloud_supplier_id' => $goods['cloud_supplier_id']]);
  806. if($supplier){
  807. $store_goods = Goods::findOne($item['goods_id']);
  808. \app\modules\admin\models\jushuitan\JuShuiTanForm::syncSupplierJstGoodsQty($supplier['id'], $goods['id'], $this->store_id, $store_goods);
  809. } else {
  810. return [
  811. 'code' => 1,
  812. 'msg' => '供货商已经被删除'
  813. ];
  814. }
  815. }
  816. $item_attr = json_decode($item['attr'], true);
  817. $checkMdFrontBindArr['goods_list'][] = [
  818. 'goods_name' => $item['name'],
  819. 'is_front_delivery' => $item['is_front_delivery'],
  820. 'cloud_supplier_id' => $item['cloud_supplier_id'],
  821. 'cloud_goods_id' => $item['cloud_goods_id'],
  822. 'goods_id' => $item['goods_id'],
  823. 'goods_no' => $item_attr['no'],
  824. 'is_front_centralize' => $item['is_front_centralize']
  825. ];
  826. }
  827. $checkMdFrontBindResult = OrderTransit::checkMdFrontBind($checkMdFrontBindArr, $order->md_id);
  828. if ($checkMdFrontBindResult['code'] !== 0) {
  829. return $checkMdFrontBindResult;
  830. }
  831. $mch_get_purchase_order_url = "/cloud/mch/mchSetPurchaseOrder";
  832. $mch_get_purchase_order_data = [];
  833. $mch_get_purchase_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  834. $mch_get_purchase_order_data['province_id'] = $this->province_id; //省
  835. $mch_get_purchase_order_data['city_id'] = $this->city_id; //市
  836. $mch_get_purchase_order_data['district_id'] = $this->district_id; //区
  837. $mch_get_purchase_order_data['address'] = $this->address; //地址详情
  838. $mch_get_purchase_order_data['name'] = $this->name; //联系人
  839. $mch_get_purchase_order_data['mobile'] = $this->tel; //联系电话
  840. $mch_get_purchase_order_data['goods_info'] = json_encode($goods_info); //商品数据
  841. $mch_get_purchase_order_data['platform_order_no'] = $this->order_id; //订单id
  842. $mch_get_purchase_order_data['type'] = 0; //创建
  843. $mch_get_purchase_order_data['is_use_platform_mch'] = $order->is_use_platform_mch; //是否是平台收款账户收款
  844. $mchGetPurchaseOrderInfo = cloud_post($this->domain.$mch_get_purchase_order_url,$mch_get_purchase_order_data);
  845. $mchGetPurchaseOrderInfo = json_decode($mchGetPurchaseOrderInfo,true);
  846. if($mchGetPurchaseOrderInfo['code'] != 0){
  847. $code = 1;//失败
  848. $order->trans_status = 2;
  849. $order->trans_error = $mchGetPurchaseOrderInfo['msg'];
  850. $order->save();
  851. return $this->outPutData($code,$mchGetPurchaseOrderInfo['msg']);
  852. }else{
  853. $total_price = 0;
  854. foreach($mchGetPurchaseOrderInfo['data'] as $key=>$val){
  855. $orderTransitInfo = OrderTransit::find()->where(['order_id'=>$val['platform_order_no'],'cloud_supplier_id'=>$val['supplier']['id'],'is_delete'=>0])->one();
  856. if($orderTransitInfo){
  857. $mchGetPurchaseOrderInfo['data'][$key]['cloud_order_status'] = true; //已转单成功
  858. }else{
  859. $mchGetPurchaseOrderInfo['data'][$key]['cloud_order_status'] = false; //未转单
  860. }
  861. $total_price += $val['pay_price'];
  862. }
  863. //检测未进件时候使用
  864. if ($type) {
  865. if ($order && $order->is_use_platform_mch) {
  866. $store = Store::findOne($order->store_id);
  867. if ($store) {
  868. $store_price = Store::getMoney($store);
  869. $profit = bcmul($order->pay_price, ($store->transfer_profit / 100), 3);
  870. $order_pay_price = bcsub($order->pay_price, $profit, 3);
  871. if ($order_pay_price < $total_price) {
  872. return [
  873. 'code' => 2,
  874. 'msg' => '商品价格可抵扣金额不足'
  875. ];
  876. }
  877. }
  878. }
  879. }
  880. $msg = "获取成功";
  881. return $this->outPutData($mchGetPurchaseOrderInfo['code'],$msg,$mchGetPurchaseOrderInfo['data']);
  882. }
  883. }
  884. //创建转单
  885. public function mchSetPurchaseOrder(){
  886. checkReplay($this->order_id);
  887. $order = Order::findOne($this->order_id);
  888. $is_use_platform_mch = $order->is_use_platform_mch;
  889. // 主要为供应链系统
  890. // 赤店新增逻辑 未进件情况下转单需要从商城余额抵扣金额 订单转单的时候不扣除云仓余额,修改给商家结算的金额即可
  891. // 转单的 如果商城订单价格没云仓订单价格高 商家可结算金额不足 那给商家结算时需要提示充值云仓余额
  892. // 调用此接口时先调用检测 检测提示可抵扣金额不足 就直接以已进件的方式进行扣除云仓金额
  893. $type = 0;
  894. $self_mini = \app\models\Option::get('self_mini', $this->store_id ?? get_store_id(), 'store', 0)['value'];
  895. if ($is_use_platform_mch && \Yii::$app->prod_is_dandianpu() && !$self_mini) {
  896. $type = 1;
  897. }
  898. if (!$type) {
  899. $is_use_platform_mch = 0;
  900. }
  901. $result = $this->mchGetPurchaseOrder($type);
  902. if ($result['code'] !== 0) {
  903. if ($is_use_platform_mch && $result['code'] === 2) {
  904. $is_use_platform_mch = 0;
  905. } else {
  906. return $result;
  907. }
  908. }
  909. if (!$this->validate()) {
  910. return $this->getErrorSummary(false)[0];
  911. }
  912. $order_detail_ids = []; //对应order_detail id
  913. $OrderDetail = OrderDetail::find()->alias('od')->where(['od.order_id' => $this->order_id])
  914. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  915. ->select('od.id order_d_id, od.goods_id, od.num, od.total_price price, od.pic pic_url, od.attr, g.attr attrs, od.goods_name')
  916. ->asArray()->all();
  917. foreach ($OrderDetail as $order_detail_index => $order_detail_item) {
  918. $orderGoodsCancelNum = OrderGoodsCancel::find()->where([
  919. 'order_id' => $this->order_id,
  920. 'order_detail_id' => $order_detail_item['order_d_id'],
  921. 'status' => [
  922. OrderGoodsCancel::STATUS_PASS,
  923. OrderGoodsCancel::STATUS_PAY,
  924. OrderGoodsCancel::STATUS_PAY_FAIL
  925. ]
  926. ])->groupBy('order_detail_id')->select('SUM(num)')->scalar();
  927. $order_detail_item['num'] = bcsub($order_detail_item['num'], $orderGoodsCancelNum);
  928. if ($order_detail_item['num'] <= 0) {
  929. unset($OrderDetail[$order_detail_index]);
  930. }
  931. }
  932. $OrderDetail = array_values($OrderDetail);
  933. $goods_info = [];
  934. // $supplier_arr = [];
  935. $price = 0;
  936. foreach ($OrderDetail as $index => $item) {
  937. //购买的规格
  938. $attr = array_column(json_decode($item['attr'], true), 'attr_id');
  939. sort($attr);
  940. //商品规格
  941. $attrs = json_decode($item['attrs'], true);
  942. foreach ($attrs as $value) {
  943. $goods_attr = array_column($value['attr_list'], 'attr_id');
  944. sort($goods_attr);
  945. if (!array_diff($goods_attr, $attr)) {
  946. $item['attr'] = json_encode($value);
  947. continue;
  948. }
  949. }
  950. unset($OrderDetail[$index]['attrs']);
  951. $goods = CloudGoodsBind::find()->where(['goods_id' => $item['goods_id']])->select('cloud_goods_id id, cloud_supplier_id')->asArray()->one();
  952. // $supplier_arr[] = $goods['cloud_supplier_id'];
  953. if (!empty($goods)) {
  954. $item['id'] = $goods['id'];
  955. $goods_info[] = $item;
  956. $order_detail_ids[] = $item['order_d_id'];
  957. $price += $item['price'];
  958. $supplier = \app\models\Supplier::findOne(['cloud_supplier_id' => $goods['cloud_supplier_id']]);
  959. if($supplier){
  960. $store_goods = Goods::findOne($item['goods_id']);
  961. \app\modules\admin\models\jushuitan\JuShuiTanForm::syncSupplierJstGoodsQty($supplier['id'], $goods['id'], $this->store_id, $store_goods);
  962. } else {
  963. return [
  964. 'code' => 1,
  965. 'msg' => '供货商已经被删除'
  966. ];
  967. }
  968. }
  969. }
  970. //一个订单一个供货商只能创建一次转单
  971. $transitInfo = OrderTransit::find()->where(['order_id'=>$this->order_id, 'is_delete'=>0])->one();
  972. if($transitInfo) {
  973. $code = 1;//失败
  974. $msg = "订单已转单";
  975. return $this->outPutData($code,$msg);
  976. }
  977. //验证订单数据
  978. $orderInfo = Order::find()->with(['detail' => function ($query) {
  979. $query->select("id,goods_id,attr,order_id,num")->asArray();
  980. }])->where(['is_delete' => 0, 'id' => $this->order_id])->asArray()->one();
  981. if(!$orderInfo || !$orderInfo['detail']){
  982. $code = 1;//失败
  983. $msg = "订单数据有误";
  984. return $this->outPutData($code,$msg);
  985. }
  986. $address_data = json_decode($order->address_data, true);
  987. $this->address = $address_data['detail'];
  988. $message = null;
  989. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, $this->store_id, $message);
  990. if (!$cloud_store_token && $message) {
  991. return $message;
  992. }
  993. $spu = [];
  994. foreach ($goods_info as $goods) {
  995. $goods_url = "/goods/getAttrParams";
  996. $param = [
  997. 'id' => $goods['id']
  998. ];
  999. //请求接口
  1000. $domain = $this->domain;
  1001. $goodsInfo = cloud_post($domain . $goods_url, $param);
  1002. $goodsInfo = json_decode($goodsInfo, true);
  1003. if ((int)$goodsInfo['code'] !== 0) {
  1004. return [
  1005. 'code' => 1,
  1006. 'msg' => $goodsInfo['msg']
  1007. ];
  1008. }
  1009. if ($goodsInfo['data']['stbz_goods_id'] > 0) {
  1010. $attr = json_decode($goods['attr'], true);
  1011. $spu[] = [
  1012. 'number' => $goods['num'],
  1013. 'sku' => $attr['no'],
  1014. ];
  1015. }
  1016. }
  1017. if (intval($order->is_offline) === 1) {
  1018. //如果订单自提就发货到商家
  1019. $store = Store::findOne($order->store_id);
  1020. if ($store) {
  1021. $this->province_id = $store->province_id;
  1022. $this->city_id = $store->city_id;
  1023. $this->district_id = $store->district_id;
  1024. $this->address = $store->address;
  1025. }
  1026. if ($order->md_id > 0) {
  1027. $md = Md::findOne(['id' => $order->md_id, 'is_delete' => 0]);
  1028. if ($md) {
  1029. $this->province_id = $md->province;
  1030. $this->city_id = $md->city;
  1031. $this->district_id = $md->district;
  1032. $this->address = $md->address;
  1033. }
  1034. }
  1035. }
  1036. if (intval($order->is_delivery) === 1) {
  1037. //如果订单自提就发货到商家
  1038. $store = Store::findOne($order->store_id);
  1039. if ($store) {
  1040. $this->province_id = $store->province_id;
  1041. $this->city_id = $store->city_id;
  1042. $this->district_id = $store->district_id;
  1043. $this->address = $store->address;
  1044. }
  1045. }
  1046. if (!empty($spu)) {
  1047. [$province, $city, $district] = District::find()->where(['id' => [$this->province_id, $this->city_id, $this->district_id]])
  1048. ->select('name')->column();
  1049. if (empty($province) || empty($city) || empty($district) || empty($this->address)) {
  1050. return [
  1051. 'code' => 1,
  1052. 'msg' => '地址信息错误'
  1053. ];
  1054. }
  1055. $params = [
  1056. 'spu' => $spu,
  1057. 'address' => [
  1058. 'consignee' => $orderInfo['name'],
  1059. 'phone' => $orderInfo['mobile'],
  1060. 'province' => $province,
  1061. 'city' => $city,
  1062. 'area' => $district,
  1063. 'street' => '',
  1064. 'description' => $this->address,
  1065. ]
  1066. ];
  1067. debug_log('胜天半子商品信息' . json_encode($params));
  1068. $checkData = \stbz_client()->getApiResponse('post', '/v2/order/beforeCheck', $params);
  1069. $checkData = \json_decode($checkData, true);
  1070. debug_log('胜天半子下单检测' . json_encode($checkData));
  1071. if ($checkData['code'] != 1) {
  1072. $order->trans_status = 2;
  1073. $order->trans_error = $checkData['msg'];
  1074. $order->save();
  1075. return $checkData;
  1076. }
  1077. }
  1078. $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
  1079. $platform_profit_strategy = json_decode($platform_profit_strategy, true);
  1080. $change_rate = 0;
  1081. if (!empty($platform_profit_strategy)) {
  1082. if (intval($platform_profit_strategy['service_charge_model'])) {
  1083. $change_rate = 1;
  1084. }
  1085. }
  1086. $mch_set_purchase_order_url = "/cloud/mch/mchSetPurchaseOrder";
  1087. $mch_set_purchase_order_data = [];
  1088. $mch_set_purchase_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  1089. $mch_set_purchase_order_data['province_id'] = $this->province_id; //省
  1090. $mch_set_purchase_order_data['city_id'] = $this->city_id; //市
  1091. $mch_set_purchase_order_data['district_id'] = $this->district_id; //区
  1092. $mch_set_purchase_order_data['address'] = $this->address; //地址详情
  1093. $mch_set_purchase_order_data['name'] = $this->name; //联系人
  1094. $mch_set_purchase_order_data['mobile'] = $this->tel; //联系电话
  1095. $mch_set_purchase_order_data['goods_info'] = json_encode($goods_info); //商品数据
  1096. $mch_set_purchase_order_data['platform_order_no'] = $this->order_id; //订单id
  1097. $mch_set_purchase_order_data['type'] = 1; //创建
  1098. $mch_set_purchase_order_data['user_id'] = $order->user_id; //用户id
  1099. $mch_set_purchase_order_data['order_no'] = $order->order_no; //订单号
  1100. $mch_set_purchase_order_data['platform_md_id'] = $order->md_id; //订单号
  1101. $mch_set_purchase_order_data['is_use_platform_mch'] = $is_use_platform_mch; //是否是平台收款账户收款
  1102. $mch_set_purchase_order_data['change_rate'] = $change_rate;
  1103. $mchSetPurchaseOrderInfo = cloud_post($this->domain.$mch_set_purchase_order_url,$mch_set_purchase_order_data);
  1104. debug_log('转单' . $mchSetPurchaseOrderInfo);
  1105. $mchSetPurchaseOrderInfo = json_decode($mchSetPurchaseOrderInfo,true);
  1106. if($mchSetPurchaseOrderInfo['code'] != 0){
  1107. $order->trans_status = 2;
  1108. $order->trans_error = $mchSetPurchaseOrderInfo['msg'];
  1109. $order->save();
  1110. $code = 1;//失败
  1111. return $this->outPutData($code, $mchSetPurchaseOrderInfo['msg']);
  1112. } else {
  1113. $arr = [];
  1114. if ($mchSetPurchaseOrderInfo['data']['order_data']) {
  1115. $order_detail = OrderDetail::find()->alias('od')
  1116. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  1117. ->where(['od.order_id' => $order->id])->select('od.id')->asArray()->all();
  1118. $order_detail_ids = array_column($order_detail, 'id');
  1119. $total_price = 0;
  1120. foreach ($mchSetPurchaseOrderInfo['data']['order_data'] as $index => $order_data) {
  1121. foreach ($order_data as $key => $value) {
  1122. $cloud_order_goods_id = array_column($value['goods_list'], 'id');
  1123. $order_detail_ = OrderDetail::find()->alias('od')
  1124. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  1125. ->where(['od.order_id' => $order->id, 'g.cloud_goods_id' => $cloud_order_goods_id])
  1126. ->select('od.id, g.name, g.cloud_goods_id')->asArray()->all();
  1127. $order_detail_ids_ = array_column($order_detail_, 'id');
  1128. $orderTransit = new OrderTransit();
  1129. $orderTransit->province_id = $this->province_id;
  1130. $orderTransit->city_id = $this->city_id;
  1131. $orderTransit->district_id = $this->district_id;
  1132. $orderTransit->address = $this->address;
  1133. $orderTransit->name = $this->name;
  1134. $orderTransit->tel = $this->tel;
  1135. $orderTransit->goods_info = json_encode($goods_info);
  1136. $orderTransit->order_id = $order->id;
  1137. $orderTransit->cloud_supplier_id = $value['supplier_id'];
  1138. $orderTransit->created_at = time();
  1139. $orderTransit->order_detail_ids = json_encode($order_detail_ids);
  1140. $orderTransit->cloud_order_id = $value['id'];
  1141. $orderTransit->cloud_order_no = $value['order_no'];
  1142. $orderTransit->deleted_at = 0;
  1143. $orderTransit->order_price = $value['pay_price'];
  1144. $orderTransit->is_use_platform_mch = $is_use_platform_mch;
  1145. $orderTransit->store_id = $order->store_id;
  1146. $orderTransit->goods_send_type = $value['goods_send_type'];
  1147. $orderTransit->save();
  1148. debug_log(json_encode($orderTransit->errors));
  1149. $supplier = Supplier::findOne(['cloud_supplier_id' => $value['supplier_id'], 'is_delete' => 0]);
  1150. if (in_array(intval($value['goods_send_type']), [5, 6])) {
  1151. //获取商城供货商
  1152. $supplierAgentFrontBind = AgentFrontBind::find()->where([
  1153. 'is_delete' => 0,
  1154. 'status' => 1,
  1155. 'type' => AgentFrontBind::TYPE_SUPPLIER,
  1156. 'type_id' => $supplier->id
  1157. ])->select('front_agent_admin_id')->column();
  1158. debug_log('供货商绑定仓库');
  1159. debug_log($value['supplier_id']);
  1160. debug_log($supplierAgentFrontBind);
  1161. $md_id = $order->md_id;
  1162. if ($md_id <= 0 || !$order->is_offline) {
  1163. $mdAgentFrontBind = AgentFrontBind::find()->where([
  1164. 'type' => AgentFrontBind::TYPE_STORE,
  1165. 'type_id' => $order->store_id,
  1166. 'is_delete' => 0,
  1167. 'status' => 1,
  1168. 'front_agent_admin_id' => $supplierAgentFrontBind,
  1169. 'is_cloud_delivery' => AgentFrontBind::IS_CLOUD_DELIVERY_TRUE
  1170. ])->select('md_id, front_agent_admin_id')->orderBy('md_id ASC')
  1171. ->asArray()->one();
  1172. if (!$mdAgentFrontBind) {
  1173. $address_data = json_decode($order->address_data ?: '', true) ?: [];
  1174. $mdAgentFrontBindArr = AgentFrontBind::find()->where([
  1175. 'type' => AgentFrontBind::TYPE_STORE,
  1176. 'type_id' => $order->store_id,
  1177. 'is_delete' => 0,
  1178. 'status' => 1,
  1179. 'front_agent_admin_id' => $supplierAgentFrontBind
  1180. ])->select('md_id, front_agent_admin_id')->orderBy('md_id ASC')
  1181. ->asArray()->all();
  1182. $mdAgentFrontBind = $mdAgentFrontBindArr[0];
  1183. debug_log(['mdAgentFrontBindArr' => $mdAgentFrontBindArr, 'address' => $address_data]);
  1184. if (!empty($address_data)) {
  1185. //如果订单中存在经纬度信息 就查询经纬度距离最近的仓库
  1186. $front_agent_admin_id_arr = array_column($mdAgentFrontBindArr, 'front_agent_admin_id');
  1187. debug_log(['front_agent_admin_id_arr' => $front_agent_admin_id_arr]);
  1188. $admin_table = Admin::tableName();
  1189. $admin_id_str = implode(',', $front_agent_admin_id_arr);
  1190. $sql = "SELECT id, acos(cos({$address_data['latitude']}*pi()/180 )*cos(lat*pi()/180)*cos({$address_data['longitude']}*pi()/180 -lng*pi()/180)+sin({$address_data['latitude']}*pi()/180 )*sin(lat*pi()/180))*6370996.81 as
  1191. distance FROM {$admin_table} WHERE `is_delete`=0 AND `id`in ({$admin_id_str}) ORDER BY distance ASC, id ASC";
  1192. $admin_info = \Yii::$app->db->createCommand($sql)->queryOne();
  1193. debug_log(['admin_info_arr' => \Yii::$app->db->createCommand($sql)->queryAll()]);
  1194. if ($admin_info) {
  1195. //如果能查询到就获得到一个仓库信息 获取不到就还是使用门店正序排列查询
  1196. $mdAgentFrontBind = AgentFrontBind::find()->where([
  1197. 'type' => AgentFrontBind::TYPE_STORE,
  1198. 'type_id' => $order->store_id,
  1199. 'is_delete' => 0,
  1200. 'status' => 1,
  1201. 'front_agent_admin_id' => $admin_info['id']
  1202. ])->select('md_id, front_agent_admin_id')->asArray()->one();
  1203. }
  1204. }
  1205. }
  1206. } else {
  1207. $mdAgentFrontBind = AgentFrontBind::find()->where([
  1208. 'md_id' => $md_id,
  1209. 'is_delete' => 0,
  1210. 'status' => 1,
  1211. 'front_agent_admin_id' => $supplierAgentFrontBind
  1212. ])->select('md_id, front_agent_admin_id')
  1213. ->orderBy('md_id ASC')->asArray()->one();
  1214. }
  1215. if (!$mdAgentFrontBind) {
  1216. debug_log('未找到供货商与门店绑定的同一个仓库');
  1217. goto mdAgentFrontBindEND;
  1218. }
  1219. //扣减仓库库存
  1220. foreach ($value['goods_list'] as $cloud_goods_item) {
  1221. $cloud_goods_attr = json_decode($cloud_goods_item['attr'], true);
  1222. $cloud_goods_attr_id = array_column($cloud_goods_attr['attr_list'], 'attr_id');
  1223. sort($cloud_goods_attr_id);
  1224. //转单后扣减仓库库存 调用供货商修改库存接口 数量传0用作更新商城库存
  1225. $erpInventoryArray = AgentFrontErpInventory::find()->where(['goods_id' => $cloud_goods_item['id'], 'front_agent_admin_id' => $mdAgentFrontBind['front_agent_admin_id']])
  1226. ->asArray()->all();
  1227. foreach ($erpInventoryArray as $erpInventory) {
  1228. $attr_ids = explode(',', $erpInventory['attr_ids']);
  1229. sort($attr_ids);
  1230. if (!array_diff($cloud_goods_attr_id, $attr_ids)) {
  1231. $ei = AgentFrontErpInventory::findOne($erpInventory['id']);
  1232. if ($ei) {
  1233. AgentFrontErpInventory::logSave($ei, -$cloud_goods_item['num'], AgentFrontErpInventoryLog::LOG_TYPE_ORDER, $value['id'], $value['order_no']);
  1234. }
  1235. break;
  1236. }
  1237. }
  1238. }
  1239. if (intval($value['goods_send_type']) === 5) {
  1240. $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]);
  1241. if (!$delivery_info) {
  1242. //不存在应该是门店自提订单
  1243. $goods_list = [];
  1244. foreach ($order_detail_ as $order_goods_item) {
  1245. foreach ($value['goods_list'] as $cloud_goods_item) {
  1246. if ($order_goods_item['cloud_goods_id'] == $cloud_goods_item['id']) {
  1247. $attr = json_decode($cloud_goods_item['attr'], true);
  1248. $goods_list[] = [
  1249. 'name' => $order_goods_item['goods_name'],
  1250. 'price' => $attr['price'],
  1251. 'pic' => $attr['pic'],
  1252. 'num' => $attr['num'],
  1253. 'weight' => $attr['weight'],
  1254. ];
  1255. }
  1256. }
  1257. }
  1258. $type = LocalDeliveryFreight::TYPE_LOCAL;
  1259. //将门店地址传入
  1260. $mdInfo = Md::findOne($order->md_id);
  1261. $address = [
  1262. 'latitude' => $mdInfo->latitude,
  1263. 'longitude' => $mdInfo->longitude
  1264. ];
  1265. $getFreight = LocalDeliveryFreight::getFreight($order->store_id, $address, '', $type, $goods_list, 0, DeliveryInfo::IS_FRONT_DELIVERY_YSE, $mdAgentFrontBind['front_agent_admin_id']);
  1266. if ($getFreight['code'] != 0) {
  1267. return $getFreight;
  1268. }
  1269. $freight = $getFreight['data'];
  1270. // 存预下单表
  1271. $delivery_info = new DeliveryInfo();
  1272. $delivery_info->store_id = $order->store_id;
  1273. $delivery_info->order_no = $order->order_no;
  1274. $delivery_info->fee = $freight;
  1275. $delivery_info->delivery_type = 0;
  1276. $delivery_info->created_at = time();
  1277. $delivery_info->is_local = DeliveryInfo::IS_LOCAL_YSE;
  1278. $delivery_info->is_store_delivery_type = 0;
  1279. $delivery_info->is_front_delivery = DeliveryInfo::IS_FRONT_DELIVERY_YSE;//是否是仓库配送订单
  1280. $serial_num = DeliveryInfo::find()->where(['>', 'created_at', strtotime(date('Y-m-d'))])->orderBy('serial_num desc')->asArray()->one()['serial_num'];
  1281. $delivery_info->serial_num = $serial_num > 0 ? $serial_num + 1 : 1;
  1282. if (!$delivery_info->save()) {
  1283. debug_log('云仓转单后仓库配送添加同城配送订单失败' . json_encode($delivery_info->errors, JSON_UNESCAPED_UNICODE));
  1284. goto mdAgentFrontBindEND;
  1285. }
  1286. $setting = json_decode(Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, 0, '', '{}')['value'], true);
  1287. $delivery_time = date('Y-m-d H:i:s', time() + 90 * 60);
  1288. if (!empty($setting['default_time']) && !empty($setting['default_time']['value'])) {
  1289. $delivery_time = date('Y-m-d H:i:s', time() + ($setting['default_time']['value'] * 60));
  1290. }
  1291. $order->delivery_time = strtotime($delivery_time);
  1292. $order->save();
  1293. }
  1294. //增加仓库配送订单
  1295. $frontDeliveryOrder = new FrontDeliveryOrder();
  1296. $frontDeliveryOrder->front_agent_admin_id = $mdAgentFrontBind['front_agent_admin_id'];
  1297. $frontDeliveryOrder->cloud_order_id = $value['id'];
  1298. $frontDeliveryOrder->delivery_info_id = $delivery_info->id;
  1299. $frontDeliveryOrder->order_id = $order->id;
  1300. $frontDeliveryOrder->store_id = $order->store_id;
  1301. $frontDeliveryOrder->cloud_supplier_id = $value['supplier_id'];
  1302. if (!$frontDeliveryOrder->save()) {
  1303. debug_log('云仓转单后仓库配送添加仓库配送订单记录失败' . json_encode($frontDeliveryOrder->errors, JSON_UNESCAPED_UNICODE));
  1304. }
  1305. }
  1306. if (intval($value['goods_send_type']) === 6) {
  1307. //增加仓库配送订单
  1308. $frontExpressOrder = new FrontExpressOrder();
  1309. $frontExpressOrder->front_agent_admin_id = $mdAgentFrontBind['front_agent_admin_id'];
  1310. $frontExpressOrder->cloud_order_id = $value['id'];
  1311. $frontExpressOrder->order_id = $order->id;
  1312. $frontExpressOrder->store_id = $order->store_id;
  1313. $frontExpressOrder->cloud_supplier_id = $value['supplier_id'];
  1314. if (!$frontExpressOrder->save()) {
  1315. debug_log('云仓转单后仓库添加仓库快递订单记录失败' . json_encode($frontExpressOrder->errors, JSON_UNESCAPED_UNICODE));
  1316. }
  1317. }
  1318. }
  1319. mdAgentFrontBindEND:
  1320. $arr[] = $orderTransit->id;
  1321. $supplier_form = new SupplierForm();
  1322. $supplier_form->order_id = (string)$value['id'];
  1323. $auto = 0;
  1324. if ($this->store_id) {
  1325. $auto = 1;
  1326. }
  1327. $res = $supplier_form->supplierPurchaseOrderList($orderTransit->cloud_supplier_id, $auto);
  1328. debug_log('supplierPurchaseOrderList' . json_encode($res));
  1329. if ($res['code'] === 0) {
  1330. $cloud_order = $res['data']['list'][0];
  1331. queue_push(new CreatedStbzOrderTranJob(['data' => $cloud_order]));
  1332. } else {
  1333. return $res;
  1334. }
  1335. $total_price += $value['pay_price'];
  1336. try {
  1337. $printer_order = new PrintOrder(0, $value['id'], 'pay', 0, 0, 1, 0, $supplier->id);
  1338. $printer_order->print_order();
  1339. } catch (\Exception $e) {
  1340. debug_log([
  1341. 'event' => 'mchSetPurchaseOrder',
  1342. 'message' => $e->getMessage(),
  1343. 'line' => $e->getLine(),
  1344. 'file' => $e->getFile()
  1345. ], 'cloud_printer.log');
  1346. }
  1347. }
  1348. }
  1349. // $del_order = Order::findOne($old_order['id']);
  1350. // $del_order->is_delete = 1;
  1351. // $del_order->save();
  1352. }
  1353. debug_log('order_transit_id' . json_encode($arr));
  1354. debug_log('order_detail_ids' . json_encode($order_detail_ids));
  1355. if(count($order_detail_ids) > 0 ){
  1356. foreach ($order_detail_ids as $index => $value) {
  1357. $OrderDetail = OrderDetail::find()->where(["id"=>$value,"is_delete"=>0])->one();
  1358. if($OrderDetail){
  1359. $OrderDetail->order_transit_id = $arr[$index] ? : ($arr[0] ?: 0);
  1360. $OrderDetail->save();
  1361. }
  1362. }
  1363. }
  1364. //上门安装逻辑
  1365. $res = $this->isNeedInstall($this->order_id, 0, true);
  1366. if ($res['code'] !== 0) {
  1367. return $res;
  1368. }
  1369. $OrderTransit = OrderTransit::findOne(['order_id' => $order->id]);
  1370. if ($OrderTransit) {
  1371. $order->trans_status = 1;
  1372. }
  1373. $order->trans_error = '';
  1374. $order->save();
  1375. $msg = "创建成功";
  1376. return $this->outPutData($mchSetPurchaseOrderInfo['code'],$msg,$mchSetPurchaseOrderInfo['data']);
  1377. }
  1378. }
  1379. //获取云仓产品更新记录
  1380. public function mchGetCloudGoodsUpdateLog() {
  1381. try {
  1382. $where = [
  1383. 'cgl.store_id' => get_store_id(),
  1384. 'g.is_delete' => 0
  1385. ];
  1386. if (in_array($this->status, [1, 2])) {
  1387. $where['is_update'] = (int)$this->status === 1 ? 1 : 0;
  1388. }
  1389. $query = CloudGoodsUpdateLog::find()->alias('cgl')
  1390. ->leftJoin(['g' => Goods::tableName()], 'cgl.cloud_goods_id = g.cloud_goods_id')
  1391. ->leftJoin(['s' => Supplier::tableName()], 'cgl.cloud_supplier_id = s.cloud_supplier_id')
  1392. ->select('cgl.id, g.cloud_goods_id, g.name, g.cover_pic, s.supplier_name, s.logo, cgl.is_audit, cgl.is_update, s.phone, cgl.created_at, cgl.updated_at')
  1393. ->where($where)
  1394. ->orderBy(['cgl.created_at' => SORT_DESC, 'cgl.updated_at' => SORT_DESC])->groupBy(['g.cloud_goods_id', 'cgl.is_update', 'cgl.is_audit']);
  1395. // var_dump($query->createCommand()->getRawSql());die;
  1396. $list = pagination_make($query);
  1397. foreach ($list['list'] as &$value) {
  1398. $value['is_audit'] *= 1;
  1399. $value['is_update'] *= 1;
  1400. }
  1401. return [
  1402. 'code' => 0,
  1403. 'msg' => 'success',
  1404. 'data' => [
  1405. 'data' => $list['list'],
  1406. 'pageNo' => $list['pageNo'],
  1407. 'totalCount' => $list['totalCount']
  1408. ]
  1409. ];
  1410. } catch (\Exception $e) {
  1411. return [
  1412. 'code' => 1,
  1413. 'msg' => $e->getMessage()
  1414. ];
  1415. }
  1416. }
  1417. //获取绑定商品列表
  1418. public function mchGetGoodsBindingList(){
  1419. if (!$this->validate()) {
  1420. return $this->getErrorSummary(false)[0];
  1421. }
  1422. $message = null;
  1423. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  1424. if (!$cloud_store_token && $message) {
  1425. return $message;
  1426. }
  1427. $mch_get_goods_bind_url = "/cloud/mch/mchGetGoodsBinding";
  1428. $mch_get_goods_bind_data = [];
  1429. $mch_get_goods_bind_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  1430. if($this->page) $mch_get_goods_bind_data['page'] = $this->page; //默认为 1
  1431. if($this->limit) $mch_get_goods_bind_data['limit'] = $this->limit; //默认20
  1432. if($this->goods_id) $mch_get_goods_bind_data['goods_id'] = $this->goods_id; //订单号
  1433. $mchGetGoodsBindingInfo = cloud_post($this->domain.$mch_get_goods_bind_url,$mch_get_goods_bind_data);
  1434. $mchGetGoodsBindingInfo = json_decode($mchGetGoodsBindingInfo,true);
  1435. if($mchGetGoodsBindingInfo['code'] != 0){
  1436. $code = 1;//失败
  1437. return $this->outPutData($code,$mchGetGoodsBindingInfo['msg']);
  1438. }else{
  1439. $msg = "获取成功";
  1440. //组装本地商城数据
  1441. if(count($mchGetGoodsBindingInfo['data'])>0){
  1442. $shop_goods_ids = $shop_goods_attr_ids = [];
  1443. foreach($mchGetGoodsBindingInfo['data']['list'] as $key=>$val){
  1444. if($val['goods_id']){
  1445. foreach(explode("-",$val['goods_id']) as $shopKey=>$shopVal){
  1446. if($shopKey == 0){
  1447. $shop_goods_ids[] = $shopVal;
  1448. $mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_id'] = $shopVal;
  1449. }else{
  1450. $shop_goods_attr_ids[] = $shopVal;
  1451. $mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_attr_ids'][] = $shopVal;
  1452. }
  1453. }
  1454. }
  1455. }
  1456. $goodsInfo = Goods::find()->where(['id'=>array_unique($shop_goods_ids),'is_delete'=>0])->all();
  1457. $newGoodsInfo = $newAttrInfo = [];
  1458. foreach($goodsInfo as $val){
  1459. $newGoodsInfo[$val->attributes['id']] = $val->attributes;
  1460. }
  1461. $attrInfo = Attr::find()->where(['id'=>array_unique($shop_goods_attr_ids),'is_delete'=>0])->all();
  1462. foreach($attrInfo as $val){
  1463. $newAttrInfo[$val->attributes['id']] = $val->attributes;
  1464. }
  1465. foreach($mchGetGoodsBindingInfo['data']['list'] as $key=>$val){
  1466. if(isset($newGoodsInfo[$mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_id']])){
  1467. $mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_info'] = $newGoodsInfo[$mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_id']];
  1468. }
  1469. if(isset($mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_attr_ids']) ){
  1470. foreach($mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_attr_ids'] as $attrVal){
  1471. if(isset($newAttrInfo[$attrVal])){
  1472. $mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_attr_info'][] = $newAttrInfo[$attrVal];
  1473. }
  1474. }
  1475. }
  1476. }
  1477. }
  1478. return $this->outPutData($mchGetGoodsBindingInfo['code'],$msg,$mchGetGoodsBindingInfo['data']);
  1479. }
  1480. }
  1481. // //获取绑定商品列表
  1482. // public function mchSetGoodsBindingList(){
  1483. // $store_id = get_store_id();
  1484. // $query = CloudGoodsBind::find()->where(['store_id'=>$store_id,'is_delete'=>0]);
  1485. // $count = $query->count();
  1486. // //使用总数来创建一个分页对象
  1487. // $pagination = new Pagination(['totalCount' => $count]);
  1488. // //使用分页对象来填充 limit 子句并取得文章数据
  1489. // $articles = $query->offset($this->page-1)
  1490. // ->limit($this->limit)
  1491. // ->all();
  1492. // $attr_ids = [];
  1493. // $goods_ids = [];
  1494. // foreach($articles as $val){
  1495. // $attrInfo = explode("-",$val->attributes['attr_id']);
  1496. // $attr_ids = array_merge($attr_ids,$attrInfo);
  1497. // $goods_ids[] = $val->attributes['goods_id'];
  1498. // }
  1499. // $goodsInfo = Goods::find()->where(['id'=>$goods_ids,'is_delete'=>0])->all();
  1500. // $attrInfo = Attr::find()->where(['id'=>$attr_ids,'is_delete'=>0])->all();
  1501. // $newAttrInfo = [];
  1502. // foreach($attrInfo as $attrVal){
  1503. // $newAttrInfo[$attrVal->attributes['id']] = $attrVal->attributes;
  1504. // }
  1505. // $dataInfo = [];
  1506. // $i = 0;
  1507. // foreach($articles as $key=>$val){
  1508. // $dataInfo[$i]['goods_id'] = $val->attributes['goods_id'];
  1509. // $dataInfo[$i]['cloud_attr_id'] = $val->attributes['cloud_attr_id'];
  1510. // foreach(explode("-",$val->attributes['attr_id']) as $attrVal){
  1511. // $temp =[];
  1512. // $temp[] = $newAttrInfo[$attrVal];
  1513. // }
  1514. // $dataInfo[$i]['attr'] = $temp;
  1515. // foreach($goodsInfo as $goodsVal){
  1516. // if($val->attributes['goods_id'] == $goodsVal->attributes['id']){
  1517. // $dataInfo[$i]['goods_info'] = $goodsVal->attributes;
  1518. // }
  1519. // }
  1520. // $i++;
  1521. // }
  1522. // $messageInfo = [];
  1523. // $messageInfo['count'] = $count;
  1524. // $messageInfo['page'] = $this->page;
  1525. // $messageInfo['limit'] = $this->limit;
  1526. // $messageInfo['data'] = $dataInfo;
  1527. // $code = 0;//失败
  1528. // $msg = '获取绑定信息列表';
  1529. // return $this->outPutData($code,$msg,$messageInfo);
  1530. // }
  1531. //导入商品
  1532. public function mchGoodsImport($goodsInfo,$goods_id, $store_id = 0){
  1533. try {
  1534. if(!$goodsInfo){
  1535. $code = 1;//失败
  1536. $msg = '导入失败';
  1537. return $this->outPutData($code,$msg);
  1538. }
  1539. $store_id = !empty($store_id) ? $store_id : get_store_id();
  1540. $cloudBind = CloudGoodsBind::findOne(['cloud_goods_id' => $goods_id, 'store_id' => $store_id, 'is_delete' => 0]) ?: new CloudGoodsBind();
  1541. $message = null;
  1542. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, $store_id, $message);
  1543. if (!$cloud_store_token && $message) {
  1544. return $message;
  1545. }
  1546. $mch_set_goods_bind_url = "/cloud/mch/mchSetGoodsBinding";
  1547. foreach($goodsInfo as $val){
  1548. $mch_set_goods_bind_data = [];
  1549. $mch_set_goods_bind_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  1550. $mch_set_goods_bind_data['goods_id'] = $goods_id;
  1551. $mch_set_goods_bind_data['status'] = 1; //1 绑定 0 解除绑定
  1552. $mchSetGoodsBindInfo =cloud_post($this->domain.$mch_set_goods_bind_url,$mch_set_goods_bind_data);
  1553. $mchSetGoodsBindInfo = json_decode($mchSetGoodsBindInfo,true);
  1554. if($mchSetGoodsBindInfo['code'] == 0){
  1555. $cloudBind->cloud_goods_id = $goods_id;
  1556. $cloudBind->cloud_supplier_id = $val['cloud_supplier_id'];
  1557. $cloudBind->store_id = $store_id;
  1558. $cloudBind->goods_id = $val['goods_id'];
  1559. $cloudBind->created_at = time();
  1560. if (!$cloudBind->save()) {
  1561. $code = 1;//失败
  1562. $msg = '导入失败:' . $cloudBind->errors;
  1563. return $this->outPutData($code,$msg);
  1564. };
  1565. } else {
  1566. $code = 1;//失败
  1567. $msg = '导入失败:' . $mchSetGoodsBindInfo['msg'];
  1568. return $this->outPutData($code,$msg);
  1569. }
  1570. }
  1571. $log = CloudGoodsUpdateLog::findOne(['cloud_goods_id' => $goods_id, 'store_id' => (!empty($store_id) ? $store_id : get_store_id()), 'is_audit' => 1, 'is_update' => 0]);
  1572. if ($log) {
  1573. $log->is_update = 1;
  1574. $log->updated_at = time();
  1575. $log->save();
  1576. }
  1577. $code = 0;//失败
  1578. $msg = '导入成功';
  1579. return $this->outPutData($code, $msg);
  1580. } catch (\Exception $e) {
  1581. debug_log($e->getMessage());
  1582. return $this->outPutData(1, $e->getMessage());
  1583. }
  1584. }
  1585. //绑定商品
  1586. public function mchSetGoodsBinding(){
  1587. if (!$this->validate()) {
  1588. return $this->getErrorSummary(false)[0];
  1589. }
  1590. $goodsInfo =Goods::find()->where(['id' => $this->goods_id,'is_delete'=>0])->one();
  1591. if(!$goodsInfo || !$goodsInfo->attributes['attr']){
  1592. $code = 1;//失败
  1593. $msg = '商城商品信息不存在';
  1594. return $this->outPutData($code,$msg);
  1595. }
  1596. $attrInfo = json_decode($goodsInfo->attributes['attr'],true);
  1597. $is_ok = false;
  1598. foreach($attrInfo as $attr){
  1599. //拼接多规格商品
  1600. $good_attr = '';
  1601. foreach($attr['attr_list'] as $attrVal){
  1602. $good_attr .= $attrVal['attr_id']."-";
  1603. }
  1604. if(substr($good_attr,0,-1) == $this->attr_id) $is_ok = true;
  1605. }
  1606. if(!$is_ok){
  1607. $code = 1;//失败
  1608. $msg = '商城商品信息不存在';
  1609. return $this->outPutData($code,$msg);
  1610. }
  1611. //创建关系
  1612. $bindInfo = CloudGoodsBind::find()->where(['goods_id'=>$this->goods_id,'attr_id'=>$this->attr_id,'is_delete'=>0])->one();
  1613. if($this->status==1 && $bindInfo){
  1614. $code = 1;//失败
  1615. $msg = '商城商品信息只能绑定一个商品';
  1616. return $this->outPutData($code,$msg);
  1617. }
  1618. $message = null;
  1619. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  1620. if (!$cloud_store_token && $message) {
  1621. return $message;
  1622. }
  1623. $mch_set_goods_bind_url = "/cloud/mch/mchSetGoodsBinding";
  1624. $mch_set_goods_bind_data = [];
  1625. $mch_set_goods_bind_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  1626. $mch_set_goods_bind_data['goods_id'] = $this->goods_id."-".$this->attr_id; //本地商城的唯一id(商品ID商品属性ID)
  1627. $mch_set_goods_bind_data['attr_id'] = $this->cloud_attr_id; //规格ID
  1628. $mch_set_goods_bind_data['status'] = $this->status; //1 绑定 0 解除绑定
  1629. $t = Yii::$app->db->beginTransaction();
  1630. if($this->status==1){ //新增逻辑
  1631. $cloudBind = new CloudGoodsBind();
  1632. $cloudBind->cloud_goods_id = $this->cloud_goods_id;
  1633. $cloudBind->cloud_attr_id = $this->cloud_attr_id;
  1634. $cloudBind->cloud_supplier_id = $this->cloud_supplier_id;
  1635. $cloudBind->store_id = get_store_id();
  1636. $cloudBind->goods_id = $this->goods_id;
  1637. $cloudBind->attr_id = $this->attr_id;
  1638. $cloudBind->created_at = time();
  1639. $cloudBind->save();
  1640. }else{ //删除绑定逻辑
  1641. if(!$bindInfo){
  1642. $code = 1;//失败
  1643. $msg = '不存在绑定商品信息';
  1644. return $this->outPutData($code,$msg);
  1645. }
  1646. $bindInfo->is_delete = 1;
  1647. $bindInfo->deleted_at = time();
  1648. $bindInfo->save();
  1649. }
  1650. $mchSetGoodsBindInfo = cloud_post($this->domain.$mch_set_goods_bind_url,$mch_set_goods_bind_data);
  1651. $mchSetGoodsBindInfo = json_decode($mchSetGoodsBindInfo,true);
  1652. if($mchSetGoodsBindInfo['code'] != 0){
  1653. $t->rollBack();
  1654. $code = 1;//失败
  1655. return $this->outPutData($code,$mchSetGoodsBindInfo['msg']);
  1656. }else{
  1657. $t->commit();
  1658. return $this->outPutData($mchSetGoodsBindInfo['code'],$mchSetGoodsBindInfo['msg']);
  1659. }
  1660. }
  1661. //余额记录
  1662. public function mchGetBalanceLog(){
  1663. if (!$this->validate()) {
  1664. return $this->getErrorSummary(false)[0];
  1665. }
  1666. $message = null;
  1667. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  1668. if (!$cloud_store_token && $message) {
  1669. return $message;
  1670. }
  1671. $get_balance_log_url = "/cloud/mch/mchGetBalanceLog";
  1672. $get_balance_log_data = [];
  1673. $get_balance_log_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  1674. if($this->page) $get_balance_log_data['page'] = $this->page; //计费方式【1=>按重计费、2=>按件计费】
  1675. if($this->limit) $get_balance_log_data['limit'] = $this->limit;
  1676. if($this->sort) $get_balance_log_data['sort'] = $this->sort;
  1677. debug_log($this->sort);
  1678. $getBalanceLogInfo = cloud_post($this->domain.$get_balance_log_url,$get_balance_log_data);
  1679. $getBalanceLogInfo = json_decode($getBalanceLogInfo,true);
  1680. if($getBalanceLogInfo['code'] != 0){
  1681. $code = 1; //失败
  1682. return $this->outPutData($code,$getBalanceLogInfo['msg']);
  1683. }else{
  1684. $msg = '获取成功';
  1685. return $this->outPutData($getBalanceLogInfo['code'],$msg,$getBalanceLogInfo['data']);
  1686. }
  1687. }
  1688. //商品列表
  1689. public function mchGetGoodsList(){
  1690. if (!$this->validate()) {
  1691. return $this->getErrorSummary(false)[0];
  1692. }
  1693. $message = null;
  1694. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  1695. if (!$cloud_store_token && $message) {
  1696. return $message;
  1697. }
  1698. if ($this->token_stroe_cloud_id) {
  1699. $store_cloud = StoreCloud::findOne(['cloud_store_id' => $this->token_stroe_cloud_id]);
  1700. } else {
  1701. $store_id = $this->store_id ?: get_store_id();
  1702. $store_cloud = StoreCloud::findOne(['store_id' => $store_id, 'is_delete' => 0]);
  1703. }
  1704. if ($store_cloud && $store_cloud->cloud_platform_list) {
  1705. $cloud_platform_list = json_decode($store_cloud->cloud_platform_list, true);
  1706. if($this->supplier_id) {
  1707. $supplier_id = explode(',', $this->supplier_id);
  1708. }
  1709. if (!empty($supplier_id)) {
  1710. $supplier_id = array_map('intval', $supplier_id);
  1711. } else {
  1712. $supplier_id = $cloud_platform_list;
  1713. }
  1714. if (!empty($cloud_platform_list)) {
  1715. $cloud_platform_list = array_map('intval', $cloud_platform_list);
  1716. $supplier_id = array_intersect($cloud_platform_list, $supplier_id);
  1717. if (!empty($supplier_id)) {
  1718. $this->supplier_id = implode(',', $supplier_id);
  1719. } else {
  1720. $this->supplier_id = "0";
  1721. }
  1722. }
  1723. }
  1724. $mch_get_goods_list_url = "/cloud/mch/mchGetGoodsList";
  1725. $mch_get_goods_list_data = [];
  1726. $mch_get_goods_list_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  1727. $mch_get_goods_list_data['page'] = $this->page; //默认为 1
  1728. $mch_get_goods_list_data['limit'] = $this->limit; //默认20
  1729. if($this->cat_id) $mch_get_goods_list_data['cat_id'] = $this->cat_id;
  1730. if($this->parent_id) $mch_get_goods_list_data['parent_id'] = $this->parent_id;
  1731. if($this->name) $mch_get_goods_list_data['name'] = $this->name; //名称
  1732. if($this->supplier_id) $mch_get_goods_list_data['supplier_id'] = $this->supplier_id; //供货商ID
  1733. $mch_get_goods_list_data['sort'] = $this->sort;
  1734. if($this->status) $mch_get_goods_list_data['status'] = $this->status; //-1 全部 0 下架 1 上架
  1735. if($this->price_sort) $mch_get_goods_list_data['sort'] = $this->price_sort;
  1736. if($this->sale_sort) $mch_get_goods_list_data['sort'] = 3;
  1737. if($this->high_price) $mch_get_goods_list_data['high_price'] = $this->high_price;
  1738. if($this->low_price) $mch_get_goods_list_data['low_price'] = $this->low_price;
  1739. if(intval($this->send_type) !== -1 && $this->send_type !== null) $mch_get_goods_list_data['send_type'] = $this->send_type;
  1740. $mch_get_goods_list_data['is_distribution'] = $this->is_distribution;
  1741. $mchGetGoodsListInfo = cloud_post($this->domain.$mch_get_goods_list_url,$mch_get_goods_list_data);
  1742. $mchGetGoodsListInfo = json_decode($mchGetGoodsListInfo,true);
  1743. if($mchGetGoodsListInfo['code'] != 0){
  1744. $code = 1;//失败
  1745. return $this->outPutData($code,$mchGetGoodsListInfo['msg']);
  1746. }else{
  1747. //获取议价比
  1748. // $content = Option::get('cloud', 0, 'saas');
  1749. // $content = json_decode($content['value'], true);
  1750. $rate = Store::find()->where(['id' => $this->store_id ?: get_store_id()])->select('rate')->scalar() ?: 0;
  1751. //填充
  1752. if (!empty($mchGetGoodsListInfo['data']['list'])) {
  1753. foreach ($mchGetGoodsListInfo['data']['list'] as &$item) {
  1754. // if (!empty($item['original_price'])) {
  1755. // $item['sale_price'] = $item['original_price'];
  1756. // } else {
  1757. $item['store_id'] = $this->store_id ?: get_store_id();
  1758. $item['cost_price'] = $item['price']; //将供货商售价作为成本价
  1759. $sale_price_type = Option::get('sale_price_type', $this->store_id ?: get_store_id(), 'store')['value'];
  1760. $item['sale_price'] = intval($sale_price_type) === 0 ? sprintf('%.2f', ($item['platform_negotiated_price'] + ($item['platform_negotiated_price']) * ($rate / 100))) : $item['original_price'];
  1761. // $item['price'] = $item['sale_price'] = sprintf("%.2f", $item['price'] + ($item['price'] * ($rate / 100)));
  1762. $item['sale_price'] = round($item['sale_price'], 2);
  1763. if (in_array(intval($this->is_distribution), [0, 1, 2])) {
  1764. $item['price'] = $item['platform_negotiated_price'];
  1765. $item['original_price'] = $item['sale_price'];
  1766. }
  1767. $item['price_rate'] = '0.00';
  1768. $price_rate = sprintf('%.2f', ($item['original_price'] - $item['price']));
  1769. if ($price_rate >= 0) {
  1770. $item['price_rate'] = sprintf('%.2f', $price_rate);
  1771. }
  1772. // }
  1773. if (!empty($item['attrs'])) {
  1774. foreach ($item['attrs'] as &$attr_item) {
  1775. //生成零售价
  1776. // if (!empty($attr_item['original_price'])) {
  1777. // $sale_price = $attr_item['original_price'];
  1778. // } else {
  1779. $attr_item['price'] = $attr_item['price'] ?: '0.00';
  1780. $attr_item['cost_price'] = $attr_item['price']; //将供货商售价作为成本价
  1781. if ($sale_price_type) {
  1782. $sale_price = $attr_item['original_price'];
  1783. } else {
  1784. $sale_price = sprintf('%.2f', ($attr_item['platform_negotiated_price'] + ($attr_item['platform_negotiated_price']) * ($rate / 100)));
  1785. }
  1786. $attr_item['sale_price'] = $sale_price;
  1787. // }
  1788. $attr_item = array_merge($attr_item, ['sale_price' => $sale_price, 'rate' => $rate]);
  1789. }
  1790. }
  1791. }
  1792. }
  1793. $msg = "获取成功";
  1794. return $this->outPutData($mchGetGoodsListInfo['code'], $msg, $mchGetGoodsListInfo['data']);
  1795. }
  1796. }
  1797. public function setSaasStoreCloudId($saas_id = 0) {
  1798. $item = get_saas_purchase_store_cloud($saas_id);
  1799. $this->token_stroe_cloud_id = $item ? $item->id : 0;
  1800. }
  1801. //创建供货单
  1802. public function purchasePrePurchaseOrder($cartList, $address){
  1803. $orderInfo = (object)[];
  1804. if($address){
  1805. $orderInfo = (object)($address);
  1806. $orderInfo->address = $address['detail'];
  1807. }
  1808. $orderInfo->is_pay = $this->is_pay;
  1809. $goodsInfo = [];
  1810. foreach($cartList['goods_list'] as $i => $v){
  1811. foreach ($v['cart'] as $cartI => $_c) {
  1812. $_goodsInfo = [
  1813. 'attr' => $_c['attr_list'],
  1814. 'goods_id' => $v['goods']['id'],
  1815. 'num' => $_c['num'],
  1816. 'goods_name' => $v['goods']['name'],
  1817. 'total_price' => $_c['price'] * $_c['num'],
  1818. ];
  1819. $goodsInfo[] = $_goodsInfo;
  1820. }
  1821. }
  1822. return $this->_purchaseSetPurchaseOrder($orderInfo, $goodsInfo);
  1823. }
  1824. //创建供货单
  1825. public function purchaseSetPurchaseOrder($orderIds = []){
  1826. if (!$this->validate()) {
  1827. return $this->getErrorSummary(false)[0];
  1828. }
  1829. if(!$orderIds){
  1830. $orderIds[] = $this->order_id;
  1831. }
  1832. $goods_info = [];
  1833. foreach ($orderIds as $order_id){
  1834. $orderInfo = PurchaseOrder::findOne([
  1835. 'id' => $order_id,
  1836. ]);
  1837. if(!$orderInfo){
  1838. \Yii::error('-----------PurchaseOrder订单数据有误' . $order_id);
  1839. $code = 1;//失败
  1840. $msg = "订单数据有误";
  1841. return $this->outPutData($code,$msg);
  1842. }
  1843. $OrderDetail = PurchaseOrderDetail::find()->where(['order_id' => $order_id])->select('id order_d_id, goods_id, num, total_price price, pic pic_url, attr')->asArray()->all();
  1844. foreach ($OrderDetail as $item) {
  1845. $goods_info[] = $item;
  1846. }
  1847. }
  1848. debug_log('orderIds' . json_encode($orderIds));
  1849. $res = $this->_purchaseSetPurchaseOrder($orderInfo, $goods_info, $orderIds);
  1850. debug_log('运行结束' . json_encode($res));
  1851. return $res;
  1852. }
  1853. private function _purchaseSetPurchaseOrder($orderInfo, $goods_info, $orderIds = []){
  1854. $new_goods_info = [];
  1855. foreach($goods_info as $item){
  1856. $goodsId = $item['goods_id'];
  1857. if(empty($new_goods_info[$goodsId])){
  1858. $new_goods_info[$goodsId] = ['id' => $goodsId];
  1859. }
  1860. $new_goods_info[$goodsId]['attr'][] = [
  1861. 'attr_list' => is_array($item['attr']) ? $item['attr'] : json_decode($item['attr']),
  1862. 'num' => $item['num'],
  1863. ];
  1864. $new_goods_info[$goodsId]['num'] += $item['num'];
  1865. }
  1866. // $this->setSaasStoreCloudId();
  1867. $message = null;
  1868. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  1869. if (!$cloud_store_token && $message) {
  1870. return $message;
  1871. }
  1872. $mch_set_purchase_order_url = "/cloud/mch/mchSetSubmitOrder";
  1873. $mch_set_purchase_order_data = [];
  1874. $mch_set_purchase_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
  1875. $mch_set_purchase_order_data['province_id'] = $orderInfo->province_id; //省
  1876. $mch_set_purchase_order_data['city_id'] = $orderInfo->city_id; //市
  1877. $mch_set_purchase_order_data['district_id'] = $orderInfo->district_id; //区
  1878. $mch_set_purchase_order_data['address'] = $orderInfo->address; //地址详情
  1879. $mch_set_purchase_order_data['name'] = $orderInfo->name; //联系人
  1880. $mch_set_purchase_order_data['mobile'] = $orderInfo->mobile; //联系电话
  1881. $mch_set_purchase_order_data['remark'] = $orderInfo->remark;
  1882. $mch_set_purchase_order_data['goods_info'] = json_encode($new_goods_info); //商品数据
  1883. $mch_set_purchase_order_data['pay_type'] = $orderInfo->pay_type == 3 ? 0 : 1;
  1884. $mch_set_purchase_order_data['type'] = $this->type; //1;
  1885. $mch_set_purchase_order_data['is_pay'] = $orderInfo->is_pay ?? 1;
  1886. $mch_set_purchase_order_data['order_id'] = $orderInfo->cloud_order_id ?? 0;
  1887. $mchSetPurchaseOrderInfo = cloud_post($this->domain.$mch_set_purchase_order_url,$mch_set_purchase_order_data);
  1888. $mchSetPurchaseOrderInfo = json_decode($mchSetPurchaseOrderInfo,true);
  1889. // \Yii::error('-----------PurchaseOrder转供货单' . Json::encode($mch_set_purchase_order_data));
  1890. // \Yii::error('-----------PurchaseOrder转供货单' . Json::encode($mchSetPurchaseOrderInfo));
  1891. if($mchSetPurchaseOrderInfo['code'] != 0){
  1892. \Yii::error('-----------PurchaseOrder转供货单失败' . Json::encode($mchSetPurchaseOrderInfo));
  1893. $code = 1;//失败
  1894. return $this->outPutData($code,$mchSetPurchaseOrderInfo['msg']);
  1895. }else{
  1896. if($mch_set_purchase_order_data['type'] == 1 && $mchSetPurchaseOrderInfo['data']){
  1897. foreach ($orderIds as $index => $orderId) {
  1898. $status = 0;
  1899. foreach ($mchSetPurchaseOrderInfo['order_data'] as $order_datum) {
  1900. foreach ($order_datum as $item) {
  1901. if (intval($item['old_order_id']) === intval($orderId)) {
  1902. if (intval($item['status']) === 2) {
  1903. $status = 3;
  1904. }
  1905. }
  1906. $print_type = 'pay';
  1907. if (intval($item['status']) === 2) {
  1908. $print_type = 'order';
  1909. }
  1910. try {
  1911. $supplier = Supplier::findOne(['cloud_supplier_id' => $item['supplier']]);
  1912. $printer_order = new PrintOrder(0, $item['id'], $print_type, 0, 0, 2, 0, $supplier->id);
  1913. $printer_order->print_order();
  1914. } catch (\Exception $e) {
  1915. debug_log([
  1916. 'event' => '_purchaseSetPurchaseOrder',
  1917. 'message' => $e->getMessage(),
  1918. 'line' => $e->getLine(),
  1919. 'file' => $e->getFile()
  1920. ], 'cloud_printer.log');
  1921. }
  1922. }
  1923. }
  1924. PurchaseOrder::updateAll(['cloud_order_id' => $mchSetPurchaseOrderInfo['data'][$index], 'trade_status' => $status], ['id' => $orderId]);
  1925. }
  1926. }
  1927. $msg = "创建成功";
  1928. return [
  1929. 'code' => $mchSetPurchaseOrderInfo['code'],
  1930. 'msg' => $msg,
  1931. 'data' => $mchSetPurchaseOrderInfo['data'],
  1932. 'order_data' => $mchSetPurchaseOrderInfo['order_data']
  1933. ];
  1934. }
  1935. }
  1936. public function getAgentOrderDistributionList() {
  1937. $message = null;
  1938. $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
  1939. if (!$cloud_store_token && $message) {
  1940. return $message;
  1941. }
  1942. $mch_set_purchase_order_url = "/cloud/mch/getAgentOrderDistributionList";
  1943. $data = [];
  1944. $data['access_token'] = $cloud_store_token;
  1945. $data['goods_name'] = $this->name;
  1946. $data['status'] = $this->status;
  1947. $data['page'] = $this->page; //默认为 1
  1948. $data['limit'] = $this->limit; //默认20
  1949. $mchSetPurchaseOrderInfo = cloud_post($this->domain.$mch_set_purchase_order_url,$data);
  1950. $mchSetPurchaseOrderInfo = json_decode($mchSetPurchaseOrderInfo,true);
  1951. if($mchSetPurchaseOrderInfo['code'] != 0){
  1952. $code = 1;//失败
  1953. return $this->outPutData($code,$mchSetPurchaseOrderInfo['msg']);
  1954. }else{
  1955. $msg = "获取成功";
  1956. if (!empty($mchSetPurchaseOrderInfo['data'])) {
  1957. foreach ($mchSetPurchaseOrderInfo['data']['data'] as &$data) {
  1958. $admin_id = $data['admin_id'];
  1959. $data['admin_name'] = Admin::findOne(['id' => $admin_id])->name;
  1960. }
  1961. }
  1962. return $this->outPutData($mchSetPurchaseOrderInfo['code'], $msg, $mchSetPurchaseOrderInfo['data']);
  1963. }
  1964. }
  1965. //胜天半子售后操作
  1966. public function stbzRrfundOrderHandle($cloud_store_token, $refund_data = null) {
  1967. try {
  1968. //获取订单信息 售后前置检测 +
  1969. if ((int)$this->order_type === 1) {
  1970. $order_url = "/cloud/mch/mchOrderList";
  1971. } else {
  1972. $order_url = "/cloud/mch/mchPurchaseOrderList";
  1973. }
  1974. $order_data['access_token'] = $cloud_store_token;
  1975. $order_data['id'] = $this->order_id;
  1976. $domain = $this->domain;
  1977. $orderInfo = cloud_post($domain. $order_url,
  1978. $order_data);
  1979. $orderInfo = json_decode($orderInfo,true);
  1980. if ($orderInfo['code'] !== 0) {
  1981. return [
  1982. 'code' => 2,
  1983. 'msg' => $orderInfo['msg']
  1984. ];
  1985. }
  1986. $data = $orderInfo['data']['list'][0];
  1987. if (!empty($data['goods_list'])) {
  1988. foreach ($data['goods_list'] as $datum) {
  1989. $attr = json_decode($datum['attr'], true);
  1990. if ($datum['stbz_goods_id'] && $attr['no']) {
  1991. $params = [
  1992. 'orderSn' => $data['order_no'],
  1993. 'sku' => $attr['no']
  1994. ];
  1995. $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale/beforeCheck', $params);
  1996. debug_log('胜天半子售后' . json_encode($checkData));
  1997. $checkData = \json_decode($checkData, true);
  1998. if ((int)$checkData['code'] === 1) {
  1999. $checkData = $checkData['data'];
  2000. $num = $checkData['num'];
  2001. if ($num <= 0) {
  2002. return [
  2003. 'code' => 1,
  2004. 'msg' => '可售后商品数量为0个'
  2005. ];
  2006. }
  2007. $serviceType = $checkData['serviceType'];
  2008. $pickType = $checkData['pickType'];
  2009. $packageType = $checkData['packageType'];
  2010. $returnType = $checkData['returnType'];
  2011. $reasonsType = $checkData['reasonsType'];
  2012. //售后类型 20换货 50退款退货
  2013. $serviceTypeCode = '0';
  2014. //目前订单未完成禁止售后
  2015. if ((int)$data['status'] === 0) {
  2016. $serviceTypeCode = '40';
  2017. } else {
  2018. if ((int)$refund_data['type'] === 1) {
  2019. $serviceTypeCodeArr = array_column($serviceType, 'code');
  2020. // $serviceTypeRes = array_search('50', $serviceType);
  2021. $serviceTypeCode = '50';
  2022. if (!in_array('50', $serviceTypeCodeArr)) {
  2023. $serviceTypeCode = '40';
  2024. if (!in_array('40', $serviceTypeCodeArr)) {
  2025. return [
  2026. 'code' => 1,
  2027. 'msg' => '当前订单状态异常,不可申请退款退货'
  2028. ];
  2029. }
  2030. }
  2031. } else {
  2032. foreach ($serviceType as $service) {
  2033. if ($service['code'] == '20') {
  2034. $serviceTypeCode = '20';
  2035. }
  2036. }
  2037. if (!$serviceTypeCode) {
  2038. return [
  2039. 'code' => 1,
  2040. 'msg' => '当前订单状态异常,不可申请换货'
  2041. ];
  2042. }
  2043. }
  2044. }
  2045. //售后原因
  2046. $reasonsTypeCode = 0;
  2047. if ((int)$data['status'] === 0) {
  2048. $reasonsTypeCode = '6';
  2049. } else {
  2050. if (!empty($reasonsType)) {
  2051. if (!empty($refund_data['reasonsTypeCode'])) {
  2052. foreach ($reasonsType as $reason) {
  2053. if ($reason['code'] == $refund_data['reasonsTypeCode']) {
  2054. $reasonsTypeCode = $refund_data['reasonsTypeCode'];
  2055. }
  2056. }
  2057. if (!$reasonsTypeCode) {
  2058. return [
  2059. 'code' => 1,
  2060. 'msg' => '售后原因编码错误'
  2061. ];
  2062. }
  2063. } else {
  2064. return [
  2065. 'code' => 1,
  2066. 'msg' => '售后原因编码错误'
  2067. ];
  2068. }
  2069. }
  2070. }
  2071. //返件类型
  2072. $returnTypeCode = 0;
  2073. if (!empty($returnType)) {
  2074. if (!empty($refund_data['returnTypeCode'])) {
  2075. foreach ($returnType as $return) {
  2076. if ($return['code'] == $refund_data['reasonsTypeCode']) {
  2077. $returnTypeCode = $refund_data['reasonsTypeCode'];
  2078. }
  2079. }
  2080. if (!$returnTypeCode) {
  2081. return [
  2082. 'code' => 1,
  2083. 'msg' => '返件类型编码错误'
  2084. ];
  2085. }
  2086. } else {
  2087. return [
  2088. 'code' => 1,
  2089. 'msg' => '返件类型编码错误'
  2090. ];
  2091. }
  2092. }
  2093. //包装情况
  2094. $packageTypeCode = 0;
  2095. if (!empty($packageType)) {
  2096. if (!empty($refund_data['packageTypeCode'])) {
  2097. foreach ($packageType as $package) {
  2098. if ($package['code'] == $refund_data['packageTypeCode']) {
  2099. $packageTypeCode = $refund_data['packageTypeCode'];
  2100. }
  2101. }
  2102. if (!$packageTypeCode) {
  2103. return [
  2104. 'code' => 1,
  2105. 'msg' => '包装情况编码错误'
  2106. ];
  2107. }
  2108. } else {
  2109. return [
  2110. 'code' => 1,
  2111. 'msg' => '包装情况编码错误'
  2112. ];
  2113. }
  2114. }
  2115. $pickTypeCode = '40'; //自主发货
  2116. $urls = [];
  2117. if ($refund_data['pic_list']) {
  2118. //上传凭证
  2119. foreach ($refund_data['pic_list'] as &$pic_list) {
  2120. $save_path = $this->saveTempImage($pic_list);
  2121. $image = file_get_contents($save_path);
  2122. $pic_list = base64_encode($image);
  2123. }
  2124. $params = array_merge($params, [
  2125. 'pictures' => $refund_data['pic_list']
  2126. ]);
  2127. $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale/picture', $params);
  2128. $checkData = \json_decode($checkData, true);
  2129. if ((int)$checkData['code'] === 1) {
  2130. $urls = $checkData['data']['urls'];
  2131. }
  2132. }
  2133. $data['address_data'] = json_decode($data['address_data'], true);
  2134. //申请售后
  2135. $refund_params = [
  2136. 'orderSn' => $params['orderSn'],
  2137. 'sku' => $params['sku'],
  2138. 'num' => $num,
  2139. 'goodsFee' => $data['goods_price'],
  2140. 'logisticFee' => $data['express_price'],
  2141. 'serviceTypeCode' => $serviceTypeCode,
  2142. 'pickTypeCode' => $pickTypeCode,
  2143. 'packageTypeCode' => $packageTypeCode,
  2144. 'returnTypeCode' => $returnTypeCode,
  2145. 'reasonsTypeCode' => $reasonsTypeCode,
  2146. 'reasonsDescription' => $refund_data['desc'] ?: '不想要了',
  2147. 'vouchers' => $urls,
  2148. 'userInfo' => [
  2149. 'consignee' => $data['name'],
  2150. 'phone' => $data['mobile'],
  2151. 'province' => $data['address_data']['province'],
  2152. 'city' => $data['address_data']['city'],
  2153. 'area' => $data['address_data']['district'],
  2154. 'street' => '',
  2155. 'description' => $data['address_data']['address'],
  2156. ]
  2157. ];
  2158. //已经发货,且为换货
  2159. if ((int)$refund_data['type'] === 0 && (int)$data['status'] !== 0) {
  2160. //如果是换货时,移除退货金额 ,将运费清0
  2161. unset($refund_params['goodsFee']);
  2162. $refund_params['logisticFee'] = 0;
  2163. }
  2164. //退款退货 或 未发货
  2165. if ((int)$refund_data['type'] === 1 || (int)$data['status'] === 0) {
  2166. //如果是退款退货时,移除收件人信息
  2167. unset($refund_params['userInfo']);
  2168. }
  2169. $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale', $refund_params);
  2170. $checkData = \json_decode($checkData, true);
  2171. if ((int)$checkData['code'] === 1) {
  2172. return [
  2173. 'code' => 0,
  2174. 'msg' => 'success',
  2175. 'data' => $checkData['data']['id']
  2176. ];
  2177. } else {
  2178. return [
  2179. 'code' => 1,
  2180. 'msg' => $checkData['msg']
  2181. ];
  2182. }
  2183. } else {
  2184. return [
  2185. 'code' => 1,
  2186. 'msg' => $checkData['msg']
  2187. ];
  2188. }
  2189. }
  2190. }
  2191. }
  2192. return [
  2193. 'code' => 2,
  2194. 'msg' => '参数错误'
  2195. ];
  2196. } catch (\Exception $e) {
  2197. return [
  2198. 'code' => 1,
  2199. 'msg' => $e->getMessage()
  2200. ];
  2201. }
  2202. }
  2203. public function stbzRrfundOrderHandleInfo($cloud_store_token, $id) {
  2204. try {
  2205. $order_url = "/cloud/mch/mchPurchaseOrderList";
  2206. $order_data['access_token'] = $cloud_store_token;
  2207. $order_data['id'] = $id;
  2208. $domain = (new OptionSetting)->getCloudDomainName();
  2209. $orderInfo = cloud_post($domain. $order_url,
  2210. $order_data);
  2211. $orderInfo = json_decode($orderInfo,true);
  2212. if ($orderInfo['code'] !== 0) {
  2213. return [
  2214. 'code' => 0,
  2215. 'msg' => $orderInfo['msg']
  2216. ];
  2217. }
  2218. $data = $orderInfo['data']['list'][0];
  2219. if (!empty($data['goods_list'])) {
  2220. foreach ($data['goods_list'] as $datum) {
  2221. $attr = json_decode($datum['attr'], true);
  2222. if ($datum['stbz_goods_id'] && $attr['no']) {
  2223. $params = [
  2224. 'orderSn' => $data['order_no'],
  2225. 'sku' => $attr['no']
  2226. ];
  2227. $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale/beforeCheck', $params);
  2228. $checkData = \json_decode($checkData, true);
  2229. if ($checkData['code'] === 1) {
  2230. $checkData = $checkData['data'];
  2231. $num = $checkData['num'];
  2232. $reasonsType = $checkData['reasonsType'];
  2233. $returnType = $checkData['returnType'];
  2234. $packageType = $checkData['packageType'];
  2235. $serviceType = $checkData['serviceType'];
  2236. return [
  2237. 'code' => 0,
  2238. 'msg' => 'success',
  2239. 'data' => [
  2240. 'reasonsType' => $reasonsType,
  2241. 'returnType' => $returnType,
  2242. 'packageType' => $packageType,
  2243. 'serviceType' => $serviceType
  2244. ]
  2245. ];
  2246. } else {
  2247. return [
  2248. 'code' => 1,
  2249. 'msg' => $checkData['msg']
  2250. ];
  2251. }
  2252. }
  2253. }
  2254. }
  2255. return [
  2256. 'code' => 0,
  2257. 'data' => [
  2258. 'reasonsType' => null,
  2259. 'returnType' => null,
  2260. 'packageType' => null
  2261. ]
  2262. ];
  2263. } catch (\Exception $e) {
  2264. return [
  2265. 'code' => 1,
  2266. 'msg' => $e->getMessage()
  2267. ];
  2268. }
  2269. }
  2270. //普通商品支付完成后需要上门安装逻辑
  2271. public function isNeedInstall($order_id, $type = 0, $submit = false) {
  2272. try {
  2273. if (intval($type)) {
  2274. $order = PurchaseOrder::findOne($order_id);
  2275. } else {
  2276. $order = Order::findOne($order_id);
  2277. }
  2278. if ($order) {
  2279. $address = [
  2280. 'province_id' => $order->province_id,
  2281. 'city_id' => $order->city_id,
  2282. 'district_id' => $order->district_id,
  2283. ];
  2284. if (intval($type)) {
  2285. $order_goods = PurchaseOrderDetail::find()->where(['order_id' => $order_id, 'is_delete' => 0])
  2286. ->select('goods_id as cloud_goods_id, id order_detail_id')->asArray()->all();
  2287. $store_address = $address;
  2288. } else {
  2289. $md_id = $order->md_id;
  2290. $md = Md::findOne($md_id);
  2291. $store_address = [
  2292. 'province_id' => $md->province,
  2293. 'city_id' => $md->city,
  2294. 'district_id' => $md->district,
  2295. ];
  2296. $order_goods = OrderDetail::find()->alias('od')
  2297. ->leftJoin(['g' => Goods::tableName()], 'g.id = od.goods_id')
  2298. ->where(['od.order_id' => $order_id])->select('g.id, g.cloud_goods_id, od.id order_detail_id')->asArray()->all();
  2299. }
  2300. if (!empty($order_goods)) {
  2301. $form = new OrderForm();
  2302. foreach ($order_goods as $item) {
  2303. $cloud_goods_id = 0;
  2304. if (intval($type)) {
  2305. $cloud_goods_id = $item['cloud_goods_id'];
  2306. }
  2307. $is_install = $this->goodsIsInstall($item['id'] ?? 0, $cloud_goods_id);
  2308. if ($is_install !== 2) {
  2309. $address = $is_install === 1 ? $address : $store_address;
  2310. $result = $form->goodsIsGoodsAgent($address, $item['id'] ?? 0, $cloud_goods_id);
  2311. if ($result['code'] === 0 && $submit) {
  2312. $model = new AgentGoodsInstallLog();
  2313. $model->goods_agent_admin_id = $result['data']->id;
  2314. $model->order_detail_id = $item['order_detail_id'];
  2315. $model->is_need_install = (int)(intval($is_install) === 1);
  2316. $model->order_id = $order_id;
  2317. $model->order_type = intval($type);
  2318. if (!$model->save()) {
  2319. throw new \Exception(json_encode($model->errors));
  2320. }
  2321. } else {
  2322. return $result;
  2323. }
  2324. }
  2325. }
  2326. }
  2327. }
  2328. return [
  2329. 'code' => 0,
  2330. 'msg' => '操作完成'
  2331. ];
  2332. } catch (\Exception $e) {
  2333. return [
  2334. 'code' => 1,
  2335. 'msg' => '转单完成后需要上门安装逻辑操作异常' . $e->getMessage() . $e->getLine() . $e->getFile()
  2336. ];
  2337. }
  2338. }
  2339. //判断是否需为代理配送且要上门安装
  2340. public function goodsIsInstall($goods_id, $cloud_goods_id = 0) {
  2341. try {
  2342. $goods = Goods::findOne($goods_id);
  2343. if (empty($cloud_goods_id)) {
  2344. if (empty($goods->cloud_goods_id)) {
  2345. return 2;
  2346. }
  2347. }
  2348. // $goods_url = "/goods/getAttrParams";
  2349. // $param = [
  2350. // 'id' => $cloud_goods_id
  2351. // ];
  2352. // //请求接口
  2353. // $domain = (new OptionSetting)->getCloudDomainName();
  2354. // $goodsInfo = cloud_post($domain . $goods_url, $param);
  2355. // $goodsInfo = json_decode($goodsInfo, true);
  2356. // if ((int)$goodsInfo['code'] === 0) {
  2357. // $goodsInfo = $goodsInfo['data'];
  2358. // //需要安装
  2359. // if ((int)$goodsInfo['send_type'] === 3 && (int)$goodsInfo['is_need_install'] === 1) {
  2360. // return 1;
  2361. // }
  2362. // //不需要安装
  2363. // if ((int)$goodsInfo['send_type'] === 3) {
  2364. // return 3;
  2365. // }
  2366. // }
  2367. if ($goods->agent_goods_status > 0) {
  2368. return intval($goods->agent_goods_status) === 1 ? 3 : 1;
  2369. }
  2370. return 2;
  2371. } catch (\Exception $e) {
  2372. return 2;
  2373. }
  2374. }
  2375. //
  2376. public function setCloudMerchantConfig($reduce_type = null, $store_cloud_id = 0) {
  2377. try {
  2378. if (!$this->validate()) {
  2379. return [
  2380. 'code' => 1,
  2381. 'msg' => $this->getErrorSummary(false)[0],
  2382. ];
  2383. }
  2384. //获取平台token
  2385. $cloud_token = get_merchant_token($store_cloud_id);
  2386. $getGoodsCateUrl = "/cloud/mch/setMchStore";
  2387. $getGoodsCateData['access_token'] = $cloud_token;
  2388. if (isset($reduce_type)) {
  2389. $getGoodsCateData['reduce_type'] = $reduce_type;
  2390. }
  2391. $getGoodsCateInfo = cloud_post($this->domain.$getGoodsCateUrl,$getGoodsCateData);
  2392. $getGoodsCateInfo = json_decode($getGoodsCateInfo,true);
  2393. if($getGoodsCateInfo['code'] != 0){
  2394. throw new \Exception($getGoodsCateInfo['msg']);
  2395. }
  2396. return $this->outPutData(0, $getGoodsCateInfo['msg'], $getGoodsCateInfo['data']);
  2397. } catch (\Exception $e) {
  2398. return $this->outPutData(1, $e->getMessage());
  2399. }
  2400. }
  2401. //转单取消订单
  2402. public function purchaseOrderCancel() {
  2403. try {
  2404. //获取平台token
  2405. $cloud_token = get_merchant_token(0, $this->store_id);
  2406. $orderDataUrl = "/cloud/mch/delPurchaseOrder";
  2407. $orderData['access_token'] = $cloud_token;
  2408. $orderData['order_id'] = $this->order_id;
  2409. $orderTransit = OrderTransit::findOne(['cloud_order_id' => $this->order_id, 'is_delete' => 0]);
  2410. if (!$orderTransit) {
  2411. throw new \Exception('订单不存在');
  2412. }
  2413. if ($orderTransit->order_id) {
  2414. if ($orderTransit->is_use_platform_mch) {
  2415. $orderData['is_use_platform_mch'] = $orderTransit->is_use_platform_mch;
  2416. }
  2417. }
  2418. //这里没太明白为啥云仓都已经退款了还是要调用申请售后的逻辑
  2419. // if($orderTransit){
  2420. //// $form = new \app\modules\admin\models\MerchantForm();
  2421. // $form = $this;
  2422. // $form->order_id = $orderTransit->cloud_order_id;
  2423. // $form->apply_data = [];
  2424. // $form->apply_data['type'] = 1;
  2425. // $form->apply_data['desc'] = 'cancel order before send';
  2426. // $form->apply_data['pic_list'] = [];
  2427. // $mchOrderRefundApply = $form->mchOrderRefundApply();
  2428. // if($mchOrderRefundApply['code']){
  2429. // return $mchOrderRefundApply;
  2430. // }
  2431. // }
  2432. $getGoodsCateInfo = cloud_post($this->domain . $orderDataUrl, $orderData);
  2433. $getGoodsCateInfo = json_decode($getGoodsCateInfo,true);
  2434. if ($getGoodsCateInfo['code'] != 0) {
  2435. throw new \Exception($getGoodsCateInfo['msg']);
  2436. }
  2437. if (isset($getGoodsCateInfo['data']['order_detail'])) {
  2438. foreach ($getGoodsCateInfo['data']['order_detail'] as $order_detail_item) {
  2439. if (in_array($order_detail_item['send_type'], [5, 6])) {
  2440. //增加仓库库存
  2441. $cloud_goods_attr = json_decode($order_detail_item['attr'], true);
  2442. $cloud_goods_attr_id = array_column($cloud_goods_attr['attr_list'], 'attr_id');
  2443. sort($cloud_goods_attr_id);
  2444. //转单后扣减仓库库存 调用供货商修改库存接口 数量传0用作更新商城库存
  2445. $erpInventoryArray = AgentFrontErpInventory::find()->where(['goods_id' => $order_detail_item['id']])
  2446. ->asArray()->all();
  2447. foreach ($erpInventoryArray as $erpInventory) {
  2448. $attr_ids = explode(',', $erpInventory['attr_ids']);
  2449. sort($attr_ids);
  2450. if (!array_diff($cloud_goods_attr_id, $attr_ids)) {
  2451. $ei = AgentFrontErpInventory::findOne($erpInventory['id']);
  2452. if ($ei) {
  2453. AgentFrontErpInventory::logSave($ei, $order_detail_item['num'], AgentFrontErpInventoryLog::LOG_TYPE_ORDER, $this->order_id, $orderTransit->cloud_order_no);
  2454. }
  2455. break;
  2456. }
  2457. }
  2458. }
  2459. }
  2460. }
  2461. // if (intval($orderTransit->is_use_platform_mch)) {
  2462. // $store_info = Store::findOne($orderTransit->store_id);
  2463. // Store::addMoney($store_info, $orderTransit->order_price, '商城云仓订单取消');
  2464. // }
  2465. $orderTransit->status = -1;
  2466. $orderTransit->save();
  2467. try {
  2468. $supplier = Supplier::findOne(['cloud_supplier_id' => $orderTransit->cloud_supplier_id]);
  2469. $printer_order = new PrintOrder(0, $this->order_id, 'confirm', 0, 0, 1, 0, $supplier->id);
  2470. $printer_order->print_order();
  2471. } catch (\Exception $e) {
  2472. debug_log([
  2473. 'event' => 'purchaseOrderCancel',
  2474. 'message' => $e->getMessage(),
  2475. 'line' => $e->getLine(),
  2476. 'file' => $e->getFile()
  2477. ], 'cloud_printer.log');
  2478. }
  2479. return [
  2480. 'code' => 0,
  2481. 'msg' => "操作成功"
  2482. ];
  2483. } catch (\Exception $e) {
  2484. return [
  2485. 'code' => $e->getCode() ?: 1,
  2486. 'msg' => $e->getMessage()
  2487. ];
  2488. }
  2489. }
  2490. //转单单个商品取消
  2491. public function purchaseOrderCancelGoods() {
  2492. try {
  2493. //获取平台token
  2494. $cloud_token = get_merchant_token(0, $this->store_id);
  2495. $orderDataUrl = "/cloud/mch/orderCancelGoods";
  2496. $orderData['access_token'] = $cloud_token;
  2497. $orderData['order_id'] = $this->order_id;
  2498. $orderData['goods_id'] = $this->goods_id;
  2499. $orderData['goods_attr'] = $this->attr;
  2500. $orderData['mch_cancel_id'] = $this->mch_cancel_id;
  2501. $orderData['goods_num'] = $this->goods_num;
  2502. $orderData['platform_md_id'] = $this->md_id;
  2503. $orderTransit = OrderTransit::findOne(['cloud_order_id' => $this->order_id, 'is_delete' => 0]);
  2504. if (!$orderTransit) {
  2505. throw new \Exception('订单不存在');
  2506. }
  2507. if ($orderTransit->order_id) {
  2508. if ($orderTransit->is_use_platform_mch) {
  2509. $orderData['is_use_platform_mch'] = $orderTransit->is_use_platform_mch;
  2510. }
  2511. }
  2512. $getGoodsCateInfo = cloud_post($this->domain . $orderDataUrl, $orderData);
  2513. $getGoodsCateInfo = json_decode($getGoodsCateInfo,true);
  2514. if ($getGoodsCateInfo['code'] != 0 || !isset($getGoodsCateInfo['code'])) {
  2515. throw new \Exception($getGoodsCateInfo['msg'] ?? '返回值错误');
  2516. }
  2517. return [
  2518. 'code' => 0,
  2519. 'msg' => "操作成功"
  2520. ];
  2521. } catch (\Exception $e) {
  2522. return [
  2523. 'code' => 1,
  2524. 'msg' => $e->getMessage()
  2525. ];
  2526. }
  2527. }
  2528. public function getCloudForbiddenGoods() {
  2529. try {
  2530. $store_id = $this->store_id;
  2531. $storeForbiddenDirectory = StoreForbiddenDirectory::findOne(['store_id' => $store_id, 'is_delete' => 0]);
  2532. if (empty($storeForbiddenDirectory->cloud_cat_id)) {
  2533. return [
  2534. 'code' => 1,
  2535. 'msg' => ''
  2536. ];
  2537. }
  2538. $cloud_cat_id = $storeForbiddenDirectory->cloud_cat_id;
  2539. $orderDataUrl = "/cloud/mch/getCatGoods";
  2540. $orderData['access_token'] = get_merchant_token(0, $store_id);
  2541. $orderData['cat_id'] = $cloud_cat_id;
  2542. $getGoodsCateInfo = cloud_post($this->domain . $orderDataUrl, $orderData);
  2543. $getGoodsCateInfo = json_decode($getGoodsCateInfo,true);
  2544. if ($getGoodsCateInfo['code'] != 0) {
  2545. throw new \Exception($getGoodsCateInfo['msg']);
  2546. }
  2547. return [
  2548. 'code' => 0,
  2549. 'data' => [
  2550. 'goods_id' => $getGoodsCateInfo['data']['goods_id']
  2551. ]
  2552. ];
  2553. } catch (\Exception $e) {
  2554. return [
  2555. 'code' => 1,
  2556. 'msg' => $e->getMessage()
  2557. ];
  2558. }
  2559. }
  2560. //同步商城订单售后状态1禁止发货 0可以发货
  2561. public function syncMchOrderRefundStatus($status) {
  2562. try {
  2563. $order_id = $this->order_id;
  2564. $orderTransit = OrderTransit::findOne(['order_id' => $order_id, 'is_delete' => 0, 'cancel_examine' => 0]);
  2565. if (!$orderTransit) {
  2566. return false;
  2567. }
  2568. $cloud_order_id = $orderTransit->cloud_order_id;
  2569. $store_id = $orderTransit->store_id;
  2570. $sync_url = "/cloud/mch/syncMchOrderRefundStatus";
  2571. $sync_data = [];
  2572. $sync_data['access_token'] = get_merchant_token(0, $store_id);
  2573. $sync_data['order_id'] = $cloud_order_id;
  2574. $sync_data['status'] = $status;
  2575. $sync_info = cloud_post($this->domain . $sync_url, $sync_data);
  2576. $sync_info = json_decode($sync_info,true);
  2577. if ($sync_info['code'] != 0) {
  2578. throw new \Exception($sync_info['msg']);
  2579. }
  2580. return true;
  2581. } catch (\Exception $e) {
  2582. return false;
  2583. }
  2584. }
  2585. //取消售后订单
  2586. public function orderRefundCancel($order_id = 0) {
  2587. try {
  2588. $orderTransit = OrderTransit::findOne(['order_id' => $order_id, 'is_delete' => 0]);
  2589. if (!$orderTransit) {
  2590. return [
  2591. 'code' => 0,
  2592. 'msg' => '非云仓订单'
  2593. ];
  2594. }
  2595. $cloud_order_id = $orderTransit->cloud_order_id;
  2596. $store_id = $orderTransit->store_id;
  2597. $refundCancelUrl = "/cloud/mch/mchOrderRefundCancel";
  2598. $refundCancelData = [];
  2599. $refundCancelData['access_token'] = get_merchant_token(0, $store_id);
  2600. $refundCancelData['order_id'] = $cloud_order_id;
  2601. $refundCancelInfo = cloud_post($this->domain . $refundCancelUrl, $refundCancelData);
  2602. $refundCancelInfo = json_decode($refundCancelInfo,true);
  2603. if ($refundCancelInfo['code'] != 0) {
  2604. throw new \Exception($refundCancelInfo['msg']);
  2605. }
  2606. return [
  2607. 'code' => 0,
  2608. 'msg' => "操作成功",
  2609. 'data' => $refundCancelInfo
  2610. ];
  2611. } catch (\Exception $e) {
  2612. return [
  2613. 'code' => 1,
  2614. 'msg' => $e->getMessage()
  2615. ];
  2616. }
  2617. }
  2618. //返回数据
  2619. private function outPutData($code,$msg,$data=[]){
  2620. $putData = [];
  2621. $putData['code'] = $code;
  2622. $putData['msg'] = $msg;
  2623. if($data) $putData['data'] = $data;
  2624. return $putData;
  2625. }
  2626. //获取token方法
  2627. private function getToken($name, $password) {
  2628. $token = get_platform_token();
  2629. $url = "/user/getToken";
  2630. $data = [];
  2631. $data['name'] = $name;
  2632. $data['pwd'] = $password;
  2633. $data['token'] = $token;
  2634. $response = cloud_post($this->domain.$url,$data);
  2635. return json_decode($response,true);
  2636. }
  2637. //获取网络图片到临时目录
  2638. public function saveTempImage($url)
  2639. {
  2640. if (strpos($url, 'http') === false) {
  2641. $url = 'http:' . trim($url);
  2642. }
  2643. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  2644. mkdir(\Yii::$app->runtimePath . '/image');
  2645. }
  2646. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  2647. CurlHelper::download($url, $save_path);
  2648. return $save_path;
  2649. }
  2650. public function getCloudGoodsInfo() {
  2651. try {
  2652. $goods_id = $this->goods_id;
  2653. $goods = Goods::find()->where(['cloud_goods_id' => $goods_id, 'is_delete' => 0, 'store_id' => get_store_id()])->one();
  2654. if ($goods) {
  2655. return [
  2656. 'code' => 0,
  2657. 'msg' => '商品已存在',
  2658. 'data' => [
  2659. 'id' => $goods->id
  2660. ]
  2661. ];
  2662. } else {
  2663. return [
  2664. 'code' => 0,
  2665. 'msg' => '商品已存在',
  2666. 'data' => []
  2667. ];
  2668. }
  2669. } catch (\Exception $e) {
  2670. return [
  2671. 'code' => 1,
  2672. 'msg' => $e->getMessage()
  2673. ];
  2674. }
  2675. }
  2676. }