OrderListForm.php 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  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\constants\OptionSetting;
  9. use app\librarys\Picqer\Barcode\BarcodeGeneratorJPG;
  10. use app\models\Admin;
  11. use app\models\common\admin\order\CommonOrderSearch;
  12. use app\models\common\ExpressDetail;
  13. use app\models\DeliveryInfo;
  14. use app\models\Express;
  15. use app\models\FoodFlag;
  16. use app\models\Goods;
  17. use app\models\GoodsCat;
  18. use app\models\Mch;
  19. use app\models\Md;
  20. use app\models\MdProfit;
  21. use app\models\MdStaff;
  22. use app\models\Option;
  23. use app\models\Order;
  24. use app\models\OrderDetail;
  25. use app\models\OrderForm;
  26. use app\models\OrderGoodsCancel;
  27. use app\models\OrderRefund;
  28. use app\models\OrderStateFlow;
  29. use app\models\SaasUser;
  30. use app\models\Salesman;
  31. use app\models\Shop;
  32. use app\models\Store;
  33. use app\models\StoreCloud;
  34. use app\models\User;
  35. use app\models\CloudGoodsBind;
  36. use app\models\OrderTransit;
  37. use app\models\Supplier;
  38. use app\models\VerifyCardSale;
  39. use app\models\VerifyCard;
  40. use app\models\VerifyCardAccount;
  41. use app\models\VideoShopOrderExt;
  42. use app\models\Warehouse;
  43. use app\models\WarehouseZone;
  44. use app\utils\Delivery\Delivery;
  45. use app\utils\Export;
  46. use app\utils\ExportList;
  47. use app\utils\QrCode;
  48. use app\utils\ShareQrcode;
  49. use app\utils\Tools;
  50. use yii\base\Model;
  51. use yii\db\Query;
  52. use yii\helpers\Json;
  53. use \app\models\AlipayAcitvityVoucherOrder;
  54. use app\models\DeliveryMaiyatian;
  55. use app\models\DeliveryKeloop;
  56. use app\modules\admin\models\keloop\KeloopForm;
  57. use app\models\Worker;
  58. use app\models\WorkerGoodsExt;
  59. use app\models\WorkerOrderExt;
  60. use app\models\BookingOrderExt;
  61. use app\models\MchGoodsCat;
  62. use app\modules\admin\models\MchForm;
  63. use app\models\GivingGiftsOrder;
  64. use app\models\LocalPublicRankingTask;
  65. use app\models\MchCat;
  66. use app\models\MdCategory;
  67. class OrderListForm extends Model
  68. {
  69. public $name;
  70. public $export;
  71. public $store_id;
  72. public $user_id;
  73. public $keyword;
  74. public $status;
  75. public $status_ext;
  76. public $worker_name;
  77. public $worker_tel;
  78. public $page;
  79. public $limit;
  80. public $activity_type;
  81. public $md_name;
  82. public $order_no;
  83. public $goods_name;
  84. public $mobile;
  85. public $mobiles;
  86. public $openids;
  87. public $book_name;
  88. public $book_mobile;
  89. public $is_offline;
  90. public $clerk_id;
  91. public $parent_id;
  92. public $shop_id;
  93. public $order_type;
  94. public $dateStart;
  95. public $dateEnd;
  96. public $express_type;
  97. public $keywordType;
  98. public $seller_comments;
  99. public $fields;
  100. public $type;
  101. public $mch;
  102. public $mch_id;
  103. public $mch_common_cat_id;
  104. public $flag; // 是否导出
  105. public $supplier_id;
  106. public $supplier;
  107. public $user_name;
  108. public $salesman_id;
  109. public $province_id;
  110. public $city_id;
  111. public $district_id;
  112. public $order_origin;
  113. public $send_time;
  114. public $booking_time_start;
  115. public $booking_time_end;
  116. public $worker_id;
  117. public $id;
  118. /**
  119. * @var 所属平台
  120. */
  121. public $platform;//所属平台
  122. /**
  123. * @var bool 是否是配送员订单
  124. */
  125. public $is_delivery = false;//是否是配送员订单
  126. /**
  127. * @var bool 是否是配送员订单
  128. */
  129. public $is_same_city = false;//是否是配送员订单
  130. /**
  131. * @var 获取默写城市的订单 例如:["\u5357\u5b81","\u4e0a\u6d77"]
  132. */
  133. public $shop_list;
  134. public $order_id;
  135. public $is_wastore;
  136. //麦芽田
  137. public $maiyatian;
  138. public $maiyatian_delivery_status;
  139. public $maiyatian_order_status;
  140. public $maiyatian_logistic_tag;
  141. public $cat_id = 0;
  142. //快跑者
  143. public $keloop;
  144. public $keloop_delivery_status;
  145. public $pay_type;
  146. public $pay_time_start;
  147. public $pay_time_end;
  148. public $brands_id = 0;
  149. public function rules()
  150. {
  151. return [
  152. [['keyword', 'flag'], 'trim'],
  153. [['status', 'page', 'limit', 'user_id', 'clerk_id', 'shop_id', 'keywordType', 'platform', 'order_type', 'salesman_id', 'order_origin', 'brands_id'], 'integer'],
  154. [['status',], 'default', 'value' => -1],
  155. [['page',], 'default', 'value' => 1],
  156. [['dateStart', 'dateEnd', 'express_type', 'md_name', 'order_no', 'goods_name', 'mobile'], 'trim'],
  157. [['seller_comments', 'flag', 'user_name', 'send_time'], 'string'],
  158. [['export', 'activity_type', 'mobiles', 'openids'], 'safe'],
  159. [['fields', 'is_offline', 'is_delivery', 'shop_list', 'book_name', 'book_mobile', 'name'], 'safe'],
  160. [['maiyatian', 'maiyatian_delivery_status', 'maiyatian_order_status', 'maiyatian_logistic_tag', 'province_id', 'city_id', 'district_id', 'id'], 'safe'],
  161. [['keloop', 'keloop_delivery_status'], 'safe'],
  162. [['status_ext', 'worker_name', 'worker_tel'], 'safe'],
  163. [['booking_time_start', 'booking_time_end', 'worker_id', 'is_wastore', 'pay_type', 'pay_time_start', 'pay_time_end'], 'safe'],
  164. ['cat_id','integer'],
  165. [['mch', 'mch_id', 'mch_common_cat_id'], 'safe'],
  166. ['cat_id','default','value'=>0]
  167. ];
  168. }
  169. public function search($is_scan_pay = false, $arr = [])
  170. {
  171. if (!$this->validate()) {
  172. return [
  173. 'code' => 1,
  174. 'msg' => $this->getErrorSummary(false)[0],
  175. ];
  176. }
  177. $addSelect = [];
  178. $query = Order::find()->alias('o')->where(['!=','o.order_type', Order::ORDER_TYPE_Adopt]);
  179. if($this->id) {
  180. $oids = explode(',',$this->id);
  181. $query->andWhere(['in','o.id',$oids]);
  182. }
  183. if($this->order_type == Order::ORDER_TYPE_WORKER){
  184. $query->leftJoin(['woe' => WorkerOrderExt::tableName()], 'o.id = woe.order_id');
  185. $query->leftJoin(['w' => Worker::tableName()], 'w.id = woe.worker_id');
  186. if($this->status_ext > -1){
  187. $this->status = -1;
  188. $query->andWhere([
  189. 'woe.status_ext' => $this->status_ext,
  190. ]);
  191. }
  192. if($this->worker_name){
  193. $query->andWhere(['like', 'w.name', $this->worker_name]);
  194. }
  195. if($this->worker_tel){
  196. $query->andWhere(['like', 'w.tel', $this->worker_tel]);
  197. }
  198. }else{
  199. $query->andWhere(['!=','o.order_type', Order::ORDER_TYPE_WORKER]);
  200. }
  201. if ($this->order_type == 2) {
  202. $query->leftJoin(['boe' => BookingOrderExt::tableName()], 'o.id = boe.order_id');
  203. $addSelect = array_merge($addSelect, ['boe.worker_id', 'boe.worker_name', 'boe.status_ext', 'boe.time_sys_confirm', 'boe.user_revoke', 'boe.user_revoke_reason']);
  204. $addSelect = array_merge($addSelect, ['IF(boe.booking_time_end < "'.date('Y-m-d H:i:s').'" AND boe.status_ext < '. BookingOrderExt::STATUS_EXT_START .', 1, 0) booking_time_out']);
  205. if($this->booking_time_start){
  206. $query->andWhere(['>=', 'boe.booking_time_end', $this->booking_time_start]);
  207. }
  208. if($this->booking_time_end){
  209. $query->andWhere(['<=', 'boe.booking_time_start', $this->booking_time_end]);
  210. }
  211. if(isset($this->worker_id)){
  212. $query->andWhere(['boe.worker_id' => $this->worker_id]);
  213. }
  214. if($this->status_ext == 500){
  215. //已超时
  216. $query->andWhere(['and', ['<', 'boe.status_ext', BookingOrderExt::STATUS_EXT_START], ['<', 'boe.booking_time_end', date('Y-m-d H:i:s')]]);
  217. }else if($this->status_ext == 600){
  218. //未分配
  219. $query->andWhere(['boe.worker_id' => 0]);
  220. }else{
  221. isset($this->status_ext) && $this->status_ext > -1 && $query->andWhere(['boe.status_ext' => $this->status_ext]);
  222. }
  223. }
  224. if (!$this->user_id && empty($this->order_id)) {
  225. if ($this->order_origin) {
  226. $query->andWhere(['o.order_origin' => $this->order_origin]);
  227. }else{
  228. $query->andWhere(['!=', 'o.order_origin',Order::ORDER_SOURCE_CASHIER]);
  229. }
  230. }
  231. if(isset($this->pay_type) && $this->pay_type > -1){
  232. $query->andWhere(['o.pay_type' => $this->pay_type]);
  233. }
  234. if($this->pay_time_start){
  235. $query->andWhere(['>=', 'o.pay_time', strtotime($this->pay_time_start)]);
  236. }
  237. if($this->pay_time_end){
  238. $query->andWhere(['<=', 'o.pay_time', strtotime($this->pay_time_end)]);
  239. }
  240. if($this->is_wastore){
  241. $query->andWhere(['o.is_wastore' => 1]);
  242. }
  243. if($this->maiyatian){
  244. $queryMaiyatian = (new Query())->select('order_no')->from(DeliveryMaiyatian::tableName())->where(['store_id' => $this->store_id]);
  245. if($this->maiyatian_delivery_status){
  246. $queryMaiyatian->andWhere(['delivery_status' => $this->maiyatian_delivery_status]);
  247. }
  248. if($this->maiyatian_order_status){
  249. $queryMaiyatian->andWhere(['order_status' => $this->maiyatian_order_status]);
  250. }
  251. if($this->maiyatian_logistic_tag){
  252. $queryMaiyatian->andWhere(['logistic_tag' => $this->maiyatian_logistic_tag]);
  253. }
  254. $query->andWhere(['order_no' => $queryMaiyatian]);
  255. }
  256. if($this->keloop){
  257. $queryKeloop = (new Query())->select('order_no')->from(DeliveryKeloop::tableName())->where(['store_id' => $this->store_id]);
  258. if($this->keloop_delivery_status){
  259. $queryKeloop->andWhere(['delivery_status' => $this->keloop_delivery_status]);
  260. }
  261. $query->andWhere(['order_no' => $queryKeloop]);
  262. }
  263. if($this->activity_type == Order::ACTIVITY_TYPE_CUT_PRICE){
  264. $query->andWhere(['>', 'o.activity_cut_price_order_id', 0]);
  265. }
  266. if($this->activity_type == Order::ACTIVITY_WECHAT_ROOM){
  267. $query->andWhere(['>', 'o.activity_wechat_room_id', 0]);
  268. }
  269. if(intval($this->activity_type) === Order::ACTIVITY_PT_PRICE){
  270. $query->andWhere(['>', 'o.pt_order_id', 0]);
  271. }
  272. if (!empty($this->user_id)) {
  273. $query->andWhere(['o.user_id' => $this->user_id, 'o.trade_status' => [Order::ORDER_FLOW_NO_SEND, Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM]]);
  274. $this->mch = -1;
  275. }
  276. if (!empty($this->province_id)) {
  277. $query->andWhere(['province_id' => $this->province_id]);
  278. }
  279. if (!empty($this->city_id)) {
  280. $query->andWhere(['city_id' => $this->city_id]);
  281. }
  282. if (!empty($this->district_id)) {
  283. $query->andWhere(['district_id' => $this->district_id]);
  284. }
  285. if(intval($this->activity_type) === Order::ACTIVITY_SECKILL_PRICE){
  286. $query->andWhere(['>', 'o.seckill_order_id', 0]);
  287. }
  288. if (isset($this->mch) && $this->mch == 1) {
  289. if ($this->mch_id > 0) {
  290. $query->andWhere(['o.mch_id' => $this->mch_id]);
  291. }else{
  292. $query->andWhere(['>', 'o.mch_id', 0]);
  293. // $query->andWhere(['o.mch_id' => \app\models\Mch::find()->select('id')->where(['store_id' => get_store_id(), 'is_delete' => 0])]);
  294. }
  295. if($this->brands_id > 0){
  296. $mchList = Mch::find()->where(['is_delete' => 0, 'store_id' => get_store_id(), 'brands_id' => $this->brands_id])->select('id')->column();
  297. $query->andWhere(['o.mch_id' => $mchList]);
  298. }
  299. if (isset($this->mch_common_cat_id) && $this->mch_common_cat_id > 0) {
  300. $query->andWhere(['o.mch_id' => \app\models\Mch::find()->select('id')->where(['store_id' => $this->store_id, 'mch_common_cat_id' => $this->mch_common_cat_id])]);
  301. }
  302. }elseif (isset($this->mch) && $this->mch == -1) {
  303. //不区分入驻商和平台
  304. }else{
  305. $query->andWhere(['o.mch_id' => 0]);
  306. }
  307. if ($arr['givingGifts'] == 1) {
  308. $query->andWhere(['o.order_type' => Order::ORDER_TYPE_GIVING_GIFTS]);
  309. if(isset($arr['givingGifts_received']) && $arr['givingGifts_received'] > -1){
  310. if ($arr['givingGifts_received'] == 1) {
  311. $query->andWhere(['>', 'o.giving_gifts_received_user_id', 0]);
  312. } else {
  313. $query->andWhere(['o.giving_gifts_received_user_id' => 0]);
  314. }
  315. }
  316. }else{
  317. // $query->andWhere(['!=', 'o.order_type', Order::ORDER_TYPE_GIVING_GIFTS]);
  318. }
  319. if ($arr['localPublicRanking'] == 1) {
  320. $queryPR = LocalPublicRankingTask::find()->select('order_id')->where(['store_id' => $this->store_id]);
  321. if($arr['localPublicRanking_parentNickname']){
  322. $queryPR->andWhere(['parent_user_id' => User::find()->select('id')->where(['like', 'nickname', $arr['localPublicRanking_parentNickname']])]);
  323. }
  324. $query->andWhere(['o.id' => $queryPR]);
  325. }
  326. if (get_md_id() > 0) {
  327. $query->andWhere(['o.md_id' => get_md_id()]);
  328. }
  329. if (!empty($this->order_type) && in_array($this->order_type, [0, 1, 2, 3, Order::ORDER_TYPE_WORKER, Order::ORDER_TYPE_INTEGRAL])) {
  330. $query->andWhere([
  331. 'o.order_type' => $this->order_type
  332. ]);
  333. }
  334. $admin = get_admin();
  335. $admin_id = $admin->id;
  336. if ($admin->username == 'admin') {
  337. $admin_id = null;
  338. }
  339. $order_ids = [];
  340. if ($this->supplier_id && !$this->type) {
  341. $supplier = Supplier::findOne(['cloud_supplier_id' => $this->supplier_id, 'is_delete' => 0]);
  342. if ($supplier) {
  343. $order_ids = OrderTransit::find()->alias('ot')->leftJoin(['o' => Order::tableName()], 'ot.order_id = o.id')
  344. ->where(['o.store_id' => $this->store_id, 'ot.cloud_supplier_id' => $supplier->cloud_supplier_id])->select('ot.order_id')->column();
  345. }
  346. $query->andWhere(['o.id' => $order_ids]);
  347. }
  348. if ($admin_id && !in_array($admin->type, [Admin::ADMIN_TYPE_SAAS_STAFF, Admin::ADMIN_TYPE_STAFF])) {
  349. if ($this->store_id < 0) {
  350. $salesman_id = $this->salesman_id;
  351. $salesman_where = [
  352. 'admin_id' => $admin_id,
  353. 'is_delete' => 0
  354. ];
  355. if ($salesman_id) {
  356. $salesman_where['id'] = $salesman_id;
  357. }
  358. $ids = Salesman::find()->where($salesman_where)->select('id')->asArray()->all();
  359. $admin_model = Admin::findOne($admin_id);
  360. $area_level = $admin_model->area_level;
  361. $store_query = Store::find()->where(['is_delete' => 0]);
  362. if ($this->salesman_id) {
  363. $store_query->andWhere(['in', 'salesman_id', array_column($ids, 'id')]);
  364. } else {
  365. if($area_level == 1){
  366. $store_query->andWhere([
  367. 'or',
  368. ['province_id' => $admin_model->province_id, 'city_id' => $admin_model->city_id, 'district_id' => $admin_model->district_id],
  369. ['admin_id' => $admin_id],
  370. ['in', 'salesman_id', array_column($ids, 'id')]
  371. ]);
  372. } elseif ($area_level == 2){
  373. $store_query->andWhere([
  374. 'or',
  375. ['province_id' => $admin_model->province_id, 'city_id' => $admin_model->city_id],
  376. ['admin_id' => $admin_id],
  377. ['in', 'salesman_id', array_column($ids, 'id')]
  378. ]);
  379. } elseif ($area_level == 3){
  380. $store_query->andWhere([
  381. 'or',
  382. ['province_id' => $admin_model->province_id],
  383. ['admin_id' => $admin_id],
  384. ['in', 'salesman_id', array_column($ids, 'id')]
  385. ]);
  386. } else {
  387. $store_query->andWhere([
  388. 'or',
  389. ['admin_id' => $admin_id],
  390. ['in', 'salesman_id', array_column($ids, 'id')]
  391. ]);
  392. }
  393. }
  394. $ids = $store_query->select('id')->column();
  395. }
  396. }
  397. if (!empty($this->name)) {
  398. $query->andWhere(['o.name' => $this->name]);
  399. }
  400. if (!$this->type) {
  401. if (!empty($ids) || ($admin->type == Admin::ADMIN_TYPE_BD_AGENT && $this->store_id < 0)) {
  402. $query->andWhere(['o.store_id' => $ids]);
  403. } else {
  404. $query->andWhere([
  405. 'o.store_id' => $this->store_id
  406. ]);
  407. }
  408. }
  409. $query->leftJoin(['u' => User::tableName()], 'u.id = o.user_id')
  410. ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id=o.id');
  411. switch ($this->status) {
  412. case 0:
  413. $query->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['o.is_delete' => Order::IS_DELETE_FALSE, 'o.is_pay' => Order::IS_PAY_FALSE]);
  414. break;
  415. case 1:
  416. $query->andWhere([
  417. 'o.trade_status' => Order::ORDER_FLOW_NO_SEND,
  418. 'o.is_delete' => Order::IS_DELETE_FALSE
  419. ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]);
  420. break;
  421. case 2:
  422. $query->andWhere([
  423. 'o.trade_status' => Order::ORDER_FLOW_SEND,
  424. 'o.is_delete' => Order::IS_DELETE_FALSE
  425. ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]);
  426. break;
  427. case 3:
  428. $query->andWhere([
  429. 'o.trade_status' => Order::ORDER_FLOW_CONFIRM,
  430. 'o.is_delete' => Order::IS_DELETE_FALSE
  431. ])->andWhere(['or', ['o.is_pay' => Order::IS_PAY_TRUE], ['o.pay_type' => Order::PAY_TYPE_COD]]);
  432. break;
  433. case 4:
  434. break;
  435. case 5:
  436. $query->andWhere(['or', ['o.trade_status' => Order::ORDER_FLOW_CANCEL],
  437. ['o.is_delete' => Order::IS_DELETE_TRUE]]);
  438. break;
  439. case 6:
  440. $query->andWhere(['and',['o.apply_delete' => Order::ORDER_APPLY_DELETE], ['!=', 'o.trade_status', Order::ORDER_FLOW_CANCEL]]);
  441. break;
  442. case 7:
  443. $query->andWhere(['o.apply_delete' => Order::ORDER_APPLY_DELETE, 'o.trade_status' => Order::ORDER_FLOW_NO_SEND]);
  444. break;
  445. default:
  446. if (empty($this->order_id)) {
  447. $query->andWhere([
  448. 'o.is_delete' => Order::IS_DELETE_FALSE
  449. ]);
  450. }
  451. break;
  452. }
  453. if (get_params('is_recycle')) {
  454. $query->andWhere([
  455. 'o.is_recycle' => Order::IS_DELETE_TRUE
  456. ]);
  457. } else {
  458. $query->andWhere([
  459. 'o.is_recycle' => Order::IS_DELETE_FALSE
  460. ]);
  461. }
  462. if ($this->order_id && $this->order_id > 0) {
  463. $query->andWhere(['o.id' => $this->order_id]);
  464. }
  465. // TODO:
  466. // $commonOrderSearch = new CommonOrderSearch();
  467. if (!$this->order_id) {
  468. // $query = $commonOrderSearch->search($query, $this);
  469. // $query = $commonOrderSearch->keyword($query, $this->keywordType, $this->keyword);
  470. if ($this->md_name) {
  471. $query->leftJoin(['md' => Md::tableName()], 'md.id=o.md_id')->andWhere(['like', 'md.name', $this->md_name]);
  472. }
  473. if ($this->order_no) {
  474. $query->andWhere(['like', 'o.order_no', $this->order_no]);
  475. }
  476. if ($this->mobile) {
  477. $query->andWhere([
  478. 'or',
  479. ['like', 'o.mobile', $this->mobile],
  480. ['like', 'u.binding', $this->mobile],
  481. ]);
  482. }
  483. if ($this->goods_name) {
  484. $query->andWhere(['like', 'od.goods_name', $this->goods_name]);
  485. }
  486. if ($this->user_name) {
  487. $query->andWhere([
  488. 'or',
  489. ['like', 'u.nickname', $this->user_name],
  490. ['u.binding' => SaasUser::find()->select('mobile')->where(['like', 'name', $this->user_name])],
  491. ['o.giving_gifts_received_user_id' => User::find()->select('id')->where(['binding' => SaasUser::find()->select('mobile')->where(['like', 'name', $this->user_name])])],
  492. ]);
  493. }
  494. }
  495. if ($this->dateStart) {
  496. $query->andWhere(['>=', 'o.created_at', strtotime($this->dateStart)]);
  497. }
  498. if ($this->dateEnd) {
  499. $query->andWhere(['<=', 'o.created_at', strtotime($this->dateEnd)]);
  500. }
  501. // TODO 这里处理自提订单中的分类搜索
  502. if($this->cat_id > 0){
  503. // 获取所有绑定这个分类ID的商品ID
  504. $goodsID = GoodsCat::find()->where(['is_delete'=>0,'cat_id'=>$this->cat_id])->select('goods_id')->column();
  505. if(!empty($goodsID)){
  506. $query->andWhere(['od.goods_id' => $goodsID]);
  507. }
  508. }
  509. if ($this->flag == Export::EXPORT) {
  510. $query_ex = clone $query;
  511. $list_ex = $query_ex;
  512. $export = new ExportList();
  513. $export->is_offline = $this->is_offline;
  514. $export->is_delivery = $this->is_delivery;
  515. $export->order_type = 0;
  516. $export->fields = $this->fields;
  517. $export->dataTransform_new($list_ex);
  518. }
  519. if (!$this->order_id) {
  520. if(isset($this->is_offline)){
  521. $query->andWhere(['o.is_offline' => $this->is_offline]);
  522. }
  523. if(!get_md_id() && isset($this->is_delivery)){
  524. $query->andWhere(['o.is_delivery' => intval($this->is_delivery)]);
  525. }
  526. }
  527. if ($this->platform !== null && $this->platform !== '') {
  528. $query->andWhere([
  529. 'o.order_origin' => $this->platform
  530. ]);
  531. }
  532. if($this->book_name){
  533. $query->andWhere(['like', 'o.book_info', $this->book_name]);
  534. }
  535. if($this->book_mobile){
  536. $query->andWhere(['like', 'o.book_info', $this->book_mobile]);
  537. }
  538. // 发货时间筛选
  539. if($this->send_time){
  540. $query->andWhere(['o.arrival_time' => strtotime($this->send_time)]);
  541. }
  542. $refundQuery1 = OrderRefund::find()->alias('or')
  543. ->select('or.status, or.order_id, or.created_at')
  544. ->where(['or.store_id' => $this->store_id, 'or.is_delete' => 0]);
  545. $refundQuery = (new Query())->from(['or' => $refundQuery1])->where('`or`.order_id=o.id')
  546. ->select('or.status')
  547. ->orderBy(['or.created_at' => SORT_DESC])
  548. ->limit(1);
  549. if ($this->mobiles || $this->openids) {
  550. if($this->mobiles){
  551. $_where = [
  552. 'or',
  553. ['o.mobile' => $this->mobiles],
  554. ['u.binding' => $this->mobiles],
  555. ];
  556. if($this->openids){
  557. $_where[] = [
  558. 'u.wechat_open_id' => $this->openids,
  559. ];
  560. }
  561. }else{
  562. $_where = [
  563. 'u.wechat_open_id' => $this->openids,
  564. ];
  565. }
  566. $query->andWhere($_where);
  567. }
  568. $query->orderBy('o.id DESC')
  569. ->addSelect(['o.*', 'u.nickname as de_name', 'u.platform', 'u.binding', 'u.avatar_url',
  570. 'refund' => $refundQuery, 'od.delivery_type', 'od.attr', 'o.name user_name', 'o.mobile user_mobile', 'o.address user_address', 'od.order_transit_id']);
  571. $query->addSelect($addSelect);
  572. $query->groupBy('o.id');
  573. $sumQuery = clone $query;
  574. $sumPayPrice = $sumQuery->sum('pay_price');
  575. $sumDailyQuery = clone $query;
  576. $sumDailyQuery->andWhere([
  577. 'and',
  578. ['>=', 'o.created_at', strtotime(date('Y-m-d 00:00:00'))],
  579. ['<=', 'o.created_at', strtotime(date('Y-m-d 23:59:59'))],
  580. ]);
  581. $sumDaily = $sumDailyQuery->sum('pay_price');
  582. $pagination = pagination_make($query);
  583. $listArray = $pagination['list'];
  584. if (isset($this->mch) && $this->mch == 1) {
  585. $mchList = (new MchForm(['store_id' => $this->store_id]))->mchSelectList();
  586. $mchCommonCatSelectList = (new MchForm(['store_id' => get_store_id()]))->mchCommonCatSelectList();
  587. }
  588. $store_delivery_time = (Option::get(OptionSetting::STORE_DELIVERY_TIME, $this->store_id, 0)['value']);
  589. if ($store_delivery_time) {
  590. $store_delivery_time = time() - ((int)$store_delivery_time * 86400);
  591. }
  592. foreach ($listArray as $i => &$item) {
  593. $item['is_store_delivery_time_out'] = $item['send_time'] < $store_delivery_time ? 1 : 0;
  594. if (isset($this->mch) && $this->mch == 1) {
  595. $item['mch'] = $mchList['data'][$item['mch_id']];
  596. $item['mch_common_cat'] = $mchCommonCatSelectList['data'][$item['mch']['mch_common_cat_id']];
  597. }
  598. $deliveryModel = DeliveryKeloop::findOne(['order_no' => $item['order_no']]);
  599. if($deliveryModel){
  600. $delivery = $deliveryModel->attributes;
  601. $delivery['delivery_status_name'] = KeloopForm::$order_status_list[$delivery['delivery_status']] ?? '';
  602. $item['delivery'] = $delivery;
  603. }
  604. $item['order_form'] = OrderForm::findAll(['order_id' => $item['id']]);
  605. if (get_md_id() > 0) {
  606. $md_profit = MdProfit::findOne(['order_id' => $item['id']]);
  607. $item['md_profit'] = [
  608. 'sale_profit' => $md_profit->sale_profit ?: '0.00',
  609. 'pay_profit' => $md_profit->pay_profit ?: '0.00',
  610. 'clerk_profit' => $md_profit->clerk_profit ?: '0.00',
  611. 'total_profit' => $md_profit->total_profit ?: '0.00'
  612. ];
  613. }
  614. $item['mobile'] = $item['mobile'] ?: $item['binding'];
  615. $item['book'] = [];
  616. if ($item['order_type'] == 1) {
  617. $attr = Json::decode($item['attr']);
  618. if (count($attr) > 1) {
  619. $date_data = current($attr)['date'] . '/' . end($attr)['date'];
  620. } else {
  621. $date_data = $attr[0]['date'];
  622. }
  623. $item['book'] = [
  624. 'date' => $date_data
  625. ];
  626. }
  627. $item['nickname'] = $item['de_name'];
  628. $item['avatar'] = $item['avatar_url'];
  629. if ($item['order_type'] == 2) {
  630. $tmp_attr = Json::decode($item['attr']);
  631. $item['book']['date'] = $tmp_attr['date'] . ' ' . $tmp_attr['time'];
  632. }
  633. $rmList = [];
  634. if ($this->order_id && $this->order_id > 0) {
  635. $rmQuery = \app\models\OrderRefundMoney::find()->where(['is_delete' => 0, 'order_id' => $this->order_id]);
  636. $rmList = $rmQuery->orderBy('id DESC')->asArray()->all();
  637. foreach($rmList as &$k){
  638. $k['created_at'] = date('Y-m-d H:i:s', $k['created_at']);
  639. }
  640. }
  641. $item['rmList'] = $rmList;
  642. if (!empty($item['food_flag_id'])) {
  643. $food_flag = FoodFlag::findOne($item['food_flag_id']);
  644. $item['food_table_num'] = $food_flag->table_num;
  645. }
  646. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  647. $item['future_sales_time'] = $item['future_sales_time'] > 0 ? date('Y-m-d H:i:s', $item['future_sales_time']) : 0;
  648. $item['arrival_time_text'] = date('Y-m-d H:i:s', $item['arrival_time']);
  649. $item['goods_list'] = $this->getOrderGoodsList($item['id']);
  650. $item['clerk_name'] = '';
  651. $shop = Shop::findOne($item['shop_id']);
  652. if ($shop) {
  653. $user = User::findOne(Shop::findOne($item['shop_id'])->user_id);
  654. if ($user) {
  655. $item['clerk_name'] = $user->nickname;
  656. }
  657. }
  658. $item['integral'] = json_decode($item['integral'], true);
  659. if (isset($item['address_data'])) {
  660. $item['address_data'] = json_decode($item['address_data']);
  661. }
  662. $item['flag'] = 0;
  663. $item['delivery_info'] = [];
  664. // 同城配送配送信息
  665. if ($item['delivery_type'] == OrderDetail::GOODS_DELIVERY_IM) {
  666. $delivery_info = DeliveryInfo::findOne(['order_no' => $item['order_no']]);
  667. $item['delivery_info'] = [
  668. 'waybill_id' => $delivery_info->waybill_id,
  669. 'status' => Delivery::$validOrderStatus[$delivery_info->status],
  670. 'reason' => $delivery_info->reason_id > 0 ? Delivery::$validReason[$delivery_info->reason_id] : '',
  671. 'fee' => $delivery_info->fee,
  672. 'duct_fee' => $delivery_info->deduct_fee,
  673. 'delivery_type' => Delivery::$deliveryType[$delivery_info->delivery_type],
  674. 'rider_name' => $delivery_info->rider_name,
  675. 'rider_mobile' => $delivery_info->rider_mobile
  676. ];
  677. $item['delivery_time'] = date('Y-m-d H:i:s',$item['delivery_time']);
  678. }
  679. if($item['delivery_info']){
  680. $item['pay_price'] > 0 && $item['express_price'] += $item['delivery_info']['fee'];
  681. // $item['pay_price'] > 0 && $item['total_price'] += $item['delivery_info']['fee'];
  682. // $item['pay_price'] > 0 && $item['pay_price'] += $item['delivery_info']['fee'];
  683. }
  684. $item['md_info'] = null;
  685. if ($item['md_id']) {
  686. $item['md_info'] = Md::find()->where(['id' => $item['md_id']])
  687. ->select('id, name, address, cover_url, start_time, end_time, contact, mobile, open_status, shop_time_type, is_single, manager, cat_id')->asArray()->one();
  688. $managerUser = SaasUser::findOne($item['md_info']['manager']);
  689. $item['md_info']['manager_name'] = $managerUser ? $managerUser->name : '-';
  690. $item['md_info']['manager_mobile'] = $managerUser ? $managerUser->mobile : '-';
  691. $mdCat = MdCategory::findOne($item['md_info']['cat_id']);
  692. $item['md_info']['md_cat_name'] = $mdCat ? $mdCat->name : '-';
  693. }
  694. $item['pay_user_info'] = null;
  695. if ($item['pay_user_id'] > 0) {
  696. $pay_saas_user = SaasUser::findOne(['mobile' => User::findOne($item['pay_user_id'])->binding]);
  697. $item['pay_user_info'] = [
  698. 'name' => $pay_saas_user->name,
  699. 'mobile' => $pay_saas_user->mobile
  700. ];
  701. }
  702. $item['all_discount'] = sprintf("%.2f", ($item['integral']['forehead'] + $item['coupon_sub_price']));
  703. $item['alipay_activity_voucher'] = AlipayAcitvityVoucherOrder::getByOrder($item['alipay_trade_no']);
  704. if ($item['store_id']) {
  705. // 增加查询商城的联系电话
  706. $item['store'] = Store::find()->where(['id' => $item['store_id']])->select('id, name, province_id, city_id, district_id, address,contact_tel')->one();
  707. }
  708. $diy_shop_name = Option::get(OptionSetting::DIY_SHOP_NAME, $item['store_id'], 'store', '自提配送')['value'] ?: '自提配送';
  709. $diy_shop_name = Option::get(OptionSetting::DIY_SHOP_NAME, $item['store_id'], 'pay', $diy_shop_name)['value'];
  710. $diy_express_name = Option::get(OptionSetting::DIY_EXPRESS_NAME, $item['store_id'], 'store', '快递配送')['value'] ?: '快递配送';
  711. $diy_express_name = Option::get(OptionSetting::DIY_EXPRESS_NAME, $item['store_id'], 'pay', $diy_express_name)['value'];
  712. $diy_delivery_name = Option::get(OptionSetting::DIY_DELIVERY_NAME, $item['store_id'], 'store', '同城配送')['value'] ?: '同城配送';
  713. $diy_delivery_name = Option::get(OptionSetting::DIY_DELIVERY_NAME, $item['store_id'], 'pay', $diy_delivery_name)['value'];
  714. $item['diy_express_name'] = intval($item['is_offline']) === 1 ? $diy_shop_name : (intval($item['is_delivery']) === 1 ? $diy_delivery_name : $diy_express_name) ;
  715. if (empty($item['address']) && (int)$item['is_offline'] && !$item['mch_id']) {
  716. if (!empty($item['store'])) {
  717. $item['address'] = '配送到门店:' . $item['store']['name'];
  718. }
  719. if (!empty($item['md_info'])) {
  720. $item['address'] = '配送到门店:' . $item['md_info']['name'];
  721. }
  722. }
  723. // 单独查询一个订单的时候再处理这些数据 避免订单列表卡
  724. if($this->order_id > 0){
  725. $item['pay_type_name'] = Order::PAY_TYP_NAME[$item['pay_type']];
  726. // 优惠明细 目前优惠有两种 优惠券优惠 和 积分抵扣
  727. $item['discount_text'] = '';
  728. if($item['integral']['forehead'] > 0 && $item['integral']['forehead_integral'] > 0){
  729. $item['discount_text'] .= '使用'.$item['integral']['forehead_integral'].'积分抵扣'.$item['integral']['forehead'].'元;';
  730. }
  731. if($item['coupon_sub_price'] > 0){
  732. $item['discount_text'] .= '使用优惠券抵扣'.$item['coupon_sub_price'].'元;';
  733. }
  734. // todo 小程序码和二维码
  735. $filename = 'order_id_'.$item['id'];
  736. try {
  737. // if (!$this->order_id) {
  738. $scene = "id:{$item['id']},store:{$item['store_id']}";
  739. $miniQrcode = ShareQrcode::wxQrcode('order/order-detail/order-detail', $scene);
  740. $item['qrcode'] = [
  741. 'mini_qrcode'=>$miniQrcode['url_path'],
  742. 'qrcode' => str_replace('http://', 'http://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename. '.jpg')
  743. ];
  744. //}
  745. } catch (\Exception $e) {
  746. $item['qrcode'] = [
  747. 'mini_qrcode'=>'',
  748. 'qrcode' => ''
  749. ];
  750. }
  751. $scene .= ",order_no:{$item['order_no']}";
  752. $text = \Yii::$app->request->hostInfo . '/h5/#/order/order-detail/order-detail?scene=' . $scene;
  753. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  754. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  755. }
  756. //处理订单是否存在云仓转单商品开始
  757. //添加转单详情
  758. //一个订单一个供货商只能创建一次转单
  759. // $transitInfo = OrderTransit::find()->where(['order_id'=>$item['id'],'is_delete'=>0])->all();
  760. // if($transitInfo){
  761. // $orderTransitInfo = [];
  762. // foreach($transitInfo as $transitInfoVal){
  763. // $orderTransitInfo[] = $transitInfoVal->attributes;
  764. // }
  765. // $listArray[$i]['orderTransitInfo'] = $orderTransitInfo;
  766. // }
  767. // $verify_card = [];
  768. $item['is_front_delivery'] = 0;
  769. $integral = 0;
  770. foreach($item['goods_list'] as $goodskey=>&$goods_info){
  771. if (isset($goods_info['is_front_delivery'])) {
  772. $item['is_front_delivery'] = $goods_info['is_front_delivery'];
  773. }
  774. $goods = Goods::findOne($goods_info['goods_id']);
  775. $warn_goods_timeout = Goods::warn_goods_timeout($item['store_id']);
  776. $goods_info['timeout_day'] = $goods->time_made_day ? date('Y-m-d', $goods->time_made_day + 86400 * $goods->time_shelf_life) : '--';
  777. $goods_info['is_warn_goods_timeout'] = ($goods->time_made_day && ($goods->time_made_day + 86400 * $goods->time_shelf_life) < (time() + $warn_goods_timeout * 86400)) ? 1 : 0;
  778. if($goods_info['goods_id'] && $goods_info['attr']){
  779. $goods_info['eipl'] = \app\models\ErpInventoryProp::logFind($item, $goods_info['goods_id'], json_decode($goods_info['attr'], true));
  780. $cards = Goods::find()->where(['id' => $goods_info['goods_id']])->select('verify_card_id, integral')->one();
  781. // if (!empty($cards->verify_card_id)) {
  782. // $goods_model = Goods::findOne($goods['goods_id']);
  783. // $ids = explode(',', $cards->verify_card_id);
  784. // $order = Order::findOne($item['id']);
  785. // $card = Tools::getVerifyList($ids, $order, $goods_model);
  786. // $verify_card = array_merge($verify_card, $card);
  787. // }
  788. if (!empty($cards->integral)) {
  789. $goods_integral = json_decode($cards->integral, true);
  790. $integral += (int)$goods_integral['give'];
  791. }
  792. if ((int)$item['is_pay'] !== 0 || intval($item['pay_type']) === 2) {
  793. $cloudGoodsInfo = CloudGoodsBind::find()->where(['goods_id' => $goods_info['goods_id'], 'is_delete' => 0])->one();
  794. if ($cloudGoodsInfo && isset($cloudGoodsInfo['cloud_goods_id'])) {
  795. $cloudInfo = [];
  796. $cloudInfo['cloud_goods_id'] = $cloudGoodsInfo['cloud_goods_id'];
  797. $cloudInfo['cloud_attr_id'] = $cloudGoodsInfo['cloud_attr_id'];
  798. $cloudInfo['cloud_supplier_id'] = $cloudGoodsInfo['cloud_supplier_id'];
  799. if ($cloudGoodsInfo['cloud_supplier_id'] > 0) {
  800. $supplierInfo = Supplier::find()->select('name,logo')->where(['cloud_supplier_id' => $cloudGoodsInfo['cloud_supplier_id'], 'is_delete' => 0])->one();
  801. if ($supplierInfo && $supplierInfo['name'] && $supplierInfo['logo']) {
  802. $cloudInfo['cloud_supplier_name'] = $supplierInfo['name'];
  803. $cloudInfo['cloud_supplier_logo'] = $supplierInfo['logo'];
  804. }
  805. }
  806. $listArray[$i]['goods_list'][$goodskey]['cloud_bind'] = json_encode($cloudInfo);
  807. if (intval($item['apply_delete']) === 0) {
  808. $listArray[$i]['is_cloud_bind'] = intval($item['trade_status']) === 0;
  809. }
  810. }
  811. }
  812. }
  813. }
  814. if ((int)$item['is_pay'] !== 0 || intval($item['pay_type']) === 2) {
  815. // $item['verify_card'] = $verify_card;
  816. $cards = [];
  817. if ($item['get_verify_id']) {
  818. $get_verify_id = explode(',', $item['get_verify_id']);
  819. $VerifyCardSale = VerifyCardSale::find()->alias('vcs')->where([
  820. 'vcs.id' => $get_verify_id,
  821. ])
  822. ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id')
  823. ->leftJoin(['vca' => VerifyCardAccount::tableName()], 'vcs.account_id = vca.id')
  824. ->select('vca.account, vc.begin_time, vc.bg_pic_url, vc.business_status, vc.business_type, vc.content
  825. , vc.created_at, vc.date_type, vc.end_time, vc.expire_day, vc.freight_id, vcs.id, vc.is_business
  826. , vc.is_give, vc.money, vc.name, vc.num, vca.password, vc.pic_url, vc.price, vcs.id sale_id, vcs.status sale_status
  827. , vc.savingsType, vc.sort, vc.status, vc.total_num, vc.total_price, vc.type, vcs.use_num, vc.video_ids
  828. , vcs.video_status, vcs.goods_id')->asArray()->all();
  829. if ($VerifyCardSale) {
  830. $cards = $VerifyCardSale;
  831. }
  832. }
  833. $item['verify_card'] = $cards;
  834. // $item['integral'] = $integral;
  835. }
  836. if($item['order_type'] == Order::ORDER_TYPE_WORKER){
  837. $woe = WorkerOrderExt::findOne(['order_id' => $item['id']]);
  838. $item['woe'] = $woe;
  839. $item['w'] = $woe ? Worker::findOne($woe['worker_id']) : null;
  840. foreach($item['goods_list'] as &$good){
  841. $wge = WorkerGoodsExt::findOne(['goods_id' => $good['goods_id']]);
  842. $good['wge'] = $wge;
  843. }
  844. }
  845. $item['givingGiftsOrderData'] = \app\modules\admin\models\givingGifts\GivingGiftsForm::givingGiftsOrderData($item);
  846. if ($arr['localPublicRanking'] == 1) {
  847. $item['LocalPublicRankingTask'] = LocalPublicRankingTask::findOne(['order_id' => $item['id']]);
  848. $item['LocalPublicRankingTask_parent'] = User::findOne($item['LocalPublicRankingTask']['parent_user_id']);
  849. }
  850. if($item['pt_order_id']){
  851. $pt = \app\models\PtActivityOrder::findOne($item['pt_order_id']);
  852. if($pt->pt_number){
  853. $item['pt_order_id'] = $pt->pt_number;
  854. }
  855. }
  856. if($item['confirm_time'] && !$item['send_time']){
  857. $item['send_time'] = $item['confirm_time'];
  858. }
  859. /*新增是否是视频号小店订单的标识*/
  860. $item['is_video_shop_order'] = VideoShopOrderExt::find()->where(['order_id' => $item['id']])->exists();
  861. /*新增是否是视频号小店订单的标识*/
  862. //获取退款的总商品金额 / 获取最新一条的状态
  863. $handle_cancel = OrderGoodsCancel::find()->where(['order_id' => $item['id'], 'status' => [
  864. OrderGoodsCancel::STATUS_APPLY, OrderGoodsCancel::STATUS_PAY_FAIL
  865. ]])->select('SUM(num) as num, SUM(refund_price) as refund_price')->groupBy('order_id')->asArray()->one() ?: null;
  866. $item['handle_cancel'] = $handle_cancel ?: null;
  867. $item['cancel_refund_price'] = OrderGoodsCancel::find()->where([
  868. 'order_id' => $item['id'],
  869. 'status' => [
  870. OrderGoodsCancel::STATUS_PASS,
  871. OrderGoodsCancel::STATUS_PAY,
  872. OrderGoodsCancel::STATUS_PAY_FAIL
  873. ]
  874. ])->sum('refund_price') ?: 0;
  875. }
  876. // 获取可导出数据
  877. $f = new ExportList();
  878. $exportList = $f->getList();
  879. if($this->export){
  880. return $this->export($listArray);
  881. }
  882. //判断是否申请云仓
  883. $is_cloud = false;
  884. if (\Yii::$app->isSaas()) {
  885. $store_cloud = StoreCloud::find()->where(['store_id' => get_store_id()])->one();
  886. if ($store_cloud) {
  887. $is_cloud = true;
  888. }
  889. }
  890. //处理订单是否存在云仓转单商品结束
  891. return [
  892. 'code' => 0,
  893. 'msg' => 'success',
  894. 'data' => [
  895. 'mch_id'=>$this->mch_id,
  896. 'q' => $query->createCommand()->getRawSql(),
  897. 'sumPayPrice' => $sumPayPrice,
  898. 'sumDaily' => $sumDaily,
  899. 'export_list' => $exportList,
  900. 'express_list' => $this->getExpressList(),
  901. 'is_cloud' => $is_cloud,
  902. 'data' => $listArray,
  903. 'pageNo' => $pagination['pageNo'],
  904. 'pageSize' => $pagination['pageSize'],
  905. 'totalCount' => $pagination['totalCount'],
  906. 'print_setting' => Option::getPrintOrderSetting()
  907. ],
  908. ];
  909. }
  910. private function export($list) {
  911. $rows = [[
  912. 'ID',
  913. '会员信息',
  914. '订单号',
  915. '订单状态',
  916. '总金额(元)',
  917. '运费',
  918. '优惠金额',
  919. '实际支付(元)',
  920. '是否已付款',
  921. '快递名称',
  922. '快递单号',
  923. '快递状态',
  924. '收货人',
  925. '收货人电话',
  926. '收货人地址',
  927. '下单门店',
  928. '门店ID',
  929. '门店店长',
  930. '门店联系人',
  931. '门店店长联系方式',
  932. '门店分类',
  933. '商品',
  934. '规格',
  935. '数量',
  936. '成本价',
  937. '售价',
  938. '单价',
  939. '订单备注',
  940. '供货商信息',
  941. '时间',
  942. ]];
  943. foreach($list as $item){
  944. $tradStatus = '';
  945. if($item['order_type'] == 1){
  946. if(in_array($item['trade_status'], [0,2])){
  947. $tradStatus = '待使用';
  948. }
  949. }
  950. if ($item['trade_status'] == 1) {
  951. $tradStatus = '已取消';
  952. }
  953. if ($item['is_pay'] == 0) {
  954. $tradStatus = '未支付';
  955. }
  956. if ($item['is_pay'] == 1 && $item['trade_status']== 0) {
  957. $tradStatus = '待发货';
  958. }
  959. if ($item['is_pay'] == 1 && $item['trade_status'] == 2) {
  960. $tradStatus = '待收货';
  961. }
  962. if ($item['is_pay'] == 1 && $item['trade_status'] == 3) {
  963. $tradStatus = '已完成';
  964. }
  965. $goods = [];
  966. $supplier_info = '';
  967. $cost_price = 0;
  968. foreach ($item['goods_list'] as $i => $gitem) {
  969. $goods_info = json_decode($gitem['goods_info'], true);
  970. $goods_info || $goods_info = Goods::findOne($gitem['goods_id']);
  971. $supplier = Supplier::findOne(['cloud_supplier_id' => $goods_info['cloud_supplier_id']]);
  972. $supplier_info = $supplier->supplier_name;
  973. $attr = [];
  974. $order_attr = array_column($gitem['attr_list'], 'attr_id');
  975. sort($order_attr);
  976. $goods_attr = json_decode($goods_info['attr'], true);
  977. $attr_cost_price = '0.00';
  978. // $attr_goods_price = '0.00';
  979. $attr_goods_price = $gitem['price'];
  980. foreach ($goods_attr as $value) {
  981. $goods_attr_id = array_column($value['attr_list'], 'attr_id');
  982. sort($goods_attr_id);
  983. if (!array_diff($goods_attr_id, $order_attr)) {
  984. $attr_cost_price = $value['cost_price'] ?? $value['price'];
  985. // $attr_goods_price = $value['price'] ?: 0.00;
  986. }
  987. }
  988. $attr_cost_price = $attr_cost_price <= 0 ? '暂无数据' : sprintf('%.2f', $attr_cost_price);
  989. // $attr_goods_price = $attr_goods_price <= 0 ? '暂无数据' : sprintf('%.2f', $attr_goods_price);
  990. $cost_price = sprintf('%.2f', (doubleval($cost_price) + doubleval($attr_cost_price)));
  991. foreach ($gitem['attr_list'] as $atitem) {
  992. $attr[] = isset($atitem['attr_name']) ? $atitem['attr_name'] . isset($atitem['cost_price']) : '';
  993. }
  994. $goods[] = $gitem['name'] . '(' . implode(',', $attr) . ')' . '(' . $gitem['num'] . $gitem['unit'] . ')' . '(' . $gitem['total_price'] . '元)(成本价:'.$attr_cost_price.')';
  995. if($i == 0){
  996. $r = [
  997. $item['id'],
  998. $item['nickname'],
  999. $item['order_no'],
  1000. $tradStatus,
  1001. $item['total_price'],
  1002. $item['express_price'],
  1003. $item['all_discount'],
  1004. $item['pay_price'],
  1005. $item['is_pay'] ? '已付款' : '未付款',
  1006. $item['express'],
  1007. $item['express_no'],
  1008. $item['status_text'],
  1009. $item['name'],
  1010. $item['mobile'],
  1011. $item['address'],
  1012. (($item['md_info'] && $item['md_info']['name']) ? $item['md_info']['name'] : '') . '-' . $item['store']['name'],
  1013. ($item['md_info'] && $item['md_info']['id']) ? $item['md_info']['id'] : '-',
  1014. ($item['md_info'] && $item['md_info']['manager_name']) ? $item['md_info']['manager_name'] : '-',
  1015. ($item['md_info'] && $item['md_info']['contact']) ? $item['md_info']['contact'] : '-',
  1016. ($item['md_info'] && $item['md_info']['mobile']) ? $item['md_info']['mobile'] : '-',
  1017. ($item['md_info'] && $item['md_info']['md_cat_name']) ? $item['md_info']['md_cat_name'] : '-',
  1018. $gitem['name'],
  1019. implode(',', $attr),
  1020. $gitem['num'] . $gitem['unit'],
  1021. $attr_cost_price,
  1022. $attr_goods_price,
  1023. $gitem['single_price'],
  1024. $item['remark'],
  1025. $supplier_info,
  1026. $item['created_at'],
  1027. ];
  1028. $rows[] = $r;
  1029. }else{
  1030. $r = [
  1031. '',
  1032. '',
  1033. '',
  1034. '',
  1035. '',
  1036. '',
  1037. '',
  1038. '',
  1039. '',
  1040. '',
  1041. '',
  1042. '',
  1043. '',
  1044. '',
  1045. '',
  1046. '',
  1047. '',
  1048. '',
  1049. $gitem['name'],
  1050. implode(',', $attr),
  1051. $gitem['num'] . $gitem['unit'],
  1052. $attr_cost_price,
  1053. $attr_goods_price,
  1054. $gitem['single_price'],
  1055. '',
  1056. $supplier_info,
  1057. '',
  1058. ];
  1059. $rows[] = $r;
  1060. }
  1061. }
  1062. }
  1063. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  1064. ->addRows($rows)->toBrowser();
  1065. }
  1066. //获取快递信息
  1067. public function getExpressData()
  1068. {
  1069. try {
  1070. $order_id = $this->order_id;
  1071. $order = Order::find()->where(['id' => $order_id, 'is_pay' => 1])->one();
  1072. if ($order) {
  1073. // $OrderTransit = OrderTransit::findOne(['order_id' => $order->id]);
  1074. // if ($OrderTransit) {
  1075. // $order['express_name'] = $OrderTransit->express_name;
  1076. // $order['express_no'] = $OrderTransit->express_no;
  1077. // }
  1078. }
  1079. if (!empty($order['express']) && !empty($order['express_no'])) {
  1080. // 查询物流信息
  1081. $model = new ExpressDetail();
  1082. $model->express = $order['express'];
  1083. $model->express_no = $order['express_no'];
  1084. $model->receive_mobile = $order['mobile'];
  1085. $model->store_id = $order->store_id == 0 ? 1 : $this->store_id;
  1086. $res = $model->search();
  1087. if ($res['code'] != 0) {
  1088. $res['code'] = 0;
  1089. if (!$res['data']) {
  1090. $res['data'] = [];
  1091. }
  1092. $res['data']['status'] = 0;
  1093. $res['data']['status_text'] = '未知';
  1094. }
  1095. $res['data']['express'] = $order['express'];
  1096. $res['data']['express_no'] = $order['express_no'];
  1097. return $res;
  1098. }
  1099. return [
  1100. 'code' => 0,
  1101. 'msg' => "没有快递信息",
  1102. 'data' => null
  1103. ];
  1104. } catch (\Exception $e) {
  1105. return [
  1106. 'code' => 1,
  1107. 'msg' => $e->getMessage()
  1108. ];
  1109. }
  1110. }
  1111. /**
  1112. * @param $data array 需要处理的数据
  1113. */
  1114. public function dataTransform($data)
  1115. {
  1116. //TODO 测试数据 需要换成真实的字段
  1117. $newFields = [];
  1118. foreach ($this->fields as &$item) {
  1119. if ($this->is_offline == 1) {
  1120. if (in_array($item['key'], ['clerk_name', 'shop_name'])) {
  1121. $item['selected'] = 1;
  1122. }
  1123. } else {
  1124. if (in_array($item['key'], ['express_price', 'express_no', 'express'])) {
  1125. $item['selected'] = 1;
  1126. }
  1127. }
  1128. if (isset($item['selected']) && $item['selected'] == 1) {
  1129. $newFields[$item['key']] = $item['value'];
  1130. }
  1131. }
  1132. $newList = [];
  1133. foreach ($data as $datum) {
  1134. $newItem = [];
  1135. $newItem['order_no'] = $datum->order_no;
  1136. $newItem['nickname'] = $datum->user->nickname;
  1137. $newItem['name'] = $datum->name;
  1138. $newItem['mobile'] = $datum->mobile;
  1139. $newItem['address'] = $datum->address;
  1140. $newItem['total_price'] = $datum->total_price;
  1141. $newItem['pay_price'] = $datum->pay_price;
  1142. $newItem['pay_time'] = $datum->pay_time ? date('Y-m-d H:i', $datum->pay_time) : '';
  1143. $newItem['send_time'] = $datum->send_time ? date('Y-m-d H:i', $datum->send_time) : '';
  1144. $newItem['confirm_time'] = $datum->confirm_time ? date('Y-m-d H:i', $datum->confirm_time) : '';
  1145. $newItem['words'] = $datum->words;
  1146. $newItem['goods_list'] = $this->getOrderGoodsList($datum['id']);
  1147. $newItem['is_pay'] = $datum['is_pay'] == 1 ? "已付款" : "未付款";
  1148. $newItem['addtime'] = date('Y-m-d H:i', $datum['created_at']);
  1149. $newItem['express_price'] = $datum['express_price'] . "元";
  1150. $newItem['apply_delete'] = ($datum['apply_delete'] == Order::ORDER_APPLY_DELETE) ? ($datum['trade_status'] == Order::ORDER_FLOW_CANCEL || $datum['is_delete'] == Order::IS_DELETE_TRUE ? '取消成功' : '取消中') : "无";
  1151. $newItem['trade_status'] = OrderStateFlow::getFlowWords($datum['trade_status']);
  1152. // 是否到店自提 0--否 1--是
  1153. if ($datum['is_offline']) {
  1154. $newItem['clerk_name'] = $datum->clerk ? $datum->clerk->nickname : '';
  1155. $newItem['shop_name'] = $datum->shop ? $datum->shop->name : '';
  1156. } else {
  1157. $newItem['express_price'] = $datum->express_price;
  1158. $newItem['express_no'] = $datum->express_no;
  1159. $newItem['express'] = $datum->express;
  1160. }
  1161. if ($datum->orderForm) {
  1162. $str = '';
  1163. foreach ($datum->orderForm as $key => $item) {
  1164. $str .= $item['key'] . ':' . $item['value'] . ',';
  1165. }
  1166. $newItem['content'] = rtrim($str, ',');
  1167. } else {
  1168. $newItem['content'] = $datum->content;
  1169. }
  1170. $newList[] = $newItem;
  1171. }
  1172. //Export::order_3($newList, $newFields);
  1173. }
  1174. public function getOrderGoodsList($order_id, $order_detail_id = 0)
  1175. {
  1176. $order = Order::findOne($order_id);
  1177. $query = OrderDetail::find()->alias('od')
  1178. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  1179. ->leftJoin(['wh' => Warehouse::tableName()], 'wh.id = g.warehouse_id')
  1180. ->leftJoin(['whz' => WarehouseZone::tableName()], 'whz.id = g.warehouse_zone_id')
  1181. ->where([
  1182. 'od.is_delete' => 0,
  1183. 'od.order_id' => $order_id,
  1184. ]);
  1185. if ($order_detail_id) {
  1186. $query->andWhere(['od.id' => $order_detail_id]);
  1187. }
  1188. $orderDetailList = $query->select(['od.num', 'od.total_price', 'g.goods_no', 'g.time_made_day', 'g.time_shelf_life', 'od.attr', 'od.food_ext_goods', 'od.is_level', 'od.batch_price_tips', 'name' => 'od.goods_name','od.pic', 'od.goods_info', 'od.delivery_type', 'od.shop_id', 'od.goods_id', 'g.attr attrs','wh.name as wh_name','whz.name as whz_name', 'g.is_front_delivery', 'order_detail_id' => 'od.id'])->asArray()->all();
  1189. foreach ($orderDetailList as &$item) {
  1190. //获取已经取消多少件
  1191. $cancel_num = OrderGoodsCancel::getCancelGoodsOrderQuery(
  1192. $order_id,
  1193. 'SUM(num)',
  1194. 'order_detail_id',
  1195. $item['order_detail_id'])->scalar() ?: 0;
  1196. //获取可取消数量
  1197. $item['max_cancel_num'] = bcsub($item['num'], $cancel_num);
  1198. //如果是营销活动产品就不给取消
  1199. if (!OrderGoodsCancel::is_activity_goods($order_id, $item['goods_id'])) {
  1200. $item['max_cancel_num'] = 0;
  1201. }
  1202. // if (intval($order->trade_status) !== \app\models\Order::ORDER_FLOW_NO_SEND) {
  1203. // $item['max_cancel_num'] = 0;
  1204. // }
  1205. if (intval($order->is_sale) || ($order->future_sales_time > 0 && $order->future_sales_time <= time())) {
  1206. $item['max_cancel_num'] = 0;
  1207. }
  1208. $item['cancel_num'] = OrderGoodsCancel::find()->where([
  1209. 'order_detail_id' => $item['order_detail_id'],
  1210. 'status' => [
  1211. OrderGoodsCancel::STATUS_PAY,
  1212. OrderGoodsCancel::STATUS_PASS,
  1213. OrderGoodsCancel::STATUS_PAY_FAIL
  1214. ]
  1215. ])->sum('num') ?: 0;
  1216. $item['cancel_status'] = 0;
  1217. $item['cancel_text'] = '';
  1218. if ($item['cancel_num'] > 0) {
  1219. $item['cancel_text'] = '已退款' .$item['cancel_num'] .'件';
  1220. $item['cancel_status'] = 2;
  1221. if ($item['cancel_num'] >= $item['num']) {
  1222. $item['cancel_status'] = 1;
  1223. $item['cancel_text'] = '已退款';
  1224. }
  1225. }
  1226. $item['warehouse_name'] = $item['wh_name'].'-'.$item['whz_name'];
  1227. $item['form'] = OrderForm::findAll(['goods_id' => $item['goods_id'], 'order_id' => $order_id]);
  1228. $item['food_ext_goods'] = (array)json_decode($item['food_ext_goods'], true);
  1229. $item['attr_list'] = json_decode($item['attr'], true);
  1230. $item['unit'] = Json::decode($item['goods_info'])['unit'];
  1231. // $item['price'] = sprintf('%.2f', ($item['total_price'] / $item['num']));
  1232. $item['price'] = $item['total_price'] > 0 ? sprintf('%.2f', ($item['total_price'] / $item['num'])) : $item['total_price'];
  1233. $item['is_front_delivery'] = intval($item['is_front_delivery']);
  1234. // $item['attrs'] = json_decode($item['attrs'], true);
  1235. // $attr_id = array_column($item['attr_list'], 'attr_id');
  1236. // sort($attr_id);
  1237. // foreach ($item['attrs'] as $attr) {
  1238. // $attr_item_id = array_column($attr['attr_list'], 'attr_id');
  1239. // sort($attr_item_id);
  1240. // if (!array_diff($attr_item_id, $attr_id)) {
  1241. // $item['price'] = $attr['price'];
  1242. // break;
  1243. // }
  1244. // }
  1245. $oattr = json_decode($item['attr'], true);
  1246. $oattrIds = array_column($oattr, 'attr_id');
  1247. sort($oattrIds);
  1248. $gattr = json_decode($item['goods_info'], true)['attr'];
  1249. $gattr = json_decode($gattr, true);
  1250. $goods_no = '';
  1251. $item['single_price'] = $item['price'];
  1252. $item['goods_no'] = trim($goods_no ?: $item['goods_no']);
  1253. $item['code_path'] = '';
  1254. if ($item['goods_no']) {
  1255. $generator = new BarcodeGeneratorJPG();
  1256. $barcode = $generator->getBarcode($item['goods_no'], $generator::TYPE_CODE_128, 2, 30);
  1257. $filename = md5('goods_no_' . $item['goods_no']);
  1258. $code_path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  1259. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  1260. file_put_contents($code_path, $barcode);
  1261. $item['code_path'] = $pic_url;
  1262. }
  1263. $item['shop'] = [];
  1264. if ($item['delivery_type'] == OrderDetail::GOODS_DELIVERY_SHOP) {
  1265. $shop_info = Shop::find()->where(['id' => $item['shop_id']])->asArray()->one();
  1266. if(!$shop_info) continue;
  1267. $item['shop']['address'] = $shop_info['address'];
  1268. $item['shop']['id'] = $shop_info['id'];
  1269. $item['shop']['name'] = $shop_info['name'];
  1270. $item['shop']['mobile'] = $shop_info['mobile'];
  1271. }
  1272. }
  1273. return $orderDetailList;
  1274. }
  1275. public static function getCountData($store_id)
  1276. {
  1277. $form = new OrderListForm();
  1278. $form->limit = 0;
  1279. $form->store_id = $store_id;
  1280. $data = [];
  1281. $form->status = -1;
  1282. $res = $form->search();
  1283. $data['all'] = $res['row_count'];
  1284. $form->status = 0;
  1285. $res = $form->search();
  1286. $data['status_0'] = $res['row_count'];
  1287. $form->status = 1;
  1288. $res = $form->search();
  1289. $data['status_1'] = $res['row_count'];
  1290. $form->status = 2;
  1291. $res = $form->search();
  1292. $data['status_2'] = $res['row_count'];
  1293. $form->status = 3;
  1294. $res = $form->search();
  1295. $data['status_3'] = $res['row_count'];
  1296. $form->status = 5;
  1297. $res = $form->search();
  1298. $data['status_5'] = $res['row_count'];
  1299. return $data;
  1300. }
  1301. /**
  1302. * @return array
  1303. */
  1304. public function getExpressList()
  1305. {
  1306. // $storeExpressList = Order::find()
  1307. // ->select('express')
  1308. // ->where([
  1309. // 'and',
  1310. // ['store_id' => $this->store_id],
  1311. // ['is_pay' => Order::IS_PAY_TRUE],
  1312. // ['in', 'trade_status', [Order::ORDER_FLOW_SEND, Order::ORDER_FLOW_CONFIRM]],
  1313. // ['!=', 'express', ''],
  1314. // ])->groupBy('express, send_time')->orderBy('send_time DESC')->limit(5)->asArray()->all();
  1315. $expressLst = Express::getExpressList();
  1316. // $newStoreExpressList = [];
  1317. // foreach ($storeExpressList as $i => $item) {
  1318. // foreach ($expressLst as $value) {
  1319. // if ($value['name'] == $item['express']) {
  1320. // $newStoreExpressList[] = $item['express'];
  1321. // break;
  1322. // }
  1323. // }
  1324. // }
  1325. $newPublicExpressList = [];
  1326. foreach ($expressLst as $i => $item) {
  1327. $newPublicExpressList[] = $item['name'];
  1328. }
  1329. return $newPublicExpressList;
  1330. }
  1331. public function orderGoodsCancelList() {
  1332. $status = $this->status;
  1333. $order_no = $this->order_no;
  1334. $goods_name = $this->goods_name;
  1335. $mobile = $this->mobile;
  1336. $dateStart = $this->dateStart;
  1337. $dateEnd = $this->dateEnd;
  1338. $md_name = $this->md_name;
  1339. $order_type = $this->order_type;
  1340. $platform = $this->platform;
  1341. $user_name = $this->user_name;
  1342. $name = $this->name;
  1343. // $activity_type = $this->activity_type;
  1344. $province_id = $this->province_id;
  1345. $district_id = $this->district_id;
  1346. $city_id = $this->city_id;
  1347. $cat_id = $this->cat_id;
  1348. $query = OrderGoodsCancel::find()->alias('ogc')
  1349. ->leftJoin(['o' => Order::tableName()], 'ogc.order_id = o.id')
  1350. ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
  1351. ->leftJoin(['od' => OrderDetail::tableName()], 'ogc.order_detail_id = od.id')
  1352. ->where(['IS NOT', 'o.id', null]);
  1353. if (isset($status) && in_array($status, [
  1354. OrderGoodsCancel::STATUS_APPLY,
  1355. OrderGoodsCancel::STATUS_PASS,
  1356. OrderGoodsCancel::STATUS_REFUSE,
  1357. ])) {
  1358. if (intval($status) === OrderGoodsCancel::STATUS_APPLY) {
  1359. $query->andWhere(['ogc.status' => [
  1360. OrderGoodsCancel::STATUS_APPLY,
  1361. OrderGoodsCancel::STATUS_PAY_FAIL
  1362. ]]);
  1363. } elseif (intval($status) === OrderGoodsCancel::STATUS_PASS) {
  1364. $query->andWhere(['ogc.status' => [
  1365. OrderGoodsCancel::STATUS_PASS,
  1366. OrderGoodsCancel::STATUS_PAY
  1367. ]]);
  1368. } else {
  1369. $query->andWhere(['ogc.status' => [
  1370. OrderGoodsCancel::STATUS_REFUSE,
  1371. OrderGoodsCancel::STATUS_CANCEL
  1372. ]]);
  1373. }
  1374. }
  1375. if (trim($order_no)) {
  1376. $query->andWhere(['LIKE', 'o.order_no', trim($order_no)]);
  1377. }
  1378. if (trim($goods_name)) {
  1379. $query->andWhere(['LIKE', 'od.goods_name', trim($goods_name)]);
  1380. }
  1381. if (trim($mobile)) {
  1382. $query->andWhere(['OR', ['LIKE', 'o.mobile', $mobile], ['LIKE', 'u.binding', $mobile]]);
  1383. }
  1384. if ($dateStart) {
  1385. $dateStart = strtotime($dateStart);
  1386. $query->andWhere(['>=', 'o.created_at', $dateStart]);
  1387. }
  1388. if ($dateEnd) {
  1389. $dateEnd = strtotime($dateEnd);
  1390. $query->andWhere(['<=', 'o.created_at', $dateEnd]);
  1391. }
  1392. if ($md_name) {
  1393. $query->leftJoin(['md' => Md::tableName()], 'md.id=o.md_id')
  1394. ->andWhere(['like', 'md.name', $md_name]);
  1395. }
  1396. if (isset($order_type) && in_array($order_type, [0, 1, 2, 3, Order::ORDER_TYPE_WORKER, Order::ORDER_TYPE_INTEGRAL])) {
  1397. $query->andWhere([
  1398. 'o.order_type' => $this->order_type
  1399. ]);
  1400. }
  1401. if (!empty($platform)) {
  1402. $query->andWhere([
  1403. 'o.order_origin' => $this->platform
  1404. ]);
  1405. }
  1406. //用户昵称
  1407. if ($user_name) {
  1408. $query->andWhere([
  1409. 'LIKE', 'u.nickname', $user_name
  1410. ]);
  1411. }
  1412. //收货人
  1413. if ($name) {
  1414. $query->andWhere([
  1415. 'LIKE', 'o.name', $name
  1416. ]);
  1417. }
  1418. if (!empty($province_id)) {
  1419. $query->andWhere(['o.province_id' => $province_id]);
  1420. }
  1421. if (!empty($city_id)) {
  1422. $query->andWhere(['o.city_id' => $city_id]);
  1423. }
  1424. if (!empty($district_id)) {
  1425. $query->andWhere(['o.district_id' => $district_id]);
  1426. }
  1427. if($cat_id > 0){
  1428. // 获取所有绑定这个分类ID的商品ID
  1429. $goodsID = GoodsCat::find()->where(['is_delete' => 0, 'cat_id' => $cat_id])->select('goods_id')->column();
  1430. if(!empty($goodsID)){
  1431. $query->andWhere(['od.goods_id' => $goodsID]);
  1432. }
  1433. }
  1434. $refundQuery1 = OrderRefund::find()->alias('or')
  1435. ->select('or.status, or.order_id, or.created_at')
  1436. ->where(['or.store_id' => $this->store_id, 'or.is_delete' => 0]);
  1437. $refundQuery = (new Query())->from(['or' => $refundQuery1])->where('`or`.order_id=o.id')
  1438. ->select('or.status')
  1439. ->orderBy(['or.created_at' => SORT_DESC])
  1440. ->limit(1);
  1441. $query->groupBy('ogc.id')->orderBy('ogc.created_at desc')->select([
  1442. 'o.store_id',
  1443. 'o.user_id',
  1444. 'o.saas_id',
  1445. 'o.order_no',
  1446. 'o.pay_price',
  1447. 'o.balance',
  1448. 'o.arrival_time',
  1449. 'o.express_price',
  1450. 'o.name',
  1451. 'o.mobile',
  1452. 'o.address',
  1453. 'o.remark',
  1454. 'o.is_pay',
  1455. 'o.pay_type',
  1456. 'o.pay_time',
  1457. 'o.send_time',
  1458. 'o.express',
  1459. 'o.express_no',
  1460. 'o.confirm_time',
  1461. 'o.apply_delete',
  1462. 'o.is_delete',
  1463. 'o.coupon_sub_price',
  1464. 'o.is_offline',
  1465. 'o.words',
  1466. 'o.rebate',
  1467. 'o.seller_comments',
  1468. 'o.mch_id',
  1469. 'o.order_origin',
  1470. 'o.is_recycle',
  1471. 'o.supplier_id',
  1472. 'o.trade_status',
  1473. 'o.combine_money',
  1474. 'o.order_type',
  1475. 'o.book_info',
  1476. 'o.is_delivery',
  1477. 'o.delivery_time',
  1478. 'o.md_id',
  1479. 'o.is_trans',
  1480. 'o.food_book_ext',
  1481. 'o.pay_user_id',
  1482. 'o.food_flag_id',
  1483. 'o.food_code',
  1484. 'o.get_verify_id',
  1485. 'o.trans_status',
  1486. 'o.trans_error',
  1487. 'o.rand_discount',
  1488. 'o.id as order_id',
  1489. 'o.integral',
  1490. 'o.give_integral'
  1491. ])->addSelect(['u.nickname', 'u.platform', 'u.binding', 'u.avatar_url as avatar',
  1492. 'refund' => $refundQuery, 'od.delivery_type', 'od.attr', 'o.name user_name', 'o.mobile user_mobile', 'o.address user_address',
  1493. 'od.order_transit_id', 'ogc.status', 'ogc.id as cancel_order_id', 'ogc.refund_price as cancel_refund_price', 'ogc.num as cancel_num', 'ogc.order_detail_id as cancel_order_detail_id', 'ogc.created_at']);
  1494. $list = pagination_make($query);
  1495. foreach ($list['list'] as &$item) {
  1496. if (!empty($item['food_flag_id'])) {
  1497. $food_flag = FoodFlag::findOne($item['food_flag_id']);
  1498. $item['food_table_num'] = $food_flag->table_num;
  1499. }
  1500. $item['status'] = intval($item['status']);
  1501. $item['status_text'] = OrderGoodsCancel::$status_text[$item['status']];
  1502. if ($item['status'] === OrderGoodsCancel::STATUS_PAY) {
  1503. $item['status'] = 1;
  1504. }
  1505. if ($item['status'] === OrderGoodsCancel::STATUS_PAY_FAIL) {
  1506. $item['status'] = 0;
  1507. }
  1508. if ($item['status'] === OrderGoodsCancel::STATUS_CANCEL) {
  1509. $item['status'] = 2;
  1510. }
  1511. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  1512. $item['goods_list'] = $this->getOrderGoodsList($item['order_id'], $item['cancel_order_detail_id']);
  1513. $item['md_info'] = null;
  1514. if ($item['md_id']) {
  1515. $item['md_info'] = Md::find()->where(['id' => $item['md_id']])
  1516. ->select('id, name, address, cover_url, start_time, end_time, contact, mobile, open_status, shop_time_type, is_single, manager, cat_id')->asArray()->one();
  1517. $managerUser = SaasUser::findOne($item['md_info']['manager']);
  1518. $item['md_info']['manager_name'] = $managerUser ? $managerUser->name : '-';
  1519. $item['md_info']['manager_mobile'] = $managerUser ? $managerUser->mobile : '-';
  1520. $mdCat = MdCategory::findOne($item['md_info']['cat_id']);
  1521. $item['md_info']['md_cat_name'] = $mdCat ? $mdCat->name : '-';
  1522. }
  1523. $item['pay_user_info'] = null;
  1524. if ($item['pay_user_id'] > 0) {
  1525. $pay_saas_user = SaasUser::findOne(['mobile' => User::findOne($item['pay_user_id'])->binding]);
  1526. $item['pay_user_info'] = [
  1527. 'name' => $pay_saas_user->name,
  1528. 'mobile' => $pay_saas_user->mobile
  1529. ];
  1530. }
  1531. $item['integral'] = json_decode($item['integral'], true);
  1532. $item['all_discount'] = sprintf("%.2f", ($item['integral']['forehead'] + $item['coupon_sub_price']));
  1533. if ($item['store_id']) {
  1534. // 增加查询商城的联系电话
  1535. $item['store'] = Store::find()->where(['id' => $item['store_id']])->select('id, name, province_id, city_id, district_id, address,contact_tel')->one();
  1536. }
  1537. $diy_shop_name = Option::get(OptionSetting::DIY_SHOP_NAME, $item['store_id'], 'store', '自提配送')['value'] ?: '自提配送';
  1538. $diy_shop_name = Option::get(OptionSetting::DIY_SHOP_NAME, $item['store_id'], 'pay', $diy_shop_name)['value'];
  1539. $diy_express_name = Option::get(OptionSetting::DIY_EXPRESS_NAME, $item['store_id'], 'store', '快递配送')['value'] ?: '快递配送';
  1540. $diy_express_name = Option::get(OptionSetting::DIY_EXPRESS_NAME, $item['store_id'], 'pay', $diy_express_name)['value'];
  1541. $diy_delivery_name = Option::get(OptionSetting::DIY_DELIVERY_NAME, $item['store_id'], 'store', '同城配送')['value'] ?: '同城配送';
  1542. $diy_delivery_name = Option::get(OptionSetting::DIY_DELIVERY_NAME, $item['store_id'], 'pay', $diy_delivery_name)['value'];
  1543. $item['diy_express_name'] = intval($item['is_offline']) === 1 ? $diy_shop_name : (intval($item['is_delivery']) === 1 ? $diy_delivery_name : $diy_express_name) ;
  1544. if (empty($item['address']) && (int)$item['is_offline'] && !$item['mch_id']) {
  1545. if (!empty($item['store'])) {
  1546. $item['address'] = '配送到门店:' . $item['store']['name'];
  1547. }
  1548. if (!empty($item['md_info'])) {
  1549. $item['address'] = '配送到门店:' . $item['md_info']['name'];
  1550. }
  1551. }
  1552. if($this->order_id > 0){
  1553. $item['pay_type_name'] = Order::PAY_TYP_NAME[$item['pay_type']];
  1554. // 优惠明细 目前优惠有两种 优惠券优惠 和 积分抵扣
  1555. $item['discount_text'] = '';
  1556. if($item['integral']['forehead'] > 0 && $item['integral']['forehead_integral'] > 0){
  1557. $item['discount_text'] .= '使用'.$item['integral']['forehead_integral'].'积分抵扣'.$item['integral']['forehead'].'元;';
  1558. }
  1559. if($item['coupon_sub_price'] > 0){
  1560. $item['discount_text'] .= '使用优惠券抵扣'.$item['coupon_sub_price'].'元;';
  1561. }
  1562. }
  1563. $item['is_front_delivery'] = 0;
  1564. $integral = 0;
  1565. foreach($item['goods_list'] as $goodskey=>&$goods_info){
  1566. if (isset($goods_info['is_front_delivery'])) {
  1567. $item['is_front_delivery'] = $goods_info['is_front_delivery'];
  1568. }
  1569. $goods_info['num'] = $item['cancel_num'];
  1570. $goods_info['total_price'] = $item['cancel_refund_price'];
  1571. }
  1572. if ((int)$item['is_pay'] !== 0 || intval($item['pay_type']) === 2) {
  1573. // $item['verify_card'] = $verify_card;
  1574. $cards = [];
  1575. if ($item['get_verify_id']) {
  1576. $get_verify_id = explode(',', $item['get_verify_id']);
  1577. $VerifyCardSale = VerifyCardSale::find()->alias('vcs')->where([
  1578. 'vcs.id' => $get_verify_id,
  1579. ])
  1580. ->leftJoin(['vc' => VerifyCard::tableName()], 'vcs.verify_card_id = vc.id')
  1581. ->leftJoin(['vca' => VerifyCardAccount::tableName()], 'vcs.account_id = vca.id')
  1582. ->select('vca.account, vc.begin_time, vc.bg_pic_url, vc.business_status, vc.business_type, vc.content
  1583. , vc.created_at, vc.date_type, vc.end_time, vc.expire_day, vc.freight_id, vcs.id, vc.is_business
  1584. , vc.is_give, vc.money, vc.name, vc.num, vca.password, vc.pic_url, vc.price, vcs.id sale_id, vcs.status sale_status
  1585. , vc.savingsType, vc.sort, vc.status, vc.total_num, vc.total_price, vc.type, vcs.use_num, vc.video_ids
  1586. , vcs.video_status, vcs.goods_id')->asArray()->all();
  1587. if ($VerifyCardSale) {
  1588. $cards = $VerifyCardSale;
  1589. }
  1590. }
  1591. $item['verify_card'] = $cards;
  1592. // $item['integral'] = $integral;
  1593. }
  1594. if($item['confirm_time'] && !$item['send_time']){
  1595. $item['send_time'] = $item['confirm_time'];
  1596. }
  1597. }
  1598. $f = new ExportList();
  1599. $exportList = $f->getList();
  1600. if($this->export){
  1601. return $this->export($list['list']);
  1602. }
  1603. return [
  1604. 'code' => 0,
  1605. 'msg' => 'success',
  1606. 'data' => [
  1607. 'export_list' => $exportList,
  1608. // 'express_list' => $this->getExpressList(),
  1609. 'data' => $list['list'],
  1610. 'pageNo' => $list['pageNo'],
  1611. 'pageSize' => $list['pageSize'],
  1612. 'totalCount' => $list['totalCount'],
  1613. 'print_setting' => Option::getPrintOrderSetting()
  1614. ],
  1615. ];
  1616. }
  1617. }