StatSaleClass.php 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatCgClass;
  4. use biz\stat\classes\StatStockInClass;
  5. use biz\stat\classes\StatStockOutClass;
  6. use bizGhs\cg\classes\CgRefundClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\expend\classes\ExpendClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\order\classes\OrderItemClass;
  11. use bizGhs\order\classes\PurchaseOrderClass;
  12. use bizGhs\order\classes\PurchaseOrderItemClass;
  13. use bizGhs\order\classes\RefundOrderClass;
  14. use bizGhs\order\classes\StockWastageOrderClass;
  15. use bizGhs\product\classes\ProductClass;
  16. use bizGhs\staff\classes\SalaryClass;
  17. use bizHd\goods\classes\PlantCgClass;
  18. use bizHd\order\classes\OrderClass as HdOrderClass;
  19. use bizGhs\order\classes\OrderClass;
  20. use bizHd\purchase\classes\PurchaseClass;
  21. use bizHd\refund\classes\HdRefundClass;
  22. use common\components\arrayUtil;
  23. use common\components\dateUtil;
  24. use common\components\dict;
  25. use common\components\noticeUtil;
  26. use common\components\util;
  27. use Yii;
  28. use bizGhs\base\classes\BaseClass;
  29. class StatSaleClass extends BaseClass
  30. {
  31. public static $baseFile = '\bizGhs\stat\models\StatSale';
  32. public static function wastage($mainId)
  33. {
  34. $get = Yii::$app->request->get();
  35. $where = [];
  36. $where['mainId'] = $mainId;
  37. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  38. $startTime = $get['startTime'] ?? '';
  39. $endTime = $get['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  41. $start = $period['startTime'];
  42. $end = $period['endTime'];
  43. $currentStartDate = date('Y-m-d', strtotime($start));
  44. $currentEndDate = date('Y-m-d', strtotime($end));
  45. $currentStartTime = $currentStartDate . ' 00:00:00';
  46. $currentEndTime = $currentEndDate . ' 23:59:59';
  47. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  48. $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
  49. $totalPrice = 0;
  50. if (!empty($list)) {
  51. foreach ($list as $order) {
  52. $price = $order['price'] ?? 0;
  53. $totalPrice = bcadd($totalPrice, $price, 2);
  54. }
  55. }
  56. $totalPrice = floatval($totalPrice);
  57. return ['totalPrice' => $totalPrice];
  58. }
  59. //结账跟踪 ssh 20230503
  60. public static function OrderClear($mainId)
  61. {
  62. $get = Yii::$app->request->get();
  63. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  64. $timeStyle = $get['timeStyle'] ?? 0;
  65. if (isset($get['searchTime']) && !empty($get['searchTime'])) {
  66. $searchTime = $get['searchTime'];
  67. $startTime = $get['startTime'] ?? '';
  68. $endTime = $get['endTime'] ?? '';
  69. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  70. $start = $period['startTime'];
  71. $end = $period['endTime'];
  72. $currentStartDate = date('Y-m-d', strtotime($start));
  73. $currentEndDate = date('Y-m-d', strtotime($end));
  74. $currentStartTime = $currentStartDate . ' 00:00:00';
  75. $currentEndTime = $currentEndDate . ' 23:59:59';
  76. if ($timeStyle == 0) {
  77. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  78. } else {
  79. $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
  80. }
  81. } else {
  82. $payTime = 'today';
  83. $startTime = $get['startTime'] ?? '';
  84. $endTime = $get['endTime'] ?? '';
  85. $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
  86. $start = $period['startTime'];
  87. $end = $period['endTime'];
  88. $currentStartDate = date('Y-m-d', strtotime($start));
  89. $currentEndDate = date('Y-m-d', strtotime($end));
  90. $currentStartTime = $currentStartDate . ' 00:00:00';
  91. $currentEndTime = $currentEndDate . ' 23:59:59';
  92. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  93. }
  94. $customId = $get['customId'] ?? 0;
  95. if (!empty($customId)) {
  96. $where['customId'] = $customId;
  97. }
  98. $unClear = $get['unClear'] ?? 0;
  99. if ($unClear == 1) {
  100. $where['clearId'] = 0;
  101. $where['remainDebtPrice>'] = 0;
  102. }
  103. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  104. return ['list' => $ghsOrderList];
  105. }
  106. //客户下单榜 ssh 20230502
  107. public static function statCustomCg($mainId)
  108. {
  109. $get = Yii::$app->request->get();
  110. $where = [];
  111. $where['mainId'] = $mainId;
  112. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  113. $startTime = $get['startTime'] ?? '';
  114. $endTime = $get['endTime'] ?? '';
  115. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  116. $start = $period['startTime'];
  117. $end = $period['endTime'];
  118. $where['time'] = ['between', [$start, $end]];
  119. $currentStartDate = date('Y-m-d', strtotime($start));
  120. $currentEndDate = date('Y-m-d', strtotime($end));
  121. $currentStartTime = $currentStartDate . ' 00:00:00';
  122. $currentEndTime = $currentEndDate . ' 23:59:59';
  123. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  124. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  125. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
  126. $arr = [];
  127. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  128. $totalAmount = 0;
  129. $totalNum = 0;
  130. if (!empty($ghsOrderList)) {
  131. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  132. $actPrice = $ghsOrder['actPrice'] ?? 0;
  133. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  134. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  135. $customId = $ghsOrder['customId'] ?? 0;
  136. $status = $ghsOrder['status'] ?? 0;
  137. $book = $ghsOrder['book'] ?? 0;
  138. if ($status == 1 || $status == 5) {
  139. continue;
  140. }
  141. if ($book == 1 && $status == 2) {
  142. continue;
  143. }
  144. $customName = $customList[$customId]['name'] ?? '';
  145. $py = $customList[$customId]['py'] ?? '';
  146. $totalNum++;
  147. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  148. if (isset($arr[$customId])) {
  149. $arr[$customId]['num']++;
  150. $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
  151. $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
  152. $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
  153. } else {
  154. $arr[$customId] = [
  155. 'customId' => $customId,
  156. 'customName' => $customName,
  157. 'py' => $py,
  158. 'num' => 1,
  159. 'amount' => $actPrice,
  160. 'levelName' => '',
  161. 'orderPrice' => $orderPrice,
  162. 'tkPrice' => $tkPrice,
  163. ];
  164. }
  165. }
  166. $arr = arrayUtil::arraySort($arr, 'amount');
  167. }
  168. $totalAmount = floatval($totalAmount);
  169. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum];
  170. }
  171. public static function profile($mainId)
  172. {
  173. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  174. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  175. $get = Yii::$app->request->get();
  176. $where = [];
  177. $where['mainId'] = $mainId;
  178. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  179. $startTime = $get['startTime'] ?? '';
  180. $endTime = $get['endTime'] ?? '';
  181. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  182. $start = $period['startTime'];
  183. $end = $period['endTime'];
  184. $where['time'] = ['between', [$start, $end]];
  185. $currentStartDate = date('Y-m-d', strtotime($start));
  186. $currentEndDate = date('Y-m-d', strtotime($end));
  187. $currentStartTime = $currentStartDate . ' 00:00:00';
  188. $currentEndTime = $currentEndDate . ' 23:59:59';
  189. //出库
  190. $stockOut = StatStockOutClass::getSum($where);
  191. //采购入库
  192. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  193. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  194. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  195. $cg = 0;
  196. if (!empty($cgList)) {
  197. foreach ($cgList as $cgKey => $cgItem) {
  198. $actPrice = $cgItem['actPrice'] ?? 0;
  199. $paidPrice = $cgItem['paidPrice'] ?? 0;
  200. if ($actPrice <= 0) {
  201. // 如果$actPrice=0,表示这个采购单已经全部退款,不需要再统计成本,本来$paidPrice要同步清零,但没有,这个问题由徐州邂逅花坊马总,提出的需求发现
  202. $cg = bcadd($cg, 0, 2);
  203. } else {
  204. $total = bcadd($actPrice, $paidPrice, 2);
  205. //物流费用不算成本。意思是说,长短途运费+提货费+本地运费,这四项和打包费一起,参与到平摊花材运费成本的计算,但那四项不参与总的采购成本登记,因为,有另外地方会独立登记运费成本。
  206. //$paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  207. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  208. if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
  209. $shortCharge = $cgItem['shortCharge'] ?? 0;
  210. $longCharge = $cgItem['longCharge'] ?? 0;
  211. $pickCharge = $cgItem['pickCharge'] ?? 0;
  212. $localCharge = $cgItem['localCharge'] ?? 0;
  213. $a1 = bcadd($shortCharge, $longCharge, 2);
  214. $a2 = bcadd($pickCharge, $localCharge, 2);
  215. $a = bcadd($a1, $a2, 2);
  216. //注意下面是减号 bcsub
  217. $total = bcsub($total, $a, 2);
  218. }
  219. $cg = bcadd($cg, $total, 2);
  220. }
  221. }
  222. }
  223. $cg = floatval($cg);
  224. $lsCgWhere = ['mainId' => $mainId, 'status' => 4, 'belongCost' => 1];
  225. $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  226. $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
  227. $lsCgAmount = 0;
  228. if (!empty($lsCgList)) {
  229. foreach ($lsCgList as $lsCgItem) {
  230. $actPrice = $lsCgItem['actPrice'] ?? 0;
  231. $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
  232. }
  233. }
  234. $lsCgAmount = floatval($lsCgAmount);
  235. $lzWhere = ['mainId' => $mainId, 'status' => 3];
  236. $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  237. $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
  238. $lzCgAmount = 0;
  239. if (!empty($lzCgList)) {
  240. foreach ($lzCgList as $lzInfo) {
  241. $actPrice = $lzInfo['actPrice'] ?? 0;
  242. $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
  243. }
  244. }
  245. $lzCgAmount = floatval($lzCgAmount);
  246. //调拨入库
  247. $stockIn = StatStockInClass::getSum($where);
  248. $totalOrderNum = 0;
  249. //批发开单
  250. $pfIncome = 0;
  251. $pfSendCost = 0;
  252. $pfPack = 0;
  253. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  254. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  255. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  256. if (!empty($ghsOrderList)) {
  257. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  258. $actPrice = $ghsOrder['actPrice'] ?? 0;
  259. $sendCost = $ghsOrder['sendCost'] ?? 0;
  260. $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
  261. $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
  262. $packCost = $ghsOrder['packCost'] ?? 0;
  263. $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
  264. $remainPackCost = bcsub($packCost, $refundPackCost, 2);
  265. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  266. $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
  267. $pfPack = bcadd($pfPack, $remainPackCost, 2);
  268. $totalOrderNum++;
  269. }
  270. }
  271. //零售开单
  272. $payCodeIncome = 0;
  273. $lsIncome = 0;
  274. $lsSendCost = 0;
  275. $lsServiceFee = 0;
  276. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  277. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  278. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  279. foreach ($hdOrderList as $hdKey => $hdOrder) {
  280. $actPrice = $hdOrder['mainPay'] ?? 0;
  281. $onlinePay = $hdOrder['onlinePay'] ?? 1;
  282. $repeat = $hdOrder['repeat'] ?? 0;
  283. $forward = $hdOrder['forward'] ?? 0;
  284. if ($forward == 1) {
  285. //售后付款不属于收入
  286. continue;
  287. }
  288. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  289. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  290. }
  291. $cash = $hdOrder['cash'] ?? 0;
  292. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  293. $sendCost = $hdOrder['sendCost'] ?? 0;
  294. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  295. $lsIncome = bcadd($lsIncome, $cash, 2);
  296. //退款取消先不计算成本
  297. if ($actPrice > 0) {
  298. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  299. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  300. }
  301. $totalOrderNum++;
  302. if ($onlinePay == 2 && floatval($cash) <= 0 && $repeat == 1) {
  303. $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
  304. $payCodeIncome = floatval($payCodeIncome);
  305. }
  306. }
  307. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  308. $salary = 0;
  309. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  310. if (!empty($salaryList)) {
  311. foreach ($salaryList as $item) {
  312. $salary = bcadd($salary, $item->amount, 2);
  313. }
  314. }
  315. $condition = [
  316. 'mainId' => $mainId,
  317. 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
  318. 'delStatus' => 0,
  319. ];
  320. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  321. $expendTypeMap = dict::getDict('expendTypeMap');
  322. $expendAmount = [];
  323. if (!empty($expendList)) {
  324. foreach ($expendList as $item) {
  325. $type = $item->type ?? 0;
  326. $amount = $item->amount ?? 0;
  327. $name = $expendTypeMap[$type] ?? '暂无';
  328. if (isset($expendAmount[$type]['amount']) == false) {
  329. $expendAmount[$type]['amount'] = 0;
  330. }
  331. $expendAmount[$type]['name'] = $name;
  332. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  333. }
  334. }
  335. $income = [
  336. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  337. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  338. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  339. ];
  340. $expend = [
  341. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  342. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  343. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  344. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  345. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  346. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  347. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  348. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  349. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  350. ];
  351. if (!empty($expendAmount)) {
  352. foreach ($expendAmount as $it) {
  353. $expend[] = $it;
  354. }
  355. }
  356. $balance = 0;
  357. foreach ($income as $it) {
  358. $amount = $it['amount'] ?? 0;
  359. $balance = bcadd($amount, $balance, 2);
  360. }
  361. foreach ($expend as $it) {
  362. $amount = $it['amount'] ?? 0;
  363. $balance = bcsub($balance, $amount, 2);
  364. }
  365. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum, 'payCodeIncome' => $payCodeIncome];
  366. }
  367. //按花材统计收入 ssh 20231111
  368. public static function incomeOutItem($mainId)
  369. {
  370. $get = Yii::$app->request->get();
  371. $export = $get['export'] ?? 0;
  372. $where = [];
  373. $where['mainId'] = $mainId;
  374. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  375. $startTime = $get['startTime'] ?? '';
  376. $endTime = $get['endTime'] ?? '';
  377. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  378. $start = $period['startTime'];
  379. $end = $period['endTime'];
  380. $where['time'] = ['between', [$start, $end]];
  381. $currentStartDate = date('Y-m-d', strtotime($start));
  382. $currentEndDate = date('Y-m-d', strtotime($end));
  383. $currentStartTime = $currentStartDate . ' 00:00:00';
  384. $currentEndTime = $currentEndDate . ' 23:59:59';
  385. $s = strtotime($currentStartTime);
  386. $e = strtotime($currentEndTime);
  387. $p = bcsub($e, $s);
  388. $xx = bcmul(86400, 90);
  389. if ($p > $xx) {
  390. util::fail('查询时间不能超过三个月');
  391. }
  392. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  393. if (empty($productList)) {
  394. util::fail('没有找到花材');
  395. }
  396. $arr = [];
  397. //打包费和运费
  398. $packFreight = 0;
  399. $onlyRefundCg = 0;
  400. $onlyRefund = 0;
  401. $totalDiscountAmount = 0;
  402. $totalLabourCost = 0;
  403. //采购入库
  404. $tbCgIncome = [];
  405. $tbCgIncomeStaff = [];
  406. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  407. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  408. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  409. if (!empty($cgList)) {
  410. foreach ($cgList as $cgKey => $cgItem) {
  411. $packingCharge = $cgItem['packingCharge'] ?? 0;
  412. $shortCharge = $cgItem['shortCharge'] ?? 0;
  413. $longCharge = $cgItem['longCharge'] ?? 0;
  414. $pickCharge = $cgItem['pickCharge'] ?? 0;
  415. $localCharge = $cgItem['localCharge'] ?? 0;
  416. $packFreight = bcadd($packFreight, $packingCharge, 2);
  417. $packFreight = bcadd($packFreight, $shortCharge, 2);
  418. $packFreight = bcadd($packFreight, $longCharge, 2);
  419. $packFreight = bcadd($packFreight, $pickCharge, 2);
  420. $packFreight = bcadd($packFreight, $localCharge, 2);
  421. $tkPrice = $cgItem['tkPrice'] ?? 0;
  422. $orderSn = $cgItem['orderSn'] ?? '';
  423. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  424. if (!empty($cgItemInfoList)) {
  425. foreach ($cgItemInfoList as $cgData) {
  426. $itemNum = $cgData->itemNum ?? 0;
  427. $productId = $cgData->productId ?? 0;
  428. $refundNum = $cgData->refundNum ?? 0;
  429. $remainNum = bcsub($itemNum, $refundNum);
  430. $bigPrice = $cgData->bigPrice ?? 0;
  431. $amount = bcmul($remainNum, $bigPrice, 2);
  432. $itemName = $productList[$productId]['name'] ?? '已删除';
  433. $arr[$productId]['name'] = $itemName;
  434. $arr[$productId]['id'] = $productId;
  435. if (isset($arr[$productId]['expend'])) {
  436. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  437. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  438. } else {
  439. $arr[$productId]['expend'] = $amount;
  440. $arr[$productId]['outNum'] = $remainNum;
  441. $arr[$productId]['inNum'] = 0;
  442. $arr[$productId]['income'] = 0;
  443. }
  444. }
  445. if ($tkPrice > 0) {
  446. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  447. if (!empty($cgRefundOrderList)) {
  448. foreach ($cgRefundOrderList as $cgRefundOrder) {
  449. if ($cgRefundOrder->refundType == 2) {
  450. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  451. }
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. //批发开单
  459. $pfIncome = 0;
  460. $tbSaleIncome = [];
  461. $tbSaleIncomeStaff = [];
  462. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  463. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  464. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  465. if (!empty($ghsOrderList)) {
  466. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  467. $book = $ghsOrder['book'] ?? 0;
  468. $status = $ghsOrder['status'] ?? 0;
  469. if ($book == 1 && $status == 2) {
  470. //预订单待发货的不算收入
  471. continue;
  472. }
  473. if ($status == 0 || $status == 5) {
  474. continue;
  475. }
  476. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  477. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  478. $orderSn = $ghsOrder['orderSn'] ?? '';
  479. $actPrice = $ghsOrder['actPrice'] ?? 0;
  480. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  481. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  482. $labourCost = $ghsOrder['labourCost'] ?? 0;
  483. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  484. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  485. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  486. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  487. if (!empty($orderItemList)) {
  488. foreach ($orderItemList as $orderItem) {
  489. $refundNum = $orderItem->refundNum ?? 0;
  490. $name = $orderItem->name ?? '';
  491. $productId = $orderItem->productId ?? 0;
  492. $num = $orderItem->xhNum ?? 0;
  493. $remainNum = bcsub($num, $refundNum);
  494. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  495. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  496. $itemName = $productList[$productId]['name'] ?? '已删除';
  497. $arr[$productId]['name'] = $itemName;
  498. $arr[$productId]['id'] = $productId;
  499. if (isset($arr[$productId]['income'])) {
  500. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  501. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  502. } else {
  503. $arr[$productId]['expend'] = 0;
  504. $arr[$productId]['outNum'] = 0;
  505. $arr[$productId]['inNum'] = $remainNum;
  506. $arr[$productId]['income'] = $amount;
  507. }
  508. //单独统计
  509. $belongCost = $orderItem->belongCost ?? 0;
  510. if ($belongCost == 1) {
  511. if (isset($tbSaleIncome[$productId])) {
  512. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  513. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  514. } else {
  515. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  516. }
  517. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  518. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  519. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  520. } else {
  521. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  522. }
  523. }
  524. }
  525. if ($tkPrice > 0) {
  526. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  527. if (!empty($refundList)) {
  528. foreach ($refundList as $refund) {
  529. if ($refund->refundType == 1) {
  530. //退货退款前面已经考虑,这里不再处理 OK
  531. } else {
  532. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  533. }
  534. }
  535. }
  536. }
  537. }
  538. }
  539. }
  540. //零售开单
  541. $lsIncome = 0;
  542. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  543. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  544. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  545. foreach ($hdOrderList as $hdKey => $hdOrder) {
  546. $actPrice = $hdOrder['mainPay'] ?? 0;
  547. $cash = $hdOrder['cash'] ?? 0;
  548. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  549. $lsIncome = bcadd($lsIncome, $cash, 2);
  550. $orderSn = $hdOrder['orderSn'] ?? '';
  551. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  552. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  553. if (!empty($orderItemList)) {
  554. foreach ($orderItemList as $orderItem) {
  555. $unitPrice = $orderItem->unitPrice ?? 0;
  556. $refundNum = $orderItem->refundNum ?? 0;
  557. $num = $orderItem->num ?? 0;
  558. $productId = $orderItem->itemId ?? 0;
  559. $remainNum = bcsub($num, $refundNum);
  560. $amount = bcmul($remainNum, $unitPrice, 2);
  561. $itemName = $productList[$productId]['name'] ?? '已删除';
  562. $arr[$productId]['name'] = $itemName;
  563. $arr[$productId]['id'] = $productId;
  564. if (isset($arr[$productId]['income'])) {
  565. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  566. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  567. } else {
  568. $arr[$productId]['expend'] = 0;
  569. $arr[$productId]['outNum'] = 0;
  570. $arr[$productId]['inNum'] = $remainNum;
  571. $arr[$productId]['income'] = $amount;
  572. }
  573. }
  574. if ($tkPrice > 0) {
  575. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  576. if (!empty($refundList)) {
  577. foreach ($refundList as $refund) {
  578. if ($refund->refundType == 2) {
  579. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  580. }
  581. }
  582. }
  583. }
  584. }
  585. }
  586. $totalIncome = 0;
  587. $totalExpend = 0;
  588. if (!empty($arr)) {
  589. foreach ($arr as $key => $val) {
  590. $income = $val['income'] ?? 0;
  591. $income = floatval($income);
  592. $totalIncome = bcadd($totalIncome, $income, 2);
  593. $expend = $val['expend'] ?? 0;
  594. $expend = floatval($expend);
  595. $totalExpend = bcadd($totalExpend, $expend, 2);
  596. $profit = bcsub($income, $expend, 2);
  597. $arr[$key]['profit'] = floatval($profit);
  598. $arr[$key]['income'] = floatval($income);
  599. $arr[$key]['expend'] = floatval($expend);
  600. }
  601. }
  602. //总成本算上物流打包等费用
  603. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  604. //总收入算上采购仅退款
  605. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  606. //总收入加上人工费
  607. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  608. //总收入减掉优惠金额
  609. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  610. //总成本算上销售仅退款
  611. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  612. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  613. $totalIncome = floatval($totalIncome);
  614. $totalExpend = floatval($totalExpend);
  615. $totalProfit = floatval($totalProfit);
  616. $arr = array_values($arr);
  617. $arr = arrayUtil::arraySort($arr, 'profit');
  618. if ($export == 1) {
  619. $statData = [
  620. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  621. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  622. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  623. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  624. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  625. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  626. ];
  627. $list = array_merge($arr, $statData);
  628. self::exportIncomeOutItem($list, $mainId);
  629. }
  630. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  631. 'totalIncome' => $totalIncome,
  632. 'totalExpend' => $totalExpend,
  633. 'totalProfit' => $totalProfit,
  634. 'tbSaleIncome' => $tbSaleIncome,
  635. 'tbCgIncome' => $tbCgIncome,
  636. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  637. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  638. 'totalLabourCost' => $totalLabourCost,
  639. 'totalDiscountAmount' => $totalDiscountAmount,
  640. ]);
  641. }
  642. //按分类统计收入 ssh 20231111
  643. public static function classProfile($mainId)
  644. {
  645. $get = Yii::$app->request->get();
  646. $export = $get['export'] ?? 0;
  647. $where = [];
  648. $where['mainId'] = $mainId;
  649. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  650. $startTime = $get['startTime'] ?? '';
  651. $endTime = $get['endTime'] ?? '';
  652. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  653. $start = $period['startTime'];
  654. $end = $period['endTime'];
  655. $where['time'] = ['between', [$start, $end]];
  656. $currentStartDate = date('Y-m-d', strtotime($start));
  657. $currentEndDate = date('Y-m-d', strtotime($end));
  658. $currentStartTime = $currentStartDate . ' 00:00:00';
  659. $currentEndTime = $currentEndDate . ' 23:59:59';
  660. $s = strtotime($currentStartTime);
  661. $e = strtotime($currentEndTime);
  662. $p = bcsub($e, $s);
  663. $xx = bcmul(86400, 90);
  664. if ($p > $xx) {
  665. util::fail('查询时间不能超过三个月');
  666. }
  667. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  668. if (empty($classList)) {
  669. util::fail('没有找到花材');
  670. }
  671. $arr = [];
  672. //打包费和运费
  673. $packFreight = 0;
  674. $onlyRefundCg = 0;
  675. $onlyRefund = 0;
  676. $totalDiscountAmount = 0;
  677. $totalLabourCost = 0;
  678. //采购入库
  679. $tbCgIncome = [];
  680. $tbCgIncomeStaff = [];
  681. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  682. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  683. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  684. if (!empty($cgList)) {
  685. foreach ($cgList as $cgKey => $cgItem) {
  686. $packingCharge = $cgItem['packingCharge'] ?? 0;
  687. $shortCharge = $cgItem['shortCharge'] ?? 0;
  688. $longCharge = $cgItem['longCharge'] ?? 0;
  689. $pickCharge = $cgItem['pickCharge'] ?? 0;
  690. $localCharge = $cgItem['localCharge'] ?? 0;
  691. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  692. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  693. $actPrice = $cgItem['actPrice'] ?? 0;
  694. if ($actPrice <= 0) {
  695. continue;
  696. }
  697. $packFreight = bcadd($packFreight, $packingCharge, 2);
  698. $packFreight = bcadd($packFreight, $shortCharge, 2);
  699. $packFreight = bcadd($packFreight, $longCharge, 2);
  700. $packFreight = bcadd($packFreight, $pickCharge, 2);
  701. $packFreight = bcadd($packFreight, $localCharge, 2);
  702. $tkPrice = $cgItem['tkPrice'] ?? 0;
  703. $orderSn = $cgItem['orderSn'] ?? '';
  704. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  705. if (!empty($cgItemInfoList)) {
  706. foreach ($cgItemInfoList as $cgData) {
  707. $itemNum = $cgData->itemNum ?? 0;
  708. $productId = $cgData->productId ?? 0;
  709. $name = $cgData->name ?? '';
  710. $refundNum = $cgData->refundNum ?? 0;
  711. $remainNum = bcsub($itemNum, $refundNum);
  712. $bigPrice = $cgData->bigPrice ?? 0;
  713. $amount = bcmul($remainNum, $bigPrice, 2);
  714. $classId = $cgData->classId;
  715. $className = $classList[$classId]['name'] ?? '已删除';
  716. if ($className == '运费') {
  717. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  718. }
  719. $arr[$classId]['className'] = $className;
  720. $arr[$classId]['classId'] = $classId;
  721. if (isset($arr[$classId]['expend'])) {
  722. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  723. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  724. } else {
  725. $arr[$classId]['expend'] = $amount;
  726. $arr[$classId]['income'] = 0;
  727. $arr[$classId]['cgNum'] = $remainNum;
  728. $arr[$classId]['saleNum'] = 0;
  729. }
  730. //单独统计
  731. $belongCost = $cgData->belongCost ?? 0;
  732. if ($belongCost == 1) {
  733. if (isset($tbCgIncome[$productId])) {
  734. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  735. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  736. } else {
  737. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  738. }
  739. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  740. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  741. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  742. } else {
  743. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  744. }
  745. }
  746. }
  747. if ($tkPrice > 0) {
  748. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  749. if (!empty($cgRefundOrderList)) {
  750. foreach ($cgRefundOrderList as $cgRefundOrder) {
  751. if ($cgRefundOrder->refundType == 2) {
  752. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  753. }
  754. }
  755. }
  756. }
  757. }
  758. }
  759. }
  760. //批发开单
  761. $pfIncome = 0;
  762. $tbSaleIncome = [];
  763. $tbSaleIncomeStaff = [];
  764. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  765. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  766. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  767. if (!empty($ghsOrderList)) {
  768. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  769. $book = $ghsOrder['book'] ?? 0;
  770. $status = $ghsOrder['status'] ?? 0;
  771. if ($book == 1 && $status == 2) {
  772. //预订单待发货的不算收入
  773. continue;
  774. }
  775. if ($status == 0 || $status == 5) {
  776. continue;
  777. }
  778. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  779. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  780. $orderSn = $ghsOrder['orderSn'] ?? '';
  781. $actPrice = $ghsOrder['actPrice'] ?? 0;
  782. if ($actPrice <= 0) {
  783. continue;
  784. }
  785. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  786. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  787. $labourCost = $ghsOrder['labourCost'] ?? 0;
  788. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  789. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  790. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  791. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  792. if (!empty($orderItemList)) {
  793. foreach ($orderItemList as $orderItem) {
  794. $refundNum = $orderItem->refundNum ?? 0;
  795. $name = $orderItem->name ?? '';
  796. $productId = $orderItem->productId ?? 0;
  797. $num = $orderItem->xhNum ?? 0;
  798. $remainNum = bcsub($num, $refundNum);
  799. $classId = $orderItem->classId ?? 0;
  800. $className = $classList[$classId]['name'] ?? '已删除';
  801. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  802. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  803. $arr[$classId]['className'] = $className;
  804. $arr[$classId]['classId'] = $classId;
  805. if (isset($arr[$classId]['income'])) {
  806. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  807. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  808. } else {
  809. $arr[$classId]['expend'] = 0;
  810. $arr[$classId]['income'] = $amount;
  811. $arr[$classId]['saleNum'] = $remainNum;
  812. $arr[$classId]['cgNum'] = 0;
  813. }
  814. //单独统计
  815. $belongCost = $orderItem->belongCost ?? 0;
  816. if ($belongCost == 1) {
  817. if (isset($tbSaleIncome[$productId])) {
  818. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  819. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  820. } else {
  821. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  822. }
  823. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  824. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  825. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  826. } else {
  827. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  828. }
  829. }
  830. }
  831. if ($tkPrice > 0) {
  832. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  833. if (!empty($refundList)) {
  834. foreach ($refundList as $refund) {
  835. if ($refund->refundType == 1) {
  836. //退货退款前面已经考虑,这里不再处理 OK
  837. } else {
  838. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  839. }
  840. }
  841. }
  842. }
  843. }
  844. }
  845. }
  846. //零售开单
  847. $lsIncome = 0;
  848. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  849. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  850. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  851. foreach ($hdOrderList as $hdKey => $hdOrder) {
  852. $actPrice = $hdOrder['mainPay'] ?? 0;
  853. $cash = $hdOrder['cash'] ?? 0;
  854. if ($actPrice <= 0 && $cash <= 0) {
  855. continue;
  856. }
  857. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  858. $lsIncome = bcadd($lsIncome, $cash, 2);
  859. $orderSn = $hdOrder['orderSn'] ?? '';
  860. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  861. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  862. if (!empty($orderItemList)) {
  863. foreach ($orderItemList as $orderItem) {
  864. $classId = $orderItem->classId ?? 0;
  865. $unitPrice = $orderItem->unitPrice ?? 0;
  866. $refundNum = $orderItem->refundNum ?? 0;
  867. $num = $orderItem->num ?? 0;
  868. $productId = $orderItem->itemId ?? 0;
  869. $name = $orderItem->name ?? '';
  870. $remainNum = bcsub($num, $refundNum);
  871. $amount = bcmul($remainNum, $unitPrice, 2);
  872. $className = $classList[$classId]['name'] ?? '已删除';
  873. $arr[$classId]['className'] = $className;
  874. $arr[$classId]['classId'] = $classId;
  875. if (isset($arr[$classId]['income'])) {
  876. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  877. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  878. } else {
  879. $arr[$classId]['expend'] = 0;
  880. $arr[$classId]['income'] = $amount;
  881. $arr[$classId]['saleNum'] = $remainNum;
  882. $arr[$classId]['cgNum'] = 0;
  883. }
  884. $belongCost = $orderItem->belongCost ?? 0;
  885. if ($belongCost == 1) {
  886. if (isset($tbSaleIncome[$productId])) {
  887. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  888. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  889. } else {
  890. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  891. }
  892. }
  893. }
  894. if ($tkPrice > 0) {
  895. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  896. if (!empty($refundList)) {
  897. foreach ($refundList as $refund) {
  898. if ($refund->refundType == 2) {
  899. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  900. }
  901. }
  902. }
  903. }
  904. }
  905. }
  906. $totalIncome = 0;
  907. $totalExpend = 0;
  908. if (!empty($arr)) {
  909. foreach ($arr as $key => $val) {
  910. $income = $val['income'] ?? 0;
  911. $income = floatval($income);
  912. $totalIncome = bcadd($totalIncome, $income, 2);
  913. $expend = $val['expend'] ?? 0;
  914. $expend = floatval($expend);
  915. $totalExpend = bcadd($totalExpend, $expend, 2);
  916. $profit = bcsub($income, $expend, 2);
  917. $arr[$key]['profit'] = floatval($profit);
  918. $arr[$key]['income'] = floatval($income);
  919. $arr[$key]['expend'] = floatval($expend);
  920. }
  921. }
  922. //总成本算上物流打包等费用
  923. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  924. //总收入算上采购仅退款
  925. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  926. //总收入加上人工费
  927. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  928. //总收入减掉优惠金额
  929. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  930. //总成本算上销售仅退款
  931. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  932. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  933. $totalIncome = floatval($totalIncome);
  934. $totalExpend = floatval($totalExpend);
  935. $totalProfit = floatval($totalProfit);
  936. $arr = array_values($arr);
  937. $arr = arrayUtil::arraySort($arr, 'profit');
  938. if ($export == 1) {
  939. $statData = [
  940. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  941. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  942. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  943. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  944. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  945. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  946. ];
  947. $list = array_merge($arr, $statData);
  948. self::exportIncomeClass($list, $mainId);
  949. }
  950. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  951. 'totalIncome' => $totalIncome,
  952. 'totalExpend' => $totalExpend,
  953. 'totalProfit' => $totalProfit,
  954. 'tbSaleIncome' => $tbSaleIncome,
  955. 'tbCgIncome' => $tbCgIncome,
  956. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  957. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  958. 'totalLabourCost' => $totalLabourCost,
  959. 'totalDiscountAmount' => $totalDiscountAmount,
  960. ]);
  961. }
  962. public static function exportIncomeOutItem($list, $mainId)
  963. {
  964. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  965. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  966. $objPHPExcel = new \PHPExcel();
  967. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  968. ->setLastModifiedBy("Maarten Balliauw")
  969. ->setTitle("Office 2007 XLSX Document")
  970. ->setSubject("Office 2007 XLSX Document")
  971. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  972. ->setKeywords("office 2007 openxml php")
  973. ->setCategory("file");
  974. $ghsTitle = '供货商';
  975. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  976. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  977. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  978. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  979. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  980. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  981. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  982. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  983. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  984. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  985. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  986. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  987. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  988. //设置宽度
  989. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  990. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  991. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  992. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  993. //加粗
  994. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  995. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  996. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  997. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  998. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  999. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1000. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1001. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1002. $baseRow = 2;
  1003. foreach ($list as $key => $custom) {
  1004. $i = $baseRow + $key;
  1005. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1006. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  1007. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  1008. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  1009. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  1010. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1011. //居左
  1012. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1013. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1014. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1015. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1016. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1017. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1018. }
  1019. $fileName = '收支花材-' . date("m-d");
  1020. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1021. $objPHPExcel->setActiveSheetIndex(0);
  1022. $dir = './priceTable/' . $mainId;
  1023. if (file_exists($dir) == false) {
  1024. mkdir($dir, 0777, true);
  1025. }
  1026. $date = $fileName;
  1027. $file = $date . '.xls';
  1028. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1029. if (file_exists($dir . '/' . $file)) {
  1030. unlink($dir . '/' . $file);
  1031. }
  1032. $objWriter->save($dir . '/' . $file);
  1033. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1034. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1035. }
  1036. public static function exportIncomeClass($list, $mainId)
  1037. {
  1038. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1039. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1040. $objPHPExcel = new \PHPExcel();
  1041. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1042. ->setLastModifiedBy("Maarten Balliauw")
  1043. ->setTitle("Office 2007 XLSX Document")
  1044. ->setSubject("Office 2007 XLSX Document")
  1045. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1046. ->setKeywords("office 2007 openxml php")
  1047. ->setCategory("file");
  1048. $ghsTitle = '供货商';
  1049. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1050. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1051. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1052. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1053. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1054. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1055. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1056. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1057. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1058. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1059. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1060. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1061. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1062. //设置宽度
  1063. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1064. //加粗
  1065. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1066. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1067. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1068. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1069. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1070. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1071. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1072. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1073. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1074. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1075. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1076. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1077. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1078. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1079. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1080. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1081. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1082. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1083. $baseRow = 2;
  1084. foreach ($list as $key => $custom) {
  1085. $i = $baseRow + $key;
  1086. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1087. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1088. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1089. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1090. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1091. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1092. //居左
  1093. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1094. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1095. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1096. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1097. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1098. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1099. }
  1100. $fileName = '收支分类-' . time();
  1101. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1102. $objPHPExcel->setActiveSheetIndex(0);
  1103. $dir = './priceTable/' . $mainId;
  1104. if (file_exists($dir) == false) {
  1105. mkdir($dir, 0777, true);
  1106. }
  1107. $date = $fileName;
  1108. $file = $date . '.xls';
  1109. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1110. if (file_exists($dir . '/' . $file)) {
  1111. unlink($dir . '/' . $file);
  1112. }
  1113. $objWriter->save($dir . '/' . $file);
  1114. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1115. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1116. }
  1117. public static function exportItemSale($list, $mainId)
  1118. {
  1119. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1120. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1121. $objPHPExcel = new \PHPExcel();
  1122. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1123. ->setLastModifiedBy("Maarten Balliauw")
  1124. ->setTitle("Office 2007 XLSX Document")
  1125. ->setSubject("Office 2007 XLSX Document")
  1126. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1127. ->setKeywords("office 2007 openxml php")
  1128. ->setCategory("file");
  1129. $ghsTitle = '供货商';
  1130. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1131. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1132. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1133. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1134. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1135. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1136. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1137. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1138. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1139. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1140. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1141. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1142. //设置宽度
  1143. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1144. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1145. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1146. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1147. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1148. //加粗
  1149. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1150. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1151. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1152. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1153. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1154. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1155. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1156. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1157. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1158. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1159. $baseRow = 2;
  1160. foreach ($list as $key => $custom) {
  1161. $i = $baseRow + $key;
  1162. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1163. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1164. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1165. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1166. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1167. //居左
  1168. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1169. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1170. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1171. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1172. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1173. }
  1174. $fileName = '花材销量-' . date("m-d");
  1175. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1176. $objPHPExcel->setActiveSheetIndex(0);
  1177. $dir = './priceTable/' . $mainId;
  1178. if (file_exists($dir) == false) {
  1179. mkdir($dir, 0777, true);
  1180. }
  1181. $date = $fileName;
  1182. $file = $date . '.xls';
  1183. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1184. if (file_exists($dir . '/' . $file)) {
  1185. unlink($dir . '/' . $file);
  1186. }
  1187. $objWriter->save($dir . '/' . $file);
  1188. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1189. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1190. }
  1191. public static function send($mainId)
  1192. {
  1193. $get = Yii::$app->request->get();
  1194. $where = [];
  1195. $where['mainId'] = $mainId;
  1196. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1197. $startTime = $get['startTime'] ?? '';
  1198. $endTime = $get['endTime'] ?? '';
  1199. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1200. $start = $period['startTime'];
  1201. $end = $period['endTime'];
  1202. $where['time'] = ['between', [$start, $end]];
  1203. $currentStartDate = date('Y-m-d', strtotime($start));
  1204. $currentEndDate = date('Y-m-d', strtotime($end));
  1205. $currentStartTime = $currentStartDate . ' 00:00:00';
  1206. $currentEndTime = $currentEndDate . ' 23:59:59';
  1207. $s = strtotime($currentStartTime);
  1208. $e = strtotime($currentEndTime);
  1209. $p = bcsub($e, $s);
  1210. $xx = bcmul(86400, 90);
  1211. if ($p > $xx) {
  1212. util::fail('查询时间不能超过三个月');
  1213. }
  1214. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1215. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1216. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1217. $arr = [];
  1218. if (!empty($ghsOrderList)) {
  1219. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  1220. $book = $ghsOrder['book'] ?? 0;
  1221. $status = $ghsOrder['status'] ?? 0;
  1222. if ($book == 1 && $status == 2) {
  1223. //预订单待发货的不算收入
  1224. continue;
  1225. }
  1226. if ($status == 0 || $status == 5) {
  1227. continue;
  1228. }
  1229. $orderSn = $ghsOrder['orderSn'] ?? '';
  1230. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1231. if ($actPrice <= 0) {
  1232. continue;
  1233. }
  1234. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1235. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1236. if (isset($arr[$sendStaffId])) {
  1237. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1238. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1239. } else {
  1240. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1241. }
  1242. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1243. if (!empty($orderItemList)) {
  1244. foreach ($orderItemList as $orderItem) {
  1245. $refundNum = $orderItem->refundNum ?? 0;
  1246. $num = $orderItem->xhNum ?? 0;
  1247. $remainNum = bcsub($num, $refundNum);
  1248. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1249. }
  1250. }
  1251. }
  1252. }
  1253. if (!empty($arr)) {
  1254. $arr = array_values($arr);
  1255. }
  1256. return $arr;
  1257. }
  1258. }